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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 int win = 0;
  41. static void
  42. PrintString(const char *s)
  43. {
  44. while (*s) {
  45. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  46. s++;
  47. }
  48. }
  49. static void Idle( void )
  50. {
  51. static int lastTime = 0;
  52. int time = glutGet(GLUT_ELAPSED_TIME);
  53. int step;
  54. if (lastTime == 0)
  55. lastTime = time;
  56. else if (time - lastTime < 10)
  57. return;
  58. step = (time - lastTime) / 10 * BiasStepSign;
  59. lastTime = time;
  60. Bias += step;
  61. if (Bias < BiasMin) {
  62. Bias = BiasMin;
  63. BiasStepSign = +1;
  64. }
  65. else if (Bias > BiasMax) {
  66. Bias = BiasMax;
  67. BiasStepSign = -1;
  68. }
  69. glutPostRedisplay();
  70. }
  71. static void Display( void )
  72. {
  73. char str[100];
  74. glClear( GL_COLOR_BUFFER_BIT );
  75. glMatrixMode( GL_PROJECTION );
  76. glLoadIdentity();
  77. glOrtho(-1, 1, -1, 1, -1, 1);
  78. glMatrixMode( GL_MODELVIEW );
  79. glLoadIdentity();
  80. glDisable(GL_TEXTURE_2D);
  81. glColor3f(1,1,1);
  82. glRasterPos3f(-0.9, -0.9, 0.0);
  83. sprintf(str, "Texture LOD Bias = %4.1f", Bias * 0.01);
  84. PrintString(str);
  85. glMatrixMode( GL_PROJECTION );
  86. glLoadIdentity();
  87. glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
  88. glMatrixMode( GL_MODELVIEW );
  89. glLoadIdentity();
  90. glTranslatef( 0.0, 0.0, -8.0 );
  91. glPushMatrix();
  92. glRotatef(Xrot, 1, 0, 0);
  93. glRotatef(Yrot, 0, 1, 0);
  94. glRotatef(Zrot, 0, 0, 1);
  95. glEnable(GL_TEXTURE_2D);
  96. glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.01 * Bias);
  97. glBegin(GL_POLYGON);
  98. glTexCoord2f(0, 0); glVertex2f(-1, -1);
  99. glTexCoord2f(2, 0); glVertex2f( 1, -1);
  100. glTexCoord2f(2, 2); glVertex2f( 1, 1);
  101. glTexCoord2f(0, 2); glVertex2f(-1, 1);
  102. glEnd();
  103. glPopMatrix();
  104. glutSwapBuffers();
  105. }
  106. static void Reshape( int width, int height )
  107. {
  108. glViewport( 0, 0, width, height );
  109. }
  110. static void Key( unsigned char key, int x, int y )
  111. {
  112. const GLfloat step = 3.0;
  113. (void) x;
  114. (void) y;
  115. switch (key) {
  116. case 'a':
  117. Anim = !Anim;
  118. if (Anim)
  119. glutIdleFunc(Idle);
  120. else
  121. glutIdleFunc(NULL);
  122. break;
  123. case 'z':
  124. Zrot -= step;
  125. break;
  126. case 'Z':
  127. Zrot += step;
  128. break;
  129. case 'b':
  130. Bias -= 10;
  131. break;
  132. case 'B':
  133. Bias += 10;
  134. break;
  135. case '0':
  136. case '1':
  137. case '2':
  138. case '3':
  139. case '4':
  140. case '5':
  141. case '6':
  142. case '7':
  143. case '8':
  144. case '9':
  145. Bias = 100.0 * (key - '0');
  146. break;
  147. case 27:
  148. glutDestroyWindow(win);
  149. exit(0);
  150. break;
  151. }
  152. glutPostRedisplay();
  153. }
  154. static void SpecialKey( int key, int x, int y )
  155. {
  156. const GLfloat step = 3.0;
  157. (void) x;
  158. (void) y;
  159. switch (key) {
  160. case GLUT_KEY_UP:
  161. Xrot -= step;
  162. break;
  163. case GLUT_KEY_DOWN:
  164. Xrot += step;
  165. break;
  166. case GLUT_KEY_LEFT:
  167. Yrot -= step;
  168. break;
  169. case GLUT_KEY_RIGHT:
  170. Yrot += step;
  171. break;
  172. }
  173. glutPostRedisplay();
  174. }
  175. static void Init( void )
  176. {
  177. GLfloat maxBias;
  178. if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
  179. printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
  180. exit(1);
  181. }
  182. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  183. if (glutExtensionSupported("GL_SGIS_generate_mipmap")) {
  184. /* test auto mipmap generation */
  185. GLint width, height, i;
  186. GLenum format;
  187. GLubyte *image = LoadRGBImage(TEXTURE_FILE, &width, &height, &format);
  188. if (!image) {
  189. printf("Error: could not load texture image %s\n", TEXTURE_FILE);
  190. exit(1);
  191. }
  192. /* resize to 256 x 256 */
  193. if (width != 256 || height != 256) {
  194. GLubyte *newImage = malloc(256 * 256 * 4);
  195. gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
  196. 256, 256, GL_UNSIGNED_BYTE, newImage);
  197. free(image);
  198. image = newImage;
  199. }
  200. printf("Using GL_SGIS_generate_mipmap\n");
  201. glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
  202. glTexImage2D(GL_TEXTURE_2D, 0, format, 256, 256, 0,
  203. format, GL_UNSIGNED_BYTE, image);
  204. free(image);
  205. /* make sure mipmap was really generated correctly */
  206. width = height = 256;
  207. for (i = 0; i < 9; i++) {
  208. GLint w, h;
  209. glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w);
  210. glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h);
  211. printf("Level %d size: %d x %d\n", i, w, h);
  212. assert(w == width);
  213. assert(h == height);
  214. width /= 2;
  215. height /= 2;
  216. }
  217. }
  218. else if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
  219. printf("Error: could not load texture image %s\n", TEXTURE_FILE);
  220. exit(1);
  221. }
  222. /* mipmapping required for this extension */
  223. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  224. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  225. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  226. glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT, &maxBias);
  227. printf("LOD bias range: [%g, %g]\n", -maxBias, maxBias);
  228. BiasMin = -100 * maxBias;
  229. BiasMax = 100 * maxBias;
  230. /* Since we have (about) 8 mipmap levels, no need to bias beyond
  231. * the range [-1, +8].
  232. */
  233. if (BiasMin < -100)
  234. BiasMin = -100;
  235. if (BiasMax > 800)
  236. BiasMax = 800;
  237. }
  238. int main( int argc, char *argv[] )
  239. {
  240. glutInit( &argc, argv );
  241. glutInitWindowPosition( 0, 0 );
  242. glutInitWindowSize( 350, 350 );
  243. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  244. win = glutCreateWindow(argv[0]);
  245. glutReshapeFunc( Reshape );
  246. glutKeyboardFunc( Key );
  247. glutSpecialFunc( SpecialKey );
  248. glutDisplayFunc( Display );
  249. if (Anim)
  250. glutIdleFunc(Idle);
  251. Init();
  252. glutMainLoop();
  253. return 0;
  254. }