source: trunk/src/debugquad.cpp @ 772

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

debug: display larger debug quads to avoid wasting space.

  • Property svn:keywords set to Id
File size: 12.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
31#define NUM_SHADERS 3
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        glGenBuffers(3, data->buflist);
75
76        static char const *vertexshader =
77            "//#version 130\n"
78            "varying vec2 in_Position;\n"
79            "varying vec4 in_Color;\n"
80            "varying vec2 in_TexCoord;\n"
81            "varying vec4 pass_Color;\n"
82            "void main()\n"
83            "{\n"
84            "gl_TexCoord[0] = gl_MultiTexCoord0;\n"
85            "    gl_Position = vec4(in_Position, 0.0f, 1.0f);\n"
86            "    gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
87            "    pass_Color = in_Color;\n"
88            "}\n";
89        static char const *fragmentshader =
90            "//#version 130\n"
91            "varying vec4 pass_Color;\n"
92            "uniform sampler2D in_Texture;\n"
93            "void main()\n"
94            "{\n"
95            "    vec4 col = pass_Color;\n"
96            "    vec4 tex = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
97            "    gl_FragColor = col * tex;\n"
98            "    gl_FragColor = vec4(1.0, 1.0, 1.0, 0.0);\n"
99            "}\n";
100        data->shader[0] = Shader::Create(vertexshader, fragmentshader);
101        glGenTextures(1, data->texlist);
102
103        glEnable(GL_TEXTURE_2D);
104        glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
105        for (int j = 0; j < 32; j++)
106            for (int i = 0; i < 32; i++)
107            {
108                uint8_t wb = (((i / 2) ^ (j / 2)) & 1) * 0xff;
109                data->image[0][(j * 32 + i) * 4 + 0] = wb;
110                data->image[0][(j * 32 + i) * 4 + 1] = wb;
111                data->image[0][(j * 32 + i) * 4 + 2] = wb;
112                data->image[0][(j * 32 + i) * 4 + 3] = wb;
113            }
114        /* Use GL_RGBA instead of 4 for the internal format (Android) */
115        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0,
116                     GL_RGBA, GL_UNSIGNED_BYTE, data->image[0]);
117        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
118        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
119
120        /* Quad #4: create texture in fragment shader */
121        data->shader[0] = Shader::Create(
122            "#version 120\n"
123            "void main()"
124            "{"
125            "    gl_Position = gl_Vertex;"
126            "}",
127
128            "#version 120\n"
129            "void main()"
130            "{"
131            "    float dx = mod(gl_FragCoord.x * gl_FragCoord.y, 2.0);"
132            "    float dy = mod(gl_FragCoord.x * 0.125, 1.0);"
133            "    float dz = mod(gl_FragCoord.y * 0.125, 1.0);"
134            "    gl_FragColor = vec4(dx, dy, dz, 1.0);"
135            "}");
136
137        /* Quad #5: pass color from vertex shader to fragment shader */
138        data->shader[1] = Shader::Create(
139            "#version 120\n"
140            "varying vec4 color;"
141            "void main()"
142            "{"
143            "    float r = gl_MultiTexCoord0.x;"
144            "    float g = gl_MultiTexCoord0.y;"
145            "    color = vec4(1.0 - r, 1.0 - g, r, 1.0);"
146            "    gl_Position = gl_Vertex;"
147            "}",
148
149            "#version 120\n"
150            "varying vec4 color;"
151            "void main()"
152            "{"
153            "    gl_FragColor = color;"
154            "}");
155
156        /* Quad #6: apply texture in fragment shader */
157        data->shader[2] = Shader::Create(
158            "void main()"
159            "{"
160            "    gl_TexCoord[0] = gl_MultiTexCoord0;"
161            "    gl_Position = gl_Vertex;"
162            "}",
163
164            "uniform sampler2D tex;"
165            "void main()"
166            "{"
167            "    gl_FragColor = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
168            "}");
169
170        data->initialised = 1;
171    }
172    else if (data->initialised && IsDestroying())
173    {
174        glDeleteBuffers(3, data->buflist);
175        Shader::Destroy(data->shader[0]);
176        Shader::Destroy(data->shader[1]);
177        Shader::Destroy(data->shader[2]);
178        glDeleteTextures(1, data->texlist);
179
180        data->initialised = 0;
181    }
182
183    float const st = sinf(0.0005f * data->time);
184    float const ct = cosf(0.0005f * data->time);
185
186    GLfloat const verts[6][2] =
187    {
188        { -0.7f * (st + ct),  0.7f * (st - ct) },
189        {  0.7f * (st - ct),  0.7f * (st + ct) },
190        { -0.7f * (st - ct), -0.7f * (st + ct) },
191
192        { -0.7f * (st - ct), -0.7f * (st + ct) },
193        {  0.7f * (st - ct),  0.7f * (st + ct) },
194        {  0.7f * (st + ct), -0.7f * (st - ct) },
195    };
196
197    /* Using only 3 components breaks on Android for some reason. */
198    static GLfloat const cols[6][4] =
199    {
200        { 1.0f, 0.2f, 0.2f, 1.0f },
201        { 0.2f, 0.2f, 1.0f, 1.0f },
202        { 1.0f, 1.0f, 0.2f, 1.0f },
203
204        { 1.0f, 1.0f, 0.2f, 1.0f },
205        { 0.2f, 0.2f, 1.0f, 1.0f },
206        { 0.2f, 1.0f, 0.2f, 1.0f },
207    };
208
209    static GLfloat const tcs[6][2] =
210    {
211        { 0.0f, 1.0f }, { 1.0f, 1.0f }, { 0.0f, 0.0f },
212        { 0.0f, 0.0f }, { 1.0f, 1.0f }, { 1.0f, 0.0f },
213    };
214
215#if defined __CELLOS_LV2__
216    glEnableClientState(GL_VERTEX_ARRAY);
217    glEnableClientState(GL_COLOR_ARRAY);
218    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
219
220    glActiveTexture(GL_TEXTURE0);
221    glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
222
223    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
224    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), verts, GL_STATIC_DRAW);
225    glVertexPointer(2, GL_FLOAT, GL_FALSE, 0);
226
227    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
228    glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW);
229    glColorPointer(4, GL_FLOAT, GL_FALSE, 0);
230
231    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
232    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), tcs, GL_STATIC_DRAW);
233    glTexCoordPointer(2, GL_FLOAT, GL_FALSE, 0);
234
235    glDrawArrays(GL_TRIANGLES, 0, 6);
236
237    glDisableClientState(GL_VERTEX_ARRAY);
238    glDisableClientState(GL_COLOR_ARRAY);
239    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
240#else
241    data->shader[0]->Bind();
242    GLuint attr_pos, attr_col, attr_tex;
243    attr_pos = data->shader[0]->GetAttribLocation("in_Position");
244    attr_col = data->shader[0]->GetAttribLocation("in_Color");
245    attr_tex = data->shader[0]->GetAttribLocation("in_TexCoord");
246
247    glEnableVertexAttribArray(attr_pos);
248    glEnableVertexAttribArray(attr_col);
249    glEnableVertexAttribArray(attr_tex);
250
251    /* Bind texture */
252    glActiveTexture(GL_TEXTURE0);
253    glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
254
255    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
256    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), verts, GL_STATIC_DRAW);
257    glVertexAttribPointer(attr_pos, 2, GL_FLOAT, GL_FALSE, 0, 0);
258
259    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
260    glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW);
261    glVertexAttribPointer(attr_col, 4, GL_FLOAT, GL_FALSE, 0, 0);
262
263    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
264    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), tcs, GL_STATIC_DRAW);
265    glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, 0);
266
267    glDrawArrays(GL_TRIANGLES, 0, 6);
268
269    glDisableVertexAttribArray(attr_pos);
270    glDisableVertexAttribArray(attr_col);
271    glDisableVertexAttribArray(attr_tex);
272#endif
273
274    /* Reset GL states */
275    glMatrixMode(GL_PROJECTION);
276    glLoadIdentity();
277    glMatrixMode(GL_MODELVIEW);
278    glLoadIdentity();
279    glDisable(GL_TEXTURE_2D);
280    glUseProgram(0);
281
282    /* Prepare our quad coordinates */
283    vec2i const layout(4, 3);
284    data->step = vec2(2.0f, -2.0f) / (3 * layout + vec2i(1));
285    data->orig = vec2(-1.0f, 1.0f) + data->step;
286    data->aa = data->orig;
287    data->bb = data->orig + 2.0f * data->step;
288
289    /* Generate a few random numbers */
290    float f1 = 0.5f + 0.5f * sinf(0.00034f * data->time);
291    float f2 = 0.5f + 0.5f * sinf(0.00053f * data->time + 1.0f);
292    float f3 = 0.5f + 0.5f * sinf(0.00072f * data->time + 4.0f);
293    float f4 = 0.5f + 0.5f * sinf(0.00091f * data->time + 8.0f);
294
295    /* Quad #1: simple glBegin program */
296    glColor3f(0.8f, 0.5f, 0.2f);
297    glBegin(GL_TRIANGLES);
298        glVertex3f(data->aa.x, data->bb.y, 0.0f);
299        glVertex3f(data->bb.x, data->bb.y, 0.0f);
300        glVertex3f(data->bb.x, data->aa.y, 0.0f);
301
302        glVertex3f(data->bb.x, data->aa.y, 0.0f);
303        glVertex3f(data->aa.x, data->aa.y, 0.0f);
304        glVertex3f(data->aa.x, data->bb.y, 0.0f);
305    glEnd();
306
307    Advance();
308
309    /* Quad #2: glBegin program with varying color */
310    glBegin(GL_TRIANGLES);
311        glColor3f(f1, f2, f3);
312        glVertex3f(data->aa.x, data->bb.y, 0.0f);
313        glColor3f(f4, f2, f1);
314        glVertex3f(data->bb.x, data->bb.y, 0.0f);
315        glColor3f(f3, f1, f4);
316        glVertex3f(data->bb.x, data->aa.y, 0.0f);
317
318        glVertex3f(data->bb.x, data->aa.y, 0.0f);
319        glColor3f(f4, f3, f2);
320        glVertex3f(data->aa.x, data->aa.y, 0.0f);
321        glColor3f(f1, f2, f3);
322        glVertex3f(data->aa.x, data->bb.y, 0.0f);
323    glEnd();
324
325    Advance();
326
327    /* Quad #3: textured quad */
328    glEnable(GL_TEXTURE_2D);
329    glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
330    glColor3f(1.0f, 1.0f, 1.0f);
331    glBegin(GL_TRIANGLES);
332        glTexCoord2f(f1, f3);
333        glVertex3f(data->aa.x, data->bb.y, 0.0f);
334        glTexCoord2f(f3, f2);
335        glVertex3f(data->bb.x, data->bb.y, 0.0f);
336        glTexCoord2f(f2, f4);
337        glVertex3f(data->bb.x, data->aa.y, 0.0f);
338
339        glTexCoord2f(f2, f4);
340        glVertex3f(data->bb.x, data->aa.y, 0.0f);
341        glTexCoord2f(f4, f1);
342        glVertex3f(data->aa.x, data->aa.y, 0.0f);
343        glTexCoord2f(f1, f3);
344        glVertex3f(data->aa.x, data->bb.y, 0.0f);
345    glEnd();
346
347    Advance();
348
349    /* Quad #4: set color in fragment shader */
350    data->shader[0]->Bind();
351    glColor3f(0.0f, 1.0f, 1.0f);
352    glBegin(GL_TRIANGLES);
353        glVertex3f(data->aa.x, data->bb.y, 0.0f);
354        glVertex3f(data->bb.x, data->bb.y, 0.0f);
355        glVertex3f(data->bb.x, data->aa.y, 0.0f);
356
357        glVertex3f(data->bb.x, data->aa.y, 0.0f);
358        glVertex3f(data->aa.x, data->aa.y, 0.0f);
359        glVertex3f(data->aa.x, data->bb.y, 0.0f);
360    glEnd();
361    glUseProgram(0);
362
363    Advance();
364
365    /* Quad #5: pass color from vertex shader to fragment shader */
366    data->shader[1]->Bind();
367    glColor3f(0.0f, 1.0f, 1.0f);
368    glBegin(GL_TRIANGLES);
369        glTexCoord2f(f1, f3);
370        glVertex3f(data->aa.x, data->bb.y, 0.0f);
371        glTexCoord2f(f3, f2);
372        glVertex3f(data->bb.x, data->bb.y, 0.0f);
373        glTexCoord2f(f2, f4);
374        glVertex3f(data->bb.x, data->aa.y, 0.0f);
375
376        glTexCoord2f(f2, f4);
377        glVertex3f(data->bb.x, data->aa.y, 0.0f);
378        glTexCoord2f(f4, f1);
379        glVertex3f(data->aa.x, data->aa.y, 0.0f);
380        glTexCoord2f(f1, f3);
381        glVertex3f(data->aa.x, data->bb.y, 0.0f);
382    glEnd();
383    glUseProgram(0);
384
385    Advance();
386
387    /* Quad #6: apply texture in fragment shader */
388    data->shader[2]->Bind();
389    glColor3f(0.0f, 1.0f, 1.0f);
390    glEnable(GL_TEXTURE_2D);
391    glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
392    glBegin(GL_TRIANGLES);
393        glTexCoord2f(f1, f3);
394        glVertex3f(data->aa.x, data->bb.y, 0.0f);
395        glTexCoord2f(f3, f2);
396        glVertex3f(data->bb.x, data->bb.y, 0.0f);
397        glTexCoord2f(f2, f4);
398        glVertex3f(data->bb.x, data->aa.y, 0.0f);
399
400        glTexCoord2f(f2, f4);
401        glVertex3f(data->bb.x, data->aa.y, 0.0f);
402        glTexCoord2f(f4, f1);
403        glVertex3f(data->aa.x, data->aa.y, 0.0f);
404        glTexCoord2f(f1, f3);
405        glVertex3f(data->aa.x, data->bb.y, 0.0f);
406    glEnd();
407    glUseProgram(0);
408
409    Advance();
410
411}
412
413void DebugQuad::Advance()
414{
415    data->aa.x += 3.0f * data->step.x;
416    data->bb.x += 3.0f * data->step.x;
417    if (data->bb.x > 1.0f)
418    {
419        data->aa.x = data->orig.x;
420        data->bb.x = data->orig.x + 2.0f * data->step.x;
421        data->aa.y += 3.0f * data->step.y;
422        data->bb.y += 3.0f * data->step.y;
423    }
424}
425
426DebugQuad::~DebugQuad()
427{
428    delete data;
429}
430
431} /* namespace lol */
432
Note: See TracBrowser for help on using the repository browser.