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.

ugltexcyl.c 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Textured cylinder demo: lighting, texturing, reflection mapping.
  3. *
  4. * Brian Paul May 1997 This program is in the public domain.
  5. *
  6. * Conversion to UGL/Mesa by Stephane Raimbault
  7. */
  8. /*
  9. * $Log: ugltexcyl.c,v $
  10. * Revision 1.2 2001/09/10 19:21:13 brianp
  11. * WindML updates (Stephane Raimbault)
  12. *
  13. * Revision 1.1 2001/08/20 16:07:11 brianp
  14. * WindML driver (Stephane Raimbault)
  15. *
  16. * Revision 1.5 2001/03/27 17:35:26 brianp
  17. * set initial window pos
  18. *
  19. * Revision 1.4 2000/12/24 22:53:54 pesco
  20. * * demos/Makefile.am (INCLUDES): Added -I$(top_srcdir)/util.
  21. * * demos/Makefile.X11, demos/Makefile.BeOS-R4, demos/Makefile.cygnus:
  22. * Essentially the same.
  23. * Program files updated to include "readtex.c", not "../util/readtex.c".
  24. * * demos/reflect.c: Likewise for "showbuffer.c".
  25. *
  26. *
  27. * * Makefile.am (EXTRA_DIST): Added top-level regular files.
  28. *
  29. * * include/GL/Makefile.am (INC_X11): Added glxext.h.
  30. *
  31. *
  32. * * src/GGI/include/ggi/mesa/Makefile.am (EXTRA_HEADERS): Include
  33. * Mesa GGI headers in dist even if HAVE_GGI is not given.
  34. *
  35. * * configure.in: Look for GLUT and demo source dirs in $srcdir.
  36. *
  37. * * src/swrast/Makefile.am (libMesaSwrast_la_SOURCES): Set to *.[ch].
  38. * More source list updates in various Makefile.am's.
  39. *
  40. * * Makefile.am (dist-hook): Remove CVS directory from distribution.
  41. * (DIST_SUBDIRS): List all possible subdirs here.
  42. * (SUBDIRS): Only list subdirs selected for build again.
  43. * The above two applied to all subdir Makefile.am's also.
  44. *
  45. * Revision 1.3 2000/09/29 23:09:39 brianp
  46. * added fps output
  47. *
  48. * Revision 1.2 1999/10/21 16:39:06 brianp
  49. * added -info command line option
  50. *
  51. * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
  52. * Imported sources
  53. *
  54. * Revision 3.3 1999/03/28 18:24:37 brianp
  55. * minor clean-up
  56. *
  57. * Revision 3.2 1998/11/05 04:34:04 brianp
  58. * moved image files to ../images/ directory
  59. *
  60. * Revision 3.1 1998/06/23 03:16:51 brianp
  61. * added Point/Linear sampling menu items
  62. *
  63. * Revision 3.0 1998/02/14 18:42:29 brianp
  64. * initial rev
  65. *
  66. */
  67. #include <stdio.h>
  68. #include <stdlib.h>
  69. #include <math.h>
  70. #include <tickLib.h>
  71. #include <ugl/ugl.h>
  72. #include <ugl/uglucode.h>
  73. #include <ugl/uglevent.h>
  74. #include <ugl/uglinput.h>
  75. #include <GL/uglmesa.h>
  76. #include <GL/glu.h>
  77. #include "../util/readtex.h"
  78. #define TEXTURE_FILE "Mesa/images/reflect.rgb"
  79. #define LIT 1
  80. #define TEXTURED 2
  81. #define REFLECT 3
  82. #define ANIMATE 10
  83. #define POINT_FILTER 20
  84. #define LINEAR_FILTER 21
  85. #define QUIT 100
  86. #define COUNT_FRAMES
  87. UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
  88. UGL_LOCAL UGL_EVENT_Q_ID qId;
  89. UGL_LOCAL volatile UGL_BOOL stopWex;
  90. UGL_LOCAL UGL_MESA_CONTEXT umc;
  91. UGL_LOCAL GLuint CylinderObj;
  92. UGL_LOCAL GLboolean Animate;
  93. UGL_LOCAL GLboolean linearFilter;
  94. UGL_LOCAL GLfloat Xrot, Yrot, Zrot;
  95. UGL_LOCAL GLfloat DXrot, DYrot;
  96. UGL_LOCAL GLuint limit;
  97. UGL_LOCAL GLuint count;
  98. UGL_LOCAL GLuint tickStart, tickStop, tickBySec;
  99. UGL_LOCAL void cleanUp (void);
  100. UGL_LOCAL void drawGL(void)
  101. {
  102. #ifdef COUNT_FRAMES
  103. int time;
  104. #endif
  105. glClear( GL_COLOR_BUFFER_BIT );
  106. glPushMatrix();
  107. glRotatef(Xrot, 1.0, 0.0, 0.0);
  108. glRotatef(Yrot, 0.0, 1.0, 0.0);
  109. glRotatef(Zrot, 0.0, 0.0, 1.0);
  110. glScalef(5.0, 5.0, 5.0);
  111. glCallList(CylinderObj);
  112. glPopMatrix();
  113. uglMesaSwapBuffers();
  114. if (Animate)
  115. {
  116. Xrot += DXrot;
  117. Yrot += DYrot;
  118. }
  119. #ifdef COUNT_FRAMES
  120. if (count > limit)
  121. {
  122. tickStop = tickGet ();
  123. time = (tickStop-tickStart)/tickBySec;
  124. printf (" %i fps\n", count/time);
  125. tickStart = tickStop;
  126. count = 0;
  127. }
  128. else
  129. count++;
  130. #endif
  131. }
  132. UGL_LOCAL void echoUse(void)
  133. {
  134. printf("Keys:\n");
  135. printf(" Up/Down Rotate on Y\n");
  136. printf(" Left/Right Rotate on X\n");
  137. printf(" a Toggle animation\n");
  138. printf(" f Toggle point/linear filtered\n");
  139. printf(" l Lit\n");
  140. printf(" t Textured\n");
  141. printf(" r Reflect\n");
  142. printf(" ESC Exit\n");
  143. }
  144. UGL_LOCAL void readKey(UGL_WCHAR key)
  145. {
  146. float step = 3.0;
  147. switch (key)
  148. {
  149. case 'a':
  150. Animate = !Animate;
  151. break;
  152. case 'f':
  153. if(linearFilter)
  154. {
  155. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  156. GL_NEAREST);
  157. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  158. GL_NEAREST);
  159. }
  160. else
  161. {
  162. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  163. GL_LINEAR);
  164. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  165. GL_LINEAR);
  166. }
  167. linearFilter = !linearFilter;
  168. break;
  169. case 'l':
  170. glEnable(GL_LIGHTING);
  171. glDisable(GL_TEXTURE_2D);
  172. glDisable(GL_TEXTURE_GEN_S);
  173. glDisable(GL_TEXTURE_GEN_T);
  174. break;
  175. case 't':
  176. glDisable(GL_LIGHTING);
  177. glEnable(GL_TEXTURE_2D);
  178. glDisable(GL_TEXTURE_GEN_S);
  179. glDisable(GL_TEXTURE_GEN_T);
  180. break;
  181. case 'r':
  182. glDisable(GL_LIGHTING);
  183. glEnable(GL_TEXTURE_2D);
  184. glEnable(GL_TEXTURE_GEN_S);
  185. glEnable(GL_TEXTURE_GEN_T);
  186. break;
  187. case UGL_UNI_UP_ARROW:
  188. Xrot += step;
  189. break;
  190. case UGL_UNI_DOWN_ARROW:
  191. Xrot -= step;
  192. break;
  193. case UGL_UNI_LEFT_ARROW:
  194. Yrot += step;
  195. break;
  196. case UGL_UNI_RIGHT_ARROW:
  197. Yrot -= step;
  198. break;
  199. case UGL_UNI_ESCAPE:
  200. stopWex = UGL_TRUE;
  201. break;
  202. }
  203. }
  204. UGL_LOCAL void loopEvent(void)
  205. {
  206. UGL_EVENT event;
  207. UGL_INPUT_EVENT * pInputEvent;
  208. UGL_FOREVER
  209. {
  210. if (uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT)
  211. != UGL_STATUS_Q_EMPTY)
  212. {
  213. pInputEvent = (UGL_INPUT_EVENT *)&event;
  214. if (pInputEvent->header.type == UGL_EVENT_TYPE_KEYBOARD &&
  215. pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
  216. readKey(pInputEvent->type.keyboard.key);
  217. }
  218. drawGL();
  219. if (stopWex)
  220. break;
  221. }
  222. }
  223. UGL_LOCAL void initGL(void)
  224. {
  225. GLUquadricObj *q = gluNewQuadric();
  226. CylinderObj = glGenLists(1);
  227. glNewList(CylinderObj, GL_COMPILE);
  228. glTranslatef(0.0, 0.0, -1.0);
  229. /* cylinder */
  230. gluQuadricNormals(q, GL_SMOOTH);
  231. gluQuadricTexture(q, GL_TRUE);
  232. gluCylinder(q, 0.6, 0.6, 2.0, 24, 1);
  233. /* end cap */
  234. glTranslatef(0.0, 0.0, 2.0);
  235. gluDisk(q, 0.0, 0.6, 24, 1);
  236. /* other end cap */
  237. glTranslatef(0.0, 0.0, -2.0);
  238. gluQuadricOrientation(q, GLU_INSIDE);
  239. gluDisk(q, 0.0, 0.6, 24, 1);
  240. glEndList();
  241. gluDeleteQuadric(q);
  242. /* lighting */
  243. glEnable(GL_LIGHTING);
  244. {
  245. GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
  246. GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
  247. GLfloat teal[4] = { 0.0, 1.0, 0.8, 1.0 };
  248. glMaterialfv(GL_FRONT, GL_DIFFUSE, teal);
  249. glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
  250. glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
  251. glEnable(GL_LIGHT0);
  252. }
  253. /* fitering = nearest, initially */
  254. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  255. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  256. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  257. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  258. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  259. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  260. if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB))
  261. {
  262. printf("Error: couldn't load texture image\n");
  263. cleanUp();
  264. exit(1);
  265. }
  266. glEnable(GL_CULL_FACE); /* don't need Z testing for convex objects */
  267. glEnable(GL_LIGHTING);
  268. glMatrixMode( GL_PROJECTION );
  269. glLoadIdentity();
  270. glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
  271. glMatrixMode( GL_MODELVIEW );
  272. glLoadIdentity();
  273. glTranslatef( 0.0, 0.0, -70.0 );
  274. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  275. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  276. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  277. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  278. #ifdef COUNT_FRAMES
  279. tickStart = tickGet ();
  280. tickBySec = sysClkRateGet ();
  281. #endif
  282. }
  283. UGL_LOCAL void cleanUp (void)
  284. {
  285. uglEventQDestroy (eventServiceId, qId);
  286. uglMesaDestroyContext();
  287. uglDeinitialize ();
  288. }
  289. void windMLTexCyl (UGL_BOOL windMLMode);
  290. void ugltexcyl (void)
  291. {
  292. taskSpawn ("tTexCyl", 210, VX_FP_TASK, 100000, (FUNCPTR)windMLTexCyl,
  293. UGL_FALSE,1,2,3,4,5,6,7,8,9);
  294. }
  295. void windMLTexCyl (UGL_BOOL windMLMode)
  296. {
  297. UGL_INPUT_DEVICE_ID keyboardDevId;
  298. GLsizei displayWidth, displayHeight;
  299. GLsizei x, y, w, h;
  300. CylinderObj = 0;
  301. Animate = GL_TRUE;
  302. linearFilter = GL_FALSE;
  303. Xrot = 0.0;
  304. Yrot = 0.0;
  305. Zrot = 0.0;
  306. DXrot = 1.0;
  307. DYrot = 2.5;
  308. limit = 100;
  309. count = 1;
  310. uglInitialize ();
  311. uglDriverFind (UGL_KEYBOARD_TYPE, 0,
  312. (UGL_UINT32 *)&keyboardDevId);
  313. uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0, (UGL_UINT32 *)&eventServiceId);
  314. qId = uglEventQCreate (eventServiceId, 100);
  315. /* Double buffering */
  316. if (windMLMode)
  317. umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE
  318. | UGL_MESA_WINDML_EXCLUSIVE, NULL);
  319. else
  320. umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL);
  321. if (umc == NULL)
  322. {
  323. uglDeinitialize ();
  324. return;
  325. }
  326. uglMesaMakeCurrentContext (umc, 0, 0, 1, 1);
  327. uglMesaGetIntegerv(UGL_MESA_DISPLAY_WIDTH, &displayWidth);
  328. uglMesaGetIntegerv(UGL_MESA_DISPLAY_HEIGHT, &displayHeight);
  329. h = (displayHeight*3)/4;
  330. w = h;
  331. x = (displayWidth-w)/2;
  332. y = (displayHeight-h)/2;
  333. uglMesaMoveToWindow(x, y);
  334. uglMesaResizeToWindow(w, h);
  335. initGL ();
  336. echoUse();
  337. stopWex = UGL_FALSE;
  338. loopEvent();
  339. cleanUp();
  340. return;
  341. }