source: trunk/src/debugquad.cpp @ 735

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

core: get rid of now useless <cstdio> includes.

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