Skip to content

Commit

Permalink
Fixed and simplified compressed texture demo.
Browse files Browse the repository at this point in the history
This is in preparation of converting it into a gl_test.

Review URL: https://chromiumcodereview.appspot.com/21097007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@214865 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
alokp@chromium.org committed Jul 31, 2013
1 parent b6c510a commit 30aeb11
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 68 deletions.
80 changes: 13 additions & 67 deletions gpu/demos/compressed_textures/compressed_textures.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ int g_numTextures = 1;
GLuint g_textures[5];
int g_textureLoc = -1;
GLuint g_programObject = 0;
GLuint g_worldMatrixLoc = 0;
GLuint g_vbo = 0;
GLsizei g_texCoordOffset = 0;
int g_angle = 0;

void CheckGLError(const char* func_name, int line_no) {
#ifndef NDEBUG
Expand Down Expand Up @@ -65,15 +62,12 @@ GLuint LoadShader(GLenum type, const char* shaderSrc) {

void InitShaders() {
static const char* vShaderStr =
"uniform mat4 worldMatrix;\n"
"attribute vec3 g_Position;\n"
"attribute vec2 g_TexCoord0;\n"
"attribute vec2 g_Position;\n"
"varying vec2 texCoord;\n"
"void main()\n"
"{\n"
" gl_Position = worldMatrix *\n"
" vec4(g_Position.x, g_Position.y, g_Position.z, 1.0);\n"
" texCoord = g_TexCoord0;\n"
" gl_Position = vec4(g_Position.x, g_Position.y, 0.0, 1.0);\n"
" texCoord = g_Position * vec2(0.5) + vec2(0.5);\n"
"}\n";
static const char* fShaderStr =
"precision mediump float;"
Expand All @@ -96,9 +90,7 @@ void InitShaders() {
glAttachShader(programObject, vertexShader);
glAttachShader(programObject, fragmentShader);
// Bind g_Position to attribute 0
// Bind g_TexCoord0 to attribute 1
glBindAttribLocation(programObject, 0, "g_Position");
glBindAttribLocation(programObject, 1, "g_TexCoord0");
// Link the program
glLinkProgram(programObject);
// Check the link status
Expand All @@ -114,34 +106,21 @@ void InitShaders() {
return;
}
g_programObject = programObject;
g_worldMatrixLoc = glGetUniformLocation(g_programObject, "worldMatrix");
g_textureLoc = glGetUniformLocation(g_programObject, "tex");
glGenBuffers(1, &g_vbo);
glBindBuffer(GL_ARRAY_BUFFER, g_vbo);
static float vertices[] = {
0.25, 0.75, 0.0,
-0.75, 0.75, 0.0,
-0.75, -0.25, 0.0,
0.25, 0.75, 0.0,
-0.75, -0.25, 0.0,
0.25, -0.25, 0.0,
1.0f, 1.0f,
-1.0f, 1.0f,
-1.0f, -1.0f,
1.0f, 1.0f,
-1.0f, -1.0f,
1.0f, -1.0f
};
static float texCoords[] = {
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0,
};
g_texCoordOffset = sizeof(vertices);
glBufferData(GL_ARRAY_BUFFER,
sizeof(vertices) + sizeof(texCoords),
NULL,
sizeof(vertices),
vertices,
GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBufferSubData(GL_ARRAY_BUFFER, g_texCoordOffset,
sizeof(texCoords), texCoords);
CheckGLError("InitShaders", __LINE__);
}

Expand Down Expand Up @@ -334,35 +313,7 @@ static int stInit(ESContext *esContext) {
}

static void stDraw (ESContext *esContext) {
const float kPi = 3.1415926535897932384626433832795f;

CheckGLError("GLFromCPPDraw", __LINE__);
// TODO(kbr): base the angle on time rather than on ticks
g_angle = (g_angle + 1) % 360;
// Rotate about the Z axis
GLfloat rot_matrix[16];
GLfloat cos_angle = cosf(static_cast<GLfloat>(g_angle) * kPi / 180.0f);
GLfloat sin_angle = sinf(static_cast<GLfloat>(g_angle) * kPi / 180.0f);
// OpenGL matrices are column-major
rot_matrix[0] = cos_angle;
rot_matrix[1] = sin_angle;
rot_matrix[2] = 0.0f;
rot_matrix[3] = 0.0f;

rot_matrix[4] = -sin_angle;
rot_matrix[5] = cos_angle;
rot_matrix[6] = 0.0f;
rot_matrix[7] = 0.0f;

rot_matrix[8] = 0.0f;
rot_matrix[9] = 0.0f;
rot_matrix[10] = 1.0f;
rot_matrix[11] = 0.0f;

rot_matrix[12] = 0.0f;
rot_matrix[13] = 0.0f;
rot_matrix[14] = 0.0f;
rot_matrix[15] = 1.0f;

g_frameCount++;
if (g_frameCount > 60)
Expand All @@ -371,7 +322,7 @@ static void stDraw (ESContext *esContext) {
g_textureIndex = (g_textureIndex + 1) % g_numTextures;
}

// Note: the viewport is automatically set up to cover the entire Canvas.
glViewport(0, 0, esContext->width, esContext->height);
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
Expand All @@ -380,16 +331,11 @@ static void stDraw (ESContext *esContext) {
// Use the program object
glUseProgram(g_programObject);
CheckGLError("GLFromCPPDraw", __LINE__);
// Set up the model matrix
glUniformMatrix4fv(g_worldMatrixLoc, 1, GL_FALSE, rot_matrix);

// Load the vertex data
glBindBuffer(GL_ARRAY_BUFFER, g_vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0,
reinterpret_cast<const void*>(g_texCoordOffset));
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
CheckGLError("GLFromCPPDraw", __LINE__);
// Bind the texture to texture unit 0
glBindTexture(GL_TEXTURE_2D, g_textures[g_textureIndex]);
Expand Down
1 change: 0 additions & 1 deletion gpu/demos/framework/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ bool Window::CreateRenderContext(gfx::AcceleratedWidget hwnd) {
gles2_cmd_helper_.get(),
NULL,
transfer_buffer_.get(),
false,
true,
NULL));

Expand Down

0 comments on commit 30aeb11

Please sign in to comment.