2 * Test floating point textures.
3 * No actual rendering, yet.
14 #include "shaderutil.h"
17 static const char *TexFile
= "../images/arch.rgb";
19 static const char *FragShaderText
=
20 "uniform sampler2D tex1; \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"
28 static const char *VertShaderText
=
31 " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
32 " gl_Position = ftransform(); \n"
35 static struct uniform_info Uniforms
[] = {
36 { "tex1", 1, GL_INT
, { 0, 0, 0, 0 }, -1 },
41 static GLuint Program
;
46 CheckError( int line
)
48 GLenum error
= glGetError();
50 char *err
= (char *) gluErrorString( error
);
51 fprintf( stderr
, "GL Error: %s at line %d\n", err
, line
);
61 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
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 );
79 Reshape(int width
, int height
)
81 glViewport(0, 0, width
, height
);
82 glMatrixMode(GL_PROJECTION
);
84 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
85 glMatrixMode(GL_MODELVIEW
);
87 glTranslatef(0.0, 0.0, -8.0);
92 Key(unsigned char key
, int x
, int y
)
109 GLenum filter
= GL_LINEAR
;
110 GLint imgWidth
, imgHeight
;
112 GLubyte
*image
= NULL
;
116 image
= LoadRGBImage(TexFile
, &imgWidth
, &imgHeight
, &imgFormat
);
118 printf("Couldn't read %s\n", TexFile
);
122 assert(imgFormat
== GL_RGB
);
124 ftex
= (float *) malloc(imgWidth
* imgHeight
* 4 * sizeof(float));
126 printf("out of memory\n");
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
);
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
);
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
);
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",
173 tex
[i
][j
][0], tex
[i
][j
][1], tex
[i
][j
][2], tex
[i
][j
][3],
175 tex2
[i
][j
][0], tex2
[i
][j
][1], tex2
[i
][j
][2], tex2
[i
][j
][3]);
186 GLuint fragShader
, vertShader
, program
;
188 vertShader
= CompileShaderText(GL_VERTEX_SHADER
, VertShaderText
);
189 fragShader
= CompileShaderText(GL_FRAGMENT_SHADER
, FragShaderText
);
191 program
= LinkShaders(vertShader
, fragShader
);
195 // InitUniforms(program, Uniforms);
204 glClearColor(0.25, 0.25, 0.25, 0.0);
208 if (!ShadersSupported()) {
209 printf("Sorry, this test requires GLSL\n");
213 if (!glutExtensionSupported("GL_MESAX_texture_float")) {
214 printf("Sorry, this test requires GL_MESAX_texture_float\n");
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
);