1 // Simple OpenGL ES 1.x application showing how to initialize and draw something.
6 #include <GLES/glext.h>
8 #include <WindowSurface.h>
16 using namespace android;
18 EGLDisplay eglDisplay;
19 EGLSurface eglSurface;
20 EGLContext eglContext;
23 #define FIXED_ONE 0x10000
26 int init_gl_surface(const WindowSurface& windowSurface);
27 void free_gl_surface(void);
28 void init_scene(void);
30 void create_texture(void);
33 static void printGLString(const char *name, GLenum s) {
34 const char *v = (const char *) glGetString(s);
35 fprintf(stderr, "GL %s = %s\n", name, v);
38 static void gluLookAt(float eyeX, float eyeY, float eyeZ,
39 float centerX, float centerY, float centerZ, float upX, float upY,
42 // See the OpenGL GLUT documentation for gluLookAt for a description
43 // of the algorithm. We implement it in a straightforward way:
45 float fx = centerX - eyeX;
46 float fy = centerY - eyeY;
47 float fz = centerZ - eyeZ;
50 float rlf = 1.0f / sqrtf(fx*fx + fy*fy + fz*fz);
56 float rlup = 1.0f / sqrtf(upX*upX + upY*upY + upZ*upZ);
61 // compute s = f x up (x means "cross product")
63 float sx = fy * upZ - fz * upY;
64 float sy = fz * upX - fx * upZ;
65 float sz = fx * upY - fy * upX;
68 float ux = sy * fz - sz * fy;
69 float uy = sz * fx - sx * fz;
70 float uz = sx * fy - sy * fx;
94 glTranslatef(-eyeX, -eyeY, -eyeZ);
97 void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) {
99 #define X(VAL) {VAL, #VAL}
100 struct {EGLint attribute; const char* name;} names[] = {
108 X(EGL_CONFIG_CAVEAT),
111 X(EGL_MAX_PBUFFER_HEIGHT),
112 X(EGL_MAX_PBUFFER_PIXELS),
113 X(EGL_MAX_PBUFFER_WIDTH),
114 X(EGL_NATIVE_RENDERABLE),
115 X(EGL_NATIVE_VISUAL_ID),
116 X(EGL_NATIVE_VISUAL_TYPE),
118 X(EGL_SAMPLE_BUFFERS),
120 X(EGL_TRANSPARENT_TYPE),
121 X(EGL_TRANSPARENT_RED_VALUE),
122 X(EGL_TRANSPARENT_GREEN_VALUE),
123 X(EGL_TRANSPARENT_BLUE_VALUE),
124 X(EGL_BIND_TO_TEXTURE_RGB),
125 X(EGL_BIND_TO_TEXTURE_RGBA),
126 X(EGL_MIN_SWAP_INTERVAL),
127 X(EGL_MAX_SWAP_INTERVAL),
128 X(EGL_LUMINANCE_SIZE),
129 X(EGL_ALPHA_MASK_SIZE),
130 X(EGL_COLOR_BUFFER_TYPE),
131 X(EGL_RENDERABLE_TYPE),
136 for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) {
138 EGLint returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value);
139 EGLint error = eglGetError();
140 if (returnVal && error == EGL_SUCCESS) {
141 printf(" %s: ", names[j].name);
142 printf("%d (0x%x)", value, value);
148 static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
149 if (returnVal != EGL_TRUE) {
150 fprintf(stderr, "%s() returned %d\n", op, returnVal);
153 for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
155 fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
160 int printEGLConfigurations(EGLDisplay dpy) {
161 EGLint numConfig = 0;
162 EGLint returnVal = eglGetConfigs(dpy, NULL, 0, &numConfig);
163 checkEglError("eglGetConfigs", returnVal);
168 printf("Number of EGL configurations: %d\n", numConfig);
170 EGLConfig* configs = (EGLConfig*) malloc(sizeof(EGLConfig) * numConfig);
172 printf("Could not allocate configs.\n");
176 returnVal = eglGetConfigs(dpy, configs, numConfig, &numConfig);
177 checkEglError("eglGetConfigs", returnVal);
183 for(int i = 0; i < numConfig; i++) {
184 printf("Configuration %d\n", i);
185 printEGLConfiguration(dpy, configs[i]);
192 int main(int argc, char **argv)
196 printf("Initializing EGL...\n");
197 WindowSurface windowSurface;
198 if(!init_gl_surface(windowSurface))
200 printf("GL initialisation failed - exiting\n");
205 printf("Running...\n");
213 int init_gl_surface(const WindowSurface& windowSurface)
215 EGLint numConfigs = 1;
216 EGLConfig myConfig = {0};
219 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
223 if ( (eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY )
225 printf("eglGetDisplay failed\n");
229 if ( eglInitialize(eglDisplay, NULL, NULL) != EGL_TRUE )
231 printf("eglInitialize failed\n");
235 if (! printEGLConfigurations(eglDisplay)) {
236 printf("printEGLConfigurations failed.\n");
240 EGLNativeWindowType window = windowSurface.getSurface();
241 EGLUtils::selectConfigForNativeWindow(eglDisplay, attrib, window, &myConfig);
243 if ( (eglSurface = eglCreateWindowSurface(eglDisplay, myConfig,
244 window, 0)) == EGL_NO_SURFACE )
246 printf("eglCreateWindowSurface failed\n");
250 if ( (eglContext = eglCreateContext(eglDisplay, myConfig, 0, 0)) == EGL_NO_CONTEXT )
252 printf("eglCreateContext failed\n");
256 if ( eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext) != EGL_TRUE )
258 printf("eglMakeCurrent failed\n");
264 eglQuerySurface(eglDisplay, eglSurface, EGL_WIDTH, &w);
265 checkEglError("eglQuerySurface");
266 eglQuerySurface(eglDisplay, eglSurface, EGL_HEIGHT, &h);
267 checkEglError("eglQuerySurface");
268 GLint dim = w < h ? w : h;
270 fprintf(stderr, "Window dimensions: %d x %d\n", w, h);
272 printGLString("Version", GL_VERSION);
273 printGLString("Vendor", GL_VENDOR);
274 printGLString("Renderer", GL_RENDERER);
275 printGLString("Extensions", GL_EXTENSIONS);
280 void free_gl_surface(void)
282 if (eglDisplay != EGL_NO_DISPLAY)
284 eglMakeCurrent( EGL_NO_DISPLAY, EGL_NO_SURFACE,
285 EGL_NO_SURFACE, EGL_NO_CONTEXT );
286 eglDestroyContext( eglDisplay, eglContext );
287 eglDestroySurface( eglDisplay, eglSurface );
288 eglTerminate( eglDisplay );
289 eglDisplay = EGL_NO_DISPLAY;
293 void init_scene(void)
295 glDisable(GL_DITHER);
296 glEnable(GL_CULL_FACE);
297 float ratio = 320.0f / 480.0f;
298 glViewport(0, 0, 320, 480);
299 glMatrixMode(GL_PROJECTION);
301 glFrustumf(-ratio, ratio, -1, 1, 1, 10);
302 glMatrixMode(GL_MODELVIEW);
308 glEnable(GL_TEXTURE_2D);
309 glEnableClientState(GL_VERTEX_ARRAY);
310 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
313 void create_texture(void)
315 const unsigned int on = 0xff0000ff;
316 const unsigned int off = 0xffffffff;
317 const unsigned int pixels[] =
319 on, off, on, off, on, off, on, off,
320 off, on, off, on, off, on, off, on,
321 on, off, on, off, on, off, on, off,
322 off, on, off, on, off, on, off, on,
323 on, off, on, off, on, off, on, off,
324 off, on, off, on, off, on, off, on,
325 on, off, on, off, on, off, on, off,
326 off, on, off, on, off, on, off, on,
329 glGenTextures(1, &texture);
330 glBindTexture(GL_TEXTURE_2D, texture);
331 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
332 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
333 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
334 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
342 const GLfloat vertices[] = {
349 const GLfixed texCoords[] = {
352 FIXED_ONE, FIXED_ONE,
356 const GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
358 glVertexPointer(3, GL_FLOAT, 0, vertices);
359 glTexCoordPointer(2, GL_FIXED, 0, texCoords);
360 glClearColor(1.0, 1.0, 1.0, 1.0);
361 int nelem = sizeof(indices)/sizeof(indices[0]);
362 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
363 glDrawElements(GL_TRIANGLES, nelem, GL_UNSIGNED_SHORT, indices);
364 eglSwapBuffers(eglDisplay, eglSurface);