Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

texcompress2.c 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Test texture compression.
  3. */
  4. #define GL_GLEXT_PROTOTYPES
  5. #include <assert.h>
  6. #include <stdio.h>
  7. #include <GL/glut.h>
  8. #include <GL/glx.h>
  9. #include "readtex.c"
  10. #define IMAGE_FILE "../images/arch.rgb"
  11. static int ImgWidth, ImgHeight;
  12. static GLenum ImgFormat;
  13. static GLenum CompFormat;
  14. static GLfloat EyeDist = 5.0;
  15. static GLfloat Rot = 0.0;
  16. const GLenum Target = GL_TEXTURE_2D;
  17. static void
  18. CheckError(int line)
  19. {
  20. GLenum err = glGetError();
  21. if (err) {
  22. printf("GL Error %d at line %d\n", (int) err, line);
  23. }
  24. }
  25. static const char *
  26. LookupFormat(GLenum format)
  27. {
  28. switch (format) {
  29. case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
  30. return "GL_COMPRESSED_RGB_S3TC_DXT1_EXT";
  31. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  32. return "GL_COMPRESSED_RGBA_S3TC_DXT1_EXT";
  33. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  34. return "GL_COMPRESSED_RGBA_S3TC_DXT3_EXT";
  35. default:
  36. return "other";
  37. }
  38. }
  39. static void
  40. TestSubTex(void)
  41. {
  42. GLboolean all = 0*GL_TRUE;
  43. GLubyte *buffer;
  44. GLint size, fmt;
  45. int i;
  46. glGetTexLevelParameteriv(Target, 0,
  47. GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB, &size);
  48. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_INTERNAL_FORMAT, &fmt);
  49. buffer = (GLubyte *) malloc(size);
  50. glGetCompressedTexImageARB(Target, 0, buffer);
  51. printf("Testing sub-texture replacement\n");
  52. if (all)
  53. glCompressedTexImage2DARB(Target, 0,
  54. fmt, ImgWidth, ImgHeight, 0,
  55. size, buffer);
  56. else {
  57. /* bottom half */
  58. glCompressedTexSubImage2DARB(Target, 0,
  59. 0, 0, /* pos */
  60. ImgWidth, ImgHeight / 2,
  61. fmt, size/2, buffer);
  62. /* top half */
  63. glCompressedTexSubImage2DARB(Target, 0,
  64. 0, ImgHeight / 2, /* pos */
  65. ImgWidth, ImgHeight / 2,
  66. fmt, size/2, buffer + size / 2);
  67. }
  68. free(buffer);
  69. }
  70. static void
  71. LoadCompressedImage(const char *file)
  72. {
  73. const GLenum filter = GL_LINEAR;
  74. GLubyte *image;
  75. GLint p;
  76. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  77. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  78. /*
  79. * Load image and scale if needed.
  80. */
  81. image = LoadRGBImage( file, &ImgWidth, &ImgHeight, &ImgFormat );
  82. if (!image) {
  83. printf("Couldn't read %s\n", IMAGE_FILE);
  84. exit(0);
  85. }
  86. printf("Image is %d x %d\n", ImgWidth, ImgHeight);
  87. /* power of two */
  88. assert(ImgWidth == 128 || ImgWidth == 256 || ImgWidth == 512);
  89. assert(ImgWidth == 128 || ImgHeight == 256 || ImgHeight == 512);
  90. if (ImgFormat == GL_RGB)
  91. CompFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
  92. else
  93. CompFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  94. if (ImgFormat == GL_RGBA) {
  95. int i, numAlpha = 0;
  96. for (i = 0; i < ImgWidth * ImgHeight; i++) {
  97. if (image[i*4+3] != 0 && image[i*4+3] != 0xff) {
  98. numAlpha++;
  99. }
  100. if (image[i*4+3] == 0)
  101. image[i*4+3] = 4 * i / ImgWidth;
  102. }
  103. printf("Num Alpha !=0,255: %d\n", numAlpha);
  104. }
  105. CompFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  106. /*
  107. * Give image to OpenGL and have it compress it.
  108. */
  109. glTexImage2D(Target, 0, CompFormat, ImgWidth, ImgHeight, 0,
  110. ImgFormat, GL_UNSIGNED_BYTE, image);
  111. CheckError(__LINE__);
  112. free(image);
  113. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_INTERNAL_FORMAT, &p);
  114. printf("Compressed Internal Format: %s (0x%x)\n", LookupFormat(p), p);
  115. assert(p == CompFormat);
  116. printf("Original size: %d bytes\n", ImgWidth * ImgHeight * 3);
  117. glGetTexLevelParameteriv(Target, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB, &p);
  118. printf("Compressed size: %d bytes\n", p);
  119. glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, filter);
  120. glTexParameteri(Target, GL_TEXTURE_MAG_FILTER, filter);
  121. TestSubTex();
  122. }
  123. static void
  124. Init(const char *file)
  125. {
  126. GLint numFormats, formats[100];
  127. GLint p;
  128. if (!glutExtensionSupported("GL_ARB_texture_compression")) {
  129. printf("Sorry, GL_ARB_texture_compression is required.\n");
  130. exit(1);
  131. }
  132. if (!glutExtensionSupported("GL_EXT_texture_compression_s3tc")) {
  133. printf("Sorry, GL_EXT_texture_compression_s3tc is required.\n");
  134. exit(1);
  135. }
  136. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  137. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  138. glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB, &numFormats);
  139. glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS_ARB, formats);
  140. printf("%d supported compression formats: ", numFormats);
  141. for (p = 0; p < numFormats; p++)
  142. printf("0x%x ", formats[p]);
  143. printf("\n");
  144. LoadCompressedImage(file);
  145. glEnable(GL_TEXTURE_2D);
  146. if (ImgFormat == GL_RGBA) {
  147. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  148. glEnable(GL_BLEND);
  149. }
  150. }
  151. static void
  152. Reshape( int width, int height )
  153. {
  154. glViewport( 0, 0, width, height );
  155. glMatrixMode( GL_PROJECTION );
  156. glLoadIdentity();
  157. glFrustum(-1, 1, -1, 1, 4, 100);
  158. glMatrixMode( GL_MODELVIEW );
  159. glLoadIdentity();
  160. }
  161. static void
  162. Key( unsigned char key, int x, int y )
  163. {
  164. (void) x;
  165. (void) y;
  166. switch (key) {
  167. case 'd':
  168. EyeDist -= 1.0;
  169. if (EyeDist < 4.0)
  170. EyeDist = 4.0;
  171. break;
  172. case 'D':
  173. EyeDist += 1.0;
  174. break;
  175. case 'z':
  176. Rot += 5.0;
  177. break;
  178. case 'Z':
  179. Rot -= 5.0;
  180. break;
  181. case 27:
  182. exit(0);
  183. break;
  184. }
  185. glutPostRedisplay();
  186. }
  187. static void
  188. Draw( void )
  189. {
  190. glClearColor(0.3, 0.3, .8, 0);
  191. glClear(GL_COLOR_BUFFER_BIT);
  192. glPushMatrix();
  193. glTranslatef(0, 0, -(EyeDist+0.01));
  194. glRotatef(Rot, 0, 0, 1);
  195. glBegin(GL_POLYGON);
  196. glTexCoord2f(0, 0); glVertex2f(-1, -1);
  197. glTexCoord2f(1, 0); glVertex2f( 1, -1);
  198. glTexCoord2f(1, 1); glVertex2f( 1, 1);
  199. glTexCoord2f(0, 1); glVertex2f(-1, 1);
  200. glEnd();
  201. glPopMatrix();
  202. glutSwapBuffers();
  203. }
  204. int
  205. main( int argc, char *argv[] )
  206. {
  207. glutInit( &argc, argv );
  208. glutInitWindowSize( 600, 600 );
  209. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE);
  210. glutCreateWindow(argv[0]);
  211. glutReshapeFunc( Reshape );
  212. glutKeyboardFunc( Key );
  213. glutDisplayFunc( Draw );
  214. if (argc > 1)
  215. Init(argv[1]);
  216. else
  217. Init(IMAGE_FILE);
  218. glutMainLoop();
  219. return 0;
  220. }