r300: Cleanup r300_fragment_program_code
[mesa.git] / progs / glsl / multitex.c
index b4be463787a84095239f5b9ac1fd6aa962bc64df..5e971716add858e9f5c6ed12e7b70b1c44c69911 100644 (file)
@@ -28,9 +28,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <GL/glew.h>
 #include "GL/glut.h"
 #include "readtex.h"
-#include "extfuncs.h"
 #include "shaderutil.h"
 
 static const char *Demo = "multitex";
@@ -51,6 +51,8 @@ static GLfloat Xrot = 0.0, Yrot = .0, Zrot = 0.0;
 static GLfloat EyeDist = 10;
 static GLboolean Anim = GL_TRUE;
 static GLboolean UseArrays = GL_TRUE;
+static GLboolean UseVBO = GL_TRUE;
+static GLuint VBO = 0;
 
 static GLint VertCoord_attr = -1, TexCoord0_attr = -1, TexCoord1_attr = -1;
 
@@ -76,28 +78,81 @@ static const GLfloat VertCoords[4][2] = {
 };
 
 
+
+static void
+SetupVertexBuffer(void)
+{
+   glGenBuffersARB(1, &VBO);
+   glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBO);
+
+   glBufferDataARB(GL_ARRAY_BUFFER_ARB,
+                        sizeof(VertCoords) +
+                        sizeof(Tex0Coords) +
+                        sizeof(Tex1Coords),
+                        NULL,
+                        GL_STATIC_DRAW_ARB);
+
+   /* non-interleaved vertex arrays */
+
+   glBufferSubDataARB(GL_ARRAY_BUFFER_ARB,
+                           0,                   /* offset */
+                           sizeof(VertCoords),  /* size */
+                           VertCoords);         /* data */
+
+   glBufferSubDataARB(GL_ARRAY_BUFFER_ARB,
+                           sizeof(VertCoords),  /* offset */
+                           sizeof(Tex0Coords),  /* size */
+                           Tex0Coords);         /* data */
+
+   glBufferSubDataARB(GL_ARRAY_BUFFER_ARB,
+                           sizeof(VertCoords) +
+                           sizeof(Tex0Coords),  /* offset */
+                           sizeof(Tex1Coords),  /* size */
+                           Tex1Coords);         /* data */
+
+   glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+}
+
+
 static void
 DrawPolygonArray(void)
 {
+   void *vertPtr, *tex0Ptr, *tex1Ptr;
+
+   if (UseVBO) {
+      glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBO);
+      vertPtr = (void *) 0;
+      tex0Ptr = (void *) sizeof(VertCoords);
+      tex1Ptr = (void *) (sizeof(VertCoords) + sizeof(Tex0Coords));
+   }
+   else {
+      glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+      vertPtr = VertCoords;
+      tex0Ptr = Tex0Coords;
+      tex1Ptr = Tex1Coords;
+   }
+
    if (VertCoord_attr >= 0) {
-      glVertexAttribPointer_func(VertCoord_attr, 2, GL_FLOAT, GL_FALSE,
+      glVertexAttribPointer(VertCoord_attr, 2, GL_FLOAT, GL_FALSE,
                                  0, VertCoords);
-      glEnableVertexAttribArray_func(VertCoord_attr);
+      glEnableVertexAttribArray(VertCoord_attr);
    }
    else {
-      glVertexPointer(2, GL_FLOAT, 0, VertCoords);
-      glEnable(GL_VERTEX_ARRAY);
+      glVertexPointer(2, GL_FLOAT, 0, vertPtr);
+      glEnableClientState(GL_VERTEX_ARRAY);
    }
 
-   glVertexAttribPointer_func(TexCoord0_attr, 2, GL_FLOAT, GL_FALSE,
+   glVertexAttribPointer(TexCoord0_attr, 2, GL_FLOAT, GL_FALSE,
                               0, Tex0Coords);
-   glEnableVertexAttribArray_func(TexCoord0_attr);
+   glEnableVertexAttribArray(TexCoord0_attr);
 
-   glVertexAttribPointer_func(TexCoord1_attr, 2, GL_FLOAT, GL_FALSE,
+   glVertexAttribPointer(TexCoord1_attr, 2, GL_FLOAT, GL_FALSE,
                               0, Tex1Coords);
-   glEnableVertexAttribArray_func(TexCoord1_attr);
+   glEnableVertexAttribArray(TexCoord1_attr);
 
    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+
+   glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
 }
 
 
@@ -109,11 +164,11 @@ DrawPolygonVert(void)
    glBegin(GL_TRIANGLE_FAN);
 
    for (i = 0; i < 4; i++) {
-      glVertexAttrib2fv_func(TexCoord0_attr, Tex0Coords[i]);
-      glVertexAttrib2fv_func(TexCoord1_attr, Tex1Coords[i]);
+      glVertexAttrib2fv(TexCoord0_attr, Tex0Coords[i]);
+      glVertexAttrib2fv(TexCoord1_attr, Tex1Coords[i]);
 
       if (VertCoord_attr >= 0)
-         glVertexAttrib2fv_func(VertCoord_attr, VertCoords[i]);
+         glVertexAttrib2fv(VertCoord_attr, VertCoords[i]);
       else
          glVertex2fv(VertCoords[i]);
    }
@@ -163,6 +218,10 @@ key(unsigned char k, int x, int y)
       UseArrays = !UseArrays;
       printf("Arrays: %d\n", UseArrays);
       break;
+   case 'v':
+      UseVBO = !UseVBO;
+      printf("Use VBO: %d\n", UseVBO);
+      break;
    case ' ':
       Anim = !Anim;
       if (Anim)
@@ -267,13 +326,28 @@ CreateProgram(const char *vertProgFile, const char *fragProgFile,
    assert(vertShader);
    program = LinkShaders(vertShader, fragShader);
 
-   glUseProgram_func(program);
+   glUseProgram(program);
 
    InitUniforms(program, uniforms);
 
-   TexCoord0_attr = glGetAttribLocation_func(program, "TexCoord0");
-   TexCoord1_attr = glGetAttribLocation_func(program, "TexCoord1");
-   VertCoord_attr = glGetAttribLocation_func(program, "VertCoord");
+   VertCoord_attr = glGetAttribLocation(program, "VertCoord");
+   if (VertCoord_attr > 0) {
+      /* We want the VertCoord attrib to have position zero so that
+       * the call to glVertexAttrib(0, xyz) triggers vertex processing.
+       * Otherwise, if TexCoord0 or TexCoord1 gets position 0 we'd have
+       * to set that attribute last (which is a PITA to manage).
+       */
+      glBindAttribLocation(program, 0, "VertCoord");
+      /* re-link */
+      glLinkProgram(program);
+      /* VertCoord_attr should be zero now */
+      VertCoord_attr = glGetAttribLocation(program, "VertCoord");
+      assert(VertCoord_attr == 0);
+   }
+
+   TexCoord0_attr = glGetAttribLocation(program, "TexCoord0");
+   TexCoord1_attr = glGetAttribLocation(program, "TexCoord1");
+
    printf("TexCoord0_attr = %d\n", TexCoord0_attr);
    printf("TexCoord1_attr = %d\n", TexCoord1_attr);
    printf("VertCoord_attr = %d\n", VertCoord_attr);
@@ -299,12 +373,18 @@ InitGL(void)
       /*exit(1);*/
    }
    printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
-
-   GetExtensionFuncs();
+   printf("Usage:\n");
+   printf("  a     - toggle arrays vs. immediate mode rendering\n");
+   printf("  v     - toggle VBO usage for array rendering\n");
+   printf("  z/Z   - change viewing distance\n");
+   printf("  SPACE - toggle animation\n");
+   printf("  Esc   - exit\n");
 
    InitTextures();
    InitPrograms();
 
+   SetupVertexBuffer();
+
    glEnable(GL_DEPTH_TEST);
 
    glClearColor(.6, .6, .9, 0);
@@ -319,6 +399,7 @@ main(int argc, char *argv[])
    glutInitWindowSize(500, 400);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutCreateWindow(Demo);
+   glewInit();
    glutReshapeFunc(Reshape);
    glutKeyboardFunc(key);
    glutSpecialFunc(specialkey);