Debian Codegraph @

examples/opengl/legacy/samplebuffers/glwidget.cpp

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