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
49 void
esScale(ESMatrix * result,GLfloat sx,GLfloat sy,GLfloat sz)50 esScale(ESMatrix *result, GLfloat sx, GLfloat sy, GLfloat sz)
51 {
52 result->m[0][0] *= sx;
53 result->m[0][1] *= sx;
54 result->m[0][2] *= sx;
55 result->m[0][3] *= sx;
56
57 result->m[1][0] *= sy;
58 result->m[1][1] *= sy;
59 result->m[1][2] *= sy;
60 result->m[1][3] *= sy;
61
62 result->m[2][0] *= sz;
63 result->m[2][1] *= sz;
64 result->m[2][2] *= sz;
65 result->m[2][3] *= sz;
66 }
67
68 void
esTranslate(ESMatrix * result,GLfloat tx,GLfloat ty,GLfloat tz)69 esTranslate(ESMatrix *result, GLfloat tx, GLfloat ty, GLfloat tz)
70 {
71 result->m[3][0] += (result->m[0][0] * tx + result->m[1][0] * ty + result->m[2][0] * tz);
72 result->m[3][1] += (result->m[0][1] * tx + result->m[1][1] * ty + result->m[2][1] * tz);
73 result->m[3][2] += (result->m[0][2] * tx + result->m[1][2] * ty + result->m[2][2] * tz);
74 result->m[3][3] += (result->m[0][3] * tx + result->m[1][3] * ty + result->m[2][3] * tz);
75 }
76
77 void
esRotate(ESMatrix * result,GLfloat angle,GLfloat x,GLfloat y,GLfloat z)78 esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
79 {
80 GLfloat sinAngle, cosAngle;
81 GLfloat mag = sqrtf(x * x + y * y + z * z);
82
83 sinAngle = sinf ( angle * PI / 180.0f );
84 cosAngle = cosf ( angle * PI / 180.0f );
85 if ( mag > 0.0f )
86 {
87 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs;
88 GLfloat oneMinusCos;
89 ESMatrix rotMat;
90
91 x /= mag;
92 y /= mag;
93 z /= mag;
94
95 xx = x * x;
96 yy = y * y;
97 zz = z * z;
98 xy = x * y;
99 yz = y * z;
100 zx = z * x;
101 xs = x * sinAngle;
102 ys = y * sinAngle;
103 zs = z * sinAngle;
104 oneMinusCos = 1.0f - cosAngle;
105
106 rotMat.m[0][0] = (oneMinusCos * xx) + cosAngle;
107 rotMat.m[0][1] = (oneMinusCos * xy) - zs;
108 rotMat.m[0][2] = (oneMinusCos * zx) + ys;
109 rotMat.m[0][3] = 0.0F;
110
111 rotMat.m[1][0] = (oneMinusCos * xy) + zs;
112 rotMat.m[1][1] = (oneMinusCos * yy) + cosAngle;
113 rotMat.m[1][2] = (oneMinusCos * yz) - xs;
114 rotMat.m[1][3] = 0.0F;
115
116 rotMat.m[2][0] = (oneMinusCos * zx) - ys;
117 rotMat.m[2][1] = (oneMinusCos * yz) + xs;
118 rotMat.m[2][2] = (oneMinusCos * zz) + cosAngle;
119 rotMat.m[2][3] = 0.0F;
120
121 rotMat.m[3][0] = 0.0F;
122 rotMat.m[3][1] = 0.0F;
123 rotMat.m[3][2] = 0.0F;
124 rotMat.m[3][3] = 1.0F;
125
126 esMatrixMultiply( result, &rotMat, result );
127 }
128 }
129
130 void
esFrustum(ESMatrix * result,float left,float right,float bottom,float top,float nearZ,float farZ)131 esFrustum(ESMatrix *result, float left, float right, float bottom, float top, float nearZ, float farZ)
132 {
133 float deltaX = right - left;
134 float deltaY = top - bottom;
135 float deltaZ = farZ - nearZ;
136 ESMatrix frust;
137
138 if ( (nearZ <= 0.0f) || (farZ <= 0.0f) ||
139 (deltaX <= 0.0f) || (deltaY <= 0.0f) || (deltaZ <= 0.0f) )
140 return;
141
142 frust.m[0][0] = 2.0f * nearZ / deltaX;
143 frust.m[0][1] = frust.m[0][2] = frust.m[0][3] = 0.0f;
144
145 frust.m[1][1] = 2.0f * nearZ / deltaY;
146 frust.m[1][0] = frust.m[1][2] = frust.m[1][3] = 0.0f;
147
148 frust.m[2][0] = (right + left) / deltaX;
149 frust.m[2][1] = (top + bottom) / deltaY;
150 frust.m[2][2] = -(nearZ + farZ) / deltaZ;
151 frust.m[2][3] = -1.0f;
152
153 frust.m[3][2] = -2.0f * nearZ * farZ / deltaZ;
154 frust.m[3][0] = frust.m[3][1] = frust.m[3][3] = 0.0f;
155
156 esMatrixMultiply(result, &frust, result);
157 }
158
159
160 void
esPerspective(ESMatrix * result,float fovy,float aspect,float nearZ,float farZ)161 esPerspective(ESMatrix *result, float fovy, float aspect, float nearZ, float farZ)
162 {
163 GLfloat frustumW, frustumH;
164
165 frustumH = tanf( fovy / 360.0f * PI ) * nearZ;
166 frustumW = frustumH * aspect;
167
168 esFrustum( result, -frustumW, frustumW, -frustumH, frustumH, nearZ, farZ );
169 }
170
171 void
esOrtho(ESMatrix * result,float left,float right,float bottom,float top,float nearZ,float farZ)172 esOrtho(ESMatrix *result, float left, float right, float bottom, float top, float nearZ, float farZ)
173 {
174 float deltaX = right - left;
175 float deltaY = top - bottom;
176 float deltaZ = farZ - nearZ;
177 ESMatrix ortho;
178
179 if ( (deltaX == 0.0f) || (deltaY == 0.0f) || (deltaZ == 0.0f) )
180 return;
181
182 esMatrixLoadIdentity(&ortho);
183 ortho.m[0][0] = 2.0f / deltaX;
184 ortho.m[3][0] = -(right + left) / deltaX;
185 ortho.m[1][1] = 2.0f / deltaY;
186 ortho.m[3][1] = -(top + bottom) / deltaY;
187 ortho.m[2][2] = -2.0f / deltaZ;
188 ortho.m[3][2] = -(nearZ + farZ) / deltaZ;
189
190 esMatrixMultiply(result, &ortho, result);
191 }
192
193
194 void
esMatrixMultiply(ESMatrix * result,ESMatrix * srcA,ESMatrix * srcB)195 esMatrixMultiply(ESMatrix *result, ESMatrix *srcA, ESMatrix *srcB)
196 {
197 ESMatrix tmp;
198 int i;
199
200 for (i=0; i<4; i++)
201 {
202 tmp.m[i][0] = (srcA->m[i][0] * srcB->m[0][0]) +
203 (srcA->m[i][1] * srcB->m[1][0]) +
204 (srcA->m[i][2] * srcB->m[2][0]) +
205 (srcA->m[i][3] * srcB->m[3][0]) ;
206
207 tmp.m[i][1] = (srcA->m[i][0] * srcB->m[0][1]) +
208 (srcA->m[i][1] * srcB->m[1][1]) +
209 (srcA->m[i][2] * srcB->m[2][1]) +
210 (srcA->m[i][3] * srcB->m[3][1]) ;
211
212 tmp.m[i][2] = (srcA->m[i][0] * srcB->m[0][2]) +
213 (srcA->m[i][1] * srcB->m[1][2]) +
214 (srcA->m[i][2] * srcB->m[2][2]) +
215 (srcA->m[i][3] * srcB->m[3][2]) ;
216
217 tmp.m[i][3] = (srcA->m[i][0] * srcB->m[0][3]) +
218 (srcA->m[i][1] * srcB->m[1][3]) +
219 (srcA->m[i][2] * srcB->m[2][3]) +
220 (srcA->m[i][3] * srcB->m[3][3]) ;
221 }
222 memcpy(result, &tmp, sizeof(ESMatrix));
223 }
224
225
226 void
esMatrixLoadIdentity(ESMatrix * result)227 esMatrixLoadIdentity(ESMatrix *result)
228 {
229 memset(result, 0x0, sizeof(ESMatrix));
230 result->m[0][0] = 1.0f;
231 result->m[1][1] = 1.0f;
232 result->m[2][2] = 1.0f;
233 result->m[3][3] = 1.0f;
234 }
235
236