7#include "ps2gl/lighting.h"
8#include "ps2gl/dlist.h"
9#include "ps2gl/glcontext.h"
10#include "ps2gl/material.h"
11#include "ps2gl/matrix.h"
19int CImmLight::NumLights[3] = { 0, 0, 0 };
21CImmLight::CImmLight(
CGLContext& context,
int lightNum)
22 :
CLight(context, lightNum)
23 , Ambient(0.0f, 0.0f, 0.0f, 1.0f)
24 , Diffuse(0.0f, 0.0f, 0.0f, 0.0f)
25 , Specular(0.0f, 0.0f, 0.0f, 0.0f)
26 , Position(0.0f, 0.0f, 1.0f, 0.0f)
27 , SpotDirection(0.0f, 0.0f, -1.0f, 0.0f)
38void CImmLight::CheckTypeChange(tLightType oldType)
40 if (oldType != Type && bIsEnabled) {
44 rm.NumLightsChanged(oldType, NumLights[oldType]);
45 rm.NumLightsChanged(Type, NumLights[Type]);
49void CImmLight::SetEnabled(
bool enabled)
51 if (bIsEnabled != enabled) {
58 GLContext.GetImmLighting().SpecularChanged();
61 rm.NumLightsChanged(Type, NumLights[Type]);
65void CImmLight::SetSpecular(cpu_vec_xyzw specular)
67 cpu_vec_4 zero(0, 0, 0, 0);
68 if ((specular == zero) ^ (Specular == zero)) {
70 GLContext.GetImmLighting().SpecularChanged();
74 TellRendererLightPropChanged();
77void CImmLight::SetPosition(cpu_vec_xyzw position)
81 Position = modelView.GetTop() * position;
82 TellRendererLightPropChanged();
84 tLightType oldType = Type;
85 Type = (SpotCutoff == 180.0f) ? kPoint : kSpot;
86 CheckTypeChange(oldType);
89void CImmLight::SetDirection(cpu_vec_xyzw direction)
93 Position = modelView.GetTop() * direction.normalize();
94 TellRendererLightPropChanged();
96 tLightType oldType = Type;
98 CheckTypeChange(oldType);
106 GLenum LightNum, Property;
110 CLightPropCmd(GLenum lightNum, GLenum prop, cpu_vec_xyzw value)
118 glLightfv(LightNum, Property,
reinterpret_cast<float*
>(&Value));
119 return CDListCmd::GetNextCmd(
this);
123void CDListLight::SetAmbient(cpu_vec_xyzw ambient)
125 GLContext.GetDListManager().GetOpenDList()
127 TellRendererLightPropChanged();
130void CDListLight::SetDiffuse(cpu_vec_xyzw diffuse)
132 GLContext.GetDListManager().GetOpenDList()
134 TellRendererLightPropChanged();
137void CDListLight::SetSpecular(cpu_vec_xyzw specular)
139 GLContext.GetDListManager().GetOpenDList()
140 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_SPECULAR, specular);
141 TellRendererLightPropChanged();
142 GLContext.SpecularEnabledChanged();
145void CDListLight::SetPosition(cpu_vec_xyzw position)
147 GLContext.GetDListManager().GetOpenDList()
148 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_POSITION, position);
149 TellRendererLightPropChanged();
152void CDListLight::SetDirection(cpu_vec_xyzw direction)
154 GLContext.GetDListManager().GetOpenDList()
155 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_POSITION, direction);
156 TellRendererLightPropChanged();
159void CDListLight::SetSpotDirection(cpu_vec_xyzw dir)
161 GLContext.GetDListManager().GetOpenDList()
162 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_SPOT_DIRECTION, dir);
163 TellRendererLightPropChanged();
166void CDListLight::SetSpotCutoff(
float cutoff)
168 GLContext.GetDListManager().GetOpenDList()
169 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_SPOT_CUTOFF, cpu_vec_xyzw(cutoff, 0, 0, 0));
170 TellRendererLightPropChanged();
173void CDListLight::SetSpotExponent(
float exp)
175 GLContext.GetDListManager().GetOpenDList()
176 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_AMBIENT, cpu_vec_xyzw(exp, 0, 0, 0));
177 TellRendererLightPropChanged();
180void CDListLight::SetConstantAtten(
float atten)
182 GLContext.GetDListManager().GetOpenDList()
183 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_CONSTANT_ATTENUATION, cpu_vec_xyzw(atten, 0, 0, 0));
184 TellRendererLightPropChanged();
187void CDListLight::SetLinearAtten(
float atten)
189 GLContext.GetDListManager().GetOpenDList()
190 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_LINEAR_ATTENUATION, cpu_vec_xyzw(atten, 0, 0, 0));
191 TellRendererLightPropChanged();
194void CDListLight::SetQuadAtten(
float atten)
196 GLContext.GetDListManager().GetOpenDList()
197 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_QUADRATIC_ATTENUATION, cpu_vec_xyzw(atten, 0, 0, 0));
198 TellRendererLightPropChanged();
201void CDListLight::SetEnabled(
bool yesNo)
203 GLContext.GetDListManager().GetOpenDList()
205 TellRendererLightsEnabledChanged();
212CImmLighting::CImmLighting(
CGLContext& context)
214 , CurrentColor(0.0f, 0.0f, 0.0f, 0.0f)
215 , GlobalAmbient(0.0f, 0.0f, 0.0f, 0.0f)
225 , NumLightsWithNonzeroSpecular(0)
237void CImmLighting::SpecularChanged()
240 cpu_vec_4 zero(0, 0, 0, 0);
241 for (
int i = 0; i < 8; i++)
242 if (Lights[i]->IsEnabled() && Lights[i]->GetSpecular() != zero)
245 NumLightsWithNonzeroSpecular = count;
246 if (NumLightsWithNonzeroSpecular == 0) {
247 GLContext.SpecularEnabledChanged();
248 GLContext.GetImmGeomManager().GetRendererManager().SpecularEnabledChanged(
false);
250 GLContext.GetMaterialManager().GetImmMaterial().LightsHaveSpecular();
253void CImmLighting::MaterialHasSpecular()
255 if (NumLightsWithNonzeroSpecular > 0) {
256 GLContext.SpecularEnabledChanged();
257 GLContext.GetImmGeomManager().GetRendererManager().SpecularEnabledChanged(
true);
261CDListLighting::CDListLighting(
CGLContext& context)
292 pGLContext->GetImmLighting().SetLightingEnabled(IsEnabled);
293 return CDListCmd::GetNextCmd(
this);
297void CDListLighting::SetLightingEnabled(
bool enabled)
300 GLContext.LightingEnabledChanged();
304 cpu_vec_xyzw Ambient;
313 pGLContext->GetImmLighting().SetGlobalAmbient(Ambient);
314 return CDListCmd::GetNextCmd(
this);
318void CDListLighting::SetGlobalAmbient(cpu_vec_xyzw newAmb)
321 TellRendererLightPropChanged();
328void setPosition(
CLight& light,
float x,
float y,
float z,
float w)
330 cpu_vec_xyzw pos(x, y, z, w);
332 light.SetDirection(pos);
334 light.SetPosition(pos);
337void glLightfv(GLenum lightNum,
339 const GLfloat* params)
341 GL_FUNC_DEBUG(
"%s\n", __FUNCTION__);
343 CLighting& lighting = pGLContext->GetLighting();
344 CLight& light = lighting.GetLight(0x7 & lightNum);
348 light.SetAmbient(cpu_vec_xyzw(params[0], params[1], params[2], params[3]));
351 light.SetDiffuse(cpu_vec_xyzw(params[0], params[1], params[2], params[3]));
354 light.SetSpecular(cpu_vec_xyzw(params[0], params[1], params[2], params[3]));
357 setPosition(light, params[0], params[1], params[2], params[3]);
359 case GL_SPOT_DIRECTION:
360 light.SetPosition(cpu_vec_xyzw(params[0], params[1], params[2], 0.0f).normalize());
362 case GL_SPOT_EXPONENT:
363 light.SetSpotExponent(*params);
366 light.SetSpotCutoff(*params);
368 case GL_CONSTANT_ATTENUATION:
369 light.SetConstantAtten(*params);
371 case GL_LINEAR_ATTENUATION:
372 light.SetLinearAtten(*params);
374 case GL_QUADRATIC_ATTENUATION:
375 light.SetQuadAtten(*params);
378 mError(
"Shouldn't get here.");
382void glLightf(GLenum lightNum, GLenum pname, GLfloat param)
384 GL_FUNC_DEBUG(
"%s\n", __FUNCTION__);
386 CLighting& lighting = pGLContext->GetLighting();
387 CLight& light = lighting.GetLight(0x7 & lightNum);
390 case GL_SPOT_EXPONENT:
391 light.SetSpotExponent(param);
394 light.SetSpotCutoff(param);
396 case GL_CONSTANT_ATTENUATION:
397 light.SetConstantAtten(param);
399 case GL_LINEAR_ATTENUATION:
400 light.SetLinearAtten(param);
402 case GL_QUADRATIC_ATTENUATION:
403 light.SetQuadAtten(param);
406 mError(
"Shouldn't get here.");
410void glLightModelfv(GLenum pname,
const GLfloat* params)
412 GL_FUNC_DEBUG(
"%s\n", __FUNCTION__);
415 case GL_LIGHT_MODEL_AMBIENT:
416 pGLContext->GetLighting().SetGlobalAmbient(cpu_vec_xyzw(params[0],
421 case GL_LIGHT_MODEL_COLOR_CONTROL:
422 if ((
int)*params == GL_SEPARATE_SPECULAR_COLOR) {
423 mNotImplemented(
"separate specular color computation");
426 case GL_LIGHT_MODEL_LOCAL_VIEWER:
427 if ((
int)*params != 0) {
428 mNotImplemented(
"local viewer");
431 case GL_LIGHT_MODEL_TWO_SIDE:
432 if ((
int)*params != 0) {
433 mNotImplemented(
"two-sided lighting");
437 mError(
"shouldn't get here");
441void glLightModeli(GLenum pname,
int param)
443 GL_FUNC_DEBUG(
"%s\n", __FUNCTION__);
448void glGetLightfv(GLenum light, GLenum pname,
float* params)
450 GL_FUNC_DEBUG(
"%s\n", __FUNCTION__);
455void glFogf(GLenum pname, GLfloat param)
457 GL_FUNC_DEBUG(
"%s\n", __FUNCTION__);
462void glFogfv(GLenum pname,
const GLfloat* params)
464 GL_FUNC_DEBUG(
"%s\n", __FUNCTION__);