Clone of mesa.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* $Id: manytex.c,v 1.1 2000/08/02 17:57:56 brianp Exp $ */
  2. /*
  3. * test handling of many texture maps
  4. * Also tests texture priority and residency.
  5. *
  6. * Brian Paul
  7. * August 2, 2000
  8. */
  9. #include <assert.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <math.h>
  13. #include <GL/glut.h>
  14. static GLint NumTextures = 20;
  15. static GLuint *TextureID = NULL;
  16. static GLboolean *TextureResidency = NULL;
  17. static GLint TexWidth = 128, TexHeight = 128;
  18. static GLfloat Zrot = 0;
  19. static GLboolean Anim = GL_TRUE;
  20. static GLint WinWidth = 500, WinHeight = 400;
  21. static GLboolean MipMap = GL_FALSE;
  22. static GLboolean LinearFilter = GL_FALSE;
  23. static GLboolean RandomSize = GL_FALSE;
  24. static GLint Rows, Columns;
  25. static GLuint LowPriorityCount = 0;
  26. static void Idle( void )
  27. {
  28. Zrot += 1.0;
  29. glutPostRedisplay();
  30. }
  31. static void Display( void )
  32. {
  33. GLfloat spacing = WinWidth / Columns;
  34. GLfloat size = spacing * 0.4;
  35. GLint i;
  36. /* test residency */
  37. {
  38. GLboolean b;
  39. GLint i, resident;
  40. b = glAreTexturesResident(NumTextures, TextureID, TextureResidency);
  41. if (b) {
  42. printf("all resident\n");
  43. }
  44. else {
  45. resident = 0;
  46. for (i = 0; i < NumTextures; i++) {
  47. if (TextureResidency[i]) {
  48. resident++;
  49. }
  50. }
  51. printf("%d of %d texture resident\n", resident, NumTextures);
  52. }
  53. }
  54. /* render the textured quads */
  55. glClear( GL_COLOR_BUFFER_BIT );
  56. for (i = 0; i < NumTextures; i++) {
  57. GLint row = i / Columns;
  58. GLint col = i % Columns;
  59. GLfloat x = col * spacing + spacing * 0.5;
  60. GLfloat y = row * spacing + spacing * 0.5;
  61. glPushMatrix();
  62. glTranslatef(x, y, 0.0);
  63. glRotatef(Zrot, 0, 0, 1);
  64. glScalef(size, size, 1);
  65. glBindTexture(GL_TEXTURE_2D, TextureID[i]);
  66. glBegin(GL_POLYGON);
  67. glTexCoord2f(0, 0); glVertex2f(-1, -1);
  68. glTexCoord2f(1, 0); glVertex2f( 1, -1);
  69. glTexCoord2f(1, 1); glVertex2f( 1, 1);
  70. glTexCoord2f(0, 1); glVertex2f(-1, 1);
  71. glEnd();
  72. glPopMatrix();
  73. }
  74. glutSwapBuffers();
  75. }
  76. static void Reshape( int width, int height )
  77. {
  78. WinWidth = width;
  79. WinHeight = height;
  80. glViewport( 0, 0, width, height );
  81. glMatrixMode( GL_PROJECTION );
  82. glLoadIdentity();
  83. glOrtho(0, width, 0, height, -1, 1);
  84. glMatrixMode( GL_MODELVIEW );
  85. glLoadIdentity();
  86. }
  87. static void Init( void )
  88. {
  89. GLint i;
  90. if (RandomSize) {
  91. printf("Creating %d %s random-size textures, ", NumTextures,
  92. MipMap ? "Mipmapped" : "non-Mipmapped");
  93. }
  94. else {
  95. printf("Creating %d %s %d x %d textures, ", NumTextures,
  96. MipMap ? "Mipmapped" : "non-Mipmapped",
  97. TexWidth, TexHeight);
  98. }
  99. if (LinearFilter) {
  100. printf("bilinear filtering\n");
  101. }
  102. else {
  103. printf("nearest filtering\n");
  104. }
  105. /* compute number of rows and columns of rects */
  106. {
  107. GLfloat area = (GLfloat) (WinWidth * WinHeight) / (GLfloat) NumTextures;
  108. GLfloat edgeLen = sqrt(area);
  109. Columns = WinWidth / edgeLen;
  110. Rows = (NumTextures + Columns - 1) / Columns;
  111. printf("Rows: %d Cols: %d\n", Rows, Columns);
  112. }
  113. if (!TextureID) {
  114. TextureID = (GLuint *) malloc(sizeof(GLuint) * NumTextures);
  115. assert(TextureID);
  116. glGenTextures(NumTextures, TextureID);
  117. }
  118. if (!TextureResidency) {
  119. TextureResidency = (GLboolean *) malloc(sizeof(GLboolean) * NumTextures);
  120. assert(TextureResidency);
  121. }
  122. for (i = 0; i < NumTextures; i++) {
  123. GLubyte color[4];
  124. GLubyte *texImage;
  125. GLint j, row, col;
  126. row = i / Columns;
  127. col = i % Columns;
  128. glBindTexture(GL_TEXTURE_2D, TextureID[i]);
  129. if (i < LowPriorityCount)
  130. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 0.5F);
  131. if (RandomSize) {
  132. int k = (glutGet(GLUT_ELAPSED_TIME) % 7) + 2;
  133. TexWidth = 1 << k;
  134. TexHeight = 1 << k;
  135. }
  136. texImage = (GLubyte*) malloc(4 * TexWidth * TexHeight * sizeof(GLubyte));
  137. assert(texImage);
  138. /* determine texture color */
  139. color[0] = (GLint) (255.0 * ((float) col / (Columns - 1)));
  140. color[1] = 127;
  141. color[2] = (GLint) (255.0 * ((float) row / (Rows - 1)));
  142. color[3] = 255;
  143. /* fill in solid-colored teximage */
  144. for (j = 0; j < TexWidth * TexHeight; j++) {
  145. texImage[j*4+0] = color[0];
  146. texImage[j*4+1] = color[1];
  147. texImage[j*4+2] = color[2];
  148. texImage[j*4+3] = color[3];
  149. }
  150. if (MipMap) {
  151. GLint level = 0;
  152. GLint w = TexWidth, h = TexHeight;
  153. while (1) {
  154. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0,
  155. GL_RGBA, GL_UNSIGNED_BYTE, texImage);
  156. if (w == 1 && h == 1)
  157. break;
  158. if (w > 1)
  159. w /= 2;
  160. if (h > 1)
  161. h /= 2;
  162. level++;
  163. /*printf("%d: %d x %d\n", level, w, h);*/
  164. }
  165. if (LinearFilter) {
  166. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  167. GL_LINEAR_MIPMAP_LINEAR);
  168. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  169. }
  170. else {
  171. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  172. GL_NEAREST_MIPMAP_NEAREST);
  173. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  174. }
  175. }
  176. else {
  177. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TexWidth, TexHeight, 0,
  178. GL_RGBA, GL_UNSIGNED_BYTE, texImage);
  179. if (LinearFilter) {
  180. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  181. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  182. }
  183. else {
  184. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  185. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  186. }
  187. }
  188. free(texImage);
  189. }
  190. glEnable(GL_TEXTURE_2D);
  191. }
  192. static void Key( unsigned char key, int x, int y )
  193. {
  194. const GLfloat step = 3.0;
  195. (void) x;
  196. (void) y;
  197. switch (key) {
  198. case 'a':
  199. Anim = !Anim;
  200. if (Anim)
  201. glutIdleFunc(Idle);
  202. else
  203. glutIdleFunc(NULL);
  204. break;
  205. case 'z':
  206. Zrot -= step;
  207. break;
  208. case 'Z':
  209. Zrot += step;
  210. break;
  211. case ' ':
  212. Init();
  213. break;
  214. case 27:
  215. exit(0);
  216. break;
  217. }
  218. glutPostRedisplay();
  219. }
  220. int main( int argc, char *argv[] )
  221. {
  222. GLint i;
  223. glutInit( &argc, argv );
  224. glutInitWindowPosition( 0, 0 );
  225. glutInitWindowSize( WinWidth, WinHeight );
  226. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  227. glutCreateWindow(argv[0]);
  228. glutReshapeFunc( Reshape );
  229. glutKeyboardFunc( Key );
  230. glutDisplayFunc( Display );
  231. if (Anim)
  232. glutIdleFunc(Idle);
  233. for (i = 1; i < argc; i++) {
  234. if (strcmp(argv[i], "-n") == 0) {
  235. NumTextures = atoi(argv[i+1]);
  236. if (NumTextures <= 0) {
  237. printf("Error, bad number of textures\n");
  238. return 1;
  239. }
  240. i++;
  241. }
  242. else if (strcmp(argv[i], "-mipmap") == 0) {
  243. MipMap = GL_TRUE;
  244. }
  245. else if (strcmp(argv[i], "-linear") == 0) {
  246. LinearFilter = GL_TRUE;
  247. }
  248. else if (strcmp(argv[i], "-size") == 0) {
  249. TexWidth = atoi(argv[i+1]);
  250. TexHeight = atoi(argv[i+2]);
  251. assert(TexWidth >= 1);
  252. assert(TexHeight >= 1);
  253. i += 2;
  254. }
  255. else if (strcmp(argv[i], "-randomsize") == 0) {
  256. RandomSize = GL_TRUE;
  257. }
  258. else if (strcmp(argv[i], "-lowpri") == 0) {
  259. LowPriorityCount = atoi(argv[i+1]);
  260. i++;
  261. }
  262. else {
  263. printf("Usage:\n");
  264. printf(" manytex [options]\n");
  265. printf("Options:\n");
  266. printf(" -n <number of texture objects>\n");
  267. printf(" -size <width> <height> - specify texture size\n");
  268. printf(" -randomsize - use random size textures\n");
  269. printf(" -mipmap - generate mipmaps\n");
  270. printf(" -linear - use linear filtering instead of nearest\n");
  271. printf(" -lowpri <n> - Set lower priority on <n> textures\n");
  272. return 0;
  273. }
  274. }
  275. Init();
  276. glutMainLoop();
  277. return 0;
  278. }