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.

tri.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (C) 2008 Brian Paul All Rights Reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  18. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. /*
  22. * Draw a triangle with X/EGL and OpenGL ES 1.x
  23. * Brian Paul
  24. * 5 June 2008
  25. */
  26. #define USE_FIXED_POINT 0
  27. #include <assert.h>
  28. #include <math.h>
  29. #include <stdio.h>
  30. #include <GLES/gl.h> /* use OpenGL ES 1.x */
  31. #include <GLES/glext.h>
  32. #include <EGL/egl.h>
  33. #include "eglut.h"
  34. #define FLOAT_TO_FIXED(X) ((X) * 65535.0)
  35. static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0;
  36. static void
  37. draw(void)
  38. {
  39. #if USE_FIXED_POINT
  40. static const GLfixed verts[3][2] = {
  41. { -65536, -65536 },
  42. { 65536, -65536 },
  43. { 0, 65536 }
  44. };
  45. static const GLfixed colors[3][4] = {
  46. { 65536, 0, 0, 65536 },
  47. { 0, 65536, 0 , 65536},
  48. { 0, 0, 65536 , 65536}
  49. };
  50. #else
  51. static const GLfloat verts[3][2] = {
  52. { -1, -1 },
  53. { 1, -1 },
  54. { 0, 1 }
  55. };
  56. static const GLfloat colors[3][4] = {
  57. { 1, 0, 0, 1 },
  58. { 0, 1, 0, 1 },
  59. { 0, 0, 1, 1 }
  60. };
  61. #endif
  62. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  63. glPushMatrix();
  64. glRotatef(view_rotx, 1, 0, 0);
  65. glRotatef(view_roty, 0, 1, 0);
  66. glRotatef(view_rotz, 0, 0, 1);
  67. {
  68. #if USE_FIXED_POINT
  69. glVertexPointer(2, GL_FIXED, 0, verts);
  70. glColorPointer(4, GL_FIXED, 0, colors);
  71. #else
  72. glVertexPointer(2, GL_FLOAT, 0, verts);
  73. glColorPointer(4, GL_FLOAT, 0, colors);
  74. #endif
  75. glEnableClientState(GL_VERTEX_ARRAY);
  76. glEnableClientState(GL_COLOR_ARRAY);
  77. /* draw triangle */
  78. glDrawArrays(GL_TRIANGLES, 0, 3);
  79. /* draw some points */
  80. glPointSizex(FLOAT_TO_FIXED(15.5));
  81. glDrawArrays(GL_POINTS, 0, 3);
  82. glDisableClientState(GL_VERTEX_ARRAY);
  83. glDisableClientState(GL_COLOR_ARRAY);
  84. }
  85. if (0) {
  86. /* test code */
  87. GLfixed size;
  88. glGetFixedv(GL_POINT_SIZE, &size);
  89. printf("GL_POINT_SIZE = 0x%x %f\n", size, size / 65536.0);
  90. }
  91. glPopMatrix();
  92. }
  93. /* new window size or exposure */
  94. static void
  95. reshape(int width, int height)
  96. {
  97. GLfloat ar = (GLfloat) width / (GLfloat) height;
  98. glViewport(0, 0, (GLint) width, (GLint) height);
  99. glMatrixMode(GL_PROJECTION);
  100. glLoadIdentity();
  101. #ifdef GL_VERSION_ES_CM_1_0
  102. glFrustumf(-ar, ar, -1, 1, 5.0, 60.0);
  103. #else
  104. glFrustum(-ar, ar, -1, 1, 5.0, 60.0);
  105. #endif
  106. glMatrixMode(GL_MODELVIEW);
  107. glLoadIdentity();
  108. glTranslatef(0.0, 0.0, -10.0);
  109. }
  110. static void
  111. test_query_matrix(void)
  112. {
  113. PFNGLQUERYMATRIXXOESPROC procQueryMatrixx;
  114. typedef void (*voidproc)();
  115. GLfixed mantissa[16];
  116. GLint exponent[16];
  117. GLbitfield rv;
  118. int i;
  119. procQueryMatrixx = (PFNGLQUERYMATRIXXOESPROC) eglGetProcAddress("glQueryMatrixxOES");
  120. assert(procQueryMatrixx);
  121. /* Actually try out this one */
  122. rv = (*procQueryMatrixx)(mantissa, exponent);
  123. for (i = 0; i < 16; i++) {
  124. if (rv & (1<<i)) {
  125. printf("matrix[%d] invalid\n", i);
  126. }
  127. else {
  128. printf("matrix[%d] = %f * 2^(%d)\n", i, mantissa[i]/65536.0, exponent[i]);
  129. }
  130. }
  131. assert(!eglGetProcAddress("glFoo"));
  132. }
  133. static void
  134. init(void)
  135. {
  136. glClearColor(0.4, 0.4, 0.4, 0.0);
  137. if (0)
  138. test_query_matrix();
  139. }
  140. static void
  141. special_key(int special)
  142. {
  143. switch (special) {
  144. case EGLUT_KEY_LEFT:
  145. view_roty += 5.0;
  146. break;
  147. case EGLUT_KEY_RIGHT:
  148. view_roty -= 5.0;
  149. break;
  150. case EGLUT_KEY_UP:
  151. view_rotx += 5.0;
  152. break;
  153. case EGLUT_KEY_DOWN:
  154. view_rotx -= 5.0;
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. int
  161. main(int argc, char *argv[])
  162. {
  163. eglutInitWindowSize(300, 300);
  164. eglutInitAPIMask(EGLUT_OPENGL_ES1_BIT);
  165. eglutInit(argc, argv);
  166. eglutCreateWindow("tri");
  167. eglutReshapeFunc(reshape);
  168. eglutDisplayFunc(draw);
  169. eglutSpecialFunc(special_key);
  170. init();
  171. eglutMainLoop();
  172. return 0;
  173. }