progs/tests: Use compressed texture in mipmap_comp_tests
[mesa.git] / progs / glsl / mandelbrot.c
index e7b2b04b0df83ccf95bca2a61f8e5a695eeb31f0..eeea4eb52ae2b6d259adee89598d4f4e5477d0e2 100644 (file)
@@ -9,14 +9,16 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
+#include <GL/glew.h>
 #include <GL/gl.h>
 #include <GL/glut.h>
 #include <GL/glext.h>
 #include "extfuncs.h"
+#include "shaderutil.h"
 
 
-static char *FragProgFile = "CH18-mandel.frag.txt";
-static char *VertProgFile = "CH18-mandel.vert.txt";
+static char *FragProgFile = "CH18-mandel.frag";
+static char *VertProgFile = "CH18-mandel.vert";
 
 /* program/shader objects */
 static GLuint fragShader;
@@ -24,28 +26,21 @@ static GLuint vertShader;
 static GLuint program;
 
 
-struct uniform_info {
-   const char *name;
-   GLuint size;
-   GLint location;
-   GLfloat value[4];
-};
-
 static struct uniform_info Uniforms[] = {
    /* vert */
-   { "LightPosition",        3, -1, { 0.1, 0.1, 9.0, 0} },
-   { "SpecularContribution", 1, -1, { 0.5, 0, 0, 0 } },
-   { "DiffuseContribution",  1, -1, { 0.5, 0, 0, 0 } },
-   { "Shininess",            1, -1, { 20.0, 0, 0, 0 } },
+   { "LightPosition",        3, GL_FLOAT, { 0.1, 0.1, 9.0, 0}, -1 },
+   { "SpecularContribution", 1, GL_FLOAT, { 0.5, 0, 0, 0 }, -1 },
+   { "DiffuseContribution",  1, GL_FLOAT, { 0.5, 0, 0, 0 }, -1 },
+   { "Shininess",            1, GL_FLOAT, { 20.0, 0, 0, 0 }, -1 },
    /* frag */
-   { "MaxIterations",        1, -1, { 12, 0, 0, 0 } },
-   { "Zoom",                 1, -1, { 0.125, 0, 0, 0 } },
-   { "Xcenter",              1, -1, { -1.5, 0, 0, 0 } },
-   { "Ycenter",              1, -1, { .005, 0, 0, 0 } },
-   { "InnerColor",           3, -1, { 1, 0, 0, 0 } },
-   { "OuterColor1",          3, -1, { 0, 1, 0, 0 } },
-   { "OuterColor2",          3, -1, { 0, 0, 1, 0 } },
-   { NULL, 0, 0, { 0, 0, 0, 0 } }
+   { "MaxIterations",        1, GL_FLOAT, { 12, 0, 0, 0 }, -1 },
+   { "Zoom",                 1, GL_FLOAT, { 0.125, 0, 0, 0 }, -1 },
+   { "Xcenter",              1, GL_FLOAT, { -1.5, 0, 0, 0 }, -1 },
+   { "Ycenter",              1, GL_FLOAT, { .005, 0, 0, 0 }, -1 },
+   { "InnerColor",           3, GL_FLOAT, { 1, 0, 0, 0 }, -1 },
+   { "OuterColor1",          3, GL_FLOAT, { 0, 1, 0, 0 }, -1 },
+   { "OuterColor2",          3, GL_FLOAT, { 0, 0, 1, 0 }, -1 },
+   END_OF_UNIFORMS
 };
 
 static GLint win = 0;
@@ -80,8 +75,6 @@ Redisplay(void)
 
    glPopMatrix();
 
-   glFinish();
-   glFlush();
    glutSwapBuffers();
 }
 
@@ -157,123 +150,21 @@ SpecialKey(int key, int x, int y)
 }
 
 
-
-static void
-LoadAndCompileShader(GLuint shader, const char *text)
-{
-   GLint stat;
-
-   glShaderSource_func(shader, 1, (const GLchar **) &text, NULL);
-
-   glCompileShader_func(shader);
-
-   glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat);
-   if (!stat) {
-      GLchar log[1000];
-      GLsizei len;
-      glGetShaderInfoLog_func(shader, 1000, &len, log);
-      fprintf(stderr, "mandelbrot: problem compiling shader: %s\n", log);
-      exit(1);
-   }
-   else {
-      printf("Shader compiled OK\n");
-   }
-}
-
-
-/**
- * Read a shader from a file.
- */
-static void
-ReadShader(GLuint shader, const char *filename)
-{
-   const int max = 100*1000;
-   int n;
-   char *buffer = (char*) malloc(max);
-   FILE *f = fopen(filename, "r");
-   if (!f) {
-      fprintf(stderr, "mandelbrot: Unable to open shader file %s\n", filename);
-      exit(1);
-   }
-
-   n = fread(buffer, 1, max, f);
-   printf("mandelbrot: read %d bytes from shader file %s\n", n, filename);
-   if (n > 0) {
-      buffer[n] = 0;
-      LoadAndCompileShader(shader, buffer);
-   }
-
-   fclose(f);
-   free(buffer);
-}
-
-
-static void
-CheckLink(GLuint prog)
-{
-   GLint stat;
-   glGetProgramiv_func(prog, GL_LINK_STATUS, &stat);
-   if (!stat) {
-      GLchar log[1000];
-      GLsizei len;
-      glGetProgramInfoLog_func(prog, 1000, &len, log);
-      fprintf(stderr, "Linker error:\n%s\n", log);
-   }
-   else {
-      fprintf(stderr, "Link success!\n");
-   }
-}
-
-
 static void
 Init(void)
 {
-   const char *version;
-   GLint i;
-
-   version = (const char *) glGetString(GL_VERSION);
-   if (version[0] != '2' || version[1] != '.') {
-      printf("Warning: this program expects OpenGL 2.0\n");
-      /*exit(1);*/
-   }
+   if (!ShadersSupported())
+      exit(1);
 
    GetExtensionFuncs();
 
-   vertShader = glCreateShader_func(GL_VERTEX_SHADER);
-   ReadShader(vertShader, VertProgFile);
-
-   fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
-   ReadShader(fragShader, FragProgFile);
+   vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
+   fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
+   program = LinkShaders(vertShader, fragShader);
 
-   program = glCreateProgram_func();
-   glAttachShader_func(program, fragShader);
-   glAttachShader_func(program, vertShader);
-   glLinkProgram_func(program);
-   CheckLink(program);
    glUseProgram_func(program);
 
-   for (i = 0; Uniforms[i].name; i++) {
-      Uniforms[i].location
-         = glGetUniformLocation_func(program, Uniforms[i].name);
-      printf("Uniform %s location: %d\n", Uniforms[i].name,
-             Uniforms[i].location);
-      switch (Uniforms[i].size) {
-      case 1:
-         glUniform1fv_func(Uniforms[i].location, 1, Uniforms[i].value);
-         break;
-      case 2:
-         glUniform2fv_func(Uniforms[i].location, 1, Uniforms[i].value);
-         break;
-      case 3:
-         glUniform3fv_func(Uniforms[i].location, 1, Uniforms[i].value);
-         break;
-      case 4:
-         glUniform4fv_func(Uniforms[i].location, 1, Uniforms[i].value);
-         break;
-      default:
-         abort();
-      }
-   }
+   InitUniforms(program, Uniforms);
 
    uZoom = glGetUniformLocation_func(program, "Zoom");
    uXcenter = glGetUniformLocation_func(program, "Xcenter");
@@ -316,6 +207,7 @@ main(int argc, char *argv[])
    glutInitWindowSize(400, 400);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    win = glutCreateWindow(argv[0]);
+   glewInit();
    glutReshapeFunc(Reshape);
    glutKeyboardFunc(Key);
    glutSpecialFunc(SpecialKey);