diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2017-03-22 13:18:52 +0100 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2017-06-20 09:53:46 +0000 |
commit | 02b7ec05d5713aec67577cebc18e5906b5a98598 (patch) | |
tree | a8b7fe65eb276c50b5617e808ec26ddfdf065ab8 /examples/opengl | |
parent | 82deb0ad160459c16c77cbbea69b56387723c3a4 (diff) |
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 <thiago.macieira@intel.com>
Diffstat (limited to 'examples/opengl')
-rw-r--r-- | examples/opengl/hellowindow/hellowindow.cpp | 25 | ||||
-rw-r--r-- | examples/opengl/legacy/framebufferobject2/glwidget.cpp | 5 | ||||
-rw-r--r-- | examples/opengl/legacy/grabber/glwidget.cpp | 16 | ||||
-rw-r--r-- | examples/opengl/legacy/pbuffers2/glwidget.cpp | 13 | ||||
-rw-r--r-- | examples/opengl/legacy/samplebuffers/glwidget.cpp | 26 | ||||
-rw-r--r-- | examples/opengl/qopenglwidget/glwidget.cpp | 26 | ||||
-rw-r--r-- | examples/opengl/qopenglwindow/background.frag | 2 | ||||
-rw-r--r-- | examples/opengl/threadedqopenglwidget/glwidget.cpp | 26 |
8 files changed, 66 insertions, 73 deletions
diff --git a/examples/opengl/hellowindow/hellowindow.cpp b/examples/opengl/hellowindow/hellowindow.cpp index dc48cc4b76..235775f09a 100644 --- a/examples/opengl/hellowindow/hellowindow.cpp +++ b/examples/opengl/hellowindow/hellowindow.cpp @@ -276,21 +276,20 @@ void Renderer::createGeometry() extrude(x4, y4, y4, x4); extrude(y4, x4, y3, x3); - const qreal Pi = 3.14159f; const int NumSectors = 100; - + const qreal sectorAngle = 2 * qreal(M_PI) / NumSectors; for (int i = 0; i < NumSectors; ++i) { - qreal angle1 = (i * 2 * Pi) / NumSectors; - qreal x5 = 0.30 * qSin(angle1); - qreal y5 = 0.30 * qCos(angle1); - qreal x6 = 0.20 * qSin(angle1); - qreal y6 = 0.20 * qCos(angle1); - - qreal angle2 = ((i + 1) * 2 * Pi) / NumSectors; - qreal x7 = 0.20 * qSin(angle2); - qreal y7 = 0.20 * qCos(angle2); - qreal x8 = 0.30 * qSin(angle2); - qreal y8 = 0.30 * qCos(angle2); + qreal angle = i * sectorAngle; + qreal x5 = 0.30 * qSin(angle); + qreal y5 = 0.30 * qCos(angle); + qreal x6 = 0.20 * qSin(angle); + qreal y6 = 0.20 * qCos(angle); + + angle += sectorAngle; + qreal x7 = 0.20 * qSin(angle); + qreal y7 = 0.20 * qCos(angle); + qreal x8 = 0.30 * qSin(angle); + qreal y8 = 0.30 * qCos(angle); quad(x5, y5, x6, y6, x7, y7, x8, y8); diff --git a/examples/opengl/legacy/framebufferobject2/glwidget.cpp b/examples/opengl/legacy/framebufferobject2/glwidget.cpp index ac8deddd1f..f14d31aff6 100644 --- a/examples/opengl/legacy/framebufferobject2/glwidget.cpp +++ b/examples/opengl/legacy/framebufferobject2/glwidget.cpp @@ -50,8 +50,7 @@ #include "glwidget.h" #include <QtGui/QImage> - -#include <math.h> +#include <qmath.h> static GLint cubeArray[][3] = { {0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}, @@ -253,6 +252,6 @@ void GLWidget::drawCube(int i, GLfloat z, GLfloat rotation, GLfloat jmp, GLfloat xOffs[i] = xOffs[i] > 1.0f ? 1.0f : -1.0f; } xOffs[i] += xInc[i]; - yOffs[i] = qAbs(cos((-3.141592f * jmp) * xOffs[i]) * amp) - 1; + yOffs[i] = qAbs(cos((-GLfloat(M_PI) * jmp) * xOffs[i]) * amp) - 1; rot[i] += rotation; } 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 <QMouseEvent> #include <QTimer> - -#include <math.h> +#include <qmath.h> 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); diff --git a/examples/opengl/legacy/pbuffers2/glwidget.cpp b/examples/opengl/legacy/pbuffers2/glwidget.cpp index c99f9e40fa..447461dbb3 100644 --- a/examples/opengl/legacy/pbuffers2/glwidget.cpp +++ b/examples/opengl/legacy/pbuffers2/glwidget.cpp @@ -49,10 +49,9 @@ ****************************************************************************/ #include <QtGui/QImage> +#include <qmath.h> #include "glwidget.h" -#include <math.h> - #ifndef GL_MULTISAMPLE #define GL_MULTISAMPLE 0x809D #endif @@ -291,8 +290,6 @@ void GLWidget::restoreGLState() glPopAttrib(); } -#define PI 3.14159 - void GLWidget::timerEvent(QTimerEvent *) { if (QApplication::mouseButtons() != 0) @@ -310,7 +307,7 @@ void GLWidget::timerEvent(QTimerEvent *) rot_x += 0.1f; int dx, dy; // disturbance point - float s, v, W, t; + float s, v, W; int i, j; static float wt[128][128]; const int width = logo.width(); @@ -325,11 +322,11 @@ void GLWidget::timerEvent(QTimerEvent *) for ( j = 0; j < width; ++j) { s = sqrt((double) ((j - dx) * (j - dx) + (i - dy) * (i - dy))); wt[i][j] += 0.1f; - t = s / v; + const double angle = 2 * M_PI * W * (wt[i][j] + s / v); if (s != 0) - wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t)) / (0.2*(s + 2)); + wave[i * width + j] = AMP * sin(angle) / (0.2 * (s + 2)); else - wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t)); + wave[i * width + j] = AMP * sin(angle); } } } diff --git a/examples/opengl/legacy/samplebuffers/glwidget.cpp b/examples/opengl/legacy/samplebuffers/glwidget.cpp index 14c59c9143..da30de4d55 100644 --- a/examples/opengl/legacy/samplebuffers/glwidget.cpp +++ b/examples/opengl/legacy/samplebuffers/glwidget.cpp @@ -49,7 +49,7 @@ ****************************************************************************/ #include "glwidget.h" -#include <math.h> +#include <qmath.h> #ifndef GL_MULTISAMPLE #define GL_MULTISAMPLE 0x809D @@ -116,7 +116,6 @@ void GLWidget::timerEvent(QTimerEvent *) void GLWidget::makeObject() { QColor qtGreen(QColor::fromCmykF(0.40, 0.0, 1.0, 0.0)); - const double Pi = 3.14159265358979323846; const int NumSectors = 15; GLdouble x1 = +0.06; GLdouble y1 = -0.14; @@ -130,18 +129,19 @@ void GLWidget::makeObject() list = glGenLists(1); glNewList(list, GL_COMPILE); { + const double sectorAngle = 2 * M_PI / NumSectors; for (int i = 0; i < NumSectors; ++i) { - double angle1 = (i * 2 * Pi) / NumSectors; - GLdouble x5 = 0.30 * sin(angle1); - GLdouble y5 = 0.30 * cos(angle1); - GLdouble x6 = 0.20 * sin(angle1); - GLdouble y6 = 0.20 * cos(angle1); - - double angle2 = ((i + 1) * 2 * Pi) / NumSectors; - GLdouble x7 = 0.20 * sin(angle2); - GLdouble y7 = 0.20 * cos(angle2); - GLdouble x8 = 0.30 * sin(angle2); - GLdouble y8 = 0.30 * cos(angle2); + double angle = i * sectorAngle; + GLdouble x5 = 0.30 * sin(angle); + GLdouble y5 = 0.30 * cos(angle); + GLdouble x6 = 0.20 * sin(angle); + GLdouble y6 = 0.20 * cos(angle); + + angle += sectorAngle; + GLdouble x7 = 0.20 * sin(angle); + GLdouble y7 = 0.20 * cos(angle); + GLdouble x8 = 0.30 * sin(angle); + GLdouble y8 = 0.30 * cos(angle); qglColor(qtGreen); quad(GL_QUADS, x5, y5, x6, y6, x7, y7, x8, y8); diff --git a/examples/opengl/qopenglwidget/glwidget.cpp b/examples/opengl/qopenglwidget/glwidget.cpp index bb7c952a26..40a1258dd5 100644 --- a/examples/opengl/qopenglwidget/glwidget.cpp +++ b/examples/opengl/qopenglwidget/glwidget.cpp @@ -54,7 +54,7 @@ #include <QOpenGLShaderProgram> #include <QOpenGLTexture> #include <QCoreApplication> -#include <math.h> +#include <qmath.h> #include "mainwindow.h" #include "bubble.h" @@ -455,21 +455,21 @@ void GLWidget::createGeometry() extrude(x4, y4, y4, x4); extrude(y4, x4, y3, x3); - const qreal Pi = 3.14159f; const int NumSectors = 100; + const qreal sectorAngle = 2 * qreal(M_PI) / NumSectors; for (int i = 0; i < NumSectors; ++i) { - qreal angle1 = (i * 2 * Pi) / NumSectors; - qreal x5 = 0.30 * sin(angle1); - qreal y5 = 0.30 * cos(angle1); - qreal x6 = 0.20 * sin(angle1); - qreal y6 = 0.20 * cos(angle1); - - qreal angle2 = ((i + 1) * 2 * Pi) / NumSectors; - qreal x7 = 0.20 * sin(angle2); - qreal y7 = 0.20 * cos(angle2); - qreal x8 = 0.30 * sin(angle2); - qreal y8 = 0.30 * cos(angle2); + qreal angle = i * sectorAngle; + qreal x5 = 0.30 * sin(angle); + qreal y5 = 0.30 * cos(angle); + qreal x6 = 0.20 * sin(angle); + qreal y6 = 0.20 * cos(angle); + + angle += sectorAngle; + qreal x7 = 0.20 * sin(angle); + qreal y7 = 0.20 * cos(angle); + qreal x8 = 0.30 * sin(angle); + qreal y8 = 0.30 * cos(angle); quad(x5, y5, x6, y6, x7, y7, x8, y8); diff --git a/examples/opengl/qopenglwindow/background.frag b/examples/opengl/qopenglwindow/background.frag index eecb6d3120..4af85aae8e 100644 --- a/examples/opengl/qopenglwindow/background.frag +++ b/examples/opengl/qopenglwindow/background.frag @@ -1,4 +1,4 @@ -#define M_PI 3.1415926535897932384626433832795 +#define M_PI 3.14159265358979323846 #define SPEED 10000.0 uniform int currentTime; diff --git a/examples/opengl/threadedqopenglwidget/glwidget.cpp b/examples/opengl/threadedqopenglwidget/glwidget.cpp index f9738db2e5..cc528a734a 100644 --- a/examples/opengl/threadedqopenglwidget/glwidget.cpp +++ b/examples/opengl/threadedqopenglwidget/glwidget.cpp @@ -49,7 +49,7 @@ ****************************************************************************/ #include "glwidget.h" -#include <math.h> +#include <qmath.h> #include <QGuiApplication> GLWidget::GLWidget(QWidget *parent) @@ -282,21 +282,21 @@ void Renderer::createGeometry() extrude(x4, y4, y4, x4); extrude(y4, x4, y3, x3); - const qreal Pi = 3.14159f; const int NumSectors = 100; + const qreal sectorAngle = 2 * qreal(M_PI) / NumSectors; for (int i = 0; i < NumSectors; ++i) { - qreal angle1 = (i * 2 * Pi) / NumSectors; - qreal x5 = 0.30 * sin(angle1); - qreal y5 = 0.30 * cos(angle1); - qreal x6 = 0.20 * sin(angle1); - qreal y6 = 0.20 * cos(angle1); - - qreal angle2 = ((i + 1) * 2 * Pi) / NumSectors; - qreal x7 = 0.20 * sin(angle2); - qreal y7 = 0.20 * cos(angle2); - qreal x8 = 0.30 * sin(angle2); - qreal y8 = 0.30 * cos(angle2); + qreal angle = i * sectorAngle; + qreal x5 = 0.30 * sin(angle); + qreal y5 = 0.30 * cos(angle); + qreal x6 = 0.20 * sin(angle); + qreal y6 = 0.20 * cos(angle); + + angle += sectorAngle; + qreal x7 = 0.20 * sin(angle); + qreal y7 = 0.20 * cos(angle); + qreal x8 = 0.30 * sin(angle); + qreal y8 = 0.30 * cos(angle); quad(x5, y5, x6, y6, x7, y7, x8, y8); |