Clone of mesa.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

minmag.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Test minification vs. magnification filtering.
  3. * Draw two quads with different filtering modes:
  4. *
  5. * +--------------------------+ +--------------------------+
  6. * | MagFilter = GL_LINEAR | | MagFilter = GL_LINEAR |
  7. * | MinFilter = GL_LINEAR | | MinFilter = GL_NEAREST |
  8. * +--------------------------+ +--------------------------+
  9. *
  10. * They should look different when the quad is smaller than the level 0
  11. * texture size (when minifying).
  12. */
  13. #include <assert.h>
  14. #include <math.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <GL/glew.h>
  19. #include <GL/glut.h>
  20. static GLint Width = 1000, Height = 500;
  21. static GLint TexWidth = 256, TexHeight = 256;
  22. static GLfloat Zpos = 5;
  23. static GLboolean MipMap = 0*GL_TRUE;
  24. static GLboolean LinearFilter = GL_TRUE;
  25. static void
  26. redraw(void)
  27. {
  28. GLfloat w = 1.0;
  29. GLfloat h = 1.0;
  30. glClear( GL_COLOR_BUFFER_BIT );
  31. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  32. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  33. glPushMatrix();
  34. glTranslatef(-1.5, 0, -Zpos);
  35. glBegin(GL_POLYGON);
  36. glTexCoord2f(0, 0); glVertex2f(-w, -h);
  37. glTexCoord2f(1, 0); glVertex2f( w, -h);
  38. glTexCoord2f(1, 1); glVertex2f( w, h);
  39. glTexCoord2f(0, 1); glVertex2f(-w, h);
  40. glEnd();
  41. glPopMatrix();
  42. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  43. glPushMatrix();
  44. glTranslatef(1.5, 0, -Zpos);
  45. glBegin(GL_POLYGON);
  46. glTexCoord2f(0, 0); glVertex2f(-w, -h);
  47. glTexCoord2f(1, 0); glVertex2f( w, -h);
  48. glTexCoord2f(1, 1); glVertex2f( w, h);
  49. glTexCoord2f(0, 1); glVertex2f(-w, h);
  50. glEnd();
  51. glPopMatrix();
  52. glutSwapBuffers();
  53. }
  54. static void
  55. init(void)
  56. {
  57. GLubyte color[10][4] = {
  58. { 0, 0, 0, 0 },
  59. { 1, 0, 0, 0 },
  60. { 0, 1, 0, 0 },
  61. { 0, 0, 1, 0 },
  62. { 0, 1, 1, 0 },
  63. { 1, 0, 1, 0 },
  64. { 1, 1, 0, 0 },
  65. { 1, 0, 0, 0 },
  66. { 0, 1, 0, 0 },
  67. { 0, 0, 1, 0 }
  68. };
  69. GLubyte *texImage;
  70. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  71. printf("Left quad should be linear filtered and right should be nearest filtered.\n");
  72. printf("Press z/Z to change quad distance.\n");
  73. texImage = (GLubyte*) malloc(4 * TexWidth * TexHeight * sizeof(GLubyte));
  74. assert(texImage);
  75. {
  76. GLint level = 0;
  77. GLint w = TexWidth, h = TexHeight;
  78. while (1) {
  79. int i, j;
  80. for (i = 0; i < h; i++) {
  81. for (j = 0;j < w; j++) {
  82. if (w==1 || h==1 || (((i / 2) ^ (j / 2)) & 1)) {
  83. /*if (j < i) {*/
  84. texImage[(i*w+j) * 4 + 0] = 255;
  85. texImage[(i*w+j) * 4 + 1] = 255;
  86. texImage[(i*w+j) * 4 + 2] = 255;
  87. texImage[(i*w+j) * 4 + 3] = 255;
  88. }
  89. else {
  90. texImage[(i*w+j) * 4 + 0] = color[level][0] * 255;
  91. texImage[(i*w+j) * 4 + 1] = color[level][1] * 255;
  92. texImage[(i*w+j) * 4 + 2] = color[level][2] * 255;
  93. texImage[(i*w+j) * 4 + 3] = color[level][3] * 255;
  94. }
  95. }
  96. }
  97. glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0,
  98. GL_RGBA, GL_UNSIGNED_BYTE, texImage);
  99. printf("Texture level %d: %d x %d\n", level, w, h);
  100. if (!MipMap)
  101. break;
  102. if (w == 1 && h == 1)
  103. break;
  104. if (w > 1)
  105. w /= 2;
  106. if (h > 1)
  107. h /= 2;
  108. level++;
  109. }
  110. }
  111. free(texImage);
  112. glClearColor(0.25, 0.25, 0.25, 1.0);
  113. glEnable(GL_TEXTURE_2D);
  114. glViewport(0, 0, Width, Height);
  115. }
  116. static void
  117. Reshape(int width, int height)
  118. {
  119. float ar = (float) width /height;
  120. Width = width;
  121. Height = height;
  122. glViewport(0, 0, width, height);
  123. glMatrixMode(GL_PROJECTION);
  124. glLoadIdentity();
  125. glFrustum(-ar, ar, -1.0, 1.0, 5.0, 2500.0);
  126. glMatrixMode(GL_MODELVIEW);
  127. glLoadIdentity();
  128. glTranslatef(0.0, 0.0, -15.0);
  129. }
  130. static void
  131. Key(unsigned char key, int x, int y)
  132. {
  133. (void) x;
  134. (void) y;
  135. switch (key) {
  136. case 'z':
  137. Zpos--;
  138. break;
  139. case 'Z':
  140. Zpos++;
  141. break;
  142. case 'f':
  143. LinearFilter = !LinearFilter;
  144. break;
  145. case 27:
  146. exit(0);
  147. break;
  148. }
  149. glutPostRedisplay();
  150. }
  151. int
  152. main(int argc, char *argv[])
  153. {
  154. glutInit(&argc, argv);
  155. glutInitWindowPosition(0, 0);
  156. glutInitWindowSize(Width, Height);
  157. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  158. glutCreateWindow(argv[0]);
  159. glewInit();
  160. glutReshapeFunc(Reshape);
  161. glutKeyboardFunc(Key);
  162. glutDisplayFunc(redraw);
  163. init();
  164. glutMainLoop();
  165. return 0;
  166. }