source: trunk/src/debug/quad.cpp @ 819

Last change on this file since 819 was 819, checked in by sam, 10 years ago

debug: make the GL test compile on Android.

  • Property svn:keywords set to Id
File size: 20.4 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cmath>
16#include <cstdio>
17
18#include "core.h"
19#include "lolgl.h"
20#include "loldebug.h"
21
22using namespace std;
23
24namespace lol
25{
26
27/*
28 * DebugQuad implementation class
29 */
30
31static int const NUM_ARRAYS = 2;
32static int const NUM_BUFFERS = 6;
33static int const NUM_ATTRS = 6;
34static int const NUM_SHADERS = 6;
35static int const NUM_TEXTURES = 1;
36
37static int const TEX_SIZE = 32;
38
39class DebugQuadData
40{
41    friend class DebugQuad;
42
43private:
44    vec2 orig, step, aa, bb;
45
46    int initialised;
47    float time;
48#if !defined __CELLOS_LV2__
49    GLuint array[NUM_ARRAYS];
50#endif
51    GLuint buffer[NUM_BUFFERS];
52    Shader *shader[NUM_SHADERS];
53    GLuint attr[NUM_ATTRS];
54    GLuint texture[NUM_TEXTURES];
55    uint8_t image[1][TEX_SIZE * TEX_SIZE * 4];
56};
57
58/*
59 * Public DebugQuad class
60 */
61
62DebugQuad::DebugQuad()
63  : data(new DebugQuadData())
64{
65    data->initialised = 0;
66    data->time = RandF(10000.0f);
67
68    drawgroup = DRAWGROUP_HUD;
69}
70
71void DebugQuad::TickGame(float deltams)
72{
73    Entity::TickGame(deltams);
74
75    data->time += deltams;
76}
77
78void DebugQuad::TickDraw(float deltams)
79{
80    Entity::TickDraw(deltams);
81
82    if (!data->initialised && !IsDestroying())
83    {
84#if !defined __CELLOS_LV2__ && !defined ANDROID_NDK
85        glGenVertexArrays(NUM_ARRAYS, data->array);
86#endif
87        glGenBuffers(NUM_BUFFERS, data->buffer);
88        glGenTextures(NUM_TEXTURES, data->texture);
89        for (int i = 0; i < NUM_SHADERS; i++)
90            data->shader[i] = NULL;
91
92        /* Checkerboard texture */
93        glEnable(GL_TEXTURE_2D);
94        glBindTexture(GL_TEXTURE_2D, data->texture[0]);
95        for (int j = 0; j < TEX_SIZE; j++)
96            for (int i = 0; i < TEX_SIZE; i++)
97            {
98                uint8_t wb = (((i / 2) ^ (j / 2)) & 1) * 0xff;
99                data->image[0][(j * TEX_SIZE + i) * 4 + 0] = wb;
100                data->image[0][(j * TEX_SIZE + i) * 4 + 1] = wb;
101                data->image[0][(j * TEX_SIZE + i) * 4 + 2] = wb;
102                data->image[0][(j * TEX_SIZE + i) * 4 + 3] = 0xff;
103            }
104        /* Use GL_RGBA instead of 4 for the internal format (Android) */
105        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_SIZE, TEX_SIZE, 0,
106                     GL_RGBA, GL_UNSIGNED_BYTE, data->image[0]);
107        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
108        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
109
110        data->initialised = 1;
111    }
112    else if (data->initialised && IsDestroying())
113    {
114#if !defined __CELLOS_LV2__ && !defined ANDROID_NDK
115        glDeleteVertexArrays(NUM_ARRAYS, data->array);
116#endif
117        glDeleteBuffers(NUM_BUFFERS, data->buffer);
118        glDeleteTextures(NUM_TEXTURES, data->texture);
119
120        for (int i = 0; i < NUM_SHADERS; i++)
121            if (data->shader[i])
122                Shader::Destroy(data->shader[i]);
123
124        data->initialised = 0;
125    }
126
127    /* Prepare our quad coordinates */
128    vec2i const layout(4, 3);
129    data->step = vec2(2.0f, -2.0f) / (3 * layout + vec2i(1));
130    data->orig = vec2(-1.0f, 1.0f) + data->step;
131    data->aa = data->orig;
132    data->bb = data->orig + 2.0f * data->step;
133
134    /* Generate a few random numbers */
135    float f1 = 0.5f + 0.5f * sinf(0.00034f * data->time);
136    float f2 = 0.5f + 0.5f * sinf(0.00053f * data->time + 1.0f);
137    float f3 = 0.5f + 0.5f * sinf(0.00072f * data->time + 4.0f);
138    float f4 = 0.5f + 0.5f * sinf(0.00091f * data->time + 8.0f);
139
140    GLfloat const colors[] = { f1, f2, f3, f4, f2, f1, f3, f1, f4,
141                               f3, f1, f4, f4, f3, f2, f1, f2, f3 };
142    GLfloat const texcoords[] = { f1, f3, f3, f2, f2, f4,
143                                  f2, f4, f4, f1, f1, f3 };
144
145    ResetState();
146
147#if defined HAVE_GLBEGIN || defined USE_GLEW
148    /*
149     * Test #1: simple glBegin code
150     *
151     * Renders an orange square.
152     */
153    glColor3f(0.8f, 0.5f, 0.2f);
154    glBegin(GL_TRIANGLES);
155        glVertex3f(data->aa.x, data->bb.y, 0.0f);
156        glVertex3f(data->bb.x, data->bb.y, 0.0f);
157        glVertex3f(data->bb.x, data->aa.y, 0.0f);
158
159        glVertex3f(data->bb.x, data->aa.y, 0.0f);
160        glVertex3f(data->aa.x, data->aa.y, 0.0f);
161        glVertex3f(data->aa.x, data->bb.y, 0.0f);
162    glEnd();
163
164    Advance();
165    ResetState();
166
167    /*
168     * Test #2: glBegin + per-vertex coloring
169     *
170     * Renders a multicolored square with varying colors.
171     */
172
173    glBegin(GL_TRIANGLES);
174        glColor3f(f1, f2, f3);
175        glVertex3f(data->aa.x, data->bb.y, 0.0f);
176        glColor3f(f4, f2, f1);
177        glVertex3f(data->bb.x, data->bb.y, 0.0f);
178        glColor3f(f3, f1, f4);
179        glVertex3f(data->bb.x, data->aa.y, 0.0f);
180
181        glVertex3f(data->bb.x, data->aa.y, 0.0f);
182        glColor3f(f4, f3, f2);
183        glVertex3f(data->aa.x, data->aa.y, 0.0f);
184        glColor3f(f1, f2, f3);
185        glVertex3f(data->aa.x, data->bb.y, 0.0f);
186    glEnd();
187
188    Advance();
189    ResetState();
190
191    /*
192     * Test #3: glBegin + texture
193     *
194     * Renders an animated black-and-white distorted checkerboard.
195     */
196    glEnable(GL_TEXTURE_2D);
197    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
198    glColor3f(1.0f, 1.0f, 1.0f);
199    glBegin(GL_TRIANGLES);
200        glTexCoord2f(f1, f3);
201        glVertex3f(data->aa.x, data->bb.y, 0.0f);
202        glTexCoord2f(f3, f2);
203        glVertex3f(data->bb.x, data->bb.y, 0.0f);
204        glTexCoord2f(f2, f4);
205        glVertex3f(data->bb.x, data->aa.y, 0.0f);
206
207        glTexCoord2f(f2, f4);
208        glVertex3f(data->bb.x, data->aa.y, 0.0f);
209        glTexCoord2f(f4, f1);
210        glVertex3f(data->aa.x, data->aa.y, 0.0f);
211        glTexCoord2f(f1, f3);
212        glVertex3f(data->aa.x, data->bb.y, 0.0f);
213    glEnd();
214    glDisable(GL_TEXTURE_2D);
215
216    Advance();
217    ResetState();
218
219    /*
220     * Test #4: glBegin + color in fragment shader
221     *
222     * Renders a static, coloured and tiled pattern.
223     */
224#if !defined __CELLOS_LV2__
225    if (!data->shader[0])
226        data->shader[0] = Shader::Create(
227            "#version 110\n"
228            "void main()"
229            "{"
230            "    gl_Position = gl_Vertex;"
231            "}",
232
233            "#version 110\n"
234            "void main()"
235            "{"
236            "    float dx = mod(gl_FragCoord.x * gl_FragCoord.y, 2.0);"
237            "    float dy = mod(gl_FragCoord.x * 0.125, 1.0);"
238            "    float dz = mod(gl_FragCoord.y * 0.125, 1.0);"
239            "    gl_FragColor = vec4(dx, dy, dz, 1.0);"
240            "}");
241    data->shader[0]->Bind();
242    glColor3f(0.0f, 1.0f, 1.0f);
243    glBegin(GL_TRIANGLES);
244        glVertex3f(data->aa.x, data->bb.y, 0.0f);
245        glVertex3f(data->bb.x, data->bb.y, 0.0f);
246        glVertex3f(data->bb.x, data->aa.y, 0.0f);
247
248        glVertex3f(data->bb.x, data->aa.y, 0.0f);
249        glVertex3f(data->aa.x, data->aa.y, 0.0f);
250        glVertex3f(data->aa.x, data->bb.y, 0.0f);
251    glEnd();
252    glUseProgram(0);
253
254    Advance();
255    ResetState();
256#endif
257
258    /*
259     * Test #5: glBegin + pass vertex coord from vertex shader to fragment
260     * shader for use as color information.
261     *
262     * Renders a multicolored square with varying colors.
263     */
264#if !defined __CELLOS_LV2__
265    if (!data->shader[1])
266        data->shader[1] = Shader::Create(
267            "#version 110\n"
268            "varying vec4 pass_Color;"
269            "void main()"
270            "{"
271            "    float r = gl_MultiTexCoord0.x;"
272            "    float g = gl_MultiTexCoord0.y;"
273            "    float b = gl_MultiTexCoord0.z;"
274            "    pass_Color = vec4(r, g, b, 1.0);"
275            "    gl_Position = gl_Vertex;"
276            "}",
277
278            "#version 110\n"
279            "varying vec4 pass_Color;"
280            "void main()"
281            "{"
282            "    gl_FragColor = pass_Color;"
283            "}");
284    data->shader[1]->Bind();
285    glColor3f(0.0f, 1.0f, 1.0f);
286    glBegin(GL_TRIANGLES);
287        glTexCoord3f(f1, f2, f3);
288        glVertex3f(data->aa.x, data->bb.y, 0.0f);
289        glTexCoord3f(f4, f2, f1);
290        glVertex3f(data->bb.x, data->bb.y, 0.0f);
291        glTexCoord3f(f3, f1, f4);
292        glVertex3f(data->bb.x, data->aa.y, 0.0f);
293
294        glVertex3f(data->bb.x, data->aa.y, 0.0f);
295        glTexCoord3f(f4, f3, f2);
296        glVertex3f(data->aa.x, data->aa.y, 0.0f);
297        glTexCoord3f(f1, f2, f3);
298        glVertex3f(data->aa.x, data->bb.y, 0.0f);
299    glEnd();
300    glUseProgram(0);
301
302    Advance();
303    ResetState();
304#endif
305
306    /*
307     * Test #6: glBegin + apply texture in fragment shader
308     *
309     * Renders an animated black-and-white distorted checkerboard with a
310     * zoom ratio twice the one in test #3.
311     *
312     * Note: there is no need to glEnable(GL_TEXTURE_2D) when the
313     * texture lookup is done in a shader.
314     */
315#if !defined __CELLOS_LV2__
316    if (!data->shader[2])
317        data->shader[2] = Shader::Create(
318            "#version 110\n"
319            "void main()"
320            "{"
321            "    gl_TexCoord[0] = gl_MultiTexCoord0;"
322            "    gl_Position = gl_Vertex;"
323            "}",
324
325            "#version 110\n"
326            "uniform sampler2D tex;"
327            "void main()"
328            "{"
329            "    gl_FragColor = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
330            "}");
331    data->shader[2]->Bind();
332    glColor3f(0.0f, 1.0f, 1.0f);
333    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
334    glBegin(GL_TRIANGLES);
335        glTexCoord2f(f1, f3);
336        glVertex3f(data->aa.x, data->bb.y, 0.0f);
337        glTexCoord2f(f3, f2);
338        glVertex3f(data->bb.x, data->bb.y, 0.0f);
339        glTexCoord2f(f2, f4);
340        glVertex3f(data->bb.x, data->aa.y, 0.0f);
341
342        glTexCoord2f(f2, f4);
343        glVertex3f(data->bb.x, data->aa.y, 0.0f);
344        glTexCoord2f(f4, f1);
345        glVertex3f(data->aa.x, data->aa.y, 0.0f);
346        glTexCoord2f(f1, f3);
347        glVertex3f(data->aa.x, data->bb.y, 0.0f);
348    glEnd();
349    glUseProgram(0);
350
351    Advance();
352    ResetState();
353#endif
354#endif
355
356#if !defined ANDROID_NDK
357    /*
358     * Test #7: vertex buffer + per-vertex coloring
359     *
360     * Renders a multicolored square with varying colors.
361     */
362    GLfloat const vertices1[] = { data->aa.x, data->bb.y, 0.0f,
363                                  data->bb.x, data->bb.y, 0.0f,
364                                  data->bb.x, data->aa.y, 0.0f,
365                                  data->bb.x, data->aa.y, 0.0f,
366                                  data->aa.x, data->aa.y, 0.0f,
367                                  data->aa.x, data->bb.y, 0.0f };
368
369    glEnableClientState(GL_COLOR_ARRAY);
370    glEnableClientState(GL_VERTEX_ARRAY);
371
372    glColorPointer(3, GL_FLOAT, 0, colors);
373    glVertexPointer(3, GL_FLOAT, 0, vertices1);
374    glDrawArrays(GL_TRIANGLES, 0, 6);
375
376    glDisableClientState(GL_VERTEX_ARRAY);
377    glDisableClientState(GL_COLOR_ARRAY);
378
379    Advance();
380    ResetState();
381#endif
382
383    /*
384     * Test #8: vertex buffer + per-vertex coloring + texture
385     *
386     * Renders a multicolored square with varying colors multiplied with an
387     * animated distorted checkerboard.
388     */
389#if !defined ANDROID_NDK
390    GLfloat const vertices2[] = { data->aa.x, data->bb.y, 0.0f,
391                                  data->bb.x, data->bb.y, 0.0f,
392                                  data->bb.x, data->aa.y, 0.0f,
393                                  data->bb.x, data->aa.y, 0.0f,
394                                  data->aa.x, data->aa.y, 0.0f,
395                                  data->aa.x, data->bb.y, 0.0f };
396
397    glEnable(GL_TEXTURE_2D);
398    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
399    glEnableClientState(GL_COLOR_ARRAY);
400    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
401    glEnableClientState(GL_VERTEX_ARRAY);
402
403    glColorPointer(3, GL_FLOAT, 0, colors);
404    glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
405    glVertexPointer(3, GL_FLOAT, 0, vertices2);
406    glDrawArrays(GL_TRIANGLES, 0, 6);
407
408    glDisableClientState(GL_VERTEX_ARRAY);
409    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
410    glDisableClientState(GL_COLOR_ARRAY);
411    glDisable(GL_TEXTURE_2D);
412
413    Advance();
414    ResetState();
415#endif
416
417    /*
418     * Test #9: vertex buffer + texture & color in 1.10 fragment shader
419     *
420     * Renders a multicolored square with varying colors xored with an
421     * animated distorted checkerboard.
422     */
423#if !defined __CELLOS_LV2__ && !defined ANDROID_NDK
424    if (!data->shader[3])
425        data->shader[3] = Shader::Create(
426            "#version 110\n"
427            "varying vec4 pass_Color;"
428            "void main()"
429            "{"
430            "    gl_TexCoord[0] = gl_MultiTexCoord0;"
431            "    pass_Color = gl_Color;"
432            "    gl_Position = gl_Vertex;"
433            "}",
434
435            "#version 110\n"
436            "varying vec4 pass_Color;"
437            "uniform sampler2D tex;"
438            "void main()"
439            "{"
440            "    vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
441            "    gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);"
442            "}");
443
444    data->shader[3]->Bind();
445    GLfloat const vertices3[] = { data->aa.x, data->bb.y, 0.0f,
446                                  data->bb.x, data->bb.y, 0.0f,
447                                  data->bb.x, data->aa.y, 0.0f,
448                                  data->bb.x, data->aa.y, 0.0f,
449                                  data->aa.x, data->aa.y, 0.0f,
450                                  data->aa.x, data->bb.y, 0.0f };
451
452    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
453
454    glEnableClientState(GL_VERTEX_ARRAY);
455    glEnableClientState(GL_COLOR_ARRAY);
456    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
457
458    glVertexPointer(3, GL_FLOAT, 0, vertices3);
459    glColorPointer(3, GL_FLOAT, 0, colors);
460    glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
461    glDrawArrays(GL_TRIANGLES, 0, 6);
462
463    glDisableClientState(GL_VERTEX_ARRAY);
464    glDisableClientState(GL_COLOR_ARRAY);
465    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
466    glUseProgram(0);
467
468    Advance();
469    ResetState();
470#endif
471
472    /*
473     * Test #10: vertex buffer + texture & color in 1.20 fragment shader
474     *
475     * Renders a multicolored square with varying colors xored with an
476     * animated distorted checkerboard.
477     */
478#if !defined __CELLOS_LV2__ && !defined ANDROID_NDK
479    if (!data->shader[4])
480    {
481        data->shader[4] = Shader::Create(
482            "#version 120\n"
483            "attribute vec3 in_Vertex;"
484            "attribute vec3 in_Color;"
485            "attribute vec2 in_MultiTexCoord0;"
486            "varying vec4 pass_Color;"
487            "void main()"
488            "{"
489            "    gl_TexCoord[0] = vec4(in_MultiTexCoord0, 0.0, 0.0);"
490            "    pass_Color = vec4(in_Color, 1.0);"
491            "    gl_Position = vec4(in_Vertex, 1.0);"
492            "}",
493
494            "#version 120\n"
495            "varying vec4 pass_Color;"
496            "uniform sampler2D tex;"
497            "void main()"
498            "{"
499            "    vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
500            "    gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);"
501            "}");
502        data->attr[0] = data->shader[4]->GetAttribLocation("in_Vertex");
503        data->attr[1] = data->shader[4]->GetAttribLocation("in_Color");
504        data->attr[2] = data->shader[4]->GetAttribLocation("in_MultiTexCoord0");
505    }
506    data->shader[4]->Bind();
507    GLfloat const vertices4[] = { data->aa.x, data->bb.y, 0.0f,
508                                  data->bb.x, data->bb.y, 0.0f,
509                                  data->bb.x, data->aa.y, 0.0f,
510                                  data->bb.x, data->aa.y, 0.0f,
511                                  data->aa.x, data->aa.y, 0.0f,
512                                  data->aa.x, data->bb.y, 0.0f };
513
514    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
515
516    glBindVertexArray(data->array[0]);
517
518    glBindBuffer(GL_ARRAY_BUFFER, data->buffer[0]);
519    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices4), vertices4,
520                 GL_DYNAMIC_DRAW);
521    glVertexAttribPointer(data->attr[0], 3, GL_FLOAT, GL_FALSE, 0, 0);
522    glEnableVertexAttribArray(data->attr[0]);
523
524    glBindBuffer(GL_ARRAY_BUFFER, data->buffer[1]);
525    glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors,
526                 GL_DYNAMIC_DRAW);
527    glVertexAttribPointer(data->attr[1], 3, GL_FLOAT, GL_FALSE, 0, 0);
528    glEnableVertexAttribArray(data->attr[1]);
529
530    glBindBuffer(GL_ARRAY_BUFFER, data->buffer[2]);
531    glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords,
532                 GL_DYNAMIC_DRAW);
533    glVertexAttribPointer(data->attr[2], 2, GL_FLOAT, GL_FALSE, 0, 0);
534    glEnableVertexAttribArray(data->attr[2]);
535
536    glDrawArrays(GL_TRIANGLES, 0, 6);
537    glBindVertexArray(0);
538
539    glDisableVertexAttribArray(data->attr[0]);
540    glDisableVertexAttribArray(data->attr[1]);
541    glDisableVertexAttribArray(data->attr[2]);
542    glUseProgram(0);
543
544    Advance();
545    ResetState();
546#endif
547
548    /*
549     * Test #11: vertex buffer + texture & color in 1.30 fragment shader
550     *
551     * Renders a multicolored square with varying colors xored with an
552     * animated distorted checkerboard.
553     */
554#if !defined __CELLOS_LV2__ && !defined ANDROID_NDK
555    if (!data->shader[5])
556    {
557        data->shader[5] = Shader::Create(
558            "#version 130\n"
559            "in vec3 in_Vertex;"
560            "in vec3 in_Color;"
561            "in vec2 in_MultiTexCoord0;"
562            "out vec4 pass_Color;"
563            "void main()"
564            "{"
565            "    gl_TexCoord[0] = vec4(in_MultiTexCoord0, 0.0, 0.0);"
566            "    pass_Color = vec4(in_Color, 1.0);"
567            "    gl_Position = vec4(in_Vertex, 1.0);"
568            "}",
569
570            "#version 130\n"
571            "in vec4 pass_Color;"
572            "uniform sampler2D tex;"
573            "void main()"
574            "{"
575            "    vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
576            "    gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);"
577            "}");
578        data->attr[3] = data->shader[4]->GetAttribLocation("in_Vertex");
579        data->attr[4] = data->shader[4]->GetAttribLocation("in_Color");
580        data->attr[5] = data->shader[4]->GetAttribLocation("in_MultiTexCoord0");
581    }
582    data->shader[5]->Bind();
583    GLfloat const vertices5[] = { data->aa.x, data->bb.y, 0.0f,
584                                  data->bb.x, data->bb.y, 0.0f,
585                                  data->bb.x, data->aa.y, 0.0f,
586                                  data->bb.x, data->aa.y, 0.0f,
587                                  data->aa.x, data->aa.y, 0.0f,
588                                  data->aa.x, data->bb.y, 0.0f };
589
590    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
591
592    glBindVertexArray(data->array[1]);
593
594    glBindBuffer(GL_ARRAY_BUFFER, data->buffer[3]);
595    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices5), vertices5,
596                 GL_DYNAMIC_DRAW);
597    glVertexAttribPointer(data->attr[3], 3, GL_FLOAT, GL_FALSE, 0, 0);
598    glEnableVertexAttribArray(data->attr[3]);
599
600    glBindBuffer(GL_ARRAY_BUFFER, data->buffer[4]);
601    glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors,
602                 GL_DYNAMIC_DRAW);
603    glVertexAttribPointer(data->attr[4], 3, GL_FLOAT, GL_FALSE, 0, 0);
604    glEnableVertexAttribArray(data->attr[4]);
605
606    glBindBuffer(GL_ARRAY_BUFFER, data->buffer[5]);
607    glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords,
608                 GL_DYNAMIC_DRAW);
609    glVertexAttribPointer(data->attr[5], 2, GL_FLOAT, GL_FALSE, 0, 0);
610    glEnableVertexAttribArray(data->attr[5]);
611
612    glDrawArrays(GL_TRIANGLES, 0, 6);
613    glBindVertexArray(0);
614
615    glDisableVertexAttribArray(data->attr[3]);
616    glDisableVertexAttribArray(data->attr[4]);
617    glDisableVertexAttribArray(data->attr[5]);
618    glUseProgram(0);
619
620    Advance();
621    ResetState();
622#endif
623}
624
625void DebugQuad::ResetState()
626{
627    /* Reset GL states to something reasonably safe */
628#if defined HAVE_GLBEGIN || defined USE_GLEW
629    glMatrixMode(GL_PROJECTION);
630    glLoadIdentity();
631    glMatrixMode(GL_MODELVIEW);
632    glLoadIdentity();
633#endif
634
635    glEnable(GL_TEXTURE_2D);
636    glBindTexture(GL_TEXTURE_2D, 0);
637#if defined HAVE_GLBEGIN || defined USE_GLEW
638    glClientActiveTexture(GL_TEXTURE0);
639#endif
640    glDisable(GL_TEXTURE_2D);
641
642    glBindBuffer(GL_ARRAY_BUFFER, 0);
643#if !defined __CELLOS_LV2__
644    glUseProgram(0);
645#endif
646}
647
648void DebugQuad::Advance()
649{
650    data->aa.x += 3.0f * data->step.x;
651    data->bb.x += 3.0f * data->step.x;
652    if (data->bb.x > 1.0f)
653    {
654        data->aa.x = data->orig.x;
655        data->bb.x = data->orig.x + 2.0f * data->step.x;
656        data->aa.y += 3.0f * data->step.y;
657        data->bb.y += 3.0f * data->step.y;
658    }
659}
660
661DebugQuad::~DebugQuad()
662{
663    delete data;
664}
665
666} /* namespace lol */
667
Note: See TracBrowser for help on using the repository browser.