Completely fixed light issues

Directional and spot lights are now set properly
dev-ui
Piotr Dziwinski 2012-09-27 23:18:12 +02:00
parent a394c9efec
commit 6d0ed0d26a
4 changed files with 52 additions and 32 deletions

View File

@ -186,6 +186,8 @@ CEngine::CEngine(CInstanceManager *iMan, CApplication *app)
m_updateGeometry = false;
m_interfaceMode = false;
m_mice[ENG_MOUSE_NORM] = EngineMouse( 0, 1, 32, ENG_RSTATE_TTEXTURE_WHITE, ENG_RSTATE_TTEXTURE_BLACK, Math::Point( 1.0f, 1.0f));
m_mice[ENG_MOUSE_WAIT] = EngineMouse( 2, 3, 33, ENG_RSTATE_TTEXTURE_WHITE, ENG_RSTATE_TTEXTURE_BLACK, Math::Point( 8.0f, 12.0f));
m_mice[ENG_MOUSE_HAND] = EngineMouse( 4, 5, 34, ENG_RSTATE_TTEXTURE_WHITE, ENG_RSTATE_TTEXTURE_BLACK, Math::Point( 7.0f, 2.0f));
@ -1852,7 +1854,8 @@ void CEngine::SetState(int state, const Color& color)
params.colorOperation = TEX_MIX_OPER_MODULATE;
params.colorArg1 = TEX_MIX_ARG_TEXTURE;
params.colorArg2 = TEX_MIX_ARG_FACTOR;
params.alphaOperation = TEX_MIX_OPER_DEFAULT; // TODO: replace with src color ?
params.alphaOperation = TEX_MIX_OPER_DEFAULT;
params.alphaOperation = TEX_MIX_OPER_REPLACE; // TODO: replace with src color ?
m_device->SetTextureEnabled(0, true);
m_device->SetTextureStageParams(0, params);
@ -1947,13 +1950,8 @@ void CEngine::SetState(int state, const Color& color)
m_device->SetBlendFunc(BLEND_SRC_ALPHA, BLEND_INV_SRC_ALPHA);
m_device->SetRenderState(RENDER_STATE_TEXTURING, true);
TextureStageParams params;
params.colorOperation = TEX_MIX_OPER_DEFAULT; // default modulate operation
params.alphaOperation = TEX_MIX_OPER_DEFAULT; // default modulate operation
m_device->SetTextureEnabled(0, true);
m_device->SetTextureStageParams(0, params);
m_device->SetTextureStageParams(0, TextureStageParams()); // default operation
}
else if (state & ENG_RSTATE_ALPHA) // image with alpha channel?
{
@ -2069,6 +2067,13 @@ void CEngine::SetState(int state, const Color& color)
m_device->SetGlobalAmbient(Color(1.0f, 1.0f, 1.0f, 1.0f));
else
m_device->SetGlobalAmbient(m_ambientColor[m_rankView]);
// In interface mode, disable lighting
if (m_interfaceMode)
{
m_device->SetRenderState(RENDER_STATE_LIGHTING, false);
}
}
void CEngine::SetMaterial(const Material& mat)
@ -2810,6 +2815,7 @@ void CEngine::Draw3DScene()
if (m_shadowVisible)
{
m_device->DebugHook();
m_lightMan->UpdateDeviceLights(ENG_OBJTYPE_TERRAIN);
// Draw the terrain
@ -3041,7 +3047,7 @@ void CEngine::Draw3DScene()
}
}
m_lightMan->UpdateDeviceLights(ENG_OBJTYPE_NULL);
m_lightMan->UpdateDeviceLights(ENG_OBJTYPE_TERRAIN);
if (m_waterMode) m_water->DrawSurf(); // draws water surface
@ -3064,11 +3070,20 @@ void CEngine::DrawInterface()
m_device->SetTransform(TRANSFORM_PROJECTION, m_matProjInterface);
m_device->SetTransform(TRANSFORM_WORLD, m_matWorldInterface);
// Force new state to disable lighting
m_interfaceMode = true;
m_lastState = -1;
SetState(Gfx::ENG_RSTATE_NORMAL);
// Draw the entire interface
Ui::CInterface* interface = static_cast<Ui::CInterface*>( m_iMan->SearchInstance(CLASS_INTERFACE) );
if (interface != nullptr)
interface->Draw();
m_interfaceMode = false;
m_lastState = -1;
SetState(Gfx::ENG_RSTATE_NORMAL);
m_particle->DrawParticle(SH_INTERFACE); // draws the particles of the interface
// 3D objects drawn in front of interface

View File

@ -1375,6 +1375,9 @@ protected:
std::string m_lastTexture[2];
//! Last material
Material m_lastMaterial;
//! True when drawing 2D UI
bool m_interfaceMode;
};

View File

@ -307,9 +307,8 @@ void CGLDevice::SetLight(int index, const Light &light)
if (light.type == LIGHT_SPOT)
{
// TODO: fix spotlight problems
//glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, light.spotAngle);
//glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, light.spotIntensity);
glLightf(GL_LIGHT0 + index, GL_SPOT_CUTOFF, light.spotAngle);
glLightf(GL_LIGHT0 + index, GL_SPOT_EXPONENT, light.spotIntensity);
}
else
{
@ -330,25 +329,33 @@ void CGLDevice::UpdateLightPosition(int index)
glLoadIdentity();
glScalef(1.0f, 1.0f, -1.0f);
glMultMatrixf(m_viewMat.Array());
Math::Matrix mat = m_viewMat;
mat.Set(1, 4, 0.0f);
mat.Set(2, 4, 0.0f);
mat.Set(3, 4, 0.0f);
glMultMatrixf(mat.Array());
if (m_lights[index].type == LIGHT_SPOT)
{
GLfloat direction[4] = { -m_lights[index].direction.x, -m_lights[index].direction.y, -m_lights[index].direction.z, 1.0f };
glLightfv(GL_LIGHT0 + index, GL_SPOT_DIRECTION, direction);
}
if (m_lights[index].type == LIGHT_DIRECTIONAL)
{
GLfloat position[4] = { m_lights[index].direction.x, m_lights[index].direction.y, m_lights[index].direction.z, 0.0f };
GLfloat position[4] = { -m_lights[index].direction.x, -m_lights[index].direction.y, -m_lights[index].direction.z, 0.0f };
glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
}
else
{
glLoadIdentity();
glScalef(1.0f, 1.0f, -1.0f);
glMultMatrixf(m_viewMat.Array());
GLfloat position[4] = { m_lights[index].position.x, m_lights[index].position.y, m_lights[index].position.z, 1.0f };
glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
}
if (m_lights[index].type == LIGHT_SPOT)
{
GLfloat direction[4] = { m_lights[index].direction.x, m_lights[index].direction.y, m_lights[index].direction.z, 0.0f };
glLightfv(GL_LIGHT0 + index, GL_SPOT_DIRECTION, direction);
}
glPopMatrix();
}
@ -367,7 +374,10 @@ void CGLDevice::SetLightEnabled(int index, bool enabled)
m_lightsEnabled[index] = enabled;
glEnable(GL_LIGHT0 + index);
if (enabled)
glEnable(GL_LIGHT0 + index);
else
glDisable(GL_LIGHT0 + index);
}
bool CGLDevice::GetLightEnabled(int index)

View File

@ -2244,14 +2244,12 @@ bool CObject::CreateInsect(Math::Vector pos, float angle, ObjectType type)
bool CObject::CreateShadowLight(float height, Gfx::Color color)
{
Gfx::Light light;
Math::Vector pos;
if ( !m_engine->GetLightMode() ) return true;
pos = GetPosition(0);
Math::Vector pos = GetPosition(0);
m_shadowHeight = height;
Gfx::Light light;
light.type = Gfx::LIGHT_SPOT;
light.diffuse.r = color.r;
light.diffuse.g = color.g;
@ -2262,12 +2260,10 @@ bool CObject::CreateShadowLight(float height, Gfx::Color color)
light.direction.x = 0.0f;
light.direction.y = -1.0f; // against the bottom
light.direction.z = 0.0f;
//TODO Is this value correct
light.spotIntensity = 128;
light.attenuation0 = 1.0f;
light.attenuation1 = 0.0f;
light.attenuation2 = 0.0f;
//TODO Is this value correct
light.spotAngle = 90;
m_shadowLight = m_lightMan->CreateLight();
@ -2292,13 +2288,11 @@ int CObject::GetShadowLight()
bool CObject::CreateEffectLight(float height, Gfx::Color color)
{
Gfx::Light light;
if ( !m_engine->GetLightMode() ) return true;
m_effectHeight = height;
memset( &light, 0, sizeof(light) );
Gfx::Light light;
light.type = Gfx::LIGHT_SPOT;
light.diffuse.r = color.r;
light.diffuse.g = color.g;
@ -2309,12 +2303,10 @@ bool CObject::CreateEffectLight(float height, Gfx::Color color)
light.direction.x = 0.0f;
light.direction.y = -1.0f; // against the bottom
light.direction.z = 0.0f;
//TODO Is this value correct
light.spotIntensity = 1.0f;
light.spotIntensity = 0.0f;
light.attenuation0 = 1.0f;
light.attenuation1 = 0.0f;
light.attenuation2 = 0.0f;
//TODO Is this value correct
light.spotAngle = 90;
m_effectLight = m_lightMan->CreateLight();