From 02b7ec05d5713aec67577cebc18e5906b5a98598 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 22 Mar 2017 13:18:52 +0100 Subject: Be (somewhat more) consistent about the value of pi Use M_PI (and friends), where possible, in favor of hand-coded approximations of various (in)accuracies. Where that's not available (e.g. fragment shaders), use the same value that qmath.h uses for M_PI, for consistency. Replaced math.h with qmath.h in places that defined a fall-back in case math.h omits it (it's not in the C++ standard, although M_PI is in POSIX); or removed this entirely where it wasn't used. Reworked some code to reduce the amount of arithmetic needed, in the process; e.g. pulling common factors out of loops. Revised an example's doc to not waste time talking about using a six-sig-fig value for pi (which we no longer do) - it really wasn't relevant, or anything to be proud of; nor did the doc mention its later use. Task-number: QTBUG-58083 Change-Id: I5a31e3a2b6a823b97a43209bed61a37b9aa6c05f Reviewed-by: Thiago Macieira --- examples/opengl/legacy/grabber/glwidget.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'examples/opengl/legacy/grabber/glwidget.cpp') diff --git a/examples/opengl/legacy/grabber/glwidget.cpp b/examples/opengl/legacy/grabber/glwidget.cpp index 958b8055cd..6be4d30597 100644 --- a/examples/opengl/legacy/grabber/glwidget.cpp +++ b/examples/opengl/legacy/grabber/glwidget.cpp @@ -52,8 +52,7 @@ #include #include - -#include +#include GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent) @@ -190,8 +189,6 @@ GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius, GLdouble outerRadius, GLdouble thickness, GLdouble toothSize, GLint toothCount) { - const double Pi = 3.14159265358979323846; - GLuint list = glGenLists(1); glNewList(list, GL_COMPILE); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, reflectance); @@ -199,7 +196,8 @@ GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius, GLdouble r0 = innerRadius; GLdouble r1 = outerRadius - toothSize / 2.0; GLdouble r2 = outerRadius + toothSize / 2.0; - GLdouble delta = (2.0 * Pi / toothCount) / 4.0; + GLdouble toothAngle = 2 * M_PI / toothCount; + GLdouble delta = toothAngle / 4.0; GLdouble z = thickness / 2.0; glShadeModel(GL_FLAT); @@ -211,7 +209,7 @@ GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius, glBegin(GL_QUAD_STRIP); for (int j = 0; j <= toothCount; ++j) { - GLdouble angle = 2.0 * Pi * j / toothCount; + GLdouble angle = j * toothAngle; glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z); glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z); glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z); @@ -221,7 +219,7 @@ GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius, glBegin(GL_QUADS); for (int j = 0; j < toothCount; ++j) { - GLdouble angle = 2.0 * Pi * j / toothCount; + GLdouble angle = j * toothAngle; glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z); glVertex3d(r2 * cos(angle + delta), r2 * sin(angle + delta), sign * z); glVertex3d(r2 * cos(angle + 2 * delta), r2 * sin(angle + 2 * delta), sign * z); @@ -233,7 +231,7 @@ GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius, glBegin(GL_QUAD_STRIP); for (int i = 0; i < toothCount; ++i) { for (int j = 0; j < 2; ++j) { - GLdouble angle = 2.0 * Pi * (i + j / 2.0) / toothCount; + GLdouble angle = (i + j / 2.0) * toothAngle; GLdouble s1 = r1; GLdouble s2 = r2; if (j == 1) @@ -257,7 +255,7 @@ GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius, glBegin(GL_QUAD_STRIP); for (int i = 0; i <= toothCount; ++i) { - GLdouble angle = i * 2.0 * Pi / toothCount; + GLdouble angle = i * toothAngle; glNormal3d(-cos(angle), -sin(angle), 0.0); glVertex3d(r0 * cos(angle), r0 * sin(angle), +z); glVertex3d(r0 * cos(angle), r0 * sin(angle), -z); -- cgit v1.2.3