source: trunk/src/debugquad.cpp @ 745

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

video: enforce shader-enabled GL implementations, getting rid of a lot
of code.

  • Property svn:keywords set to Id
File size: 5.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
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    GLuint buflist[3];
36    Shader *shader;
37    GLuint texlist[1];
38    uint8_t image[1][32 * 32 * 4];
39};
40
41/*
42 * Public DebugQuad class
43 */
44
45DebugQuad::DebugQuad()
46  : data(new DebugQuadData())
47{
48    data->initialised = 0;
49    data->time = RandF(10000.0f);
50
51    drawgroup = DRAWGROUP_HUD;
52}
53
54void DebugQuad::TickGame(float deltams)
55{
56    Entity::TickGame(deltams);
57
58    data->time += deltams;
59}
60
61void DebugQuad::TickDraw(float deltams)
62{
63    Entity::TickDraw(deltams);
64
65    if (IsDestroying())
66    {
67        if (data->initialised)
68        {
69            glDeleteBuffers(3, data->buflist);
70            Shader::Destroy(data->shader);
71            glDeleteTextures(1, data->texlist);
72            data->initialised = 0;
73        }
74    }
75    else if (!data->initialised)
76    {
77        glGenBuffers(3, data->buflist);
78
79        static char const *vertexshader =
80            "#version 130\n"
81            "in vec2 in_Position;\n"
82            "in vec4 in_Color;\n"
83            "in vec2 in_TexCoord;\n"
84            "out vec4 pass_Color;\n"
85            "void main()\n"
86            "{\n"
87            "    gl_Position = vec4(in_Position, 0.0f, 1.0f);\n"
88            "    gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
89            "    pass_Color = in_Color;\n"
90            "}\n";
91        static char const *fragmentshader =
92            "#version 130\n"
93            "in vec4 pass_Color;\n"
94            "uniform sampler2D in_Texture;\n"
95            "void main()\n"
96            "{\n"
97            "    vec4 col = pass_Color;\n"
98            "    vec4 tex = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
99            "    gl_FragColor = col * tex;\n"
100            "}\n";
101        data->shader = Shader::Create(vertexshader, fragmentshader);
102        glGenTextures(1, data->texlist);
103
104        glEnable(GL_TEXTURE_2D);
105        glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
106        for (int j = 0; j < 32; j++)
107            for (int i = 0; i < 32; i++)
108            {
109                uint8_t wb = (((i / 2) ^ (j / 2)) & 1) * 0xff;
110                data->image[0][(j * 32 + i) * 4 + 0] = wb;
111                data->image[0][(j * 32 + i) * 4 + 1] = wb;
112                data->image[0][(j * 32 + i) * 4 + 2] = wb;
113                data->image[0][(j * 32 + i) * 4 + 3] = wb;
114            }
115        /* Use GL_RGBA instead of 4 for the internal format (Android) */
116        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0,
117                     GL_RGBA, GL_UNSIGNED_BYTE, data->image[0]);
118        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
119        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
120
121        data->initialised = 1;
122    }
123
124    float const st = sinf(0.0005f * data->time);
125    float const ct = cosf(0.0005f * data->time);
126
127    GLfloat const verts[6][2] =
128    {
129        { -0.7f * (st + ct),  0.7f * (st - ct) },
130        {  0.7f * (st - ct),  0.7f * (st + ct) },
131        { -0.7f * (st - ct), -0.7f * (st + ct) },
132
133        { -0.7f * (st - ct), -0.7f * (st + ct) },
134        {  0.7f * (st - ct),  0.7f * (st + ct) },
135        {  0.7f * (st + ct), -0.7f * (st - ct) },
136    };
137
138    /* Using only 3 components breaks on Android for some reason. */
139    static GLfloat const cols[6][4] =
140    {
141        { 1.0f, 0.2f, 0.2f, 1.0f },
142        { 0.2f, 0.2f, 1.0f, 1.0f },
143        { 1.0f, 1.0f, 0.2f, 1.0f },
144
145        { 1.0f, 1.0f, 0.2f, 1.0f },
146        { 0.2f, 0.2f, 1.0f, 1.0f },
147        { 0.2f, 1.0f, 0.2f, 1.0f },
148    };
149
150    static GLfloat const tcs[6][2] =
151    {
152        { 0.0f, 1.0f }, { 1.0f, 1.0f }, { 0.0f, 0.0f },
153        { 0.0f, 0.0f }, { 1.0f, 1.0f }, { 1.0f, 0.0f },
154    };
155
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    /* Bind texture */
167    glActiveTexture(GL_TEXTURE0);
168    glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
169
170    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
171    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), verts, GL_STATIC_DRAW);
172    glVertexAttribPointer(attr_pos, 2, GL_FLOAT, GL_FALSE, 0, 0);
173
174    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
175    glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW);
176    glVertexAttribPointer(attr_col, 4, GL_FLOAT, GL_FALSE, 0, 0);
177
178    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
179    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), tcs, GL_STATIC_DRAW);
180    glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, 0);
181
182    glDrawArrays(GL_TRIANGLES, 0, 6);
183
184    glDisableVertexAttribArray(attr_pos);
185    glDisableVertexAttribArray(attr_col);
186    glDisableVertexAttribArray(attr_tex);
187}
188
189DebugQuad::~DebugQuad()
190{
191    delete data;
192}
193
194} /* namespace lol */
195
Note: See TracBrowser for help on using the repository browser.