| 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> |
| 23 | 24 |
| 24 #define PI 3.1415926535897932384626433832795f | 25 #define PI 3.1415926535897932384626433832795f |
| 25 | 26 |
| 26 void ESUTIL_API | 27 void esScale(ESMatrix *result, GLfloat sx, GLfloat sy, GLfloat sz) |
| 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 ESUTIL_API | 45 void esTranslate(ESMatrix *result, GLfloat tx, GLfloat ty, GLfloat tz) |
| 46 esTranslate(ESMatrix *result, GLfloat tx, GLfloat ty, GLfloat tz) | |
| 47 { | 46 { |
| 48 result->m[3][0] += (result->m[0][0] * tx + result->m[1][0] * ty + result->m[
2][0] * tz); | 47 result->m[3][0] += (result->m[0][0] * tx + result->m[1][0] * ty + result->m[
2][0] * tz); |
| 49 result->m[3][1] += (result->m[0][1] * tx + result->m[1][1] * ty + result->m[
2][1] * tz); | 48 result->m[3][1] += (result->m[0][1] * tx + result->m[1][1] * ty + result->m[
2][1] * tz); |
| 50 result->m[3][2] += (result->m[0][2] * tx + result->m[1][2] * ty + result->m[
2][2] * tz); | 49 result->m[3][2] += (result->m[0][2] * tx + result->m[1][2] * ty + result->m[
2][2] * tz); |
| 51 result->m[3][3] += (result->m[0][3] * tx + result->m[1][3] * ty + result->m[
2][3] * tz); | 50 result->m[3][3] += (result->m[0][3] * tx + result->m[1][3] * ty + result->m[
2][3] * tz); |
| 52 } | 51 } |
| 53 | 52 |
| 54 void ESUTIL_API | 53 void esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z) |
| 55 esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z) | |
| 56 { | 54 { |
| 57 GLfloat sinAngle, cosAngle; | 55 GLfloat sinAngle, cosAngle; |
| 58 GLfloat mag = sqrtf(x * x + y * y + z * z); | 56 GLfloat mag = sqrtf(x * x + y * y + z * z); |
| 59 | 57 |
| 60 sinAngle = sinf ( angle * PI / 180.0f ); | 58 sinAngle = sinf ( angle * PI / 180.0f ); |
| 61 cosAngle = cosf ( angle * PI / 180.0f ); | 59 cosAngle = cosf ( angle * PI / 180.0f ); |
| 62 if ( mag > 0.0f ) | 60 if ( mag > 0.0f ) |
| 63 { | 61 { |
| 64 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs; | 62 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs; |
| 65 GLfloat oneMinusCos; | 63 GLfloat oneMinusCos; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 95 |
| 98 rotMat.m[3][0] = 0.0F; | 96 rotMat.m[3][0] = 0.0F; |
| 99 rotMat.m[3][1] = 0.0F; | 97 rotMat.m[3][1] = 0.0F; |
| 100 rotMat.m[3][2] = 0.0F; | 98 rotMat.m[3][2] = 0.0F; |
| 101 rotMat.m[3][3] = 1.0F; | 99 rotMat.m[3][3] = 1.0F; |
| 102 | 100 |
| 103 esMatrixMultiply( result, &rotMat, result ); | 101 esMatrixMultiply( result, &rotMat, result ); |
| 104 } | 102 } |
| 105 } | 103 } |
| 106 | 104 |
| 107 void ESUTIL_API | 105 void esFrustum(ESMatrix *result, float left, float right, float bottom, float to
p, float nearZ, float farZ) |
| 108 esFrustum(ESMatrix *result, float left, float right, float bottom, float top, fl
oat nearZ, float farZ) | |
| 109 { | 106 { |
| 110 float deltaX = right - left; | 107 float deltaX = right - left; |
| 111 float deltaY = top - bottom; | 108 float deltaY = top - bottom; |
| 112 float deltaZ = farZ - nearZ; | 109 float deltaZ = farZ - nearZ; |
| 113 ESMatrix frust; | 110 ESMatrix frust; |
| 114 | 111 |
| 115 if ( (nearZ <= 0.0f) || (farZ <= 0.0f) || | 112 if ( (nearZ <= 0.0f) || (farZ <= 0.0f) || |
| 116 (deltaX <= 0.0f) || (deltaY <= 0.0f) || (deltaZ <= 0.0f) ) | 113 (deltaX <= 0.0f) || (deltaY <= 0.0f) || (deltaZ <= 0.0f) ) |
| 117 return; | 114 return; |
| 118 | 115 |
| 119 frust.m[0][0] = 2.0f * nearZ / deltaX; | 116 frust.m[0][0] = 2.0f * nearZ / deltaX; |
| 120 frust.m[0][1] = frust.m[0][2] = frust.m[0][3] = 0.0f; | 117 frust.m[0][1] = frust.m[0][2] = frust.m[0][3] = 0.0f; |
| 121 | 118 |
| 122 frust.m[1][1] = 2.0f * nearZ / deltaY; | 119 frust.m[1][1] = 2.0f * nearZ / deltaY; |
| 123 frust.m[1][0] = frust.m[1][2] = frust.m[1][3] = 0.0f; | 120 frust.m[1][0] = frust.m[1][2] = frust.m[1][3] = 0.0f; |
| 124 | 121 |
| 125 frust.m[2][0] = (right + left) / deltaX; | 122 frust.m[2][0] = (right + left) / deltaX; |
| 126 frust.m[2][1] = (top + bottom) / deltaY; | 123 frust.m[2][1] = (top + bottom) / deltaY; |
| 127 frust.m[2][2] = -(nearZ + farZ) / deltaZ; | 124 frust.m[2][2] = -(nearZ + farZ) / deltaZ; |
| 128 frust.m[2][3] = -1.0f; | 125 frust.m[2][3] = -1.0f; |
| 129 | 126 |
| 130 frust.m[3][2] = -2.0f * nearZ * farZ / deltaZ; | 127 frust.m[3][2] = -2.0f * nearZ * farZ / deltaZ; |
| 131 frust.m[3][0] = frust.m[3][1] = frust.m[3][3] = 0.0f; | 128 frust.m[3][0] = frust.m[3][1] = frust.m[3][3] = 0.0f; |
| 132 | 129 |
| 133 esMatrixMultiply(result, &frust, result); | 130 esMatrixMultiply(result, &frust, result); |
| 134 } | 131 } |
| 135 | 132 |
| 136 | 133 |
| 137 void ESUTIL_API | 134 void esPerspective(ESMatrix *result, float fovy, float aspect, float nearZ, floa
t farZ) |
| 138 esPerspective(ESMatrix *result, float fovy, float aspect, float nearZ, float far
Z) | |
| 139 { | 135 { |
| 140 GLfloat frustumW, frustumH; | 136 GLfloat frustumW, frustumH; |
| 141 | 137 |
| 142 frustumH = tanf( fovy / 360.0f * PI ) * nearZ; | 138 frustumH = tanf( fovy / 360.0f * PI ) * nearZ; |
| 143 frustumW = frustumH * aspect; | 139 frustumW = frustumH * aspect; |
| 144 | 140 |
| 145 esFrustum( result, -frustumW, frustumW, -frustumH, frustumH, nearZ, farZ ); | 141 esFrustum( result, -frustumW, frustumW, -frustumH, frustumH, nearZ, farZ ); |
| 146 } | 142 } |
| 147 | 143 |
| 148 void ESUTIL_API | 144 void esOrtho(ESMatrix *result, float left, float right, float bottom, float top,
float nearZ, float farZ) |
| 149 esOrtho(ESMatrix *result, float left, float right, float bottom, float top, floa
t nearZ, float farZ) | |
| 150 { | 145 { |
| 151 float deltaX = right - left; | 146 float deltaX = right - left; |
| 152 float deltaY = top - bottom; | 147 float deltaY = top - bottom; |
| 153 float deltaZ = farZ - nearZ; | 148 float deltaZ = farZ - nearZ; |
| 154 ESMatrix ortho; | 149 ESMatrix ortho; |
| 155 | 150 |
| 156 if ( (deltaX == 0.0f) || (deltaY == 0.0f) || (deltaZ == 0.0f) ) | 151 if ( (deltaX == 0.0f) || (deltaY == 0.0f) || (deltaZ == 0.0f) ) |
| 157 return; | 152 return; |
| 158 | 153 |
| 159 esMatrixLoadIdentity(&ortho); | 154 esMatrixLoadIdentity(&ortho); |
| 160 ortho.m[0][0] = 2.0f / deltaX; | 155 ortho.m[0][0] = 2.0f / deltaX; |
| 161 ortho.m[3][0] = -(right + left) / deltaX; | 156 ortho.m[3][0] = -(right + left) / deltaX; |
| 162 ortho.m[1][1] = 2.0f / deltaY; | 157 ortho.m[1][1] = 2.0f / deltaY; |
| 163 ortho.m[3][1] = -(top + bottom) / deltaY; | 158 ortho.m[3][1] = -(top + bottom) / deltaY; |
| 164 ortho.m[2][2] = -2.0f / deltaZ; | 159 ortho.m[2][2] = -2.0f / deltaZ; |
| 165 ortho.m[3][2] = -(nearZ + farZ) / deltaZ; | 160 ortho.m[3][2] = -(nearZ + farZ) / deltaZ; |
| 166 | 161 |
| 167 esMatrixMultiply(result, &ortho, result); | 162 esMatrixMultiply(result, &ortho, result); |
| 168 } | 163 } |
| 169 | 164 |
| 170 | 165 |
| 171 void ESUTIL_API | 166 void esMatrixMultiply(ESMatrix *result, ESMatrix *srcA, ESMatrix *srcB) |
| 172 esMatrixMultiply(ESMatrix *result, ESMatrix *srcA, ESMatrix *srcB) | |
| 173 { | 167 { |
| 174 ESMatrix tmp; | 168 ESMatrix tmp; |
| 175 int i; | 169 int i; |
| 176 | 170 |
| 177 for (i=0; i<4; i++) | 171 for (i=0; i<4; i++) |
| 178 { | 172 { |
| 179 tmp.m[i][0] = (srcA->m[i][0] * srcB->m[0][0]) + | 173 tmp.m[i][0] = (srcA->m[i][0] * srcB->m[0][0]) + |
| 180 (srcA->m[i][1] * srcB->m[1][0])
+ | 174 (srcA->m[i][1] * srcB->m[1][0])
+ |
| 181 (srcA->m[i][2] * srcB->m[2][0])
+ | 175 (srcA->m[i][2] * srcB->m[2][0])
+ |
| 182 (srcA->m[i][3] * srcB->m[3][0])
; | 176 (srcA->m[i][3] * srcB->m[3][0])
; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 193 | 187 |
| 194 tmp.m[i][3] = (srcA->m[i][0] * srcB->m[0][3]) + | 188 tmp.m[i][3] = (srcA->m[i][0] * srcB->m[0][3]) + |
| 195 (srcA->m[i][1] * srcB->m[1][3])
+ | 189 (srcA->m[i][1] * srcB->m[1][3])
+ |
| 196 (srcA->m[i][2] * srcB->m[2][3])
+ | 190 (srcA->m[i][2] * srcB->m[2][3])
+ |
| 197 (srcA->m[i][3] * srcB->m[3][3])
; | 191 (srcA->m[i][3] * srcB->m[3][3])
; |
| 198 } | 192 } |
| 199 memcpy(result, &tmp, sizeof(ESMatrix)); | 193 memcpy(result, &tmp, sizeof(ESMatrix)); |
| 200 } | 194 } |
| 201 | 195 |
| 202 | 196 |
| 203 void ESUTIL_API | 197 void esMatrixLoadIdentity(ESMatrix *result) |
| 204 esMatrixLoadIdentity(ESMatrix *result) | |
| 205 { | 198 { |
| 206 memset(result, 0x0, sizeof(ESMatrix)); | 199 memset(result, 0x0, sizeof(ESMatrix)); |
| 207 result->m[0][0] = 1.0f; | 200 result->m[0][0] = 1.0f; |
| 208 result->m[1][1] = 1.0f; | 201 result->m[1][1] = 1.0f; |
| 209 result->m[2][2] = 1.0f; | 202 result->m[2][2] = 1.0f; |
| 210 result->m[3][3] = 1.0f; | 203 result->m[3][3] = 1.0f; |
| 211 } | 204 } |
| 212 | |
| OLD | NEW |