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.

lodbias.c 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * GL_EXT_texture_lod_bias demo
  3. *
  4. * Thanks to Michael Vance for implementing this extension in Mesa.
  5. *
  6. * Brian Paul
  7. * 20 March 2000
  8. *
  9. * Copyright (C) 2000 Brian Paul All Rights Reserved.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included
  19. * in all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  22. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  24. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  25. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  26. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. #include <assert.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <math.h>
  32. #include <GL/glut.h>
  33. #include <GL/glext.h>
  34. #include "readtex.h"
  35. #define TEXTURE_FILE "../images/girl.rgb"
  36. static GLfloat Xrot = 0, Yrot = -30, Zrot = 0;
  37. static GLboolean Anim = GL_TRUE;
  38. static GLint Bias = 0, BiasStepSign = +1; /* ints avoid fp precision problem */
  39. static GLint BiasMin = -400, BiasMax = 400;
  40. static void
  41. PrintString(const char *s)
  42. {
  43. while (*s) {
  44. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  45. s++;
  46. }
  47. }
  48. static void Idle( void )
  49. {
  50. static int lastTime = 0;
  51. int time = glutGet(GLUT_ELAPSED_TIME);
  52. int step;
  53. if (lastTime == 0)
  54. lastTime = time;
  55. else if (time - lastTime < 10)
  56. return;
  57. step = (time - lastTime) / 10 * BiasStepSign;
  58. lastTime = time;
  59. Bias += step;
  60. if (Bias < BiasMin) {
  61. Bias = BiasMin;
  62. BiasStepSign = +1;
  63. }
  64. else if (Bias > BiasMax) {
  65. Bias = BiasMax;
  66. BiasStepSign = -1;
  67. }
  68. glutPostRedisplay();
  69. }
  70. static void Display( void )
  71. {
  72. char str[100];
  73. glClear( GL_COLOR_BUFFER_BIT );
  74. glMatrixMode( GL_PROJECTION );
  75. glLoadIdentity();
  76. glOrtho(-1, 1, -1, 1, -1, 1);
  77. glMatrixMode( GL_MODELVIEW );
  78. glLoadIdentity();
  79. glDisable(GL_TEXTURE_2D);
  80. glColor3f(1,1,1);
  81. glRasterPos3f(-0.9, -0.9, 0.0);
  82. sprintf(str, "Texture LOD Bias = %4.1f", Bias * 0.01);
  83. PrintString(str);
  84. glMatrixMode( GL_PROJECTION );
  85. glLoadIdentity();
  86. glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
  87. glMatrixMode( GL_MODELVIEW );
  88. glLoadIdentity();
  89. glTranslatef( 0.0, 0.0, -8.0 );
  90. glPushMatrix();
  91. glRotatef(Xrot, 1, 0, 0);
  92. glRotatef(Yrot, 0, 1, 0);
  93. glRotatef(Zrot, 0, 0, 1);
  94. glEnable(GL_TEXTURE_2D);
  95. glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.01 * Bias);
  96. glBegin(GL_POLYGON);
  97. glTexCoord2f(0, 0); glVertex2f(-1, -1);
  98. glTexCoord2f(2, 0); glVertex2f( 1, -1);
  99. glTexCoord2f(2, 2); glVertex2f( 1, 1);
  100. glTexCoord2f(0, 2); glVertex2f(-1, 1);
  101. glEnd();
  102. glPopMatrix();
  103. glutSwapBuffers();
  104. }
  105. static void Reshape( int width, int height )
  106. {
  107. glViewport( 0, 0, width, height );
  108. }
  109. static void Key( unsigned char key, int x, int y )
  110. {
  111. const GLfloat step = 3.0;
  112. (void) x;
  113. (void) y;
  114. switch (key) {
  115. case 'a':
  116. Anim = !Anim;
  117. if (Anim)
  118. glutIdleFunc(Idle);
  119. else
  120. glutIdleFunc(NULL);
  121. break;
  122. case 'z':
  123. Zrot -= step;
  124. break;
  125. case 'Z':
  126. Zrot += step;
  127. break;
  128. case 'b':
  129. Bias -= 10;
  130. break;
  131. case 'B':
  132. Bias += 10;
  133. break;
  134. case '0':
  135. case '1':
  136. case '2':
  137. case '3':
  138. case '4':
  139. case '5':
  140. case '6':
  141. case '7':
  142. case '8':
  143. case '9':
  144. Bias = 100.0 * (key - '0');
  145. break;
  146. case 27:
  147. exit(0);
  148. break;
  149. }
  150. glutPostRedisplay();
  151. }
  152. static void SpecialKey( int key, int x, int y )
  153. {
  154. const GLfloat step = 3.0;
  155. (void) x;
  156. (void) y;
  157. switch (key) {
  158. case GLUT_KEY_UP:
  159. Xrot -= step;
  160. break;
  161. case GLUT_KEY_DOWN:
  162. Xrot += step;
  163. break;
  164. case GLUT_KEY_LEFT:
  165. Yrot -= step;
  166. break;
  167. case GLUT_KEY_RIGHT:
  168. Yrot += step;
  169. break;
  170. }
  171. glutPostRedisplay();
  172. }
  173. static void Init( void )
  174. {
  175. GLfloat maxBias;
  176. if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
  177. printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
  178. exit(1);
  179. }
  180. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  181. if (glutExtensionSupported("GL_SGIS_generate_mipmap")) {
  182. /* test auto mipmap generation */
  183. GLint width, height, i;
  184. GLenum format;
  185. GLubyte *image = LoadRGBImage(TEXTURE_FILE, &width, &height, &format);
  186. if (!image) {
  187. printf("Error: could not load texture image %s\n", TEXTURE_FILE);
  188. exit(1);
  189. }
  190. /* resize to 256 x 256 */
  191. if (width != 256 || height != 256) {
  192. GLubyte *newImage = malloc(256 * 256 * 4);
  193. gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
  194. 256, 256, GL_UNSIGNED_BYTE, newImage);
  195. free(image);
  196. image = newImage;
  197. }
  198. printf("Using GL_SGIS_generate_mipmap\n");
  199. glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
  200. glTexImage2D(GL_TEXTURE_2D, 0, format, 256, 256, 0,
  201. format, GL_UNSIGNED_BYTE, image);
  202. free(image);
  203. /* make sure mipmap was really generated correctly */
  204. width = height = 256;
  205. for (i = 0; i < 9; i++) {
  206. GLint w, h;
  207. glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w);
  208. glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h);
  209. printf("Level %d size: %d x %d\n", i, w, h);
  210. assert(w == width);
  211. assert(h == height);
  212. width /= 2;
  213. height /= 2;
  214. }
  215. }
  216. else if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
  217. printf("Error: could not load texture image %s\n", TEXTURE_FILE);
  218. exit(1);
  219. }
  220. /* mipmapping required for this extension */
  221. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  222. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  223. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  224. glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT, &maxBias);
  225. printf("LOD bias range: [%g, %g]\n", -maxBias, maxBias);
  226. BiasMin = -100 * maxBias;
  227. BiasMax = 100 * maxBias;
  228. /* Since we have (about) 8 mipmap levels, no need to bias beyond
  229. * the range [-1, +8].
  230. */
  231. if (BiasMin < -100)
  232. BiasMin = -100;
  233. if (BiasMax > 800)
  234. BiasMax = 800;
  235. }
  236. int main( int argc, char *argv[] )
  237. {
  238. glutInit( &argc, argv );
  239. glutInitWindowPosition( 0, 0 );
  240. glutInitWindowSize( 350, 350 );
  241. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  242. glutCreateWindow(argv[0]);
  243. glutReshapeFunc( Reshape );
  244. glutKeyboardFunc( Key );
  245. glutSpecialFunc( SpecialKey );
  246. glutDisplayFunc( Display );
  247. if (Anim)
  248. glutIdleFunc(Idle);
  249. Init();
  250. glutMainLoop();
  251. return 0;
  252. }