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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* $Id: texcyl.c,v 1.5 2001/03/27 17:35:26 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.5 2001/03/27 17:35:26 brianp
  14. * set initial window pos
  15. *
  16. * Revision 1.4 2000/12/24 22:53:54 pesco
  17. * * demos/Makefile.am (INCLUDES): Added -I$(top_srcdir)/util.
  18. * * demos/Makefile.X11, demos/Makefile.BeOS-R4, demos/Makefile.cygnus:
  19. * Essentially the same.
  20. * Program files updated to include "readtex.c", not "../util/readtex.c".
  21. * * demos/reflect.c: Likewise for "showbuffer.c".
  22. *
  23. *
  24. * * Makefile.am (EXTRA_DIST): Added top-level regular files.
  25. *
  26. * * include/GL/Makefile.am (INC_X11): Added glxext.h.
  27. *
  28. *
  29. * * src/GGI/include/ggi/mesa/Makefile.am (EXTRA_HEADERS): Include
  30. * Mesa GGI headers in dist even if HAVE_GGI is not given.
  31. *
  32. * * configure.in: Look for GLUT and demo source dirs in $srcdir.
  33. *
  34. * * src/swrast/Makefile.am (libMesaSwrast_la_SOURCES): Set to *.[ch].
  35. * More source list updates in various Makefile.am's.
  36. *
  37. * * Makefile.am (dist-hook): Remove CVS directory from distribution.
  38. * (DIST_SUBDIRS): List all possible subdirs here.
  39. * (SUBDIRS): Only list subdirs selected for build again.
  40. * The above two applied to all subdir Makefile.am's also.
  41. *
  42. * Revision 1.3 2000/09/29 23:09:39 brianp
  43. * added fps output
  44. *
  45. * Revision 1.2 1999/10/21 16:39:06 brianp
  46. * added -info command line option
  47. *
  48. * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
  49. * Imported sources
  50. *
  51. * Revision 3.3 1999/03/28 18:24:37 brianp
  52. * minor clean-up
  53. *
  54. * Revision 3.2 1998/11/05 04:34:04 brianp
  55. * moved image files to ../images/ directory
  56. *
  57. * Revision 3.1 1998/06/23 03:16:51 brianp
  58. * added Point/Linear sampling menu items
  59. *
  60. * Revision 3.0 1998/02/14 18:42:29 brianp
  61. * initial rev
  62. *
  63. */
  64. #include <stdio.h>
  65. #include <stdlib.h>
  66. #include <math.h>
  67. #include <GL/glut.h>
  68. #include "readtex.c" /* I know, this is a hack. */
  69. #define TEXTURE_FILE "../images/reflect.rgb"
  70. #define LIT 1
  71. #define TEXTURED 2
  72. #define REFLECT 3
  73. #define ANIMATE 10
  74. #define POINT_FILTER 20
  75. #define LINEAR_FILTER 21
  76. #define QUIT 100
  77. static GLuint CylinderObj = 0;
  78. static GLboolean Animate = GL_TRUE;
  79. static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
  80. static GLfloat DXrot = 1.0, DYrot = 2.5;
  81. /* performance info */
  82. static GLint T0 = 0;
  83. static GLint Frames = 0;
  84. static void Idle( void )
  85. {
  86. if (Animate) {
  87. Xrot += DXrot;
  88. Yrot += DYrot;
  89. glutPostRedisplay();
  90. }
  91. }
  92. static void Display( void )
  93. {
  94. glClear( GL_COLOR_BUFFER_BIT );
  95. glPushMatrix();
  96. glRotatef(Xrot, 1.0, 0.0, 0.0);
  97. glRotatef(Yrot, 0.0, 1.0, 0.0);
  98. glRotatef(Zrot, 0.0, 0.0, 1.0);
  99. glScalef(5.0, 5.0, 5.0);
  100. glCallList(CylinderObj);
  101. glPopMatrix();
  102. glutSwapBuffers();
  103. if (Animate) {
  104. GLint t = glutGet(GLUT_ELAPSED_TIME);
  105. Frames++;
  106. if (t - T0 >= 5000) {
  107. GLfloat seconds = (t - T0) / 1000.0;
  108. GLfloat fps = Frames / seconds;
  109. printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
  110. T0 = t;
  111. Frames = 0;
  112. }
  113. }
  114. }
  115. static void Reshape( int width, int height )
  116. {
  117. glViewport( 0, 0, width, height );
  118. glMatrixMode( GL_PROJECTION );
  119. glLoadIdentity();
  120. glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
  121. glMatrixMode( GL_MODELVIEW );
  122. glLoadIdentity();
  123. glTranslatef( 0.0, 0.0, -70.0 );
  124. }
  125. static void SetMode(GLuint m)
  126. {
  127. /* disable everything */
  128. glDisable(GL_LIGHTING);
  129. glDisable(GL_TEXTURE_2D);
  130. glDisable(GL_TEXTURE_GEN_S);
  131. glDisable(GL_TEXTURE_GEN_T);
  132. /* enable what's needed */
  133. if (m==LIT) {
  134. glEnable(GL_LIGHTING);
  135. }
  136. else if (m==TEXTURED) {
  137. glEnable(GL_TEXTURE_2D);
  138. }
  139. else if (m==REFLECT) {
  140. glEnable(GL_TEXTURE_2D);
  141. glEnable(GL_TEXTURE_GEN_S);
  142. glEnable(GL_TEXTURE_GEN_T);
  143. }
  144. }
  145. static void ModeMenu(int entry)
  146. {
  147. if (entry==ANIMATE) {
  148. Animate = !Animate;
  149. }
  150. else if (entry==POINT_FILTER) {
  151. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  152. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  153. }
  154. else if (entry==LINEAR_FILTER) {
  155. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  156. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  157. }
  158. else if (entry==QUIT) {
  159. exit(0);
  160. }
  161. else {
  162. SetMode(entry);
  163. }
  164. glutPostRedisplay();
  165. }
  166. static void Key( unsigned char key, int x, int y )
  167. {
  168. (void) x;
  169. (void) y;
  170. switch (key) {
  171. case 27:
  172. exit(0);
  173. break;
  174. }
  175. glutPostRedisplay();
  176. }
  177. static void SpecialKey( int key, int x, int y )
  178. {
  179. float step = 3.0;
  180. (void) x;
  181. (void) y;
  182. switch (key) {
  183. case GLUT_KEY_UP:
  184. Xrot += step;
  185. break;
  186. case GLUT_KEY_DOWN:
  187. Xrot -= step;
  188. break;
  189. case GLUT_KEY_LEFT:
  190. Yrot += step;
  191. break;
  192. case GLUT_KEY_RIGHT:
  193. Yrot -= step;
  194. break;
  195. }
  196. glutPostRedisplay();
  197. }
  198. static void Init( int argc, char *argv[] )
  199. {
  200. GLUquadricObj *q = gluNewQuadric();
  201. CylinderObj = glGenLists(1);
  202. glNewList(CylinderObj, GL_COMPILE);
  203. glTranslatef(0.0, 0.0, -1.0);
  204. /* cylinder */
  205. gluQuadricNormals(q, GL_SMOOTH);
  206. gluQuadricTexture(q, GL_TRUE);
  207. gluCylinder(q, 0.6, 0.6, 2.0, 24, 1);
  208. /* end cap */
  209. glTranslatef(0.0, 0.0, 2.0);
  210. gluDisk(q, 0.0, 0.6, 24, 1);
  211. /* other end cap */
  212. glTranslatef(0.0, 0.0, -2.0);
  213. gluQuadricOrientation(q, GLU_INSIDE);
  214. gluDisk(q, 0.0, 0.6, 24, 1);
  215. glEndList();
  216. gluDeleteQuadric(q);
  217. /* lighting */
  218. glEnable(GL_LIGHTING);
  219. {
  220. GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
  221. GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
  222. GLfloat teal[4] = { 0.0, 1.0, 0.8, 1.0 };
  223. glMaterialfv(GL_FRONT, GL_DIFFUSE, teal);
  224. glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
  225. glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
  226. glEnable(GL_LIGHT0);
  227. }
  228. /* fitering = nearest, initially */
  229. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  230. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  231. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  232. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  233. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  234. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  235. if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
  236. printf("Error: couldn't load texture image\n");
  237. exit(1);
  238. }
  239. glEnable(GL_CULL_FACE); /* don't need Z testing for convex objects */
  240. SetMode(LIT);
  241. if (argc > 1 && strcmp(argv[1], "-info")==0) {
  242. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  243. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  244. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  245. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  246. }
  247. }
  248. int main( int argc, char *argv[] )
  249. {
  250. glutInit( &argc, argv );
  251. glutInitWindowSize( 400, 400 );
  252. glutInitWindowPosition( 0, 0 );
  253. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  254. glutCreateWindow(argv[0] );
  255. Init(argc, argv);
  256. glutReshapeFunc( Reshape );
  257. glutKeyboardFunc( Key );
  258. glutSpecialFunc( SpecialKey );
  259. glutDisplayFunc( Display );
  260. glutIdleFunc( Idle );
  261. glutCreateMenu(ModeMenu);
  262. glutAddMenuEntry("Lit", LIT);
  263. glutAddMenuEntry("Textured", TEXTURED);
  264. glutAddMenuEntry("Reflect", REFLECT);
  265. glutAddMenuEntry("Point Filtered", POINT_FILTER);
  266. glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
  267. glutAddMenuEntry("Toggle Animation", ANIMATE);
  268. glutAddMenuEntry("Quit", QUIT);
  269. glutAttachMenu(GLUT_RIGHT_BUTTON);
  270. glutMainLoop();
  271. return 0;
  272. }