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.

ugltexcube.c 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* ugltexcube.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. * Authors:
  28. * Stephane Raimbault <stephane.raimbault@windriver.com>
  29. */
  30. /*
  31. DESCRIPTION
  32. Draw a textured cube
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <math.h>
  37. #include <ugl/ugl.h>
  38. #include <ugl/uglevent.h>
  39. #include <ugl/uglinput.h>
  40. #include <GL/uglmesa.h>
  41. #include <GL/glu.h>
  42. #include "../util/readtex.h"
  43. #define IMAGE_FILE "Mesa/images/wrs_logo.rgb"
  44. UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
  45. UGL_LOCAL UGL_EVENT_Q_ID qId;
  46. UGL_LOCAL UGL_MESA_CONTEXT umc;
  47. UGL_LOCAL GLfloat xrot, yrot, zrot;
  48. UGL_LOCAL GLuint texture[1];
  49. UGL_LOCAL GLuint theTexCube;
  50. typedef struct {
  51. GLubyte *data;
  52. int width, height;
  53. GLenum format;
  54. } TEX_IMAGE;
  55. UGL_LOCAL void cleanUp (void);
  56. UGL_LOCAL void loadGLTexture()
  57. {
  58. TEX_IMAGE * texImage=NULL;
  59. texImage = (TEX_IMAGE *) malloc(sizeof(TEX_IMAGE));
  60. if (texImage == NULL)
  61. {
  62. printf("Error allocating space for image");
  63. cleanUp();
  64. exit(1);
  65. }
  66. texImage->data = LoadRGBImage(IMAGE_FILE, &texImage->width,
  67. &texImage->height, &texImage->format);
  68. if (!texImage->data)
  69. {
  70. printf("Couldn't read %s\n", IMAGE_FILE);
  71. free(texImage);
  72. cleanUp();
  73. exit(1);
  74. }
  75. /* Create Texture */
  76. glGenTextures(1, &texture[0]);
  77. glBindTexture(GL_TEXTURE_2D, texture[0]);
  78. glTexImage2D(GL_TEXTURE_2D, 0, 3,
  79. texImage->width, texImage->height,
  80. 0, GL_RGB, GL_UNSIGNED_BYTE, texImage->data);
  81. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  82. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  83. free(texImage->data);
  84. free(texImage);
  85. }
  86. UGL_LOCAL void initGL(int width, int height)
  87. {
  88. /* Load the texture(s) */
  89. loadGLTexture();
  90. /* Enable texture mapping */
  91. glEnable(GL_TEXTURE_2D);
  92. /* Clear the background color to black */
  93. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  94. glEnable(GL_CULL_FACE);
  95. /* Enables smooth color shading */
  96. glShadeModel(GL_SMOOTH);
  97. /* glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); */
  98. /* glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); */
  99. theTexCube = glGenLists(1);
  100. glNewList(theTexCube, GL_COMPILE);
  101. /* Choose the texture to use */
  102. glBindTexture(GL_TEXTURE_2D, texture[0]);
  103. /* Begin drawing a cube */
  104. glBegin(GL_QUADS);
  105. /* Front face (note that the texture's corners have to match the
  106. quad's corners) */
  107. /* Bottom left of the texture and quad */
  108. glTexCoord2f(0.0f, 0.0f);
  109. glVertex3f(-1.0f, -1.0f, 1.0f);
  110. /* Bottom Right Of The Texture and Quad */
  111. glTexCoord2f(1.0f, 0.0f);
  112. glVertex3f(1.0f, -1.0f, 1.0f);
  113. /* Top Right Of The Texture and Quad */
  114. glTexCoord2f(1.0f, 1.0f);
  115. glVertex3f(1.0f, 1.0f, 1.0f);
  116. /* Top Left Of The Texture and Quad */
  117. glTexCoord2f(0.0f, 1.0f);
  118. glVertex3f(-1.0f, 1.0f, 1.0f);
  119. /* Back Face */
  120. /* Bottom Right Of The Texture and Quad */
  121. glTexCoord2f(1.0f, 0.0f);
  122. glVertex3f(-1.0f, -1.0f, -1.0f);
  123. /* Top Right Of The Texture and Quad */
  124. glTexCoord2f(1.0f, 1.0f);
  125. glVertex3f(-1.0f, 1.0f, -1.0f);
  126. /* Top Left Of The Texture and Quad */
  127. glTexCoord2f(0.0f, 1.0f);
  128. glVertex3f(1.0f, 1.0f, -1.0f);
  129. /* Bottom Left Of The Texture and Quad */
  130. glTexCoord2f(0.0f, 0.0f);
  131. glVertex3f(1.0f, -1.0f, -1.0f);
  132. /* Top Face */
  133. /* Top Left Of The Texture and Quad */
  134. glTexCoord2f(0.0f, 1.0f);
  135. glVertex3f(-1.0f, 1.0f, -1.0f);
  136. /* Bottom Left Of The Texture and Quad */
  137. glTexCoord2f(0.0f, 0.0f);
  138. glVertex3f(-1.0f, 1.0f, 1.0f);
  139. /* Bottom Right Of The Texture and Quad */
  140. glTexCoord2f(1.0f, 0.0f);
  141. glVertex3f(1.0f, 1.0f, 1.0f);
  142. /* Top Right Of The Texture and Quad */
  143. glTexCoord2f(1.0f, 1.0f);
  144. glVertex3f(1.0f, 1.0f, -1.0f);
  145. /* Bottom Face */
  146. /* Top Right Of The Texture and Quad */
  147. glTexCoord2f(1.0f, 1.0f);
  148. glVertex3f(-1.0f, -1.0f, -1.0f);
  149. /* Top Left Of The Texture and Quad */
  150. glTexCoord2f(0.0f, 1.0f);
  151. glVertex3f(1.0f, -1.0f, -1.0f);
  152. /* Bottom Left Of The Texture and Quad */
  153. glTexCoord2f(0.0f, 0.0f);
  154. glVertex3f(1.0f, -1.0f, 1.0f);
  155. /* Bottom Right Of The Texture and Quad */
  156. glTexCoord2f(1.0f, 0.0f);
  157. glVertex3f(-1.0f, -1.0f, 1.0f);
  158. /* Right face */
  159. /* Bottom Right Of The Texture and Quad */
  160. glTexCoord2f(1.0f, 0.0f);
  161. glVertex3f(1.0f, -1.0f, -1.0f);
  162. /* Top Right Of The Texture and Quad */
  163. glTexCoord2f(1.0f, 1.0f);
  164. glVertex3f(1.0f, 1.0f, -1.0f);
  165. /* Top Left Of The Texture and Quad */
  166. glTexCoord2f(0.0f, 1.0f);
  167. glVertex3f(1.0f, 1.0f, 1.0f);
  168. /* Bottom Left Of The Texture and Quad */
  169. glTexCoord2f(0.0f, 0.0f);
  170. glVertex3f(1.0f, -1.0f, 1.0f);
  171. /* Left Face */
  172. /* Bottom Left Of The Texture and Quad */
  173. glTexCoord2f(0.0f, 0.0f);
  174. glVertex3f(-1.0f, -1.0f, -1.0f);
  175. /* Bottom Right Of The Texture and Quad */
  176. glTexCoord2f(1.0f, 0.0f);
  177. glVertex3f(-1.0f, -1.0f, 1.0f);
  178. /* Top Right Of The Texture and Quad */
  179. glTexCoord2f(1.0f, 1.0f);
  180. glVertex3f(-1.0f, 1.0f, 1.0f);
  181. /* Top Left Of The Texture and Quad */
  182. glTexCoord2f(0.0f, 1.0f);
  183. glVertex3f(-1.0f, 1.0f, -1.0f);
  184. glEnd(); /* done with the polygon */
  185. glEndList();
  186. glDisable(GL_DITHER);
  187. glMatrixMode(GL_PROJECTION);
  188. /* Reset the projection matrix */
  189. glLoadIdentity();
  190. /* Calculate the aspect ratio of the window */
  191. gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f);
  192. glMatrixMode(GL_MODELVIEW);
  193. glLoadIdentity();
  194. }
  195. UGL_LOCAL void drawGL()
  196. {
  197. glClear(GL_COLOR_BUFFER_BIT);
  198. /* Reset The View */
  199. glPushMatrix();
  200. /* Move 8 units into the screen */
  201. glTranslatef(0.0f, 0.0f, -8.0f);
  202. /* Rotate on the X axis */
  203. glRotatef(xrot, 1.0f, 0.0f, 0.0f);
  204. /* Rotate on the Y axis */
  205. glRotatef(yrot, 0.0f, 1.0f, 0.0f);
  206. /* Rotate On The Z Axis */
  207. glRotatef(zrot, 0.0f, 0.0f, 1.0f);
  208. glCallList(theTexCube);
  209. glFlush();
  210. uglMesaSwapBuffers();
  211. glPopMatrix();
  212. xrot += 1.6f;
  213. yrot += 1.6f;
  214. zrot += 1.6f;
  215. }
  216. UGL_LOCAL int getEvent(void)
  217. {
  218. UGL_EVENT event;
  219. UGL_STATUS status;
  220. int retVal = 0;
  221. status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
  222. while (status != UGL_STATUS_Q_EMPTY)
  223. {
  224. UGL_INPUT_EVENT * pInputEvent = (UGL_INPUT_EVENT *)&event;
  225. if (pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
  226. retVal = 1;
  227. status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
  228. }
  229. return(retVal);
  230. }
  231. UGL_LOCAL void cleanUp (void)
  232. {
  233. if (eventServiceId != UGL_NULL)
  234. uglEventQDestroy (eventServiceId, qId);
  235. uglMesaDestroyContext();
  236. uglDeinitialize();
  237. }
  238. void windMLTexCube (UGL_BOOL windMLMode);
  239. void ugltexcube (void)
  240. {
  241. taskSpawn("tTexCube", 210, VX_FP_TASK, 100000, (FUNCPTR)windMLTexCube,
  242. UGL_FALSE,1,2,3,4,5,6,7,8,9);
  243. }
  244. void windMLTexCube(UGL_BOOL windMLMode)
  245. {
  246. GLuint width, height;
  247. UGL_INPUT_DEVICE_ID keyboardDevId;
  248. uglInitialize();
  249. uglDriverFind (UGL_KEYBOARD_TYPE, 0, (UGL_UINT32 *)&keyboardDevId);
  250. if (uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0,
  251. (UGL_UINT32 *)&eventServiceId) == UGL_STATUS_OK)
  252. {
  253. qId = uglEventQCreate (eventServiceId, 100);
  254. }
  255. else
  256. {
  257. eventServiceId = UGL_NULL;
  258. }
  259. if (windMLMode)
  260. umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE
  261. | UGL_MESA_WINDML_EXCLUSIVE, NULL);
  262. else
  263. umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL);
  264. if (umc == NULL)
  265. {
  266. uglDeinitialize();
  267. return;
  268. }
  269. uglMesaMakeCurrentContext(umc, 0, 0,
  270. UGL_MESA_FULLSCREEN_WIDTH,
  271. UGL_MESA_FULLSCREEN_HEIGHT);
  272. uglMesaGetIntegerv(UGL_MESA_WIDTH, &width);
  273. uglMesaGetIntegerv(UGL_MESA_HEIGHT, &height);
  274. initGL(width, height);
  275. while(!getEvent())
  276. drawGL();
  277. cleanUp();
  278. return;
  279. }