|
| 1 | +/* GLwindow.cpp |
| 2 | + * |
| 3 | + * Karl Phillip Buhr, 2012, 2013 |
| 4 | + |
| 5 | + * http://stackoverflow.com/users/176769/karlphillip |
| 6 | + * |
| 7 | + * Ingress logo with glowing effect, based on Nehe lesson 36. |
| 8 | + */ |
| 9 | +#include "GLwindow.h" |
| 10 | + |
| 11 | +#include <iostream> |
| 12 | +#include <QKeyEvent> |
| 13 | +#include <QTimer> |
| 14 | + |
| 15 | + |
| 16 | +GLwindow::GLwindow(QWidget *parent) |
| 17 | +: QGLWidget(parent) |
| 18 | +{ |
| 19 | + _width = 0; |
| 20 | + _height = 0; |
| 21 | +} |
| 22 | + |
| 23 | +GLwindow::~GLwindow() |
| 24 | +{ |
| 25 | + glDeleteTextures(1, &_blur_texture); |
| 26 | +} |
| 27 | + |
| 28 | +void GLwindow::_tick() |
| 29 | +{ |
| 30 | + update(); // triggers paintGL() |
| 31 | + QTimer::singleShot(33, this, SLOT(_tick())); |
| 32 | +} |
| 33 | + |
| 34 | +void GLwindow::initializeGL() |
| 35 | +{ |
| 36 | + _blur_texture = _gen_empty_texture(); |
| 37 | + |
| 38 | + GLfloat global_ambient[4]={0.2f, 0.2f, 0.2f, 1.0f}; |
| 39 | + glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient); |
| 40 | + glEnable(GL_LIGHTING); |
| 41 | + |
| 42 | + GLfloat light0pos[4] = { 0.0f, 2.0f, 10.0f, 1.0f }; |
| 43 | + GLfloat light0ambient[4] = { 0.2f, 0.2f, 0.2f, 1.0f }; |
| 44 | + GLfloat light0diffuse[4] = { 0.3f, 0.3f, 0.3f, 1.0f }; |
| 45 | + GLfloat light0specular[4] = { 0.8f, 0.8f, 0.8f, 1.0f }; |
| 46 | + |
| 47 | + glLightfv(GL_LIGHT0, GL_POSITION, light0pos); |
| 48 | + glLightfv(GL_LIGHT0, GL_AMBIENT, light0ambient); |
| 49 | + glLightfv(GL_LIGHT0, GL_DIFFUSE, light0diffuse); |
| 50 | + glLightfv(GL_LIGHT0, GL_SPECULAR, light0specular); |
| 51 | + glEnable(GL_LIGHT0); |
| 52 | + |
| 53 | + glMateriali(GL_FRONT, GL_SHININESS, 128); |
| 54 | + |
| 55 | + glClearColor(0.0f, 0.0f, 0.0f, 0.5f); |
| 56 | + glShadeModel(GL_SMOOTH); |
| 57 | + glEnable(GL_LINE_SMOOTH); |
| 58 | + |
| 59 | + _tick(); |
| 60 | +} |
| 61 | + |
| 62 | +GLuint GLwindow::_gen_empty_texture() |
| 63 | +{ |
| 64 | + // Create storage space for texture data (256x256x4) |
| 65 | + unsigned int* data = (unsigned int*) new GLuint[((256 * 256)* 4 * sizeof(unsigned int))]; |
| 66 | + memset(data, 0, ((256 * 256)* 4 * sizeof(unsigned int))); |
| 67 | + |
| 68 | + GLuint txtnumber = 0; |
| 69 | + glGenTextures(1, &txtnumber); |
| 70 | + glBindTexture(GL_TEXTURE_2D, txtnumber); |
| 71 | + |
| 72 | + glTexImage2D(GL_TEXTURE_2D, 0, 4, 256, 256, 0, |
| 73 | + GL_RGBA, GL_UNSIGNED_BYTE, data); |
| 74 | + |
| 75 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 76 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 77 | + |
| 78 | + if (data) |
| 79 | + delete[] data; |
| 80 | + |
| 81 | + return txtnumber; |
| 82 | +} |
| 83 | + |
| 84 | +void GLwindow::paintGL() |
| 85 | +{ |
| 86 | + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 87 | + |
| 88 | + glMatrixMode(GL_MODELVIEW); |
| 89 | + glLoadIdentity(); |
| 90 | + |
| 91 | + _render_to_texture(); |
| 92 | + _draw_ingress_logo(); |
| 93 | + _draw_blur(8, 0.008f); |
| 94 | +} |
| 95 | + |
| 96 | +void GLwindow::_render_to_texture() |
| 97 | +{ |
| 98 | + glViewport(0, 0, 256, 256); |
| 99 | + |
| 100 | + _draw_ingress_logo(); |
| 101 | + |
| 102 | + glBindTexture(GL_TEXTURE_2D, _blur_texture); |
| 103 | + |
| 104 | + glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 0, 0, 256, 256, 0); |
| 105 | + |
| 106 | + glClearColor(0.0f, 0.0f, 0.0f, 0.5); |
| 107 | + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 108 | + |
| 109 | + glViewport(0, 0, _width ,_height); |
| 110 | +} |
| 111 | + |
| 112 | +void GLwindow::_draw_ingress_logo() |
| 113 | +{ |
| 114 | + GLfloat material_color[] = { 0.5f, 0.5f, 1.0f, 1.0f }; // Set the material color |
| 115 | + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, material_color); |
| 116 | + |
| 117 | + GLfloat specular[] = { 0.5f, 0.5f, 1.0f, 1.0f }; // Sets the specular lighting |
| 118 | + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular); |
| 119 | + |
| 120 | + glMaterialf(GL_FRONT, GL_SHININESS, 20.0f); |
| 121 | + |
| 122 | + _draw_quad(); |
| 123 | + _draw_triangle(); |
| 124 | +} |
| 125 | + |
| 126 | +void GLwindow::_draw_blur(int times, float inc) |
| 127 | +{ |
| 128 | + glDisable(GL_TEXTURE_GEN_S); |
| 129 | + glDisable(GL_TEXTURE_GEN_T); |
| 130 | + |
| 131 | + glEnable(GL_TEXTURE_2D); // Enable 2D Texture Mapping |
| 132 | + glDisable(GL_DEPTH_TEST); // Disable Depth Testing |
| 133 | + glBlendFunc(GL_SRC_ALPHA,GL_ONE); // Set Blending Mode |
| 134 | + glEnable(GL_BLEND); // Enable Blending |
| 135 | + glBindTexture(GL_TEXTURE_2D, _blur_texture); // Bind To The Blur Texture |
| 136 | + _view_ortho(); // Switch To An Ortho View |
| 137 | + |
| 138 | + float alpha = 0.2f; // Starting Alpha Value |
| 139 | + float alphainc = alpha / times; // alphainc=0.3f / Times To Render Blur |
| 140 | + |
| 141 | + int num; // Starting Alpha Value |
| 142 | + float spost = 0.0f; // Starting Texture Coordinate Offset |
| 143 | + glBegin(GL_QUADS); // Begin Drawing Quads |
| 144 | + for (num = 0; num < times; num++) // Number Of Times To Render Blur |
| 145 | + { |
| 146 | + glColor4f(1.0f, 1.0f, 1.0f, alpha); // Set The Alpha Value (Starts At 0.2) |
| 147 | + |
| 148 | + glTexCoord2f(0+spost, 1-spost); // Texture Coordinate ( 0, 1 ) |
| 149 | + glVertex2f(0,0); // First Vertex ( 0, 0 ) |
| 150 | + |
| 151 | + glTexCoord2f(0+spost, 0+spost); // Texture Coordinate ( 0, 0 ) |
| 152 | + glVertex2f(0, _height); // Second Vertex ( 0, _height ) |
| 153 | + |
| 154 | + glTexCoord2f(1-spost, 0+spost); // Texture Coordinate ( 1, 0 ) |
| 155 | + glVertex2f(_width, _height); // Third Vertex ( _width, _height ) |
| 156 | + |
| 157 | + glTexCoord2f(1-spost, 1-spost); // Texture Coordinate ( 1, 1 ) |
| 158 | + glVertex2f(_width, 0); // Fourth Vertex ( _width, 0 ) |
| 159 | + |
| 160 | + spost += inc; // Gradually Increase spost (Zooming Closer To Texture Center) |
| 161 | + alpha = alpha - alphainc; // Gradually Decrease alpha (Gradually Fading Image Out) |
| 162 | + } |
| 163 | + glEnd(); // Done Drawing Quads |
| 164 | + |
| 165 | + _view_perspective(); // Switch To A Perspective View |
| 166 | + |
| 167 | + glEnable(GL_DEPTH_TEST); // Enable Depth Testing |
| 168 | + glDisable(GL_TEXTURE_2D); // Disable 2D Texture Mapping |
| 169 | + glDisable(GL_BLEND); // Disable Blending |
| 170 | + glBindTexture(GL_TEXTURE_2D,0); // Unbind The Blur Texture |
| 171 | +} |
| 172 | + |
| 173 | +void GLwindow::_draw_triangle() |
| 174 | +{ |
| 175 | + glPushMatrix(); |
| 176 | + |
| 177 | + glTranslatef(0.0f, 4.0f, -9.0f); |
| 178 | + glScalef(2.f, 2.f, 2.f); |
| 179 | + |
| 180 | + glLineWidth(5.0f); |
| 181 | + glColor3f(1.0f, 1.0f, 1.0f); // White |
| 182 | + glBegin(GL_LINES); |
| 183 | + |
| 184 | + glVertex3f(-1.4f, -1.5f, -1.0f); // Top |
| 185 | + glVertex3f( 1.4f, -1.5f, -1.0f); |
| 186 | + |
| 187 | + glVertex3f( 1.4f, -1.5f, -1.0f); // Right |
| 188 | + glVertex3f( 0.0f, -3.8f, -1.0f); |
| 189 | + |
| 190 | + glVertex3f(-1.4f, -1.5f, -1.0f); // Left |
| 191 | + glVertex3f( 0.0f, -3.8f, -1.0f); |
| 192 | + |
| 193 | + glVertex3f( 0.0f, -2.3f, -1.0f); // Right diagonal |
| 194 | + glVertex3f( 1.4f, -1.5f, -1.0f); |
| 195 | + |
| 196 | + glVertex3f( 0.0f, -2.3f, -1.0f); // Left diagonal |
| 197 | + glVertex3f(-1.4f, -1.5f, -1.0f); |
| 198 | + |
| 199 | + glVertex3f( 0.0f, -2.3f, -1.0f); // Bottom diagonal |
| 200 | + glVertex3f( 0.0f, -3.8f, -1.0f); |
| 201 | + |
| 202 | + glEnd(); |
| 203 | + |
| 204 | + glPopMatrix(); |
| 205 | +} |
| 206 | + |
| 207 | +void GLwindow::_draw_quad() |
| 208 | +{ |
| 209 | + glPushMatrix(); |
| 210 | + |
| 211 | + glTranslatef(0.0f, 4.0f, -9.0f); |
| 212 | + glScalef(2.f, 2.f, 2.f); |
| 213 | + |
| 214 | + glLineWidth(5.0f); |
| 215 | + glColor3f(1.0f, 1.0f, 1.0f); // White |
| 216 | + |
| 217 | + glBegin(GL_LINES); |
| 218 | + glVertex3f( 0.0f, 0.0f, -1.0f); // Top vertical line |
| 219 | + glVertex3f( 0.0f, -1.0f, -1.0f); |
| 220 | + |
| 221 | + glVertex3f( 1.9f, -3.2f, -1.0f); // Right diagonal line |
| 222 | + glVertex3f( 1.1f, -2.7f, -1.0f); |
| 223 | + |
| 224 | + glVertex3f(-1.9f, -3.2f, -1.0f); // Left diagonal line |
| 225 | + glVertex3f(-1.1f, -2.7f, -1.0f); |
| 226 | + |
| 227 | + glVertex3f( 0.0f, 0.0f, -1.0f); // Right Of The Quad (Top) |
| 228 | + glVertex3f( 2.0f, -1.2f, -1.0f); |
| 229 | + |
| 230 | + glVertex3f( 2.0f, -1.2f, -1.0f); // Right Of The Quad (Center) |
| 231 | + glVertex3f( 2.0f, -3.2f, -1.0f); |
| 232 | + |
| 233 | + glVertex3f( 2.0f, -3.2f, -1.0f); // Right Of The Quad (Bottom) |
| 234 | + glVertex3f( 0.0f, -4.4f, -1.0f); |
| 235 | + |
| 236 | + glVertex3f( 0.0f, 0.0f, -1.0f); // Left Of The Quad (Top) |
| 237 | + glVertex3f(-2.0f, -1.2f, -1.0f); |
| 238 | + |
| 239 | + glVertex3f(-2.0f, -1.2f, -1.0f); // Left Of The Quad (Center) |
| 240 | + glVertex3f(-2.0f, -3.2f, -1.0f); |
| 241 | + |
| 242 | + glVertex3f(-2.0f, -3.2f, -1.0f); // Left Of The Quad (Bottom) |
| 243 | + glVertex3f( 0.0f, -4.4f, -1.0f); |
| 244 | + glEnd(); |
| 245 | + |
| 246 | + glPopMatrix(); |
| 247 | +} |
| 248 | + |
| 249 | +void GLwindow::resizeGL( int w, int h) |
| 250 | +{ |
| 251 | + _width = w; |
| 252 | + _height = h; |
| 253 | + glViewport(0, 0, _width, _height); |
| 254 | + |
| 255 | + glMatrixMode(GL_PROJECTION); |
| 256 | + glLoadIdentity(); |
| 257 | + |
| 258 | + // Compute aspect ratio of the window |
| 259 | + if (_height == 0) |
| 260 | + { |
| 261 | + gluPerspective (60, (float) _width, 1.0, 50.0); |
| 262 | + } |
| 263 | + else |
| 264 | + { |
| 265 | + gluPerspective (60, (float) _width / (float) _height, 1.0, 50.0); |
| 266 | + } |
| 267 | + |
| 268 | + gluLookAt(0.0, 0.0, 2.0, // eye |
| 269 | + 0.0, 0.0, 0.0, // center |
| 270 | + 0.0, 1.0, 0.0); // up |
| 271 | +} |
| 272 | + |
| 273 | +void GLwindow::_view_ortho() // Set Up An Ortho View |
| 274 | +{ |
| 275 | + glMatrixMode(GL_PROJECTION); // Select Projection |
| 276 | + glPushMatrix(); // Push The Matrix |
| 277 | + glLoadIdentity(); // Reset The Matrix |
| 278 | + glOrtho( 0, _width , _height, 0, -1, 1 ); // Select Ortho Mode (640x480) |
| 279 | + glMatrixMode(GL_MODELVIEW); // Select Modelview Matrix |
| 280 | + glPushMatrix(); // Push The Matrix |
| 281 | + glLoadIdentity(); // Reset The Matrix |
| 282 | +} |
| 283 | + |
| 284 | +void GLwindow::_view_perspective() // Set Up A Perspective View |
| 285 | +{ |
| 286 | + glMatrixMode( GL_PROJECTION ); // Select Projection |
| 287 | + glPopMatrix(); // Pop The Matrix |
| 288 | + glMatrixMode( GL_MODELVIEW ); // Select Modelview |
| 289 | + glPopMatrix(); // Pop The Matrix |
| 290 | +} |
0 commit comments