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