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.

uglteapot.c 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Mesa 3-D graphics library
  3. * Version: 3.5
  4. *
  5. * The MIT License
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. /*
  25. * Linux Magazine July 2001
  26. * Conversion to UGL/Mesa from GLUT by Stephane Raimbault, 2001
  27. */
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <math.h>
  31. #include <ugl/ugl.h>
  32. #include <ugl/uglevent.h>
  33. #include <ugl/uglinput.h>
  34. #include <ugl/uglucode.h>
  35. #include <GL/uglmesa.h>
  36. #include <GL/glu.h>
  37. /* Need GLUT_SHAPES */
  38. #include <GL/uglglutshapes.h>
  39. #ifndef PI
  40. #define PI 3.14159265
  41. #endif
  42. UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
  43. UGL_LOCAL UGL_EVENT_Q_ID qId;
  44. UGL_LOCAL UGL_MESA_CONTEXT umc;
  45. UGL_LOCAL volatile UGL_BOOL stopWex;
  46. UGL_LOCAL GLint angle;
  47. UGL_LOCAL GLfloat Sin[360], Cos[360];
  48. UGL_LOCAL GLfloat L0pos[]={0.0, 2.0, -1.0};
  49. UGL_LOCAL GLfloat L0dif[]={0.3, 0.3, 0.8};
  50. UGL_LOCAL GLfloat L1pos[]={2.0, 2.0, 2.0};
  51. UGL_LOCAL GLfloat L1dif[]={0.5, 0.5, 0.5};
  52. UGL_LOCAL GLfloat Mspec[3];
  53. UGL_LOCAL GLfloat Mshiny;
  54. UGL_LOCAL GLuint theTeapot;
  55. UGL_LOCAL void calcTableCosSin()
  56. {
  57. int i;
  58. for(i=0;i<360;i++) {
  59. Cos[i] = cos(((float)i)/180.0*PI);
  60. Sin[i] = sin(((float)i)/180.0*PI);
  61. }
  62. }
  63. UGL_LOCAL void initGL(void)
  64. {
  65. glClearColor(0.0, 0.0, 0.0, 0.0);
  66. glColor3f(1.0, 0.0, 0.0);
  67. glEnable(GL_DEPTH_TEST);
  68. glShadeModel(GL_SMOOTH);
  69. glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
  70. glEnable(GL_LIGHTING);
  71. glEnable(GL_LIGHT0);
  72. glEnable(GL_LIGHT1);
  73. glLightfv(GL_LIGHT0, GL_DIFFUSE, L0dif);
  74. glLightfv(GL_LIGHT0, GL_SPECULAR, L0dif);
  75. glLightfv(GL_LIGHT1, GL_DIFFUSE, L1dif);
  76. glLightfv(GL_LIGHT1, GL_SPECULAR, L1dif);
  77. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Mspec);
  78. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, Mshiny);
  79. glMatrixMode(GL_PROJECTION);
  80. glLoadIdentity();
  81. gluPerspective(45.0, 1.0, 0.1, 10.0);
  82. glMatrixMode(GL_MODELVIEW);
  83. theTeapot = glGenLists(1);
  84. glNewList(theTeapot, GL_COMPILE);
  85. glutSolidTeapot(1.0);
  86. glEndList();
  87. }
  88. UGL_LOCAL void drawGL()
  89. {
  90. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  91. glLoadIdentity();
  92. gluLookAt(4.5*Cos[angle], 2.0,4.5*Sin[angle],0.0,0.0,0.0,0.0,
  93. 1.0,0.0);
  94. glLightfv(GL_LIGHT0, GL_POSITION, L0pos);
  95. glLightfv(GL_LIGHT1, GL_POSITION, L1pos);
  96. glCallList(theTeapot);
  97. glFlush();
  98. uglMesaSwapBuffers();
  99. }
  100. UGL_LOCAL void echoUse(void)
  101. {
  102. printf("tTeapot keys:\n");
  103. printf(" Left Counter clockwise rotation (y-axis)\n");
  104. printf(" Right Clockwise rotation (y-axis)\n");
  105. printf(" j Enable/disable Light0\n");
  106. printf(" k Enable/disable Light1\n");
  107. printf(" m Add specular\n");
  108. printf(" l Remove specular\n");
  109. printf(" o Add shininess\n");
  110. printf(" p Remove shininess\n");
  111. printf(" ESC Exit\n");
  112. }
  113. UGL_LOCAL void readKey (UGL_WCHAR key)
  114. {
  115. switch(key)
  116. {
  117. case UGL_UNI_RIGHT_ARROW:
  118. angle +=2;
  119. if (angle>= 360)
  120. angle-=360;
  121. break;
  122. case UGL_UNI_LEFT_ARROW:
  123. angle -=2;
  124. if (angle<0)
  125. angle+=360;
  126. break;
  127. case 'j':
  128. glIsEnabled(GL_LIGHT0) ?
  129. glDisable(GL_LIGHT0) : glEnable(GL_LIGHT0);
  130. break;
  131. case 'k':
  132. glIsEnabled(GL_LIGHT1) ?
  133. glDisable(GL_LIGHT1) : glEnable(GL_LIGHT1);
  134. break;
  135. case 'm':
  136. Mspec[0]+=0.1;
  137. if(Mspec[0]>1)
  138. Mspec[0]=1;
  139. Mspec[1]+=0.1;
  140. if(Mspec[1]>1)
  141. Mspec[1]=1;
  142. Mspec[2]+=0.1;
  143. if(Mspec[2]>1)
  144. Mspec[2]=1;
  145. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Mspec);
  146. break;
  147. case 'l':
  148. Mspec[0]-=0.1;
  149. if(Mspec[0]>1)
  150. Mspec[0]=1;
  151. Mspec[1]-=0.1;
  152. if(Mspec[1]>1)
  153. Mspec[1]=1;
  154. Mspec[2]-=0.1;
  155. if(Mspec[2]>1)
  156. Mspec[2]=1;
  157. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Mspec);
  158. break;
  159. case 'o':
  160. Mshiny -= 1;
  161. if (Mshiny<0)
  162. Mshiny=0;
  163. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, Mshiny);
  164. break;
  165. case 'p':
  166. Mshiny += 1;
  167. if (Mshiny>128)
  168. Mshiny=128;
  169. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, Mshiny);
  170. break;
  171. case UGL_UNI_ESCAPE:
  172. stopWex = UGL_TRUE;
  173. break;
  174. }
  175. }
  176. UGL_LOCAL void loopEvent(void)
  177. {
  178. UGL_EVENT event;
  179. UGL_INPUT_EVENT * pInputEvent;
  180. UGL_FOREVER
  181. {
  182. if (uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT)
  183. != UGL_STATUS_Q_EMPTY)
  184. {
  185. pInputEvent = (UGL_INPUT_EVENT *)&event;
  186. if (pInputEvent->header.type == UGL_EVENT_TYPE_KEYBOARD &&
  187. pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
  188. readKey(pInputEvent->type.keyboard.key);
  189. }
  190. drawGL();
  191. if (stopWex)
  192. break;
  193. }
  194. }
  195. void windMLTeapot (UGL_BOOL windMLMode);
  196. void uglteapot (void)
  197. {
  198. taskSpawn ("tTeapot", 210, VX_FP_TASK, 100000, (FUNCPTR)windMLTeapot,
  199. UGL_FALSE,1,2,3,4,5,6,7,8,9);
  200. }
  201. void windMLTeapot (UGL_BOOL windMLMode)
  202. {
  203. UGL_INPUT_DEVICE_ID keyboardDevId;
  204. GLsizei displayWidth, displayHeight;
  205. GLsizei x, y, w, h;
  206. angle = 45;
  207. Mspec[0] = 0.5;
  208. Mspec[1] = 0.5;
  209. Mspec[2] = 0.5;
  210. Mshiny = 50;
  211. uglInitialize ();
  212. uglDriverFind (UGL_KEYBOARD_TYPE, 0,
  213. (UGL_UINT32 *)&keyboardDevId);
  214. uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0, (UGL_UINT32 *)&eventServiceId);
  215. qId = uglEventQCreate (eventServiceId, 100);
  216. /* Double buffering */
  217. if (windMLMode)
  218. umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE
  219. | UGL_MESA_WINDML_EXCLUSIVE, NULL);
  220. else
  221. umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL);
  222. if (umc == NULL)
  223. {
  224. uglDeinitialize ();
  225. return;
  226. }
  227. uglMesaMakeCurrentContext (umc, 0, 0, 1, 1);
  228. uglMesaGetIntegerv(UGL_MESA_DISPLAY_WIDTH, &displayWidth);
  229. uglMesaGetIntegerv(UGL_MESA_DISPLAY_HEIGHT, &displayHeight);
  230. h = (displayHeight*2)/3;
  231. w = h;
  232. x = (displayWidth-w)/2;
  233. y = (displayHeight-h)/2;
  234. uglMesaMoveToWindow(x, y);
  235. uglMesaResizeToWindow(w, h);
  236. calcTableCosSin();
  237. initGL ();
  238. echoUse();
  239. stopWex = UGL_FALSE;
  240. loopEvent();
  241. uglEventQDestroy (eventServiceId, qId);
  242. uglMesaDestroyContext();
  243. uglDeinitialize ();
  244. return;
  245. }