source: trunk/src/debugquad.cpp @ 777

Last change on this file since 777 was 777, checked in by sam, 9 years ago

debug: convert debug quad shaders to GLSL 1.30.

  • Property svn:keywords set to Id
File size: 16.1 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
31#define NUM_SHADERS 4
32
33class DebugQuadData
34{
35    friend class DebugQuad;
36
37private:
38    vec2 orig, step, aa, bb;
39
40    int initialised;
41    float time;
42    GLuint buflist[3];
43    Shader *shader[NUM_SHADERS];
44    GLuint texlist[1];
45    uint8_t image[1][32 * 32 * 4];
46};
47
48/*
49 * Public DebugQuad class
50 */
51
52DebugQuad::DebugQuad()
53  : data(new DebugQuadData())
54{
55    data->initialised = 0;
56    data->time = RandF(10000.0f);
57
58    drawgroup = DRAWGROUP_HUD;
59}
60
61void DebugQuad::TickGame(float deltams)
62{
63    Entity::TickGame(deltams);
64
65    data->time += deltams;
66}
67
68void DebugQuad::TickDraw(float deltams)
69{
70    Entity::TickDraw(deltams);
71
72    if (!data->initialised && !IsDestroying())
73    {
74#if 0
75        glGenBuffers(3, data->buflist);
76
77        static char const *vertexshader =
78            "//#version 130\n"
79            "varying vec2 in_Position;\n"
80            "varying vec4 in_Color;\n"
81            "varying vec2 in_TexCoord;\n"
82            "varying vec4 pass_Color;\n"
83            "void main()\n"
84            "{\n"
85            "gl_TexCoord[0] = gl_MultiTexCoord0;\n"
86            "    gl_Position = vec4(in_Position, 0.0f, 1.0f);\n"
87            "    gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
88            "    pass_Color = in_Color;\n"
89            "}\n";
90        static char const *fragmentshader =
91            "//#version 130\n"
92            "varying vec4 pass_Color;\n"
93            "uniform sampler2D in_Texture;\n"
94            "void main()\n"
95            "{\n"
96            "    vec4 col = pass_Color;\n"
97            "    vec4 tex = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
98            "    gl_FragColor = col * tex;\n"
99            "    gl_FragColor = vec4(1.0, 1.0, 1.0, 0.0);\n"
100            "}\n";
101        data->shader[0] = Shader::Create(vertexshader, fragmentshader);
102        glGenTextures(1, data->texlist);
103#endif
104
105        /* Checkerboard texture */
106        glEnable(GL_TEXTURE_2D);
107        glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
108        for (int j = 0; j < 32; j++)
109            for (int i = 0; i < 32; i++)
110            {
111                uint8_t wb = (((i / 2) ^ (j / 2)) & 1) * 0xff;
112                data->image[0][(j * 32 + i) * 4 + 0] = wb;
113                data->image[0][(j * 32 + i) * 4 + 1] = wb;
114                data->image[0][(j * 32 + i) * 4 + 2] = wb;
115                data->image[0][(j * 32 + i) * 4 + 3] = 0xff;
116            }
117        /* Use GL_RGBA instead of 4 for the internal format (Android) */
118        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0,
119                     GL_RGBA, GL_UNSIGNED_BYTE, data->image[0]);
120        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
121        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
122
123        /* Quad #4: create texture in fragment shader */
124        data->shader[0] = Shader::Create(
125            "#version 130\n"
126            "in vec2 in_Vertex;"
127            "void main()"
128            "{"
129            "    gl_Position = vec4(in_Vertex, 0.0, 1.0);"
130            "}",
131
132            "#version 130\n"
133            "void main()"
134            "{"
135            "    float dx = mod(gl_FragCoord.x * gl_FragCoord.y, 2.0);"
136            "    float dy = mod(gl_FragCoord.x * 0.125, 1.0);"
137            "    float dz = mod(gl_FragCoord.y * 0.125, 1.0);"
138            "    gl_FragColor = vec4(dx, dy, dz, 1.0);"
139            "}");
140
141        /* Quad #5: pass color from vertex shader to fragment shader */
142        data->shader[1] = Shader::Create(
143            "#version 130\n"
144            "in vec2 in_Vertex;"
145            "in vec2 in_MultiTexCoord0;"
146            "varying vec4 pass_Color;"
147            "void main()"
148            "{"
149            "    float r = in_MultiTexCoord0.x;"
150            "    float g = in_MultiTexCoord0.y;"
151            "    pass_Color = vec4(1.0 - r, 1.0 - g, r, 1.0);"
152            "    gl_Position = vec4(in_Vertex, 0.0, 1.0);"
153            "}",
154
155            "#version 130\n"
156            "varying vec4 pass_Color;"
157            "void main()"
158            "{"
159            "    gl_FragColor = pass_Color;"
160            "}");
161
162        /* Quad #6: apply texture in fragment shader */
163        data->shader[2] = Shader::Create(
164            "#version 130\n"
165            "in vec2 in_Vertex;"
166            "in vec2 in_MultiTexCoord0;"
167            "void main()"
168            "{"
169            "    gl_TexCoord[0] = vec4(in_MultiTexCoord0, 0.0, 0.0);"
170            "    gl_Position = vec4(in_Vertex, 0.0, 1.0);"
171            "}",
172
173            "#version 130\n"
174            "uniform sampler2D tex;"
175            "void main()"
176            "{"
177            "    gl_FragColor = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
178            "}");
179
180        /* Quad #8: vertex buffer, apply texture in fragment shader */
181        data->shader[3] = Shader::Create(
182            "#version 130\n"
183            "in vec2 in_Vertex;"
184            "in vec2 in_MultiTexCoord0;"
185            "in vec4 in_Color;"
186            "varying vec4 pass_Color;"
187            "void main()"
188            "{"
189            "    gl_TexCoord[0] = vec4(in_MultiTexCoord0, 0.0, 0.0);"
190            "    pass_Color = in_Color;"
191            "    gl_Position = vec4(in_Vertex, 0.0, 1.0);"
192            "}",
193
194            "#version 130\n"
195            "varying vec4 pass_Color;"
196            "uniform sampler2D tex;"
197            "void main()"
198            "{"
199            "    vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
200            "    gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);"
201            "}");
202
203
204        data->initialised = 1;
205    }
206    else if (data->initialised && IsDestroying())
207    {
208#if 0
209        glDeleteBuffers(3, data->buflist);
210#endif
211        Shader::Destroy(data->shader[0]);
212        Shader::Destroy(data->shader[1]);
213        Shader::Destroy(data->shader[2]);
214        Shader::Destroy(data->shader[3]);
215        glDeleteTextures(1, data->texlist);
216
217        data->initialised = 0;
218    }
219
220#if 0
221    float const st = sinf(0.0005f * data->time);
222    float const ct = cosf(0.0005f * data->time);
223
224    GLfloat const verts[6][2] =
225    {
226        { -0.7f * (st + ct),  0.7f * (st - ct) },
227        {  0.7f * (st - ct),  0.7f * (st + ct) },
228        { -0.7f * (st - ct), -0.7f * (st + ct) },
229
230        { -0.7f * (st - ct), -0.7f * (st + ct) },
231        {  0.7f * (st - ct),  0.7f * (st + ct) },
232        {  0.7f * (st + ct), -0.7f * (st - ct) },
233    };
234
235    /* Using only 3 components breaks on Android for some reason. */
236    static GLfloat const cols[6][4] =
237    {
238        { 1.0f, 0.2f, 0.2f, 1.0f },
239        { 0.2f, 0.2f, 1.0f, 1.0f },
240        { 1.0f, 1.0f, 0.2f, 1.0f },
241
242        { 1.0f, 1.0f, 0.2f, 1.0f },
243        { 0.2f, 0.2f, 1.0f, 1.0f },
244        { 0.2f, 1.0f, 0.2f, 1.0f },
245    };
246
247    static GLfloat const tcs[6][2] =
248    {
249        { 0.0f, 1.0f }, { 1.0f, 1.0f }, { 0.0f, 0.0f },
250        { 0.0f, 0.0f }, { 1.0f, 1.0f }, { 1.0f, 0.0f },
251    };
252
253#if defined __CELLOS_LV2__
254    glEnableClientState(GL_VERTEX_ARRAY);
255    glEnableClientState(GL_COLOR_ARRAY);
256    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
257
258    glActiveTexture(GL_TEXTURE0);
259    glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
260
261    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
262    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), verts, GL_STATIC_DRAW);
263    glVertexPointer(2, GL_FLOAT, GL_FALSE, 0);
264
265    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
266    glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW);
267    glColorPointer(4, GL_FLOAT, GL_FALSE, 0);
268
269    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
270    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), tcs, GL_STATIC_DRAW);
271    glTexCoordPointer(2, GL_FLOAT, GL_FALSE, 0);
272
273    glDrawArrays(GL_TRIANGLES, 0, 6);
274
275    glDisableClientState(GL_VERTEX_ARRAY);
276    glDisableClientState(GL_COLOR_ARRAY);
277    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
278#else
279    data->shader[0]->Bind();
280    GLuint attr_pos, attr_col, attr_tex;
281    attr_pos = data->shader[0]->GetAttribLocation("in_Position");
282    attr_col = data->shader[0]->GetAttribLocation("in_Color");
283    attr_tex = data->shader[0]->GetAttribLocation("in_TexCoord");
284
285    glEnableVertexAttribArray(attr_pos);
286    glEnableVertexAttribArray(attr_col);
287    glEnableVertexAttribArray(attr_tex);
288
289    /* Bind texture */
290    glActiveTexture(GL_TEXTURE0);
291    glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
292
293    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
294    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), verts, GL_STATIC_DRAW);
295    glVertexAttribPointer(attr_pos, 2, GL_FLOAT, GL_FALSE, 0, 0);
296
297    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
298    glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW);
299    glVertexAttribPointer(attr_col, 4, GL_FLOAT, GL_FALSE, 0, 0);
300
301    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
302    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), tcs, GL_STATIC_DRAW);
303    glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, 0);
304
305    glBindBuffer(GL_ARRAY_BUFFER, 0);
306
307    glDrawArrays(GL_TRIANGLES, 0, 6);
308
309    glDisableVertexAttribArray(attr_pos);
310    glDisableVertexAttribArray(attr_col);
311    glDisableVertexAttribArray(attr_tex);
312#endif
313#endif
314
315    /* Reset GL states */
316    glMatrixMode(GL_PROJECTION);
317    glLoadIdentity();
318    glMatrixMode(GL_MODELVIEW);
319    glLoadIdentity();
320    glDisable(GL_TEXTURE_2D);
321    glBindBuffer(GL_ARRAY_BUFFER, 0);
322    glUseProgram(0);
323
324    /* Prepare our quad coordinates */
325    vec2i const layout(3, 3);
326    data->step = vec2(2.0f, -2.0f) / (3 * layout + vec2i(1));
327    data->orig = vec2(-1.0f, 1.0f) + data->step;
328    data->aa = data->orig;
329    data->bb = data->orig + 2.0f * data->step;
330
331    /* Generate a few random numbers */
332    float f1 = 0.5f + 0.5f * sinf(0.00034f * data->time);
333    float f2 = 0.5f + 0.5f * sinf(0.00053f * data->time + 1.0f);
334    float f3 = 0.5f + 0.5f * sinf(0.00072f * data->time + 4.0f);
335    float f4 = 0.5f + 0.5f * sinf(0.00091f * data->time + 8.0f);
336
337    GLfloat const colors[] = { f1, f2, f3, f4, f2, f1, f3, f1, f4,
338                               f3, f1, f4, f4, f3, f2, f1, f2, f3 };
339    GLfloat const texcoords[] = { f1, f3, f3, f2, f2, f4,
340                                  f2, f4, f4, f1, f1, f3 };
341
342    /* Quad #1: simple glBegin program */
343    glColor3f(0.8f, 0.5f, 0.2f);
344    glBegin(GL_TRIANGLES);
345        glVertex3f(data->aa.x, data->bb.y, 0.0f);
346        glVertex3f(data->bb.x, data->bb.y, 0.0f);
347        glVertex3f(data->bb.x, data->aa.y, 0.0f);
348
349        glVertex3f(data->bb.x, data->aa.y, 0.0f);
350        glVertex3f(data->aa.x, data->aa.y, 0.0f);
351        glVertex3f(data->aa.x, data->bb.y, 0.0f);
352    glEnd();
353
354    Advance();
355
356    /* Quad #2: glBegin program with varying color */
357    glBegin(GL_TRIANGLES);
358        glColor3f(f1, f2, f3);
359        glVertex3f(data->aa.x, data->bb.y, 0.0f);
360        glColor3f(f4, f2, f1);
361        glVertex3f(data->bb.x, data->bb.y, 0.0f);
362        glColor3f(f3, f1, f4);
363        glVertex3f(data->bb.x, data->aa.y, 0.0f);
364
365        glVertex3f(data->bb.x, data->aa.y, 0.0f);
366        glColor3f(f4, f3, f2);
367        glVertex3f(data->aa.x, data->aa.y, 0.0f);
368        glColor3f(f1, f2, f3);
369        glVertex3f(data->aa.x, data->bb.y, 0.0f);
370    glEnd();
371
372    Advance();
373
374    /* Quad #3: textured quad */
375    glEnable(GL_TEXTURE_2D);
376    glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
377    glColor3f(1.0f, 1.0f, 1.0f);
378    glBegin(GL_TRIANGLES);
379        glTexCoord2f(f1, f3);
380        glVertex3f(data->aa.x, data->bb.y, 0.0f);
381        glTexCoord2f(f3, f2);
382        glVertex3f(data->bb.x, data->bb.y, 0.0f);
383        glTexCoord2f(f2, f4);
384        glVertex3f(data->bb.x, data->aa.y, 0.0f);
385
386        glTexCoord2f(f2, f4);
387        glVertex3f(data->bb.x, data->aa.y, 0.0f);
388        glTexCoord2f(f4, f1);
389        glVertex3f(data->aa.x, data->aa.y, 0.0f);
390        glTexCoord2f(f1, f3);
391        glVertex3f(data->aa.x, data->bb.y, 0.0f);
392    glEnd();
393    glDisable(GL_TEXTURE_2D);
394
395    Advance();
396
397    /* Quad #4: set color in fragment shader */
398    data->shader[0]->Bind();
399    glColor3f(0.0f, 1.0f, 1.0f);
400    glBegin(GL_TRIANGLES);
401        glVertex3f(data->aa.x, data->bb.y, 0.0f);
402        glVertex3f(data->bb.x, data->bb.y, 0.0f);
403        glVertex3f(data->bb.x, data->aa.y, 0.0f);
404
405        glVertex3f(data->bb.x, data->aa.y, 0.0f);
406        glVertex3f(data->aa.x, data->aa.y, 0.0f);
407        glVertex3f(data->aa.x, data->bb.y, 0.0f);
408    glEnd();
409    glUseProgram(0);
410
411    Advance();
412
413    /* Quad #5: pass color from vertex shader to fragment shader */
414    data->shader[1]->Bind();
415    glColor3f(0.0f, 1.0f, 1.0f);
416    glBegin(GL_TRIANGLES);
417        glTexCoord2f(f1, f3);
418        glVertex3f(data->aa.x, data->bb.y, 0.0f);
419        glTexCoord2f(f3, f2);
420        glVertex3f(data->bb.x, data->bb.y, 0.0f);
421        glTexCoord2f(f2, f4);
422        glVertex3f(data->bb.x, data->aa.y, 0.0f);
423
424        glTexCoord2f(f2, f4);
425        glVertex3f(data->bb.x, data->aa.y, 0.0f);
426        glTexCoord2f(f4, f1);
427        glVertex3f(data->aa.x, data->aa.y, 0.0f);
428        glTexCoord2f(f1, f3);
429        glVertex3f(data->aa.x, data->bb.y, 0.0f);
430    glEnd();
431    glUseProgram(0);
432
433    Advance();
434
435    /* Quad #6: apply texture in fragment shader */
436    data->shader[2]->Bind();
437    glColor3f(0.0f, 1.0f, 1.0f);
438    glEnable(GL_TEXTURE_2D);
439    glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
440    glBegin(GL_TRIANGLES);
441        glTexCoord2f(f1, f3);
442        glVertex3f(data->aa.x, data->bb.y, 0.0f);
443        glTexCoord2f(f3, f2);
444        glVertex3f(data->bb.x, data->bb.y, 0.0f);
445        glTexCoord2f(f2, f4);
446        glVertex3f(data->bb.x, data->aa.y, 0.0f);
447
448        glTexCoord2f(f2, f4);
449        glVertex3f(data->bb.x, data->aa.y, 0.0f);
450        glTexCoord2f(f4, f1);
451        glVertex3f(data->aa.x, data->aa.y, 0.0f);
452        glTexCoord2f(f1, f3);
453        glVertex3f(data->aa.x, data->bb.y, 0.0f);
454    glEnd();
455    glUseProgram(0);
456    glDisable(GL_TEXTURE_2D);
457
458    Advance();
459
460    /* Quad #7: simple vertex buffer */
461    GLfloat const vertices1[] = { data->aa.x, data->bb.y, 0.0f,
462                                  data->bb.x, data->bb.y, 0.0f,
463                                  data->bb.x, data->aa.y, 0.0f,
464                                  data->bb.x, data->aa.y, 0.0f,
465                                  data->aa.x, data->aa.y, 0.0f,
466                                  data->aa.x, data->bb.y, 0.0f };
467
468    glEnableClientState(GL_COLOR_ARRAY);
469    glEnableClientState(GL_VERTEX_ARRAY);
470
471    glColorPointer(3, GL_FLOAT, 0, colors);
472    glVertexPointer(3, GL_FLOAT, 0, vertices1);
473    glDrawArrays(GL_TRIANGLES, 0, 6);
474
475    glDisableClientState(GL_VERTEX_ARRAY);
476    glDisableClientState(GL_COLOR_ARRAY);
477
478    Advance();
479
480    /* Quad #8: vertex buffer, apply texture and color in fragment shader */
481    data->shader[3]->Bind();
482    GLfloat const vertices2[] = { data->aa.x, data->bb.y, 0.0f,
483                                  data->bb.x, data->bb.y, 0.0f,
484                                  data->bb.x, data->aa.y, 0.0f,
485                                  data->bb.x, data->aa.y, 0.0f,
486                                  data->aa.x, data->aa.y, 0.0f,
487                                  data->aa.x, data->bb.y, 0.0f };
488
489    glEnableClientState(GL_VERTEX_ARRAY);
490    glEnableClientState(GL_COLOR_ARRAY);
491    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
492
493    glVertexPointer(3, GL_FLOAT, 0, vertices2);
494    glColorPointer(3, GL_FLOAT, 0, colors);
495    glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
496    glDrawArrays(GL_TRIANGLES, 0, 6);
497
498    glDisableClientState(GL_VERTEX_ARRAY);
499    glDisableClientState(GL_COLOR_ARRAY);
500    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
501
502    Advance();
503}
504
505void DebugQuad::Advance()
506{
507    data->aa.x += 3.0f * data->step.x;
508    data->bb.x += 3.0f * data->step.x;
509    if (data->bb.x > 1.0f)
510    {
511        data->aa.x = data->orig.x;
512        data->bb.x = data->orig.x + 2.0f * data->step.x;
513        data->aa.y += 3.0f * data->step.y;
514        data->bb.y += 3.0f * data->step.y;
515    }
516}
517
518DebugQuad::~DebugQuad()
519{
520    delete data;
521}
522
523} /* namespace lol */
524
Note: See TracBrowser for help on using the repository browser.