Browse Source

debug: reset the GL state more aggressively between quad renderings; this

helped find a few bugs such as textures not bound.
legacy
Sam Hocevar sam 13 years ago
parent
commit
b71ed99d8e
2 changed files with 62 additions and 20 deletions
  1. +61
    -20
      src/debug/quad.cpp
  2. +1
    -0
      src/debug/quad.h

+ 61
- 20
src/debug/quad.cpp View File

@@ -118,16 +118,6 @@ void DebugQuad::TickDraw(float deltams)
data->initialised = 0;
}

/* Reset GL states to something reasonably safe */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_TEXTURE_2D);
glClientActiveTexture(GL_TEXTURE0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glUseProgram(0);

/* Prepare our quad coordinates */
vec2i const layout(4, 3);
data->step = vec2(2.0f, -2.0f) / (3 * layout + vec2i(1));
@@ -146,9 +136,12 @@ void DebugQuad::TickDraw(float deltams)
GLfloat const texcoords[] = { f1, f3, f3, f2, f2, f4,
f2, f4, f4, f1, f1, f3 };

ResetState();

#if defined HAVE_GLBEGIN || defined USE_GLEW
/*
* Test #1: simple glBegin code
*
* Renders an orange square.
*/
glColor3f(0.8f, 0.5f, 0.2f);
@@ -163,11 +156,14 @@ void DebugQuad::TickDraw(float deltams)
glEnd();

Advance();
ResetState();

/*
* Test #2: glBegin + per-vertex coloring
*
* Renders a multicolored square with varying colors.
*/

glBegin(GL_TRIANGLES);
glColor3f(f1, f2, f3);
glVertex3f(data->aa.x, data->bb.y, 0.0f);
@@ -184,9 +180,11 @@ void DebugQuad::TickDraw(float deltams)
glEnd();

Advance();
ResetState();

/*
* Test #3: glBegin + texture
*
* Renders an animated black-and-white distorted checkerboard.
*/
glEnable(GL_TEXTURE_2D);
@@ -210,9 +208,11 @@ void DebugQuad::TickDraw(float deltams)
glDisable(GL_TEXTURE_2D);

Advance();
ResetState();

/*
* Test #4: glBegin + color in fragment shader
*
* Renders a static, coloured and tiled pattern.
*/
if (!data->shader[0])
@@ -245,9 +245,12 @@ void DebugQuad::TickDraw(float deltams)
glUseProgram(0);

Advance();
ResetState();

/*
* Test #5: glBegin + pass color from vertex shader to fragment shader
* Test #5: glBegin + pass vertex coord from vertex shader to fragment
* shader for use as color information.
*
* Renders a multicolored square with varying colors.
*/
if (!data->shader[1])
@@ -258,7 +261,8 @@ void DebugQuad::TickDraw(float deltams)
"{"
" float r = gl_MultiTexCoord0.x;"
" float g = gl_MultiTexCoord0.y;"
" pass_Color = vec4(1.0 - r, 1.0 - g, r, 1.0);"
" float b = gl_MultiTexCoord0.z;"
" pass_Color = vec4(r, g, b, 1.0);"
" gl_Position = gl_Vertex;"
"}",

@@ -271,28 +275,33 @@ void DebugQuad::TickDraw(float deltams)
data->shader[1]->Bind();
glColor3f(0.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLES);
glTexCoord2f(f1, f3);
glTexCoord3f(f1, f2, f3);
glVertex3f(data->aa.x, data->bb.y, 0.0f);
glTexCoord2f(f3, f2);
glTexCoord3f(f4, f2, f1);
glVertex3f(data->bb.x, data->bb.y, 0.0f);
glTexCoord2f(f2, f4);
glTexCoord3f(f3, f1, f4);
glVertex3f(data->bb.x, data->aa.y, 0.0f);

glTexCoord2f(f2, f4);
glVertex3f(data->bb.x, data->aa.y, 0.0f);
glTexCoord2f(f4, f1);
glTexCoord3f(f4, f3, f2);
glVertex3f(data->aa.x, data->aa.y, 0.0f);
glTexCoord2f(f1, f3);
glTexCoord3f(f1, f2, f3);
glVertex3f(data->aa.x, data->bb.y, 0.0f);
glEnd();
glEnd();
glUseProgram(0);

Advance();
ResetState();

/*
* Test #6: glBegin + apply texture in fragment shader
*
* Renders an animated black-and-white distorted checkerboard with a
* zoom ratio twice the one in test #3.
*
* Note: there is no need to glEnable(GL_TEXTURE_2D) when the
* texture lookup is done in a shader.
*/
if (!data->shader[2])
data->shader[2] = Shader::Create(
@@ -311,7 +320,6 @@ void DebugQuad::TickDraw(float deltams)
"}");
data->shader[2]->Bind();
glColor3f(0.0f, 1.0f, 1.0f);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, data->texture[0]);
glBegin(GL_TRIANGLES);
glTexCoord2f(f1, f3);
@@ -329,13 +337,14 @@ void DebugQuad::TickDraw(float deltams)
glVertex3f(data->aa.x, data->bb.y, 0.0f);
glEnd();
glUseProgram(0);
glDisable(GL_TEXTURE_2D);

Advance();
ResetState();
#endif

/*
* Test #7: vertex buffer + per-vertex coloring
*
* Renders a multicolored square with varying colors.
*/
GLfloat const vertices1[] = { data->aa.x, data->bb.y, 0.0f,
@@ -356,9 +365,11 @@ void DebugQuad::TickDraw(float deltams)
glDisableClientState(GL_COLOR_ARRAY);

Advance();
ResetState();

/*
* Test #8: vertex buffer + per-vertex coloring + texture
*
* Renders a multicolored square with varying colors multiplied with an
* animated distorted checkerboard.
*/
@@ -386,9 +397,11 @@ void DebugQuad::TickDraw(float deltams)
glDisable(GL_TEXTURE_2D);

Advance();
ResetState();

/*
* Test #9: vertex buffer + texture & color in 1.10 fragment shader
*
* Renders a multicolored square with varying colors xored with an
* animated distorted checkerboard.
*/
@@ -420,6 +433,8 @@ void DebugQuad::TickDraw(float deltams)
data->aa.x, data->aa.y, 0.0f,
data->aa.x, data->bb.y, 0.0f };

glBindTexture(GL_TEXTURE_2D, data->texture[0]);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
@@ -435,9 +450,11 @@ void DebugQuad::TickDraw(float deltams)
glUseProgram(0);

Advance();
ResetState();

/*
* Test #10: vertex buffer + texture & color in 1.20 fragment shader
*
* Renders a multicolored square with varying colors xored with an
* animated distorted checkerboard.
*/
@@ -476,6 +493,8 @@ void DebugQuad::TickDraw(float deltams)
data->aa.x, data->aa.y, 0.0f,
data->aa.x, data->bb.y, 0.0f };

glBindTexture(GL_TEXTURE_2D, data->texture[0]);

glBindVertexArray(data->array[0]);

glBindBuffer(GL_ARRAY_BUFFER, data->buffer[0]);
@@ -505,9 +524,11 @@ void DebugQuad::TickDraw(float deltams)
glUseProgram(0);

Advance();
ResetState();

/*
* Test #11: vertex buffer + texture & color in 1.30 fragment shader
*
* Renders a multicolored square with varying colors xored with an
* animated distorted checkerboard.
*/
@@ -546,6 +567,8 @@ void DebugQuad::TickDraw(float deltams)
data->aa.x, data->aa.y, 0.0f,
data->aa.x, data->bb.y, 0.0f };

glBindTexture(GL_TEXTURE_2D, data->texture[0]);

glBindVertexArray(data->array[1]);

glBindBuffer(GL_ARRAY_BUFFER, data->buffer[3]);
@@ -575,6 +598,24 @@ void DebugQuad::TickDraw(float deltams)
glUseProgram(0);

Advance();
ResetState();
}

void DebugQuad::ResetState()
{
/* Reset GL states to something reasonably safe */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glClientActiveTexture(GL_TEXTURE0);
glDisable(GL_TEXTURE_2D);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glUseProgram(0);
}

void DebugQuad::Advance()


+ 1
- 0
src/debug/quad.h View File

@@ -34,6 +34,7 @@ protected:
virtual void TickDraw(float deltams);

private:
void ResetState();
void Advance();

DebugQuadData *data;


Loading…
Cancel
Save