From 70b0fee526d879b4ce345f7fd289cca3718863a9 Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Wed, 5 Oct 2016 23:26:55 +0200 Subject: [PATCH] post processing light --- data/shader/multitexture.frag | 20 +++ data/shader/multitexture.vert | 13 ++ data/shader/particle.frag | 4 +- data/shader/postprocess.frag | 7 +- game/game_window.hpp | 4 + .../renderer_polygon_3d.cpp | 155 +++++++++++++----- .../renderer_polygon_3d.hpp | 24 +-- 7 files changed, 169 insertions(+), 58 deletions(-) create mode 100644 data/shader/multitexture.frag create mode 100644 data/shader/multitexture.vert diff --git a/data/shader/multitexture.frag b/data/shader/multitexture.frag new file mode 100644 index 0000000..dc26702 --- /dev/null +++ b/data/shader/multitexture.frag @@ -0,0 +1,20 @@ +#version 120 + +uniform sampler2D tex0; +uniform sampler2D tex1; + +varying vec3 vertex; +varying vec2 uv; + +void main() +{ + vec3 color = texture2D(tex0, uv).rgb; + vec3 current = texture2D(tex1, uv).rgb; + float a = length(color) - length(current); + if (a > 0.3) { + color = mix(vec3(a/3.0), current, color); + } else { + color = current; + } + gl_FragColor = vec4(texture2D(tex0, uv).xyz + texture2D(tex1, uv).xyz, 1.0); +} diff --git a/data/shader/multitexture.vert b/data/shader/multitexture.vert new file mode 100644 index 0000000..d0df7b5 --- /dev/null +++ b/data/shader/multitexture.vert @@ -0,0 +1,13 @@ +#version 120 + +uniform vec2 uvScale; + +varying vec3 vertex; +varying vec2 uv; + +void main() +{ + gl_Position = gl_Vertex; + vertex = gl_Position.xyz; + uv = uvScale * (0.5 + vertex.xy / 2.0); +} diff --git a/data/shader/particle.frag b/data/shader/particle.frag index 08c24de..f639316 100644 --- a/data/shader/particle.frag +++ b/data/shader/particle.frag @@ -42,8 +42,8 @@ void main() //v /= 1.0 + max(0.0, min(35.0*(decay-0.2), 1.0)) + 10.0 * explCenterDist; v /= 1.0 + 10.0 * explCenterDist; - gl_FragColor = vec4(hsv2rgb(vec3(h, s, v)), 1.0); + gl_FragColor = vec4(hsv2rgb(vec3(h, s, v)), v*0.2); //gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0); //gl_FragColor.rgb = vec3(0.5+0.5*(a/pi)); -} \ No newline at end of file +} diff --git a/data/shader/postprocess.frag b/data/shader/postprocess.frag index ac4c7f2..488ecc2 100644 --- a/data/shader/postprocess.frag +++ b/data/shader/postprocess.frag @@ -1,7 +1,6 @@ #version 120 -uniform sampler2D tex0; -uniform sampler2D tex1; +uniform sampler2D tex; varying vec3 vertex; varying vec2 uv; @@ -18,9 +17,9 @@ void main() vec2 texCoord = uv.xy - float(int(gaussRadius/2)) * scale; vec3 color = vec3(0.0, 0.0, 0.0); for (int i=0; iposition, 0.0f)); v = glm::length(v) * planetNormal; @@ -383,7 +430,7 @@ namespace endofthejedi { if (!gotIt) { addExplosionEffect( expl->id, expl->position, - expl->missileVelocity, + glm::vec3(expl->missileVelocity,1.0), (expl->hit == game::Hit::Planet), 1000, 1.0); } @@ -401,7 +448,7 @@ namespace endofthejedi { addExplosionEffect( expl->id, expl->position, - expl->missileVelocity, + glm::vec3(expl->missileVelocity,1.0), (expl->hit == game::Hit::Planet), 1000, 1.0); } @@ -449,31 +496,59 @@ namespace endofthejedi { for (ParticleBatch *batch : rm) { m_particles.remove(batch); - delete(batch); + delete (batch); } } - void RendererPolygon3d::renderPlanets() - { +void RendererPolygon3d::renderPlanets() { m_planetModel->bind(); // TODO: put material into attributes and render witd glDrawInstanced // too (same for missiles) for (const game::Planet *planet : m_state->planets) { glm::mat4 model = computeModelMatrix(planet); - glUniformMatrix4fv(m_shader_game_objects.location("model"), 1, GL_FALSE, glm::value_ptr(model)); + glUniformMatrix4fv(m_shader_game_objects.location("model"), 1, GL_FALSE, + glm::value_ptr(model)); glm::vec3 c = planet->getColor(); - glUniform3f(m_shader_game_objects.location("materialColor"), c.x, c.y, c.z); - glUniform1i(m_shader_game_objects.location("materialSeed"), planet->seed); - glUniform1i(m_shader_game_objects.location("materialKind"), (int) planet->material); + glUniform3f(m_shader_game_objects.location("materialColor"), c.x, c.y, + c.z); + glUniform1i(m_shader_game_objects.location("materialSeed"), + planet->seed); + glUniform1i(m_shader_game_objects.location("materialKind"), + (int)planet->material); m_planetModel->render(); } } - void RendererPolygon3d::renderMissiles() - { +void RendererPolygon3d::renderSun() { + m_planetModel->bind(); + + // TODO: put material into attributes and render witd glDrawInstanced + // too (same for missiles) + for (const game::Planet *planet : m_state->planets) { + + if (planet->material != game::Planet::Material::Sun) + continue; + + glm::mat4 model = computeModelMatrix(planet); + glUniformMatrix4fv(m_shader_game_objects.location("model"), 1, GL_FALSE, + glm::value_ptr(model)); + + glm::vec3 c = planet->getColor(); + glUniform3f(m_shader_game_objects.location("materialColor"), c.x, c.y, + c.z); + glUniform1i(m_shader_game_objects.location("materialSeed"), + planet->seed); + glUniform1i(m_shader_game_objects.location("materialKind"), + (int)planet->material); + + m_planetModel->render(); + } +} + +void RendererPolygon3d::renderMissiles() { // TODO: add fire trail for missiles near the sun m_missileModel->bind(); diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.hpp b/game/renderer_polygon_3d/renderer_polygon_3d.hpp index 2efaeb4..aa7b07b 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.hpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.hpp @@ -2,29 +2,27 @@ #include -#include #include -#include +#include #include +#include +#include #include -#include - #include "glclasses.hpp" #include "game.hpp" -#include "state/trace.hpp" -#include "state/object.hpp" -#include "state/missile.hpp" -#include "state/player.hpp" -#include "state/planet.hpp" -#include "state/ship.hpp" #include "state/explosion.hpp" +#include "state/missile.hpp" +#include "state/object.hpp" +#include "state/planet.hpp" +#include "state/player.hpp" +#include "state/ship.hpp" +#include "state/trace.hpp" +#include "image_texture.hpp" #include "particle_batch.hpp" #include "polygon_model.hpp" -#include "image_texture.hpp" - namespace endofthejedi { class RendererPolygon3d : public Renderer { @@ -37,6 +35,7 @@ namespace endofthejedi { private: void renderPlanets(); + void renderSun(); void renderMissiles(); void renderShips(); void renderParticles(); @@ -97,6 +96,7 @@ namespace endofthejedi { std::string m_backgroundTexturePath; ImageTexture *m_texture; Shader m_postprocess_shader; + Shader m_multitexture_shader; Texture m_postprocess_tex0; Texture m_postprocess_tex1; Framebuffer m_postprocess_fbo;