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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. #define IMAGE_FILE "Mesa/windmldemos/wrs_logo.bmp"
  43. UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
  44. UGL_LOCAL UGL_EVENT_Q_ID qId;
  45. UGL_LOCAL UGL_MESA_CONTEXT umc;
  46. UGL_LOCAL GLfloat xrot, yrot, zrot;
  47. UGL_LOCAL GLuint texture[1];
  48. UGL_LOCAL GLuint theTexCube;
  49. typedef struct {
  50. unsigned long sizeX;
  51. unsigned long sizeY;
  52. char *data;
  53. } TEX_IMAGE;
  54. UGL_LOCAL void cleanUp (void);
  55. UGL_LOCAL GLboolean imageLoad(char *filename, TEX_IMAGE * texImage)
  56. {
  57. FILE * file = NULL;
  58. unsigned long size;
  59. unsigned long i;
  60. unsigned short int planes;
  61. unsigned short int bpp;
  62. char temp;
  63. if ((file = fopen(filename, "rb")) == NULL)
  64. {
  65. printf("File Not Found : %s\n", filename);
  66. return GL_FALSE;
  67. }
  68. fseek(file, 18, SEEK_CUR);
  69. if ((i = fread(&texImage->sizeX, 4, 1, file)) != 1)
  70. {
  71. printf("Error reading width from %s.\n", filename);
  72. return GL_FALSE;
  73. }
  74. printf("Width of %s: %lu\n", filename, texImage->sizeX);
  75. if ((i = fread(&texImage->sizeY, 4, 1, file)) != 1)
  76. {
  77. printf("Error reading height from %s.\n", filename);
  78. return GL_FALSE;
  79. }
  80. printf("Height of %s: %lu\n", filename, texImage->sizeY);
  81. size = texImage->sizeX * texImage->sizeY * 3;
  82. if ((fread(&planes, 2, 1, file)) != 1)
  83. {
  84. printf("Error reading planes from %s.\n", filename);
  85. return GL_FALSE;
  86. }
  87. if (planes != 1)
  88. {
  89. printf("Planes from %s is not 1: %u\n", filename, planes);
  90. return GL_FALSE;
  91. }
  92. if ((i = fread(&bpp, 2, 1, file)) != 1)
  93. {
  94. printf("Error reading bpp from %s.\n", filename);
  95. return GL_FALSE;
  96. }
  97. if (bpp != 24)
  98. {
  99. printf("Bpp from %s is not 24: %u\n", filename, bpp);
  100. return GL_FALSE;
  101. }
  102. fseek(file, 24, SEEK_CUR);
  103. texImage->data = (char *) malloc(size);
  104. if (texImage->data == NULL)
  105. {
  106. printf("Error allocating memory for color-corrected texImage data");
  107. return GL_FALSE;
  108. }
  109. if ((i = fread(texImage->data, size, 1, file)) != 1)
  110. {
  111. printf("Error reading texImage data from %s.\n", filename);
  112. free(texImage->data);
  113. return GL_FALSE;
  114. }
  115. /* bgr -> rgb */
  116. for (i=0; i<size; i+=3)
  117. {
  118. temp = texImage->data[i];
  119. texImage->data[i] = texImage->data[i + 2];
  120. texImage->data[i + 2] = temp;
  121. }
  122. fclose(file);
  123. return GL_TRUE;
  124. }
  125. UGL_LOCAL void loadGLTexture()
  126. {
  127. TEX_IMAGE * texImage=NULL;
  128. texImage = (TEX_IMAGE *) malloc(sizeof(TEX_IMAGE));
  129. if (texImage == NULL)
  130. {
  131. printf("Error allocating space for image");
  132. cleanUp();
  133. exit(1);
  134. }
  135. if (!imageLoad(IMAGE_FILE, texImage))
  136. {
  137. printf("Error allocating space for image data");
  138. free(texImage);
  139. cleanUp();
  140. exit(1);
  141. }
  142. /* Create Texture */
  143. glGenTextures(1, &texture[0]);
  144. glBindTexture(GL_TEXTURE_2D, texture[0]);
  145. glTexImage2D(GL_TEXTURE_2D, 0, 3,
  146. texImage->sizeX, texImage->sizeY,
  147. 0, GL_RGB, GL_UNSIGNED_BYTE, texImage->data);
  148. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  149. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  150. free(texImage->data);
  151. free(texImage);
  152. }
  153. UGL_LOCAL void initGL(int width, int height)
  154. {
  155. /* Load the texture(s) */
  156. loadGLTexture();
  157. /* Enable texture mapping */
  158. glEnable(GL_TEXTURE_2D);
  159. /* Clear the background color to black */
  160. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  161. glEnable(GL_CULL_FACE);
  162. /* Enables smooth color shading */
  163. glShadeModel(GL_SMOOTH);
  164. /* glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); */
  165. /* glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); */
  166. theTexCube = glGenLists(1);
  167. glNewList(theTexCube, GL_COMPILE);
  168. /* Choose the texture to use */
  169. glBindTexture(GL_TEXTURE_2D, texture[0]);
  170. /* Begin drawing a cube */
  171. glBegin(GL_QUADS);
  172. /* Front face (note that the texture's corners have to match the
  173. quad's corners) */
  174. /* Bottom left of the texture and quad */
  175. glTexCoord2f(0.0f, 0.0f);
  176. glVertex3f(-1.0f, -1.0f, 1.0f);
  177. /* Bottom Right Of The Texture and Quad */
  178. glTexCoord2f(1.0f, 0.0f);
  179. glVertex3f(1.0f, -1.0f, 1.0f);
  180. /* Top Right Of The Texture and Quad */
  181. glTexCoord2f(1.0f, 1.0f);
  182. glVertex3f(1.0f, 1.0f, 1.0f);
  183. /* Top Left Of The Texture and Quad */
  184. glTexCoord2f(0.0f, 1.0f);
  185. glVertex3f(-1.0f, 1.0f, 1.0f);
  186. /* Back Face */
  187. /* Bottom Right Of The Texture and Quad */
  188. glTexCoord2f(1.0f, 0.0f);
  189. glVertex3f(-1.0f, -1.0f, -1.0f);
  190. /* Top Right Of The Texture and Quad */
  191. glTexCoord2f(1.0f, 1.0f);
  192. glVertex3f(-1.0f, 1.0f, -1.0f);
  193. /* Top Left Of The Texture and Quad */
  194. glTexCoord2f(0.0f, 1.0f);
  195. glVertex3f(1.0f, 1.0f, -1.0f);
  196. /* Bottom Left Of The Texture and Quad */
  197. glTexCoord2f(0.0f, 0.0f);
  198. glVertex3f(1.0f, -1.0f, -1.0f);
  199. /* Top Face */
  200. /* Top Left Of The Texture and Quad */
  201. glTexCoord2f(0.0f, 1.0f);
  202. glVertex3f(-1.0f, 1.0f, -1.0f);
  203. /* Bottom Left Of The Texture and Quad */
  204. glTexCoord2f(0.0f, 0.0f);
  205. glVertex3f(-1.0f, 1.0f, 1.0f);
  206. /* Bottom Right Of The Texture and Quad */
  207. glTexCoord2f(1.0f, 0.0f);
  208. glVertex3f(1.0f, 1.0f, 1.0f);
  209. /* Top Right Of The Texture and Quad */
  210. glTexCoord2f(1.0f, 1.0f);
  211. glVertex3f(1.0f, 1.0f, -1.0f);
  212. /* Bottom Face */
  213. /* Top Right Of The Texture and Quad */
  214. glTexCoord2f(1.0f, 1.0f);
  215. glVertex3f(-1.0f, -1.0f, -1.0f);
  216. /* Top Left Of The Texture and Quad */
  217. glTexCoord2f(0.0f, 1.0f);
  218. glVertex3f(1.0f, -1.0f, -1.0f);
  219. /* Bottom Left Of The Texture and Quad */
  220. glTexCoord2f(0.0f, 0.0f);
  221. glVertex3f(1.0f, -1.0f, 1.0f);
  222. /* Bottom Right Of The Texture and Quad */
  223. glTexCoord2f(1.0f, 0.0f);
  224. glVertex3f(-1.0f, -1.0f, 1.0f);
  225. /* Right face */
  226. /* Bottom Right Of The Texture and Quad */
  227. glTexCoord2f(1.0f, 0.0f);
  228. glVertex3f(1.0f, -1.0f, -1.0f);
  229. /* Top Right Of The Texture and Quad */
  230. glTexCoord2f(1.0f, 1.0f);
  231. glVertex3f(1.0f, 1.0f, -1.0f);
  232. /* Top Left Of The Texture and Quad */
  233. glTexCoord2f(0.0f, 1.0f);
  234. glVertex3f(1.0f, 1.0f, 1.0f);
  235. /* Bottom Left Of The Texture and Quad */
  236. glTexCoord2f(0.0f, 0.0f);
  237. glVertex3f(1.0f, -1.0f, 1.0f);
  238. /* Left Face */
  239. /* Bottom Left Of The Texture and Quad */
  240. glTexCoord2f(0.0f, 0.0f);
  241. glVertex3f(-1.0f, -1.0f, -1.0f);
  242. /* Bottom Right Of The Texture and Quad */
  243. glTexCoord2f(1.0f, 0.0f);
  244. glVertex3f(-1.0f, -1.0f, 1.0f);
  245. /* Top Right Of The Texture and Quad */
  246. glTexCoord2f(1.0f, 1.0f);
  247. glVertex3f(-1.0f, 1.0f, 1.0f);
  248. /* Top Left Of The Texture and Quad */
  249. glTexCoord2f(0.0f, 1.0f);
  250. glVertex3f(-1.0f, 1.0f, -1.0f);
  251. glEnd(); /* done with the polygon */
  252. glEndList();
  253. glMatrixMode(GL_PROJECTION);
  254. /* Reset the projection matrix */
  255. glLoadIdentity();
  256. /* Calculate the aspect ratio of the window */
  257. gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f);
  258. glMatrixMode(GL_MODELVIEW);
  259. glLoadIdentity();
  260. }
  261. UGL_LOCAL void drawGL()
  262. {
  263. glClear(GL_COLOR_BUFFER_BIT);
  264. /* Reset The View */
  265. glPushMatrix();
  266. /* Move 8 units into the screen */
  267. glTranslatef(0.0f, 0.0f, -8.0f);
  268. /* Rotate on the X axis */
  269. glRotatef(xrot, 1.0f, 0.0f, 0.0f);
  270. /* Rotate on the Y axis */
  271. glRotatef(yrot, 0.0f, 1.0f, 0.0f);
  272. /* Rotate On The Z Axis */
  273. glRotatef(zrot, 0.0f, 0.0f, 1.0f);
  274. glCallList(theTexCube);
  275. glFlush();
  276. uglMesaSwapBuffers();
  277. glPopMatrix();
  278. xrot += 1.6f;
  279. yrot += 1.6f;
  280. zrot += 1.6f;
  281. }
  282. UGL_LOCAL int getEvent(void)
  283. {
  284. UGL_EVENT event;
  285. UGL_STATUS status;
  286. int retVal = 0;
  287. status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
  288. while (status != UGL_STATUS_Q_EMPTY)
  289. {
  290. UGL_INPUT_EVENT * pInputEvent = (UGL_INPUT_EVENT *)&event;
  291. if (pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
  292. retVal = 1;
  293. status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
  294. }
  295. return(retVal);
  296. }
  297. UGL_LOCAL void cleanUp (void)
  298. {
  299. if (eventServiceId != UGL_NULL)
  300. uglEventQDestroy (eventServiceId, qId);
  301. uglMesaDestroyContext();
  302. uglDeinitialize();
  303. }
  304. void windMLTexCube (void);
  305. void ugltexcube (void)
  306. {
  307. taskSpawn("tTexCube", 210, VX_FP_TASK, 100000, (FUNCPTR)windMLTexCube,
  308. 0,1,2,3,4,5,6,7,8,9);
  309. }
  310. void windMLTexCube(void)
  311. {
  312. GLuint width, height;
  313. UGL_INPUT_DEVICE_ID keyboardDevId;
  314. uglInitialize();
  315. uglDriverFind (UGL_KEYBOARD_TYPE, 0, (UGL_UINT32 *)&keyboardDevId);
  316. if (uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0,
  317. (UGL_UINT32 *)&eventServiceId) == UGL_STATUS_OK)
  318. {
  319. qId = uglEventQCreate (eventServiceId, 100);
  320. }
  321. else
  322. {
  323. eventServiceId = UGL_NULL;
  324. }
  325. umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL);
  326. if (umc == NULL)
  327. {
  328. uglDeinitialize();
  329. return;
  330. }
  331. uglMesaMakeCurrentContext(umc, 0, 0,
  332. UGL_MESA_FULLSCREEN_WIDTH,
  333. UGL_MESA_FULLSCREEN_HEIGHT);
  334. uglMesaGetIntegerv(UGL_MESA_WIDTH, &width);
  335. uglMesaGetIntegerv(UGL_MESA_HEIGHT, &height);
  336. initGL(width, height);
  337. while(!getEvent())
  338. drawGL();
  339. cleanUp();
  340. return;
  341. }