Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * (C) Copyright IBM Corporation 2005
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * on the rights to use, copy, modify, merge, publish, distribute, sub
  9. * license, and/or sell copies of the Software, and to permit persons to whom
  10. * the Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19. * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
  20. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  21. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  22. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <math.h>
  28. #include <GL/glut.h>
  29. const GLenum filter_modes[] = {
  30. GL_NEAREST,
  31. GL_LINEAR,
  32. GL_NEAREST_MIPMAP_NEAREST,
  33. GL_NEAREST_MIPMAP_LINEAR,
  34. GL_LINEAR_MIPMAP_NEAREST,
  35. GL_LINEAR_MIPMAP_LINEAR,
  36. };
  37. static GLenum min_filter = GL_LINEAR_MIPMAP_LINEAR;
  38. static GLenum mag_filter = GL_LINEAR;
  39. static unsigned segments = 64;
  40. static GLfloat * position_data = NULL;
  41. static GLfloat * texcoord_data = NULL;
  42. static GLfloat max_anisotropy = 0.0;
  43. static GLfloat anisotropy = 1.0;
  44. static void generate_tunnel( unsigned num_segs, GLfloat ** pos_data,
  45. GLfloat ** tex_data );
  46. static void generate_textures( unsigned mode );
  47. #define min(a,b) ( (a) < (b) ) ? (a) : (b)
  48. #define max(a,b) ( (a) > (b) ) ? (a) : (b)
  49. static void Display( void )
  50. {
  51. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter );
  52. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter );
  53. if ( max_anisotropy > 0.0 ) {
  54. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
  55. anisotropy );
  56. }
  57. glClear( GL_COLOR_BUFFER_BIT );
  58. glLoadIdentity();
  59. glTranslatef( 0.0f, 0.0f, -19.0f );
  60. glVertexPointer( 4, GL_FLOAT, 0, position_data );
  61. glTexCoordPointer( 2, GL_FLOAT, 0, texcoord_data );
  62. glEnableClientState( GL_VERTEX_ARRAY );
  63. glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  64. glDrawArrays( GL_QUADS, 0, 4 * segments );
  65. glutSwapBuffers();
  66. }
  67. static void Reshape( int width, int height )
  68. {
  69. glViewport(0, 0, width, height);
  70. glMatrixMode(GL_PROJECTION);
  71. glLoadIdentity();
  72. gluPerspective(45.0f, (GLfloat)(width)/(GLfloat)(height), 0.1f, 100.0f);
  73. glMatrixMode(GL_MODELVIEW);
  74. glLoadIdentity();
  75. }
  76. static void Key( unsigned char key, int x, int y )
  77. {
  78. GLfloat new_anisotropy = anisotropy;
  79. (void) x;
  80. (void) y;
  81. switch( key ) {
  82. case 'a': {
  83. new_anisotropy = anisotropy - 1.0;
  84. break;
  85. }
  86. case 'A': {
  87. new_anisotropy = anisotropy + 1.0;
  88. break;
  89. }
  90. case 's': {
  91. segments--;
  92. if ( segments < 3 ) {
  93. segments = 3;
  94. }
  95. generate_tunnel( segments, & position_data, & texcoord_data );
  96. break;
  97. }
  98. case 'S': {
  99. segments++;
  100. if ( segments > 128 ) {
  101. segments = 128;
  102. }
  103. generate_tunnel( segments, & position_data, & texcoord_data );
  104. break;
  105. }
  106. case 'q':
  107. case 'Q':
  108. case 27:
  109. exit(0);
  110. break;
  111. }
  112. new_anisotropy = max( new_anisotropy, 1.0 );
  113. new_anisotropy = min( new_anisotropy, max_anisotropy );
  114. if ( new_anisotropy != anisotropy ) {
  115. anisotropy = new_anisotropy;
  116. printf( "Texture anisotropy: %f%s\n", anisotropy,
  117. (anisotropy == 1.0) ? " (disabled)" : "" );
  118. }
  119. glutPostRedisplay();
  120. }
  121. static void SpecialKey( int key, int x, int y )
  122. {
  123. (void) x;
  124. (void) y;
  125. (void) key;
  126. glutPostRedisplay();
  127. }
  128. static void menu_handler( int selection )
  129. {
  130. switch( selection >> 3 ) {
  131. case 0:
  132. glBindTexture( GL_TEXTURE_2D, selection );
  133. break;
  134. case 1:
  135. min_filter = filter_modes[ selection & 7 ];
  136. break;
  137. case 2:
  138. mag_filter = filter_modes[ selection & 7 ];
  139. break;
  140. }
  141. glutPostRedisplay();
  142. }
  143. static void Init( void )
  144. {
  145. glDisable(GL_CULL_FACE);
  146. glEnable(GL_TEXTURE_2D);
  147. glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
  148. glShadeModel(GL_SMOOTH);
  149. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  150. generate_tunnel( segments, & position_data, & texcoord_data );
  151. glBindTexture( GL_TEXTURE_2D, 1 );
  152. generate_textures(1);
  153. glBindTexture( GL_TEXTURE_2D, 2 );
  154. generate_textures(2);
  155. glBindTexture( GL_TEXTURE_2D, 3 );
  156. generate_textures(3);
  157. if ( glutExtensionSupported( "GL_EXT_texture_filter_anisotropic" ) ) {
  158. glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, & max_anisotropy );
  159. }
  160. printf("Maximum texture anisotropy: %f\n", max_anisotropy );
  161. /* Create the menus. */
  162. glutCreateMenu( menu_handler );
  163. glutAddMenuEntry( "Min filter: GL_NEAREST", 8 + 0 );
  164. glutAddMenuEntry( "Min filter: GL_LINEAR", 8 + 1 );
  165. glutAddMenuEntry( "Min filter: GL_NEAREST_MIMMAP_NEAREST", 8 + 2 );
  166. glutAddMenuEntry( "Min filter: GL_NEAREST_MIMMAP_LINEAR", 8 + 3 );
  167. glutAddMenuEntry( "Min filter: GL_LINEAR_MIMMAP_NEAREST", 8 + 4 );
  168. glutAddMenuEntry( "Min filter: GL_LINEAR_MIMMAP_LINEAR", 8 + 5 );
  169. glutAddMenuEntry( "Mag filter: GL_NEAREST", 16 + 0 );
  170. glutAddMenuEntry( "Mag filter: GL_LINEAR", 16 + 1 );
  171. glutAddMenuEntry( "Texture: regular mipmaps", 1 );
  172. glutAddMenuEntry( "Texture: blended mipmaps", 2 );
  173. glutAddMenuEntry( "Texture: color mipmaps", 3 );
  174. glutAttachMenu( GLUT_RIGHT_BUTTON );
  175. }
  176. static void generate_tunnel( unsigned num_segs, GLfloat ** pos_data,
  177. GLfloat ** tex_data )
  178. {
  179. const GLfloat far = 20.0f;
  180. const GLfloat near = -90.0f;
  181. const GLfloat far_tex = 30.0f;
  182. const GLfloat near_tex = 0.0f;
  183. const GLfloat angle_step = (2 * M_PI) / num_segs;
  184. const GLfloat tex_coord_step = 2.0 / num_segs;
  185. GLfloat angle = 0.0f;
  186. GLfloat tex_coord = 0.0f;
  187. unsigned i;
  188. GLfloat * position;
  189. GLfloat * texture;
  190. position = realloc( *pos_data, sizeof( GLfloat ) * num_segs * 4 * 4 );
  191. texture = realloc( *tex_data, sizeof( GLfloat ) * num_segs * 4 * 2 );
  192. *pos_data = position;
  193. *tex_data = texture;
  194. for ( i = 0 ; i < num_segs ; i++ ) {
  195. position[0] = 2.5 * sinf( angle );
  196. position[1] = 2.5 * cosf( angle );
  197. position[2] = (i & 1) ? far : near;
  198. position[3] = 1.0f;
  199. position[4] = position[0];
  200. position[5] = position[1];
  201. position[6] = (i & 1) ? near : far;
  202. position[7] = 1.0f;
  203. position += 8;
  204. texture[0] = tex_coord;
  205. texture[1] = (i & 1) ? far_tex : near_tex;
  206. texture += 2;
  207. texture[0] = tex_coord;
  208. texture[1] = (i & 1) ? near_tex : far_tex;
  209. texture += 2;
  210. angle += angle_step;
  211. tex_coord += tex_coord_step;
  212. position[0] = 2.5 * sinf( angle );
  213. position[1] = 2.5 * cosf( angle );
  214. position[2] = (i & 1) ? near : far;
  215. position[3] = 1.0f;
  216. position[4] = position[0];
  217. position[5] = position[1];
  218. position[6] = (i & 1) ? far : near;
  219. position[7] = 1.0f;
  220. position += 8;
  221. texture[0] = tex_coord;
  222. texture[1] = (i & 1) ? near_tex : far_tex;
  223. texture += 2;
  224. texture[0] = tex_coord;
  225. texture[1] = (i & 1) ? far_tex : near_tex;
  226. texture += 2;
  227. }
  228. }
  229. static void generate_textures( unsigned mode )
  230. {
  231. #define LEVEL_COLORS 6
  232. const GLfloat colors[LEVEL_COLORS][3] = {
  233. { 1.0, 0.0, 0.0 }, /* 32 x 32 */
  234. { 0.0, 1.0, 0.0 }, /* 16 x 16 */
  235. { 0.0, 0.0, 1.0 }, /* 8 x 8 */
  236. { 1.0, 0.0, 1.0 }, /* 4 x 4 */
  237. { 1.0, 1.0, 1.0 }, /* 2 x 2 */
  238. { 1.0, 1.0, 0.0 } /* 1 x 1 */
  239. };
  240. const unsigned checkers_per_level = 2;
  241. GLfloat * tex;
  242. unsigned level;
  243. unsigned size;
  244. GLint max_size;
  245. glGetIntegerv( GL_MAX_TEXTURE_SIZE, & max_size );
  246. if ( max_size > 512 ) {
  247. max_size = 512;
  248. }
  249. tex = malloc( sizeof( GLfloat ) * 3 * max_size * max_size );
  250. level = 0;
  251. for ( size = max_size ; size > 0 ; size >>= 1 ) {
  252. unsigned divisor = size / checkers_per_level;
  253. unsigned i;
  254. unsigned j;
  255. GLfloat checkers[2][3];
  256. if ((level == 0) || (mode == 1)) {
  257. checkers[0][0] = 1.0;
  258. checkers[0][1] = 1.0;
  259. checkers[0][2] = 1.0;
  260. checkers[1][0] = 0.0;
  261. checkers[1][1] = 0.0;
  262. checkers[1][2] = 0.0;
  263. }
  264. else if (mode == 2) {
  265. checkers[0][0] = colors[level % LEVEL_COLORS][0];
  266. checkers[0][1] = colors[level % LEVEL_COLORS][1];
  267. checkers[0][2] = colors[level % LEVEL_COLORS][2];
  268. checkers[1][0] = colors[level % LEVEL_COLORS][0] * 0.5;
  269. checkers[1][1] = colors[level % LEVEL_COLORS][1] * 0.5;
  270. checkers[1][2] = colors[level % LEVEL_COLORS][2] * 0.5;
  271. }
  272. else {
  273. checkers[0][0] = colors[level % LEVEL_COLORS][0];
  274. checkers[0][1] = colors[level % LEVEL_COLORS][1];
  275. checkers[0][2] = colors[level % LEVEL_COLORS][2];
  276. checkers[1][0] = colors[level % LEVEL_COLORS][0];
  277. checkers[1][1] = colors[level % LEVEL_COLORS][1];
  278. checkers[1][2] = colors[level % LEVEL_COLORS][2];
  279. }
  280. if ( divisor == 0 ) {
  281. divisor = 1;
  282. checkers[0][0] = (checkers[0][0] + checkers[1][0]) / 2;
  283. checkers[0][1] = (checkers[0][0] + checkers[1][0]) / 2;
  284. checkers[0][2] = (checkers[0][0] + checkers[1][0]) / 2;
  285. checkers[1][0] = checkers[0][0];
  286. checkers[1][1] = checkers[0][1];
  287. checkers[1][2] = checkers[0][2];
  288. }
  289. for ( i = 0 ; i < size ; i++ ) {
  290. for ( j = 0 ; j < size ; j++ ) {
  291. const unsigned idx = ((i ^ j) / divisor) & 1;
  292. tex[ ((i * size) + j) * 3 + 0] = checkers[ idx ][0];
  293. tex[ ((i * size) + j) * 3 + 1] = checkers[ idx ][1];
  294. tex[ ((i * size) + j) * 3 + 2] = checkers[ idx ][2];
  295. }
  296. }
  297. glTexImage2D( GL_TEXTURE_2D, level, GL_RGB, size, size, 0,
  298. GL_RGB, GL_FLOAT, tex );
  299. level++;
  300. }
  301. free( tex );
  302. }
  303. int main( int argc, char ** argv )
  304. {
  305. glutInit( &argc, argv );
  306. glutInitWindowPosition( 0, 0 );
  307. glutInitWindowSize( 800, 600 );
  308. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  309. glutCreateWindow( "Texture Filter Test" );
  310. glutReshapeFunc( Reshape );
  311. glutKeyboardFunc( Key );
  312. glutSpecialFunc( SpecialKey );
  313. glutDisplayFunc( Display );
  314. Init();
  315. printf("\nUse the right-button menu to select the texture and filter mode.\n");
  316. printf("Use 'A' and 'a' to increase and decrease the aniotropy.\n");
  317. printf("Use 'S' and 's' to increase and decrease the number of cylinder segments.\n");
  318. printf("Use 'q' to exit.\n\n");
  319. glutMainLoop();
  320. return 0;
  321. }