/opengles/src/ContextMatrix.cpp

http://ftk.googlecode.com/ · C++ · 225 lines · 134 code · 42 blank · 49 comment · 28 complexity · 4940b04295317a2de3e0eee3cea9fcab MD5 · raw file

  1. // ==========================================================================
  2. //
  3. // ContextMatrix Rendering Context Class for 3D Rendering Library
  4. //
  5. // Matrix and transformaton related settings
  6. //
  7. // --------------------------------------------------------------------------
  8. //
  9. // 10-13-2003 Hans-Martin Will adaptation from prototype
  10. //
  11. // --------------------------------------------------------------------------
  12. //
  13. // Copyright (c) 2004, Hans-Martin Will. All rights reserved.
  14. //
  15. // Redistribution and use in source and binary forms, with or without
  16. // modification, are permitted provided that the following conditions are
  17. // met:
  18. //
  19. // * Redistributions of source code must retain the above copyright
  20. // notice, this list of conditions and the following disclaimer.
  21. // * Redistributions in binary form must reproduce the above copyright
  22. // notice, this list of conditions and the following disclaimer in the
  23. // documentation and/or other materials provided with the distribution.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  30. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. // THE POSSIBILITY OF SUCH DAMAGE.
  36. //
  37. // ==========================================================================
  38. #include "stdafx.h"
  39. #include "Context.h"
  40. #include "RasterizerState.h"
  41. using namespace EGL;
  42. // --------------------------------------------------------------------------
  43. // Manipulation of the matrix stacks
  44. // --------------------------------------------------------------------------
  45. void Context :: MatrixMode(GLenum mode) {
  46. switch (mode) {
  47. case GL_MODELVIEW:
  48. m_CurrentMatrixStack = &m_ModelViewMatrixStack;
  49. m_MatrixModePaletteEnabled = false;
  50. break;
  51. case GL_PROJECTION:
  52. m_CurrentMatrixStack = &m_ProjectionMatrixStack;
  53. m_MatrixModePaletteEnabled = false;
  54. break;
  55. case GL_TEXTURE:
  56. m_CurrentMatrixStack = &m_TextureMatrixStack[m_ActiveTexture];
  57. m_MatrixModePaletteEnabled = false;
  58. break;
  59. case GL_MATRIX_PALETTE_OES:
  60. m_MatrixModePaletteEnabled = true;
  61. break;
  62. default:
  63. RecordError(GL_INVALID_ENUM);
  64. return;
  65. }
  66. m_MatrixMode = mode;
  67. }
  68. void Context :: RebuildMatrices(void) {
  69. if (m_CurrentMatrixStack == &m_ModelViewMatrixStack) {
  70. m_InverseModelViewMatrix = m_ModelViewMatrixStack.CurrentMatrix().InverseUpper3(m_RescaleNormalEnabled);
  71. m_FullInverseModelViewMatrix = m_ModelViewMatrixStack.CurrentMatrix().Inverse();
  72. }
  73. }
  74. void Context :: UpdateInverseModelViewMatrix(void) {
  75. m_InverseModelViewMatrix = m_ModelViewMatrixStack.CurrentMatrix().InverseUpper3(m_RescaleNormalEnabled);
  76. for (size_t index = 0; index < MATRIX_PALETTE_SIZE; ++index) {
  77. m_MatrixPaletteInverse[index] = m_MatrixPalette[index].InverseUpper3(m_RescaleNormalEnabled);
  78. }
  79. }
  80. void Context :: LoadIdentity(void) {
  81. if (!m_MatrixModePaletteEnabled) {
  82. CurrentMatrixStack()->LoadIdentity();
  83. RebuildMatrices();
  84. } else {
  85. m_MatrixPalette[m_CurrentPaletteMatrix].MakeIdentity();
  86. m_MatrixPaletteInverse[m_CurrentPaletteMatrix].MakeIdentity();
  87. }
  88. }
  89. void Context :: LoadMatrixx(const GLfixed *m) {
  90. if (!m_MatrixModePaletteEnabled) {
  91. CurrentMatrixStack()->LoadMatrix(m);
  92. RebuildMatrices();
  93. } else {
  94. m_MatrixPalette[m_CurrentPaletteMatrix] = Matrix4x4(m);
  95. m_MatrixPaletteInverse[m_CurrentPaletteMatrix] = m_MatrixPalette[m_CurrentPaletteMatrix].InverseUpper3(m_RescaleNormalEnabled);
  96. }
  97. }
  98. void Context :: MultMatrix(const Matrix4x4 & m) {
  99. if (!m_MatrixModePaletteEnabled) {
  100. CurrentMatrixStack()->MultMatrix(m);
  101. RebuildMatrices();
  102. } else {
  103. m_MatrixPalette[m_CurrentPaletteMatrix] = m_MatrixPalette[m_CurrentPaletteMatrix] * Matrix4x4(m);
  104. m_MatrixPaletteInverse[m_CurrentPaletteMatrix] = m_MatrixPalette[m_CurrentPaletteMatrix].InverseUpper3(m_RescaleNormalEnabled);
  105. }
  106. }
  107. void Context :: MultMatrixx(const GLfixed *m) {
  108. MultMatrix(Matrix4x4(m));
  109. }
  110. void Context :: PopMatrix(void) {
  111. if (m_MatrixModePaletteEnabled) {
  112. RecordError(GL_STACK_UNDERFLOW);
  113. return;
  114. }
  115. if (CurrentMatrixStack()->PopMatrix()) {
  116. RebuildMatrices();
  117. RecordError(GL_NO_ERROR);
  118. } else {
  119. RecordError(GL_STACK_UNDERFLOW);
  120. }
  121. }
  122. void Context :: PushMatrix(void) {
  123. if (m_MatrixModePaletteEnabled) {
  124. RecordError(GL_STACK_OVERFLOW);
  125. return;
  126. }
  127. if (CurrentMatrixStack()->PushMatrix()) {
  128. RecordError(GL_NO_ERROR);
  129. } else {
  130. RecordError(GL_STACK_OVERFLOW);
  131. }
  132. }
  133. // --------------------------------------------------------------------------
  134. // Calculation of specific matrixes
  135. // --------------------------------------------------------------------------
  136. void Context :: Rotatex(GLfixed angle, GLfixed x, GLfixed y, GLfixed z) {
  137. MultMatrix(Matrix4x4::CreateRotate(angle, x, y, z));
  138. }
  139. void Context :: Scalex(GLfixed x, GLfixed y, GLfixed z) {
  140. MultMatrix(Matrix4x4::CreateScale(x, y, z));
  141. }
  142. void Context :: Translatex(GLfixed x, GLfixed y, GLfixed z) {
  143. MultMatrix(Matrix4x4::CreateTranslate(x, y, z));
  144. }
  145. void Context :: Frustumx(GLfixed left, GLfixed right, GLfixed bottom, GLfixed top, GLfixed zNear, GLfixed zFar) {
  146. if (left == right || top == bottom || zNear <= 0 || zFar <= 0) {
  147. RecordError(GL_INVALID_VALUE);
  148. return;
  149. }
  150. MultMatrix(Matrix4x4::CreateFrustrum(left, right, bottom, top, zNear, zFar));
  151. }
  152. void Context :: Orthox(GLfixed left, GLfixed right, GLfixed bottom, GLfixed top, GLfixed zNear, GLfixed zFar) {
  153. MultMatrix(Matrix4x4::CreateOrtho(left, right, bottom, top, zNear, zFar));
  154. }
  155. // --------------------------------------------------------------------------
  156. // Matrix querying extension
  157. // --------------------------------------------------------------------------
  158. GLbitfield Context :: QueryMatrixx(GLfixed mantissa[16], GLint exponent[16]) {
  159. const Matrix4x4* currentMatrix;
  160. if (!m_MatrixModePaletteEnabled) {
  161. currentMatrix = &CurrentMatrixStack()->CurrentMatrix();
  162. } else {
  163. currentMatrix = m_MatrixPalette + m_CurrentPaletteMatrix;
  164. }
  165. for (int index = 0; index < 16; ++index) {
  166. mantissa[index] = currentMatrix->Element(index);
  167. exponent[index] = 0;
  168. }
  169. return 0;
  170. }
  171. // --------------------------------------------------------------------------
  172. // Matrix palette extension
  173. // --------------------------------------------------------------------------
  174. void Context :: CurrentPaletteMatrix(GLuint index) {
  175. if (index < 0 || index > MATRIX_PALETTE_SIZE) {
  176. RecordError(GL_INVALID_VALUE);
  177. } else {
  178. m_CurrentPaletteMatrix = index;
  179. }
  180. }
  181. void Context :: LoadPaletteFromModelViewMatrix(void) {
  182. m_MatrixPalette[m_CurrentPaletteMatrix] = m_ModelViewMatrixStack.CurrentMatrix();
  183. }