PageRenderTime 64ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/gfx/angle/samples/gles2_book/Common/esTransform.c

https://bitbucket.org/MeeGoAdmin/mozilla-central/
C | 212 lines | 154 code | 40 blank | 18 comment | 12 complexity | 77ba0436eb2e2c4c3a1fe5b1aa6515e6 MD5 | raw file
Possible License(s): AGPL-1.0, MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, 0BSD, LGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0, JSON
  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. // ESUtil.c
  11. //
  12. // A utility library for OpenGL ES. This library provides a
  13. // basic common framework for the example applications in the
  14. // OpenGL ES 2.0 Programming Guide.
  15. //
  16. ///
  17. // Includes
  18. //
  19. #include "esUtil.h"
  20. #include <math.h>
  21. #define PI 3.1415926535897932384626433832795f
  22. void ESUTIL_API
  23. esScale(ESMatrix *result, GLfloat sx, GLfloat sy, GLfloat sz)
  24. {
  25. result->m[0][0] *= sx;
  26. result->m[0][1] *= sx;
  27. result->m[0][2] *= sx;
  28. result->m[0][3] *= sx;
  29. result->m[1][0] *= sy;
  30. result->m[1][1] *= sy;
  31. result->m[1][2] *= sy;
  32. result->m[1][3] *= sy;
  33. result->m[2][0] *= sz;
  34. result->m[2][1] *= sz;
  35. result->m[2][2] *= sz;
  36. result->m[2][3] *= sz;
  37. }
  38. void ESUTIL_API
  39. esTranslate(ESMatrix *result, GLfloat tx, GLfloat ty, GLfloat tz)
  40. {
  41. result->m[3][0] += (result->m[0][0] * tx + result->m[1][0] * ty + result->m[2][0] * tz);
  42. result->m[3][1] += (result->m[0][1] * tx + result->m[1][1] * ty + result->m[2][1] * tz);
  43. result->m[3][2] += (result->m[0][2] * tx + result->m[1][2] * ty + result->m[2][2] * tz);
  44. result->m[3][3] += (result->m[0][3] * tx + result->m[1][3] * ty + result->m[2][3] * tz);
  45. }
  46. void ESUTIL_API
  47. esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
  48. {
  49. GLfloat sinAngle, cosAngle;
  50. GLfloat mag = sqrtf(x * x + y * y + z * z);
  51. sinAngle = sinf ( angle * PI / 180.0f );
  52. cosAngle = cosf ( angle * PI / 180.0f );
  53. if ( mag > 0.0f )
  54. {
  55. GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs;
  56. GLfloat oneMinusCos;
  57. ESMatrix rotMat;
  58. x /= mag;
  59. y /= mag;
  60. z /= mag;
  61. xx = x * x;
  62. yy = y * y;
  63. zz = z * z;
  64. xy = x * y;
  65. yz = y * z;
  66. zx = z * x;
  67. xs = x * sinAngle;
  68. ys = y * sinAngle;
  69. zs = z * sinAngle;
  70. oneMinusCos = 1.0f - cosAngle;
  71. rotMat.m[0][0] = (oneMinusCos * xx) + cosAngle;
  72. rotMat.m[0][1] = (oneMinusCos * xy) - zs;
  73. rotMat.m[0][2] = (oneMinusCos * zx) + ys;
  74. rotMat.m[0][3] = 0.0F;
  75. rotMat.m[1][0] = (oneMinusCos * xy) + zs;
  76. rotMat.m[1][1] = (oneMinusCos * yy) + cosAngle;
  77. rotMat.m[1][2] = (oneMinusCos * yz) - xs;
  78. rotMat.m[1][3] = 0.0F;
  79. rotMat.m[2][0] = (oneMinusCos * zx) - ys;
  80. rotMat.m[2][1] = (oneMinusCos * yz) + xs;
  81. rotMat.m[2][2] = (oneMinusCos * zz) + cosAngle;
  82. rotMat.m[2][3] = 0.0F;
  83. rotMat.m[3][0] = 0.0F;
  84. rotMat.m[3][1] = 0.0F;
  85. rotMat.m[3][2] = 0.0F;
  86. rotMat.m[3][3] = 1.0F;
  87. esMatrixMultiply( result, &rotMat, result );
  88. }
  89. }
  90. void ESUTIL_API
  91. esFrustum(ESMatrix *result, float left, float right, float bottom, float top, float nearZ, float farZ)
  92. {
  93. float deltaX = right - left;
  94. float deltaY = top - bottom;
  95. float deltaZ = farZ - nearZ;
  96. ESMatrix frust;
  97. if ( (nearZ <= 0.0f) || (farZ <= 0.0f) ||
  98. (deltaX <= 0.0f) || (deltaY <= 0.0f) || (deltaZ <= 0.0f) )
  99. return;
  100. frust.m[0][0] = 2.0f * nearZ / deltaX;
  101. frust.m[0][1] = frust.m[0][2] = frust.m[0][3] = 0.0f;
  102. frust.m[1][1] = 2.0f * nearZ / deltaY;
  103. frust.m[1][0] = frust.m[1][2] = frust.m[1][3] = 0.0f;
  104. frust.m[2][0] = (right + left) / deltaX;
  105. frust.m[2][1] = (top + bottom) / deltaY;
  106. frust.m[2][2] = -(nearZ + farZ) / deltaZ;
  107. frust.m[2][3] = -1.0f;
  108. frust.m[3][2] = -2.0f * nearZ * farZ / deltaZ;
  109. frust.m[3][0] = frust.m[3][1] = frust.m[3][3] = 0.0f;
  110. esMatrixMultiply(result, &frust, result);
  111. }
  112. void ESUTIL_API
  113. esPerspective(ESMatrix *result, float fovy, float aspect, float nearZ, float farZ)
  114. {
  115. GLfloat frustumW, frustumH;
  116. frustumH = tanf( fovy / 360.0f * PI ) * nearZ;
  117. frustumW = frustumH * aspect;
  118. esFrustum( result, -frustumW, frustumW, -frustumH, frustumH, nearZ, farZ );
  119. }
  120. void ESUTIL_API
  121. esOrtho(ESMatrix *result, float left, float right, float bottom, float top, float nearZ, float farZ)
  122. {
  123. float deltaX = right - left;
  124. float deltaY = top - bottom;
  125. float deltaZ = farZ - nearZ;
  126. ESMatrix ortho;
  127. if ( (deltaX == 0.0f) || (deltaY == 0.0f) || (deltaZ == 0.0f) )
  128. return;
  129. esMatrixLoadIdentity(&ortho);
  130. ortho.m[0][0] = 2.0f / deltaX;
  131. ortho.m[3][0] = -(right + left) / deltaX;
  132. ortho.m[1][1] = 2.0f / deltaY;
  133. ortho.m[3][1] = -(top + bottom) / deltaY;
  134. ortho.m[2][2] = -2.0f / deltaZ;
  135. ortho.m[3][2] = -(nearZ + farZ) / deltaZ;
  136. esMatrixMultiply(result, &ortho, result);
  137. }
  138. void ESUTIL_API
  139. esMatrixMultiply(ESMatrix *result, ESMatrix *srcA, ESMatrix *srcB)
  140. {
  141. ESMatrix tmp;
  142. int i;
  143. for (i=0; i<4; i++)
  144. {
  145. tmp.m[i][0] = (srcA->m[i][0] * srcB->m[0][0]) +
  146. (srcA->m[i][1] * srcB->m[1][0]) +
  147. (srcA->m[i][2] * srcB->m[2][0]) +
  148. (srcA->m[i][3] * srcB->m[3][0]) ;
  149. tmp.m[i][1] = (srcA->m[i][0] * srcB->m[0][1]) +
  150. (srcA->m[i][1] * srcB->m[1][1]) +
  151. (srcA->m[i][2] * srcB->m[2][1]) +
  152. (srcA->m[i][3] * srcB->m[3][1]) ;
  153. tmp.m[i][2] = (srcA->m[i][0] * srcB->m[0][2]) +
  154. (srcA->m[i][1] * srcB->m[1][2]) +
  155. (srcA->m[i][2] * srcB->m[2][2]) +
  156. (srcA->m[i][3] * srcB->m[3][2]) ;
  157. tmp.m[i][3] = (srcA->m[i][0] * srcB->m[0][3]) +
  158. (srcA->m[i][1] * srcB->m[1][3]) +
  159. (srcA->m[i][2] * srcB->m[2][3]) +
  160. (srcA->m[i][3] * srcB->m[3][3]) ;
  161. }
  162. memcpy(result, &tmp, sizeof(ESMatrix));
  163. }
  164. void ESUTIL_API
  165. esMatrixLoadIdentity(ESMatrix *result)
  166. {
  167. memset(result, 0x0, sizeof(ESMatrix));
  168. result->m[0][0] = 1.0f;
  169. result->m[1][1] = 1.0f;
  170. result->m[2][2] = 1.0f;
  171. result->m[3][3] = 1.0f;
  172. }