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.

mipmap_limits.c 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* Test GL_TEXTURE_BASE_LEVEL and GL_TEXTURE_MAX_LEVEL
  2. * Brian Paul
  3. * 10 May 2006
  4. */
  5. /* Copyright (c) Mark J. Kilgard, 1994. */
  6. /*
  7. * (c) Copyright 1993, Silicon Graphics, Inc.
  8. * ALL RIGHTS RESERVED
  9. * Permission to use, copy, modify, and distribute this software for
  10. * any purpose and without fee is hereby granted, provided that the above
  11. * copyright notice appear in all copies and that both the copyright notice
  12. * and this permission notice appear in supporting documentation, and that
  13. * the name of Silicon Graphics, Inc. not be used in advertising
  14. * or publicity pertaining to distribution of the software without specific,
  15. * written prior permission.
  16. *
  17. * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  18. * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  19. * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  20. * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  21. * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  22. * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  23. * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  24. * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  25. * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
  26. * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  27. * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  28. * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  29. *
  30. * US Government Users Restricted Rights
  31. * Use, duplication, or disclosure by the Government is subject to
  32. * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  33. * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  34. * clause at DFARS 252.227-7013 and/or in similar or successor
  35. * clauses in the FAR or the DOD or NASA FAR Supplement.
  36. * Unpublished-- rights reserved under the copyright laws of the
  37. * United States. Contractor/manufacturer is Silicon Graphics,
  38. * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
  39. *
  40. * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  41. */
  42. /* mipmap.c
  43. * This program demonstrates using mipmaps for texture maps.
  44. * To overtly show the effect of mipmaps, each mipmap reduction
  45. * level has a solidly colored, contrasting texture image.
  46. * Thus, the quadrilateral which is drawn is drawn with several
  47. * different colors.
  48. */
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <GL/glew.h>
  52. #include <GL/glut.h>
  53. #include "readtex.h"
  54. #define TEXTURE_FILE "../images/girl.rgb"
  55. static GLint BaseLevel = 0, MaxLevel = 9;
  56. static GLfloat MinLod = -1, MaxLod = 9;
  57. static GLfloat LodBias = 0.0;
  58. static GLboolean NearestFilter = GL_TRUE;
  59. static GLuint texImage, texColor, texCurrent;
  60. static void
  61. InitValues(void)
  62. {
  63. BaseLevel = 0;
  64. MaxLevel = 9;
  65. MinLod = -1;
  66. MaxLod = 9;
  67. LodBias = 0.0;
  68. NearestFilter = GL_TRUE;
  69. }
  70. static void
  71. MakeImage(int level, int width, int height, const GLubyte color[4])
  72. {
  73. const int makeStripes = 0;
  74. GLubyte img[512 * 512 * 3];
  75. int i, j;
  76. for (i = 0; i < height; i++) {
  77. for (j = 0; j < width; j++) {
  78. int k = (i * width + j) * 3;
  79. int p = (i / 8) & makeStripes;
  80. if (p == 0) {
  81. img[k + 0] = color[0];
  82. img[k + 1] = color[1];
  83. img[k + 2] = color[2];
  84. }
  85. else {
  86. img[k + 0] = 0;
  87. img[k + 1] = 0;
  88. img[k + 2] = 0;
  89. }
  90. }
  91. }
  92. glTexImage2D(GL_TEXTURE_2D, level, GL_RGB, width, height, 0,
  93. GL_RGB, GL_UNSIGNED_BYTE, img);
  94. }
  95. static void
  96. makeImages(int image)
  97. {
  98. #define WIDTH 512
  99. #define HEIGHT 512
  100. if (glutExtensionSupported("GL_SGIS_generate_mipmap") && image) {
  101. /* test auto mipmap generation */
  102. GLint width, height, i;
  103. GLenum format;
  104. GLubyte *image = LoadRGBImage(TEXTURE_FILE, &width, &height, &format);
  105. if (!image) {
  106. printf("Error: could not load texture image %s\n", TEXTURE_FILE);
  107. exit(1);
  108. }
  109. /* resize */
  110. if (width != WIDTH || height != HEIGHT) {
  111. GLubyte *newImage = malloc(WIDTH * HEIGHT * 4);
  112. gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
  113. WIDTH, HEIGHT, GL_UNSIGNED_BYTE, newImage);
  114. free(image);
  115. image = newImage;
  116. }
  117. printf("Using GL_SGIS_generate_mipmap\n");
  118. glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
  119. glTexImage2D(GL_TEXTURE_2D, 0, format, WIDTH, HEIGHT, 0,
  120. format, GL_UNSIGNED_BYTE, image);
  121. glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_FALSE);
  122. free(image);
  123. /* make sure mipmap was really generated correctly */
  124. width = WIDTH;
  125. height = HEIGHT;
  126. for (i = 0; i < 10; i++) {
  127. GLint w, h;
  128. glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w);
  129. glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h);
  130. printf("Level %d size: %d x %d\n", i, w, h);
  131. width /= 2;
  132. height /= 2;
  133. }
  134. }
  135. else {
  136. static const GLubyte colors[10][3] = {
  137. {128, 128, 128},
  138. {0, 255, 255},
  139. {255, 255, 0},
  140. {255, 0, 255},
  141. {255, 0, 0},
  142. {0, 255, 0},
  143. {0, 0, 255},
  144. {0, 255, 255},
  145. {255, 255, 0},
  146. {255, 255, 255}
  147. };
  148. int i, sz = 512;
  149. for (i = 0; i < 10; i++) {
  150. MakeImage(i, sz, sz, colors[i]);
  151. printf("Level %d size: %d x %d\n", i, sz, sz);
  152. sz /= 2;
  153. }
  154. }
  155. }
  156. static void
  157. myinit(void)
  158. {
  159. InitValues();
  160. glEnable(GL_DEPTH_TEST);
  161. glDepthFunc(GL_LESS);
  162. glShadeModel(GL_FLAT);
  163. glTranslatef(0.0, 0.0, -3.6);
  164. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  165. glGenTextures(1, &texImage);
  166. glBindTexture(GL_TEXTURE_2D, texImage);
  167. makeImages(1);
  168. glGenTextures(1, &texColor);
  169. glBindTexture(GL_TEXTURE_2D, texColor);
  170. makeImages(0);
  171. texCurrent = texImage;
  172. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  173. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  174. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  175. glEnable(GL_TEXTURE_2D);
  176. }
  177. static void
  178. display(void)
  179. {
  180. GLfloat tcm = 1.0;
  181. glBindTexture(GL_TEXTURE_2D, texCurrent);
  182. printf
  183. ("BASE_LEVEL=%d MAX_LEVEL=%d MIN_LOD=%.2g MAX_LOD=%.2g Bias=%.2g Filter=%s\n",
  184. BaseLevel, MaxLevel, MinLod, MaxLod, LodBias,
  185. NearestFilter ? "NEAREST" : "LINEAR");
  186. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, BaseLevel);
  187. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, MaxLevel);
  188. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, MinLod);
  189. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, MaxLod);
  190. if (NearestFilter) {
  191. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  192. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  193. GL_NEAREST_MIPMAP_NEAREST);
  194. }
  195. else {
  196. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  197. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  198. GL_LINEAR_MIPMAP_LINEAR);
  199. }
  200. glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, LodBias);
  201. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  202. glBegin(GL_QUADS);
  203. glTexCoord2f(0.0, 0.0);
  204. glVertex3f(-2.0, -1.0, 0.0);
  205. glTexCoord2f(0.0, tcm);
  206. glVertex3f(-2.0, 1.0, 0.0);
  207. glTexCoord2f(tcm * 3000.0, tcm);
  208. glVertex3f(3000.0, 1.0, -6000.0);
  209. glTexCoord2f(tcm * 3000.0, 0.0);
  210. glVertex3f(3000.0, -1.0, -6000.0);
  211. glEnd();
  212. glFlush();
  213. }
  214. static void
  215. myReshape(int w, int h)
  216. {
  217. glViewport(0, 0, w, h);
  218. glMatrixMode(GL_PROJECTION);
  219. glLoadIdentity();
  220. gluPerspective(60.0, 1.0 * (GLfloat) w / (GLfloat) h, 1.0, 30000.0);
  221. glMatrixMode(GL_MODELVIEW);
  222. glLoadIdentity();
  223. }
  224. static void
  225. usage(void)
  226. {
  227. printf("usage:\n");
  228. printf(" b/B decrease/increase GL_TEXTURE_BASE_LEVEL\n");
  229. printf(" m/M decrease/increase GL_TEXTURE_MAX_LEVEL\n");
  230. printf(" n/N decrease/increase GL_TEXTURE_MIN_LOD\n");
  231. printf(" x/X decrease/increase GL_TEXTURE_MAX_LOD\n");
  232. printf(" l/L decrease/increase GL_TEXTURE_LOD_BIAS\n");
  233. printf(" f toggle nearest/linear filtering\n");
  234. printf(" t toggle texture color/image\n");
  235. printf(" SPACE reset values\n");
  236. }
  237. static void
  238. key(unsigned char k, int x, int y)
  239. {
  240. (void) x;
  241. (void) y;
  242. switch (k) {
  243. case 'b':
  244. BaseLevel--;
  245. if (BaseLevel < 0)
  246. BaseLevel = 0;
  247. break;
  248. case 'B':
  249. BaseLevel++;
  250. if (BaseLevel > 10)
  251. BaseLevel = 10;
  252. break;
  253. case 'm':
  254. MaxLevel--;
  255. if (MaxLevel < 0)
  256. MaxLevel = 0;
  257. break;
  258. case 'M':
  259. MaxLevel++;
  260. if (MaxLevel > 10)
  261. MaxLevel = 10;
  262. break;
  263. case 'l':
  264. LodBias -= 0.25;
  265. break;
  266. case 'L':
  267. LodBias += 0.25;
  268. break;
  269. case 'n':
  270. MinLod -= 0.25;
  271. break;
  272. case 'N':
  273. MinLod += 0.25;
  274. break;
  275. case 'x':
  276. MaxLod -= 0.25;
  277. break;
  278. case 'X':
  279. MaxLod += 0.25;
  280. break;
  281. case 'f':
  282. NearestFilter = !NearestFilter;
  283. break;
  284. case 't':
  285. if (texCurrent == texColor)
  286. texCurrent = texImage;
  287. else
  288. texCurrent = texColor;
  289. break;
  290. case ' ':
  291. InitValues();
  292. /* fall-through */
  293. case 'u':
  294. usage();
  295. break;
  296. case 27: /* Escape */
  297. exit(0);
  298. break;
  299. default:
  300. return;
  301. }
  302. glutPostRedisplay();
  303. }
  304. int
  305. main(int argc, char **argv)
  306. {
  307. glutInit(&argc, argv);
  308. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  309. glutInitWindowSize(600, 600);
  310. glutCreateWindow(argv[0]);
  311. glewInit();
  312. myinit();
  313. glutReshapeFunc(myReshape);
  314. glutDisplayFunc(display);
  315. glutKeyboardFunc(key);
  316. usage();
  317. glutMainLoop();
  318. return 0; /* ANSI C requires main to return int. */
  319. }