progs/egl: Port drawtex and torus to eglut.
[mesa.git] / progs / tests / floattex.c
1 /*
2 * Test floating point textures.
3 */
4
5
6 #include <assert.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <math.h>
10 #include <GL/glew.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_SAMPLER_2D, { 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 static void
106 InitTexture(void)
107 {
108 GLenum filter = GL_LINEAR;
109 GLint imgWidth, imgHeight;
110 GLenum imgFormat;
111 GLubyte *image = NULL;
112 GLfloat *ftex;
113 GLint i, t;
114
115 image = LoadRGBImage(TexFile, &imgWidth, &imgHeight, &imgFormat);
116 if (!image) {
117 printf("Couldn't read %s\n", TexFile);
118 exit(0);
119 }
120
121 assert(imgFormat == GL_RGB);
122
123 ftex = (float *) malloc(imgWidth * imgHeight * 4 * sizeof(float));
124 if (!ftex) {
125 printf("out of memory\n");
126 exit(0);
127 }
128
129 /* convert ubytes to floats, negated */
130 for (i = 0; i < imgWidth * imgHeight * 3; i++) {
131 ftex[i] = -1.0f * image[i];
132 }
133
134 glActiveTexture(GL_TEXTURE0);
135 glBindTexture(GL_TEXTURE_2D, 42);
136
137 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB,
138 imgWidth, imgHeight, 0,
139 GL_RGB, GL_FLOAT, ftex);
140
141
142 CheckError(__LINE__);
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 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
156 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
157 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
158 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
159
160 if (1) {
161 /* read back the texture and make sure values are correct */
162 GLfloat *tex2 = (GLfloat *)
163 malloc(imgWidth * imgHeight * 4 * sizeof(GLfloat));
164 glGetTexImage(GL_TEXTURE_2D, 0, imgFormat, GL_FLOAT, tex2);
165 CheckError(__LINE__);
166 for (i = 0; i < imgWidth * imgHeight * 4; i++) {
167 if (ftex[i] != tex2[i]) {
168 printf("tex[%d] %g != tex2[%d] %g\n",
169 i, ftex[i], i, tex2[i]);
170 }
171 }
172 }
173
174 free(ftex);
175 }
176
177
178 static GLuint
179 CreateProgram(void)
180 {
181 GLuint fragShader, vertShader, program;
182
183 vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText);
184 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText);
185 assert(vertShader);
186 program = LinkShaders(vertShader, fragShader);
187
188 assert(program);
189
190 glUseProgram_func(program);
191
192 SetUniformValues(program, Uniforms);
193
194 return program;
195 }
196
197
198 static void
199 Init(void)
200 {
201 glClearColor(0.25, 0.25, 0.25, 0.0);
202
203 GetExtensionFuncs();
204
205 if (!ShadersSupported()) {
206 printf("Sorry, this test requires GLSL\n");
207 exit(1);
208 }
209
210 if (!glutExtensionSupported("GL_MESAX_texture_float") &&
211 !glutExtensionSupported("GL_ARB_texture_float")) {
212 printf("Sorry, this test requires GL_MESAX/ARB_texture_float\n");
213 exit(1);
214 }
215
216 InitTexture();
217
218 Program = CreateProgram();
219 glUseProgram_func(Program);
220 }
221
222
223 int
224 main(int argc, char *argv[])
225 {
226 glutInit(&argc, argv);
227 glutInitWindowPosition(0, 0);
228 glutInitWindowSize(400, 400);
229 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
230 glutCreateWindow(argv[0]);
231 glewInit();
232 glutReshapeFunc(Reshape);
233 glutKeyboardFunc(Key);
234 glutDisplayFunc(Draw);
235 Init();
236 glutMainLoop();
237 return 0;
238 }