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

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