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