Merge branch 'mesa_7_5_branch'
[mesa.git] / progs / tests / floattex.c
index 2345a49b27053bd82c0e674019eb5d79bf14bd4e..ad14cacdcbb1e8ae32f29871c6ae3e118a471891 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Test floating point textures.
- * No actual rendering, yet.
  */
 
 
@@ -8,33 +7,39 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
+#include <GL/glew.h>
 #include <GL/glut.h>
+#include "extfuncs.h"
+#include "readtex.h"
+#include "shaderutil.h"
 
 
-/* XXX - temporary */
-#ifndef GL_ARB_texture_float
-#define GL_ARB_texture_float 1
-#define GL_TEXTURE_RED_TYPE_ARB             0x9000
-#define GL_TEXTURE_GREEN_TYPE_ARB           0x9001
-#define GL_TEXTURE_BLUE_TYPE_ARB            0x9002
-#define GL_TEXTURE_ALPHA_TYPE_ARB           0x9003
-#define GL_TEXTURE_LUMINANCE_TYPE_ARB       0x9004
-#define GL_TEXTURE_INTENSITY_TYPE_ARB       0x9005
-#define GL_TEXTURE_DEPTH_TYPE_ARB           0x9006
-#define GL_UNSIGNED_NORMALIZED_ARB          0x9007
-#define GL_RGBA32F_ARB                      0x8814
-#define GL_RGB32F_ARB                       0x8815
-#define GL_ALPHA32F_ARB                     0x8816
-#define GL_INTENSITY32F_ARB                 0x8817
-#define GL_LUMINANCE32F_ARB                 0x8818
-#define GL_LUMINANCE_ALPHA32F_ARB           0x8819
-#define GL_RGBA16F_ARB                      0x881A
-#define GL_RGB16F_ARB                       0x881B
-#define GL_ALPHA16F_ARB                     0x881C
-#define GL_INTENSITY16F_ARB                 0x881D
-#define GL_LUMINANCE16F_ARB                 0x881E
-#define GL_LUMINANCE_ALPHA16F_ARB           0x881F
-#endif
+static const char *TexFile = "../images/arch.rgb";
+
+static const char *FragShaderText = 
+   "uniform sampler2D tex1; \n"
+   "void main() \n"
+   "{ \n"
+   "   vec4 t = texture2D(tex1, gl_TexCoord[0].xy); \n"
+   "   // convert from [-255,0] to [0,1] \n"
+   "   gl_FragColor = t * (-1.0 / 255.0); \n"
+   "} \n";
+
+static const char *VertShaderText = 
+   "void main() \n"
+   "{ \n"
+   "   gl_TexCoord[0] = gl_MultiTexCoord0; \n"
+   "   gl_Position = ftransform(); \n"
+   "} \n";
+
+static struct uniform_info Uniforms[] = {
+   { "tex1",  1, GL_INT, { 0, 0, 0, 0 }, -1 },
+   END_OF_UNIFORMS
+};
+
+
+static GLuint Program;
+
 
 
 static GLboolean
@@ -57,7 +62,12 @@ Draw(void)
 
    glPushMatrix();
 
-   glutSolidCube(2.0);
+   glBegin(GL_POLYGON);
+   glTexCoord2f( 0.0, 0.0 );   glVertex2f( -1.0, -1.0 );
+   glTexCoord2f( 1.0, 0.0 );   glVertex2f(  1.0, -1.0 );
+   glTexCoord2f( 1.0, 1.0 );   glVertex2f(  1.0,  1.0 );
+   glTexCoord2f( 0.0, 1.0 );   glVertex2f( -1.0,  1.0 );
+   glEnd();
 
    glPopMatrix();
 
@@ -74,7 +84,7 @@ Reshape(int width, int height)
    glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
-   glTranslatef(0.0, 0.0, -15.0);
+   glTranslatef(0.0, 0.0, -8.0);
 }
 
 
@@ -92,33 +102,46 @@ Key(unsigned char key, int x, int y)
 }
 
 
-
 static void
-Init(void)
+InitTexture(void)
 {
-   GLfloat tex[16][16][4];
-   GLfloat tex2[16][16][4];
-   GLint i, j, t;
+   GLenum filter = GL_LINEAR;
+   GLint imgWidth, imgHeight;
+   GLenum imgFormat;
+   GLubyte *image = NULL;
+   GLfloat *ftex;
+   GLint i, t;
+
+   image = LoadRGBImage(TexFile, &imgWidth, &imgHeight, &imgFormat);
+   if (!image) {
+      printf("Couldn't read %s\n", TexFile);
+      exit(0);
+   }
 
-   if (!glutExtensionSupported("GL_MESAX_texture_float")) {
-      printf("Sorry, this test requires GL_MESAX_texture_float\n");
-      exit(1);
+   assert(imgFormat == GL_RGB);
+
+   ftex = (float *) malloc(imgWidth * imgHeight * 4 * sizeof(float));
+   if (!ftex) {
+      printf("out of memory\n");
+      exit(0);
    }
 
-   for (i = 0; i < 16; i++) {
-      for (j = 0; j < 16; j++) {
-         GLfloat s = i / 15.0;
-         tex[i][j][0] = s;
-         tex[i][j][1] = 2.0 * s;
-         tex[i][j][2] = -3.0 * s;
-         tex[i][j][3] = 4.0 * s;
-      }
+   /* convert ubytes to floats, negated */
+   for (i = 0; i < imgWidth * imgHeight * 3; i++) {
+      ftex[i] = -1.0f * image[i];
    }
 
-   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, 16, 16, 0, GL_RGBA,
-                GL_FLOAT, tex);
+   glActiveTexture(GL_TEXTURE0);
+   glBindTexture(GL_TEXTURE_2D, 42);
+
+   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB,
+                imgWidth, imgHeight, 0,
+                GL_RGB, GL_FLOAT, ftex);
+
+
    CheckError(__LINE__);
 
+   /* sanity checks */
    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_RED_TYPE_ARB, &t);
    assert(t == GL_FLOAT);
    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_TYPE_ARB, &t);
@@ -128,27 +151,72 @@ Init(void)
    glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_TYPE_ARB, &t);
    assert(t == GL_FLOAT);
 
-   CheckError(__LINE__);
-
-   /* read back the texture and make sure values are correct */
-   glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, tex2);
-   CheckError(__LINE__);
-   for (i = 0; i < 16; i++) {
-      for (j = 0; j < 16; j++) {
-         if (tex[i][j][0] != tex2[i][j][0] ||
-             tex[i][j][1] != tex2[i][j][1] ||
-             tex[i][j][2] != tex2[i][j][2] ||
-             tex[i][j][3] != tex2[i][j][3]) {
-            printf("tex[%d][%d] %g %g %g %g != tex2[%d][%d] %g %g %g %g\n",
-                   i, j,
-                   tex[i][j][0], tex[i][j][1], tex[i][j][2], tex[i][j][3],
-                   i, j,
-                   tex2[i][j][0], tex2[i][j][1], tex2[i][j][2], tex2[i][j][3]);
+   free(image);
+   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
+   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
+
+   if (1) {
+      /* read back the texture and make sure values are correct */
+      GLfloat *tex2 = (GLfloat *)
+         malloc(imgWidth * imgHeight * 4 * sizeof(GLfloat));
+      glGetTexImage(GL_TEXTURE_2D, 0, imgFormat, GL_FLOAT, tex2);
+      CheckError(__LINE__);
+      for (i = 0; i < imgWidth * imgHeight * 4; i++) {
+         if (ftex[i] != tex2[i]) {
+            printf("tex[%d] %g != tex2[%d] %g\n",
+                   i, ftex[i], i, tex2[i]);
          }
       }
    }
 
+   free(ftex);
+}
+
+
+static GLuint
+CreateProgram(void)
+{
+   GLuint fragShader, vertShader, program;
+
+   vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText);
+   fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText);
+   assert(vertShader);
+   program = LinkShaders(vertShader, fragShader);
+
+   assert(program);
+
+   glUseProgram_func(program);
+
+   InitUniforms(program, Uniforms);
+
+   return program;
+}
+
+
+static void
+Init(void)
+{
+   glClearColor(0.25, 0.25, 0.25, 0.0);
+
+   GetExtensionFuncs();
+
+   if (!ShadersSupported()) {
+      printf("Sorry, this test requires GLSL\n");
+      exit(1);
+   }
+
+   if (!glutExtensionSupported("GL_MESAX_texture_float") &&
+       !glutExtensionSupported("GL_ARB_texture_float")) {
+      printf("Sorry, this test requires GL_MESAX/ARB_texture_float\n");
+      exit(1);
+   }
+
+   InitTexture();
 
+   Program = CreateProgram();
+   glUseProgram_func(Program);
 }
 
 
@@ -160,6 +228,7 @@ main(int argc, char *argv[])
    glutInitWindowSize(400, 400);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow(argv[0]);
+   glewInit();
    glutReshapeFunc(Reshape);
    glutKeyboardFunc(Key);
    glutDisplayFunc(Draw);