Merge remote branch 'origin/master' into radeon-rewrite
[mesa-demos.git] / src / tests / floattex.c
blobdd6d882089c33cd4de714a4884f0a8e77c065c8e
1 /*
2 * Test floating point textures.
3 * No actual rendering, yet.
4 */
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <math.h>
11 #include <GL/glut.h>
12 #include "extfuncs.h"
13 #include "readtex.h"
14 #include "shaderutil.h"
17 static const char *TexFile = "../images/arch.rgb";
19 static const char *FragShaderText =
20 "uniform sampler2D tex1; \n"
21 "void main() \n"
22 "{ \n"
23 " vec4 t = texture2D(tex1, gl_TexCoord[0].xy); \n"
24 " // convert from [-255,0] to [0,1] \n"
25 " gl_FragColor = t * (-1.0 / 255.0); \n"
26 "} \n";
28 static const char *VertShaderText =
29 "void main() \n"
30 "{ \n"
31 " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
32 " gl_Position = ftransform(); \n"
33 "} \n";
35 static struct uniform_info Uniforms[] = {
36 { "tex1", 1, GL_INT, { 0, 0, 0, 0 }, -1 },
37 END_OF_UNIFORMS
41 static GLuint Program;
45 static GLboolean
46 CheckError( int line )
48 GLenum error = glGetError();
49 if (error) {
50 char *err = (char *) gluErrorString( error );
51 fprintf( stderr, "GL Error: %s at line %d\n", err, line );
52 return GL_TRUE;
54 return GL_FALSE;
58 static void
59 Draw(void)
61 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
63 glPushMatrix();
65 glBegin(GL_POLYGON);
66 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
67 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
68 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
69 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
70 glEnd();
72 glPopMatrix();
74 glutSwapBuffers();
78 static void
79 Reshape(int width, int height)
81 glViewport(0, 0, width, height);
82 glMatrixMode(GL_PROJECTION);
83 glLoadIdentity();
84 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
85 glMatrixMode(GL_MODELVIEW);
86 glLoadIdentity();
87 glTranslatef(0.0, 0.0, -8.0);
91 static void
92 Key(unsigned char key, int x, int y)
94 (void) x;
95 (void) y;
96 switch (key) {
97 case 27:
98 exit(0);
99 break;
101 glutPostRedisplay();
106 static void
107 InitTexture(void)
109 GLenum filter = GL_LINEAR;
110 GLint imgWidth, imgHeight;
111 GLenum imgFormat;
112 GLubyte *image = NULL;
113 GLfloat *ftex;
114 GLint i, t;
116 image = LoadRGBImage(TexFile, &imgWidth, &imgHeight, &imgFormat);
117 if (!image) {
118 printf("Couldn't read %s\n", TexFile);
119 exit(0);
122 assert(imgFormat == GL_RGB);
124 ftex = (float *) malloc(imgWidth * imgHeight * 4 * sizeof(float));
125 if (!ftex) {
126 printf("out of memory\n");
127 exit(0);
130 /* convert ubytes to floats, negated */
131 for (i = 0; i < imgWidth * imgHeight * 3; i++) {
132 ftex[i] = -1.0f * image[i];
135 glActiveTexture(GL_TEXTURE0);
136 glBindTexture(GL_TEXTURE_2D, 42);
138 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB,
139 imgWidth, imgHeight, 0,
140 GL_RGB, GL_FLOAT, ftex);
143 /* sanity checks */
144 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_RED_TYPE_ARB, &t);
145 assert(t == GL_FLOAT);
146 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_TYPE_ARB, &t);
147 assert(t == GL_FLOAT);
148 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_TYPE_ARB, &t);
149 assert(t == GL_FLOAT);
150 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_TYPE_ARB, &t);
151 assert(t == GL_FLOAT);
153 free(image);
154 free(ftex);
156 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
157 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
158 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
159 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
161 #if 0
162 /* read back the texture and make sure values are correct */
163 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, tex2);
164 CheckError(__LINE__);
165 for (i = 0; i < 16; i++) {
166 for (j = 0; j < 16; j++) {
167 if (tex[i][j][0] != tex2[i][j][0] ||
168 tex[i][j][1] != tex2[i][j][1] ||
169 tex[i][j][2] != tex2[i][j][2] ||
170 tex[i][j][3] != tex2[i][j][3]) {
171 printf("tex[%d][%d] %g %g %g %g != tex2[%d][%d] %g %g %g %g\n",
172 i, j,
173 tex[i][j][0], tex[i][j][1], tex[i][j][2], tex[i][j][3],
174 i, j,
175 tex2[i][j][0], tex2[i][j][1], tex2[i][j][2], tex2[i][j][3]);
179 #endif
183 static GLuint
184 CreateProgram(void)
186 GLuint fragShader, vertShader, program;
188 vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText);
189 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText);
190 assert(vertShader);
191 program = LinkShaders(vertShader, fragShader);
193 assert(program);
195 // InitUniforms(program, Uniforms);
197 return program;
201 static void
202 Init(void)
204 glClearColor(0.25, 0.25, 0.25, 0.0);
206 GetExtensionFuncs();
208 if (!ShadersSupported()) {
209 printf("Sorry, this test requires GLSL\n");
210 exit(1);
213 if (!glutExtensionSupported("GL_MESAX_texture_float")) {
214 printf("Sorry, this test requires GL_MESAX_texture_float\n");
215 exit(1);
218 InitTexture();
220 Program = CreateProgram();
221 glUseProgram_func(Program);
226 main(int argc, char *argv[])
228 glutInit(&argc, argv);
229 glutInitWindowPosition(0, 0);
230 glutInitWindowSize(400, 400);
231 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
232 glutCreateWindow(argv[0]);
233 glutReshapeFunc(Reshape);
234 glutKeyboardFunc(Key);
235 glutDisplayFunc(Draw);
236 Init();
237 glutMainLoop();
238 return 0;