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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* $Id: texcyl.c,v 1.4 2000/12/24 22:53:54 pesco Exp $ */
  2. /*
  3. * Textured cylinder demo: lighting, texturing, reflection mapping.
  4. *
  5. * Command line options:
  6. * -info print GL implementation information
  7. *
  8. *
  9. * Brian Paul May 1997 This program is in the public domain.
  10. */
  11. /*
  12. * $Log: texcyl.c,v $
  13. * Revision 1.4 2000/12/24 22:53:54 pesco
  14. * * demos/Makefile.am (INCLUDES): Added -I$(top_srcdir)/util.
  15. * * demos/Makefile.X11, demos/Makefile.BeOS-R4, demos/Makefile.cygnus:
  16. * Essentially the same.
  17. * Program files updated to include "readtex.c", not "../util/readtex.c".
  18. * * demos/reflect.c: Likewise for "showbuffer.c".
  19. *
  20. *
  21. * * Makefile.am (EXTRA_DIST): Added top-level regular files.
  22. *
  23. * * include/GL/Makefile.am (INC_X11): Added glxext.h.
  24. *
  25. *
  26. * * src/GGI/include/ggi/mesa/Makefile.am (EXTRA_HEADERS): Include
  27. * Mesa GGI headers in dist even if HAVE_GGI is not given.
  28. *
  29. * * configure.in: Look for GLUT and demo source dirs in $srcdir.
  30. *
  31. * * src/swrast/Makefile.am (libMesaSwrast_la_SOURCES): Set to *.[ch].
  32. * More source list updates in various Makefile.am's.
  33. *
  34. * * Makefile.am (dist-hook): Remove CVS directory from distribution.
  35. * (DIST_SUBDIRS): List all possible subdirs here.
  36. * (SUBDIRS): Only list subdirs selected for build again.
  37. * The above two applied to all subdir Makefile.am's also.
  38. *
  39. * Revision 1.3 2000/09/29 23:09:39 brianp
  40. * added fps output
  41. *
  42. * Revision 1.2 1999/10/21 16:39:06 brianp
  43. * added -info command line option
  44. *
  45. * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
  46. * Imported sources
  47. *
  48. * Revision 3.3 1999/03/28 18:24:37 brianp
  49. * minor clean-up
  50. *
  51. * Revision 3.2 1998/11/05 04:34:04 brianp
  52. * moved image files to ../images/ directory
  53. *
  54. * Revision 3.1 1998/06/23 03:16:51 brianp
  55. * added Point/Linear sampling menu items
  56. *
  57. * Revision 3.0 1998/02/14 18:42:29 brianp
  58. * initial rev
  59. *
  60. */
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include <math.h>
  64. #include <GL/glut.h>
  65. #include "readtex.c" /* I know, this is a hack. */
  66. #define TEXTURE_FILE "../images/reflect.rgb"
  67. #define LIT 1
  68. #define TEXTURED 2
  69. #define REFLECT 3
  70. #define ANIMATE 10
  71. #define POINT_FILTER 20
  72. #define LINEAR_FILTER 21
  73. #define QUIT 100
  74. static GLuint CylinderObj = 0;
  75. static GLboolean Animate = GL_TRUE;
  76. static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
  77. static GLfloat DXrot = 1.0, DYrot = 2.5;
  78. /* performance info */
  79. static GLint T0 = 0;
  80. static GLint Frames = 0;
  81. static void Idle( void )
  82. {
  83. if (Animate) {
  84. Xrot += DXrot;
  85. Yrot += DYrot;
  86. glutPostRedisplay();
  87. }
  88. }
  89. static void Display( void )
  90. {
  91. glClear( GL_COLOR_BUFFER_BIT );
  92. glPushMatrix();
  93. glRotatef(Xrot, 1.0, 0.0, 0.0);
  94. glRotatef(Yrot, 0.0, 1.0, 0.0);
  95. glRotatef(Zrot, 0.0, 0.0, 1.0);
  96. glScalef(5.0, 5.0, 5.0);
  97. glCallList(CylinderObj);
  98. glPopMatrix();
  99. glutSwapBuffers();
  100. if (Animate) {
  101. GLint t = glutGet(GLUT_ELAPSED_TIME);
  102. Frames++;
  103. if (t - T0 >= 5000) {
  104. GLfloat seconds = (t - T0) / 1000.0;
  105. GLfloat fps = Frames / seconds;
  106. printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
  107. T0 = t;
  108. Frames = 0;
  109. }
  110. }
  111. }
  112. static void Reshape( int width, int height )
  113. {
  114. glViewport( 0, 0, width, height );
  115. glMatrixMode( GL_PROJECTION );
  116. glLoadIdentity();
  117. glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
  118. glMatrixMode( GL_MODELVIEW );
  119. glLoadIdentity();
  120. glTranslatef( 0.0, 0.0, -70.0 );
  121. }
  122. static void SetMode(GLuint m)
  123. {
  124. /* disable everything */
  125. glDisable(GL_LIGHTING);
  126. glDisable(GL_TEXTURE_2D);
  127. glDisable(GL_TEXTURE_GEN_S);
  128. glDisable(GL_TEXTURE_GEN_T);
  129. /* enable what's needed */
  130. if (m==LIT) {
  131. glEnable(GL_LIGHTING);
  132. }
  133. else if (m==TEXTURED) {
  134. glEnable(GL_TEXTURE_2D);
  135. }
  136. else if (m==REFLECT) {
  137. glEnable(GL_TEXTURE_2D);
  138. glEnable(GL_TEXTURE_GEN_S);
  139. glEnable(GL_TEXTURE_GEN_T);
  140. }
  141. }
  142. static void ModeMenu(int entry)
  143. {
  144. if (entry==ANIMATE) {
  145. Animate = !Animate;
  146. }
  147. else if (entry==POINT_FILTER) {
  148. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  149. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  150. }
  151. else if (entry==LINEAR_FILTER) {
  152. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  153. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  154. }
  155. else if (entry==QUIT) {
  156. exit(0);
  157. }
  158. else {
  159. SetMode(entry);
  160. }
  161. glutPostRedisplay();
  162. }
  163. static void Key( unsigned char key, int x, int y )
  164. {
  165. (void) x;
  166. (void) y;
  167. switch (key) {
  168. case 27:
  169. exit(0);
  170. break;
  171. }
  172. glutPostRedisplay();
  173. }
  174. static void SpecialKey( int key, int x, int y )
  175. {
  176. float step = 3.0;
  177. (void) x;
  178. (void) y;
  179. switch (key) {
  180. case GLUT_KEY_UP:
  181. Xrot += step;
  182. break;
  183. case GLUT_KEY_DOWN:
  184. Xrot -= step;
  185. break;
  186. case GLUT_KEY_LEFT:
  187. Yrot += step;
  188. break;
  189. case GLUT_KEY_RIGHT:
  190. Yrot -= step;
  191. break;
  192. }
  193. glutPostRedisplay();
  194. }
  195. static void Init( int argc, char *argv[] )
  196. {
  197. GLUquadricObj *q = gluNewQuadric();
  198. CylinderObj = glGenLists(1);
  199. glNewList(CylinderObj, GL_COMPILE);
  200. glTranslatef(0.0, 0.0, -1.0);
  201. /* cylinder */
  202. gluQuadricNormals(q, GL_SMOOTH);
  203. gluQuadricTexture(q, GL_TRUE);
  204. gluCylinder(q, 0.6, 0.6, 2.0, 24, 1);
  205. /* end cap */
  206. glTranslatef(0.0, 0.0, 2.0);
  207. gluDisk(q, 0.0, 0.6, 24, 1);
  208. /* other end cap */
  209. glTranslatef(0.0, 0.0, -2.0);
  210. gluQuadricOrientation(q, GLU_INSIDE);
  211. gluDisk(q, 0.0, 0.6, 24, 1);
  212. glEndList();
  213. gluDeleteQuadric(q);
  214. /* lighting */
  215. glEnable(GL_LIGHTING);
  216. {
  217. GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
  218. GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
  219. GLfloat teal[4] = { 0.0, 1.0, 0.8, 1.0 };
  220. glMaterialfv(GL_FRONT, GL_DIFFUSE, teal);
  221. glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
  222. glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
  223. glEnable(GL_LIGHT0);
  224. }
  225. /* fitering = nearest, initially */
  226. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  227. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  228. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  229. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  230. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  231. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  232. if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
  233. printf("Error: couldn't load texture image\n");
  234. exit(1);
  235. }
  236. glEnable(GL_CULL_FACE); /* don't need Z testing for convex objects */
  237. SetMode(LIT);
  238. if (argc > 1 && strcmp(argv[1], "-info")==0) {
  239. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  240. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  241. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  242. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  243. }
  244. }
  245. int main( int argc, char *argv[] )
  246. {
  247. glutInit( &argc, argv );
  248. glutInitWindowSize( 400, 400 );
  249. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  250. glutCreateWindow(argv[0] );
  251. Init(argc, argv);
  252. glutReshapeFunc( Reshape );
  253. glutKeyboardFunc( Key );
  254. glutSpecialFunc( SpecialKey );
  255. glutDisplayFunc( Display );
  256. glutIdleFunc( Idle );
  257. glutCreateMenu(ModeMenu);
  258. glutAddMenuEntry("Lit", LIT);
  259. glutAddMenuEntry("Textured", TEXTURED);
  260. glutAddMenuEntry("Reflect", REFLECT);
  261. glutAddMenuEntry("Point Filtered", POINT_FILTER);
  262. glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
  263. glutAddMenuEntry("Toggle Animation", ANIMATE);
  264. glutAddMenuEntry("Quit", QUIT);
  265. glutAttachMenu(GLUT_RIGHT_BUTTON);
  266. glutMainLoop();
  267. return 0;
  268. }