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

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

debug: minor updates to the debug quad code.

  • Property svn:keywords set to Id
File size: 21.8 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 = 9;
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__ && !defined ANDROID_NDK
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) / (4 * layout + vec2i(1));
130    data->orig = vec2(-1.0f, 1.0f) + data->step;
131    data->aa = data->orig;
132    data->bb = data->orig + 3.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    /* Cheap iterators */
146#if !defined __CELLOS_LV2__ && !defined ANDROID_NDK
147    GLuint *array = data->array;
148#endif
149    GLuint *buffer = data->buffer;
150    Shader **shader = data->shader;
151    GLuint *attr = data->attr;
152
153    ResetState();
154
155#if defined HAVE_GLBEGIN || defined USE_GLEW
156    /*
157     * Test #1: simple glBegin code
158     *
159     * Renders an orange square.
160     */
161    glColor3f(0.8f, 0.5f, 0.2f);
162    glBegin(GL_TRIANGLES);
163        glVertex3f(data->aa.x, data->bb.y, 0.0f);
164        glVertex3f(data->bb.x, data->bb.y, 0.0f);
165        glVertex3f(data->bb.x, data->aa.y, 0.0f);
166
167        glVertex3f(data->bb.x, data->aa.y, 0.0f);
168        glVertex3f(data->aa.x, data->aa.y, 0.0f);
169        glVertex3f(data->aa.x, data->bb.y, 0.0f);
170    glEnd();
171
172    Advance();
173    ResetState();
174
175    /*
176     * Test #2: glBegin + per-vertex coloring
177     *
178     * Renders a multicoloured square with varying colors.
179     */
180
181    glBegin(GL_TRIANGLES);
182        glColor3f(f1, f2, f3);
183        glVertex3f(data->aa.x, data->bb.y, 0.0f);
184        glColor3f(f4, f2, f1);
185        glVertex3f(data->bb.x, data->bb.y, 0.0f);
186        glColor3f(f3, f1, f4);
187        glVertex3f(data->bb.x, data->aa.y, 0.0f);
188
189        glVertex3f(data->bb.x, data->aa.y, 0.0f);
190        glColor3f(f4, f3, f2);
191        glVertex3f(data->aa.x, data->aa.y, 0.0f);
192        glColor3f(f1, f2, f3);
193        glVertex3f(data->aa.x, data->bb.y, 0.0f);
194    glEnd();
195
196    Advance();
197    ResetState();
198
199    /*
200     * Test #3: glBegin + texture
201     *
202     * Renders a multicoloured square with varying colors multiplied with an
203     * animated distorted checkerboard.
204     */
205    glEnable(GL_TEXTURE_2D);
206    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
207    glColor3f(1.0f, 1.0f, 1.0f);
208    glBegin(GL_TRIANGLES);
209        glColor3f(f1, f2, f3);
210        glTexCoord2f(f1, f3);
211        glVertex3f(data->aa.x, data->bb.y, 0.0f);
212        glColor3f(f4, f2, f1);
213        glTexCoord2f(f3, f2);
214        glVertex3f(data->bb.x, data->bb.y, 0.0f);
215        glColor3f(f3, f1, f4);
216        glTexCoord2f(f2, f4);
217        glVertex3f(data->bb.x, data->aa.y, 0.0f);
218
219        glTexCoord2f(f2, f4);
220        glVertex3f(data->bb.x, data->aa.y, 0.0f);
221        glColor3f(f4, f3, f2);
222        glTexCoord2f(f4, f1);
223        glVertex3f(data->aa.x, data->aa.y, 0.0f);
224        glColor3f(f1, f2, f3);
225        glTexCoord2f(f1, f3);
226        glVertex3f(data->aa.x, data->bb.y, 0.0f);
227    glEnd();
228    glDisable(GL_TEXTURE_2D);
229
230    Advance();
231    ResetState();
232
233    /*
234     * Test #4: glBegin + color in fragment shader
235     *
236     * Renders a static, coloured and tiled pattern.
237     */
238    if (!shader[0])
239        shader[0] = Shader::Create(
240            "#version 110\n"
241            "void main()"
242            "{"
243            "    gl_Position = gl_Vertex;"
244            "}",
245
246            "#version 110\n"
247            "void main()"
248            "{"
249            "    float dx = mod(gl_FragCoord.x * gl_FragCoord.y, 2.0);"
250            "    float dy = mod(gl_FragCoord.x * 0.125, 1.0);"
251            "    float dz = mod(gl_FragCoord.y * 0.125, 1.0);"
252            "    gl_FragColor = vec4(dx, dy, dz, 1.0);"
253            "}");
254    shader[0]->Bind();
255    shader++;
256    glColor3f(0.0f, 1.0f, 1.0f);
257    glBegin(GL_TRIANGLES);
258        glVertex3f(data->aa.x, data->bb.y, 0.0f);
259        glVertex3f(data->bb.x, data->bb.y, 0.0f);
260        glVertex3f(data->bb.x, data->aa.y, 0.0f);
261
262        glVertex3f(data->bb.x, data->aa.y, 0.0f);
263        glVertex3f(data->aa.x, data->aa.y, 0.0f);
264        glVertex3f(data->aa.x, data->bb.y, 0.0f);
265    glEnd();
266    glUseProgram(0);
267
268    Advance();
269    ResetState();
270
271    /*
272     * Test #5: glBegin + pass vertex coord from vertex shader to fragment
273     * shader for use as color information.
274     *
275     * Renders a multicoloured square with varying colors.
276     */
277    if (!shader[0])
278        shader[0] = Shader::Create(
279            "#version 110\n"
280            "varying vec4 pass_Color;"
281            "void main()"
282            "{"
283            "    float r = gl_MultiTexCoord0.x;"
284            "    float g = gl_MultiTexCoord0.y;"
285            "    float b = gl_MultiTexCoord0.z;"
286            "    pass_Color = vec4(r, g, b, 1.0);"
287            "    gl_Position = gl_Vertex;"
288            "}",
289
290            "#version 110\n"
291            "varying vec4 pass_Color;"
292            "void main()"
293            "{"
294            "    gl_FragColor = pass_Color;"
295            "}");
296    shader[0]->Bind();
297    shader++;
298    glColor3f(0.0f, 1.0f, 1.0f);
299    glBegin(GL_TRIANGLES);
300        glTexCoord3f(f1, f2, f3);
301        glVertex3f(data->aa.x, data->bb.y, 0.0f);
302        glTexCoord3f(f4, f2, f1);
303        glVertex3f(data->bb.x, data->bb.y, 0.0f);
304        glTexCoord3f(f3, f1, f4);
305        glVertex3f(data->bb.x, data->aa.y, 0.0f);
306
307        glVertex3f(data->bb.x, data->aa.y, 0.0f);
308        glTexCoord3f(f4, f3, f2);
309        glVertex3f(data->aa.x, data->aa.y, 0.0f);
310        glTexCoord3f(f1, f2, f3);
311        glVertex3f(data->aa.x, data->bb.y, 0.0f);
312    glEnd();
313    glUseProgram(0);
314
315    Advance();
316    ResetState();
317
318    /*
319     * Test #6: glBegin + apply texture in fragment shader
320     *
321     * Renders an animated black-and-white distorted checkerboard with a
322     * zoom ratio twice the one in test #3.
323     *
324     * Note: there is no need to glEnable(GL_TEXTURE_2D) when the
325     * texture lookup is done in a shader.
326     */
327    if (!shader[0])
328        shader[0] = Shader::Create(
329            "#version 110\n"
330            "void main()"
331            "{"
332            "    gl_TexCoord[0] = gl_MultiTexCoord0;"
333            "    gl_Position = gl_Vertex;"
334            "}",
335
336            "#version 110\n"
337            "uniform sampler2D tex;"
338            "void main()"
339            "{"
340            "    gl_FragColor = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
341            "}");
342    shader[0]->Bind();
343    shader++;
344    glColor3f(0.0f, 1.0f, 1.0f);
345    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
346    glBegin(GL_TRIANGLES);
347        glTexCoord2f(f1, f3);
348        glVertex3f(data->aa.x, data->bb.y, 0.0f);
349        glTexCoord2f(f3, f2);
350        glVertex3f(data->bb.x, data->bb.y, 0.0f);
351        glTexCoord2f(f2, f4);
352        glVertex3f(data->bb.x, data->aa.y, 0.0f);
353
354        glTexCoord2f(f2, f4);
355        glVertex3f(data->bb.x, data->aa.y, 0.0f);
356        glTexCoord2f(f4, f1);
357        glVertex3f(data->aa.x, data->aa.y, 0.0f);
358        glTexCoord2f(f1, f3);
359        glVertex3f(data->aa.x, data->bb.y, 0.0f);
360    glEnd();
361    glUseProgram(0);
362
363    Advance();
364    ResetState();
365#endif
366
367    /*
368     * Test #7: simple vertex buffer
369     *
370     * Renders an orange square.
371     */
372#if !defined ANDROID_NDK
373    GLfloat const vertices1[] = { data->aa.x, data->bb.y, 0.0f,
374                                  data->bb.x, data->bb.y, 0.0f,
375                                  data->bb.x, data->aa.y, 0.0f,
376                                  data->bb.x, data->aa.y, 0.0f,
377                                  data->aa.x, data->aa.y, 0.0f,
378                                  data->aa.x, data->bb.y, 0.0f };
379
380    glColor4f(0.8f, 0.5f, 0.2f, 1.0f);
381    glEnableClientState(GL_VERTEX_ARRAY);
382
383    glVertexPointer(3, GL_FLOAT, 0, vertices1);
384    glDrawArrays(GL_TRIANGLES, 0, 6);
385
386    glDisableClientState(GL_VERTEX_ARRAY);
387
388    Advance();
389    ResetState();
390#endif
391
392    /*
393     * Test #8: vertex buffer + per-vertex coloring
394     *
395     * Renders a multicoloured square with varying colors.
396     */
397#if !defined ANDROID_NDK
398    GLfloat const vertices2[] = { data->aa.x, data->bb.y, 0.0f,
399                                  data->bb.x, data->bb.y, 0.0f,
400                                  data->bb.x, data->aa.y, 0.0f,
401                                  data->bb.x, data->aa.y, 0.0f,
402                                  data->aa.x, data->aa.y, 0.0f,
403                                  data->aa.x, data->bb.y, 0.0f };
404
405    glEnableClientState(GL_VERTEX_ARRAY);
406    glEnableClientState(GL_COLOR_ARRAY);
407
408    glVertexPointer(3, GL_FLOAT, 0, vertices2);
409    glColorPointer(3, GL_FLOAT, 0, colors);
410    glDrawArrays(GL_TRIANGLES, 0, 6);
411
412    glDisableClientState(GL_VERTEX_ARRAY);
413    glDisableClientState(GL_COLOR_ARRAY);
414
415    Advance();
416    ResetState();
417#endif
418
419    /*
420     * Test #9: vertex buffer + per-vertex coloring + texture
421     *
422     * Renders a multicoloured square with varying colors multiplied with an
423     * animated distorted checkerboard.
424     */
425#if !defined ANDROID_NDK
426    GLfloat const vertices3[] = { data->aa.x, data->bb.y, 0.0f,
427                                  data->bb.x, data->bb.y, 0.0f,
428                                  data->bb.x, data->aa.y, 0.0f,
429                                  data->bb.x, data->aa.y, 0.0f,
430                                  data->aa.x, data->aa.y, 0.0f,
431                                  data->aa.x, data->bb.y, 0.0f };
432
433    glEnable(GL_TEXTURE_2D);
434    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
435    glEnableClientState(GL_VERTEX_ARRAY);
436    glEnableClientState(GL_COLOR_ARRAY);
437    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
438
439    glVertexPointer(3, GL_FLOAT, 0, vertices3);
440    glColorPointer(3, GL_FLOAT, 0, colors);
441    glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
442
443    glDrawArrays(GL_TRIANGLES, 0, 6);
444
445    glDisableClientState(GL_VERTEX_ARRAY);
446    glDisableClientState(GL_COLOR_ARRAY);
447    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
448    glDisable(GL_TEXTURE_2D);
449
450    Advance();
451    ResetState();
452#endif
453
454    /*
455     * Test #10: vertex buffer + texture & color in 1.10 fragment shader
456     *
457     * Renders a multicoloured square with varying colors xored with an
458     * animated distorted checkerboard.
459     */
460#if !defined __CELLOS_LV2__ && !defined ANDROID_NDK
461    if (!shader[0])
462        shader[0] = Shader::Create(
463            "#version 110\n"
464            "varying vec4 pass_Color;"
465            "void main()"
466            "{"
467            "    gl_TexCoord[0] = gl_MultiTexCoord0;"
468            "    pass_Color = gl_Color;"
469            "    gl_Position = gl_Vertex;"
470            "}",
471
472            "#version 110\n"
473            "varying vec4 pass_Color;"
474            "uniform sampler2D tex;"
475            "void main()"
476            "{"
477            "    vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
478            "    gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);"
479            "}");
480
481    shader[0]->Bind();
482    shader++;
483    GLfloat const vertices4[] = { data->aa.x, data->bb.y, 0.0f,
484                                  data->bb.x, data->bb.y, 0.0f,
485                                  data->bb.x, data->aa.y, 0.0f,
486                                  data->bb.x, data->aa.y, 0.0f,
487                                  data->aa.x, data->aa.y, 0.0f,
488                                  data->aa.x, data->bb.y, 0.0f };
489
490    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
491
492    glEnableClientState(GL_VERTEX_ARRAY);
493    glEnableClientState(GL_COLOR_ARRAY);
494    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
495
496    glVertexPointer(3, GL_FLOAT, 0, vertices4);
497    glColorPointer(3, GL_FLOAT, 0, colors);
498    glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
499    glDrawArrays(GL_TRIANGLES, 0, 6);
500
501    glDisableClientState(GL_VERTEX_ARRAY);
502    glDisableClientState(GL_COLOR_ARRAY);
503    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
504    glUseProgram(0);
505
506    Advance();
507    ResetState();
508#endif
509
510    /*
511     * Test #11: vertex buffer + texture & color in 1.20 fragment shader
512     *
513     * Renders a multicoloured square with varying colors xored with an
514     * animated distorted checkerboard.
515     */
516#if !defined __CELLOS_LV2__ && !defined ANDROID_NDK
517    if (!shader[0])
518    {
519        shader[0] = Shader::Create(
520            "#version 120\n"
521            "attribute vec3 in_Vertex;"
522            "attribute vec3 in_Color;"
523            "attribute vec2 in_MultiTexCoord0;"
524            "varying vec4 pass_Color;"
525            "void main()"
526            "{"
527            "    gl_TexCoord[0] = vec4(in_MultiTexCoord0, 0.0, 0.0);"
528            "    pass_Color = vec4(in_Color, 1.0);"
529            "    gl_Position = vec4(in_Vertex, 1.0);"
530            "}",
531
532            "#version 120\n"
533            "varying vec4 pass_Color;"
534            "uniform sampler2D tex;"
535            "void main()"
536            "{"
537            "    vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
538            "    gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);"
539            "}");
540        attr[0] = shader[0]->GetAttribLocation("in_Vertex");
541        attr[1] = shader[0]->GetAttribLocation("in_Color");
542        attr[2] = shader[0]->GetAttribLocation("in_MultiTexCoord0");
543    }
544    shader[0]->Bind();
545    shader++;
546    GLfloat const vertices5[] = { data->aa.x, data->bb.y, 0.0f,
547                                  data->bb.x, data->bb.y, 0.0f,
548                                  data->bb.x, data->aa.y, 0.0f,
549                                  data->bb.x, data->aa.y, 0.0f,
550                                  data->aa.x, data->aa.y, 0.0f,
551                                  data->aa.x, data->bb.y, 0.0f };
552
553    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
554
555    glBindVertexArray(*array++);
556
557    glBindBuffer(GL_ARRAY_BUFFER, *buffer++);
558    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices5), vertices5,
559                 GL_DYNAMIC_DRAW);
560    glVertexAttribPointer(attr[0], 3, GL_FLOAT, GL_FALSE, 0, 0);
561    glEnableVertexAttribArray(attr[0]);
562
563    glBindBuffer(GL_ARRAY_BUFFER, *buffer++);
564    glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors,
565                 GL_DYNAMIC_DRAW);
566    glVertexAttribPointer(attr[1], 3, GL_FLOAT, GL_FALSE, 0, 0);
567    glEnableVertexAttribArray(attr[1]);
568
569    glBindBuffer(GL_ARRAY_BUFFER, *buffer++);
570    glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords,
571                 GL_DYNAMIC_DRAW);
572    glVertexAttribPointer(attr[2], 2, GL_FLOAT, GL_FALSE, 0, 0);
573    glEnableVertexAttribArray(attr[2]);
574
575    glDrawArrays(GL_TRIANGLES, 0, 6);
576    glBindVertexArray(0);
577
578    glDisableVertexAttribArray(*attr++);
579    glDisableVertexAttribArray(*attr++);
580    glDisableVertexAttribArray(*attr++);
581    glUseProgram(0);
582
583    Advance();
584    ResetState();
585#endif
586
587    /*
588     * Test #12: vertex buffer + texture & color in 1.30 fragment shader
589     *
590     * Renders a multicoloured square with varying colors xored with an
591     * animated distorted checkerboard.
592     */
593#if !defined __CELLOS_LV2__ && !defined ANDROID_NDK
594    if (!shader[0])
595    {
596        shader[0] = Shader::Create(
597            "#version 130\n"
598            "in vec3 in_Vertex;"
599            "in vec3 in_Color;"
600            "in vec2 in_MultiTexCoord0;"
601            "out vec4 pass_Color;"
602            "void main()"
603            "{"
604            "    gl_TexCoord[0] = vec4(in_MultiTexCoord0, 0.0, 0.0);"
605            "    pass_Color = vec4(in_Color, 1.0);"
606            "    gl_Position = vec4(in_Vertex, 1.0);"
607            "}",
608
609            "#version 130\n"
610            "in vec4 pass_Color;"
611            "uniform sampler2D tex;"
612            "void main()"
613            "{"
614            "    vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
615            "    gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);"
616            "}");
617        attr[0] = shader[0]->GetAttribLocation("in_Vertex");
618        attr[1] = shader[0]->GetAttribLocation("in_Color");
619        attr[2] = shader[0]->GetAttribLocation("in_MultiTexCoord0");
620    }
621    shader[0]->Bind();
622    shader++;
623    GLfloat const vertices6[] = { data->aa.x, data->bb.y, 0.0f,
624                                  data->bb.x, data->bb.y, 0.0f,
625                                  data->bb.x, data->aa.y, 0.0f,
626                                  data->bb.x, data->aa.y, 0.0f,
627                                  data->aa.x, data->aa.y, 0.0f,
628                                  data->aa.x, data->bb.y, 0.0f };
629
630    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
631
632    glBindVertexArray(*array++);
633
634    glBindBuffer(GL_ARRAY_BUFFER, *buffer++);
635    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices6), vertices6,
636                 GL_DYNAMIC_DRAW);
637    glVertexAttribPointer(attr[0], 3, GL_FLOAT, GL_FALSE, 0, 0);
638    glEnableVertexAttribArray(attr[0]);
639
640    glBindBuffer(GL_ARRAY_BUFFER, *buffer++);
641    glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors,
642                 GL_DYNAMIC_DRAW);
643    glVertexAttribPointer(attr[1], 3, GL_FLOAT, GL_FALSE, 0, 0);
644    glEnableVertexAttribArray(attr[1]);
645
646    glBindBuffer(GL_ARRAY_BUFFER, *buffer++);
647    glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords,
648                 GL_DYNAMIC_DRAW);
649    glVertexAttribPointer(attr[2], 2, GL_FLOAT, GL_FALSE, 0, 0);
650    glEnableVertexAttribArray(attr[2]);
651
652    glDrawArrays(GL_TRIANGLES, 0, 6);
653    glBindVertexArray(0);
654
655    glDisableVertexAttribArray(*attr++);
656    glDisableVertexAttribArray(*attr++);
657    glDisableVertexAttribArray(*attr++);
658    glUseProgram(0);
659
660    Advance();
661    ResetState();
662#endif
663
664    /* Check that we didn't overflow our list */
665#if !defined __CELLOS_LV2__ && !defined ANDROID_NDK
666    if (array > data->array + NUM_ARRAYS)
667        Log::Error("too many arrays used\n");
668#endif
669    if (buffer > data->buffer + NUM_BUFFERS)
670        Log::Error("too many buffers used\n");
671    if (shader > data->shader + NUM_SHADERS)
672        Log::Error("too many shaders used\n");
673    if (attr > data->attr + NUM_ATTRS)
674        Log::Error("too many attributes used\n");
675}
676
677void DebugQuad::ResetState()
678{
679    /* Reset GL states to something reasonably safe */
680#if defined HAVE_GLBEGIN || defined USE_GLEW || defined __CELLOS_LV2__
681    glMatrixMode(GL_PROJECTION);
682    glLoadIdentity();
683    glMatrixMode(GL_MODELVIEW);
684    glLoadIdentity();
685#endif
686
687    glEnable(GL_TEXTURE_2D);
688    glBindTexture(GL_TEXTURE_2D, 0);
689#if defined HAVE_GLBEGIN || defined USE_GLEW || defined __CELLOS_LV2__
690    glClientActiveTexture(GL_TEXTURE0);
691#endif
692    glDisable(GL_TEXTURE_2D);
693
694    glBindBuffer(GL_ARRAY_BUFFER, 0);
695#if !defined __CELLOS_LV2__
696    glUseProgram(0);
697#endif
698}
699
700void DebugQuad::Advance()
701{
702    data->aa.x += 4.0f * data->step.x;
703    data->bb.x += 4.0f * data->step.x;
704    if (data->bb.x > 1.0f)
705    {
706        data->aa.x = data->orig.x;
707        data->bb.x = data->orig.x + 3.0f * data->step.x;
708        data->aa.y += 4.0f * data->step.y;
709        data->bb.y += 4.0f * data->step.y;
710    }
711}
712
713DebugQuad::~DebugQuad()
714{
715    delete data;
716}
717
718} /* namespace lol */
719
Note: See TracBrowser for help on using the repository browser.