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.0KB

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