2 * Test floating point textures.
3 * No actual rendering, yet.
15 #include "shaderutil.h"
18 static const char *TexFile
= "../images/arch.rgb";
20 static const char *FragShaderText
=
21 "uniform sampler2D tex1; \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"
29 static const char *VertShaderText
=
32 " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
33 " gl_Position = ftransform(); \n"
36 static struct uniform_info Uniforms
[] = {
37 { "tex1", 1, GL_INT
, { 0, 0, 0, 0 }, -1 },
42 static GLuint Program
;
47 CheckError( int line
)
49 GLenum error
= glGetError();
51 char *err
= (char *) gluErrorString( error
);
52 fprintf( stderr
, "GL Error: %s at line %d\n", err
, line
);
62 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
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 );
80 Reshape(int width
, int height
)
82 glViewport(0, 0, width
, height
);
83 glMatrixMode(GL_PROJECTION
);
85 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
86 glMatrixMode(GL_MODELVIEW
);
88 glTranslatef(0.0, 0.0, -8.0);
93 Key(unsigned char key
, int x
, int y
)
110 GLenum filter
= GL_LINEAR
;
111 GLint imgWidth
, imgHeight
;
113 GLubyte
*image
= NULL
;
117 image
= LoadRGBImage(TexFile
, &imgWidth
, &imgHeight
, &imgFormat
);
119 printf("Couldn't read %s\n", TexFile
);
123 assert(imgFormat
== GL_RGB
);
125 ftex
= (float *) malloc(imgWidth
* imgHeight
* 4 * sizeof(float));
127 printf("out of memory\n");
131 /* convert ubytes to floats, negated */
132 for (i
= 0; i
< imgWidth
* imgHeight
* 3; i
++) {
133 ftex
[i
] = -1.0f
* image
[i
];
136 glActiveTexture(GL_TEXTURE0
);
137 glBindTexture(GL_TEXTURE_2D
, 42);
139 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA32F_ARB
,
140 imgWidth
, imgHeight
, 0,
141 GL_RGB
, GL_FLOAT
, ftex
);
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
);
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
);
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",
174 tex
[i
][j
][0], tex
[i
][j
][1], tex
[i
][j
][2], tex
[i
][j
][3],
176 tex2
[i
][j
][0], tex2
[i
][j
][1], tex2
[i
][j
][2], tex2
[i
][j
][3]);
187 GLuint fragShader
, vertShader
, program
;
189 vertShader
= CompileShaderText(GL_VERTEX_SHADER
, VertShaderText
);
190 fragShader
= CompileShaderText(GL_FRAGMENT_SHADER
, FragShaderText
);
192 program
= LinkShaders(vertShader
, fragShader
);
196 // InitUniforms(program, Uniforms);
205 glClearColor(0.25, 0.25, 0.25, 0.0);
209 if (!ShadersSupported()) {
210 printf("Sorry, this test requires GLSL\n");
214 if (!glutExtensionSupported("GL_MESAX_texture_float")) {
215 printf("Sorry, this test requires GL_MESAX_texture_float\n");
221 Program
= CreateProgram();
222 glUseProgram_func(Program
);
227 main(int argc
, char *argv
[])
229 glutInit(&argc
, argv
);
230 glutInitWindowPosition(0, 0);
231 glutInitWindowSize(400, 400);
232 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
233 glutCreateWindow(argv
[0]);
235 glutReshapeFunc(Reshape
);
236 glutKeyboardFunc(Key
);
237 glutDisplayFunc(Draw
);