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.

mipmap_view.c 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Test mipmap generation and lod bias.
  3. *
  4. * Brian Paul
  5. * 17 March 2008
  6. */
  7. #include <assert.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <math.h>
  11. #include <GL/glut.h>
  12. #include <GL/glext.h>
  13. #include "readtex.h"
  14. #define TEXTURE_FILE "../images/arch.rgb"
  15. static int TexWidth = 256, TexHeight = 256;
  16. static int WinWidth = 1044, WinHeight = 900;
  17. static GLfloat Bias = 0.0;
  18. static GLboolean ScaleQuads = GL_FALSE;
  19. static void
  20. PrintString(const char *s)
  21. {
  22. while (*s) {
  23. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  24. s++;
  25. }
  26. }
  27. static void
  28. Display(void)
  29. {
  30. int x, y, bias;
  31. char str[100];
  32. int texWidth = TexWidth, texHeight = TexHeight;
  33. glClear(GL_COLOR_BUFFER_BIT);
  34. glMatrixMode(GL_PROJECTION);
  35. glLoadIdentity();
  36. glOrtho(0, WinWidth, 0, WinHeight, -1, 1);
  37. glMatrixMode(GL_MODELVIEW);
  38. glLoadIdentity();
  39. glColor3f(1,1,1);
  40. y = WinHeight - 300;
  41. x = 4;
  42. for (bias = -1; bias < 11; bias++) {
  43. if (ScaleQuads) {
  44. if (bias > 0) {
  45. if (texWidth == 1 && texHeight == 1)
  46. break;
  47. texWidth = TexWidth >> bias;
  48. texHeight = TexHeight >> bias;
  49. if (texWidth < 1)
  50. texWidth = 1;
  51. if (texHeight < 1)
  52. texHeight = 1;
  53. }
  54. glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.0);
  55. }
  56. else {
  57. glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, bias);
  58. }
  59. glRasterPos2f(x, y + TexHeight + 5);
  60. if (ScaleQuads)
  61. sprintf(str, "Texture Level %d: %d x %d",
  62. (bias < 0 ? 0 : bias),
  63. texWidth, texHeight);
  64. else
  65. sprintf(str, "Texture LOD Bias = %d", bias);
  66. PrintString(str);
  67. glPushMatrix();
  68. glTranslatef(x, y, 0);
  69. glEnable(GL_TEXTURE_2D);
  70. glBegin(GL_POLYGON);
  71. glTexCoord2f(0, 0); glVertex2f(0, 0);
  72. glTexCoord2f(1, 0); glVertex2f(texWidth, 0);
  73. glTexCoord2f(1, 1); glVertex2f(texWidth, texHeight);
  74. glTexCoord2f(0, 1); glVertex2f(0, texHeight);
  75. glEnd();
  76. glPopMatrix();
  77. glDisable(GL_TEXTURE_2D);
  78. x += TexWidth + 4;
  79. if (x >= WinWidth) {
  80. x = 4;
  81. y -= 300;
  82. }
  83. }
  84. glutSwapBuffers();
  85. }
  86. static void
  87. Reshape(int width, int height)
  88. {
  89. WinWidth = width;
  90. WinHeight = height;
  91. glViewport(0, 0, width, height);
  92. }
  93. static void
  94. Key(unsigned char key, int x, int y)
  95. {
  96. (void) x;
  97. (void) y;
  98. switch (key) {
  99. case 'b':
  100. Bias -= 10;
  101. break;
  102. case 'B':
  103. Bias += 10;
  104. break;
  105. case '0':
  106. case '1':
  107. case '2':
  108. case '3':
  109. case '4':
  110. case '5':
  111. case '6':
  112. case '7':
  113. case '8':
  114. case '9':
  115. Bias = 100.0 * (key - '0');
  116. break;
  117. case 's':
  118. ScaleQuads = !ScaleQuads;
  119. break;
  120. case 27:
  121. exit(0);
  122. break;
  123. }
  124. glutPostRedisplay();
  125. }
  126. static void
  127. Init(void)
  128. {
  129. GLfloat maxBias;
  130. if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
  131. printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
  132. exit(1);
  133. }
  134. if (!glutExtensionSupported("GL_SGIS_generate_mipmap")) {
  135. printf("Sorry, GL_SGIS_generate_mipmap not supported by this renderer.\n");
  136. exit(1);
  137. }
  138. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  139. if (1) {
  140. /* test auto mipmap generation */
  141. GLint width, height, i;
  142. GLenum format;
  143. GLubyte *image = LoadRGBImage(TEXTURE_FILE, &width, &height, &format);
  144. if (!image) {
  145. printf("Error: could not load texture image %s\n", TEXTURE_FILE);
  146. exit(1);
  147. }
  148. /* resize to TexWidth x TexHeight */
  149. if (width != TexWidth || height != TexHeight) {
  150. GLubyte *newImage = malloc(TexWidth * TexHeight * 4);
  151. gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
  152. TexWidth, TexHeight, GL_UNSIGNED_BYTE, newImage);
  153. free(image);
  154. image = newImage;
  155. }
  156. printf("Using GL_SGIS_generate_mipmap\n");
  157. glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
  158. glTexImage2D(GL_TEXTURE_2D, 0, format, TexWidth, TexHeight, 0,
  159. format, GL_UNSIGNED_BYTE, image);
  160. free(image);
  161. /* make sure mipmap was really generated correctly */
  162. width = TexWidth;
  163. height = TexHeight;
  164. for (i = 0; i < 9; i++) {
  165. GLint w, h;
  166. glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w);
  167. glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h);
  168. printf("Level %d size: %d x %d\n", i, w, h);
  169. assert(w == width);
  170. assert(h == height);
  171. width /= 2;
  172. height /= 2;
  173. }
  174. }
  175. else {
  176. if (LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
  177. printf("Using gluBuildMipmaps()\n");
  178. }
  179. else {
  180. printf("Error: could not load texture image %s\n", TEXTURE_FILE);
  181. exit(1);
  182. }
  183. }
  184. /* mipmapping required for this extension */
  185. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  186. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  187. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  188. glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT, &maxBias);
  189. printf("GL_RENDERER: %s\n", (char*) glGetString(GL_RENDERER));
  190. printf("LOD bias range: [%g, %g]\n", -maxBias, maxBias);
  191. printf("Press 's' to toggle quad scaling\n");
  192. }
  193. int
  194. main(int argc, char *argv[])
  195. {
  196. glutInit(&argc, argv);
  197. glutInitWindowPosition(0, 0);
  198. glutInitWindowSize(WinWidth, WinHeight);
  199. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  200. glutCreateWindow(argv[0]);
  201. glutReshapeFunc(Reshape);
  202. glutKeyboardFunc(Key);
  203. glutDisplayFunc(Display);
  204. Init();
  205. glutMainLoop();
  206. return 0;
  207. }