source: trunk/src/debugquad.cpp @ 701

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

Add texturing support to DebugQuad. Works with OpenGL (fixed pipeline
and programmable pipeline versions) and OpenGL ES 1.x for now.

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