From 9b04264845a5298c1876a6467d7a9a626e93d8eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20=E2=80=98Touky=E2=80=99=20Huet?= Date: Fri, 4 Oct 2013 14:57:34 +0000 Subject: [PATCH] MViewer : Added Stupid light blending --- test/data/mesh-buffer.txt | 3 +- test/meshviewer.cpp | 63 ++++++++++++++++++++++++++++++++++++--- test/scenesetup.h | 1 - 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/test/data/mesh-buffer.txt b/test/data/mesh-buffer.txt index 0fcf6117..a31374a9 100644 --- a/test/data/mesh-buffer.txt +++ b/test/data/mesh-buffer.txt @@ -1,7 +1,8 @@ -//addlight 0.0 position (0.6 0.5 1) color #ddf +addlight 0.0 position (1 0 1) color #0ff addlight 0.0 position (-0.2 -1 -0.5) color (.0 .2 .5 1) custom setmesh "sc#fff ab 2 ty 1.5 ab 2" + //splt 0 //test //[sc#f8f ab 1 splt 4 twy 90] diff --git a/test/meshviewer.cpp b/test/meshviewer.cpp index bda875f4..6707764e 100644 --- a/test/meshviewer.cpp +++ b/test/meshviewer.cpp @@ -119,6 +119,18 @@ enum MessageType MSG_MAX }; +struct LightData +{ + LightData(vec3 pos, vec4 col) + { + m_pos = pos; + m_col = col; + } + + vec3 m_pos; + vec4 m_col; +}; + class MeshViewer : public WorldEntity { public: @@ -275,6 +287,12 @@ public: // " custom setmesh \"sc#fff ab 1\"" m_ssetup->Startup(); #endif //NO_SC_SETUP + for (int i = 0; i < m_ssetup->m_lights.Count(); ++i) + { + m_light_datas << LightData(m_ssetup->m_lights[i]->GetPosition().xyz, m_ssetup->m_lights[i]->GetColor()); + m_ssetup->m_lights[i]->SetPosition(vec4(vec3::zero, m_ssetup->m_lights[i]->GetPosition().w)); + m_ssetup->m_lights[i]->SetColor(vec4::zero); + } } virtual void TickGame(float seconds) @@ -308,11 +326,18 @@ public: m_mesh_id1 = damp(m_mesh_id1, (float)m_mesh_id, .2f, seconds); #if ALL_FEATURES - //Update light position + + //Update light position & damping for (int i = 0; i < m_ssetup->m_lights.Count(); ++i) { - vec4 v = m_ssetup->m_lights[i]->GetPosition(); - m_ssetup->m_lights[i]->SetPosition(vec4((m_mat * inverse(m_mat_prev) * vec4(v.xyz, 1.f)).xyz, v.w)); + vec3 pos = (m_mat * inverse(m_mat_prev) * vec4(m_ssetup->m_lights[i]->GetPosition().xyz, 1.f)).xyz; + vec3 tgt = (m_mat * vec4(m_light_datas[i].m_pos, 1.f)).xyz; + + vec3 new_pos = damp(pos, tgt, .3f, seconds); + vec4 new_col = damp(m_ssetup->m_lights[i]->GetColor(), m_light_datas[i].m_col, .3f, seconds); + + m_ssetup->m_lights[i]->SetPosition(vec4(new_pos, m_ssetup->m_lights[i]->GetPosition().w)); + m_ssetup->m_lights[i]->SetColor(new_col); } //Camera update @@ -481,11 +506,39 @@ public: SceneSetup* new_ssetup = new SceneSetup(); if (new_ssetup->Compile(mesh.C()) && new_ssetup->GetLightNb()) { + //Store current light datas, in World + Array light_datas; + for (int i = 0; i < m_ssetup->m_lights.Count(); ++i) + light_datas << LightData(m_ssetup->m_lights[i]->GetPosition().xyz, m_ssetup->m_lights[i]->GetColor()); + if (m_ssetup) delete(m_ssetup); m_ssetup = new_ssetup; m_ssetup->Startup(); - m_mat_prev = mat4(quat::fromeuler_xyz(vec3::zero)); + + //Restore all light datas so blend can occur + mat4 light_mat = m_mat * inverse(mat4(quat::fromeuler_xyz(vec3::zero))); + for (int i = 0; i < m_ssetup->m_lights.Count(); ++i) + { + //Store local dst in current m_ld + LightData tmp = LightData(m_ssetup->m_lights[i]->GetPosition().xyz, m_ssetup->m_lights[i]->GetColor()); + if (i < m_light_datas.Count()) + m_light_datas[i] = tmp; + else + m_light_datas << tmp; + + vec3 loc = vec3::zero; + vec4 col = vec4::zero; + if (i < light_datas.Count()) + { + loc = light_datas[i].m_pos; + col = light_datas[i].m_col; + } + + //Restore old light datas in new lights + m_ssetup->m_lights[i]->SetPosition(vec4(loc, m_ssetup->m_lights[i]->GetPosition().w)); + m_ssetup->m_lights[i]->SetColor(col); + } } else { @@ -494,6 +547,7 @@ public: } } } + //Check the custom cmd even if we don't have new messages. for (int i = 0; m_ssetup && i < m_ssetup->m_custom_cmd.Count(); ++i) { @@ -643,6 +697,7 @@ public: private: SceneSetup* m_ssetup; + Array m_light_datas; Controller* m_controller; short m_input_usage; mat4 m_mat; diff --git a/test/scenesetup.h b/test/scenesetup.h index 7d4ccbf5..e5e6a0e1 100644 --- a/test/scenesetup.h +++ b/test/scenesetup.h @@ -41,7 +41,6 @@ public: //-- int GetLightNb() { return m_lights.Count(); } -//private: Array m_lights; Array m_custom_cmd; };