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)
38 Diffuse = cpu_vec_xyzw(1.0f, 1.0f, 1.0f, 1.0f);
39 Specular = cpu_vec_xyzw(1.0f, 1.0f, 1.0f, 1.0f);
43void CImmLight::CheckTypeChange(tLightType oldType)
45 if (oldType != Type && bIsEnabled) {
49 rm.NumLightsChanged(oldType, NumLights[oldType]);
50 rm.NumLightsChanged(Type, NumLights[Type]);
54void CImmLight::SetEnabled(
bool enabled)
56 if (bIsEnabled != enabled) {
63 GLContext.GetImmLighting().SpecularChanged();
66 rm.NumLightsChanged(Type, NumLights[Type]);
70void CImmLight::SetSpecular(cpu_vec_xyzw specular)
72 cpu_vec_4 zero(0, 0, 0, 0);
73 if ((specular == zero) ^ (Specular == zero)) {
75 GLContext.GetImmLighting().SpecularChanged();
79 TellRendererLightPropChanged();
82void CImmLight::SetPosition(cpu_vec_xyzw position)
86 Position = modelView.GetTop() * position;
87 TellRendererLightPropChanged();
89 tLightType oldType = Type;
90 Type = (SpotCutoff == 180.0f) ? kPoint : kSpot;
91 CheckTypeChange(oldType);
94void CImmLight::SetDirection(cpu_vec_xyzw direction)
98 Position = modelView.GetTop() * direction.normalize();
99 TellRendererLightPropChanged();
101 tLightType oldType = Type;
103 CheckTypeChange(oldType);
111 GLenum LightNum, Property;
115 CLightPropCmd(GLenum lightNum, GLenum prop, cpu_vec_xyzw value)
123 glLightfv(LightNum, Property,
reinterpret_cast<float*
>(&Value));
124 return CDListCmd::GetNextCmd(
this);
128void CDListLight::SetAmbient(cpu_vec_xyzw ambient)
130 GLContext.GetDListManager().GetOpenDList()
132 TellRendererLightPropChanged();
135void CDListLight::SetDiffuse(cpu_vec_xyzw diffuse)
137 GLContext.GetDListManager().GetOpenDList()
139 TellRendererLightPropChanged();
142void CDListLight::SetSpecular(cpu_vec_xyzw specular)
144 GLContext.GetDListManager().GetOpenDList()
145 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_SPECULAR, specular);
146 TellRendererLightPropChanged();
147 GLContext.SpecularEnabledChanged();
150void CDListLight::SetPosition(cpu_vec_xyzw position)
152 GLContext.GetDListManager().GetOpenDList()
153 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_POSITION, position);
154 TellRendererLightPropChanged();
157void CDListLight::SetDirection(cpu_vec_xyzw direction)
159 GLContext.GetDListManager().GetOpenDList()
160 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_POSITION, direction);
161 TellRendererLightPropChanged();
164void CDListLight::SetSpotDirection(cpu_vec_xyzw dir)
166 GLContext.GetDListManager().GetOpenDList()
167 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_SPOT_DIRECTION, dir);
168 TellRendererLightPropChanged();
171void CDListLight::SetSpotCutoff(
float cutoff)
173 GLContext.GetDListManager().GetOpenDList()
174 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_SPOT_CUTOFF, cpu_vec_xyzw(cutoff, 0, 0, 0));
175 TellRendererLightPropChanged();
178void CDListLight::SetSpotExponent(
float exp)
180 GLContext.GetDListManager().GetOpenDList()
181 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_AMBIENT, cpu_vec_xyzw(exp, 0, 0, 0));
182 TellRendererLightPropChanged();
185void CDListLight::SetConstantAtten(
float atten)
187 GLContext.GetDListManager().GetOpenDList()
188 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_CONSTANT_ATTENUATION, cpu_vec_xyzw(atten, 0, 0, 0));
189 TellRendererLightPropChanged();
192void CDListLight::SetLinearAtten(
float atten)
194 GLContext.GetDListManager().GetOpenDList()
195 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_LINEAR_ATTENUATION, cpu_vec_xyzw(atten, 0, 0, 0));
196 TellRendererLightPropChanged();
199void CDListLight::SetQuadAtten(
float atten)
201 GLContext.GetDListManager().GetOpenDList()
202 +=
CLightPropCmd(GL_LIGHT0 | LightNum, GL_QUADRATIC_ATTENUATION, cpu_vec_xyzw(atten, 0, 0, 0));
203 TellRendererLightPropChanged();
206void CDListLight::SetEnabled(
bool yesNo)
208 GLContext.GetDListManager().GetOpenDList()
210 TellRendererLightsEnabledChanged();
217CImmLighting::CImmLighting(
CGLContext& context)
219 , CurrentColor(0.0f, 0.0f, 0.0f, 0.0f)
220 , GlobalAmbient(0.0f, 0.0f, 0.0f, 0.0f)
230 , NumLightsWithNonzeroSpecular(0)
242void CImmLighting::SpecularChanged()
245 cpu_vec_4 zero(0, 0, 0, 0);
246 for (
int i = 0; i < 8; i++)
247 if (Lights[i]->IsEnabled() && Lights[i]->GetSpecular() != zero)
250 NumLightsWithNonzeroSpecular = count;
251 if (NumLightsWithNonzeroSpecular == 0) {
252 GLContext.SpecularEnabledChanged();
253 GLContext.GetImmGeomManager().GetRendererManager().SpecularEnabledChanged(
false);
255 GLContext.GetMaterialManager().GetImmMaterial().LightsHaveSpecular();
258void CImmLighting::MaterialHasSpecular()
260 if (NumLightsWithNonzeroSpecular > 0) {
261 GLContext.SpecularEnabledChanged();
262 GLContext.GetImmGeomManager().GetRendererManager().SpecularEnabledChanged(
true);
266CDListLighting::CDListLighting(
CGLContext& context)
297 pGLContext->GetImmLighting().SetLightingEnabled(IsEnabled);
298 return CDListCmd::GetNextCmd(
this);
302void CDListLighting::SetLightingEnabled(
bool enabled)
305 GLContext.LightingEnabledChanged();
309 cpu_vec_xyzw Ambient;
318 pGLContext->GetImmLighting().SetGlobalAmbient(Ambient);
319 return CDListCmd::GetNextCmd(
this);
323void CDListLighting::SetGlobalAmbient(cpu_vec_xyzw newAmb)
326 TellRendererLightPropChanged();
333void setPosition(
CLight& light,
float x,
float y,
float z,
float w)
335 cpu_vec_xyzw pos(x, y, z, w);
337 light.SetDirection(pos);
339 light.SetPosition(pos);
342void glLightfv(GLenum lightNum,
344 const GLfloat* params)
346 GL_FUNC_DEBUG(
"%s\n", __FUNCTION__);
348 CLighting& lighting = pGLContext->GetLighting();
349 CLight& light = lighting.GetLight(0x7 & lightNum);
353 light.SetAmbient(cpu_vec_xyzw(params[0], params[1], params[2], params[3]));
356 light.SetDiffuse(cpu_vec_xyzw(params[0], params[1], params[2], params[3]));
359 light.SetSpecular(cpu_vec_xyzw(params[0], params[1], params[2], params[3]));
362 setPosition(light, params[0], params[1], params[2], params[3]);
364 case GL_SPOT_DIRECTION:
365 light.SetPosition(cpu_vec_xyzw(params[0], params[1], params[2], 0.0f).normalize());
367 case GL_SPOT_EXPONENT:
368 light.SetSpotExponent(*params);
371 light.SetSpotCutoff(*params);
373 case GL_CONSTANT_ATTENUATION:
374 light.SetConstantAtten(*params);
376 case GL_LINEAR_ATTENUATION:
377 light.SetLinearAtten(*params);
379 case GL_QUADRATIC_ATTENUATION:
380 light.SetQuadAtten(*params);
383 mError(
"Shouldn't get here.");
387void glLightf(GLenum lightNum, GLenum pname, GLfloat param)
389 GL_FUNC_DEBUG(
"%s\n", __FUNCTION__);
391 CLighting& lighting = pGLContext->GetLighting();
392 CLight& light = lighting.GetLight(0x7 & lightNum);
395 case GL_SPOT_EXPONENT:
396 light.SetSpotExponent(param);
399 light.SetSpotCutoff(param);
401 case GL_CONSTANT_ATTENUATION:
402 light.SetConstantAtten(param);
404 case GL_LINEAR_ATTENUATION:
405 light.SetLinearAtten(param);
407 case GL_QUADRATIC_ATTENUATION:
408 light.SetQuadAtten(param);
411 mError(
"Shouldn't get here.");
415void glLightModelfv(GLenum pname,
const GLfloat* params)
417 GL_FUNC_DEBUG(
"%s\n", __FUNCTION__);
420 case GL_LIGHT_MODEL_AMBIENT:
421 pGLContext->GetLighting().SetGlobalAmbient(cpu_vec_xyzw(params[0],
426 case GL_LIGHT_MODEL_COLOR_CONTROL:
427 if ((
int)*params == GL_SEPARATE_SPECULAR_COLOR) {
428 mNotImplemented(
"separate specular color computation");
431 case GL_LIGHT_MODEL_LOCAL_VIEWER:
432 if ((
int)*params != 0) {
433 mNotImplemented(
"local viewer");
436 case GL_LIGHT_MODEL_TWO_SIDE:
437 if ((
int)*params != 0) {
438 mNotImplemented(
"two-sided lighting");
442 mError(
"shouldn't get here");
446void glLightModeli(GLenum pname,
int param)
448 GL_FUNC_DEBUG(
"%s\n", __FUNCTION__);
453void glGetLightfv(GLenum light, GLenum pname,
float* params)
455 GL_FUNC_DEBUG(
"%s\n", __FUNCTION__);
460void glFogi(GLenum pname, GLfloat param)
462 GL_FUNC_DEBUG(
"%s\n", __FUNCTION__);
467void glFogf(GLenum pname, GLfloat param)
469 GL_FUNC_DEBUG(
"%s\n", __FUNCTION__);
474void glFogfv(GLenum pname,
const GLfloat* params)
476 GL_FUNC_DEBUG(
"%s\n", __FUNCTION__);
481void glFogiv(GLenum pname,
const GLint* params)
483 GL_FUNC_DEBUG(
"%s\n", __FUNCTION__);