source: trunk/src/debugquad.cpp @ 703

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

Add transparency to DebugQuad and ensure it gets rendered over everything.

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