From fc2d4c5028079899a5c7d58eccaa67069dd1c91b Mon Sep 17 00:00:00 2001 From: Andreas Ortmann Date: Mon, 3 Oct 2016 10:47:02 +0200 Subject: [PATCH] explosions make light now. --- data/shader/gameobjects.frag | 30 ++++++++++++- data/shader/particle.frag | 1 - data/shader/particle.vert | 2 - game/renderer_polygon_3d/particle_batch.cpp | 9 +++- game/renderer_polygon_3d/particle_batch.hpp | 8 +++- .../renderer_polygon_3d.cpp | 42 ++++++++++++++++--- .../renderer_polygon_3d.hpp | 2 +- 7 files changed, 81 insertions(+), 13 deletions(-) diff --git a/data/shader/gameobjects.frag b/data/shader/gameobjects.frag index afc73bf..5b404a3 100644 --- a/data/shader/gameobjects.frag +++ b/data/shader/gameobjects.frag @@ -10,6 +10,10 @@ uniform vec3 materialColor; uniform int materialKind; uniform int materialSeed; +uniform int explLightsNum; +uniform vec3 explLightsPos[10]; +uniform float explLightsIntensities[10]; + void main() { vec3 Eye = normalize(-vertex); @@ -27,7 +31,31 @@ void main() vec3 color = materialColor; color = max(vec3(0.0), min(vec3(1.0), color)); - vec3 IDiffuse = vec3(color) * lightColor * max(dot(normal, lightDirection), 0.0); + //vec3 light = lightColor * max(dot(normal, lightDirection), 0.0); + vec3 light = vec3(0.0); + + int i; + for (i=0; i= 5.0*m_halfAge; + return m_age >= m_maxNumHalfAges*m_halfAge; } } diff --git a/game/renderer_polygon_3d/particle_batch.hpp b/game/renderer_polygon_3d/particle_batch.hpp index 5b7bc69..648e0f7 100644 --- a/game/renderer_polygon_3d/particle_batch.hpp +++ b/game/renderer_polygon_3d/particle_batch.hpp @@ -36,10 +36,15 @@ namespace endofthejedi { void render(Shader *shader); void tick(float dt); + float ageNormalized() const { return m_age / (m_maxNumHalfAges*m_halfAge); } + float age() const { return m_age; } bool done() const; size_t id() const { return m_id; } + + const glm::vec3 &explosionCenter() const { return m_center; } + private: size_t dataSizeForIndex(size_t index); void *dataSourceForIndex(size_t index); @@ -51,8 +56,9 @@ namespace endofthejedi { // uniforms for the shader size_t m_numParticles; float m_particleRadius; - const float m_halfAge; float m_age; + const float m_halfAge; + const float m_maxNumHalfAges; float m_maxVelocity; glm::vec3 m_center; diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.cpp b/game/renderer_polygon_3d/renderer_polygon_3d.cpp index fc56aa9..0ea580e 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.cpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.cpp @@ -29,7 +29,7 @@ namespace endofthejedi { //addModel("../data/mesh/small_atomic_bomb.stl", &m_missileModel); addModel("../data/mesh/rocket.stl", &m_missileModel); - addModel("../data/mesh/planet_12.stl", &m_planetModel); + addModel("../data/mesh/planet_128.stl", &m_planetModel); addModel("../data/mesh/ship_ufo.stl", &m_shipModel); } @@ -60,7 +60,7 @@ namespace endofthejedi { // TODO: add ONE sun planet // TODO: add lights for explosions - configureLightningInShader(); + configureLightningInShader(&m_shader_game_objects); //std::cout<<"setting aspect ratio: " << m_aspectRatio << std::endl; glUniform1f(m_shader_game_objects.location("aspectRatio"), m_aspectRatio); @@ -452,7 +452,7 @@ namespace endofthejedi { glPolygonMode(GL_FRONT, GL_FILL); } - void RendererPolygon3d::configureLightningInShader() + void RendererPolygon3d::configureLightningInShader(Shader *shader) const { // TODO: add a few small lights for explosions so they lit the // surroundsings @@ -469,8 +469,40 @@ namespace endofthejedi { } } - glUniform3f(m_shader_game_objects.location("lightPosition"), p.x, p.y, p.z); - glUniform3f(m_shader_game_objects.location("lightColor"), c.x, c.y, c.z); + glUniform3f(shader->location("lightPosition"), p.x, p.y, p.z); + glUniform3f(shader->location("lightColor"), c.x, c.y, c.z); + + std::vector positions; + std::vector intensities; + + size_t numExplLights = 0; + + for (ParticleBatch *batch : m_particles) { + float age = batch->ageNormalized(); + // TODO: use function with a peak for this: + // + // /\__ + // _/ \--____ + + float intensity = 1.0-age; + glm::vec3 p = batch->explosionCenter(); + + intensities.push_back(intensity); + positions.push_back(p); + + numExplLights++; + + if (numExplLights == 10) { + break; + } + } + + glUniform1i( shader->location("explLightsNum"), numExplLights); + + if (numExplLights != 0) { + glUniform3fv(shader->location("explLightsPos"), numExplLights, glm::value_ptr(positions[0])); + glUniform1fv(shader->location("explLightsIntensities"), numExplLights, intensities.data()); + } } void RendererPolygon3d::setWindowSize(int px, int py) diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.hpp b/game/renderer_polygon_3d/renderer_polygon_3d.hpp index 5f0f1b6..bfd3d1b 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.hpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.hpp @@ -59,7 +59,7 @@ namespace endofthejedi { void renderTraces(); - void configureLightningInShader(); + void configureLightningInShader(Shader *shader) const; private: // all models are also here (for easy reloading etc.)