source: trunk/src/debugquad.cpp @ 702

Last change on this file since 702 was 702, checked in by sam, 11 years ago

The DebugQuad object now rotates with time.

  • Property svn:keywords set to Id
File size: 7.4 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 <cstdio>
16#include <cmath>
17
18#include "core.h"
19#include "lolgl.h"
20#include "loldebug.h"
21
22namespace lol
23{
24
25/*
26 * DebugQuad implementation class
27 */
28
29class DebugQuadData
30{
31    friend class DebugQuad;
32
33private:
34    int initialised;
35    float time;
36#if defined HAVE_GL_2X || defined HAVE_GLES_2X
37    GLuint buflist[3];
38    Shader *shader;
39#elif defined HAVE_GL_1X || defined HAVE_GLES_1X
40    GLuint buflist[3];
41#endif
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 = 0.0f;
55}
56
57void DebugQuad::TickGame(float deltams)
58{
59    Entity::TickGame(deltams);
60
61    data->time += deltams;
62}
63
64void DebugQuad::TickDraw(float deltams)
65{
66    Entity::TickDraw(deltams);
67
68    if (IsDestroying())
69    {
70        if (data->initialised)
71        {
72#if defined HAVE_GL_2X || defined HAVE_GLES_2X
73            glDeleteBuffers(3, data->buflist);
74            Shader::Destroy(data->shader);
75#elif defined HAVE_GL_1X || defined HAVE_GLES_1X
76            glDeleteBuffers(3, data->buflist);
77#endif
78            glDeleteTextures(1, data->texlist);
79            data->initialised = 0;
80        }
81    }
82    else if (!data->initialised)
83    {
84#if defined HAVE_GL_2X || defined HAVE_GLES_2X
85        glGenBuffers(3, data->buflist);
86
87        static char const *vertexshader =
88            "#version 130\n"
89            "in vec2 in_Position;\n"
90            "in vec4 in_Color;\n"
91            "in vec2 in_TexCoord;\n"
92            "out vec4 pass_Color;\n"
93            "void main()\n"
94            "{\n"
95            "    gl_Position = vec4(in_Position, 0.0f, 1.0f);\n"
96            "    gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
97            "    pass_Color = in_Color;\n"
98            "}\n";
99        static char const *fragmentshader =
100            "#version 130\n"
101            "in vec4 pass_Color;\n"
102            "uniform sampler2D in_Texture;\n"
103            "void main()\n"
104            "{\n"
105            "    vec4 col = pass_Color;\n"
106            "    vec4 tex = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
107            "    gl_FragColor = col * tex;\n"
108            "}\n";
109        data->shader = Shader::Create(vertexshader, fragmentshader);
110#elif defined HAVE_GL_1X || defined HAVE_GLES_1X
111        glGenBuffers(3, data->buflist);
112#endif
113        glGenTextures(1, data->texlist);
114
115        glActiveTexture(GL_TEXTURE0);
116        glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
117        for (int j = 0; j < 32; j++)
118            for (int i = 0; i < 32; i++)
119            {
120                uint8_t wb = (((i / 2) ^ (j / 2)) & 1) * 0xff;
121                data->image[0][(j * 32 + i) * 4 + 0] = wb;
122                data->image[0][(j * 32 + i) * 4 + 1] = wb;
123                data->image[0][(j * 32 + i) * 4 + 2] = wb;
124                data->image[0][(j * 32 + i) * 4 + 3] = 255;
125            }
126        /* Use GL_RGBA instead of 4 for the internal format (Android) */
127        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0,
128                     GL_RGBA, GL_UNSIGNED_BYTE, data->image[0]);
129        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
130        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
131
132        data->initialised = 1;
133    }
134
135    float const st = sinf(0.0005f * data->time);
136    float const ct = cosf(0.0005f * data->time);
137
138    GLfloat const verts[6][2] =
139    {
140        { -0.7f * (st + ct),  0.7f * (st - ct) },
141        {  0.7f * (st - ct),  0.7f * (st + ct) },
142        { -0.7f * (st - ct), -0.7f * (st + ct) },
143
144        { -0.7f * (st - ct), -0.7f * (st + ct) },
145        {  0.7f * (st - ct),  0.7f * (st + ct) },
146        {  0.7f * (st + ct), -0.7f * (st - ct) },
147    };
148
149    /* Using only 3 components breaks on Android for some reason. */
150    static GLfloat const cols[6][4] =
151    {
152        { 0.8f, 0.2f, 0.2f, 1.0f },
153        { 0.2f, 0.2f, 0.8f, 1.0f },
154        { 0.8f, 0.8f, 0.2f, 1.0f },
155
156        { 0.8f, 0.8f, 0.2f, 1.0f },
157        { 0.2f, 0.2f, 0.8f, 1.0f },
158        { 0.2f, 0.8f, 0.2f, 1.0f },
159    };
160
161    static GLfloat const tcs[6][2] =
162    {
163        { 0.0f, 1.0f }, { 1.0f, 1.0f }, { 0.0f, 0.0f },
164        { 0.0f, 0.0f }, { 1.0f, 1.0f }, { 1.0f, 0.0f },
165    };
166
167#if defined HAVE_GL_2X || defined HAVE_GLES_2X
168    data->shader->Bind();
169    GLuint attr_pos, attr_col, attr_tex;
170    attr_pos = data->shader->GetAttribLocation("in_Position");
171    attr_col = data->shader->GetAttribLocation("in_Color");
172    attr_tex = data->shader->GetAttribLocation("in_TexCoord");
173
174    glEnableVertexAttribArray(attr_pos);
175    glEnableVertexAttribArray(attr_col);
176    glEnableVertexAttribArray(attr_tex);
177
178    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
179    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), verts, GL_STATIC_DRAW);
180    glVertexAttribPointer(attr_pos, 2, GL_FLOAT, GL_FALSE, 0, 0);
181
182    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
183    glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW);
184    glVertexAttribPointer(attr_col, 4, GL_FLOAT, GL_FALSE, 0, 0);
185
186    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
187    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), tcs, GL_STATIC_DRAW);
188    glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, 0);
189
190    glDrawArrays(GL_TRIANGLES, 0, 6);
191
192    glDisableVertexAttribArray(attr_pos);
193    glDisableVertexAttribArray(attr_col);
194    glDisableVertexAttribArray(attr_tex);
195#elif defined HAVE_GL_1X || defined HAVE_GLES_1X
196    /* Reset all model-view-projection matrices */
197    glMatrixMode(GL_PROJECTION);
198    glPushMatrix();
199    glLoadIdentity();
200    glMatrixMode(GL_MODELVIEW);
201    glPushMatrix();
202    glLoadIdentity();
203
204    /* Set up state machine */
205    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
206    glEnable(GL_TEXTURE_2D);
207    glEnableClientState(GL_VERTEX_ARRAY);
208    glEnableClientState(GL_COLOR_ARRAY);
209
210    glActiveTexture(GL_TEXTURE0);
211    glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
212
213    /* Bind vertex, color and texture coordinate buffers */
214#if defined HAVE_GL_1X
215    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
216    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), verts, GL_STATIC_DRAW);
217    glVertexPointer(2, GL_FLOAT, 0, NULL);
218
219    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
220    glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW);
221    glColorPointer(4, GL_FLOAT, 0, NULL);
222
223    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
224    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), tcs, GL_STATIC_DRAW);
225    glTexCoordPointer(2, GL_FLOAT, 0, NULL);
226#else
227    glVertexPointer(2, GL_FLOAT, 0, verts);
228    glColorPointer(4, GL_FLOAT, 0, cols);
229    glTexCoordPointer(2, GL_FLOAT, 0, tcs);
230#endif
231
232    /* Draw arrays */
233    glDrawArrays(GL_TRIANGLES, 0, 6);
234
235    /* Disable state machine features */
236    glDisableClientState(GL_VERTEX_ARRAY);
237    glDisableClientState(GL_COLOR_ARRAY);
238    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
239
240    /* Restore matrices */
241    glMatrixMode(GL_PROJECTION);
242    glPopMatrix();
243    glMatrixMode(GL_MODELVIEW);
244    glPopMatrix();
245#endif
246}
247
248DebugQuad::~DebugQuad()
249{
250    delete data;
251}
252
253} /* namespace lol */
254
Note: See TracBrowser for help on using the repository browser.