PageRenderTime 385ms CodeModel.GetById 222ms app.highlight 19ms RepoModel.GetById 141ms app.codeStats 1ms

/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
 39
 40#include "stdafx.h"
 41#include "Context.h"
 42#include "RasterizerState.h"
 43
 44using namespace EGL;
 45
 46
 47// --------------------------------------------------------------------------
 48// Manipulation of the matrix stacks
 49// --------------------------------------------------------------------------
 50
 51void Context :: MatrixMode(GLenum mode) {
 52	switch (mode) {
 53	case GL_MODELVIEW:
 54		m_CurrentMatrixStack = &m_ModelViewMatrixStack;
 55		m_MatrixModePaletteEnabled = false;
 56		break;
 57
 58	case GL_PROJECTION:
 59		m_CurrentMatrixStack = &m_ProjectionMatrixStack;
 60		m_MatrixModePaletteEnabled = false;
 61		break;
 62
 63	case GL_TEXTURE:
 64		m_CurrentMatrixStack = &m_TextureMatrixStack[m_ActiveTexture];
 65		m_MatrixModePaletteEnabled = false;
 66		break;
 67
 68	case GL_MATRIX_PALETTE_OES:
 69		m_MatrixModePaletteEnabled = true;
 70		break;
 71
 72	default:
 73		RecordError(GL_INVALID_ENUM);
 74		return;
 75	}
 76
 77	m_MatrixMode = mode;
 78}
 79
 80void Context :: RebuildMatrices(void) {
 81	if (m_CurrentMatrixStack == &m_ModelViewMatrixStack) {
 82		m_InverseModelViewMatrix = m_ModelViewMatrixStack.CurrentMatrix().InverseUpper3(m_RescaleNormalEnabled);
 83		m_FullInverseModelViewMatrix = m_ModelViewMatrixStack.CurrentMatrix().Inverse();
 84	}
 85}
 86
 87void Context :: UpdateInverseModelViewMatrix(void) {
 88	m_InverseModelViewMatrix = m_ModelViewMatrixStack.CurrentMatrix().InverseUpper3(m_RescaleNormalEnabled);
 89
 90	for (size_t index = 0; index < MATRIX_PALETTE_SIZE; ++index) {
 91		m_MatrixPaletteInverse[index] = m_MatrixPalette[index].InverseUpper3(m_RescaleNormalEnabled);
 92	}
 93}
 94
 95void Context :: LoadIdentity(void) { 
 96
 97	if (!m_MatrixModePaletteEnabled) {
 98		CurrentMatrixStack()->LoadIdentity();
 99		RebuildMatrices();
100	} else {
101		m_MatrixPalette[m_CurrentPaletteMatrix].MakeIdentity();
102		m_MatrixPaletteInverse[m_CurrentPaletteMatrix].MakeIdentity();
103	}
104}
105
106void Context :: LoadMatrixx(const GLfixed *m) { 
107
108	if (!m_MatrixModePaletteEnabled) {
109		CurrentMatrixStack()->LoadMatrix(m);
110		RebuildMatrices();
111	} else {
112		m_MatrixPalette[m_CurrentPaletteMatrix] = Matrix4x4(m);
113		m_MatrixPaletteInverse[m_CurrentPaletteMatrix] = m_MatrixPalette[m_CurrentPaletteMatrix].InverseUpper3(m_RescaleNormalEnabled);
114	}
115}
116
117void Context :: MultMatrix(const Matrix4x4 & m) {
118	if (!m_MatrixModePaletteEnabled) {
119		CurrentMatrixStack()->MultMatrix(m);
120		RebuildMatrices();
121	} else {
122		m_MatrixPalette[m_CurrentPaletteMatrix] = m_MatrixPalette[m_CurrentPaletteMatrix] * Matrix4x4(m);
123		m_MatrixPaletteInverse[m_CurrentPaletteMatrix] = m_MatrixPalette[m_CurrentPaletteMatrix].InverseUpper3(m_RescaleNormalEnabled);
124	}
125}
126
127void Context :: MultMatrixx(const GLfixed *m) { 
128	MultMatrix(Matrix4x4(m));
129}
130
131void Context :: PopMatrix(void) { 
132
133	if (m_MatrixModePaletteEnabled) {
134		RecordError(GL_STACK_UNDERFLOW);
135		return;
136	}
137
138	if (CurrentMatrixStack()->PopMatrix()) {
139		RebuildMatrices();
140		RecordError(GL_NO_ERROR);
141	} else {
142		RecordError(GL_STACK_UNDERFLOW);
143	}
144}
145
146void Context :: PushMatrix(void) { 
147	if (m_MatrixModePaletteEnabled) {
148		RecordError(GL_STACK_OVERFLOW);
149		return;
150	}
151
152	if (CurrentMatrixStack()->PushMatrix()) {
153		RecordError(GL_NO_ERROR);
154	} else {
155		RecordError(GL_STACK_OVERFLOW);
156	}
157}
158
159// --------------------------------------------------------------------------
160// Calculation of specific matrixes
161// --------------------------------------------------------------------------
162
163void Context :: Rotatex(GLfixed angle, GLfixed x, GLfixed y, GLfixed z) { 
164	MultMatrix(Matrix4x4::CreateRotate(angle, x, y, z));
165}
166
167void Context :: Scalex(GLfixed x, GLfixed y, GLfixed z) { 
168	MultMatrix(Matrix4x4::CreateScale(x, y, z));
169}
170
171void Context :: Translatex(GLfixed x, GLfixed y, GLfixed z) { 
172	MultMatrix(Matrix4x4::CreateTranslate(x, y, z));
173}
174
175void Context :: Frustumx(GLfixed left, GLfixed right, GLfixed bottom, GLfixed top, GLfixed zNear, GLfixed zFar) { 
176
177	if (left == right || top == bottom || zNear <= 0 || zFar <= 0) {
178		RecordError(GL_INVALID_VALUE);
179		return;
180	}
181
182	MultMatrix(Matrix4x4::CreateFrustrum(left, right, bottom, top, zNear, zFar));
183}
184
185void Context :: Orthox(GLfixed left, GLfixed right, GLfixed bottom, GLfixed top, GLfixed zNear, GLfixed zFar) { 
186	MultMatrix(Matrix4x4::CreateOrtho(left, right, bottom, top, zNear, zFar));
187}
188
189// --------------------------------------------------------------------------
190// Matrix querying extension
191// --------------------------------------------------------------------------
192
193GLbitfield Context :: QueryMatrixx(GLfixed mantissa[16], GLint exponent[16]) {
194
195	const Matrix4x4* currentMatrix;
196	
197	if (!m_MatrixModePaletteEnabled) {
198		currentMatrix = &CurrentMatrixStack()->CurrentMatrix();
199	} else {
200		currentMatrix = m_MatrixPalette + m_CurrentPaletteMatrix;
201	}
202
203	for (int index = 0; index < 16; ++index) {
204		mantissa[index] = currentMatrix->Element(index);
205		exponent[index] = 0;
206	}
207
208	return 0;
209}
210
211// --------------------------------------------------------------------------
212// Matrix palette extension
213// --------------------------------------------------------------------------
214
215void Context :: CurrentPaletteMatrix(GLuint index) {
216	if (index < 0 || index > MATRIX_PALETTE_SIZE) {
217		RecordError(GL_INVALID_VALUE);
218	} else {
219		m_CurrentPaletteMatrix = index;
220	}
221}
222
223void Context :: LoadPaletteFromModelViewMatrix(void) {
224	m_MatrixPalette[m_CurrentPaletteMatrix] = m_ModelViewMatrixStack.CurrentMatrix();
225}