[colobot] 245/377: Optimized light updating in CGLDevice

Didier Raboud odyx at moszumanska.debian.org
Wed Mar 30 13:34:22 UTC 2016


This is an automated email from the git hooks/post-receive script.

odyx pushed a commit to branch debian/master
in repository colobot.

commit 12067c1b9f295e4f67c67c5dc1bb3bf24854eba8
Author: Tomasz Kapuściński <tomaszkax86 at gmail.com>
Date:   Sat Jan 30 18:51:13 2016 +0100

    Optimized light updating in CGLDevice
---
 src/graphics/opengl/gldevice.cpp | 105 ++++++++++++++++++++++++++++++++-------
 src/graphics/opengl/gldevice.h   |   2 +
 2 files changed, 89 insertions(+), 18 deletions(-)

diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp
index 5bef6f4..cf0e71e 100644
--- a/src/graphics/opengl/gldevice.cpp
+++ b/src/graphics/opengl/gldevice.cpp
@@ -447,8 +447,7 @@ void CGLDevice::UpdateModelviewMatrix()
 
     if (m_lighting)
     {
-        for (int index = 0; index < static_cast<int>( m_lights.size() ); ++index)
-            UpdateLightPosition(index);
+        UpdateLightPositions();
     }
 }
 
@@ -501,9 +500,62 @@ void CGLDevice::UpdateLightPosition(int index)
     assert(index < static_cast<int>( m_lights.size() ));
 
     glMatrixMode(GL_MODELVIEW);
+    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();
+        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());
+
+        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);
+        }
+    }
 
+    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;
@@ -512,25 +564,43 @@ void CGLDevice::UpdateLightPosition(int index)
     mat.Set(3, 4, 0.0f);
     glMultMatrixf(mat.Array());
 
-    if (m_lights[index].type == LIGHT_SPOT)
+    for (int index = 0; index < lightCount; index++)
     {
-        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_SPOT)
+        {
+            GLfloat direction[4] = { -m_lights[index].direction.x,
+                -m_lights[index].direction.y,
+                -m_lights[index].direction.z,
+                1.0f };
 
-    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);
+            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);
+        }
     }
-    else
+
+    // update point lights
+    glLoadIdentity();
+    glScalef(1.0f, 1.0f, -1.0f);
+    glMultMatrixf(m_viewMat.Array());
+
+    for (int index = 0; index < lightCount; index++)
     {
-        glLoadIdentity();
-        glScalef(1.0f, 1.0f, -1.0f);
-        glMultMatrixf(m_viewMat.Array());
+        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 };
 
-        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();
@@ -1858,8 +1928,7 @@ void CGLDevice::SetRenderState(RenderState state, bool enabled)
 
         if (enabled)
         {
-            for (int index = 0; index < static_cast<int>( m_lights.size() ); ++index)
-                UpdateLightPosition(index);
+            UpdateLightPositions();
         }
 
         return;
diff --git a/src/graphics/opengl/gldevice.h b/src/graphics/opengl/gldevice.h
index 696e6d6..fe0635c 100644
--- a/src/graphics/opengl/gldevice.h
+++ b/src/graphics/opengl/gldevice.h
@@ -195,6 +195,8 @@ private:
     void UpdateModelviewMatrix();
     //! Updates position for given light based on transformation matrices
     void UpdateLightPosition(int index);
+    //!  Updates position for all lights based on transformation matrices
+    void UpdateLightPositions();
     //! Updates the texture params for given texture stage
     void UpdateTextureParams(int index);
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/colobot.git



More information about the Pkg-games-commits mailing list