]> git.localhorst.tv Git - blobs.git/commitdiff
multiple light sources
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 12 Nov 2017 19:18:38 +0000 (20:18 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 12 Nov 2017 20:52:47 +0000 (21:52 +0100)
src/app/states.cpp
src/graphics/PlanetSurface.hpp
src/graphics/Program.hpp
src/graphics/shader.cpp

index b9fc57a4c3c15ea179d4a83b34490c513c34ecc7..86b2e82c3b71e3d9d9ab630a515facde11573ab9 100644 (file)
@@ -56,10 +56,35 @@ void MasterState::OnKeyDown(const SDL_KeyboardEvent &e) {
 }
 
 void MasterState::OnRender(graphics::Viewport &viewport) {
-       glm::dmat4 ppos = cam.Model(**sim.Suns().begin());
        assets.shaders.planet_surface.Activate();
        assets.shaders.planet_surface.SetTexture(assets.textures.tiles);
-       assets.shaders.planet_surface.SetLight(glm::vec3(cam.View() * ppos[3]), glm::vec3(1.0f, 1.0f, 1.0f), 1.0e6f);
+
+       int num_lights = 0;
+       for (auto sun : sim.Suns()) {
+               // TODO: source sun's light color and strength
+               assets.shaders.planet_surface.SetLight(
+                       num_lights,
+                       glm::vec3(cam.View() * cam.Model(*sun)[3]),
+                       glm::vec3(1.0f, 1.0f, 1.0f),
+                       1.0e6f);
+               ++num_lights;
+               if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS) {
+                       break;
+               }
+       }
+       for (auto planet : sim.Planets()) {
+               // TODO: indirect light from planets, calculate strength and get color somehow
+               assets.shaders.planet_surface.SetLight(
+                       num_lights,
+                       glm::vec3(cam.View() * cam.Model(*planet)[3]),
+                       glm::vec3(1.0f, 1.0f, 1.0f),
+                       10.0f);
+               ++num_lights;
+               if (num_lights >= graphics::PlanetSurface::MAX_LIGHTS) {
+                       break;
+               }
+       }
+       assets.shaders.planet_surface.SetNumLights(num_lights);
 
        for (auto planet : sim.Planets()) {
                assets.shaders.planet_surface.SetMVP(cam.Model(*planet), cam.View(), cam.Projection());
index 2482703b070b8eff0fe4e07409933b0b852afb92..8e98875851d70c0d859b0386aaae604396bab070 100644 (file)
@@ -13,6 +13,9 @@ class ArrayTexture;
 
 class PlanetSurface {
 
+public:
+       static constexpr int MAX_LIGHTS = 8;
+
 public:
        PlanetSurface();
        ~PlanetSurface();
@@ -29,7 +32,8 @@ public:
        void SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept;
        void SetNormal(const glm::vec3 &) noexcept;
        void SetTexture(ArrayTexture &) noexcept;
-       void SetLight(const glm::vec3 &pos, const glm::vec3 &color, float strength) noexcept;
+       void SetLight(int n, const glm::vec3 &pos, const glm::vec3 &color, float strength) noexcept;
+       void SetNumLights(int n) noexcept;
 
        const glm::mat4 &M() const noexcept { return m; }
        const glm::mat4 &V() const noexcept { return v; }
@@ -40,6 +44,8 @@ public:
 private:
        Program prog;
 
+       int num_lights;
+
        glm::mat4 m;
        glm::mat4 v;
        glm::mat4 p;
@@ -52,9 +58,8 @@ private:
        GLuint sampler_handle;
        GLuint normal_handle;
 
-       GLuint light_position_handle;
-       GLuint light_color_handle;
-       GLuint light_strength_handle;
+       GLuint num_lights_handle;
+       GLuint light_handle[MAX_LIGHTS * 3];
 
 };
 
index 7e0e93b8fab4700f5bb97e07243922555f24a2f3..2add96c290066027544b48afb44a159b74df7b13 100644 (file)
@@ -5,6 +5,7 @@
 
 #include <iosfwd>
 #include <list>
+#include <string>
 #include <GL/glew.h>
 
 
@@ -29,7 +30,9 @@ public:
        void Log(std::ostream &) const;
 
        GLint AttributeLocation(const GLchar *name) const noexcept;
+       GLint AttributeLocation(const std::string &name) const noexcept;
        GLint UniformLocation(const GLchar *name) const noexcept;
+       GLint UniformLocation(const std::string &name) const noexcept;
 
        void Uniform(GLint, GLint) noexcept;
        void Uniform(GLint, float) noexcept;
index d771d9f943385f4709108b99c2f58540cd868db2..573e90c3731d6f25fce4cc3a7a9e4ae7b98d3d97 100644 (file)
@@ -142,10 +142,18 @@ GLint Program::AttributeLocation(const GLchar *name) const noexcept {
        return glGetAttribLocation(handle, name);
 }
 
+GLint Program::AttributeLocation(const std::string &name) const noexcept {
+       return AttributeLocation(name.c_str());
+}
+
 GLint Program::UniformLocation(const GLchar *name) const noexcept {
        return glGetUniformLocation(handle, name);
 }
 
+GLint Program::UniformLocation(const std::string &name) const noexcept {
+       return UniformLocation(name.c_str());
+}
+
 
 void Program::Uniform(GLint loc, GLint val) noexcept {
        glUniform1i(loc, val);
@@ -168,6 +176,8 @@ void Program::Uniform(GLint loc, const glm::mat4 &val) noexcept {
 }
 
 
+constexpr int PlanetSurface::MAX_LIGHTS;
+
 PlanetSurface::PlanetSurface()
 : prog() {
        prog.LoadShader(
@@ -205,24 +215,28 @@ PlanetSurface::PlanetSurface()
 
                "uniform sampler2DArray tex_sampler;\n"
                "uniform vec3 normal;\n"
-               "uniform LightSource light;\n"
+               "uniform int num_lights;\n"
+               "uniform LightSource light[8];\n"
 
                "out vec3 color;\n"
 
                "void main() {\n"
                        "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n"
-                       "vec3 to_light = light.position - vtx_viewspace;\n"
-                       "float distance = length(to_light);\n"
-                       "vec3 light_dir = normalize(to_light);\n"
-                       "float attenuation = light.strength / (distance * distance);\n"
-                       "vec3 ambient = tex_color * vec3(0.01, 0.01, 0.01);\n"
-                       "vec3 diffuse = attenuation * max(0.0, dot(normal, light_dir)) * light.color * tex_color;\n"
-                       "vec3 view_dir = vec3(0.0, 0.0, 1.0);\n"
-                       "vec3 specular = vec3(0.0, 0.0, 0.0);\n"
-                       "if (dot(normal, light_dir) >= 0.0) {\n"
-                               "attenuation * light.color * pow(max(0.0, dot(reflect(-light_dir, normal), view_dir)), 25.0);\n"
+                       "vec3 total_light = tex_color * vec3(0.01, 0.01, 0.01);\n"
+                       "for (int i = 0; i < num_lights; ++i) {\n"
+                               "vec3 to_light = light[i].position - vtx_viewspace;\n"
+                               "float distance = length(to_light) + length(vtx_viewspace);\n"
+                               "vec3 light_dir = normalize(to_light);\n"
+                               "float attenuation = light[i].strength / (distance * distance);\n"
+                               "vec3 diffuse = attenuation * max(0.0, dot(normal, light_dir)) * light[i].color * tex_color;\n"
+                               "vec3 view_dir = vec3(0.0, 0.0, 1.0);\n"
+                               "vec3 specular = vec3(0.0, 0.0, 0.0);\n"
+                               "if (dot(normal, light_dir) >= 0.0) {\n"
+                                       "attenuation * light[i].color * pow(max(0.0, dot(reflect(-light_dir, normal), view_dir)), 25.0);\n"
+                               "}\n"
+                               "total_light = total_light + diffuse + specular;\n"
                        "}\n"
-                       "color = ambient + diffuse + specular;\n"
+                       "color = total_light;\n"
                "}\n"
        );
        prog.Link();
@@ -235,9 +249,12 @@ PlanetSurface::PlanetSurface()
        mvp_handle = prog.UniformLocation("MVP");
        sampler_handle = prog.UniformLocation("tex_sampler");
        normal_handle = prog.UniformLocation("normal");
-       light_position_handle = prog.UniformLocation("light.position");
-       light_color_handle = prog.UniformLocation("light.color");
-       light_strength_handle = prog.UniformLocation("light.strength");
+       num_lights_handle = prog.UniformLocation("num_lights");
+       for (int i = 0; i < MAX_LIGHTS; ++i) {
+               light_handle[3 * i + 0]  = prog.UniformLocation("light[" + std::to_string(i) + "].position");
+               light_handle[3 * i + 1]  = prog.UniformLocation("light[" + std::to_string(i) + "].color");
+               light_handle[3 * i + 2]  = prog.UniformLocation("light[" + std::to_string(i) + "].strength");
+       }
 }
 
 PlanetSurface::~PlanetSurface() {
@@ -272,10 +289,14 @@ void PlanetSurface::SetTexture(ArrayTexture &tex) noexcept {
        prog.Uniform(sampler_handle, GLint(0));
 }
 
-void PlanetSurface::SetLight(const glm::vec3 &pos, const glm::vec3 &color, float strength) noexcept {
-       prog.Uniform(light_position_handle, pos);
-       prog.Uniform(light_color_handle, color);
-       prog.Uniform(light_strength_handle, strength);
+void PlanetSurface::SetLight(int n, const glm::vec3 &pos, const glm::vec3 &color, float strength) noexcept {
+       prog.Uniform(light_handle[3 * n + 0], pos);
+       prog.Uniform(light_handle[3 * n + 1], color);
+       prog.Uniform(light_handle[3 * n + 2], strength);
+}
+
+void PlanetSurface::SetNumLights(int n) noexcept {
+       prog.Uniform(num_lights_handle, std::min(MAX_LIGHTS, n));
 }