source: trunk/src/debugquad.cpp @ 774

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

debug: more complex vertex buffer test.

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