1 //
2 // Book: OpenGL(R) ES 2.0 Programming Guide
3 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
4 // ISBN-10: 0321502795
5 // ISBN-13: 9780321502797
6 // Publisher: Addison-Wesley Professional
7 // URLs: http://safari.informit.com/9780321563835
8 // http://www.opengles-book.com
9 //
10
11 /*
12 * (c) 2009 Aaftab Munshi, Dan Ginsburg, Dave Shreiner
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be included
22 * in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 * DEALINGS IN THE SOFTWARE.
31 */
32
33 // ESUtil.c
34 //
35 // A utility library for OpenGL ES. This library provides a
36 // basic common framework for the example applications in the
37 // OpenGL ES 2.0 Programming Guide.
38 //
39
40 ///
41 // Includes
42 //
43 #include "esTransform.h"
44 #include <math.h>
45 #include <string.h>
46
47 #define PI 3.1415926535897932384626433832795f
48
esScale(ESMatrix * result,GLfloat sx,GLfloat sy,GLfloat sz)49 void esScale(ESMatrix* result, GLfloat sx, GLfloat sy, GLfloat sz)
50 {
51 result->m[0][0] *= sx;
52 result->m[0][1] *= sx;
53 result->m[0][2] *= sx;
54 result->m[0][3] *= sx;
55
56 result->m[1][0] *= sy;
57 result->m[1][1] *= sy;
58 result->m[1][2] *= sy;
59 result->m[1][3] *= sy;
60
61 result->m[2][0] *= sz;
62 result->m[2][1] *= sz;
63 result->m[2][2] *= sz;
64 result->m[2][3] *= sz;
65 }
66
esTranslate(ESMatrix * result,GLfloat tx,GLfloat ty,GLfloat tz)67 void esTranslate(ESMatrix* result, GLfloat tx, GLfloat ty, GLfloat tz)
68 {
69 result->m[3][0] += (result->m[0][0] * tx + result->m[1][0] * ty + result->m[2][0] * tz);
70 result->m[3][1] += (result->m[0][1] * tx + result->m[1][1] * ty + result->m[2][1] * tz);
71 result->m[3][2] += (result->m[0][2] * tx + result->m[1][2] * ty + result->m[2][2] * tz);
72 result->m[3][3] += (result->m[0][3] * tx + result->m[1][3] * ty + result->m[2][3] * tz);
73 }
74
esRotate(ESMatrix * result,GLfloat angle,GLfloat x,GLfloat y,GLfloat z)75 void esRotate(ESMatrix* result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
76 {
77 GLfloat sinAngle, cosAngle;
78 GLfloat mag = sqrtf(x * x + y * y + z * z);
79
80 sinAngle = sinf(angle * PI / 180.0f);
81 cosAngle = cosf(angle * PI / 180.0f);
82 if (mag > 0.0f) {
83 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs;
84 GLfloat oneMinusCos;
85 ESMatrix rotMat;
86
87 x /= mag;
88 y /= mag;
89 z /= mag;
90
91 xx = x * x;
92 yy = y * y;
93 zz = z * z;
94 xy = x * y;
95 yz = y * z;
96 zx = z * x;
97 xs = x * sinAngle;
98 ys = y * sinAngle;
99 zs = z * sinAngle;
100 oneMinusCos = 1.0f - cosAngle;
101
102 rotMat.m[0][0] = (oneMinusCos * xx) + cosAngle;
103 rotMat.m[0][1] = (oneMinusCos * xy) - zs;
104 rotMat.m[0][2] = (oneMinusCos * zx) + ys;
105 rotMat.m[0][3] = 0.0F;
106
107 rotMat.m[1][0] = (oneMinusCos * xy) + zs;
108 rotMat.m[1][1] = (oneMinusCos * yy) + cosAngle;
109 rotMat.m[1][2] = (oneMinusCos * yz) - xs;
110 rotMat.m[1][3] = 0.0F;
111
112 rotMat.m[2][0] = (oneMinusCos * zx) - ys;
113 rotMat.m[2][1] = (oneMinusCos * yz) + xs;
114 rotMat.m[2][2] = (oneMinusCos * zz) + cosAngle;
115 rotMat.m[2][3] = 0.0F;
116
117 rotMat.m[3][0] = 0.0F;
118 rotMat.m[3][1] = 0.0F;
119 rotMat.m[3][2] = 0.0F;
120 rotMat.m[3][3] = 1.0F;
121
122 esMatrixMultiply(result, &rotMat, result);
123 }
124 }
125
esFrustum(ESMatrix * result,float left,float right,float bottom,float top,float nearZ,float farZ)126 void esFrustum(ESMatrix* result, float left, float right, float bottom, float top, float nearZ, float farZ)
127 {
128 float deltaX = right - left;
129 float deltaY = top - bottom;
130 float deltaZ = farZ - nearZ;
131 ESMatrix frust;
132
133 if ((nearZ <= 0.0f) || (farZ <= 0.0f) ||
134 (deltaX <= 0.0f) || (deltaY <= 0.0f) || (deltaZ <= 0.0f))
135 return;
136
137 frust.m[0][0] = 2.0f * nearZ / deltaX;
138 frust.m[0][1] = frust.m[0][2] = frust.m[0][3] = 0.0f;
139
140 frust.m[1][1] = 2.0f * nearZ / deltaY;
141 frust.m[1][0] = frust.m[1][2] = frust.m[1][3] = 0.0f;
142
143 frust.m[2][0] = (right + left) / deltaX;
144 frust.m[2][1] = (top + bottom) / deltaY;
145 frust.m[2][2] = -(nearZ + farZ) / deltaZ;
146 frust.m[2][3] = -1.0f;
147
148 frust.m[3][2] = -2.0f * nearZ * farZ / deltaZ;
149 frust.m[3][0] = frust.m[3][1] = frust.m[3][3] = 0.0f;
150
151 esMatrixMultiply(result, &frust, result);
152 }
153
esPerspective(ESMatrix * result,float fovy,float aspect,float nearZ,float farZ)154 void esPerspective(ESMatrix* result, float fovy, float aspect, float nearZ, float farZ)
155 {
156 GLfloat frustumW, frustumH;
157
158 frustumH = tanf(fovy / 360.0f * PI) * nearZ;
159 frustumW = frustumH * aspect;
160
161 esFrustum(result, -frustumW, frustumW, -frustumH, frustumH, nearZ, farZ);
162 }
163
esOrtho(ESMatrix * result,float left,float right,float bottom,float top,float nearZ,float farZ)164 void esOrtho(ESMatrix* result, float left, float right, float bottom, float top, float nearZ, float farZ)
165 {
166 float deltaX = right - left;
167 float deltaY = top - bottom;
168 float deltaZ = farZ - nearZ;
169 ESMatrix ortho;
170
171 if ((deltaX == 0.0f) || (deltaY == 0.0f) || (deltaZ == 0.0f))
172 return;
173
174 esMatrixLoadIdentity(&ortho);
175 ortho.m[0][0] = 2.0f / deltaX;
176 ortho.m[3][0] = -(right + left) / deltaX;
177 ortho.m[1][1] = 2.0f / deltaY;
178 ortho.m[3][1] = -(top + bottom) / deltaY;
179 ortho.m[2][2] = -2.0f / deltaZ;
180 ortho.m[3][2] = -(nearZ + farZ) / deltaZ;
181
182 esMatrixMultiply(result, &ortho, result);
183 }
184
esMatrixMultiply(ESMatrix * result,ESMatrix * srcA,ESMatrix * srcB)185 void esMatrixMultiply(ESMatrix* result, ESMatrix* srcA, ESMatrix* srcB)
186 {
187 ESMatrix tmp;
188 int i;
189
190 for (i = 0; i < 4; i++) {
191 tmp.m[i][0] = (srcA->m[i][0] * srcB->m[0][0]) +
192 (srcA->m[i][1] * srcB->m[1][0]) +
193 (srcA->m[i][2] * srcB->m[2][0]) +
194 (srcA->m[i][3] * srcB->m[3][0]);
195
196 tmp.m[i][1] = (srcA->m[i][0] * srcB->m[0][1]) +
197 (srcA->m[i][1] * srcB->m[1][1]) +
198 (srcA->m[i][2] * srcB->m[2][1]) +
199 (srcA->m[i][3] * srcB->m[3][1]);
200
201 tmp.m[i][2] = (srcA->m[i][0] * srcB->m[0][2]) +
202 (srcA->m[i][1] * srcB->m[1][2]) +
203 (srcA->m[i][2] * srcB->m[2][2]) +
204 (srcA->m[i][3] * srcB->m[3][2]);
205
206 tmp.m[i][3] = (srcA->m[i][0] * srcB->m[0][3]) +
207 (srcA->m[i][1] * srcB->m[1][3]) +
208 (srcA->m[i][2] * srcB->m[2][3]) +
209 (srcA->m[i][3] * srcB->m[3][3]);
210 }
211 memcpy(result, &tmp, sizeof(ESMatrix));
212 }
213
esMatrixLoadIdentity(ESMatrix * result)214 void esMatrixLoadIdentity(ESMatrix* result)
215 {
216 memset(result, 0x0, sizeof(ESMatrix));
217 result->m[0][0] = 1.0f;
218 result->m[1][1] = 1.0f;
219 result->m[2][2] = 1.0f;
220 result->m[3][3] = 1.0f;
221 }
222