From 6d0ed0d26aebead7c79e4fb97c6962e06e7dcb41 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Thu, 27 Sep 2012 23:18:12 +0200 Subject: Completely fixed light issues Directional and spot lights are now set properly --- src/graphics/engine/engine.cpp | 31 +++++++++++++++++++++++-------- src/graphics/engine/engine.h | 3 +++ src/graphics/opengl/gldevice.cpp | 34 ++++++++++++++++++++++------------ 3 files changed, 48 insertions(+), 20 deletions(-) (limited to 'src/graphics') diff --git a/src/graphics/engine/engine.cpp b/src/graphics/engine/engine.cpp index 80fbebc..d470b06 100644 --- a/src/graphics/engine/engine.cpp +++ b/src/graphics/engine/engine.cpp @@ -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( 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 diff --git a/src/graphics/engine/engine.h b/src/graphics/engine/engine.h index c35b7a5..e188dea 100644 --- a/src/graphics/engine/engine.h +++ b/src/graphics/engine/engine.h @@ -1375,6 +1375,9 @@ protected: std::string m_lastTexture[2]; //! Last material Material m_lastMaterial; + + //! True when drawing 2D UI + bool m_interfaceMode; }; diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index 29a0104..bffbc3a 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -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) -- cgit v1.2.3-1-g7c22