1 /* San Angeles Observation OpenGL ES version example
2 * Copyright 2004-2005 Jetro Lauha
4 * Web: http://iki.fi/jetro/
6 * This source is free software; you can redistribute it and/or
7 * modify it under the terms of EITHER:
8 * (1) The GNU Lesser General Public License as published by the Free
9 * Software Foundation; either version 2.1 of the License, or (at
10 * your option) any later version. The text of the GNU Lesser
11 * General Public License is included with this source in the
12 * file LICENSE-LGPL.txt.
13 * (2) The BSD-style license that is included with this source in
14 * the file LICENSE-BSD.txt.
16 * This source is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
19 * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
21 * $Id: demo.c,v 1.10 2005/02/08 20:54:39 tonic Exp $
37 // Total run length is 20 * camera track base unit length (see cams.h).
38 #define RUN_LENGTH (20 * CAMTRACK_LEN)
40 #define PI 3.1415926535897932f
41 #define RANDOM_UINT_MAX 65535
44 static unsigned long sRandomSeed = 0;
46 static void seedRandom(unsigned long seed)
51 static unsigned long randomUInt()
53 sRandomSeed = sRandomSeed * 0x343fd + 0x269ec3;
54 return sRandomSeed >> 16;
58 // Capped conversion from float to fixed.
59 static long floatToFixed(float value)
61 if (value < -32768) value = -32768;
62 if (value > 32767) value = 32767;
63 return (long)(value * 65536);
66 #define FIXED(value) floatToFixed(value)
69 // Definition of one GL object in this demo.
71 /* Vertex array and color array are enabled for all objects, so their
72 * pointers must always be valid and non-NULL. Normal array is not
73 * used by the ground plane, so when its pointer is NULL then normal
74 * array usage is disabled.
76 * Vertex array is supposed to use GL_FIXED datatype and stride 0
77 * (i.e. tightly packed array). Color array is supposed to have 4
78 * components per color with GL_UNSIGNED_BYTE datatype and stride 0.
79 * Normal array is supposed to use GL_FIXED datatype and stride 0.
84 GLint vertexComponents;
89 static long sStartTick = 0;
90 static long sTick = 0;
92 static int sCurrentCamTrack = 0;
93 static long sCurrentCamTrackStartTick = 0;
94 static long sNextCamTrackStartTick = 0x7fffffff;
96 static GLOBJECT *sSuperShapeObjects[SUPERSHAPE_COUNT] = { NULL };
97 static GLOBJECT *sGroundPlane = NULL;
105 static void freeGLObject(GLOBJECT *object)
109 free(object->normalArray);
110 free(object->colorArray);
111 free(object->vertexArray);
116 static GLOBJECT * newGLObject(long vertices, int vertexComponents,
120 result = (GLOBJECT *)malloc(sizeof(GLOBJECT));
123 result->count = vertices;
124 result->vertexComponents = vertexComponents;
125 result->vertexArray = (GLfixed *)malloc(vertices * vertexComponents *
127 result->colorArray = (GLubyte *)malloc(vertices * 4 * sizeof(GLubyte));
130 result->normalArray = (GLfixed *)malloc(vertices * 3 *
134 result->normalArray = NULL;
135 if (result->vertexArray == NULL ||
136 result->colorArray == NULL ||
137 (useNormalArray && result->normalArray == NULL))
139 freeGLObject(result);
146 static void drawGLObject(GLOBJECT *object)
148 assert(object != NULL);
150 glVertexPointer(object->vertexComponents, GL_FIXED,
151 0, object->vertexArray);
152 glColorPointer(4, GL_UNSIGNED_BYTE, 0, object->colorArray);
154 // Already done in initialization:
155 //glEnableClientState(GL_VERTEX_ARRAY);
156 //glEnableClientState(GL_COLOR_ARRAY);
158 if (object->normalArray)
160 glNormalPointer(GL_FIXED, 0, object->normalArray);
161 glEnableClientState(GL_NORMAL_ARRAY);
164 glDisableClientState(GL_NORMAL_ARRAY);
165 glDrawArrays(GL_TRIANGLES, 0, object->count);
169 static void vector3Sub(VECTOR3 *dest, VECTOR3 *v1, VECTOR3 *v2)
171 dest->x = v1->x - v2->x;
172 dest->y = v1->y - v2->y;
173 dest->z = v1->z - v2->z;
177 static void superShapeMap(VECTOR3 *point, float r1, float r2, float t, float p)
179 // sphere-mapping of supershape parameters
180 point->x = (float)(cos(t) * cos(p) / r1 / r2);
181 point->y = (float)(sin(t) * cos(p) / r1 / r2);
182 point->z = (float)(sin(p) / r2);
186 static float ssFunc(const float t, const float *p)
188 return (float)(pow(pow(fabs(cos(p[0] * t / 4)) / p[1], p[4]) +
189 pow(fabs(sin(p[0] * t / 4)) / p[2], p[5]), 1 / p[3]));
193 // Creates and returns a supershape object.
194 // Based on Paul Bourke's POV-Ray implementation.
195 // http://astronomy.swin.edu.au/~pbourke/povray/supershape/
196 static GLOBJECT * createSuperShape(const float *params)
198 const int resol1 = (int)params[SUPERSHAPE_PARAMS - 3];
199 const int resol2 = (int)params[SUPERSHAPE_PARAMS - 2];
200 // latitude 0 to pi/2 for no mirrored bottom
201 // (latitudeBegin==0 for -pi/2 to pi/2 originally)
202 const int latitudeBegin = resol2 / 4;
203 const int latitudeEnd = resol2 / 2; // non-inclusive
204 const int longitudeCount = resol1;
205 const int latitudeCount = latitudeEnd - latitudeBegin;
206 const long triangleCount = longitudeCount * latitudeCount * 2;
207 const long vertices = triangleCount * 3;
210 int a, longitude, latitude;
211 long currentVertex, currentQuad;
213 result = newGLObject(vertices, 3, 1);
217 for (a = 0; a < 3; ++a)
218 baseColor[a] = ((randomUInt() % 155) + 100) / 255.f;
223 // longitude -pi to pi
224 for (longitude = 0; longitude < longitudeCount; ++longitude)
227 // latitude 0 to pi/2
228 for (latitude = latitudeBegin; latitude < latitudeEnd; ++latitude)
230 float t1 = -PI + longitude * 2 * PI / resol1;
231 float t2 = -PI + (longitude + 1) * 2 * PI / resol1;
232 float p1 = -PI / 2 + latitude * 2 * PI / resol2;
233 float p2 = -PI / 2 + (latitude + 1) * 2 * PI / resol2;
234 float r0, r1, r2, r3;
236 r0 = ssFunc(t1, params);
237 r1 = ssFunc(p1, ¶ms[6]);
238 r2 = ssFunc(t2, params);
239 r3 = ssFunc(p2, ¶ms[6]);
241 if (r0 != 0 && r1 != 0 && r2 != 0 && r3 != 0)
243 VECTOR3 pa, pb, pc, pd;
247 //float lenSq, invLenSq;
249 superShapeMap(&pa, r0, r1, t1, p1);
250 superShapeMap(&pb, r2, r1, t2, p1);
251 superShapeMap(&pc, r2, r3, t2, p2);
252 superShapeMap(&pd, r0, r3, t1, p2);
254 // kludge to set lower edge of the object to fixed level
255 if (latitude == latitudeBegin + 1)
258 vector3Sub(&v1, &pb, &pa);
259 vector3Sub(&v2, &pd, &pa);
261 // Calculate normal with cross product.
263 * v1.x v1.y v1.z | v1.x v1.y
264 * v2.x v2.y v2.z | v2.x v2.y
267 n.x = v1.y * v2.z - v1.z * v2.y;
268 n.y = v1.z * v2.x - v1.x * v2.z;
269 n.z = v1.x * v2.y - v1.y * v2.x;
271 /* Pre-normalization of the normals is disabled here because
272 * they will be normalized anyway later due to automatic
273 * normalization (GL_NORMALIZE). It is enabled because the
274 * objects are scaled with glScale.
277 lenSq = n.x * n.x + n.y * n.y + n.z * n.z;
278 invLenSq = (float)(1 / sqrt(lenSq));
286 for (i = currentVertex * 3;
287 i < (currentVertex + 6) * 3;
290 result->normalArray[i] = FIXED(n.x);
291 result->normalArray[i + 1] = FIXED(n.y);
292 result->normalArray[i + 2] = FIXED(n.z);
294 for (i = currentVertex * 4;
295 i < (currentVertex + 6) * 4;
299 for (a = 0; a < 3; ++a)
301 color[a] = (int)(ca * baseColor[a] * 255);
302 if (color[a] > 255) color[a] = 255;
304 result->colorArray[i] = (GLubyte)color[0];
305 result->colorArray[i + 1] = (GLubyte)color[1];
306 result->colorArray[i + 2] = (GLubyte)color[2];
307 result->colorArray[i + 3] = 0;
309 result->vertexArray[currentVertex * 3] = FIXED(pa.x);
310 result->vertexArray[currentVertex * 3 + 1] = FIXED(pa.y);
311 result->vertexArray[currentVertex * 3 + 2] = FIXED(pa.z);
313 result->vertexArray[currentVertex * 3] = FIXED(pb.x);
314 result->vertexArray[currentVertex * 3 + 1] = FIXED(pb.y);
315 result->vertexArray[currentVertex * 3 + 2] = FIXED(pb.z);
317 result->vertexArray[currentVertex * 3] = FIXED(pd.x);
318 result->vertexArray[currentVertex * 3 + 1] = FIXED(pd.y);
319 result->vertexArray[currentVertex * 3 + 2] = FIXED(pd.z);
321 result->vertexArray[currentVertex * 3] = FIXED(pb.x);
322 result->vertexArray[currentVertex * 3 + 1] = FIXED(pb.y);
323 result->vertexArray[currentVertex * 3 + 2] = FIXED(pb.z);
325 result->vertexArray[currentVertex * 3] = FIXED(pc.x);
326 result->vertexArray[currentVertex * 3 + 1] = FIXED(pc.y);
327 result->vertexArray[currentVertex * 3 + 2] = FIXED(pc.z);
329 result->vertexArray[currentVertex * 3] = FIXED(pd.x);
330 result->vertexArray[currentVertex * 3 + 1] = FIXED(pd.y);
331 result->vertexArray[currentVertex * 3 + 2] = FIXED(pd.z);
333 } // r0 && r1 && r2 && r3
338 // Set number of vertices in object to the actual amount created.
339 result->count = currentVertex;
345 static GLOBJECT * createGroundPlane()
348 const int yBegin = -15, yEnd = 15; // ends are non-inclusive
349 const int xBegin = -15, xEnd = 15;
350 const long triangleCount = (yEnd - yBegin) * (xEnd - xBegin) * 2;
351 const long vertices = triangleCount * 3;
354 long currentVertex, currentQuad;
356 result = newGLObject(vertices, 2, 0);
363 for (y = yBegin; y < yEnd; ++y)
365 for (x = xBegin; x < xEnd; ++x)
369 color = (GLubyte)((randomUInt() & 0x5f) + 81); // 101 1111
370 for (i = currentVertex * 4; i < (currentVertex + 6) * 4; i += 4)
372 result->colorArray[i] = color;
373 result->colorArray[i + 1] = color;
374 result->colorArray[i + 2] = color;
375 result->colorArray[i + 3] = 0;
378 // Axis bits for quad triangles:
379 // x: 011100 (0x1c), y: 110001 (0x31) (clockwise)
380 // x: 001110 (0x0e), y: 100011 (0x23) (counter-clockwise)
381 for (a = 0; a < 6; ++a)
383 const int xm = x + ((0x1c >> a) & 1);
384 const int ym = y + ((0x31 >> a) & 1);
385 const float m = (float)(cos(xm * 2) * sin(ym * 4) * 0.75f);
386 result->vertexArray[currentVertex * 2] =
387 FIXED(xm * scale + m);
388 result->vertexArray[currentVertex * 2 + 1] =
389 FIXED(ym * scale + m);
399 static void drawGroundPlane()
401 glDisable(GL_CULL_FACE);
402 glDisable(GL_DEPTH_TEST);
404 glBlendFunc(GL_ZERO, GL_SRC_COLOR);
405 glDisable(GL_LIGHTING);
407 drawGLObject(sGroundPlane);
409 glEnable(GL_LIGHTING);
411 glEnable(GL_DEPTH_TEST);
415 static void drawFadeQuad()
417 static const GLfixed quadVertices[] = {
426 const int beginFade = sTick - sCurrentCamTrackStartTick;
427 const int endFade = sNextCamTrackStartTick - sTick;
428 const int minFade = beginFade < endFade ? beginFade : endFade;
432 const GLfixed fadeColor = minFade << 6;
433 glColor4x(fadeColor, fadeColor, fadeColor, 0);
435 glDisable(GL_DEPTH_TEST);
437 glBlendFunc(GL_ZERO, GL_SRC_COLOR);
438 glDisable(GL_LIGHTING);
440 glMatrixMode(GL_MODELVIEW);
443 glMatrixMode(GL_PROJECTION);
446 glDisableClientState(GL_COLOR_ARRAY);
447 glDisableClientState(GL_NORMAL_ARRAY);
448 glVertexPointer(2, GL_FIXED, 0, quadVertices);
449 glDrawArrays(GL_TRIANGLES, 0, 6);
451 glEnableClientState(GL_COLOR_ARRAY);
453 glMatrixMode(GL_MODELVIEW);
455 glEnable(GL_LIGHTING);
457 glEnable(GL_DEPTH_TEST);
462 // Called from the app framework.
467 glEnable(GL_NORMALIZE);
468 glEnable(GL_DEPTH_TEST);
469 glDisable(GL_CULL_FACE);
470 glShadeModel(GL_FLAT);
472 glEnable(GL_LIGHTING);
477 glEnableClientState(GL_VERTEX_ARRAY);
478 glEnableClientState(GL_COLOR_ARRAY);
482 for (a = 0; a < SUPERSHAPE_COUNT; ++a)
484 sSuperShapeObjects[a] = createSuperShape(sSuperShapeParams[a]);
485 assert(sSuperShapeObjects[a] != NULL);
487 sGroundPlane = createGroundPlane();
488 assert(sGroundPlane != NULL);
492 // Called from the app framework.
496 for (a = 0; a < SUPERSHAPE_COUNT; ++a)
497 freeGLObject(sSuperShapeObjects[a]);
498 freeGLObject(sGroundPlane);
502 static void gluPerspective(GLfloat fovy, GLfloat aspect,
503 GLfloat zNear, GLfloat zFar)
505 GLfloat xmin, xmax, ymin, ymax;
507 ymax = zNear * (GLfloat)tan(fovy * PI / 360);
509 xmin = ymin * aspect;
510 xmax = ymax * aspect;
512 glFrustumx((GLfixed)(xmin * 65536), (GLfixed)(xmax * 65536),
513 (GLfixed)(ymin * 65536), (GLfixed)(ymax * 65536),
514 (GLfixed)(zNear * 65536), (GLfixed)(zFar * 65536));
518 static void prepareFrame(int width, int height)
520 glViewport(0, 0, width, height);
522 glClearColorx((GLfixed)(0.1f * 65536),
523 (GLfixed)(0.2f * 65536),
524 (GLfixed)(0.3f * 65536), 0x10000);
525 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
527 glMatrixMode(GL_PROJECTION);
529 gluPerspective(45, (float)width / height, 0.5f, 150);
531 glMatrixMode(GL_MODELVIEW);
537 static void configureLightAndMaterial()
539 static GLfixed light0Position[] = { -0x40000, 0x10000, 0x10000, 0 };
540 static GLfixed light0Diffuse[] = { 0x10000, 0x6666, 0, 0x10000 };
541 static GLfixed light1Position[] = { 0x10000, -0x20000, -0x10000, 0 };
542 static GLfixed light1Diffuse[] = { 0x11eb, 0x23d7, 0x5999, 0x10000 };
543 static GLfixed light2Position[] = { -0x10000, 0, -0x40000, 0 };
544 static GLfixed light2Diffuse[] = { 0x11eb, 0x2b85, 0x23d7, 0x10000 };
545 static GLfixed materialSpecular[] = { 0x10000, 0x10000, 0x10000, 0x10000 };
547 glLightxv(GL_LIGHT0, GL_POSITION, light0Position);
548 glLightxv(GL_LIGHT0, GL_DIFFUSE, light0Diffuse);
549 glLightxv(GL_LIGHT1, GL_POSITION, light1Position);
550 glLightxv(GL_LIGHT1, GL_DIFFUSE, light1Diffuse);
551 glLightxv(GL_LIGHT2, GL_POSITION, light2Position);
552 glLightxv(GL_LIGHT2, GL_DIFFUSE, light2Diffuse);
553 glMaterialxv(GL_FRONT_AND_BACK, GL_SPECULAR, materialSpecular);
555 glMaterialx(GL_FRONT_AND_BACK, GL_SHININESS, 60 << 16);
556 glEnable(GL_COLOR_MATERIAL);
560 static void drawModels(float zScale)
562 const int translationScale = 9;
567 glScalex(1 << 16, 1 << 16, (GLfixed)(zScale * 65536));
569 for (y = -5; y <= 5; ++y)
571 for (x = -5; x <= 5; ++x)
576 int curShape = randomUInt() % SUPERSHAPE_COUNT;
577 buildingScale = sSuperShapeParams[curShape][SUPERSHAPE_PARAMS - 1];
578 fixedScale = (GLfixed)(buildingScale * 65536);
581 glTranslatex((x * translationScale) * 65536,
582 (y * translationScale) * 65536,
584 glRotatex((GLfixed)((randomUInt() % 360) << 16), 0, 0, 1 << 16);
585 glScalex(fixedScale, fixedScale, fixedScale);
587 drawGLObject(sSuperShapeObjects[curShape]);
592 for (x = -2; x <= 2; ++x)
594 const int shipScale100 = translationScale * 500;
595 const int offs100 = x * shipScale100 + (sTick % shipScale100);
596 float offs = offs100 * 0.01f;
597 GLfixed fixedOffs = (GLfixed)(offs * 65536);
599 glTranslatex(fixedOffs, -4 * 65536, 2 << 16);
600 drawGLObject(sSuperShapeObjects[SUPERSHAPE_COUNT - 1]);
603 glTranslatex(-4 * 65536, fixedOffs, 4 << 16);
604 glRotatex(90 << 16, 0, 0, 1 << 16);
605 drawGLObject(sSuperShapeObjects[SUPERSHAPE_COUNT - 1]);
611 /* Following gluLookAt implementation is adapted from the
612 * Mesa 3D Graphics library. http://www.mesa3d.org
614 static void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez,
615 GLfloat centerx, GLfloat centery, GLfloat centerz,
616 GLfloat upx, GLfloat upy, GLfloat upz)
619 GLfloat x[3], y[3], z[3];
622 /* Make rotation matrix */
625 z[0] = eyex - centerx;
626 z[1] = eyey - centery;
627 z[2] = eyez - centerz;
628 mag = (float)sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
629 if (mag) { /* mpichler, 19950515 */
640 /* X vector = Y cross Z */
641 x[0] = y[1] * z[2] - y[2] * z[1];
642 x[1] = -y[0] * z[2] + y[2] * z[0];
643 x[2] = y[0] * z[1] - y[1] * z[0];
645 /* Recompute Y = Z cross X */
646 y[0] = z[1] * x[2] - z[2] * x[1];
647 y[1] = -z[0] * x[2] + z[2] * x[0];
648 y[2] = z[0] * x[1] - z[1] * x[0];
650 /* mpichler, 19950515 */
651 /* cross product gives area of parallelogram, which is < 1.0 for
652 * non-perpendicular unit-length vectors; so normalize x, y here
655 mag = (float)sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
662 mag = (float)sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
669 #define M(row,col) m[col*4+row]
690 for (a = 0; a < 16; ++a)
691 fixedM[a] = (GLfixed)(m[a] * 65536);
692 glMultMatrixx(fixedM);
695 /* Translate Eye to Origin */
696 glTranslatex((GLfixed)(-eyex * 65536),
697 (GLfixed)(-eyey * 65536),
698 (GLfixed)(-eyez * 65536));
702 static void camTrack()
705 float eX, eY, eZ, cX, cY, cZ;
711 if (sNextCamTrackStartTick <= sTick)
714 sCurrentCamTrackStartTick = sNextCamTrackStartTick;
716 sNextCamTrackStartTick = sCurrentCamTrackStartTick +
717 sCamTracks[sCurrentCamTrack].len * CAMTRACK_LEN;
719 cam = &sCamTracks[sCurrentCamTrack];
720 currentCamTick = sTick - sCurrentCamTrackStartTick;
721 trackPos = (float)currentCamTick / (CAMTRACK_LEN * cam->len);
723 for (a = 0; a < 5; ++a)
724 lerp[a] = (cam->src[a] + cam->dest[a] * trackPos) * 0.01f;
728 float dist = cam->dist * 0.1f;
732 eX = cX - (float)cos(lerp[3]) * dist;
733 eY = cY - (float)sin(lerp[3]) * dist;
741 cX = eX + (float)cos(lerp[3]);
742 cY = eY + (float)sin(lerp[3]);
745 gluLookAt(eX, eY, eZ, cX, cY, cZ, 0, 0, 1);
749 // Called from the app framework.
750 /* The tick is current time in milliseconds, width and height
751 * are the image dimensions to be rendered.
753 void appRender(long tick, int width, int height)
760 // Actual tick value is "blurred" a little bit.
761 sTick = (sTick + tick - sStartTick) >> 1;
763 // Terminate application after running through the demonstration once.
764 if (sTick >= RUN_LENGTH)
770 // Prepare OpenGL ES for rendering of the frame.
771 prepareFrame(width, height);
773 // Update the camera position and set the lookat.
776 // Configure environment.
777 configureLightAndMaterial();
779 // Draw the reflection by drawing models with negated Z-axis.
784 // Blend the ground plane to the window.
787 // Draw all the models normally.
790 // Draw fade quad over whole window (when changing cameras).