| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:BSD$ | 
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows: | 
|---|
| 11 | ** | 
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without | 
|---|
| 13 | ** modification, are permitted provided that the following conditions are | 
|---|
| 14 | ** met: | 
|---|
| 15 | **   * Redistributions of source code must retain the above copyright | 
|---|
| 16 | **     notice, this list of conditions and the following disclaimer. | 
|---|
| 17 | **   * Redistributions in binary form must reproduce the above copyright | 
|---|
| 18 | **     notice, this list of conditions and the following disclaimer in | 
|---|
| 19 | **     the documentation and/or other materials provided with the | 
|---|
| 20 | **     distribution. | 
|---|
| 21 | **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor | 
|---|
| 22 | **     the names of its contributors may be used to endorse or promote | 
|---|
| 23 | **     products derived from this software without specific prior written | 
|---|
| 24 | **     permission. | 
|---|
| 25 | ** | 
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | 
|---|
| 37 | ** $QT_END_LICENSE$ | 
|---|
| 38 | ** | 
|---|
| 39 | ****************************************************************************/ | 
|---|
| 40 |  | 
|---|
| 41 | #include "glwidget.h" | 
|---|
| 42 | #include <math.h> | 
|---|
| 43 |  | 
|---|
| 44 | #ifndef GL_MULTISAMPLE | 
|---|
| 45 | #define GL_MULTISAMPLE  0x809D | 
|---|
| 46 | #endif | 
|---|
| 47 |  | 
|---|
| 48 | GLWidget::GLWidget(QWidget *parent) | 
|---|
| 49 | : QGLWidget(QGLFormat(QGL::SampleBuffers), parent) | 
|---|
| 50 | { | 
|---|
| 51 | startTimer(40); | 
|---|
| 52 | setWindowTitle(tr("Sample Buffers")); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | void GLWidget::initializeGL() | 
|---|
| 56 | { | 
|---|
| 57 | glMatrixMode(GL_PROJECTION); | 
|---|
| 58 | glLoadIdentity(); | 
|---|
| 59 | glOrtho(-.5, .5, .5, -.5, -1000, 1000); | 
|---|
| 60 | glMatrixMode(GL_MODELVIEW); | 
|---|
| 61 | glLoadIdentity(); | 
|---|
| 62 | glClearColor(1.0f, 1.0f, 1.0f, 1.0f); | 
|---|
| 63 |  | 
|---|
| 64 | makeObject(); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | void GLWidget::resizeGL(int w, int h) | 
|---|
| 68 | { | 
|---|
| 69 | glViewport(0, 0, w, h); | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | void GLWidget::paintGL() | 
|---|
| 73 | { | 
|---|
| 74 | static float rot = 0.0; | 
|---|
| 75 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 
|---|
| 76 |  | 
|---|
| 77 | glMatrixMode(GL_MODELVIEW); | 
|---|
| 78 | glPushMatrix(); | 
|---|
| 79 | glEnable(GL_MULTISAMPLE); | 
|---|
| 80 | glTranslatef(-0.25f, -0.10f, 0.0f); | 
|---|
| 81 | glScalef(0.75f, 1.15f, 0.0f); | 
|---|
| 82 | glRotatef(rot, 0.0f, 0.f, 1.0f); | 
|---|
| 83 | glCallList(list); | 
|---|
| 84 | glPopMatrix(); | 
|---|
| 85 |  | 
|---|
| 86 | glPushMatrix(); | 
|---|
| 87 | glDisable(GL_MULTISAMPLE); | 
|---|
| 88 | glTranslatef(0.25f, -0.10f, 0.0f); | 
|---|
| 89 | glScalef(0.75f, 1.15f, 0.0f); | 
|---|
| 90 | glRotatef(rot, 0.0f, 0.0f, 1.0f); | 
|---|
| 91 | glCallList(list); | 
|---|
| 92 | glPopMatrix(); | 
|---|
| 93 |  | 
|---|
| 94 | rot += 0.2f; | 
|---|
| 95 |  | 
|---|
| 96 | qglColor(Qt::black); | 
|---|
| 97 | renderText(-0.35, 0.4, 0.0, "Multisampling enabled"); | 
|---|
| 98 | renderText(0.15, 0.4, 0.0, "Multisampling disabled"); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | void GLWidget::timerEvent(QTimerEvent *) | 
|---|
| 102 | { | 
|---|
| 103 | update(); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | void GLWidget::makeObject() | 
|---|
| 107 | { | 
|---|
| 108 | QColor qtGreen(QColor::fromCmykF(0.40, 0.0, 1.0, 0.0)); | 
|---|
| 109 | const double Pi = 3.14159265358979323846; | 
|---|
| 110 | const int NumSectors = 15; | 
|---|
| 111 | GLdouble x1 = +0.06; | 
|---|
| 112 | GLdouble y1 = -0.14; | 
|---|
| 113 | GLdouble x2 = +0.14; | 
|---|
| 114 | GLdouble y2 = -0.06; | 
|---|
| 115 | GLdouble x3 = +0.08; | 
|---|
| 116 | GLdouble y3 = +0.00; | 
|---|
| 117 | GLdouble x4 = +0.30; | 
|---|
| 118 | GLdouble y4 = +0.22; | 
|---|
| 119 |  | 
|---|
| 120 | list = glGenLists(1); | 
|---|
| 121 | glNewList(list, GL_COMPILE); | 
|---|
| 122 | { | 
|---|
| 123 | for (int i = 0; i < NumSectors; ++i) { | 
|---|
| 124 | double angle1 = (i * 2 * Pi) / NumSectors; | 
|---|
| 125 | GLdouble x5 = 0.30 * sin(angle1); | 
|---|
| 126 | GLdouble y5 = 0.30 * cos(angle1); | 
|---|
| 127 | GLdouble x6 = 0.20 * sin(angle1); | 
|---|
| 128 | GLdouble y6 = 0.20 * cos(angle1); | 
|---|
| 129 |  | 
|---|
| 130 | double angle2 = ((i + 1) * 2 * Pi) / NumSectors; | 
|---|
| 131 | GLdouble x7 = 0.20 * sin(angle2); | 
|---|
| 132 | GLdouble y7 = 0.20 * cos(angle2); | 
|---|
| 133 | GLdouble x8 = 0.30 * sin(angle2); | 
|---|
| 134 | GLdouble y8 = 0.30 * cos(angle2); | 
|---|
| 135 |  | 
|---|
| 136 | qglColor(qtGreen); | 
|---|
| 137 | quad(GL_QUADS, x5, y5, x6, y6, x7, y7, x8, y8); | 
|---|
| 138 | qglColor(Qt::black); | 
|---|
| 139 | quad(GL_LINE_LOOP, x5, y5, x6, y6, x7, y7, x8, y8); | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | qglColor(qtGreen); | 
|---|
| 143 | quad(GL_QUADS, x1, y1, x2, y2, y2, x2, y1, x1); | 
|---|
| 144 | quad(GL_QUADS, x3, y3, x4, y4, y4, x4, y3, x3); | 
|---|
| 145 |  | 
|---|
| 146 | qglColor(Qt::black); | 
|---|
| 147 | quad(GL_LINE_LOOP, x1, y1, x2, y2, y2, x2, y1, x1); | 
|---|
| 148 | quad(GL_LINE_LOOP, x3, y3, x4, y4, y4, x4, y3, x3); | 
|---|
| 149 | } | 
|---|
| 150 | glEndList(); | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | void GLWidget::quad(GLenum primitive, GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2, | 
|---|
| 154 | GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4) | 
|---|
| 155 | { | 
|---|
| 156 | glBegin(primitive); | 
|---|
| 157 | { | 
|---|
| 158 | glVertex2d(x1, y1); | 
|---|
| 159 | glVertex2d(x2, y2); | 
|---|
| 160 | glVertex2d(x3, y3); | 
|---|
| 161 | glVertex2d(x4, y4); | 
|---|
| 162 | } | 
|---|
| 163 | glEnd(); | 
|---|
| 164 | } | 
|---|