Browse Source

Add texturing support to DebugQuad. Works with OpenGL (fixed pipeline

and programmable pipeline versions) and OpenGL ES 1.x for now.
legacy
Sam Hocevar sam 13 years ago
parent
commit
df21968ce2
2 changed files with 91 additions and 32 deletions
  1. +84
    -25
      src/debugquad.cpp
  2. +7
    -7
      src/debugquad.h

+ 84
- 25
src/debugquad.cpp View File

@@ -23,39 +23,41 @@ namespace lol
{ {


/* /*
* DebugTri implementation class
* DebugQuad implementation class
*/ */


class DebugTriData
class DebugQuadData
{ {
friend class DebugTri;
friend class DebugQuad;


private: private:
int initialised; int initialised;
#if defined HAVE_GL_2X || defined HAVE_GLES_2X #if defined HAVE_GL_2X || defined HAVE_GLES_2X
GLuint buflist[2];
GLuint buflist[3];
Shader *shader; Shader *shader;
#elif defined HAVE_GL_1X || defined HAVE_GLES_1X #elif defined HAVE_GL_1X || defined HAVE_GLES_1X
GLuint buflist[2];
GLuint buflist[3];
#endif #endif
GLuint texlist[1];
uint8_t image[1][32 * 32 * 4];
}; };


/* /*
* Public DebugTri class
* Public DebugQuad class
*/ */


DebugTri::DebugTri()
: data(new DebugTriData())
DebugQuad::DebugQuad()
: data(new DebugQuadData())
{ {
data->initialised = 0; data->initialised = 0;
} }


void DebugTri::TickGame(float deltams)
void DebugQuad::TickGame(float deltams)
{ {
Entity::TickGame(deltams); Entity::TickGame(deltams);
} }


void DebugTri::TickDraw(float deltams)
void DebugQuad::TickDraw(float deltams)
{ {
Entity::TickDraw(deltams); Entity::TickDraw(deltams);


@@ -64,40 +66,64 @@ void DebugTri::TickDraw(float deltams)
if (data->initialised) if (data->initialised)
{ {
#if defined HAVE_GL_2X || defined HAVE_GLES_2X #if defined HAVE_GL_2X || defined HAVE_GLES_2X
glDeleteBuffers(2, data->buflist);
glDeleteBuffers(3, data->buflist);
Shader::Destroy(data->shader); Shader::Destroy(data->shader);
#elif defined HAVE_GL_1X || defined HAVE_GLES_1X #elif defined HAVE_GL_1X || defined HAVE_GLES_1X
glDeleteBuffers(2, data->buflist);
glDeleteBuffers(3, data->buflist);
#endif #endif
glDeleteTextures(1, data->texlist);
data->initialised = 0; data->initialised = 0;
} }
} }
else if (!data->initialised) else if (!data->initialised)
{ {
#if defined HAVE_GL_2X || defined HAVE_GLES_2X #if defined HAVE_GL_2X || defined HAVE_GLES_2X
glGenBuffers(2, data->buflist);
glGenBuffers(3, data->buflist);


static char const *vertexshader = static char const *vertexshader =
"#version 130\n" "#version 130\n"
"in vec2 in_Position;\n" "in vec2 in_Position;\n"
"in vec3 in_Color;\n"
"out vec3 pass_Color;\n"
"in vec4 in_Color;\n"
"in vec2 in_TexCoord;\n"
"out vec4 pass_Color;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
" gl_Position = vec4(in_Position, 0.0f, 1.0f);\n" " gl_Position = vec4(in_Position, 0.0f, 1.0f);\n"
" gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
" pass_Color = in_Color;\n" " pass_Color = in_Color;\n"
"}\n"; "}\n";
static char const *fragmentshader = static char const *fragmentshader =
"#version 130\n" "#version 130\n"
"in vec3 pass_Color;\n"
"in vec4 pass_Color;\n"
"uniform sampler2D in_Texture;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
" gl_FragColor = vec4(pass_Color, 1.0);\n"
" vec4 col = pass_Color;\n"
" vec4 tex = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
" gl_FragColor = col * tex;\n"
"}\n"; "}\n";
data->shader = Shader::Create(vertexshader, fragmentshader); data->shader = Shader::Create(vertexshader, fragmentshader);
#elif defined HAVE_GL_1X || defined HAVE_GLES_1X #elif defined HAVE_GL_1X || defined HAVE_GLES_1X
glGenBuffers(2, data->buflist);
glGenBuffers(3, data->buflist);
#endif #endif
glGenTextures(1, data->texlist);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
for (int j = 0; j < 32; j++)
for (int i = 0; i < 32; i++)
{
uint8_t wb = (((i / 2) ^ (j / 2)) & 1) * 0xff;
data->image[0][(j * 32 + i) * 4 + 0] = wb;
data->image[0][(j * 32 + i) * 4 + 1] = wb;
data->image[0][(j * 32 + i) * 4 + 2] = wb;
data->image[0][(j * 32 + i) * 4 + 3] = 255;
}
/* Use GL_RGBA instead of 4 for the internal format (Android) */
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0,
GL_RGBA, GL_UNSIGNED_BYTE, data->image[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);


data->initialised = 1; data->initialised = 1;
} }
@@ -108,20 +134,34 @@ void DebugTri::TickDraw(float deltams)
{ -0.9f, -0.9f }, { 0.9f, 0.9f }, { 0.9f, -0.9f }, { -0.9f, -0.9f }, { 0.9f, 0.9f }, { 0.9f, -0.9f },
}; };


/* Using only 3 components breaks on Android for some reason. */
static GLfloat const cols[6][4] = static GLfloat const cols[6][4] =
{ {
{ 0.8f, 0.2f, 0.2f, 1.0f }, { 0.2f, 0.2f, 0.8f, 1.0f }, { 0.8f, 0.8f, 0.2f, 1.0f },
{ 0.8f, 0.8f, 0.2f, 1.0f }, { 0.2f, 0.2f, 0.8f, 1.0f }, { 0.2f, 0.8f, 0.2f, 1.0f },
{ 0.8f, 0.2f, 0.2f, 1.0f },
{ 0.2f, 0.2f, 0.8f, 1.0f },
{ 0.8f, 0.8f, 0.2f, 1.0f },

{ 0.8f, 0.8f, 0.2f, 1.0f },
{ 0.2f, 0.2f, 0.8f, 1.0f },
{ 0.2f, 0.8f, 0.2f, 1.0f },
};

static GLfloat const tcs[6][2] =
{
{ 0.0f, 1.0f }, { 1.0f, 1.0f }, { 0.0f, 0.0f },
{ 0.0f, 0.0f }, { 1.0f, 1.0f }, { 1.0f, 0.0f },
}; };


#if defined HAVE_GL_2X || defined HAVE_GLES_2X #if defined HAVE_GL_2X || defined HAVE_GLES_2X
data->shader->Bind(); data->shader->Bind();
GLuint attr_pos, attr_col;
GLuint attr_pos, attr_col, attr_tex;
attr_pos = data->shader->GetAttribLocation("in_Position"); attr_pos = data->shader->GetAttribLocation("in_Position");
attr_col = data->shader->GetAttribLocation("in_Color"); attr_col = data->shader->GetAttribLocation("in_Color");
attr_tex = data->shader->GetAttribLocation("in_TexCoord");


glEnableVertexAttribArray(attr_pos); glEnableVertexAttribArray(attr_pos);
glEnableVertexAttribArray(attr_col); glEnableVertexAttribArray(attr_col);
glEnableVertexAttribArray(attr_tex);


glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]); glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), verts, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), verts, GL_STATIC_DRAW);
@@ -131,11 +171,17 @@ void DebugTri::TickDraw(float deltams)
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW);
glVertexAttribPointer(attr_col, 4, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(attr_col, 4, GL_FLOAT, GL_FALSE, 0, 0);


glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), tcs, GL_STATIC_DRAW);
glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, 0);

glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);


glDisableVertexAttribArray(attr_pos); glDisableVertexAttribArray(attr_pos);
glDisableVertexAttribArray(attr_col); glDisableVertexAttribArray(attr_col);
glDisableVertexAttribArray(attr_tex);
#elif defined HAVE_GL_1X || defined HAVE_GLES_1X #elif defined HAVE_GL_1X || defined HAVE_GLES_1X
/* Reset all model-view-projection matrices */
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glPushMatrix(); glPushMatrix();
glLoadIdentity(); glLoadIdentity();
@@ -143,12 +189,16 @@ void DebugTri::TickDraw(float deltams)
glPushMatrix(); glPushMatrix();
glLoadIdentity(); glLoadIdentity();


glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
/* Set up state machine */
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_COLOR_ARRAY);


glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, data->texlist[0]);

/* Bind vertex, color and texture coordinate buffers */
#if defined HAVE_GL_1X #if defined HAVE_GL_1X
glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]); glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), verts, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), verts, GL_STATIC_DRAW);
@@ -157,16 +207,25 @@ void DebugTri::TickDraw(float deltams)
glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]); glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW);
glColorPointer(4, GL_FLOAT, 0, NULL); glColorPointer(4, GL_FLOAT, 0, NULL);

glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), tcs, GL_STATIC_DRAW);
glTexCoordPointer(2, GL_FLOAT, 0, NULL);
#else #else
glVertexPointer(2, GL_FLOAT, 0, verts); glVertexPointer(2, GL_FLOAT, 0, verts);
glColorPointer(4, GL_FLOAT, 0, cols); glColorPointer(4, GL_FLOAT, 0, cols);
glTexCoordPointer(2, GL_FLOAT, 0, tcs);
#endif #endif


/* Draw arrays */
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);


/* Disable state machine features */
glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);


/* Restore matrices */
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glPopMatrix(); glPopMatrix();
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
@@ -174,7 +233,7 @@ void DebugTri::TickDraw(float deltams)
#endif #endif
} }


DebugTri::~DebugTri()
DebugQuad::~DebugQuad()
{ {
delete data; delete data;
} }


+ 7
- 7
src/debugquad.h View File

@@ -9,8 +9,8 @@
// //


// //
// The DebugTri class
// ------------------
// The DebugQuad class
// -------------------
// //


#if !defined __DH_DEBUGTRI_H__ #if !defined __DH_DEBUGTRI_H__
@@ -21,20 +21,20 @@
namespace lol namespace lol
{ {


class DebugTriData;
class DebugQuadData;


class DebugTri : public Entity
class DebugQuad : public Entity
{ {
public: public:
DebugTri();
virtual ~DebugTri();
DebugQuad();
virtual ~DebugQuad();


protected: protected:
virtual void TickGame(float deltams); virtual void TickGame(float deltams);
virtual void TickDraw(float deltams); virtual void TickDraw(float deltams);


private: private:
DebugTriData *data;
DebugQuadData *data;
}; };


} /* namespace lol */ } /* namespace lol */


Loading…
Cancel
Save