From 1ee349e1fb3dcd5a46d6c018240e79120b1442f8 Mon Sep 17 00:00:00 2001 From: end Date: Thu, 29 Sep 2016 02:50:51 +0200 Subject: [PATCH] * fixes, refactoring & color attribute for planets --- game/renderer_polygon_3d/gameobjects.frag | 11 ++++ game/renderer_polygon_3d/gameobjects.vert | 12 ++++ .../renderer_polygon_3d.cpp | 48 +++----------- game/renderer_polygon_3d/simple.frag | 8 +++ game/renderer_polygon_3d/simple.vert | 9 +++ game/renderer_polygon_3d/sphere.frag | 11 ++++ game/renderer_polygon_3d/sphere.vert | 14 ++++ game/state/planet.hpp | 65 ++++++++++--------- 8 files changed, 111 insertions(+), 67 deletions(-) create mode 100644 game/renderer_polygon_3d/gameobjects.frag create mode 100644 game/renderer_polygon_3d/gameobjects.vert create mode 100644 game/renderer_polygon_3d/simple.frag create mode 100644 game/renderer_polygon_3d/simple.vert create mode 100644 game/renderer_polygon_3d/sphere.frag create mode 100644 game/renderer_polygon_3d/sphere.vert diff --git a/game/renderer_polygon_3d/gameobjects.frag b/game/renderer_polygon_3d/gameobjects.frag new file mode 100644 index 0000000..f67de40 --- /dev/null +++ b/game/renderer_polygon_3d/gameobjects.frag @@ -0,0 +1,11 @@ +R"raw_string( +#version 120 +varying vec3 vertex; +uniform vec3 color; +void main() +{ + //gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); + //gl_FragColor = vec4(0.5+0.5*vertex.xyz, 1.0); + gl_FragColor = vec4(color, 1.0); +} +)raw_string" diff --git a/game/renderer_polygon_3d/gameobjects.vert b/game/renderer_polygon_3d/gameobjects.vert new file mode 100644 index 0000000..94b23a6 --- /dev/null +++ b/game/renderer_polygon_3d/gameobjects.vert @@ -0,0 +1,12 @@ +R"raw_string( +#version 120 +varying vec3 vertex; +uniform mat4 model; +void main() +{ + //vec3 p = position + scale*gl_Vertex.xyz; + vec3 p = (model*gl_Vertex).xyz; + gl_Position = vec4(p, 1.0); + vertex = p.xyz; +} +)raw_string" diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.cpp b/game/renderer_polygon_3d/renderer_polygon_3d.cpp index b82f0e9..ac4073b 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.cpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.cpp @@ -38,59 +38,31 @@ namespace endofthejedi { m_particles->upload(); std::string vss_simple = - "#version 120\n" - "varying vec3 vertex;\n" - "void main()\n" - "{\n" - " gl_Position = gl_Vertex;\n" - " vertex = gl_Position.xyz;\n" - "}\n" + #include "simple.vert" ; std::string fss_simple = - "#version 120\n" - "varying vec3 vertex;\n" - "void main()\n" - "{\n" - " gl_FragColor = vec4(0.5+0.5*vertex.xyz, 1.0);\n" - "}\n" + #include "simple.frag" ; std::string vss_game_objects = - "#version 120\n" - "varying vec3 vertex;\n" - "uniform mat4 model;\n" - "void main()\n" - "{\n" - //" vec3 p = position + scale*gl_Vertex.xyz;\n" - " vec3 p = (model*gl_Vertex).xyz;\n" - " gl_Position = vec4(p, 1.0);\n" - " vertex = p.xyz;\n" - "}\n" + #include "gameobjects.vert" ; std::string fss_game_objects = - "#version 120\n" - "varying vec3 vertex;\n" - "uniform vec3 color;\n" - "void main()\n" - "{\n" - //" gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n" - //" gl_FragColor = vec4(0.5+0.5*vertex.xyz, 1.0);\n" - " gl_FragColor = vec4(color, 1.0);\n" - "}\n" + #include "gameobjects.frag" ; m_shader.init(); #if 0 - (void) vss_game_objects; - (void) fss_game_objects; + (void) vss_simple; + (void) fss_simple; m_shader.load(vss_simple.c_str(), GL_VERTEX_SHADER); m_shader.load(fss_simple.c_str(), GL_FRAGMENT_SHADER); #else - (void) vss_simple; - (void) fss_simple; + (void) vss_game_objects; + (void) fss_game_objects; m_shader.load(vss_game_objects.c_str(), GL_VERTEX_SHADER); m_shader.load(fss_game_objects.c_str(), GL_FRAGMENT_SHADER); #endif @@ -135,8 +107,8 @@ namespace endofthejedi { glm::mat4 model = computeModelMatrix(planet); glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model)); - glm::vec3 c = glm::vec3(0.7, 0.2, 0.1); - glUniform3f(m_shader.location("color"), c.x, c.y, c.z); + glm::vec3 c = planet->getColor(); + glUniform3f(m_shader.location("color"), c.x, c.y, c.z); m_planetModel->render(); } diff --git a/game/renderer_polygon_3d/simple.frag b/game/renderer_polygon_3d/simple.frag new file mode 100644 index 0000000..7bf2e0d --- /dev/null +++ b/game/renderer_polygon_3d/simple.frag @@ -0,0 +1,8 @@ +R"raw_string( +#version 120 +varying vec3 vertex; +void main() +{ + gl_FragColor = vec4(0.5+0.5*vertex.xyz, 1.0); +} +)raw_string" diff --git a/game/renderer_polygon_3d/simple.vert b/game/renderer_polygon_3d/simple.vert new file mode 100644 index 0000000..b05d873 --- /dev/null +++ b/game/renderer_polygon_3d/simple.vert @@ -0,0 +1,9 @@ +R"raw_string( +#version 120 +varying vec3 vertex; +void main() +{ + gl_Position = gl_Vertex; + vertex = gl_Position.xyz; +} +)raw_string" diff --git a/game/renderer_polygon_3d/sphere.frag b/game/renderer_polygon_3d/sphere.frag new file mode 100644 index 0000000..90e0164 --- /dev/null +++ b/game/renderer_polygon_3d/sphere.frag @@ -0,0 +1,11 @@ +R"raw_string( +#version 120 +varying vec3 vertex; +uniform vec3 color; +void main() +{ + //gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); + //gl_FragColor = vec4(0.5+0.5*vertex.xyz, 1.0); + gl_FragColor = vec4(color, 1.0); +} +)raw_string" \ No newline at end of file diff --git a/game/renderer_polygon_3d/sphere.vert b/game/renderer_polygon_3d/sphere.vert new file mode 100644 index 0000000..ccf4687 --- /dev/null +++ b/game/renderer_polygon_3d/sphere.vert @@ -0,0 +1,14 @@ +R"raw_string( +#version 120 +varying vec3 vertex; +uniform mat4 model; + + +void main() +{ + //vec3 p = position + scale*gl_Vertex.xyz; + vec3 p = (model*gl_Vertex).xyz; + gl_Position = vec4(p, 1.0); + vertex = p.xyz; +} +)raw_string" \ No newline at end of file diff --git a/game/state/planet.hpp b/game/state/planet.hpp index e6ff6e7..1f7436b 100644 --- a/game/state/planet.hpp +++ b/game/state/planet.hpp @@ -5,36 +5,43 @@ #include namespace game { - class Planet : public Object { - public: - /** - * 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 - }; +class Planet : public Object { + public: + /** + * 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 }; - Planet(const glm::vec2 &pos, int id, float r) : Planet(pos, id, r, Material::Rock) - { + 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(const glm::vec2 &pos, int id, float r, Material mat) - : Object(pos, r) - , id(id) - , material(mat) - , seed(rand()) - { - } - - int id; - Material material; // for rendering and physics (can fly through sun and outer gas planets) - int seed; // just for rendering variation - }; + int id; + Material material; // for rendering and physics (can fly through sun and + // outer gas planets) + int seed; // just for rendering variation +}; }