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.

arbnpot.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Test NPOT textures with the GL_ARB_texture_non_power_of_two extension.
  3. * Brian Paul
  4. * 2 July 2003
  5. */
  6. #include <assert.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include <GL/glut.h>
  11. #include "../util/readtex.c"
  12. #define IMAGE_FILE "../images/girl.rgb"
  13. static GLfloat Zrot = 0;
  14. static void Display( void )
  15. {
  16. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  17. glPushMatrix();
  18. glRotatef(Zrot, 0, 0, 1);
  19. glBegin(GL_POLYGON);
  20. glTexCoord2f(0, 0);
  21. glVertex2f(-1, -1);
  22. glTexCoord2f(1, 0);
  23. glVertex2f(1, -1);
  24. glTexCoord2f(1, 1);
  25. glVertex2f(1, 1);
  26. glTexCoord2f(0, 1);
  27. glVertex2f(-1, 1);
  28. glEnd();
  29. glPopMatrix();
  30. glutSwapBuffers();
  31. }
  32. static void Reshape( int width, int height )
  33. {
  34. glViewport( 0, 0, width, height );
  35. glMatrixMode( GL_PROJECTION );
  36. glLoadIdentity();
  37. glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
  38. glMatrixMode( GL_MODELVIEW );
  39. glLoadIdentity();
  40. glTranslatef( 0.0, 0.0, -7.0 );
  41. }
  42. static void Key( unsigned char key, int x, int y )
  43. {
  44. (void) x;
  45. (void) y;
  46. switch (key) {
  47. case 'z':
  48. Zrot -= 1.0;
  49. break;
  50. case 'Z':
  51. Zrot += 1.0;
  52. break;
  53. case 27:
  54. exit(0);
  55. break;
  56. }
  57. glutPostRedisplay();
  58. }
  59. static void Init( void )
  60. {
  61. GLubyte *image;
  62. int imgWidth, imgHeight, minDim, w;
  63. GLenum imgFormat;
  64. if (!glutExtensionSupported("GL_ARB_texture_non_power_of_two")) {
  65. printf("Sorry, this program requires GL_ARB_texture_non_power_of_two\n");
  66. exit(1);
  67. }
  68. #if 1
  69. image = LoadRGBImage( IMAGE_FILE, &imgWidth, &imgHeight, &imgFormat );
  70. if (!image) {
  71. printf("Couldn't read %s\n", IMAGE_FILE);
  72. exit(0);
  73. }
  74. #else
  75. int i, j;
  76. imgFormat = GL_RGB;
  77. imgWidth = 3;
  78. imgHeight = 3;
  79. image = malloc(imgWidth * imgHeight * 3);
  80. for (i = 0; i < imgHeight; i++) {
  81. for (j = 0; j < imgWidth; j++) {
  82. int k = (i * imgWidth + j) * 3;
  83. if ((i + j) & 1) {
  84. image[k+0] = 255;
  85. image[k+1] = 0;
  86. image[k+2] = 0;
  87. }
  88. else {
  89. image[k+0] = 0;
  90. image[k+1] = 255;
  91. image[k+2] = 0;
  92. }
  93. }
  94. }
  95. #endif
  96. printf("Read %d x %d\n", imgWidth, imgHeight);
  97. minDim = imgWidth < imgHeight ? imgWidth : imgHeight;
  98. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  99. /*
  100. * 1D Texture. Test proxy first, if that works, test non-proxy target.
  101. */
  102. glTexImage1D(GL_PROXY_TEXTURE_1D, 0, GL_RGB, imgWidth, 0,
  103. imgFormat, GL_UNSIGNED_BYTE, image);
  104. glGetTexLevelParameteriv(GL_PROXY_TEXTURE_1D, 0, GL_TEXTURE_WIDTH, &w);
  105. assert(w == imgWidth || w == 0);
  106. if (w) {
  107. glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, imgWidth, 0,
  108. imgFormat, GL_UNSIGNED_BYTE, image);
  109. assert(glGetError() == GL_NO_ERROR);
  110. }
  111. /*
  112. * 2D Texture
  113. */
  114. glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_RGB, imgWidth, imgHeight, 0,
  115. imgFormat, GL_UNSIGNED_BYTE, image);
  116. glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
  117. assert(w == imgWidth || w == 0);
  118. if (w) {
  119. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imgWidth, imgHeight, 0,
  120. imgFormat, GL_UNSIGNED_BYTE, image);
  121. assert(glGetError() == GL_NO_ERROR);
  122. }
  123. /*
  124. * 3D Texture
  125. */
  126. glTexImage3D(GL_PROXY_TEXTURE_3D, 0, GL_RGB, imgWidth, imgHeight, 1, 0,
  127. imgFormat, GL_UNSIGNED_BYTE, image);
  128. glGetTexLevelParameteriv(GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_WIDTH, &w);
  129. assert(w == imgWidth || w == 0);
  130. if (w) {
  131. glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, imgWidth, imgHeight, 1, 0,
  132. imgFormat, GL_UNSIGNED_BYTE, image);
  133. assert(glGetError() == GL_NO_ERROR);
  134. }
  135. /*
  136. * Cube Texture
  137. */
  138. glTexImage2D(GL_PROXY_TEXTURE_CUBE_MAP, 0, GL_RGB,
  139. minDim, minDim, 0,
  140. imgFormat, GL_UNSIGNED_BYTE, image);
  141. glGetTexLevelParameteriv(GL_PROXY_TEXTURE_CUBE_MAP, 0, GL_TEXTURE_WIDTH, &w);
  142. assert(w == minDim || w == 0);
  143. if (w) {
  144. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB,
  145. minDim, minDim, 0,
  146. imgFormat, GL_UNSIGNED_BYTE, image);
  147. assert(glGetError() == GL_NO_ERROR);
  148. }
  149. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  150. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  151. glEnable(GL_TEXTURE_2D);
  152. }
  153. int main( int argc, char *argv[] )
  154. {
  155. glutInit( &argc, argv );
  156. glutInitWindowPosition( 0, 0 );
  157. glutInitWindowSize( 400, 400 );
  158. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  159. glutCreateWindow(argv[0]);
  160. glutReshapeFunc( Reshape );
  161. glutKeyboardFunc( Key );
  162. glutDisplayFunc( Display );
  163. Init();
  164. glutMainLoop();
  165. return 0;
  166. }