tritex.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Calls glDrawElements() the number of times specified by
  2. // ITERATIONS. Should draw a checkerboard on the screen after
  3. // a few seconds.
  4. //
  5. // Ported from a Java version by Google.
  6. #include <EGL/egl.h>
  7. #include <GLES/gl.h>
  8. #include <GLES/glext.h>
  9. #include <WindowSurface.h>
  10. #include <EGLUtils.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <math.h>
  14. using namespace android;
  15. EGLDisplay eglDisplay;
  16. EGLSurface eglSurface;
  17. EGLContext eglContext;
  18. GLuint texture;
  19. #define FIXED_ONE 0x10000
  20. #define ITERATIONS 50
  21. int init_gl_surface(const WindowSurface&);
  22. void free_gl_surface(void);
  23. void init_scene(void);
  24. void render(int quads);
  25. void create_texture(void);
  26. int readTimer(void);
  27. static void gluLookAt(float eyeX, float eyeY, float eyeZ,
  28. float centerX, float centerY, float centerZ, float upX, float upY,
  29. float upZ)
  30. {
  31. // See the OpenGL GLUT documentation for gluLookAt for a description
  32. // of the algorithm. We implement it in a straightforward way:
  33. float fx = centerX - eyeX;
  34. float fy = centerY - eyeY;
  35. float fz = centerZ - eyeZ;
  36. // Normalize f
  37. float rlf = 1.0f / sqrtf(fx*fx + fy*fy + fz*fz);
  38. fx *= rlf;
  39. fy *= rlf;
  40. fz *= rlf;
  41. // Normalize up
  42. float rlup = 1.0f / sqrtf(upX*upX + upY*upY + upZ*upZ);
  43. upX *= rlup;
  44. upY *= rlup;
  45. upZ *= rlup;
  46. // compute s = f x up (x means "cross product")
  47. float sx = fy * upZ - fz * upY;
  48. float sy = fz * upX - fx * upZ;
  49. float sz = fx * upY - fy * upX;
  50. // compute u = s x f
  51. float ux = sy * fz - sz * fy;
  52. float uy = sz * fx - sx * fz;
  53. float uz = sx * fy - sy * fx;
  54. float m[16] ;
  55. m[0] = sx;
  56. m[1] = ux;
  57. m[2] = -fx;
  58. m[3] = 0.0f;
  59. m[4] = sy;
  60. m[5] = uy;
  61. m[6] = -fy;
  62. m[7] = 0.0f;
  63. m[8] = sz;
  64. m[9] = uz;
  65. m[10] = -fz;
  66. m[11] = 0.0f;
  67. m[12] = 0.0f;
  68. m[13] = 0.0f;
  69. m[14] = 0.0f;
  70. m[15] = 1.0f;
  71. glMultMatrixf(m);
  72. glTranslatef(-eyeX, -eyeY, -eyeZ);
  73. }
  74. int main(int argc, char **argv)
  75. {
  76. int q;
  77. int start, end;
  78. printf("Initializing EGL...\n");
  79. WindowSurface windowSurface;
  80. if(!init_gl_surface(windowSurface))
  81. {
  82. printf("GL initialisation failed - exiting\n");
  83. return 0;
  84. }
  85. init_scene();
  86. create_texture();
  87. printf("Start test...\n");
  88. render(argc==2 ? atoi(argv[1]) : ITERATIONS);
  89. free_gl_surface();
  90. return 0;
  91. }
  92. int init_gl_surface(const WindowSurface& windowSurface)
  93. {
  94. EGLint numConfigs = 1;
  95. EGLConfig myConfig = {0};
  96. EGLint attrib[] =
  97. {
  98. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  99. EGL_DEPTH_SIZE, 16,
  100. EGL_NONE
  101. };
  102. if ( (eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY )
  103. {
  104. printf("eglGetDisplay failed\n");
  105. return 0;
  106. }
  107. if ( eglInitialize(eglDisplay, NULL, NULL) != EGL_TRUE )
  108. {
  109. printf("eglInitialize failed\n");
  110. return 0;
  111. }
  112. EGLNativeWindowType window = windowSurface.getSurface();
  113. EGLUtils::selectConfigForNativeWindow(eglDisplay, attrib, window, &myConfig);
  114. if ( (eglSurface = eglCreateWindowSurface(eglDisplay, myConfig,
  115. window, 0)) == EGL_NO_SURFACE )
  116. {
  117. printf("eglCreateWindowSurface failed\n");
  118. return 0;
  119. }
  120. if ( (eglContext = eglCreateContext(eglDisplay, myConfig, 0, 0)) == EGL_NO_CONTEXT )
  121. {
  122. printf("eglCreateContext failed\n");
  123. return 0;
  124. }
  125. if ( eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext) != EGL_TRUE )
  126. {
  127. printf("eglMakeCurrent failed\n");
  128. return 0;
  129. }
  130. return 1;
  131. }
  132. void free_gl_surface(void)
  133. {
  134. if (eglDisplay != EGL_NO_DISPLAY)
  135. {
  136. eglMakeCurrent( EGL_NO_DISPLAY, EGL_NO_SURFACE,
  137. EGL_NO_SURFACE, EGL_NO_CONTEXT );
  138. eglDestroyContext( eglDisplay, eglContext );
  139. eglDestroySurface( eglDisplay, eglSurface );
  140. eglTerminate( eglDisplay );
  141. eglDisplay = EGL_NO_DISPLAY;
  142. }
  143. }
  144. void init_scene(void)
  145. {
  146. glDisable(GL_DITHER);
  147. glEnable(GL_CULL_FACE);
  148. float ratio = 320.0f / 480.0f;
  149. glViewport(0, 0, 320, 480);
  150. glMatrixMode(GL_PROJECTION);
  151. glLoadIdentity();
  152. glFrustumf(-ratio, ratio, -1, 1, 1, 10);
  153. glMatrixMode(GL_MODELVIEW);
  154. glLoadIdentity();
  155. gluLookAt(
  156. 0, 0, 3, // eye
  157. 0, 0, 0, // center
  158. 0, 1, 0); // up
  159. glEnable(GL_TEXTURE_2D);
  160. glEnableClientState(GL_VERTEX_ARRAY);
  161. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  162. }
  163. void create_texture(void)
  164. {
  165. const unsigned int on = 0xff0000ff;
  166. const unsigned int off = 0xffffffff;
  167. const unsigned int pixels[] =
  168. {
  169. on, off, on, off, on, off, on, off,
  170. off, on, off, on, off, on, off, on,
  171. on, off, on, off, on, off, on, off,
  172. off, on, off, on, off, on, off, on,
  173. on, off, on, off, on, off, on, off,
  174. off, on, off, on, off, on, off, on,
  175. on, off, on, off, on, off, on, off,
  176. off, on, off, on, off, on, off, on,
  177. };
  178. glGenTextures(1, &texture);
  179. glBindTexture(GL_TEXTURE_2D, texture);
  180. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  181. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  182. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  183. glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  184. }
  185. void render(int quads)
  186. {
  187. int i, j;
  188. const GLfloat vertices[] = {
  189. -1, -1, 0,
  190. 1, -1, 0,
  191. 1, 1, 0,
  192. -1, 1, 0
  193. };
  194. const GLfixed texCoords[] = {
  195. 0, 0,
  196. FIXED_ONE, 0,
  197. FIXED_ONE, FIXED_ONE,
  198. 0, FIXED_ONE
  199. };
  200. const GLushort quadIndices[] = { 0, 1, 2, 0, 2, 3 };
  201. GLushort* indices = (GLushort*)malloc(quads*sizeof(quadIndices));
  202. for (i=0 ; i<quads ; i++)
  203. memcpy(indices+(sizeof(quadIndices)/sizeof(indices[0]))*i, quadIndices, sizeof(quadIndices));
  204. glVertexPointer(3, GL_FLOAT, 0, vertices);
  205. glTexCoordPointer(2, GL_FIXED, 0, texCoords);
  206. // make sure to do a couple eglSwapBuffers to make sure there are
  207. // no problems with the very first ones (who knows)
  208. glClearColor(0.4, 0.4, 0.4, 0.4);
  209. glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  210. eglSwapBuffers(eglDisplay, eglSurface);
  211. glClearColor(0.6, 0.6, 0.6, 0.6);
  212. glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  213. eglSwapBuffers(eglDisplay, eglSurface);
  214. glClearColor(1.0, 1.0, 1.0, 1.0);
  215. for (j=0 ; j<10 ; j++) {
  216. printf("loop %d / 10 (%d quads / loop)\n", j, quads);
  217. int nelem = sizeof(quadIndices)/sizeof(quadIndices[0]);
  218. glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  219. glDrawElements(GL_TRIANGLES, nelem*quads, GL_UNSIGNED_SHORT, indices);
  220. eglSwapBuffers(eglDisplay, eglSurface);
  221. }
  222. free(indices);
  223. }