Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / progs / tests / floattex.c
1 /*
2 * Test floating point textures.
3 * No actual rendering, yet.
4 */
5
6
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"
15
16
17 static const char *TexFile = "../images/arch.rgb";
18
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";
27
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";
34
35 static struct uniform_info Uniforms[] = {
36 { "tex1", 1, GL_INT, { 0, 0, 0, 0 }, -1 },
37 END_OF_UNIFORMS
38 };
39
40
41 static GLuint Program;
42
43
44
45 static GLboolean
46 CheckError( int line )
47 {
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;
53 }
54 return GL_FALSE;
55 }
56
57
58 static void
59 Draw(void)
60 {
61 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
62
63 glPushMatrix();
64
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();
71
72 glPopMatrix();
73
74 glutSwapBuffers();
75 }
76
77
78 static void
79 Reshape(int width, int height)
80 {
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);
88 }
89
90
91 static void
92 Key(unsigned char key, int x, int y)
93 {
94 (void) x;
95 (void) y;
96 switch (key) {
97 case 27:
98 exit(0);
99 break;
100 }
101 glutPostRedisplay();
102 }
103
104
105
106 static void
107 InitTexture(void)
108 {
109 GLenum filter = GL_LINEAR;
110 GLint imgWidth, imgHeight;
111 GLenum imgFormat;
112 GLubyte *image = NULL;
113 GLfloat *ftex;
114 GLint i, t;
115
116 image = LoadRGBImage(TexFile, &imgWidth, &imgHeight, &imgFormat);
117 if (!image) {
118 printf("Couldn't read %s\n", TexFile);
119 exit(0);
120 }
121
122 assert(imgFormat == GL_RGB);
123
124 ftex = (float *) malloc(imgWidth * imgHeight * 4 * sizeof(float));
125 if (!ftex) {
126 printf("out of memory\n");
127 exit(0);
128 }
129
130 /* convert ubytes to floats, negated */
131 for (i = 0; i < imgWidth * imgHeight * 3; i++) {
132 ftex[i] = -1.0f * image[i];
133 }
134
135 glActiveTexture(GL_TEXTURE0);
136 glBindTexture(GL_TEXTURE_2D, 42);
137
138 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB,
139 imgWidth, imgHeight, 0,
140 GL_RGB, GL_FLOAT, ftex);
141
142
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);
152
153 free(image);
154 free(ftex);
155
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);
160
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]);
176 }
177 }
178 }
179 #endif
180 }
181
182
183 static GLuint
184 CreateProgram(void)
185 {
186 GLuint fragShader, vertShader, program;
187
188 vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText);
189 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText);
190 assert(vertShader);
191 program = LinkShaders(vertShader, fragShader);
192
193 assert(program);
194
195 // InitUniforms(program, Uniforms);
196
197 return program;
198 }
199
200
201 static void
202 Init(void)
203 {
204 glClearColor(0.25, 0.25, 0.25, 0.0);
205
206 GetExtensionFuncs();
207
208 if (!ShadersSupported()) {
209 printf("Sorry, this test requires GLSL\n");
210 exit(1);
211 }
212
213 if (!glutExtensionSupported("GL_MESAX_texture_float")) {
214 printf("Sorry, this test requires GL_MESAX_texture_float\n");
215 exit(1);
216 }
217
218 InitTexture();
219
220 Program = CreateProgram();
221 glUseProgram_func(Program);
222 }
223
224
225 int
226 main(int argc, char *argv[])
227 {
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;
239 }