diff options
author | Sarah Smith <sarah.j.smith@nokia.com> | 2009-09-21 11:23:06 +1000 |
---|---|---|
committer | Sarah Smith <sarah.j.smith@nokia.com> | 2009-09-21 11:23:06 +1000 |
commit | 11fed1f64f43593a2890e0a3f27b4e2e7ebde783 (patch) | |
tree | 99f57336eafe2403af7526154d8d4743f416333e /examples/opengl/overpainting/glwidget.cpp | |
parent | 1b8d92b1ae453bd2d395658c7086d0049916e88f (diff) | |
download | qt4-tools-11fed1f64f43593a2890e0a3f27b4e2e7ebde783.tar.gz |
remove display lists and qt3d-ize overpainting/hellogl example
On the way to making opengl examples portable, remove display lists
and go to triangles only. Use QMatrix4x4. While on the job use Qt/3D
stylee to make the QtLogo as an example of 3D programming more Qt-like.
Reviewed-by: Rhys Weatherley
Diffstat (limited to 'examples/opengl/overpainting/glwidget.cpp')
-rw-r--r-- | examples/opengl/overpainting/glwidget.cpp | 125 |
1 files changed, 20 insertions, 105 deletions
diff --git a/examples/opengl/overpainting/glwidget.cpp b/examples/opengl/overpainting/glwidget.cpp index 30d326664d..0dcccbfac4 100644 --- a/examples/opengl/overpainting/glwidget.cpp +++ b/examples/opengl/overpainting/glwidget.cpp @@ -46,6 +46,7 @@ #include <math.h> #include "bubble.h" +#include "qtlogo.h" #include "glwidget.h" #ifndef GL_MULTISAMPLE @@ -59,7 +60,7 @@ GLWidget::GLWidget(QWidget *parent) QTime midnight(0, 0, 0); qsrand(midnight.secsTo(QTime::currentTime())); - object = 0; + logo = 0; xRot = 0; yRot = 0; zRot = 0; @@ -80,28 +81,34 @@ GLWidget::GLWidget(QWidget *parent) //! [1] GLWidget::~GLWidget() { - makeCurrent(); - glDeleteLists(object, 1); } //! [1] +static void qNormalizeAngle(int &angle) +{ + while (angle < 0) + angle += 360 * 16; + while (angle > 360 * 16) + angle -= 360 * 16; +} + void GLWidget::setXRotation(int angle) { - normalizeAngle(&angle); + qNormalizeAngle(angle); if (angle != xRot) xRot = angle; } void GLWidget::setYRotation(int angle) { - normalizeAngle(&angle); + qNormalizeAngle(angle); if (angle != yRot) yRot = angle; } void GLWidget::setZRotation(int angle) { - normalizeAngle(&angle); + qNormalizeAngle(angle); if (angle != zRot) zRot = angle; } @@ -109,7 +116,10 @@ void GLWidget::setZRotation(int angle) //! [2] void GLWidget::initializeGL() { - object = makeObject(); + glEnable(GL_MULTISAMPLE); + + logo = new QtLogo(this); + logo->setColor(qtGreen.dark()); } //! [2] @@ -162,7 +172,8 @@ void GLWidget::paintEvent(QPaintEvent *event) glRotated(xRot / 16.0, 1.0, 0.0, 0.0); glRotated(yRot / 16.0, 0.0, 1.0, 0.0); glRotated(zRot / 16.0, 0.0, 0.0, 1.0); - glCallList(object); + + logo->draw(); //! [7] //! [8] @@ -207,102 +218,6 @@ QSize GLWidget::sizeHint() const return QSize(400, 400); } -GLuint GLWidget::makeObject() -{ - GLuint list = glGenLists(1); - glNewList(list, GL_COMPILE); - - glEnable(GL_NORMALIZE); - glBegin(GL_QUADS); - - static GLfloat logoDiffuseColor[4] = {qtGreen.red()/255.0, - qtGreen.green()/255.0, - qtGreen.blue()/255.0, 1.0}; - glMaterialfv(GL_FRONT, GL_DIFFUSE, logoDiffuseColor); - - GLdouble x1 = +0.06; - GLdouble y1 = -0.14; - GLdouble x2 = +0.14; - GLdouble y2 = -0.06; - GLdouble x3 = +0.08; - GLdouble y3 = +0.00; - GLdouble x4 = +0.30; - GLdouble y4 = +0.22; - - quad(x1, y1, x2, y2, y2, x2, y1, x1); - quad(x3, y3, x4, y4, y4, x4, y3, x3); - - extrude(x1, y1, x2, y2); - extrude(x2, y2, y2, x2); - extrude(y2, x2, y1, x1); - extrude(y1, x1, x1, y1); - extrude(x3, y3, x4, y4); - extrude(x4, y4, y4, x4); - extrude(y4, x4, y3, x3); - - const double Pi = 3.14159265358979323846; - const int NumSectors = 200; - - 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); - - quad(x5, y5, x6, y6, x7, y7, x8, y8); - - extrude(x6, y6, x7, y7); - extrude(x8, y8, x5, y5); - } - - glEnd(); - - glEndList(); - return list; -} - -void GLWidget::quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2, - GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4) -{ - glNormal3d(0.0, 0.0, -1.0); - glVertex3d(x1, y1, -0.05); - glVertex3d(x2, y2, -0.05); - glVertex3d(x3, y3, -0.05); - glVertex3d(x4, y4, -0.05); - - glNormal3d(0.0, 0.0, 1.0); - glVertex3d(x4, y4, +0.05); - glVertex3d(x3, y3, +0.05); - glVertex3d(x2, y2, +0.05); - glVertex3d(x1, y1, +0.05); -} - -void GLWidget::extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2) -{ - qglColor(qtGreen.dark(250 + int(100 * x1))); - - glNormal3d((x1 + x2)/2.0, (y1 + y2)/2.0, 0.0); - glVertex3d(x1, y1, +0.05); - glVertex3d(x2, y2, +0.05); - glVertex3d(x2, y2, -0.05); - glVertex3d(x1, y1, -0.05); -} - -void GLWidget::normalizeAngle(int *angle) -{ - while (*angle < 0) - *angle += 360 * 16; - while (*angle > 360 * 16) - *angle -= 360 * 16; -} - void GLWidget::createBubbles(int number) { for (int i = 0; i < number; ++i) { @@ -337,7 +252,7 @@ void GLWidget::setupViewport(int width, int height) glMatrixMode(GL_PROJECTION); glLoadIdentity(); - glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0); + glOrtho(-0.5, +0.5, -0.5, 0.5, 4.0, 15.0); glMatrixMode(GL_MODELVIEW); } //! [14] |