PageRenderTime 405ms CodeModel.GetById 191ms app.highlight 93ms RepoModel.GetById 119ms app.codeStats 0ms

/opengles/src/ContextLight.cpp

http://ftk.googlecode.com/
C++ | 285 lines | 185 code | 60 blank | 40 comment | 30 complexity | b49c296ab119fe7e2dded2459fbe8b4c MD5 | raw file
  1// ==========================================================================
  2//
  3// ContextLight.cpp	Rendering Context Class for 3D Rendering Library
  4//
  5//					Lighting and Shading related settings
  6//
  7// --------------------------------------------------------------------------
  8//
  9// 08-02-2003	Hans-Martin Will	initial version
 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#include "Utils.h"
 44
 45
 46using namespace EGL;
 47
 48
 49// ==========================================================================
 50// class Context
 51// ==========================================================================
 52
 53void Context :: Fogx(GLenum pname, GLfixed param) { 
 54	switch (pname) {
 55		case GL_FOG_MODE:
 56			switch (param) {
 57				default:
 58					RecordError(GL_INVALID_ENUM);
 59					break;
 60
 61				case GL_LINEAR:
 62					m_FogMode = FogLinear;
 63					break;
 64
 65				case GL_EXP:
 66					m_FogMode = FogModeExp;
 67					break;
 68
 69				case GL_EXP2:
 70					m_FogMode = FogModeExp2;
 71					break;
 72
 73			}
 74
 75			break;
 76
 77		case GL_FOG_START:
 78			m_FogStart = param;
 79			goto calculateFog;
 80
 81		case GL_FOG_END:
 82			m_FogEnd = param;
 83
 84calculateFog:
 85			if (m_FogStart != m_FogEnd) {
 86				EGL_Fixed diff = m_FogEnd - m_FogStart;
 87
 88				if ((diff >> 24) && (diff >> 24) + 1) {
 89					m_FogGradient = EGL_Inverse(diff >> 8);
 90					m_FogGradientShift = 8;
 91				} else {
 92					m_FogGradient = EGL_Inverse(diff);
 93					m_FogGradientShift = 0;
 94				}
 95			}
 96
 97			break;
 98
 99		case GL_FOG_DENSITY:
100			if (param < 0) {
101				RecordError(GL_INVALID_VALUE);
102			} else {
103				m_FogDensity = param;
104			}
105
106			break;
107
108		default:
109			RecordError(GL_INVALID_ENUM);
110	}
111}
112
113void Context :: Fogxv(GLenum pname, const GLfixed *params) { 
114	switch (pname) {
115		case GL_FOG_COLOR:
116			GetRasterizerState()->SetFogColor(FractionalColor(params));
117			break;
118
119		default:
120			Fogx(pname, *params);
121			break;
122	}
123}
124
125void Context :: LightModelx(GLenum pname, GLfixed param) { 
126	switch (pname) {
127	case GL_LIGHT_MODEL_TWO_SIDE:
128		m_TwoSidedLightning = (param != 0);
129		break;
130	default:
131		RecordError(GL_INVALID_ENUM);
132	}
133}
134
135void Context :: LightModelxv(GLenum pname, const GLfixed *params) { 
136
137	switch (pname) {
138	case GL_LIGHT_MODEL_AMBIENT:
139		m_LightModelAmbient = FractionalColor::Clamp(params);
140		break;
141
142	default:
143		LightModelx(pname, *params);
144		return;
145	}
146}
147
148void Context :: Lightx(GLenum light, GLenum pname, GLfixed param) { 
149
150	if (light < GL_LIGHT0 || light > GL_LIGHT7) {
151		RecordError(GL_INVALID_ENUM);
152		return;
153	}
154
155	Light * pLight = m_Lights + (light - GL_LIGHT0);
156
157	switch (pname) {
158	case GL_SPOT_EXPONENT:
159		if (param < 0 || param > EGL_FixedFromInt(128)) {
160			RecordError(GL_INVALID_VALUE);
161		} else {
162			pLight->SetSpotExponent(param);
163		}
164
165		break;
166
167	case GL_SPOT_CUTOFF:
168		if (param < 0 || param > EGL_FixedFromInt(90) && param != EGL_FixedFromInt(180)) {
169			RecordError(GL_INVALID_VALUE);
170		} else {
171			pLight->SetSpotCutoff(param);
172		}
173
174		break;
175
176	case GL_CONSTANT_ATTENUATION:
177		pLight->SetConstantAttenuation(param);
178		break;
179
180	case GL_LINEAR_ATTENUATION:
181		pLight->SetLinearAttenuation(param);
182		break;
183
184	case GL_QUADRATIC_ATTENUATION:
185		pLight->SetQuadraticAttenuation(param);
186		break;
187
188	default:
189		RecordError(GL_INVALID_ENUM);
190		return;
191	}
192}
193
194void Context :: Lightxv(GLenum light, GLenum pname, const GLfixed *params) { 
195	if (light < GL_LIGHT0 || light > GL_LIGHT7) {
196		RecordError(GL_INVALID_ENUM);
197		return;
198	}
199
200	Light * pLight = m_Lights + (light - GL_LIGHT0);
201
202	switch (pname) {
203	case GL_AMBIENT:
204		pLight->SetAmbientColor(FractionalColor::Clamp(params));
205		break;
206
207	case GL_DIFFUSE:
208		pLight->SetDiffuseColor(FractionalColor::Clamp(params));
209		break;
210
211	case GL_SPECULAR:
212		pLight->SetSpecularColor(FractionalColor::Clamp(params));
213		break;
214
215	case GL_POSITION:
216		pLight->SetPosition(m_ModelViewMatrixStack.CurrentMatrix() * Vec4D(params));
217		break;
218
219	case GL_SPOT_DIRECTION:
220		pLight->SetDirection(m_ModelViewMatrixStack.CurrentMatrix().Multiply3x3(Vec3D(params)));
221		break;
222
223	default:
224		Lightx(light, pname, *params);
225		return;
226	}
227}
228
229bool Context :: GetLightxv(GLenum light, GLenum pname, GLfixed *params) {
230	if (light < GL_LIGHT0 || light > GL_LIGHT7) {
231		RecordError(GL_INVALID_ENUM);
232		return false;
233	}
234
235	Light * pLight = m_Lights + (light - GL_LIGHT0);
236
237	switch (pname) {
238	case GL_AMBIENT:
239		CopyColor(pLight->GetAmbientColor(), params);
240		break;
241
242	case GL_DIFFUSE:
243		CopyColor(pLight->GetDiffuseColor(), params);
244		break;
245
246	case GL_SPECULAR:
247		CopyColor(pLight->GetSpecularColor(), params);
248		break;
249
250	case GL_POSITION:
251		CopyVector(pLight->GetPosition(), params);
252		break;
253
254	case GL_SPOT_DIRECTION:
255		CopyVector(pLight->GetDirection(), params);
256		break;
257
258	case GL_SPOT_EXPONENT:
259		params[0] = pLight->GetSpotExponent();
260		break;
261
262	case GL_SPOT_CUTOFF:
263		params[0] = pLight->GetSpotCutoff();
264		break;
265
266	case GL_CONSTANT_ATTENUATION:
267		params[0] = pLight->GetConstantAttenuation();
268		break;
269
270	case GL_LINEAR_ATTENUATION:
271		params[0] = pLight->GetLinearAttenuation();
272		break;
273
274	case GL_QUADRATIC_ATTENUATION:
275		params[0] = pLight->GetQuadraticAttenuation();
276		break;
277
278	default:
279		RecordError(GL_INVALID_ENUM);
280		return false;
281	}
282
283	return true;
284}
285