Put vertex attribute data into a VBO.
authorJesse Barker <jesse.barker@linaro.org>
Thu, 18 Oct 2012 16:30:49 +0000 (09:30 -0700)
committerRob Clark <rob@ti.com>
Wed, 24 Oct 2012 16:59:39 +0000 (11:59 -0500)
This makes the division between set up (init_gl()), and draw (draw())
code cleaner, and should be more efficient in general.

Signed-off-by: Jesse Barker <jesse.barker@linaro.org>
kmscube.c

index 20516155b908ddf58edc103b194f4891f797027d..bd32afed968b1bf8fccb9a26840a8debf176e978 100644 (file)
--- a/kmscube.c
+++ b/kmscube.c
@@ -49,6 +49,8 @@ static struct {
        EGLSurface surface;
        GLuint program;
        GLint modelviewmatrix, modelviewprojectionmatrix, normalmatrix;
+       GLuint vbo;
+       GLuint positionsoffset, colorsoffset, normalsoffset;
 } gl;
 
 static struct {
@@ -176,6 +178,105 @@ static int init_gl(void)
        GLuint vertex_shader, fragment_shader;
        GLint ret;
 
+       static const GLfloat vVertices[] = {
+                       // front
+                       -1.0f, -1.0f, +1.0f, // point blue
+                       +1.0f, -1.0f, +1.0f, // point magenta
+                       -1.0f, +1.0f, +1.0f, // point cyan
+                       +1.0f, +1.0f, +1.0f, // point white
+                       // back
+                       +1.0f, -1.0f, -1.0f, // point red
+                       -1.0f, -1.0f, -1.0f, // point black
+                       +1.0f, +1.0f, -1.0f, // point yellow
+                       -1.0f, +1.0f, -1.0f, // point green
+                       // right
+                       +1.0f, -1.0f, +1.0f, // point magenta
+                       +1.0f, -1.0f, -1.0f, // point red
+                       +1.0f, +1.0f, +1.0f, // point white
+                       +1.0f, +1.0f, -1.0f, // point yellow
+                       // left
+                       -1.0f, -1.0f, -1.0f, // point black
+                       -1.0f, -1.0f, +1.0f, // point blue
+                       -1.0f, +1.0f, -1.0f, // point green
+                       -1.0f, +1.0f, +1.0f, // point cyan
+                       // top
+                       -1.0f, +1.0f, +1.0f, // point cyan
+                       +1.0f, +1.0f, +1.0f, // point white
+                       -1.0f, +1.0f, -1.0f, // point green
+                       +1.0f, +1.0f, -1.0f, // point yellow
+                       // bottom
+                       -1.0f, -1.0f, -1.0f, // point black
+                       +1.0f, -1.0f, -1.0f, // point red
+                       -1.0f, -1.0f, +1.0f, // point blue
+                       +1.0f, -1.0f, +1.0f  // point magenta
+       };
+
+       static const GLfloat vColors[] = {
+                       // front
+                       0.0f,  0.0f,  1.0f, // blue
+                       1.0f,  0.0f,  1.0f, // magenta
+                       0.0f,  1.0f,  1.0f, // cyan
+                       1.0f,  1.0f,  1.0f, // white
+                       // back
+                       1.0f,  0.0f,  0.0f, // red
+                       0.0f,  0.0f,  0.0f, // black
+                       1.0f,  1.0f,  0.0f, // yellow
+                       0.0f,  1.0f,  0.0f, // green
+                       // right
+                       1.0f,  0.0f,  1.0f, // magenta
+                       1.0f,  0.0f,  0.0f, // red
+                       1.0f,  1.0f,  1.0f, // white
+                       1.0f,  1.0f,  0.0f, // yellow
+                       // left
+                       0.0f,  0.0f,  0.0f, // black
+                       0.0f,  0.0f,  1.0f, // blue
+                       0.0f,  1.0f,  0.0f, // green
+                       0.0f,  1.0f,  1.0f, // cyan
+                       // top
+                       0.0f,  1.0f,  1.0f, // cyan
+                       1.0f,  1.0f,  1.0f, // white
+                       0.0f,  1.0f,  0.0f, // green
+                       1.0f,  1.0f,  0.0f, // yellow
+                       // bottom
+                       0.0f,  0.0f,  0.0f, // black
+                       1.0f,  0.0f,  0.0f, // red
+                       0.0f,  0.0f,  1.0f, // blue
+                       1.0f,  0.0f,  1.0f  // magenta
+       };
+
+       static const GLfloat vNormals[] = {
+                       // front
+                       +0.0f, +0.0f, +1.0f, // forward
+                       +0.0f, +0.0f, +1.0f, // forward
+                       +0.0f, +0.0f, +1.0f, // forward
+                       +0.0f, +0.0f, +1.0f, // forward
+                       // back
+                       +0.0f, +0.0f, -1.0f, // backbard
+                       +0.0f, +0.0f, -1.0f, // backbard
+                       +0.0f, +0.0f, -1.0f, // backbard
+                       +0.0f, +0.0f, -1.0f, // backbard
+                       // right
+                       +1.0f, +0.0f, +0.0f, // right
+                       +1.0f, +0.0f, +0.0f, // right
+                       +1.0f, +0.0f, +0.0f, // right
+                       +1.0f, +0.0f, +0.0f, // right
+                       // left
+                       -1.0f, +0.0f, +0.0f, // left
+                       -1.0f, +0.0f, +0.0f, // left
+                       -1.0f, +0.0f, +0.0f, // left
+                       -1.0f, +0.0f, +0.0f, // left
+                       // top
+                       +0.0f, +1.0f, +0.0f, // up
+                       +0.0f, +1.0f, +0.0f, // up
+                       +0.0f, +1.0f, +0.0f, // up
+                       +0.0f, +1.0f, +0.0f, // up
+                       // bottom
+                       +0.0f, -1.0f, +0.0f, // down
+                       +0.0f, -1.0f, +0.0f, // down
+                       +0.0f, -1.0f, +0.0f, // down
+                       +0.0f, -1.0f, +0.0f  // down
+       };
+
        static const EGLint context_attribs[] = {
                EGL_CONTEXT_CLIENT_VERSION, 2,
                EGL_NONE
@@ -341,6 +442,23 @@ static int init_gl(void)
        gl.normalmatrix = glGetUniformLocation(gl.program, "normalMatrix");
 
        glViewport(0, 0, drm.mode->hdisplay, drm.mode->vdisplay);
+       glEnable(GL_CULL_FACE);
+
+       gl.positionsoffset = 0;
+       gl.colorsoffset = sizeof(vVertices);
+       gl.normalsoffset = sizeof(vVertices) + sizeof(vColors);
+       glGenBuffers(1, &gl.vbo);
+       glBindBuffer(GL_ARRAY_BUFFER, gl.vbo);
+       glBufferData(GL_ARRAY_BUFFER, sizeof(vVertices) + sizeof(vColors) + sizeof(vNormals), 0, GL_STATIC_DRAW);
+       glBufferSubData(GL_ARRAY_BUFFER, gl.positionsoffset, sizeof(vVertices), &vVertices[0]);
+       glBufferSubData(GL_ARRAY_BUFFER, gl.colorsoffset, sizeof(vColors), &vColors[0]);
+       glBufferSubData(GL_ARRAY_BUFFER, gl.normalsoffset, sizeof(vNormals), &vNormals[0]);
+       glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)gl.positionsoffset);
+       glEnableVertexAttribArray(0);
+       glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)gl.normalsoffset);
+       glEnableVertexAttribArray(1);
+       glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)gl.colorsoffset);
+       glEnableVertexAttribArray(2);
 
        return 0;
 }
@@ -348,118 +466,11 @@ static int init_gl(void)
 static void draw(uint32_t i)
 {
        ESMatrix modelview;
-       static const GLfloat vVertices[] = {
-                       // front
-                       -1.0f, -1.0f, +1.0f, // point blue
-                       +1.0f, -1.0f, +1.0f, // point magenta
-                       -1.0f, +1.0f, +1.0f, // point cyan
-                       +1.0f, +1.0f, +1.0f, // point white
-                       // back
-                       +1.0f, -1.0f, -1.0f, // point red
-                       -1.0f, -1.0f, -1.0f, // point black
-                       +1.0f, +1.0f, -1.0f, // point yellow
-                       -1.0f, +1.0f, -1.0f, // point green
-                       // right
-                       +1.0f, -1.0f, +1.0f, // point magenta
-                       +1.0f, -1.0f, -1.0f, // point red
-                       +1.0f, +1.0f, +1.0f, // point white
-                       +1.0f, +1.0f, -1.0f, // point yellow
-                       // left
-                       -1.0f, -1.0f, -1.0f, // point black
-                       -1.0f, -1.0f, +1.0f, // point blue
-                       -1.0f, +1.0f, -1.0f, // point green
-                       -1.0f, +1.0f, +1.0f, // point cyan
-                       // top
-                       -1.0f, +1.0f, +1.0f, // point cyan
-                       +1.0f, +1.0f, +1.0f, // point white
-                       -1.0f, +1.0f, -1.0f, // point green
-                       +1.0f, +1.0f, -1.0f, // point yellow
-                       // bottom
-                       -1.0f, -1.0f, -1.0f, // point black
-                       +1.0f, -1.0f, -1.0f, // point red
-                       -1.0f, -1.0f, +1.0f, // point blue
-                       +1.0f, -1.0f, +1.0f  // point magenta
-       };
-
-       static const GLfloat vColors[] = {
-                       // front
-                       0.0f,  0.0f,  1.0f, // blue
-                       1.0f,  0.0f,  1.0f, // magenta
-                       0.0f,  1.0f,  1.0f, // cyan
-                       1.0f,  1.0f,  1.0f, // white
-                       // back
-                       1.0f,  0.0f,  0.0f, // red
-                       0.0f,  0.0f,  0.0f, // black
-                       1.0f,  1.0f,  0.0f, // yellow
-                       0.0f,  1.0f,  0.0f, // green
-                       // right
-                       1.0f,  0.0f,  1.0f, // magenta
-                       1.0f,  0.0f,  0.0f, // red
-                       1.0f,  1.0f,  1.0f, // white
-                       1.0f,  1.0f,  0.0f, // yellow
-                       // left
-                       0.0f,  0.0f,  0.0f, // black
-                       0.0f,  0.0f,  1.0f, // blue
-                       0.0f,  1.0f,  0.0f, // green
-                       0.0f,  1.0f,  1.0f, // cyan
-                       // top
-                       0.0f,  1.0f,  1.0f, // cyan
-                       1.0f,  1.0f,  1.0f, // white
-                       0.0f,  1.0f,  0.0f, // green
-                       1.0f,  1.0f,  0.0f, // yellow
-                       // bottom
-                       0.0f,  0.0f,  0.0f, // black
-                       1.0f,  0.0f,  0.0f, // red
-                       0.0f,  0.0f,  1.0f, // blue
-                       1.0f,  0.0f,  1.0f  // magenta
-       };
-
-       static const GLfloat vNormals[] = {
-                       // front
-                       +0.0f, +0.0f, +1.0f, // forward
-                       +0.0f, +0.0f, +1.0f, // forward
-                       +0.0f, +0.0f, +1.0f, // forward
-                       +0.0f, +0.0f, +1.0f, // forward
-                       // back
-                       +0.0f, +0.0f, -1.0f, // backbard
-                       +0.0f, +0.0f, -1.0f, // backbard
-                       +0.0f, +0.0f, -1.0f, // backbard
-                       +0.0f, +0.0f, -1.0f, // backbard
-                       // right
-                       +1.0f, +0.0f, +0.0f, // right
-                       +1.0f, +0.0f, +0.0f, // right
-                       +1.0f, +0.0f, +0.0f, // right
-                       +1.0f, +0.0f, +0.0f, // right
-                       // left
-                       -1.0f, +0.0f, +0.0f, // left
-                       -1.0f, +0.0f, +0.0f, // left
-                       -1.0f, +0.0f, +0.0f, // left
-                       -1.0f, +0.0f, +0.0f, // left
-                       // top
-                       +0.0f, +1.0f, +0.0f, // up
-                       +0.0f, +1.0f, +0.0f, // up
-                       +0.0f, +1.0f, +0.0f, // up
-                       +0.0f, +1.0f, +0.0f, // up
-                       // bottom
-                       +0.0f, -1.0f, +0.0f, // down
-                       +0.0f, -1.0f, +0.0f, // down
-                       +0.0f, -1.0f, +0.0f, // down
-                       +0.0f, -1.0f, +0.0f  // down
-       };
 
        /* clear the color buffer */
        glClearColor(0.5, 0.5, 0.5, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);
 
-       glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
-       glEnableVertexAttribArray(0);
-
-       glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, vNormals);
-       glEnableVertexAttribArray(1);
-
-       glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, vColors);
-       glEnableVertexAttribArray(2);
-
        esMatrixLoadIdentity(&modelview);
        esTranslate(&modelview, 0.0f, 0.0f, -8.0f);
        esRotate(&modelview, 45.0f + (0.25f * i), 1.0f, 0.0f, 0.0f);
@@ -491,8 +502,6 @@ static void draw(uint32_t i)
        glUniformMatrix4fv(gl.modelviewprojectionmatrix, 1, GL_FALSE, &modelviewprojection.m[0][0]);
        glUniformMatrix3fv(gl.normalmatrix, 1, GL_FALSE, normal);
 
-       glEnable(GL_CULL_FACE);
-
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
        glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
        glDrawArrays(GL_TRIANGLE_STRIP, 8, 4);