Clone of mesa.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ugltexcyl.c 9.3KB

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