OLD | NEW |
| (Empty) |
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 // ESUtil.c | |
12 // | |
13 // A utility library for OpenGL ES. This library provides a | |
14 // basic common framework for the example applications in the | |
15 // OpenGL ES 2.0 Programming Guide. | |
16 // | |
17 | |
18 /// | |
19 // Includes | |
20 // | |
21 #include "esUtil.h" | |
22 #include <math.h> | |
23 #include <string.h> | |
24 | |
25 #define PI 3.1415926535897932384626433832795f | |
26 | |
27 void esScale(ESMatrix *result, GLfloat sx, GLfloat sy, GLfloat sz) | |
28 { | |
29 result->m[0][0] *= sx; | |
30 result->m[0][1] *= sx; | |
31 result->m[0][2] *= sx; | |
32 result->m[0][3] *= sx; | |
33 | |
34 result->m[1][0] *= sy; | |
35 result->m[1][1] *= sy; | |
36 result->m[1][2] *= sy; | |
37 result->m[1][3] *= sy; | |
38 | |
39 result->m[2][0] *= sz; | |
40 result->m[2][1] *= sz; | |
41 result->m[2][2] *= sz; | |
42 result->m[2][3] *= sz; | |
43 } | |
44 | |
45 void esTranslate(ESMatrix *result, GLfloat tx, GLfloat ty, GLfloat tz) | |
46 { | |
47 result->m[3][0] += (result->m[0][0] * tx + result->m[1][0] * ty + result->m[
2][0] * tz); | |
48 result->m[3][1] += (result->m[0][1] * tx + result->m[1][1] * ty + result->m[
2][1] * tz); | |
49 result->m[3][2] += (result->m[0][2] * tx + result->m[1][2] * ty + result->m[
2][2] * tz); | |
50 result->m[3][3] += (result->m[0][3] * tx + result->m[1][3] * ty + result->m[
2][3] * tz); | |
51 } | |
52 | |
53 void esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z) | |
54 { | |
55 GLfloat sinAngle, cosAngle; | |
56 GLfloat mag = sqrtf(x * x + y * y + z * z); | |
57 | |
58 sinAngle = sinf ( angle * PI / 180.0f ); | |
59 cosAngle = cosf ( angle * PI / 180.0f ); | |
60 if ( mag > 0.0f ) | |
61 { | |
62 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs; | |
63 GLfloat oneMinusCos; | |
64 ESMatrix rotMat; | |
65 | |
66 x /= mag; | |
67 y /= mag; | |
68 z /= mag; | |
69 | |
70 xx = x * x; | |
71 yy = y * y; | |
72 zz = z * z; | |
73 xy = x * y; | |
74 yz = y * z; | |
75 zx = z * x; | |
76 xs = x * sinAngle; | |
77 ys = y * sinAngle; | |
78 zs = z * sinAngle; | |
79 oneMinusCos = 1.0f - cosAngle; | |
80 | |
81 rotMat.m[0][0] = (oneMinusCos * xx) + cosAngle; | |
82 rotMat.m[0][1] = (oneMinusCos * xy) - zs; | |
83 rotMat.m[0][2] = (oneMinusCos * zx) + ys; | |
84 rotMat.m[0][3] = 0.0F; | |
85 | |
86 rotMat.m[1][0] = (oneMinusCos * xy) + zs; | |
87 rotMat.m[1][1] = (oneMinusCos * yy) + cosAngle; | |
88 rotMat.m[1][2] = (oneMinusCos * yz) - xs; | |
89 rotMat.m[1][3] = 0.0F; | |
90 | |
91 rotMat.m[2][0] = (oneMinusCos * zx) - ys; | |
92 rotMat.m[2][1] = (oneMinusCos * yz) + xs; | |
93 rotMat.m[2][2] = (oneMinusCos * zz) + cosAngle; | |
94 rotMat.m[2][3] = 0.0F; | |
95 | |
96 rotMat.m[3][0] = 0.0F; | |
97 rotMat.m[3][1] = 0.0F; | |
98 rotMat.m[3][2] = 0.0F; | |
99 rotMat.m[3][3] = 1.0F; | |
100 | |
101 esMatrixMultiply( result, &rotMat, result ); | |
102 } | |
103 } | |
104 | |
105 void esFrustum(ESMatrix *result, float left, float right, float bottom, float to
p, float nearZ, float farZ) | |
106 { | |
107 float deltaX = right - left; | |
108 float deltaY = top - bottom; | |
109 float deltaZ = farZ - nearZ; | |
110 ESMatrix frust; | |
111 | |
112 if ( (nearZ <= 0.0f) || (farZ <= 0.0f) || | |
113 (deltaX <= 0.0f) || (deltaY <= 0.0f) || (deltaZ <= 0.0f) ) | |
114 return; | |
115 | |
116 frust.m[0][0] = 2.0f * nearZ / deltaX; | |
117 frust.m[0][1] = frust.m[0][2] = frust.m[0][3] = 0.0f; | |
118 | |
119 frust.m[1][1] = 2.0f * nearZ / deltaY; | |
120 frust.m[1][0] = frust.m[1][2] = frust.m[1][3] = 0.0f; | |
121 | |
122 frust.m[2][0] = (right + left) / deltaX; | |
123 frust.m[2][1] = (top + bottom) / deltaY; | |
124 frust.m[2][2] = -(nearZ + farZ) / deltaZ; | |
125 frust.m[2][3] = -1.0f; | |
126 | |
127 frust.m[3][2] = -2.0f * nearZ * farZ / deltaZ; | |
128 frust.m[3][0] = frust.m[3][1] = frust.m[3][3] = 0.0f; | |
129 | |
130 esMatrixMultiply(result, &frust, result); | |
131 } | |
132 | |
133 | |
134 void esPerspective(ESMatrix *result, float fovy, float aspect, float nearZ, floa
t farZ) | |
135 { | |
136 GLfloat frustumW, frustumH; | |
137 | |
138 frustumH = tanf( fovy / 360.0f * PI ) * nearZ; | |
139 frustumW = frustumH * aspect; | |
140 | |
141 esFrustum( result, -frustumW, frustumW, -frustumH, frustumH, nearZ, farZ ); | |
142 } | |
143 | |
144 void esOrtho(ESMatrix *result, float left, float right, float bottom, float top,
float nearZ, float farZ) | |
145 { | |
146 float deltaX = right - left; | |
147 float deltaY = top - bottom; | |
148 float deltaZ = farZ - nearZ; | |
149 ESMatrix ortho; | |
150 | |
151 if ( (deltaX == 0.0f) || (deltaY == 0.0f) || (deltaZ == 0.0f) ) | |
152 return; | |
153 | |
154 esMatrixLoadIdentity(&ortho); | |
155 ortho.m[0][0] = 2.0f / deltaX; | |
156 ortho.m[3][0] = -(right + left) / deltaX; | |
157 ortho.m[1][1] = 2.0f / deltaY; | |
158 ortho.m[3][1] = -(top + bottom) / deltaY; | |
159 ortho.m[2][2] = -2.0f / deltaZ; | |
160 ortho.m[3][2] = -(nearZ + farZ) / deltaZ; | |
161 | |
162 esMatrixMultiply(result, &ortho, result); | |
163 } | |
164 | |
165 | |
166 void esMatrixMultiply(ESMatrix *result, ESMatrix *srcA, ESMatrix *srcB) | |
167 { | |
168 ESMatrix tmp; | |
169 int i; | |
170 | |
171 for (i=0; i<4; i++) | |
172 { | |
173 tmp.m[i][0] = (srcA->m[i][0] * srcB->m[0][0]) + | |
174 (srcA->m[i][1] * srcB->m[1][0])
+ | |
175 (srcA->m[i][2] * srcB->m[2][0])
+ | |
176 (srcA->m[i][3] * srcB->m[3][0])
; | |
177 | |
178 tmp.m[i][1] = (srcA->m[i][0] * srcB->m[0][1]) + | |
179 (srcA->m[i][1] * srcB->m[1][1])
+ | |
180 (srcA->m[i][2] * srcB->m[2][1])
+ | |
181 (srcA->m[i][3] * srcB->m[3][1])
; | |
182 | |
183 tmp.m[i][2] = (srcA->m[i][0] * srcB->m[0][2]) + | |
184 (srcA->m[i][1] * srcB->m[1][2])
+ | |
185 (srcA->m[i][2] * srcB->m[2][2])
+ | |
186 (srcA->m[i][3] * srcB->m[3][2])
; | |
187 | |
188 tmp.m[i][3] = (srcA->m[i][0] * srcB->m[0][3]) + | |
189 (srcA->m[i][1] * srcB->m[1][3])
+ | |
190 (srcA->m[i][2] * srcB->m[2][3])
+ | |
191 (srcA->m[i][3] * srcB->m[3][3])
; | |
192 } | |
193 memcpy(result, &tmp, sizeof(ESMatrix)); | |
194 } | |
195 | |
196 | |
197 void esMatrixLoadIdentity(ESMatrix *result) | |
198 { | |
199 memset(result, 0x0, sizeof(ESMatrix)); | |
200 result->m[0][0] = 1.0f; | |
201 result->m[1][1] = 1.0f; | |
202 result->m[2][2] = 1.0f; | |
203 result->m[3][3] = 1.0f; | |
204 } | |
OLD | NEW |