Browse Source

debug: some code cleanup in the quad tests.

legacy
Sam Hocevar sam 13 years ago
parent
commit
a84eb53f8e
1 changed files with 191 additions and 277 deletions
  1. +191
    -277
      src/debug/quad.cpp

+ 191
- 277
src/debug/quad.cpp View File

@@ -28,11 +28,13 @@ namespace lol
* DebugQuad implementation class
*/

#define NUM_ARRAYS 2
#define NUM_BUFFERS 6
#define NUM_ATTRS 6
#define NUM_SHADERS 6
#define NUM_TEXTURES 1
static int const NUM_ARRAYS = 2;
static int const NUM_BUFFERS = 6;
static int const NUM_ATTRS = 6;
static int const NUM_SHADERS = 6;
static int const NUM_TEXTURES = 1;

static int const TEX_SIZE = 32;

class DebugQuadData
{
@@ -48,7 +50,7 @@ private:
Shader *shader[NUM_SHADERS];
GLuint attr[NUM_ATTRS];
GLuint texture[NUM_TEXTURES];
uint8_t image[1][32 * 32 * 4];
uint8_t image[1][TEX_SIZE * TEX_SIZE * 4];
};

/*
@@ -79,289 +81,44 @@ void DebugQuad::TickDraw(float deltams)
{
glGenVertexArrays(NUM_ARRAYS, data->array);
glGenBuffers(NUM_BUFFERS, data->buffer);
#if 0

static char const *vertexshader =
"//#version 130\n"
"varying vec2 in_Position;\n"
"varying vec4 in_Color;\n"
"varying vec2 in_TexCoord;\n"
"varying vec4 pass_Color;\n"
"void main()\n"
"{\n"
"gl_TexCoord[0] = gl_MultiTexCoord0;\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"
"}\n";
static char const *fragmentshader =
"//#version 130\n"
"varying vec4 pass_Color;\n"
"uniform sampler2D in_Texture;\n"
"void main()\n"
"{\n"
" vec4 col = pass_Color;\n"
" vec4 tex = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
" gl_FragColor = col * tex;\n"
" gl_FragColor = vec4(1.0, 1.0, 1.0, 0.0);\n"
"}\n";
data->shader[0] = Shader::Create(vertexshader, fragmentshader);
glGenTextures(1, data->texture);
#endif
glGenTextures(NUM_TEXTURES, data->texture);
for (int i = 0; i < NUM_SHADERS; i++)
data->shader[i] = NULL;

/* Checkerboard texture */
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, data->texture[0]);
for (int j = 0; j < 32; j++)
for (int i = 0; i < 32; i++)
for (int j = 0; j < TEX_SIZE; j++)
for (int i = 0; i < TEX_SIZE; 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] = 0xff;
data->image[0][(j * TEX_SIZE + i) * 4 + 0] = wb;
data->image[0][(j * TEX_SIZE + i) * 4 + 1] = wb;
data->image[0][(j * TEX_SIZE + i) * 4 + 2] = wb;
data->image[0][(j * TEX_SIZE + i) * 4 + 3] = 0xff;
}
/* Use GL_RGBA instead of 4 for the internal format (Android) */
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0,
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_SIZE, TEX_SIZE, 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);

/* Quad #4: create texture in 1.10 fragment shader */
data->shader[0] = Shader::Create(
"#version 110\n"
"void main()"
"{"
" gl_Position = gl_Vertex;"
"}",

"#version 110\n"
"void main()"
"{"
" float dx = mod(gl_FragCoord.x * gl_FragCoord.y, 2.0);"
" float dy = mod(gl_FragCoord.x * 0.125, 1.0);"
" float dz = mod(gl_FragCoord.y * 0.125, 1.0);"
" gl_FragColor = vec4(dx, dy, dz, 1.0);"
"}");

/* Quad #5: pass color from 1.10 vertex shader to fragment shader */
data->shader[1] = Shader::Create(
"#version 110\n"
"varying vec4 pass_Color;"
"void main()"
"{"
" float r = gl_MultiTexCoord0.x;"
" float g = gl_MultiTexCoord0.y;"
" pass_Color = vec4(1.0 - r, 1.0 - g, r, 1.0);"
" gl_Position = gl_Vertex;"
"}",

"#version 110\n"
"varying vec4 pass_Color;"
"void main()"
"{"
" gl_FragColor = pass_Color;"
"}");

/* Quad #6: apply texture in 1.10 fragment shader */
data->shader[2] = Shader::Create(
"#version 110\n"
"void main()"
"{"
" gl_TexCoord[0] = gl_MultiTexCoord0;"
" gl_Position = gl_Vertex;"
"}",

"#version 110\n"
"uniform sampler2D tex;"
"void main()"
"{"
" gl_FragColor = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
"}");

/* Quad #8: vertex buffer, apply texture in 1.10 fragment shader */
data->shader[3] = Shader::Create(
"#version 110\n"
"varying vec4 pass_Color;"
"void main()"
"{"
" gl_TexCoord[0] = gl_MultiTexCoord0;"
" pass_Color = gl_Color;"
" gl_Position = gl_Vertex;"
"}",

"#version 110\n"
"varying vec4 pass_Color;"
"uniform sampler2D tex;"
"void main()"
"{"
" vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
" gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);"
"}");

/* Quad #9: vertex buffer, apply texture and color in 1.20 shader */
data->shader[4] = Shader::Create(
"#version 120\n"
"attribute vec3 in_Vertex;"
"attribute vec3 in_Color;"
"attribute vec2 in_MultiTexCoord0;"
"varying vec4 pass_Color;"
"void main()"
"{"
" gl_TexCoord[0] = vec4(in_MultiTexCoord0, 0.0, 0.0);"
" pass_Color = vec4(in_Color, 1.0);"
" gl_Position = vec4(in_Vertex, 1.0);"
"}",

"#version 120\n"
"varying vec4 pass_Color;"
"uniform sampler2D tex;"
"void main()"
"{"
" vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
" gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);"
"}");
data->attr[0] = data->shader[4]->GetAttribLocation("in_Vertex");
data->attr[1] = data->shader[4]->GetAttribLocation("in_Color");
data->attr[2] = data->shader[4]->GetAttribLocation("in_MultiTexCoord0");

/* Quad #10: vertex buffer, apply texture and color in 1.30 shader */
data->shader[5] = Shader::Create(
"#version 130\n"
"in vec3 in_Vertex;"
"in vec3 in_Color;"
"in vec2 in_MultiTexCoord0;"
"out vec4 pass_Color;"
"void main()"
"{"
" gl_TexCoord[0] = vec4(in_MultiTexCoord0, 0.0, 0.0);"
" pass_Color = vec4(in_Color, 1.0);"
" gl_Position = vec4(in_Vertex, 1.0);"
"}",

"#version 130\n"
"in vec4 pass_Color;"
"uniform sampler2D tex;"
"void main()"
"{"
" vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
" gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);"
"}");
data->attr[3] = data->shader[4]->GetAttribLocation("in_Vertex");
data->attr[4] = data->shader[4]->GetAttribLocation("in_Color");
data->attr[5] = data->shader[4]->GetAttribLocation("in_MultiTexCoord0");

data->initialised = 1;
}
else if (data->initialised && IsDestroying())
{
glDeleteVertexArrays(NUM_ARRAYS, data->array);
glDeleteBuffers(NUM_BUFFERS, data->buffer);
Shader::Destroy(data->shader[0]);
Shader::Destroy(data->shader[1]);
Shader::Destroy(data->shader[2]);
Shader::Destroy(data->shader[3]);
glDeleteTextures(NUM_TEXTURES, data->texture);

for (int i = 0; i < NUM_SHADERS; i++)
if (data->shader[i])
Shader::Destroy(data->shader[i]);

data->initialised = 0;
}

#if 0
float const st = sinf(0.0005f * data->time);
float const ct = cosf(0.0005f * data->time);

GLfloat const verts[6][2] =
{
{ -0.7f * (st + ct), 0.7f * (st - ct) },
{ 0.7f * (st - ct), 0.7f * (st + ct) },
{ -0.7f * (st - ct), -0.7f * (st + ct) },

{ -0.7f * (st - ct), -0.7f * (st + ct) },
{ 0.7f * (st - ct), 0.7f * (st + ct) },
{ 0.7f * (st + ct), -0.7f * (st - ct) },
};

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

{ 1.0f, 1.0f, 0.2f, 1.0f },
{ 0.2f, 0.2f, 1.0f, 1.0f },
{ 0.2f, 1.0f, 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 __CELLOS_LV2__
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

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

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

glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW);
glColorPointer(4, GL_FLOAT, GL_FALSE, 0);

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

glDrawArrays(GL_TRIANGLES, 0, 6);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
#else
data->shader[0]->Bind();
GLuint attr_pos, attr_col, attr_tex;
attr_pos = data->shader[0]->GetAttribLocation("in_Position");
attr_col = data->shader[0]->GetAttribLocation("in_Color");
attr_tex = data->shader[0]->GetAttribLocation("in_TexCoord");

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

/* Bind texture */
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, data->texture[0]);

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

glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW);
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);

glBindBuffer(GL_ARRAY_BUFFER, 0);

glDrawArrays(GL_TRIANGLES, 0, 6);

glDisableVertexAttribArray(attr_pos);
glDisableVertexAttribArray(attr_col);
glDisableVertexAttribArray(attr_tex);
#endif
#endif

/* Reset GL states */
/* Reset GL states to something reasonably safe */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
@@ -388,8 +145,11 @@ void DebugQuad::TickDraw(float deltams)
GLfloat const texcoords[] = { f1, f3, f3, f2, f2, f4,
f2, f4, f4, f1, f1, f3 };

#if defined HAVE_GLBEGIN
/* Quad #1: simple glBegin program */
#if defined HAVE_GLBEGIN || defined USE_GLEW
/*
* Test #1: simple glBegin code
* Renders an orange square.
*/
glColor3f(0.8f, 0.5f, 0.2f);
glBegin(GL_TRIANGLES);
glVertex3f(data->aa.x, data->bb.y, 0.0f);
@@ -403,7 +163,10 @@ void DebugQuad::TickDraw(float deltams)

Advance();

/* Quad #2: glBegin program with varying color */
/*
* 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);
@@ -421,7 +184,10 @@ void DebugQuad::TickDraw(float deltams)

Advance();

/* Quad #3: textured quad */
/*
* Test #3: glBegin + texture
* Renders an animated black-and-white distorted checkerboard.
*/
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, data->texture[0]);
glColor3f(1.0f, 1.0f, 1.0f);
@@ -444,7 +210,26 @@ void DebugQuad::TickDraw(float deltams)

Advance();

/* Quad #4: set color in fragment shader */
/*
* Test #4: glBegin + color in fragment shader
* Renders a static, coloured and tiled pattern.
*/
if (!data->shader[0])
data->shader[0] = Shader::Create(
"#version 110\n"
"void main()"
"{"
" gl_Position = gl_Vertex;"
"}",

"#version 110\n"
"void main()"
"{"
" float dx = mod(gl_FragCoord.x * gl_FragCoord.y, 2.0);"
" float dy = mod(gl_FragCoord.x * 0.125, 1.0);"
" float dz = mod(gl_FragCoord.y * 0.125, 1.0);"
" gl_FragColor = vec4(dx, dy, dz, 1.0);"
"}");
data->shader[0]->Bind();
glColor3f(0.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLES);
@@ -460,7 +245,28 @@ void DebugQuad::TickDraw(float deltams)

Advance();

/* Quad #5: pass color from vertex shader to fragment shader */
/*
* Test #5: glBegin + pass color from vertex shader to fragment shader
* Renders a multicolored square with varying colors.
*/
if (!data->shader[1])
data->shader[1] = Shader::Create(
"#version 110\n"
"varying vec4 pass_Color;"
"void main()"
"{"
" float r = gl_MultiTexCoord0.x;"
" float g = gl_MultiTexCoord0.y;"
" pass_Color = vec4(1.0 - r, 1.0 - g, r, 1.0);"
" gl_Position = gl_Vertex;"
"}",

"#version 110\n"
"varying vec4 pass_Color;"
"void main()"
"{"
" gl_FragColor = pass_Color;"
"}");
data->shader[1]->Bind();
glColor3f(0.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLES);
@@ -482,7 +288,26 @@ void DebugQuad::TickDraw(float deltams)

Advance();

/* Quad #6: apply texture in fragment shader */
/*
* 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.
*/
if (!data->shader[2])
data->shader[2] = Shader::Create(
"#version 110\n"
"void main()"
"{"
" gl_TexCoord[0] = gl_MultiTexCoord0;"
" gl_Position = gl_Vertex;"
"}",

"#version 110\n"
"uniform sampler2D tex;"
"void main()"
"{"
" gl_FragColor = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
"}");
data->shader[2]->Bind();
glColor3f(0.0f, 1.0f, 1.0f);
glEnable(GL_TEXTURE_2D);
@@ -508,7 +333,10 @@ void DebugQuad::TickDraw(float deltams)
Advance();
#endif

/* Quad #7: simple vertex buffer, no shader */
/*
* Test #7: vertex buffer
* Renders a multicolored square with varying colors.
*/
GLfloat const vertices1[] = { data->aa.x, data->bb.y, 0.0f,
data->bb.x, data->bb.y, 0.0f,
data->bb.x, data->aa.y, 0.0f,
@@ -529,7 +357,31 @@ void DebugQuad::TickDraw(float deltams)

Advance();

/* Quad #8: vertex buffer, apply texture and color in fragment shader */
/*
* Test #8: vertex buffer + texture & color in 1.10 fragment shader
* Renders a multicolored square with varying colors xored with an
* animated distorted checkerboard.
*/
if (!data->shader[3])
data->shader[3] = Shader::Create(
"#version 110\n"
"varying vec4 pass_Color;"
"void main()"
"{"
" gl_TexCoord[0] = gl_MultiTexCoord0;"
" pass_Color = gl_Color;"
" gl_Position = gl_Vertex;"
"}",

"#version 110\n"
"varying vec4 pass_Color;"
"uniform sampler2D tex;"
"void main()"
"{"
" vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
" gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);"
"}");

data->shader[3]->Bind();
GLfloat const vertices2[] = { data->aa.x, data->bb.y, 0.0f,
data->bb.x, data->bb.y, 0.0f,
@@ -554,7 +406,38 @@ void DebugQuad::TickDraw(float deltams)

Advance();

/* Quad #9: vertex buffer, apply texture and color in 1.20 shader */
/*
* Test #8: vertex buffer + texture & color in 1.20 fragment shader
* Renders a multicolored square with varying colors xored with an
* animated distorted checkerboard.
*/
if (!data->shader[4])
{
data->shader[4] = Shader::Create(
"#version 120\n"
"attribute vec3 in_Vertex;"
"attribute vec3 in_Color;"
"attribute vec2 in_MultiTexCoord0;"
"varying vec4 pass_Color;"
"void main()"
"{"
" gl_TexCoord[0] = vec4(in_MultiTexCoord0, 0.0, 0.0);"
" pass_Color = vec4(in_Color, 1.0);"
" gl_Position = vec4(in_Vertex, 1.0);"
"}",

"#version 120\n"
"varying vec4 pass_Color;"
"uniform sampler2D tex;"
"void main()"
"{"
" vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
" gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);"
"}");
data->attr[0] = data->shader[4]->GetAttribLocation("in_Vertex");
data->attr[1] = data->shader[4]->GetAttribLocation("in_Color");
data->attr[2] = data->shader[4]->GetAttribLocation("in_MultiTexCoord0");
}
data->shader[4]->Bind();
GLfloat const vertices3[] = { data->aa.x, data->bb.y, 0.0f,
data->bb.x, data->bb.y, 0.0f,
@@ -593,7 +476,38 @@ void DebugQuad::TickDraw(float deltams)

Advance();

/* Quad #10: vertex buffer, apply texture and color in 1.30 shader */
/*
* Test #8: vertex buffer + texture & color in 1.30 fragment shader
* Renders a multicolored square with varying colors xored with an
* animated distorted checkerboard.
*/
if (!data->shader[5])
{
data->shader[5] = Shader::Create(
"#version 130\n"
"in vec3 in_Vertex;"
"in vec3 in_Color;"
"in vec2 in_MultiTexCoord0;"
"out vec4 pass_Color;"
"void main()"
"{"
" gl_TexCoord[0] = vec4(in_MultiTexCoord0, 0.0, 0.0);"
" pass_Color = vec4(in_Color, 1.0);"
" gl_Position = vec4(in_Vertex, 1.0);"
"}",

"#version 130\n"
"in vec4 pass_Color;"
"uniform sampler2D tex;"
"void main()"
"{"
" vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);"
" gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);"
"}");
data->attr[3] = data->shader[4]->GetAttribLocation("in_Vertex");
data->attr[4] = data->shader[4]->GetAttribLocation("in_Color");
data->attr[5] = data->shader[4]->GetAttribLocation("in_MultiTexCoord0");
}
data->shader[5]->Bind();
GLfloat const vertices4[] = { data->aa.x, data->bb.y, 0.0f,
data->bb.x, data->bb.y, 0.0f,


Loading…
Cancel
Save