From 8ee0ab2edb0ca6d632a5346ce6febaf1c651f997 Mon Sep 17 00:00:00 2001 From: elipzer Date: Mon, 15 Oct 2018 22:38:59 -0400 Subject: [PATCH] Lighting works... Just not properly Commiting now so that there is something displaying for the lit scene. --- CharcoalBuiltin/BasicPipeline.h | 2 +- CharcoalBuiltin/BuiltinPipeline.h | 1 + CharcoalBuiltin/BuiltinTypes.h | 12 ++++++------ CharcoalBuiltin/GLUtil.cpp | 4 ++-- CharcoalBuiltin/GLUtil.h | 2 +- CharcoalBuiltin/LitPipeline.h | 15 +++++++++++++++ CharcoalBuiltin/LitTypes.h | 2 +- CharcoalBuiltin/TexturedPipeline.h | 2 +- Example/MyBuiltinLitScene.cpp | 6 +++++- Example/MyBuiltinLitScene.h | 4 ++-- 10 files changed, 35 insertions(+), 15 deletions(-) diff --git a/CharcoalBuiltin/BasicPipeline.h b/CharcoalBuiltin/BasicPipeline.h index 4288171..4c621c9 100644 --- a/CharcoalBuiltin/BasicPipeline.h +++ b/CharcoalBuiltin/BasicPipeline.h @@ -14,7 +14,7 @@ namespace charcoal { class Pipeline : public builtin::Pipeline, public WithCamera { - public: + protected: void prepare_opengl() override { glEnable(GL_DEPTH_TEST); diff --git a/CharcoalBuiltin/BuiltinPipeline.h b/CharcoalBuiltin/BuiltinPipeline.h index 175c1ae..33bff3e 100644 --- a/CharcoalBuiltin/BuiltinPipeline.h +++ b/CharcoalBuiltin/BuiltinPipeline.h @@ -22,6 +22,7 @@ namespace charcoal } } + protected: virtual void prepare_opengl() {}; virtual void prepare_uniforms() {}; diff --git a/CharcoalBuiltin/BuiltinTypes.h b/CharcoalBuiltin/BuiltinTypes.h index 5630de2..e1f06ef 100644 --- a/CharcoalBuiltin/BuiltinTypes.h +++ b/CharcoalBuiltin/BuiltinTypes.h @@ -24,12 +24,12 @@ namespace charcoal // Shader Data Types - struct Light + struct PhongLight { typedef vec3 Power; typedef vec3 Fade; - Light( + PhongLight( const Position& position, const Power& power, const ColorRGB& ambient, @@ -53,9 +53,9 @@ namespace charcoal Fade fade; }; - struct Material + struct PhongMaterial { - Material( + PhongMaterial( float ambient = 1.0f, float diffuse = 1.0f, float specular = 0.0f, @@ -86,11 +86,11 @@ namespace charcoal { void set_position(const Position& position) { this->position = position; } void set_normal(const Normal& normal) { this->normal = normal; } - void set_material(const Material& material) { this->material = material; } + void set_material(const PhongMaterial& material) { this->material = material; } Position position; Normal normal; - Material material; + PhongMaterial material; }; struct PTVertex diff --git a/CharcoalBuiltin/GLUtil.cpp b/CharcoalBuiltin/GLUtil.cpp index be3de61..37dd732 100644 --- a/CharcoalBuiltin/GLUtil.cpp +++ b/CharcoalBuiltin/GLUtil.cpp @@ -38,9 +38,9 @@ namespace charcoal glUniformMatrix4fv(uniform_index, 1, transpose ? GL_TRUE : GL_FALSE, &matrix[0][0]); } - void uniform_lights(int uniform_index, const std::vector& lights) + void uniform_lights(int uniform_index, const std::vector& lights) { - for (std::vector::size_type i = 0; i < lights.size(); ++i) + for (std::vector::size_type i = 0; i < lights.size(); ++i) { glUniform3fv(uniform_index++, 1, &lights[i].position[0]); glUniform3fv(uniform_index++, 1, &lights[i].power[0]); diff --git a/CharcoalBuiltin/GLUtil.h b/CharcoalBuiltin/GLUtil.h index 88bfbc7..3410b57 100644 --- a/CharcoalBuiltin/GLUtil.h +++ b/CharcoalBuiltin/GLUtil.h @@ -19,7 +19,7 @@ namespace charcoal void uniform_float(int uniform_index, float value); void uniform_vec3(int uniform_index, const vec3& value); void uniform_matrix(int uniform_index, const mat4& matrix, bool transpose = false); - void uniform_lights(int uniform_index, const std::vector& lights); // TODO: This may want to be moved somewhere else + void uniform_lights(int uniform_index, const std::vector& lights); // TODO: This may want to be moved somewhere else } } } \ No newline at end of file diff --git a/CharcoalBuiltin/LitPipeline.h b/CharcoalBuiltin/LitPipeline.h index 8e3635e..1d1bb20 100644 --- a/CharcoalBuiltin/LitPipeline.h +++ b/CharcoalBuiltin/LitPipeline.h @@ -16,6 +16,16 @@ namespace charcoal class Pipeline : public builtin::Pipeline, public WithCamera { public: + void reset_lights() + { + m_lights.clear(); + } + + void add_light(const Light& light) + { + m_lights.push_back(light); + } + protected: void prepare_opengl() override { glEnable(GL_DEPTH_TEST); @@ -25,7 +35,12 @@ namespace charcoal void prepare_uniforms() override { glutil::uniform_matrix(0, get_camera()->get_world_to_view_matrix()); + glutil::uniform_vec3(4, get_camera()->get_position()); + glutil::uniform_uint(5, m_lights.size()); + glutil::uniform_lights(6, m_lights); } + private: + std::vector m_lights; }; } } diff --git a/CharcoalBuiltin/LitTypes.h b/CharcoalBuiltin/LitTypes.h index 188f0f9..5743aa5 100644 --- a/CharcoalBuiltin/LitTypes.h +++ b/CharcoalBuiltin/LitTypes.h @@ -2,13 +2,13 @@ #include "BuiltinTypes.h" - namespace charcoal { namespace builtin { namespace lit { + typedef PhongLight Light; typedef PNMVertex Vertex; typedef Index Index; typedef RenderableT Renderable; diff --git a/CharcoalBuiltin/TexturedPipeline.h b/CharcoalBuiltin/TexturedPipeline.h index 2d0d3cf..5364d81 100644 --- a/CharcoalBuiltin/TexturedPipeline.h +++ b/CharcoalBuiltin/TexturedPipeline.h @@ -14,7 +14,7 @@ namespace charcoal { class Pipeline : public builtin::Pipeline, public WithCamera { - public: + protected: void prepare_opengl() override { glEnable(GL_DEPTH_TEST); diff --git a/Example/MyBuiltinLitScene.cpp b/Example/MyBuiltinLitScene.cpp index d2df1d1..930090f 100644 --- a/Example/MyBuiltinLitScene.cpp +++ b/Example/MyBuiltinLitScene.cpp @@ -13,7 +13,8 @@ MyBuiltinLitScene::MyBuiltinLitScene(Application& application) : Scene(application), m_shape(meshgenerator::gen_cube_p(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f), DrawMode::DRAW_TRIANGLES), m_camera((float)TAU_1_4, (float)m_screen_size.x / m_screen_size.y, 1.0f, 10.0f, vec3(0.0f, 0.0f, 5.0f)), - m_batch(&m_shape, 2) + m_batch(&m_shape, 2), + m_light(Position(0.0f, 5.0f, 0.0f), PhongLight::Power(0.1f, 100.0f, 1.0f), ColorRGB(1.0f, 1.0f, 1.0f), ColorRGB(1.0f, 1.0f, 1.0f), ColorRGB(1.0f, 1.0f, 1.0f), PhongLight::Fade(1.0f, 0.1f, 0.01f)) { m_pipeline.add_batch(&m_batch); m_pipeline.set_camera(&m_camera); @@ -65,6 +66,9 @@ void MyBuiltinLitScene::update(float delta_time, clock_t clock) m_batch.reset_rendered(); m_batch.add_rendered(m_pose_a); m_batch.add_rendered(m_pose_b); + + m_pipeline.reset_lights(); + m_pipeline.add_light(m_light); } void MyBuiltinLitScene::prerender() diff --git a/Example/MyBuiltinLitScene.h b/Example/MyBuiltinLitScene.h index 308154f..e80337c 100644 --- a/Example/MyBuiltinLitScene.h +++ b/Example/MyBuiltinLitScene.h @@ -11,8 +11,6 @@ using namespace charcoal::builtin; class MyBuiltinLitScene : public Scene { public: - // TODO: Add the lights! - // This should be done in the pipeline! MyBuiltinLitScene(Application& application); void init() override; @@ -34,6 +32,8 @@ private: lit::Pipeline m_pipeline; + lit::Light m_light; + Poseable m_pose_a; Poseable m_pose_b; }; \ No newline at end of file