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.4KB

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