Clone of mesa.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

lodbias.c 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 <stdlib.h>
  29. #include <stdio.h>
  30. #include <math.h>
  31. #include <GL/glut.h>
  32. #include <GL/glext.h>
  33. #include "readtex.c" /* I know, this is a hack. */
  34. #define TEXTURE_FILE "../images/girl.rgb"
  35. static GLfloat Xrot = 0, Yrot = -30, Zrot = 0;
  36. static GLboolean Anim = GL_TRUE;
  37. static GLint Bias = 0, BiasStepSign = +1; /* ints avoid fp precision problem */
  38. static GLint BiasMin = -400, BiasMax = 400;
  39. static void
  40. PrintString(const char *s)
  41. {
  42. while (*s) {
  43. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  44. s++;
  45. }
  46. }
  47. static void Idle( void )
  48. {
  49. static int lastTime = 0;
  50. int time = glutGet(GLUT_ELAPSED_TIME);
  51. int step;
  52. if (lastTime == 0)
  53. lastTime = time;
  54. else if (time - lastTime < 10)
  55. return;
  56. step = (time - lastTime) / 10 * BiasStepSign;
  57. lastTime = time;
  58. Bias += step;
  59. if (Bias < BiasMin) {
  60. Bias = BiasMin;
  61. BiasStepSign = +1;
  62. }
  63. else if (Bias > BiasMax) {
  64. Bias = BiasMax;
  65. BiasStepSign = -1;
  66. }
  67. glutPostRedisplay();
  68. }
  69. static void Display( void )
  70. {
  71. char str[100];
  72. glClear( GL_COLOR_BUFFER_BIT );
  73. glMatrixMode( GL_PROJECTION );
  74. glLoadIdentity();
  75. glOrtho(-1, 1, -1, 1, -1, 1);
  76. glMatrixMode( GL_MODELVIEW );
  77. glLoadIdentity();
  78. glDisable(GL_TEXTURE_2D);
  79. glColor3f(1,1,1);
  80. glRasterPos3f(-0.9, -0.9, 0.0);
  81. sprintf(str, "Texture LOD Bias = %4.1f", Bias * 0.01);
  82. PrintString(str);
  83. glMatrixMode( GL_PROJECTION );
  84. glLoadIdentity();
  85. glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
  86. glMatrixMode( GL_MODELVIEW );
  87. glLoadIdentity();
  88. glTranslatef( 0.0, 0.0, -8.0 );
  89. glPushMatrix();
  90. glRotatef(Xrot, 1, 0, 0);
  91. glRotatef(Yrot, 0, 1, 0);
  92. glRotatef(Zrot, 0, 0, 1);
  93. glEnable(GL_TEXTURE_2D);
  94. glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.01 * Bias);
  95. glBegin(GL_POLYGON);
  96. glTexCoord2f(0, 0); glVertex2f(-1, -1);
  97. glTexCoord2f(2, 0); glVertex2f( 1, -1);
  98. glTexCoord2f(2, 2); glVertex2f( 1, 1);
  99. glTexCoord2f(0, 2); glVertex2f(-1, 1);
  100. glEnd();
  101. glPopMatrix();
  102. glutSwapBuffers();
  103. }
  104. static void Reshape( int width, int height )
  105. {
  106. glViewport( 0, 0, width, height );
  107. }
  108. static void Key( unsigned char key, int x, int y )
  109. {
  110. const GLfloat step = 3.0;
  111. (void) x;
  112. (void) y;
  113. switch (key) {
  114. case 'a':
  115. Anim = !Anim;
  116. if (Anim)
  117. glutIdleFunc(Idle);
  118. else
  119. glutIdleFunc(NULL);
  120. break;
  121. case 'z':
  122. Zrot -= step;
  123. break;
  124. case 'Z':
  125. Zrot += step;
  126. break;
  127. case 'b':
  128. Bias -= 10;
  129. break;
  130. case 'B':
  131. Bias += 10;
  132. break;
  133. case 27:
  134. exit(0);
  135. break;
  136. }
  137. glutPostRedisplay();
  138. }
  139. static void SpecialKey( int key, int x, int y )
  140. {
  141. const GLfloat step = 3.0;
  142. (void) x;
  143. (void) y;
  144. switch (key) {
  145. case GLUT_KEY_UP:
  146. Xrot -= step;
  147. break;
  148. case GLUT_KEY_DOWN:
  149. Xrot += step;
  150. break;
  151. case GLUT_KEY_LEFT:
  152. Yrot -= step;
  153. break;
  154. case GLUT_KEY_RIGHT:
  155. Yrot += step;
  156. break;
  157. }
  158. glutPostRedisplay();
  159. }
  160. static void Init( void )
  161. {
  162. const char *exten = (const char *) glGetString(GL_EXTENSIONS);
  163. GLfloat maxBias;
  164. if (!strstr(exten, "GL_EXT_texture_lod_bias")) {
  165. printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
  166. exit(1);
  167. }
  168. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  169. if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
  170. printf("Error: could not load texture image %s\n", TEXTURE_FILE);
  171. exit(1);
  172. }
  173. /* mipmapping required for this extension */
  174. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  175. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  176. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  177. glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT, &maxBias);
  178. printf("LOD bias range: [%g, %g]\n", -maxBias, maxBias);
  179. BiasMin = -100 * maxBias;
  180. BiasMax = 100 * maxBias;
  181. }
  182. int main( int argc, char *argv[] )
  183. {
  184. glutInit( &argc, argv );
  185. glutInitWindowPosition( 0, 0 );
  186. glutInitWindowSize( 350, 350 );
  187. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  188. glutCreateWindow(argv[0]);
  189. glutReshapeFunc( Reshape );
  190. glutKeyboardFunc( Key );
  191. glutSpecialFunc( SpecialKey );
  192. glutDisplayFunc( Display );
  193. if (Anim)
  194. glutIdleFunc(Idle);
  195. Init();
  196. glutMainLoop();
  197. return 0;
  198. }