progs/tests: compile with SCons and glew
[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/glew.h>
12 #include <GL/glut.h>
13 #include "extfuncs.h"
14 #include "readtex.h"
15 #include "shaderutil.h"
16
17
18 static const char *TexFile = "../images/arch.rgb";
19
20 static const char *FragShaderText =
21 "uniform sampler2D tex1; \n"
22 "void main() \n"
23 "{ \n"
24 " vec4 t = texture2D(tex1, gl_TexCoord[0].xy); \n"
25 " // convert from [-255,0] to [0,1] \n"
26 " gl_FragColor = t * (-1.0 / 255.0); \n"
27 "} \n";
28
29 static const char *VertShaderText =
30 "void main() \n"
31 "{ \n"
32 " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
33 " gl_Position = ftransform(); \n"
34 "} \n";
35
36 static struct uniform_info Uniforms[] = {
37 { "tex1", 1, GL_INT, { 0, 0, 0, 0 }, -1 },
38 END_OF_UNIFORMS
39 };
40
41
42 static GLuint Program;
43
44
45
46 static GLboolean
47 CheckError( int line )
48 {
49 GLenum error = glGetError();
50 if (error) {
51 char *err = (char *) gluErrorString( error );
52 fprintf( stderr, "GL Error: %s at line %d\n", err, line );
53 return GL_TRUE;
54 }
55 return GL_FALSE;
56 }
57
58
59 static void
60 Draw(void)
61 {
62 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
63
64 glPushMatrix();
65
66 glBegin(GL_POLYGON);
67 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
68 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
69 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
70 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
71 glEnd();
72
73 glPopMatrix();
74
75 glutSwapBuffers();
76 }
77
78
79 static void
80 Reshape(int width, int height)
81 {
82 glViewport(0, 0, width, height);
83 glMatrixMode(GL_PROJECTION);
84 glLoadIdentity();
85 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
86 glMatrixMode(GL_MODELVIEW);
87 glLoadIdentity();
88 glTranslatef(0.0, 0.0, -8.0);
89 }
90
91
92 static void
93 Key(unsigned char key, int x, int y)
94 {
95 (void) x;
96 (void) y;
97 switch (key) {
98 case 27:
99 exit(0);
100 break;
101 }
102 glutPostRedisplay();
103 }
104
105
106
107 static void
108 InitTexture(void)
109 {
110 GLenum filter = GL_LINEAR;
111 GLint imgWidth, imgHeight;
112 GLenum imgFormat;
113 GLubyte *image = NULL;
114 GLfloat *ftex;
115 GLint i, t;
116
117 image = LoadRGBImage(TexFile, &imgWidth, &imgHeight, &imgFormat);
118 if (!image) {
119 printf("Couldn't read %s\n", TexFile);
120 exit(0);
121 }
122
123 assert(imgFormat == GL_RGB);
124
125 ftex = (float *) malloc(imgWidth * imgHeight * 4 * sizeof(float));
126 if (!ftex) {
127 printf("out of memory\n");
128 exit(0);
129 }
130
131 /* convert ubytes to floats, negated */
132 for (i = 0; i < imgWidth * imgHeight * 3; i++) {
133 ftex[i] = -1.0f * image[i];
134 }
135
136 glActiveTexture(GL_TEXTURE0);
137 glBindTexture(GL_TEXTURE_2D, 42);
138
139 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB,
140 imgWidth, imgHeight, 0,
141 GL_RGB, GL_FLOAT, ftex);
142
143
144 /* sanity checks */
145 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_RED_TYPE_ARB, &t);
146 assert(t == GL_FLOAT);
147 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_TYPE_ARB, &t);
148 assert(t == GL_FLOAT);
149 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_TYPE_ARB, &t);
150 assert(t == GL_FLOAT);
151 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_TYPE_ARB, &t);
152 assert(t == GL_FLOAT);
153
154 free(image);
155 free(ftex);
156
157 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
158 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
159 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
160 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
161
162 #if 0
163 /* read back the texture and make sure values are correct */
164 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, tex2);
165 CheckError(__LINE__);
166 for (i = 0; i < 16; i++) {
167 for (j = 0; j < 16; j++) {
168 if (tex[i][j][0] != tex2[i][j][0] ||
169 tex[i][j][1] != tex2[i][j][1] ||
170 tex[i][j][2] != tex2[i][j][2] ||
171 tex[i][j][3] != tex2[i][j][3]) {
172 printf("tex[%d][%d] %g %g %g %g != tex2[%d][%d] %g %g %g %g\n",
173 i, j,
174 tex[i][j][0], tex[i][j][1], tex[i][j][2], tex[i][j][3],
175 i, j,
176 tex2[i][j][0], tex2[i][j][1], tex2[i][j][2], tex2[i][j][3]);
177 }
178 }
179 }
180 #endif
181 }
182
183
184 static GLuint
185 CreateProgram(void)
186 {
187 GLuint fragShader, vertShader, program;
188
189 vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText);
190 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText);
191 assert(vertShader);
192 program = LinkShaders(vertShader, fragShader);
193
194 assert(program);
195
196 // InitUniforms(program, Uniforms);
197
198 return program;
199 }
200
201
202 static void
203 Init(void)
204 {
205 glClearColor(0.25, 0.25, 0.25, 0.0);
206
207 GetExtensionFuncs();
208
209 if (!ShadersSupported()) {
210 printf("Sorry, this test requires GLSL\n");
211 exit(1);
212 }
213
214 if (!glutExtensionSupported("GL_MESAX_texture_float")) {
215 printf("Sorry, this test requires GL_MESAX_texture_float\n");
216 exit(1);
217 }
218
219 InitTexture();
220
221 Program = CreateProgram();
222 glUseProgram_func(Program);
223 }
224
225
226 int
227 main(int argc, char *argv[])
228 {
229 glutInit(&argc, argv);
230 glutInitWindowPosition(0, 0);
231 glutInitWindowSize(400, 400);
232 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
233 glutCreateWindow(argv[0]);
234 glewInit();
235 glutReshapeFunc(Reshape);
236 glutKeyboardFunc(Key);
237 glutDisplayFunc(Draw);
238 Init();
239 glutMainLoop();
240 return 0;
241 }