2 * Test floating point textures.
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
)
108 GLenum filter
= GL_LINEAR
;
109 GLint imgWidth
, imgHeight
;
111 GLubyte
*image
= NULL
;
115 image
= LoadRGBImage(TexFile
, &imgWidth
, &imgHeight
, &imgFormat
);
117 printf("Couldn't read %s\n", TexFile
);
121 assert(imgFormat
== GL_RGB
);
123 ftex
= (float *) malloc(imgWidth
* imgHeight
* 4 * sizeof(float));
125 printf("out of memory\n");
129 /* convert ubytes to floats, negated */
130 for (i
= 0; i
< imgWidth
* imgHeight
* 3; i
++) {
131 ftex
[i
] = -1.0f
* image
[i
];
134 glActiveTexture(GL_TEXTURE0
);
135 glBindTexture(GL_TEXTURE_2D
, 42);
137 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA32F_ARB
,
138 imgWidth
, imgHeight
, 0,
139 GL_RGB
, GL_FLOAT
, ftex
);
142 CheckError(__LINE__
);
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
);
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
);
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
]);
181 GLuint fragShader
, vertShader
, program
;
183 vertShader
= CompileShaderText(GL_VERTEX_SHADER
, VertShaderText
);
184 fragShader
= CompileShaderText(GL_FRAGMENT_SHADER
, FragShaderText
);
186 program
= LinkShaders(vertShader
, fragShader
);
190 glUseProgram_func(program
);
192 SetUniformValues(program
, Uniforms
);
201 glClearColor(0.25, 0.25, 0.25, 0.0);
205 if (!ShadersSupported()) {
206 printf("Sorry, this test requires GLSL\n");
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");
218 Program
= CreateProgram();
219 glUseProgram_func(Program
);
224 main(int argc
, char *argv
[])
226 glutInit(&argc
, argv
);
227 glutInitWindowPosition(0, 0);
228 glutInitWindowSize(400, 400);
229 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
230 glutCreateWindow(argv
[0]);
232 glutReshapeFunc(Reshape
);
233 glutKeyboardFunc(Key
);
234 glutDisplayFunc(Draw
);