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_view.c 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Test mipmap generation and lod bias.
  3. *
  4. * Brian Paul
  5. * 17 March 2008
  6. */
  7. #include <assert.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <math.h>
  11. #include <GL/glew.h>
  12. #include <GL/glut.h>
  13. #include <GL/glext.h>
  14. #include "readtex.h"
  15. #define TEXTURE_FILE "../images/arch.rgb"
  16. #define LEVELS 8
  17. #define SIZE (1<<LEVELS)
  18. static int TexWidth = SIZE, TexHeight = SIZE;
  19. static int WinWidth = 1044, WinHeight = 900;
  20. static GLfloat Bias = 0.0;
  21. static GLboolean ScaleQuads = GL_FALSE;
  22. static GLboolean Linear = GL_FALSE;
  23. static GLint Win = 0;
  24. static GLint RenderTextureLevel = 0;
  25. static GLuint TexObj;
  26. static void
  27. CheckError(int line)
  28. {
  29. GLenum err = glGetError();
  30. if (err) {
  31. printf("GL Error 0x%x at line %d\n", (int) err, line);
  32. }
  33. }
  34. static void
  35. PrintString(const char *s)
  36. {
  37. while (*s) {
  38. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  39. s++;
  40. }
  41. }
  42. static void
  43. MipGenTexture( void )
  44. {
  45. /* test auto mipmap generation */
  46. GLint width, height, i;
  47. GLenum format;
  48. GLubyte *image = LoadRGBImage(TEXTURE_FILE, &width, &height, &format);
  49. if (!image) {
  50. printf("Error: could not load texture image %s\n", TEXTURE_FILE);
  51. exit(1);
  52. }
  53. /* resize to TexWidth x TexHeight */
  54. if (width != TexWidth || height != TexHeight) {
  55. GLubyte *newImage = malloc(TexWidth * TexHeight * 4);
  56. fprintf(stderr, "rescale %d %d to %d %d\n", width, height,
  57. TexWidth, TexHeight);
  58. fflush(stderr);
  59. gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
  60. TexWidth, TexHeight, GL_UNSIGNED_BYTE, newImage);
  61. free(image);
  62. image = newImage;
  63. }
  64. printf("Using GL_SGIS_generate_mipmap\n");
  65. glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
  66. glTexImage2D(GL_TEXTURE_2D, 0, format, TexWidth, TexHeight, 0,
  67. format, GL_UNSIGNED_BYTE, image);
  68. free(image);
  69. /* make sure mipmap was really generated correctly */
  70. width = TexWidth;
  71. height = TexHeight;
  72. for (i = 0; i < 9; i++) {
  73. GLint w, h;
  74. glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w);
  75. glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h);
  76. printf("Level %d size: %d x %d\n", i, w, h);
  77. assert(w == width);
  78. assert(h == height);
  79. width /= 2;
  80. height /= 2;
  81. }
  82. glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_FALSE);
  83. }
  84. static void
  85. ResetTextureLevel( int i )
  86. {
  87. GLubyte tex2d[SIZE*SIZE][4];
  88. {
  89. GLint Width = TexWidth / (1 << i);
  90. GLint Height = TexHeight / (1 << i);
  91. GLint s, t;
  92. for (s = 0; s < Width; s++) {
  93. for (t = 0; t < Height; t++) {
  94. tex2d[t*Width+s][0] = ((s / 16) % 2) ? 0 : 255;
  95. tex2d[t*Width+s][1] = ((t / 16) % 2) ? 0 : 255;
  96. tex2d[t*Width+s][2] = 128;
  97. tex2d[t*Width+s][3] = 255;
  98. }
  99. }
  100. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  101. glTexImage2D(GL_TEXTURE_2D, i, GL_RGB, Width, Height, 0,
  102. GL_RGBA, GL_UNSIGNED_BYTE, tex2d);
  103. }
  104. }
  105. static void
  106. ResetTexture( void )
  107. {
  108. #if 0
  109. /* This doesn't work so well as the arch texture is 512x512.
  110. */
  111. LoadRGBMipmaps(TEXTURE_FILE, GL_RGB);
  112. #else
  113. {
  114. int i;
  115. for (i = 0; i <= LEVELS; i++)
  116. {
  117. ResetTextureLevel(i);
  118. }
  119. }
  120. #endif
  121. }
  122. static void
  123. RenderTexture( void )
  124. {
  125. GLenum status;
  126. GLuint MyFB;
  127. fprintf(stderr, "RenderTextureLevel %d\n", RenderTextureLevel);
  128. fflush(stderr);
  129. /* gen framebuffer id, delete it, do some assertions, just for testing */
  130. glGenFramebuffersEXT(1, &MyFB);
  131. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
  132. assert(glIsFramebufferEXT(MyFB));
  133. CheckError(__LINE__);
  134. /* Render color to texture */
  135. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
  136. GL_COLOR_ATTACHMENT0_EXT,
  137. GL_TEXTURE_2D, TexObj,
  138. RenderTextureLevel);
  139. CheckError(__LINE__);
  140. glMatrixMode(GL_PROJECTION);
  141. glLoadIdentity();
  142. glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  143. glMatrixMode(GL_MODELVIEW);
  144. glLoadIdentity();
  145. glTranslatef(0.0, 0.0, -15.0);
  146. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  147. if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
  148. printf("Framebuffer incomplete!!!\n");
  149. }
  150. glViewport(0, 0,
  151. TexWidth / (1 << RenderTextureLevel),
  152. TexHeight / (1 << RenderTextureLevel));
  153. glClearColor(0.5, 0.5, 1.0, 0.0);
  154. glClear(GL_COLOR_BUFFER_BIT);
  155. CheckError(__LINE__);
  156. glBegin(GL_POLYGON);
  157. glColor3f(1, 0, 0);
  158. glVertex2f(-1, -1);
  159. glColor3f(0, 1, 0);
  160. glVertex2f(1, -1);
  161. glColor3f(0, 0, 1);
  162. glVertex2f(0, 1);
  163. glEnd();
  164. /* Bind normal framebuffer */
  165. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  166. CheckError(__LINE__);
  167. glDeleteFramebuffersEXT(1, &MyFB);
  168. CheckError(__LINE__);
  169. glClearColor(0, 0, 0, 0);
  170. }
  171. static void
  172. Display(void)
  173. {
  174. int x, y, bias;
  175. char str[100];
  176. int texWidth = TexWidth, texHeight = TexHeight;
  177. glViewport(0, 0, WinHeight, WinHeight);
  178. glClear(GL_COLOR_BUFFER_BIT);
  179. glMatrixMode(GL_PROJECTION);
  180. glLoadIdentity();
  181. glOrtho(0, WinWidth, 0, WinHeight, -1, 1);
  182. glMatrixMode(GL_MODELVIEW);
  183. glLoadIdentity();
  184. glColor3f(1,1,1);
  185. if (Linear) {
  186. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  187. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  188. }
  189. else {
  190. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
  191. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  192. }
  193. y = WinHeight - 300;
  194. x = 4;
  195. for (bias = -1; bias < 11; bias++) {
  196. if (ScaleQuads) {
  197. if (bias > 0) {
  198. if (texWidth == 1 && texHeight == 1)
  199. break;
  200. texWidth = TexWidth >> bias;
  201. texHeight = TexHeight >> bias;
  202. if (texWidth < 1)
  203. texWidth = 1;
  204. if (texHeight < 1)
  205. texHeight = 1;
  206. }
  207. glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.0);
  208. }
  209. else {
  210. glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, bias);
  211. }
  212. glRasterPos2f(x, y + TexHeight + 5);
  213. if (ScaleQuads)
  214. sprintf(str, "Texture Level %d: %d x %d",
  215. (bias < 0 ? 0 : bias),
  216. texWidth, texHeight);
  217. else
  218. sprintf(str, "Texture LOD Bias = %d", bias);
  219. PrintString(str);
  220. glPushMatrix();
  221. glTranslatef(x, y, 0);
  222. glEnable(GL_TEXTURE_2D);
  223. glBegin(GL_POLYGON);
  224. glTexCoord2f(0, 0); glVertex2f(0, 0);
  225. glTexCoord2f(1, 0); glVertex2f(texWidth, 0);
  226. glTexCoord2f(1, 1); glVertex2f(texWidth, texHeight);
  227. glTexCoord2f(0, 1); glVertex2f(0, texHeight);
  228. glEnd();
  229. glPopMatrix();
  230. glDisable(GL_TEXTURE_2D);
  231. x += TexWidth + 4;
  232. if (x >= WinWidth) {
  233. x = 4;
  234. y -= 300;
  235. }
  236. }
  237. glutSwapBuffers();
  238. }
  239. static void
  240. Reshape(int width, int height)
  241. {
  242. WinWidth = width;
  243. WinHeight = height;
  244. }
  245. static void
  246. Key(unsigned char key, int x, int y)
  247. {
  248. (void) x;
  249. (void) y;
  250. switch (key) {
  251. case 'b':
  252. Bias -= 10;
  253. break;
  254. case 'B':
  255. Bias += 10;
  256. break;
  257. case 'l':
  258. Linear = !Linear;
  259. break;
  260. case 'v':
  261. RenderTextureLevel++;
  262. break;
  263. case 'V':
  264. RenderTextureLevel--;
  265. break;
  266. case 'r':
  267. RenderTexture();
  268. break;
  269. case 'X':
  270. ResetTexture();
  271. break;
  272. case 'x':
  273. ResetTextureLevel(RenderTextureLevel);
  274. break;
  275. case '0':
  276. case '1':
  277. case '2':
  278. case '3':
  279. case '4':
  280. case '5':
  281. case '6':
  282. case '7':
  283. case '8':
  284. case '9':
  285. Bias = 100.0 * (key - '0');
  286. break;
  287. case 's':
  288. ScaleQuads = !ScaleQuads;
  289. break;
  290. case ' ':
  291. MipGenTexture();
  292. Bias = 0;
  293. Linear = 0;
  294. RenderTextureLevel = 0;
  295. ScaleQuads = 0;
  296. break;
  297. case 27:
  298. glutDestroyWindow(Win);
  299. exit(0);
  300. break;
  301. }
  302. glutPostRedisplay();
  303. }
  304. static void
  305. Init(void)
  306. {
  307. GLfloat maxBias;
  308. if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
  309. printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
  310. exit(1);
  311. }
  312. if (!glutExtensionSupported("GL_SGIS_generate_mipmap")) {
  313. printf("Sorry, GL_SGIS_generate_mipmap not supported by this renderer.\n");
  314. exit(1);
  315. }
  316. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  317. glGenTextures(1, &TexObj);
  318. glBindTexture(GL_TEXTURE_2D, TexObj);
  319. if (1)
  320. MipGenTexture();
  321. else
  322. ResetTexture();
  323. /* mipmapping required for this extension */
  324. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  325. glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT, &maxBias);
  326. printf("GL_RENDERER: %s\n", (char*) glGetString(GL_RENDERER));
  327. printf("LOD bias range: [%g, %g]\n", -maxBias, maxBias);
  328. printf("Press 's' to toggle quad scaling\n");
  329. }
  330. int
  331. main(int argc, char *argv[])
  332. {
  333. glutInit(&argc, argv);
  334. glutInitWindowPosition(0, 0);
  335. glutInitWindowSize(WinWidth, WinHeight);
  336. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  337. Win = glutCreateWindow(argv[0]);
  338. glewInit();
  339. glutReshapeFunc(Reshape);
  340. glutKeyboardFunc(Key);
  341. glutDisplayFunc(Display);
  342. Init();
  343. glutMainLoop();
  344. return 0;
  345. }