From f2bdadd49fe1cd0435ac5e85c165a99b679a7ac9 Mon Sep 17 00:00:00 2001 From: Andreas Ortmann Date: Fri, 30 Sep 2016 22:47:28 +0200 Subject: [PATCH] one planet is always the sun and rendering the sun dynamically now. --- data/shader/gameobjects.frag | 22 ++++++---- .../renderer_polygon_3d.cpp | 25 ++++++++--- .../renderer_polygon_3d.hpp | 2 + game/state/planet.cpp | 18 ++++++++ game/state/planet.hpp | 43 +++++++------------ game/state/state.cpp | 7 ++- 6 files changed, 75 insertions(+), 42 deletions(-) diff --git a/data/shader/gameobjects.frag b/data/shader/gameobjects.frag index 78d2e63..9d78938 100644 --- a/data/shader/gameobjects.frag +++ b/data/shader/gameobjects.frag @@ -1,20 +1,26 @@ #version 120 -varying vec3 vertex; -varying vec3 normal; +varying vec3 vertex; +varying vec3 normal; +varying vec3 lightDirection; -uniform vec3 materialColor; -uniform vec3 lightColor; -uniform vec3 lightPosition; - -varying vec3 lightDirection; +uniform vec3 lightColor; +uniform vec3 lightPosition; +uniform float materialKind; +uniform vec3 materialColor; void main() { vec3 Eye = normalize(-vertex); vec3 Reflected = normalize(reflect( -lightPosition, normal)); - vec3 IAmbient = vec3(0.2f); + vec3 IAmbient = vec3(0.05f); + + if (materialKind == 6) { + // sun: shines by itself + gl_FragColor = vec4(materialColor, 1.0); + return; + } // TODO: add noise texture vec3 IDiffuse = vec3(materialColor) * lightColor * max(dot(normal, lightDirection), 0.0); diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.cpp b/game/renderer_polygon_3d/renderer_polygon_3d.cpp index a1898e8..8c35070 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.cpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.cpp @@ -60,11 +60,7 @@ namespace endofthejedi { // TODO: add ONE sun planet // TODO: add lights for explosions - glm::vec3 c = glm::vec3(1.0, 1.0, 0.8); - glUniform3f(m_shader.location("lightColor"), c.x, c.y, c.z); - - glm::vec3 p = glm::vec3(0.3f, 0.4f, 0.0f); - glUniform3f(m_shader.location("lightPosition"), p.x, p.y, p.z); + configureLightningInShader(); renderPlanets(); renderShips(); @@ -167,13 +163,15 @@ namespace endofthejedi { { 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.location("model"), 1, GL_FALSE, glm::value_ptr(model)); glm::vec3 c = planet->getColor(); glUniform3f(m_shader.location("materialColor"), c.x, c.y, c.z); - glUniform3f(m_shader.location("color"), c.x, c.y, c.z); + glUniform1f(m_shader.location("materialKind"), (float) planet->material); m_planetModel->render(); } @@ -293,4 +291,19 @@ namespace endofthejedi { } glPolygonMode(GL_FRONT, GL_FILL); } + + void RendererPolygon3d::configureLightningInShader() + { + glm::vec3 c = glm::vec3(1.0, 1.0, 0.8); + glUniform3f(m_shader.location("lightColor"), c.x, c.y, c.z); + + glm::vec3 p = glm::vec3(0.3f, 0.4f, 0.0f); + for (const game::Planet *planet : m_state->planets) { + if (planet->material == game::Planet::Material::Sun) { + p = glm::vec3(planet->position, 0.0); + break; + } + } + glUniform3f(m_shader.location("lightPosition"), p.x, p.y, p.z); + } } diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.hpp b/game/renderer_polygon_3d/renderer_polygon_3d.hpp index bfb19f7..69a0152 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.hpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.hpp @@ -49,6 +49,8 @@ namespace endofthejedi { void renderTraces(); + void configureLightningInShader(); + private: // all models are also here (for easy reloading etc.) std::vector m_models; diff --git a/game/state/planet.cpp b/game/state/planet.cpp index e69de29..f5f9173 100644 --- a/game/state/planet.cpp +++ b/game/state/planet.cpp @@ -0,0 +1,18 @@ +#include "planet.hpp" + +namespace game { + + glm::vec3 Planet::getColor() const + { + switch (material) { + case Material::Rock: return glm::vec3(0.4, 0.2, 0.1); + case Material::Metal: return glm::vec3(0.1, 0.1, 0.1); + case Material::Sand: return glm::vec3(0.6, 0.6, 0.3); + case Material::Gas: return glm::vec3(0.8, 0.5, 0.2); + case Material::Ice: return glm::vec3(0.7, 0.8, 1.0); + case Material::Water: return glm::vec3(0.2, 0.7, 1.0); + case Material::Sun: return glm::vec3(1.0, 1.0, 0.8); + default: return glm::vec3(1.0, 0.0, 1.0); // diagnostic color + } + } +} diff --git a/game/state/planet.hpp b/game/state/planet.hpp index 1f7436b..21741dd 100644 --- a/game/state/planet.hpp +++ b/game/state/planet.hpp @@ -11,37 +11,26 @@ class Planet : public Object { * Planets are build out of one material. * TODO: support mixture or multiple material per planet. */ - enum class Material { Rock, Metal, Sand, Gas, Ice, Water, Sun }; + enum class Material { Rock=0, Metal=1, Sand=2, Gas=3, Ice=4, Water=5, Sun=6 }; Planet(const glm::vec2 &pos, int id, float r) - : Planet(pos, id, r, Material::Rock) {} - - Planet(const glm::vec2 &pos, int id, float r, Material mat) - : Object(pos, r), id(id), material(mat), seed(rand()) {} - - glm::vec3 getColor() const { - switch (material) { - case Material::Rock: - return {x : 0.19, y : 0.19, z : 0.19}; - case Material::Metal: - return {x : 0.19, y : 0.19, z : 0.19}; - case Material::Sand: - return {x : 0.19, y : 0.19, z : 0.19}; - case Material::Gas: - return {x : 0.19, y : 0.19, z : 0.19}; - case Material::Ice: - return {x : 0.19, y : 0.19, z : 0.19}; - case Material::Water: - return {x : 0.19, y : 0.40, z : 0.72}; - case Material::Sun: - return {x : 0.64, y : 0.67, z : 0.19}; - } - return {x : 0.1, y : 0.1, z : 0.1}; + : Planet(pos, id, r, Material::Rock) + { } + Planet(const glm::vec2 &pos, int id, float r, Material mat) + : Object(pos, r), id(id), material(mat), seed(rand()) + { + } + + glm::vec3 getColor() const; + int id; - Material material; // for rendering and physics (can fly through sun and - // outer gas planets) - int seed; // just for rendering variation + + // for rendering and physics (can fly through sun and outer gas planets) + Material material; + + // just for rendering variation + int seed; }; } diff --git a/game/state/state.cpp b/game/state/state.cpp index a487223..6b72060 100644 --- a/game/state/state.cpp +++ b/game/state/state.cpp @@ -49,7 +49,12 @@ namespace game { } } while(glm::length(pos) < 0.2 && tries++ < 1000); - planets.push_back(new Planet(pos, i, 0.03 + 0.07*util::randf_0_1())); + Planet::Material mat = Planet::Material::Rock; + if (i == 0) { + mat = Planet::Material::Sun; + } + + planets.push_back(new Planet(pos, i, 0.03 + 0.07*util::randf_0_1(), mat)); } }