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.

uglcube.c 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* uglcube.c - WindML/Mesa example program */
  2. /* Copyright (C) 2001 by Wind River Systems, Inc */
  3. /*
  4. * Mesa 3-D graphics library
  5. * Version: 3.5
  6. *
  7. * The MIT License
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. */
  26. /*
  27. DESCRIPTION
  28. Draw a smooth cube.
  29. */
  30. #include <stdio.h>
  31. #include <math.h>
  32. #include <ugl/uglevent.h>
  33. #include <ugl/uglinput.h>
  34. #include <GL/uglmesa.h>
  35. #include <GL/glu.h>
  36. UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
  37. UGL_LOCAL UGL_EVENT_Q_ID qId;
  38. UGL_LOCAL UGL_MESA_CONTEXT umc;
  39. UGL_LOCAL GLfloat rotx;
  40. UGL_LOCAL GLfloat roty;
  41. UGL_LOCAL GLuint theCube;
  42. UGL_LOCAL void cube()
  43. {
  44. /* Front */
  45. glBegin(GL_QUADS);
  46. glColor3f(0.0f, 0.0f, 1.0f);
  47. glVertex3f(-1.0f, 1.0f, 1.0f);
  48. glColor3f(0.0f, 1.0f, 0.0f);
  49. glVertex3f(1.0f, 1.0f, 1.0f);
  50. glColor3f(1.0f, 0.0f, 0.0f);
  51. glVertex3f(1.0f, -1.0f, 1.0f);
  52. glColor3f(1.0f, 1.0f, 0.0f);
  53. glVertex3f(-1.0f, -1.0f, 1.0f);
  54. glEnd();
  55. /* Back */
  56. glBegin(GL_QUADS);
  57. glColor3f(0.0f, 0.0f, 1.0f);
  58. glVertex3f(-1.0f, 1.0f, -1.0f);
  59. glColor3f(0.0f, 1.0f, 0.0f);
  60. glVertex3f(1.0f, 1.0f, -1.0f);
  61. glColor3f(1.0f, 0.0f, 0.0f);
  62. glVertex3f(1.0f, -1.0f, -1.0f);
  63. glColor3f(1.0f, 1.0f, 0.0f);
  64. glVertex3f(-1.0f, -1.0f, -1.0f);
  65. glEnd();
  66. /* Right */
  67. glBegin(GL_QUADS);
  68. glColor3f(0.0f, 1.0f, 0.0f);
  69. glVertex3f(1.0f, 1.0f, 1.0f);
  70. glColor3f(0.0f, 1.0f, 0.0f);
  71. glVertex3f(1.0f, 1.0f, -1.0f);
  72. glColor3f(1.0f, 0.0f, 0.0f);
  73. glVertex3f(1.0f, -1.0f, -1.0f);
  74. glColor3f(1.0f, 0.0f, 0.0f);
  75. glVertex3f(1.0f, -1.0f, 1.0f);
  76. glEnd();
  77. /* Left */
  78. glBegin(GL_QUADS);
  79. glColor3f(0.0f, 0.0f, 1.0f);
  80. glVertex3f(-1.0f, 1.0f, 1.0f);
  81. glColor3f(0.0f, 0.0f, 1.0f);
  82. glVertex3f(-1.0f, 1.0f, -1.0f);
  83. glColor3f(1.0f, 1.0f, 0.0f);
  84. glVertex3f(-1.0f, -1.0f, -1.0f);
  85. glColor3f(1.0f, 1.0f, 0.0f);
  86. glVertex3f(-1.0f, -1.0f, 1.0f);
  87. glEnd();
  88. /* Top */
  89. glBegin(GL_QUADS);
  90. glColor3f(0.0f, 0.0f, 1.0f);
  91. glVertex3f(-1.0f, 1.0f, -1.0f);
  92. glColor3f(0.0f, 1.0f, 0.0f);
  93. glVertex3f(1.0f, 1.0f, -1.0f);
  94. glColor3f(0.0f, 1.0f, 0.0f);
  95. glVertex3f(1.0f, 1.0f, 1.0f);
  96. glColor3f(0.0f, 0.0f, 1.0f);
  97. glVertex3f(-1.0f, 1.0f, 1.0f);
  98. glEnd();
  99. /* Bottom */
  100. glBegin(GL_QUADS);
  101. glColor3f(1.0f, 1.0f, 0.0f);
  102. glVertex3f(-1.0f, -1.0f, -1.0f);
  103. glColor3f(1.0f, 0.0f, 0.0f);
  104. glVertex3f(1.0f, -1.0f, -1.0f);
  105. glColor3f(1.0f, 0.0f, 0.0f);
  106. glVertex3f(1.0f, -1.0f, 1.0f);
  107. glColor3f(1.0f, 1.0f, 0.0f);
  108. glVertex3f(-1.0f, -1.0f, 1.0f);
  109. glEnd();
  110. }
  111. UGL_LOCAL void initGL
  112. (
  113. int Width,
  114. int Height
  115. )
  116. {
  117. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  118. glDepthFunc(GL_LESS);
  119. glEnable(GL_DEPTH_TEST);
  120. glShadeModel(GL_SMOOTH);
  121. theCube = glGenLists(1);
  122. glNewList(theCube, GL_COMPILE);
  123. cube();
  124. glEndList();
  125. glMatrixMode(GL_PROJECTION);
  126. glLoadIdentity();
  127. gluPerspective(45.0f, (GLfloat) Width / (GLfloat) Height, 0.1f, 100.0f);
  128. glMatrixMode(GL_MODELVIEW);
  129. glLoadIdentity();
  130. glTranslatef(0.0f, 0.0f, -6.0f);
  131. }
  132. /* The main drawing function. */
  133. UGL_LOCAL void drawGL()
  134. {
  135. /* Clear The Screen And The Depth Buffer */
  136. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  137. /* Rotate the cube */
  138. glRotatef(rotx, 1.0f, 0.0f, 0.0f);
  139. glRotatef(roty, 0.0f, 1.0f, 0.0f);
  140. glCallList(theCube);
  141. glFlush();
  142. uglMesaSwapBuffers();
  143. }
  144. /* The function called whenever a key is pressed. */
  145. UGL_LOCAL int getEvent(void)
  146. {
  147. UGL_EVENT event;
  148. UGL_STATUS status;
  149. int retVal = 0;
  150. status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
  151. while (status != UGL_STATUS_Q_EMPTY)
  152. {
  153. UGL_INPUT_EVENT * pInputEvent = (UGL_INPUT_EVENT *)&event;
  154. if (pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
  155. retVal = 1;
  156. status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
  157. }
  158. return(retVal);
  159. }
  160. void windMLCube (void);
  161. void uglcube (void)
  162. {
  163. taskSpawn("tCube", 210, VX_FP_TASK, 100000, (FUNCPTR)windMLCube,
  164. 0,1,2,3,4,5,6,7,8,9);
  165. }
  166. void windMLCube (void)
  167. {
  168. GLsizei width, height;
  169. UGL_INPUT_DEVICE_ID keyboardDevId;
  170. rotx = 2.5f;
  171. roty = 1.0f;
  172. uglInitialize();
  173. uglDriverFind (UGL_KEYBOARD_TYPE, 0, (UGL_UINT32 *)&keyboardDevId);
  174. if (uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0,
  175. (UGL_UINT32 *)&eventServiceId) == UGL_STATUS_OK)
  176. {
  177. qId = uglEventQCreate (eventServiceId, 100);
  178. }
  179. else
  180. {
  181. eventServiceId = UGL_NULL;
  182. }
  183. umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL);
  184. /* Fullscreen */
  185. uglMesaMakeCurrentContext(umc, 0, 0, UGL_MESA_FULLSCREEN_WIDTH,
  186. UGL_MESA_FULLSCREEN_HEIGHT);
  187. uglMesaGetIntegerv(UGL_MESA_WIDTH, &width);
  188. uglMesaGetIntegerv(UGL_MESA_HEIGHT, &height);
  189. /* Initialize our window. */
  190. initGL(width, height);
  191. while (!getEvent())
  192. drawGL();
  193. if (eventServiceId != UGL_NULL)
  194. uglEventQDestroy (eventServiceId, qId);
  195. uglMesaDestroyContext();
  196. uglDeinitialize();
  197. return;
  198. }