Lighting works... Just not properly

Commiting now so that there is something displaying for the lit
scene.
This commit is contained in:
elipzer 2018-10-15 22:38:59 -04:00
parent 98c6d3868c
commit 8ee0ab2edb
10 changed files with 35 additions and 15 deletions

View File

@ -14,7 +14,7 @@ namespace charcoal
{ {
class Pipeline : public builtin::Pipeline<ShaderProgram, Batch>, public WithCamera class Pipeline : public builtin::Pipeline<ShaderProgram, Batch>, public WithCamera
{ {
public: protected:
void prepare_opengl() override void prepare_opengl() override
{ {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);

View File

@ -22,6 +22,7 @@ namespace charcoal
} }
} }
protected:
virtual void prepare_opengl() {}; virtual void prepare_opengl() {};
virtual void prepare_uniforms() {}; virtual void prepare_uniforms() {};

View File

@ -24,12 +24,12 @@ namespace charcoal
// Shader Data Types // Shader Data Types
struct Light struct PhongLight
{ {
typedef vec3 Power; typedef vec3 Power;
typedef vec3 Fade; typedef vec3 Fade;
Light( PhongLight(
const Position& position, const Position& position,
const Power& power, const Power& power,
const ColorRGB& ambient, const ColorRGB& ambient,
@ -53,9 +53,9 @@ namespace charcoal
Fade fade; Fade fade;
}; };
struct Material struct PhongMaterial
{ {
Material( PhongMaterial(
float ambient = 1.0f, float ambient = 1.0f,
float diffuse = 1.0f, float diffuse = 1.0f,
float specular = 0.0f, float specular = 0.0f,
@ -86,11 +86,11 @@ namespace charcoal
{ {
void set_position(const Position& position) { this->position = position; } void set_position(const Position& position) { this->position = position; }
void set_normal(const Normal& normal) { this->normal = normal; } 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; Position position;
Normal normal; Normal normal;
Material material; PhongMaterial material;
}; };
struct PTVertex struct PTVertex

View File

@ -38,9 +38,9 @@ namespace charcoal
glUniformMatrix4fv(uniform_index, 1, transpose ? GL_TRUE : GL_FALSE, &matrix[0][0]); glUniformMatrix4fv(uniform_index, 1, transpose ? GL_TRUE : GL_FALSE, &matrix[0][0]);
} }
void uniform_lights(int uniform_index, const std::vector<Light>& lights) void uniform_lights(int uniform_index, const std::vector<PhongLight>& lights)
{ {
for (std::vector<Light>::size_type i = 0; i < lights.size(); ++i) for (std::vector<PhongLight>::size_type i = 0; i < lights.size(); ++i)
{ {
glUniform3fv(uniform_index++, 1, &lights[i].position[0]); glUniform3fv(uniform_index++, 1, &lights[i].position[0]);
glUniform3fv(uniform_index++, 1, &lights[i].power[0]); glUniform3fv(uniform_index++, 1, &lights[i].power[0]);

View File

@ -19,7 +19,7 @@ namespace charcoal
void uniform_float(int uniform_index, float value); void uniform_float(int uniform_index, float value);
void uniform_vec3(int uniform_index, const vec3& value); void uniform_vec3(int uniform_index, const vec3& value);
void uniform_matrix(int uniform_index, const mat4& matrix, bool transpose = false); void uniform_matrix(int uniform_index, const mat4& matrix, bool transpose = false);
void uniform_lights(int uniform_index, const std::vector<Light>& lights); // TODO: This may want to be moved somewhere else void uniform_lights(int uniform_index, const std::vector<PhongLight>& lights); // TODO: This may want to be moved somewhere else
} }
} }
} }

View File

@ -16,6 +16,16 @@ namespace charcoal
class Pipeline : public builtin::Pipeline<ShaderProgram, Batch>, public WithCamera class Pipeline : public builtin::Pipeline<ShaderProgram, Batch>, public WithCamera
{ {
public: public:
void reset_lights()
{
m_lights.clear();
}
void add_light(const Light& light)
{
m_lights.push_back(light);
}
protected:
void prepare_opengl() override void prepare_opengl() override
{ {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
@ -25,7 +35,12 @@ namespace charcoal
void prepare_uniforms() override void prepare_uniforms() override
{ {
glutil::uniform_matrix(0, get_camera()->get_world_to_view_matrix()); 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<PhongLight> m_lights;
}; };
} }
} }

View File

@ -2,13 +2,13 @@
#include "BuiltinTypes.h" #include "BuiltinTypes.h"
namespace charcoal namespace charcoal
{ {
namespace builtin namespace builtin
{ {
namespace lit namespace lit
{ {
typedef PhongLight Light;
typedef PNMVertex Vertex; typedef PNMVertex Vertex;
typedef Index Index; typedef Index Index;
typedef RenderableT<Vertex, Index> Renderable; typedef RenderableT<Vertex, Index> Renderable;

View File

@ -14,7 +14,7 @@ namespace charcoal
{ {
class Pipeline : public builtin::Pipeline<ShaderProgram, Batch>, public WithCamera class Pipeline : public builtin::Pipeline<ShaderProgram, Batch>, public WithCamera
{ {
public: protected:
void prepare_opengl() override void prepare_opengl() override
{ {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);

View File

@ -13,7 +13,8 @@ MyBuiltinLitScene::MyBuiltinLitScene(Application& application)
: Scene(application), : Scene(application),
m_shape(meshgenerator::gen_cube_p<lit::Vertex, lit::Index>(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f), DrawMode::DRAW_TRIANGLES), m_shape(meshgenerator::gen_cube_p<lit::Vertex, lit::Index>(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_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.add_batch(&m_batch);
m_pipeline.set_camera(&m_camera); 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.reset_rendered();
m_batch.add_rendered(m_pose_a); m_batch.add_rendered(m_pose_a);
m_batch.add_rendered(m_pose_b); m_batch.add_rendered(m_pose_b);
m_pipeline.reset_lights();
m_pipeline.add_light(m_light);
} }
void MyBuiltinLitScene::prerender() void MyBuiltinLitScene::prerender()

View File

@ -11,8 +11,6 @@ using namespace charcoal::builtin;
class MyBuiltinLitScene : public Scene class MyBuiltinLitScene : public Scene
{ {
public: public:
// TODO: Add the lights!
// This should be done in the pipeline!
MyBuiltinLitScene(Application& application); MyBuiltinLitScene(Application& application);
void init() override; void init() override;
@ -34,6 +32,8 @@ private:
lit::Pipeline m_pipeline; lit::Pipeline m_pipeline;
lit::Light m_light;
Poseable m_pose_a; Poseable m_pose_a;
Poseable m_pose_b; Poseable m_pose_b;
}; };