Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

floattex.c 5.2KB

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