source: trunk/src/debugquad.cpp @ 764

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

debug: re-add old fixed pipeline code to the debugquad.

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