OLD | NEW |
1 // | 1 // |
2 // Book: OpenGL(R) ES 2.0 Programming Guide | 2 // Book: OpenGL(R) ES 2.0 Programming Guide |
3 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner | 3 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner |
4 // ISBN-10: 0321502795 | 4 // ISBN-10: 0321502795 |
5 // ISBN-13: 9780321502797 | 5 // ISBN-13: 9780321502797 |
6 // Publisher: Addison-Wesley Professional | 6 // Publisher: Addison-Wesley Professional |
7 // URLs: http://safari.informit.com/9780321563835 | 7 // URLs: http://safari.informit.com/9780321563835 |
8 // http://www.opengles-book.com | 8 // http://www.opengles-book.com |
9 // | 9 // |
10 | 10 |
11 // ESUtil.c | 11 // ESUtil.c |
12 // | 12 // |
13 // A utility library for OpenGL ES. This library provides a | 13 // A utility library for OpenGL ES. This library provides a |
14 // basic common framework for the example applications in the | 14 // basic common framework for the example applications in the |
15 // OpenGL ES 2.0 Programming Guide. | 15 // OpenGL ES 2.0 Programming Guide. |
16 // | 16 // |
17 | 17 |
18 /// | 18 /// |
19 // Includes | 19 // Includes |
20 // | 20 // |
21 #include "esUtil.h" | 21 #include "esUtil.h" |
22 #include <math.h> | 22 #include <math.h> |
23 #include <string.h> | |
24 | 23 |
25 #define PI 3.1415926535897932384626433832795f | 24 #define PI 3.1415926535897932384626433832795f |
26 | 25 |
27 void esScale(ESMatrix *result, GLfloat sx, GLfloat sy, GLfloat sz) | 26 void ESUTIL_API |
| 27 esScale(ESMatrix *result, GLfloat sx, GLfloat sy, GLfloat sz) |
28 { | 28 { |
29 result->m[0][0] *= sx; | 29 result->m[0][0] *= sx; |
30 result->m[0][1] *= sx; | 30 result->m[0][1] *= sx; |
31 result->m[0][2] *= sx; | 31 result->m[0][2] *= sx; |
32 result->m[0][3] *= sx; | 32 result->m[0][3] *= sx; |
33 | 33 |
34 result->m[1][0] *= sy; | 34 result->m[1][0] *= sy; |
35 result->m[1][1] *= sy; | 35 result->m[1][1] *= sy; |
36 result->m[1][2] *= sy; | 36 result->m[1][2] *= sy; |
37 result->m[1][3] *= sy; | 37 result->m[1][3] *= sy; |
38 | 38 |
39 result->m[2][0] *= sz; | 39 result->m[2][0] *= sz; |
40 result->m[2][1] *= sz; | 40 result->m[2][1] *= sz; |
41 result->m[2][2] *= sz; | 41 result->m[2][2] *= sz; |
42 result->m[2][3] *= sz; | 42 result->m[2][3] *= sz; |
43 } | 43 } |
44 | 44 |
45 void esTranslate(ESMatrix *result, GLfloat tx, GLfloat ty, GLfloat tz) | 45 void ESUTIL_API |
| 46 esTranslate(ESMatrix *result, GLfloat tx, GLfloat ty, GLfloat tz) |
46 { | 47 { |
47 result->m[3][0] += (result->m[0][0] * tx + result->m[1][0] * ty + result->m[
2][0] * tz); | 48 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][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][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 result->m[3][3] += (result->m[0][3] * tx + result->m[1][3] * ty + result->m[
2][3] * tz); |
51 } | 52 } |
52 | 53 |
53 void esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z) | 54 void ESUTIL_API |
| 55 esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z) |
54 { | 56 { |
55 GLfloat sinAngle, cosAngle; | 57 GLfloat sinAngle, cosAngle; |
56 GLfloat mag = sqrtf(x * x + y * y + z * z); | 58 GLfloat mag = sqrtf(x * x + y * y + z * z); |
57 | 59 |
58 sinAngle = sinf ( angle * PI / 180.0f ); | 60 sinAngle = sinf ( angle * PI / 180.0f ); |
59 cosAngle = cosf ( angle * PI / 180.0f ); | 61 cosAngle = cosf ( angle * PI / 180.0f ); |
60 if ( mag > 0.0f ) | 62 if ( mag > 0.0f ) |
61 { | 63 { |
62 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs; | 64 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs; |
63 GLfloat oneMinusCos; | 65 GLfloat oneMinusCos; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 | 97 |
96 rotMat.m[3][0] = 0.0F; | 98 rotMat.m[3][0] = 0.0F; |
97 rotMat.m[3][1] = 0.0F; | 99 rotMat.m[3][1] = 0.0F; |
98 rotMat.m[3][2] = 0.0F; | 100 rotMat.m[3][2] = 0.0F; |
99 rotMat.m[3][3] = 1.0F; | 101 rotMat.m[3][3] = 1.0F; |
100 | 102 |
101 esMatrixMultiply( result, &rotMat, result ); | 103 esMatrixMultiply( result, &rotMat, result ); |
102 } | 104 } |
103 } | 105 } |
104 | 106 |
105 void esFrustum(ESMatrix *result, float left, float right, float bottom, float to
p, float nearZ, float farZ) | 107 void ESUTIL_API |
| 108 esFrustum(ESMatrix *result, float left, float right, float bottom, float top, fl
oat nearZ, float farZ) |
106 { | 109 { |
107 float deltaX = right - left; | 110 float deltaX = right - left; |
108 float deltaY = top - bottom; | 111 float deltaY = top - bottom; |
109 float deltaZ = farZ - nearZ; | 112 float deltaZ = farZ - nearZ; |
110 ESMatrix frust; | 113 ESMatrix frust; |
111 | 114 |
112 if ( (nearZ <= 0.0f) || (farZ <= 0.0f) || | 115 if ( (nearZ <= 0.0f) || (farZ <= 0.0f) || |
113 (deltaX <= 0.0f) || (deltaY <= 0.0f) || (deltaZ <= 0.0f) ) | 116 (deltaX <= 0.0f) || (deltaY <= 0.0f) || (deltaZ <= 0.0f) ) |
114 return; | 117 return; |
115 | 118 |
116 frust.m[0][0] = 2.0f * nearZ / deltaX; | 119 frust.m[0][0] = 2.0f * nearZ / deltaX; |
117 frust.m[0][1] = frust.m[0][2] = frust.m[0][3] = 0.0f; | 120 frust.m[0][1] = frust.m[0][2] = frust.m[0][3] = 0.0f; |
118 | 121 |
119 frust.m[1][1] = 2.0f * nearZ / deltaY; | 122 frust.m[1][1] = 2.0f * nearZ / deltaY; |
120 frust.m[1][0] = frust.m[1][2] = frust.m[1][3] = 0.0f; | 123 frust.m[1][0] = frust.m[1][2] = frust.m[1][3] = 0.0f; |
121 | 124 |
122 frust.m[2][0] = (right + left) / deltaX; | 125 frust.m[2][0] = (right + left) / deltaX; |
123 frust.m[2][1] = (top + bottom) / deltaY; | 126 frust.m[2][1] = (top + bottom) / deltaY; |
124 frust.m[2][2] = -(nearZ + farZ) / deltaZ; | 127 frust.m[2][2] = -(nearZ + farZ) / deltaZ; |
125 frust.m[2][3] = -1.0f; | 128 frust.m[2][3] = -1.0f; |
126 | 129 |
127 frust.m[3][2] = -2.0f * nearZ * farZ / deltaZ; | 130 frust.m[3][2] = -2.0f * nearZ * farZ / deltaZ; |
128 frust.m[3][0] = frust.m[3][1] = frust.m[3][3] = 0.0f; | 131 frust.m[3][0] = frust.m[3][1] = frust.m[3][3] = 0.0f; |
129 | 132 |
130 esMatrixMultiply(result, &frust, result); | 133 esMatrixMultiply(result, &frust, result); |
131 } | 134 } |
132 | 135 |
133 | 136 |
134 void esPerspective(ESMatrix *result, float fovy, float aspect, float nearZ, floa
t farZ) | 137 void ESUTIL_API |
| 138 esPerspective(ESMatrix *result, float fovy, float aspect, float nearZ, float far
Z) |
135 { | 139 { |
136 GLfloat frustumW, frustumH; | 140 GLfloat frustumW, frustumH; |
137 | 141 |
138 frustumH = tanf( fovy / 360.0f * PI ) * nearZ; | 142 frustumH = tanf( fovy / 360.0f * PI ) * nearZ; |
139 frustumW = frustumH * aspect; | 143 frustumW = frustumH * aspect; |
140 | 144 |
141 esFrustum( result, -frustumW, frustumW, -frustumH, frustumH, nearZ, farZ ); | 145 esFrustum( result, -frustumW, frustumW, -frustumH, frustumH, nearZ, farZ ); |
142 } | 146 } |
143 | 147 |
144 void esOrtho(ESMatrix *result, float left, float right, float bottom, float top,
float nearZ, float farZ) | 148 void ESUTIL_API |
| 149 esOrtho(ESMatrix *result, float left, float right, float bottom, float top, floa
t nearZ, float farZ) |
145 { | 150 { |
146 float deltaX = right - left; | 151 float deltaX = right - left; |
147 float deltaY = top - bottom; | 152 float deltaY = top - bottom; |
148 float deltaZ = farZ - nearZ; | 153 float deltaZ = farZ - nearZ; |
149 ESMatrix ortho; | 154 ESMatrix ortho; |
150 | 155 |
151 if ( (deltaX == 0.0f) || (deltaY == 0.0f) || (deltaZ == 0.0f) ) | 156 if ( (deltaX == 0.0f) || (deltaY == 0.0f) || (deltaZ == 0.0f) ) |
152 return; | 157 return; |
153 | 158 |
154 esMatrixLoadIdentity(&ortho); | 159 esMatrixLoadIdentity(&ortho); |
155 ortho.m[0][0] = 2.0f / deltaX; | 160 ortho.m[0][0] = 2.0f / deltaX; |
156 ortho.m[3][0] = -(right + left) / deltaX; | 161 ortho.m[3][0] = -(right + left) / deltaX; |
157 ortho.m[1][1] = 2.0f / deltaY; | 162 ortho.m[1][1] = 2.0f / deltaY; |
158 ortho.m[3][1] = -(top + bottom) / deltaY; | 163 ortho.m[3][1] = -(top + bottom) / deltaY; |
159 ortho.m[2][2] = -2.0f / deltaZ; | 164 ortho.m[2][2] = -2.0f / deltaZ; |
160 ortho.m[3][2] = -(nearZ + farZ) / deltaZ; | 165 ortho.m[3][2] = -(nearZ + farZ) / deltaZ; |
161 | 166 |
162 esMatrixMultiply(result, &ortho, result); | 167 esMatrixMultiply(result, &ortho, result); |
163 } | 168 } |
164 | 169 |
165 | 170 |
166 void esMatrixMultiply(ESMatrix *result, ESMatrix *srcA, ESMatrix *srcB) | 171 void ESUTIL_API |
| 172 esMatrixMultiply(ESMatrix *result, ESMatrix *srcA, ESMatrix *srcB) |
167 { | 173 { |
168 ESMatrix tmp; | 174 ESMatrix tmp; |
169 int i; | 175 int i; |
170 | 176 |
171 for (i=0; i<4; i++) | 177 for (i=0; i<4; i++) |
172 { | 178 { |
173 tmp.m[i][0] = (srcA->m[i][0] * srcB->m[0][0]) + | 179 tmp.m[i][0] = (srcA->m[i][0] * srcB->m[0][0]) + |
174 (srcA->m[i][1] * srcB->m[1][0])
+ | 180 (srcA->m[i][1] * srcB->m[1][0])
+ |
175 (srcA->m[i][2] * srcB->m[2][0])
+ | 181 (srcA->m[i][2] * srcB->m[2][0])
+ |
176 (srcA->m[i][3] * srcB->m[3][0])
; | 182 (srcA->m[i][3] * srcB->m[3][0])
; |
(...skipping 10 matching lines...) Expand all Loading... |
187 | 193 |
188 tmp.m[i][3] = (srcA->m[i][0] * srcB->m[0][3]) + | 194 tmp.m[i][3] = (srcA->m[i][0] * srcB->m[0][3]) + |
189 (srcA->m[i][1] * srcB->m[1][3])
+ | 195 (srcA->m[i][1] * srcB->m[1][3])
+ |
190 (srcA->m[i][2] * srcB->m[2][3])
+ | 196 (srcA->m[i][2] * srcB->m[2][3])
+ |
191 (srcA->m[i][3] * srcB->m[3][3])
; | 197 (srcA->m[i][3] * srcB->m[3][3])
; |
192 } | 198 } |
193 memcpy(result, &tmp, sizeof(ESMatrix)); | 199 memcpy(result, &tmp, sizeof(ESMatrix)); |
194 } | 200 } |
195 | 201 |
196 | 202 |
197 void esMatrixLoadIdentity(ESMatrix *result) | 203 void ESUTIL_API |
| 204 esMatrixLoadIdentity(ESMatrix *result) |
198 { | 205 { |
199 memset(result, 0x0, sizeof(ESMatrix)); | 206 memset(result, 0x0, sizeof(ESMatrix)); |
200 result->m[0][0] = 1.0f; | 207 result->m[0][0] = 1.0f; |
201 result->m[1][1] = 1.0f; | 208 result->m[1][1] = 1.0f; |
202 result->m[2][2] = 1.0f; | 209 result->m[2][2] = 1.0f; |
203 result->m[3][3] = 1.0f; | 210 result->m[3][3] = 1.0f; |
204 } | 211 } |
| 212 |
OLD | NEW |