source: trunk/src/debugquad.cpp @ 699

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

Add a DebugQuad class for simple feature testing.

  • Property svn:keywords set to Id
File size: 4.7 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 * DebugTri implementation class
27 */
28
29class DebugTriData
30{
31    friend class DebugTri;
32
33private:
34    int initialised;
35#if defined HAVE_GL_2X || defined HAVE_GLES_2X
36    GLuint buflist[2];
37    Shader *shader;
38#elif defined HAVE_GL_1X || defined HAVE_GLES_1X
39    GLuint buflist[2];
40#endif
41};
42
43/*
44 * Public DebugTri class
45 */
46
47DebugTri::DebugTri()
48  : data(new DebugTriData())
49{
50    data->initialised = 0;
51}
52
53void DebugTri::TickGame(float deltams)
54{
55    Entity::TickGame(deltams);
56}
57
58void DebugTri::TickDraw(float deltams)
59{
60    Entity::TickDraw(deltams);
61
62    if (IsDestroying())
63    {
64        if (data->initialised)
65        {
66#if defined HAVE_GL_2X || defined HAVE_GLES_2X
67            glDeleteBuffers(2, data->buflist);
68            Shader::Destroy(data->shader);
69#elif defined HAVE_GL_1X || defined HAVE_GLES_1X
70            glDeleteBuffers(2, data->buflist);
71#endif
72            data->initialised = 0;
73        }
74    }
75    else if (!data->initialised)
76    {
77#if defined HAVE_GL_2X || defined HAVE_GLES_2X
78        glGenBuffers(2, data->buflist);
79
80        static char const *vertexshader =
81            "#version 130\n"
82            "in vec2 in_Position;\n"
83            "in vec3 in_Color;\n"
84            "out vec3 pass_Color;\n"
85            "void main()\n"
86            "{\n"
87            "    gl_Position = vec4(in_Position, 0.0f, 1.0f);\n"
88            "    pass_Color = in_Color;\n"
89            "}\n";
90        static char const *fragmentshader =
91            "#version 130\n"
92            "in vec3 pass_Color;\n"
93            "void main()\n"
94            "{\n"
95            "    gl_FragColor = vec4(pass_Color, 1.0);\n"
96            "}\n";
97        data->shader = Shader::Create(vertexshader, fragmentshader);
98#elif defined HAVE_GL_1X || defined HAVE_GLES_1X
99        glGenBuffers(2, data->buflist);
100#endif
101
102        data->initialised = 1;
103    }
104
105    static GLfloat const verts[6][2] =
106    {
107        { -0.9f,  0.9f }, {  0.9f,  0.9f }, { -0.9f, -0.9f },
108        { -0.9f, -0.9f }, {  0.9f,  0.9f }, {  0.9f, -0.9f },
109    };
110
111    static GLfloat const cols[6][4] =
112    {
113        { 0.8f, 0.2f, 0.2f, 1.0f }, { 0.2f, 0.2f, 0.8f, 1.0f }, { 0.8f, 0.8f, 0.2f, 1.0f },
114        { 0.8f, 0.8f, 0.2f, 1.0f }, { 0.2f, 0.2f, 0.8f, 1.0f }, { 0.2f, 0.8f, 0.2f, 1.0f },
115    };
116
117#if defined HAVE_GL_2X || defined HAVE_GLES_2X
118    data->shader->Bind();
119    GLuint attr_pos, attr_col;
120    attr_pos = data->shader->GetAttribLocation("in_Position");
121    attr_col = data->shader->GetAttribLocation("in_Color");
122
123    glEnableVertexAttribArray(attr_pos);
124    glEnableVertexAttribArray(attr_col);
125
126    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
127    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), verts, GL_STATIC_DRAW);
128    glVertexAttribPointer(attr_pos, 2, GL_FLOAT, GL_FALSE, 0, 0);
129
130    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
131    glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW);
132    glVertexAttribPointer(attr_col, 4, GL_FLOAT, GL_FALSE, 0, 0);
133
134    glDrawArrays(GL_TRIANGLES, 0, 6);
135
136    glDisableVertexAttribArray(attr_pos);
137    glDisableVertexAttribArray(attr_col);
138#elif defined HAVE_GL_1X || defined HAVE_GLES_1X
139    glMatrixMode(GL_PROJECTION);
140    glPushMatrix();
141    glLoadIdentity();
142    glMatrixMode(GL_MODELVIEW);
143    glPushMatrix();
144    glLoadIdentity();
145
146    glDisableClientState(GL_NORMAL_ARRAY);
147    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
148    glDisable(GL_TEXTURE_2D);
149    glEnableClientState(GL_VERTEX_ARRAY);
150    glEnableClientState(GL_COLOR_ARRAY);
151
152#if defined HAVE_GL_1X
153    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
154    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), verts, GL_STATIC_DRAW);
155    glVertexPointer(2, GL_FLOAT, 0, NULL);
156
157    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
158    glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW);
159    glColorPointer(4, GL_FLOAT, 0, NULL);
160#else
161    glVertexPointer(2, GL_FLOAT, 0, verts);
162    glColorPointer(4, GL_FLOAT, 0, cols);
163#endif
164
165    glDrawArrays(GL_TRIANGLES, 0, 6);
166
167    glDisableClientState(GL_VERTEX_ARRAY);
168    glDisableClientState(GL_COLOR_ARRAY);
169
170    glMatrixMode(GL_PROJECTION);
171    glPopMatrix();
172    glMatrixMode(GL_MODELVIEW);
173    glPopMatrix();
174#endif
175}
176
177DebugTri::~DebugTri()
178{
179    delete data;
180}
181
182} /* namespace lol */
183
Note: See TracBrowser for help on using the repository browser.