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

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