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.

texcyl.c 6.6KB

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