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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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/glew.h>
  29. #include <GL/glut.h>
  30. const GLenum filter_modes[] = {
  31. GL_NEAREST,
  32. GL_LINEAR,
  33. GL_NEAREST_MIPMAP_NEAREST,
  34. GL_NEAREST_MIPMAP_LINEAR,
  35. GL_LINEAR_MIPMAP_NEAREST,
  36. GL_LINEAR_MIPMAP_LINEAR,
  37. };
  38. static GLenum min_filter = GL_LINEAR_MIPMAP_LINEAR;
  39. static GLenum mag_filter = GL_LINEAR;
  40. static unsigned segments = 64;
  41. static GLfloat * position_data = NULL;
  42. static GLfloat * texcoord_data = NULL;
  43. static GLfloat max_anisotropy = 0.0;
  44. static GLfloat anisotropy = 1.0;
  45. static void generate_tunnel( unsigned num_segs, GLfloat ** pos_data,
  46. GLfloat ** tex_data );
  47. static void generate_textures( unsigned mode );
  48. #define min(a,b) ( (a) < (b) ) ? (a) : (b)
  49. #define max(a,b) ( (a) > (b) ) ? (a) : (b)
  50. static void Display( void )
  51. {
  52. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter );
  53. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter );
  54. if ( max_anisotropy > 0.0 ) {
  55. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
  56. anisotropy );
  57. }
  58. glClear( GL_COLOR_BUFFER_BIT );
  59. glLoadIdentity();
  60. glTranslatef( 0.0f, 0.0f, -19.0f );
  61. glVertexPointer( 4, GL_FLOAT, 0, position_data );
  62. glTexCoordPointer( 2, GL_FLOAT, 0, texcoord_data );
  63. glEnableClientState( GL_VERTEX_ARRAY );
  64. glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  65. glDrawArrays( GL_QUADS, 0, 4 * segments );
  66. glutSwapBuffers();
  67. }
  68. static void Reshape( int width, int height )
  69. {
  70. glViewport(0, 0, width, height);
  71. glMatrixMode(GL_PROJECTION);
  72. glLoadIdentity();
  73. gluPerspective(45.0f, (GLfloat)(width)/(GLfloat)(height), 0.1f, 100.0f);
  74. glMatrixMode(GL_MODELVIEW);
  75. glLoadIdentity();
  76. }
  77. static void Key( unsigned char key, int x, int y )
  78. {
  79. GLfloat new_anisotropy = anisotropy;
  80. (void) x;
  81. (void) y;
  82. switch( key ) {
  83. case 'a': {
  84. new_anisotropy = anisotropy - 1.0;
  85. break;
  86. }
  87. case 'A': {
  88. new_anisotropy = anisotropy + 1.0;
  89. break;
  90. }
  91. case 's': {
  92. segments--;
  93. if ( segments < 3 ) {
  94. segments = 3;
  95. }
  96. generate_tunnel( segments, & position_data, & texcoord_data );
  97. break;
  98. }
  99. case 'S': {
  100. segments++;
  101. if ( segments > 128 ) {
  102. segments = 128;
  103. }
  104. generate_tunnel( segments, & position_data, & texcoord_data );
  105. break;
  106. }
  107. case 'q':
  108. case 'Q':
  109. case 27:
  110. exit(0);
  111. break;
  112. }
  113. new_anisotropy = max( new_anisotropy, 1.0 );
  114. new_anisotropy = min( new_anisotropy, max_anisotropy );
  115. if ( new_anisotropy != anisotropy ) {
  116. anisotropy = new_anisotropy;
  117. printf( "Texture anisotropy: %f%s\n", anisotropy,
  118. (anisotropy == 1.0) ? " (disabled)" : "" );
  119. }
  120. glutPostRedisplay();
  121. }
  122. static void SpecialKey( int key, int x, int y )
  123. {
  124. (void) x;
  125. (void) y;
  126. (void) key;
  127. glutPostRedisplay();
  128. }
  129. static void menu_handler( int selection )
  130. {
  131. switch( selection >> 3 ) {
  132. case 0:
  133. glBindTexture( GL_TEXTURE_2D, selection );
  134. break;
  135. case 1:
  136. min_filter = filter_modes[ selection & 7 ];
  137. break;
  138. case 2:
  139. mag_filter = filter_modes[ selection & 7 ];
  140. break;
  141. }
  142. glutPostRedisplay();
  143. }
  144. static void Init( void )
  145. {
  146. glDisable(GL_CULL_FACE);
  147. glEnable(GL_TEXTURE_2D);
  148. glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
  149. glShadeModel(GL_SMOOTH);
  150. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  151. generate_tunnel( segments, & position_data, & texcoord_data );
  152. glBindTexture( GL_TEXTURE_2D, 1 );
  153. generate_textures(1);
  154. glBindTexture( GL_TEXTURE_2D, 2 );
  155. generate_textures(2);
  156. glBindTexture( GL_TEXTURE_2D, 3 );
  157. generate_textures(3);
  158. if ( glutExtensionSupported( "GL_EXT_texture_filter_anisotropic" ) ) {
  159. glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, & max_anisotropy );
  160. }
  161. printf("Maximum texture anisotropy: %f\n", max_anisotropy );
  162. /* Create the menus. */
  163. glutCreateMenu( menu_handler );
  164. glutAddMenuEntry( "Min filter: GL_NEAREST", 8 + 0 );
  165. glutAddMenuEntry( "Min filter: GL_LINEAR", 8 + 1 );
  166. glutAddMenuEntry( "Min filter: GL_NEAREST_MIMMAP_NEAREST", 8 + 2 );
  167. glutAddMenuEntry( "Min filter: GL_NEAREST_MIMMAP_LINEAR", 8 + 3 );
  168. glutAddMenuEntry( "Min filter: GL_LINEAR_MIMMAP_NEAREST", 8 + 4 );
  169. glutAddMenuEntry( "Min filter: GL_LINEAR_MIMMAP_LINEAR", 8 + 5 );
  170. glutAddMenuEntry( "Mag filter: GL_NEAREST", 16 + 0 );
  171. glutAddMenuEntry( "Mag filter: GL_LINEAR", 16 + 1 );
  172. glutAddMenuEntry( "Texture: regular mipmaps", 1 );
  173. glutAddMenuEntry( "Texture: blended mipmaps", 2 );
  174. glutAddMenuEntry( "Texture: color mipmaps", 3 );
  175. glutAttachMenu( GLUT_RIGHT_BUTTON );
  176. }
  177. static void generate_tunnel( unsigned num_segs, GLfloat ** pos_data,
  178. GLfloat ** tex_data )
  179. {
  180. const GLfloat far_distance = 20.0f;
  181. const GLfloat near_distance = -90.0f;
  182. const GLfloat far_tex = 30.0f;
  183. const GLfloat near_tex = 0.0f;
  184. const GLfloat angle_step = (2 * M_PI) / num_segs;
  185. const GLfloat tex_coord_step = 2.0 / num_segs;
  186. GLfloat angle = 0.0f;
  187. GLfloat tex_coord = 0.0f;
  188. unsigned i;
  189. GLfloat * position;
  190. GLfloat * texture;
  191. position = realloc( *pos_data, sizeof( GLfloat ) * num_segs * 4 * 4 );
  192. texture = realloc( *tex_data, sizeof( GLfloat ) * num_segs * 4 * 2 );
  193. *pos_data = position;
  194. *tex_data = texture;
  195. for ( i = 0 ; i < num_segs ; i++ ) {
  196. position[0] = 2.5 * sinf( angle );
  197. position[1] = 2.5 * cosf( angle );
  198. position[2] = (i & 1) ? far_distance : near_distance;
  199. position[3] = 1.0f;
  200. position[4] = position[0];
  201. position[5] = position[1];
  202. position[6] = (i & 1) ? near_distance : far_distance;
  203. position[7] = 1.0f;
  204. position += 8;
  205. texture[0] = tex_coord;
  206. texture[1] = (i & 1) ? far_tex : near_tex;
  207. texture += 2;
  208. texture[0] = tex_coord;
  209. texture[1] = (i & 1) ? near_tex : far_tex;
  210. texture += 2;
  211. angle += angle_step;
  212. tex_coord += tex_coord_step;
  213. position[0] = 2.5 * sinf( angle );
  214. position[1] = 2.5 * cosf( angle );
  215. position[2] = (i & 1) ? near_distance : far_distance;
  216. position[3] = 1.0f;
  217. position[4] = position[0];
  218. position[5] = position[1];
  219. position[6] = (i & 1) ? far_distance : near_distance;
  220. position[7] = 1.0f;
  221. position += 8;
  222. texture[0] = tex_coord;
  223. texture[1] = (i & 1) ? near_tex : far_tex;
  224. texture += 2;
  225. texture[0] = tex_coord;
  226. texture[1] = (i & 1) ? far_tex : near_tex;
  227. texture += 2;
  228. }
  229. }
  230. static void generate_textures( unsigned mode )
  231. {
  232. #define LEVEL_COLORS 6
  233. const GLfloat colors[LEVEL_COLORS][3] = {
  234. { 1.0, 0.0, 0.0 }, /* 32 x 32 */
  235. { 0.0, 1.0, 0.0 }, /* 16 x 16 */
  236. { 0.0, 0.0, 1.0 }, /* 8 x 8 */
  237. { 1.0, 0.0, 1.0 }, /* 4 x 4 */
  238. { 1.0, 1.0, 1.0 }, /* 2 x 2 */
  239. { 1.0, 1.0, 0.0 } /* 1 x 1 */
  240. };
  241. const unsigned checkers_per_level = 2;
  242. GLfloat * tex;
  243. unsigned level;
  244. unsigned size;
  245. GLint max_size;
  246. glGetIntegerv( GL_MAX_TEXTURE_SIZE, & max_size );
  247. if ( max_size > 512 ) {
  248. max_size = 512;
  249. }
  250. tex = malloc( sizeof( GLfloat ) * 3 * max_size * max_size );
  251. level = 0;
  252. for ( size = max_size ; size > 0 ; size >>= 1 ) {
  253. unsigned divisor = size / checkers_per_level;
  254. unsigned i;
  255. unsigned j;
  256. GLfloat checkers[2][3];
  257. if ((level == 0) || (mode == 1)) {
  258. checkers[0][0] = 1.0;
  259. checkers[0][1] = 1.0;
  260. checkers[0][2] = 1.0;
  261. checkers[1][0] = 0.0;
  262. checkers[1][1] = 0.0;
  263. checkers[1][2] = 0.0;
  264. }
  265. else if (mode == 2) {
  266. checkers[0][0] = colors[level % LEVEL_COLORS][0];
  267. checkers[0][1] = colors[level % LEVEL_COLORS][1];
  268. checkers[0][2] = colors[level % LEVEL_COLORS][2];
  269. checkers[1][0] = colors[level % LEVEL_COLORS][0] * 0.5;
  270. checkers[1][1] = colors[level % LEVEL_COLORS][1] * 0.5;
  271. checkers[1][2] = colors[level % LEVEL_COLORS][2] * 0.5;
  272. }
  273. else {
  274. checkers[0][0] = colors[level % LEVEL_COLORS][0];
  275. checkers[0][1] = colors[level % LEVEL_COLORS][1];
  276. checkers[0][2] = colors[level % LEVEL_COLORS][2];
  277. checkers[1][0] = colors[level % LEVEL_COLORS][0];
  278. checkers[1][1] = colors[level % LEVEL_COLORS][1];
  279. checkers[1][2] = colors[level % LEVEL_COLORS][2];
  280. }
  281. if ( divisor == 0 ) {
  282. divisor = 1;
  283. checkers[0][0] = (checkers[0][0] + checkers[1][0]) / 2;
  284. checkers[0][1] = (checkers[0][0] + checkers[1][0]) / 2;
  285. checkers[0][2] = (checkers[0][0] + checkers[1][0]) / 2;
  286. checkers[1][0] = checkers[0][0];
  287. checkers[1][1] = checkers[0][1];
  288. checkers[1][2] = checkers[0][2];
  289. }
  290. for ( i = 0 ; i < size ; i++ ) {
  291. for ( j = 0 ; j < size ; j++ ) {
  292. const unsigned idx = ((i ^ j) / divisor) & 1;
  293. tex[ ((i * size) + j) * 3 + 0] = checkers[ idx ][0];
  294. tex[ ((i * size) + j) * 3 + 1] = checkers[ idx ][1];
  295. tex[ ((i * size) + j) * 3 + 2] = checkers[ idx ][2];
  296. }
  297. }
  298. glTexImage2D( GL_TEXTURE_2D, level, GL_RGB, size, size, 0,
  299. GL_RGB, GL_FLOAT, tex );
  300. level++;
  301. }
  302. free( tex );
  303. }
  304. int main( int argc, char ** argv )
  305. {
  306. glutInit( &argc, argv );
  307. glutInitWindowPosition( 0, 0 );
  308. glutInitWindowSize( 800, 600 );
  309. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  310. glutCreateWindow( "Texture Filter Test" );
  311. glewInit();
  312. glutReshapeFunc( Reshape );
  313. glutKeyboardFunc( Key );
  314. glutSpecialFunc( SpecialKey );
  315. glutDisplayFunc( Display );
  316. Init();
  317. printf("\nUse the right-button menu to select the texture and filter mode.\n");
  318. printf("Use 'A' and 'a' to increase and decrease the aniotropy.\n");
  319. printf("Use 'S' and 's' to increase and decrease the number of cylinder segments.\n");
  320. printf("Use 'q' to exit.\n\n");
  321. glutMainLoop();
  322. return 0;
  323. }