Optimized light updating in CGLDevice

dev-time-step
Tomasz Kapuściński 2016-01-30 18:51:13 +01:00
parent f6db624d00
commit 12067c1b9f
2 changed files with 91 additions and 20 deletions

View File

@ -447,8 +447,7 @@ void CGLDevice::UpdateModelviewMatrix()
if (m_lighting) if (m_lighting)
{ {
for (int index = 0; index < static_cast<int>( m_lights.size() ); ++index) UpdateLightPositions();
UpdateLightPosition(index);
} }
} }
@ -501,9 +500,23 @@ void CGLDevice::UpdateLightPosition(int index)
assert(index < static_cast<int>( m_lights.size() )); assert(index < static_cast<int>( m_lights.size() ));
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glPushMatrix(); glPushMatrix();
if (m_lights[index].type == LIGHT_POINT)
{
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);
}
else
{
glLoadIdentity(); glLoadIdentity();
glScalef(1.0f, 1.0f, -1.0f); glScalef(1.0f, 1.0f, -1.0f);
Math::Matrix mat = m_viewMat; Math::Matrix mat = m_viewMat;
@ -514,24 +527,81 @@ void CGLDevice::UpdateLightPosition(int index)
if (m_lights[index].type == LIGHT_SPOT) 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 }; 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); glLightfv(GL_LIGHT0 + index, GL_SPOT_DIRECTION, direction);
} }
else if (m_lights[index].type == LIGHT_DIRECTIONAL)
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); glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
} }
else }
glPopMatrix();
}
void CGLDevice::UpdateLightPositions()
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
int lightCount = m_lights.size();
// update spotlights and directional lights
glLoadIdentity();
glScalef(1.0f, 1.0f, -1.0f);
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());
for (int index = 0; index < lightCount; index++)
{ {
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);
}
else 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 };
glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
}
}
// update point lights
glLoadIdentity(); glLoadIdentity();
glScalef(1.0f, 1.0f, -1.0f); glScalef(1.0f, 1.0f, -1.0f);
glMultMatrixf(m_viewMat.Array()); glMultMatrixf(m_viewMat.Array());
GLfloat position[4] = { m_lights[index].position.x, m_lights[index].position.y, m_lights[index].position.z, 1.0f }; for (int index = 0; index < lightCount; index++)
{
if (m_lights[index].type == LIGHT_POINT)
{
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); glLightfv(GL_LIGHT0 + index, GL_POSITION, position);
} }
}
glPopMatrix(); glPopMatrix();
} }
@ -1858,8 +1928,7 @@ void CGLDevice::SetRenderState(RenderState state, bool enabled)
if (enabled) if (enabled)
{ {
for (int index = 0; index < static_cast<int>( m_lights.size() ); ++index) UpdateLightPositions();
UpdateLightPosition(index);
} }
return; return;

View File

@ -195,6 +195,8 @@ private:
void UpdateModelviewMatrix(); void UpdateModelviewMatrix();
//! Updates position for given light based on transformation matrices //! Updates position for given light based on transformation matrices
void UpdateLightPosition(int index); void UpdateLightPosition(int index);
//! Updates position for all lights based on transformation matrices
void UpdateLightPositions();
//! Updates the texture params for given texture stage //! Updates the texture params for given texture stage
void UpdateTextureParams(int index); void UpdateTextureParams(int index);