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

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