]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/kmscube.git/blob - esTransform.c
update copyright
[glsdk/kmscube.git] / esTransform.c
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 //
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  */
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 //
40 ///
41 //  Includes
42 //
43 #include "esUtil.h"
44 #include <math.h>
45 #include <string.h>
47 #define PI 3.1415926535897932384626433832795f
49 void ESUTIL_API
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;
57     result->m[1][0] *= sy;
58     result->m[1][1] *= sy;
59     result->m[1][2] *= sy;
60     result->m[1][3] *= sy;
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 }
68 void ESUTIL_API
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 }
77 void ESUTIL_API
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;
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;
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; 
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;
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; 
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;
126       esMatrixMultiply( result, &rotMat, result );
127    }
130 void ESUTIL_API
131 esFrustum(ESMatrix *result, float left, float right, float bottom, float top, float nearZ, float farZ)
133     float       deltaX = right - left;
134     float       deltaY = top - bottom;
135     float       deltaZ = farZ - nearZ;
136     ESMatrix    frust;
138     if ( (nearZ <= 0.0f) || (farZ <= 0.0f) ||
139          (deltaX <= 0.0f) || (deltaY <= 0.0f) || (deltaZ <= 0.0f) )
140          return;
142     frust.m[0][0] = 2.0f * nearZ / deltaX;
143     frust.m[0][1] = frust.m[0][2] = frust.m[0][3] = 0.0f;
145     frust.m[1][1] = 2.0f * nearZ / deltaY;
146     frust.m[1][0] = frust.m[1][2] = frust.m[1][3] = 0.0f;
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;
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;
156     esMatrixMultiply(result, &frust, result);
160 void ESUTIL_API 
161 esPerspective(ESMatrix *result, float fovy, float aspect, float nearZ, float farZ)
163    GLfloat frustumW, frustumH;
164    
165    frustumH = tanf( fovy / 360.0f * PI ) * nearZ;
166    frustumW = frustumH * aspect;
168    esFrustum( result, -frustumW, frustumW, -frustumH, frustumH, nearZ, farZ );
171 void ESUTIL_API
172 esOrtho(ESMatrix *result, float left, float right, float bottom, float top, float nearZ, float farZ)
174     float       deltaX = right - left;
175     float       deltaY = top - bottom;
176     float       deltaZ = farZ - nearZ;
177     ESMatrix    ortho;
179     if ( (deltaX == 0.0f) || (deltaY == 0.0f) || (deltaZ == 0.0f) )
180         return;
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;
190     esMatrixMultiply(result, &ortho, result);
194 void ESUTIL_API
195 esMatrixMultiply(ESMatrix *result, ESMatrix *srcA, ESMatrix *srcB)
197     ESMatrix    tmp;
198     int         i;
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]) ;
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]) ;
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]) ;
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));
226 void ESUTIL_API
227 esMatrixLoadIdentity(ESMatrix *result)
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;