source: trunk/src/debugquad.cpp @ 770

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

test: start writing really minimalist GL rendering tests.

  • Property svn:keywords set to Id
File size: 9.2 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
31class DebugQuadData
32{
33    friend class DebugQuad;
34
35private:
36    vec2 orig, step, aa, bb;
37
38    int initialised;
39    float time;
40    GLuint buflist[3];
41    Shader *shader;
42    GLuint texlist[1];
43    uint8_t image[1][32 * 32 * 4];
44};
45
46/*
47 * Public DebugQuad class
48 */
49
50DebugQuad::DebugQuad()
51  : data(new DebugQuadData())
52{
53    data->initialised = 0;
54    data->time = RandF(10000.0f);
55
56    drawgroup = DRAWGROUP_HUD;
57}
58
59void DebugQuad::TickGame(float deltams)
60{
61    Entity::TickGame(deltams);
62
63    data->time += deltams;
64}
65
66void DebugQuad::TickDraw(float deltams)
67{
68    Entity::TickDraw(deltams);
69
70    if (!data->initialised && !IsDestroying())
71    {
72        glGenBuffers(3, data->buflist);
73
74        static char const *vertexshader =
75            "//#version 130\n"
76            "varying vec2 in_Position;\n"
77            "varying vec4 in_Color;\n"
78            "varying vec2 in_TexCoord;\n"
79            "varying vec4 pass_Color;\n"
80            "void main()\n"
81            "{\n"
82            "gl_TexCoord[0] = gl_MultiTexCoord0;\n"
83            "    gl_Position = vec4(in_Position, 0.0f, 1.0f);\n"
84            "    gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
85            "    pass_Color = in_Color;\n"
86            "}\n";
87        static char const *fragmentshader =
88            "//#version 130\n"
89            "varying vec4 pass_Color;\n"
90            "uniform sampler2D in_Texture;\n"
91            "void main()\n"
92            "{\n"
93            "    vec4 col = pass_Color;\n"
94            "    vec4 tex = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
95            "    gl_FragColor = col * tex;\n"
96            "    gl_FragColor = vec4(1.0, 1.0, 1.0, 0.0);\n"
97            "}\n";
98        data->shader = Shader::Create(vertexshader, fragmentshader);
99        glGenTextures(1, data->texlist);
100
101        glEnable(GL_TEXTURE_2D);
102        glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
103        for (int j = 0; j < 32; j++)
104            for (int i = 0; i < 32; i++)
105            {
106                uint8_t wb = (((i / 2) ^ (j / 2)) & 1) * 0xff;
107                data->image[0][(j * 32 + i) * 4 + 0] = wb;
108                data->image[0][(j * 32 + i) * 4 + 1] = wb;
109                data->image[0][(j * 32 + i) * 4 + 2] = wb;
110                data->image[0][(j * 32 + i) * 4 + 3] = wb;
111            }
112        /* Use GL_RGBA instead of 4 for the internal format (Android) */
113        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0,
114                     GL_RGBA, GL_UNSIGNED_BYTE, data->image[0]);
115        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
116        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
117
118        data->initialised = 1;
119    }
120    else if (data->initialised && IsDestroying())
121    {
122        glDeleteBuffers(3, data->buflist);
123        Shader::Destroy(data->shader);
124        glDeleteTextures(1, data->texlist);
125
126        data->initialised = 0;
127    }
128
129    float const st = sinf(0.0005f * data->time);
130    float const ct = cosf(0.0005f * data->time);
131
132    GLfloat const verts[6][2] =
133    {
134        { -0.7f * (st + ct),  0.7f * (st - ct) },
135        {  0.7f * (st - ct),  0.7f * (st + ct) },
136        { -0.7f * (st - ct), -0.7f * (st + ct) },
137
138        { -0.7f * (st - ct), -0.7f * (st + ct) },
139        {  0.7f * (st - ct),  0.7f * (st + ct) },
140        {  0.7f * (st + ct), -0.7f * (st - ct) },
141    };
142
143    /* Using only 3 components breaks on Android for some reason. */
144    static GLfloat const cols[6][4] =
145    {
146        { 1.0f, 0.2f, 0.2f, 1.0f },
147        { 0.2f, 0.2f, 1.0f, 1.0f },
148        { 1.0f, 1.0f, 0.2f, 1.0f },
149
150        { 1.0f, 1.0f, 0.2f, 1.0f },
151        { 0.2f, 0.2f, 1.0f, 1.0f },
152        { 0.2f, 1.0f, 0.2f, 1.0f },
153    };
154
155    static GLfloat const tcs[6][2] =
156    {
157        { 0.0f, 1.0f }, { 1.0f, 1.0f }, { 0.0f, 0.0f },
158        { 0.0f, 0.0f }, { 1.0f, 1.0f }, { 1.0f, 0.0f },
159    };
160
161#if defined __CELLOS_LV2__
162    glEnableClientState(GL_VERTEX_ARRAY);
163    glEnableClientState(GL_COLOR_ARRAY);
164    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
165
166    glActiveTexture(GL_TEXTURE0);
167    glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
168
169    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
170    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), verts, GL_STATIC_DRAW);
171    glVertexPointer(2, GL_FLOAT, GL_FALSE, 0);
172
173    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
174    glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW);
175    glColorPointer(4, GL_FLOAT, GL_FALSE, 0);
176
177    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
178    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), tcs, GL_STATIC_DRAW);
179    glTexCoordPointer(2, GL_FLOAT, GL_FALSE, 0);
180
181    glDrawArrays(GL_TRIANGLES, 0, 6);
182
183    glDisableClientState(GL_VERTEX_ARRAY);
184    glDisableClientState(GL_COLOR_ARRAY);
185    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
186#else
187    data->shader->Bind();
188    GLuint attr_pos, attr_col, attr_tex;
189    attr_pos = data->shader->GetAttribLocation("in_Position");
190    attr_col = data->shader->GetAttribLocation("in_Color");
191    attr_tex = data->shader->GetAttribLocation("in_TexCoord");
192
193    glEnableVertexAttribArray(attr_pos);
194    glEnableVertexAttribArray(attr_col);
195    glEnableVertexAttribArray(attr_tex);
196
197    /* Bind texture */
198    glActiveTexture(GL_TEXTURE0);
199    glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
200
201    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
202    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), verts, GL_STATIC_DRAW);
203    glVertexAttribPointer(attr_pos, 2, GL_FLOAT, GL_FALSE, 0, 0);
204
205    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
206    glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW);
207    glVertexAttribPointer(attr_col, 4, GL_FLOAT, GL_FALSE, 0, 0);
208
209    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
210    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), tcs, GL_STATIC_DRAW);
211    glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, 0);
212
213    glDrawArrays(GL_TRIANGLES, 0, 6);
214
215    glDisableVertexAttribArray(attr_pos);
216    glDisableVertexAttribArray(attr_col);
217    glDisableVertexAttribArray(attr_tex);
218#endif
219
220    /* Reset GL states */
221    glMatrixMode(GL_PROJECTION);
222    glLoadIdentity();
223    glMatrixMode(GL_MODELVIEW);
224    glLoadIdentity();
225    glDisable(GL_TEXTURE_2D);
226    glUseProgram(0);
227
228    /* Prepare our quad coordinates */
229    vec2i const layout(4, 3);
230    data->step = vec2(2.0f, -2.0f) / (2 * layout + vec2i(1));
231    data->orig = vec2(-1.0f, 1.0f) + data->step;
232    data->aa = data->orig;
233    data->bb = data->orig + data->step;
234
235    /* Generate a few random numbers */
236    float f1 = 0.5f + 0.5f * sinf(0.00034f * data->time);
237    float f2 = 0.5f + 0.5f * sinf(0.00053f * data->time + 1.0f);
238    float f3 = 0.5f + 0.5f * sinf(0.00072f * data->time + 4.0f);
239    float f4 = 0.5f + 0.5f * sinf(0.00091f * data->time + 8.0f);
240
241    /* Quad #1: simple glBegin program */
242    glColor3f(1.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
253    Advance();
254
255    /* Quad #2: glBegin program with varying color */
256    glBegin(GL_TRIANGLES);
257        glColor3f(f1, f2, f3);
258        glVertex3f(data->aa.x, data->bb.y, 0.0f);
259        glColor3f(f4, f2, f1);
260        glVertex3f(data->bb.x, data->bb.y, 0.0f);
261        glColor3f(f3, f1, f4);
262        glVertex3f(data->bb.x, data->aa.y, 0.0f);
263
264        glVertex3f(data->bb.x, data->aa.y, 0.0f);
265        glColor3f(f4, f3, f2);
266        glVertex3f(data->aa.x, data->aa.y, 0.0f);
267        glColor3f(f1, f2, f3);
268        glVertex3f(data->aa.x, data->bb.y, 0.0f);
269    glEnd();
270
271    Advance();
272
273    /* Quad #3: textured quad */
274    glEnable(GL_TEXTURE_2D);
275    glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
276    glColor3f(1.0f, 1.0f, 1.0f);
277    glBegin(GL_TRIANGLES);
278        glTexCoord2f(f1, f3);
279        glVertex3f(data->aa.x, data->bb.y, 0.0f);
280        glTexCoord2f(f3, f2);
281        glVertex3f(data->bb.x, data->bb.y, 0.0f);
282        glTexCoord2f(f2, f4);
283        glVertex3f(data->bb.x, data->aa.y, 0.0f);
284
285        glTexCoord2f(f2, f4);
286        glVertex3f(data->bb.x, data->aa.y, 0.0f);
287        glTexCoord2f(f4, f1);
288        glVertex3f(data->aa.x, data->aa.y, 0.0f);
289        glTexCoord2f(f1, f3);
290        glVertex3f(data->aa.x, data->bb.y, 0.0f);
291    glEnd();
292
293    Advance();
294
295    /* Quad #4: vertex buffer */
296}
297
298void DebugQuad::Advance()
299{
300    data->aa.x += 2.0f * data->step.x;
301    data->bb.x += 2.0f * data->step.x;
302    if (data->bb.x > 1.0f)
303    {
304        data->aa.x = data->orig.x;
305        data->bb.x = data->orig.x + data->step.x;
306        data->aa.y += 2.0f * data->step.y;
307        data->bb.y += 2.0f * data->step.y;
308    }
309}
310
311DebugQuad::~DebugQuad()
312{
313    delete data;
314}
315
316} /* namespace lol */
317
Note: See TracBrowser for help on using the repository browser.