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 5.6KB

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