Added namespaces to builtin types

This commit is contained in:
Elipzer 2018-09-19 03:52:42 -04:00
parent cc9db05987
commit 821111416f
46 changed files with 578 additions and 449 deletions

View File

@ -4,26 +4,29 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
void BasicBatch::setup_vao() namespace basic
{ {
glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo); void Batch::setup_vao()
glEnableVertexAttribArray(0); {
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(BasicVertex), NULL); glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[0]); glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), NULL);
glEnableVertexAttribArray(2); glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[0]);
glEnableVertexAttribArray(3); glEnableVertexAttribArray(1);
glEnableVertexAttribArray(4); glEnableVertexAttribArray(2);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(0 * sizeof(vec4))); glEnableVertexAttribArray(3);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(1 * sizeof(vec4))); glEnableVertexAttribArray(4);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(2 * sizeof(vec4))); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(0 * sizeof(vec4)));
glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(3 * sizeof(vec4))); glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(1 * sizeof(vec4)));
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(2 * sizeof(vec4)));
glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(3 * sizeof(vec4)));
glVertexAttribDivisor(0, 0); // Send the mesh data once glVertexAttribDivisor(0, 0); // Send the mesh data once
glVertexAttribDivisor(1, 1); // Send the offset data for each instance drawn glVertexAttribDivisor(1, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(2, 1); // Send the offset data for each instance drawn glVertexAttribDivisor(2, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(3, 1); // Send the offset data for each instance drawn glVertexAttribDivisor(3, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(4, 1); // Send the offset data for each instance drawn glVertexAttribDivisor(4, 1); // Send the offset data for each instance drawn
}
} }
} }
} }

View File

@ -1,30 +1,33 @@
#pragma once #pragma once
#include "PoseableBatch.h" #include "PoseableBatch.h"
#include "BuiltinTypes.h" #include "BasicTypes.h"
namespace charcoal namespace charcoal
{ {
namespace builtin namespace builtin
{ {
class BasicBatch : public PoseableBatch<BasicVertex, BasicIndex, BasicRenderable> namespace basic
{ {
public: class Batch : public PoseableBatch<Vertex, Index, Renderable>
BasicBatch( {
BasicRenderable* renderable, public:
int element_count Batch(
) : PoseableBatch<BasicVertex, BasicIndex, BasicRenderable>(renderable, element_count) Renderable* renderable,
{} int element_count
) : PoseableBatch<Vertex, Index, Renderable>(renderable, element_count)
{}
BasicBatch( Batch(
BasicRenderable* renderable, Renderable* renderable,
int element_count, int element_count,
int element_render_count int element_render_count
) : PoseableBatch<BasicVertex, BasicIndex, BasicRenderable>(renderable, element_count, element_render_count) ) : PoseableBatch<Vertex, Index, Renderable>(renderable, element_count, element_render_count)
{} {}
protected: protected:
void setup_vao() override; void setup_vao() override;
}; };
}
} }
} }

View File

@ -9,36 +9,39 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
void BasicScene::init() namespace basic
{ {
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter) void Scene::init()
{ {
BasicBatch& batch = *iter; for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
batch.init(); {
add_prerenderable(&batch); Batch& batch = *iter;
batch.init();
add_prerenderable(&batch);
}
} }
}
void BasicScene::use() void Scene::use()
{
// TODO: move to glutil
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
}
void BasicScene::unuse()
{
}
void BasicScene::render()
{
glutil::clear_screen();
m_shader_program.use();
glutil::uniform_matrix(0, m_p_camera->get_world_to_view_matrix());
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
{ {
iter->render(); // TODO: move to glutil
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
}
void Scene::unuse()
{
}
void Scene::render()
{
glutil::clear_screen();
m_shader_program.use();
glutil::uniform_matrix(0, m_p_camera->get_world_to_view_matrix());
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
{
iter->render();
}
} }
} }
} }

View File

@ -2,10 +2,11 @@
#include <vector> #include <vector>
#include "Application.h"
#include "AutoPrerenderingScene.h" #include "AutoPrerenderingScene.h"
#include "BasicShaderProgram.h" #include "BasicShaderProgram.h"
#include "BuiltinTypes.h" #include "BasicTypes.h"
#include "Camera.h" #include "Camera.h"
#include "Batched.h" #include "Batched.h"
#include "BasicBatch.h" #include "BasicBatch.h"
@ -16,26 +17,29 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
class BasicScene : public AutoPrerenderingScene, public Batched<BasicRenderable, BasicBatch> namespace basic
{ {
public: class Scene : public AutoPrerenderingScene, public Batched<Renderable, Batch>
BasicScene(Application& application) : AutoPrerenderingScene(application) {} {
virtual ~BasicScene() {} public:
Scene(Application& application) : AutoPrerenderingScene(application) {}
virtual ~Scene() {}
void init() override; void init() override;
void use() override; void use() override;
void unuse() override; void unuse() override;
void render() override; void render() override;
protected: protected:
void set_camera(const Camera* p_camera) { m_p_camera = p_camera; } void set_camera(const Camera* p_camera) { m_p_camera = p_camera; }
private: private:
BasicShaderProgram m_shader_program; ShaderProgram m_shader_program;
const Camera* m_p_camera = nullptr; const Camera* m_p_camera = nullptr;
}; };
}
} }
} }

View File

@ -6,10 +6,13 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
class BasicShaderProgram : public VertexFragmentShaderProgram namespace basic
{ {
public: class ShaderProgram : public VertexFragmentShaderProgram
BasicShaderProgram() : VertexFragmentShaderProgram(SHADER_PATH "BasicVS.glsl", SHADER_PATH "BasicFS.glsl") {} {
}; public:
ShaderProgram() : VertexFragmentShaderProgram(SHADER_PATH "BasicVS.glsl", SHADER_PATH "BasicFS.glsl") {}
};
}
} }
} }

16
OpenGLEngine/BasicTypes.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include "BuiltinTypes.h"
namespace charcoal
{
namespace builtin
{
namespace basic
{
typedef PVertex Vertex;
typedef Index Index;
typedef RenderableT<Vertex, Index> Renderable;
}
}
}

View File

@ -5,11 +5,13 @@
#include <cstddef> #include <cstddef>
#include <vector> #include <vector>
#include "Renderable.h"
#include "Exception.h" #include "Exception.h"
namespace charcoal namespace charcoal
{ {
template <typename VertexType, typename IndexType, int element_buffer_count, typename Renderable = Renderable<VertexType, IndexType> > template <typename VertexType, typename IndexType, int element_buffer_count, typename Renderable = RenderableT<VertexType, IndexType> >
class Batch class Batch
{ {
public: public:

View File

@ -7,16 +7,16 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
template <typename VertexType, typename IndexType, int element_buffer_count, typename Renderable = Renderable<VertexType, IndexType> > template <typename VertexType, typename IndexType, int element_buffer_count, typename RenderableT = RenderableT<VertexType, IndexType> >
class Batch : public Prerenderable, public charcoal::Batch<VertexType, IndexType, element_buffer_count, Renderable> class Batch : public Prerenderable, public charcoal::Batch<VertexType, IndexType, element_buffer_count, RenderableT>
{ {
public: public:
Batch( Batch(
const Renderable* renderable, const RenderableT* renderable,
int element_render_count int element_render_count
) : charcoal::Batch<VertexType, IndexType, element_buffer_count, Renderable>(renderable, element_render_count) {} ) : charcoal::Batch<VertexType, IndexType, element_buffer_count, RenderableT>(renderable, element_render_count) {}
void prerender() override { charcoal::Batch<VertexType, IndexType, element_buffer_count, Renderable>::prerender(); } void prerender() override { charcoal::Batch<VertexType, IndexType, element_buffer_count, RenderableT>::prerender(); }
}; };
} }
} }

View File

@ -99,23 +99,5 @@ namespace charcoal
Position position; Position position;
UV uv; UV uv;
}; };
// typedefs for builtin types
typedef PVertex BasicVertex;
typedef Index BasicIndex;
typedef Renderable<BasicVertex, BasicIndex> BasicRenderable;
typedef PNMVertex LitVertex;
typedef Index LitIndex;
typedef Renderable<LitVertex, LitIndex> LitRenderable;
typedef PNMVertex LitShadowedVertex;
typedef Index LitShadowedIndex;
typedef Renderable<LitShadowedVertex, LitShadowedIndex> LitShadowedRenderable;
typedef PTVertex TexturedVertex;
typedef Index TexturedIndex;
typedef TextureRenderable<TexturedVertex, TexturedIndex> TexturedRenderable;
} }
} }

View File

@ -4,32 +4,35 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
void LitBatch::setup_vao() namespace lit
{ {
glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo); void Batch::setup_vao()
glEnableVertexAttribArray(0); {
glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo);
glEnableVertexAttribArray(2); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(LitVertex), (void*)(offsetof(LitVertex, position))); glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(LitVertex), (void*)(offsetof(LitVertex, normal))); glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(LitVertex), (void*)(offsetof(LitVertex, material))); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(offsetof(Vertex, position)));
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[0]); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(offsetof(Vertex, normal)));
glEnableVertexAttribArray(3); glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(offsetof(Vertex, material)));
glEnableVertexAttribArray(4); glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[0]);
glEnableVertexAttribArray(5); glEnableVertexAttribArray(3);
glEnableVertexAttribArray(6); glEnableVertexAttribArray(4);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(0 * sizeof(vec4))); glEnableVertexAttribArray(5);
glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(1 * sizeof(vec4))); glEnableVertexAttribArray(6);
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(2 * sizeof(vec4))); glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(0 * sizeof(vec4)));
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(3 * sizeof(vec4))); glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(1 * sizeof(vec4)));
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(2 * sizeof(vec4)));
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(3 * sizeof(vec4)));
glVertexAttribDivisor(0, 0); // Send the mesh data once glVertexAttribDivisor(0, 0); // Send the mesh data once
glVertexAttribDivisor(1, 0); // Send the mesh data once glVertexAttribDivisor(1, 0); // Send the mesh data once
glVertexAttribDivisor(2, 0); // Send the mesh data once glVertexAttribDivisor(2, 0); // Send the mesh data once
glVertexAttribDivisor(3, 1); // Send the offset data for each instance drawn glVertexAttribDivisor(3, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(4, 1); // Send the offset data for each instance drawn glVertexAttribDivisor(4, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(5, 1); // Send the offset data for each instance drawn glVertexAttribDivisor(5, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(6, 1); // Send the offset data for each instance drawn glVertexAttribDivisor(6, 1); // Send the offset data for each instance drawn
}
} }
} }
} }

View File

@ -1,30 +1,33 @@
#pragma once #pragma once
#include "PoseableBatch.h" #include "PoseableBatch.h"
#include "BuiltinTypes.h" #include "LitTypes.h"
namespace charcoal namespace charcoal
{ {
namespace builtin namespace builtin
{ {
class LitBatch : public PoseableBatch<LitVertex, LitIndex, LitRenderable> namespace lit
{ {
public: class Batch : public PoseableBatch<Vertex, Index, Renderable>
LitBatch( {
LitRenderable* renderable, public:
int element_count Batch(
) : PoseableBatch<LitVertex, LitIndex, LitRenderable>(renderable, element_count) Renderable* renderable,
{} int element_count
) : PoseableBatch<Vertex, Index, Renderable>(renderable, element_count)
{}
LitBatch( Batch(
LitRenderable* renderable, Renderable* renderable,
int element_count, int element_count,
int element_render_count int element_render_count
) : PoseableBatch<LitVertex, LitIndex, LitRenderable>(renderable, element_count, element_render_count) ) : PoseableBatch<Vertex, Index, Renderable>(renderable, element_count, element_render_count)
{} {}
protected: protected:
void setup_vao() override; void setup_vao() override;
}; };
}
} }
} }

View File

@ -10,39 +10,42 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
void LitScene::init() namespace lit
{ {
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter) void Scene::init()
{ {
LitBatch& batch = *iter; for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
batch.init(); {
add_prerenderable(&batch); Batch& batch = *iter;
batch.init();
add_prerenderable(&batch);
}
} }
}
void LitScene::use() void Scene::use()
{
// TODO: move to glutil
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
}
void LitScene::unuse()
{
}
void LitScene::render()
{
glutil::clear_screen();
m_shader_program.use();
glutil::uniform_matrix(0, m_p_camera->get_world_to_view_matrix());
glutil::uniform_vec3(4, m_p_camera->get_position());
glutil::uniform_uint(5, (unsigned int)m_lights.size());
glutil::uniform_lights(6, m_lights);
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
{ {
iter->render(); // TODO: move to glutil
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
}
void Scene::unuse()
{
}
void Scene::render()
{
glutil::clear_screen();
m_shader_program.use();
glutil::uniform_matrix(0, m_p_camera->get_world_to_view_matrix());
glutil::uniform_vec3(4, m_p_camera->get_position());
glutil::uniform_uint(5, (unsigned int)m_lights.size());
glutil::uniform_lights(6, m_lights);
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
{
iter->render();
}
} }
} }
} }

View File

@ -3,7 +3,7 @@
#include <vector> #include <vector>
#include "AutoPrerenderingScene.h" #include "AutoPrerenderingScene.h"
#include "BuiltinTypes.h" #include "LitTypes.h"
#include "Camera.h" #include "Camera.h"
#include "Batched.h" #include "Batched.h"
#include "LitBatch.h" #include "LitBatch.h"
@ -13,41 +13,44 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
// A scene lit by the Phong Reflection Model (See https://en.wikipedia.org/wiki/Phong_reflection_model ) namespace lit
class LitScene : public AutoPrerenderingScene, public Batched<LitRenderable, LitBatch>
{ {
public: // A scene lit by the Phong Reflection Model (See https://en.wikipedia.org/wiki/Phong_reflection_model )
LitScene(Application& application) : AutoPrerenderingScene(application) {} class Scene : public AutoPrerenderingScene, public Batched<Renderable, Batch>
virtual ~LitScene() {}
void init() override;
void use() override;
void unuse() override;
void render() override;
protected:
void set_camera(const Camera* p_camera) { m_p_camera = p_camera; }
Light& add_light(
const Position& position,
const Light::Power& power,
const ColorRGB ambient,
const ColorRGB diffuse,
const ColorRGB specular,
const Light::Fade& fade
)
{ {
m_lights.emplace_back(position, power, ambient, diffuse, specular, fade); public:
return m_lights.back(); Scene(Application& application) : AutoPrerenderingScene(application) {}
} virtual ~Scene() {}
private: void init() override;
LitShaderProgram m_shader_program;
const Camera* m_p_camera = nullptr; void use() override;
std::vector<Light> m_lights;
}; void unuse() override;
void render() override;
protected:
void set_camera(const Camera* p_camera) { m_p_camera = p_camera; }
Light& add_light(
const Position& position,
const Light::Power& power,
const ColorRGB ambient,
const ColorRGB diffuse,
const ColorRGB specular,
const Light::Fade& fade
)
{
m_lights.emplace_back(position, power, ambient, diffuse, specular, fade);
return m_lights.back();
}
private:
ShaderProgram m_shader_program;
const Camera* m_p_camera = nullptr;
std::vector<Light> m_lights;
};
}
} }
} }

View File

@ -6,11 +6,14 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
// TODO: Add constatns for the uniform and vertex attribute locations (for all shader programs) namespace lit
class LitShaderProgram : public VertexFragmentShaderProgram
{ {
public: // TODO: Add constants for the uniform and vertex attribute locations (for all shader programs)
LitShaderProgram() : VertexFragmentShaderProgram(SHADER_PATH "LitVS.glsl", SHADER_PATH "LitFS.glsl") {} class ShaderProgram : public VertexFragmentShaderProgram
}; {
public:
ShaderProgram() : VertexFragmentShaderProgram(SHADER_PATH "LitVS.glsl", SHADER_PATH "LitFS.glsl") {}
};
}
} }
} }

View File

@ -4,32 +4,35 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
void LitShadowedBatch::setup_vao() namespace litshadowed
{ {
glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo); void Batch::setup_vao()
glEnableVertexAttribArray(0); {
glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo);
glEnableVertexAttribArray(2); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(LitVertex), (void*)(offsetof(LitVertex, position))); glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(LitVertex), (void*)(offsetof(LitVertex, normal))); glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(LitVertex), (void*)(offsetof(LitVertex, material))); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(offsetof(Vertex, position)));
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[0]); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(offsetof(Vertex, normal)));
glEnableVertexAttribArray(3); glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(offsetof(Vertex, material)));
glEnableVertexAttribArray(4); glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[0]);
glEnableVertexAttribArray(5); glEnableVertexAttribArray(3);
glEnableVertexAttribArray(6); glEnableVertexAttribArray(4);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(0 * sizeof(vec4))); glEnableVertexAttribArray(5);
glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(1 * sizeof(vec4))); glEnableVertexAttribArray(6);
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(2 * sizeof(vec4))); glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(0 * sizeof(vec4)));
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(3 * sizeof(vec4))); glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(1 * sizeof(vec4)));
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(2 * sizeof(vec4)));
glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(3 * sizeof(vec4)));
glVertexAttribDivisor(0, 0); // Send the mesh data once glVertexAttribDivisor(0, 0); // Send the mesh data once
glVertexAttribDivisor(1, 0); // Send the mesh data once glVertexAttribDivisor(1, 0); // Send the mesh data once
glVertexAttribDivisor(2, 0); // Send the mesh data once glVertexAttribDivisor(2, 0); // Send the mesh data once
glVertexAttribDivisor(3, 1); // Send the offset data for each instance drawn glVertexAttribDivisor(3, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(4, 1); // Send the offset data for each instance drawn glVertexAttribDivisor(4, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(5, 1); // Send the offset data for each instance drawn glVertexAttribDivisor(5, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(6, 1); // Send the offset data for each instance drawn glVertexAttribDivisor(6, 1); // Send the offset data for each instance drawn
}
} }
} }
} }

View File

@ -1,30 +1,33 @@
#pragma once #pragma once
#include "PoseableBatch.h" #include "PoseableBatch.h"
#include "BuiltinTypes.h" #include "LitShadowedTypes.h"
namespace charcoal namespace charcoal
{ {
namespace builtin namespace builtin
{ {
class LitShadowedBatch : public PoseableBatch<LitVertex, LitIndex, LitRenderable> namespace litshadowed
{ {
public: class Batch : public PoseableBatch<Vertex, Index, Renderable>
LitShadowedBatch( {
LitRenderable* renderable, public:
int element_count Batch(
) : PoseableBatch<LitVertex, LitIndex, LitRenderable>(renderable, element_count) Renderable* renderable,
{} int element_count
) : PoseableBatch<Vertex, Index, Renderable>(renderable, element_count)
{}
LitShadowedBatch( Batch(
LitRenderable* renderable, Renderable* renderable,
int element_count, int element_count,
int element_render_count int element_render_count
) : PoseableBatch<LitVertex, LitIndex, LitRenderable>(renderable, element_count, element_render_count) ) : PoseableBatch<Vertex, Index, Renderable>(renderable, element_count, element_render_count)
{} {}
protected: protected:
void setup_vao() override; void setup_vao() override;
}; };
}
} }
} }

View File

@ -10,39 +10,42 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
void LitShadowedScene::init() namespace litshadowed
{ {
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter) void Scene::init()
{ {
LitShadowedBatch& batch = *iter; for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
batch.init(); {
add_prerenderable(&batch); Batch& batch = *iter;
batch.init();
add_prerenderable(&batch);
}
} }
}
void LitShadowedScene::use() void Scene::use()
{
// TODO: move to glutil
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
}
void LitShadowedScene::unuse()
{
}
void LitShadowedScene::render()
{
glutil::clear_screen();
m_shader_program.use();
glutil::uniform_matrix(0, m_p_camera->get_world_to_view_matrix());
glutil::uniform_vec3(4, m_p_camera->get_position());
glutil::uniform_uint(5, (unsigned int)m_lights.size());
glutil::uniform_lights(6, m_lights);
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
{ {
iter->render(); // TODO: move to glutil
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
}
void Scene::unuse()
{
}
void Scene::render()
{
glutil::clear_screen();
m_shader_program.use();
glutil::uniform_matrix(0, m_p_camera->get_world_to_view_matrix());
glutil::uniform_vec3(4, m_p_camera->get_position());
glutil::uniform_uint(5, (unsigned int)m_lights.size());
glutil::uniform_lights(6, m_lights);
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
{
iter->render();
}
} }
} }
} }

View File

@ -3,7 +3,7 @@
#include <vector> #include <vector>
#include "AutoPrerenderingScene.h" #include "AutoPrerenderingScene.h"
#include "BuiltinTypes.h" #include "LitShadowedTypes.h"
#include "Camera.h" #include "Camera.h"
#include "Batched.h" #include "Batched.h"
#include "LitShadowedBatch.h" #include "LitShadowedBatch.h"
@ -13,41 +13,44 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
// A scene lit by the Phong Reflection Model (See https://en.wikipedia.org/wiki/Phong_reflection_model ) namespace litshadowed
class LitShadowedScene : public AutoPrerenderingScene, public Batched<LitShadowedRenderable, LitShadowedBatch>
{ {
public: // A scene lit by the Phong Reflection Model (See https://en.wikipedia.org/wiki/Phong_reflection_model )
LitShadowedScene(Application& application) : AutoPrerenderingScene(application) {} class Scene : public AutoPrerenderingScene, public Batched<Renderable, Batch>
virtual ~LitShadowedScene() {}
void init() override;
void use() override;
void unuse() override;
void render() override;
protected:
void set_camera(const Camera* p_camera) { m_p_camera = p_camera; }
Light& add_light(
const Position& position,
const Light::Power& power,
const ColorRGB ambient,
const ColorRGB diffuse,
const ColorRGB specular,
const Light::Fade& fade
)
{ {
m_lights.emplace_back(position, power, ambient, diffuse, specular, fade); public:
return m_lights.back(); Scene(Application& application) : AutoPrerenderingScene(application) {}
} virtual ~Scene() {}
private: void init() override;
LitShadowedShaderProgram m_shader_program;
const Camera* m_p_camera = nullptr; void use() override;
std::vector<Light> m_lights;
}; void unuse() override;
void render() override;
protected:
void set_camera(const Camera* p_camera) { m_p_camera = p_camera; }
Light& add_light(
const Position& position,
const Light::Power& power,
const ColorRGB ambient,
const ColorRGB diffuse,
const ColorRGB specular,
const Light::Fade& fade
)
{
m_lights.emplace_back(position, power, ambient, diffuse, specular, fade);
return m_lights.back();
}
private:
ShaderProgram m_shader_program;
const Camera* m_p_camera = nullptr;
std::vector<Light> m_lights;
};
}
} }
} }

View File

@ -6,11 +6,14 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
// TODO: Add constatns for the uniform and vertex attribute locations (for all shader programs) namespace litshadowed
class LitShadowedShaderProgram : public VertexFragmentShaderProgram
{ {
public: // TODO: Add constants for the uniform and vertex attribute locations (for all shader programs)
LitShadowedShaderProgram() : VertexFragmentShaderProgram(SHADER_PATH "LitShadowedVS.glsl", SHADER_PATH "LitShadowedFS.glsl") {} class ShaderProgram : public VertexFragmentShaderProgram
}; {
public:
ShaderProgram() : VertexFragmentShaderProgram(SHADER_PATH "LitShadowedVS.glsl", SHADER_PATH "LitShadowedFS.glsl") {}
};
}
} }
} }

View File

@ -0,0 +1,17 @@
#pragma once
#include "BuiltinTypes.h"
namespace charcoal
{
namespace builtin
{
namespace litshadowed
{
typedef PNMVertex Vertex;
typedef Index Index;
typedef RenderableT<Vertex, Index> Renderable;
typedef Light Light;
}
}
}

17
OpenGLEngine/LitTypes.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include "BuiltinTypes.h"
namespace charcoal
{
namespace builtin
{
namespace lit
{
typedef PNMVertex Vertex;
typedef Index Index;
typedef RenderableT<Vertex, Index> Renderable;
typedef Light Light;
}
}
}

View File

@ -28,7 +28,7 @@ public:
float a = 1.0f; float a = 1.0f;
}; };
typedef unsigned int Index; typedef unsigned int Index;
typedef Renderable<Vertex, Index> Renderable; typedef RenderableT<Vertex, Index> RenderableT;
typedef Mesh<Vertex, Index> Mesh; typedef Mesh<Vertex, Index> Mesh;
MyBasicShaderProgram() : VertexFragmentShaderProgram(SHADER_PATH "MyBasicVS.glsl", SHADER_PATH "MyBasicFS.glsl") {} MyBasicShaderProgram() : VertexFragmentShaderProgram(SHADER_PATH "MyBasicVS.glsl", SHADER_PATH "MyBasicFS.glsl") {}

View File

@ -12,12 +12,12 @@ class MyBatch : public charcoal::Batch<MySimpleShaderProgram::Vertex, MySimpleSh
{ {
public: public:
MyBatch( MyBatch(
const MySimpleShaderProgram::Renderable* renderable, const MySimpleShaderProgram::RenderableT* renderable,
int element_count int element_count
) : MyBatch(renderable, element_count, element_count) {} ) : MyBatch(renderable, element_count, element_count) {}
MyBatch( MyBatch(
const MySimpleShaderProgram::Renderable* renderable, const MySimpleShaderProgram::RenderableT* renderable,
int element_count, int element_count,
int element_render_count int element_render_count
) : Batch(renderable, element_render_count), m_color_elements(element_count), m_poseable_elements(element_count) {} ) : Batch(renderable, element_render_count), m_color_elements(element_count), m_poseable_elements(element_count) {}

View File

@ -5,8 +5,8 @@
#include "constants.h" #include "constants.h"
MyBuiltinCubeScene::MyBuiltinCubeScene(Application& application) MyBuiltinCubeScene::MyBuiltinCubeScene(Application& application)
: BasicScene(application), : basic::Scene(application),
m_shape(meshgenerator::gen_cube_p<BasicVertex, BasicIndex>(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f), DrawMode::DRAW_TRIANGLES), m_shape(meshgenerator::gen_cube_p<basic::Vertex, basic::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(add_batch(&m_shape, 1)) m_batch(add_batch(&m_shape, 1))
{ {

View File

@ -6,14 +6,14 @@
using namespace charcoal; using namespace charcoal;
using namespace charcoal::builtin; using namespace charcoal::builtin;
class MyBuiltinCubeScene : public BasicScene class MyBuiltinCubeScene : public basic::Scene
{ {
public: public:
MyBuiltinCubeScene(Application& application); MyBuiltinCubeScene(Application& application);
void update(float delta_time, clock_t clock) override; void update(float delta_time, clock_t clock) override;
private: private:
BasicRenderable m_shape; basic::Renderable m_shape;
builtin::Camera3D m_camera; builtin::Camera3D m_camera;
BasicBatch& m_batch; basic::Batch& m_batch;
}; };

View File

@ -5,10 +5,10 @@
#include "constants.h" #include "constants.h"
MyBuiltinLitScene::MyBuiltinLitScene(Application& application) MyBuiltinLitScene::MyBuiltinLitScene(Application& application)
: LitScene(application), : lit::Scene(application),
m_shape( m_shape(
meshgenerator::set_material<LitVertex, LitIndex>( meshgenerator::set_material<lit::Vertex, lit::Index>(
meshgenerator::gen_cube_pn<LitVertex, LitIndex>(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f), meshgenerator::gen_cube_pn<lit::Vertex, lit::Index>(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f),
Material(1.0f, 1.0f, 0.2f, 1.0f) Material(1.0f, 1.0f, 0.2f, 1.0f)
), DrawMode::DRAW_TRIANGLES ), DrawMode::DRAW_TRIANGLES
), ),

View File

@ -6,14 +6,15 @@
using namespace charcoal; using namespace charcoal;
using namespace charcoal::builtin; using namespace charcoal::builtin;
class MyBuiltinLitScene : public LitScene // TODO: Use the lit namespace in builtin
class MyBuiltinLitScene : public lit::Scene
{ {
public: public:
MyBuiltinLitScene(Application& application); MyBuiltinLitScene(Application& application);
void update(float delta_time, clock_t clock) override; void update(float delta_time, clock_t clock) override;
private: private:
LitRenderable m_shape; lit::Renderable m_shape;
builtin::Camera3D m_camera; builtin::Camera3D m_camera;
LitBatch& m_batch; lit::Batch& m_batch;
}; };

View File

@ -5,10 +5,10 @@
#include "constants.h" #include "constants.h"
MyBuiltinLitShadowedScene::MyBuiltinLitShadowedScene(Application& application) MyBuiltinLitShadowedScene::MyBuiltinLitShadowedScene(Application& application)
: LitShadowedScene(application), : litshadowed::Scene(application),
m_shape( m_shape(
meshgenerator::set_material<LitVertex, LitIndex>( meshgenerator::set_material<litshadowed::Vertex, litshadowed::Index>(
meshgenerator::gen_cube_pn<LitVertex, LitIndex>(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f), meshgenerator::gen_cube_pn<litshadowed::Vertex, litshadowed::Index>(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f),
Material(1.0f, 1.0f, 0.2f, 1.0f) Material(1.0f, 1.0f, 0.2f, 1.0f)
), DrawMode::DRAW_TRIANGLES ), DrawMode::DRAW_TRIANGLES
), ),

View File

@ -6,14 +6,14 @@
using namespace charcoal; using namespace charcoal;
using namespace charcoal::builtin; using namespace charcoal::builtin;
class MyBuiltinLitShadowedScene : public LitShadowedScene class MyBuiltinLitShadowedScene : public litshadowed::Scene
{ {
public: public:
MyBuiltinLitShadowedScene(Application& application); MyBuiltinLitShadowedScene(Application& application);
void update(float delta_time, clock_t clock) override; void update(float delta_time, clock_t clock) override;
private: private:
LitShadowedRenderable m_shape; litshadowed::Renderable m_shape;
builtin::Camera3D m_camera; builtin::Camera3D m_camera;
LitShadowedBatch& m_batch; litshadowed::Batch& m_batch;
}; };

View File

@ -6,9 +6,9 @@
#include "constants.h" #include "constants.h"
MyBuiltinTexturedScene::MyBuiltinTexturedScene(Application& application) MyBuiltinTexturedScene::MyBuiltinTexturedScene(Application& application)
: TexturedScene(application), : textured::Scene(application),
m_shape( m_shape(
meshgenerator::gen_cube_pt<TexturedVertex, TexturedIndex>(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f), meshgenerator::gen_cube_pt<textured::Vertex, textured::Index>(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f),
texturegenerator::gen_quick_cube_texture(), texturegenerator::gen_quick_cube_texture(),
texturegenerator::gen_quick_sampler(), texturegenerator::gen_quick_sampler(),
DrawMode::DRAW_TRIANGLES DrawMode::DRAW_TRIANGLES

View File

@ -6,14 +6,14 @@
using namespace charcoal; using namespace charcoal;
using namespace charcoal::builtin; using namespace charcoal::builtin;
class MyBuiltinTexturedScene : public TexturedScene class MyBuiltinTexturedScene : public textured::Scene
{ {
public: public:
MyBuiltinTexturedScene(Application& application); MyBuiltinTexturedScene(Application& application);
void update(float delta_time, clock_t clock) override; void update(float delta_time, clock_t clock) override;
private: private:
TexturedRenderable m_shape; textured::Renderable m_shape;
builtin::Camera3D m_camera; builtin::Camera3D m_camera;
TexturedBatch& m_batch; textured::Batch& m_batch;
}; };

View File

@ -29,7 +29,7 @@ public:
private: private:
MySimpleShaderProgram m_shader_program; MySimpleShaderProgram m_shader_program;
MySimpleShaderProgram::Renderable m_shape; MySimpleShaderProgram::RenderableT m_shape;
MyBatch m_batch; MyBatch m_batch;
Camera2D m_camera; Camera2D m_camera;
}; };

View File

@ -29,7 +29,7 @@ public:
private: private:
MySimpleShaderProgram m_shader_program; MySimpleShaderProgram m_shader_program;
MySimpleShaderProgram::Renderable m_shape; MySimpleShaderProgram::RenderableT m_shape;
MyBatch m_batch; MyBatch m_batch;
charcoal::Camera3D m_camera; charcoal::Camera3D m_camera;
}; };

View File

@ -29,7 +29,7 @@ public:
private: private:
MySimpleShaderProgram m_shader_program; MySimpleShaderProgram m_shader_program;
MySimpleShaderProgram::Renderable m_shape; MySimpleShaderProgram::RenderableT m_shape;
MyBatch m_batch; MyBatch m_batch;
charcoal::Camera3D m_camera; charcoal::Camera3D m_camera;
}; };

View File

@ -28,7 +28,7 @@ public:
float a = 1.0f; float a = 1.0f;
}; };
typedef unsigned int Index; typedef unsigned int Index;
typedef Renderable<Vertex, Index> Renderable; typedef RenderableT<Vertex, Index> RenderableT;
typedef Mesh<Vertex, Index> Mesh; typedef Mesh<Vertex, Index> Mesh;
MySimpleShaderProgram() : VertexFragmentShaderProgram(SHADER_PATH "MySimpleVS.glsl", SHADER_PATH "MySimpleFS.glsl") {} MySimpleShaderProgram() : VertexFragmentShaderProgram(SHADER_PATH "MySimpleVS.glsl", SHADER_PATH "MySimpleFS.glsl") {}

View File

@ -195,6 +195,7 @@
<ClInclude Include="BasicBatch.h" /> <ClInclude Include="BasicBatch.h" />
<ClInclude Include="BasicScene.h" /> <ClInclude Include="BasicScene.h" />
<ClInclude Include="BasicShaderProgram.h" /> <ClInclude Include="BasicShaderProgram.h" />
<ClInclude Include="BasicTypes.h" />
<ClInclude Include="Batch.h" /> <ClInclude Include="Batch.h" />
<ClInclude Include="Batched.h" /> <ClInclude Include="Batched.h" />
<ClInclude Include="BuiltinBatch.h" /> <ClInclude Include="BuiltinBatch.h" />
@ -213,6 +214,8 @@
<ClInclude Include="LitShadowedBatch.h" /> <ClInclude Include="LitShadowedBatch.h" />
<ClInclude Include="LitShadowedScene.h" /> <ClInclude Include="LitShadowedScene.h" />
<ClInclude Include="LitShadowedShaderProgram.h" /> <ClInclude Include="LitShadowedShaderProgram.h" />
<ClInclude Include="LitShadowedTypes.h" />
<ClInclude Include="LitTypes.h" />
<ClInclude Include="MyBuiltinCubeScene.h" /> <ClInclude Include="MyBuiltinCubeScene.h" />
<ClInclude Include="MyBuiltinLitScene.h" /> <ClInclude Include="MyBuiltinLitScene.h" />
<ClInclude Include="MyBuiltinLitShadowedScene.h" /> <ClInclude Include="MyBuiltinLitShadowedScene.h" />
@ -222,6 +225,7 @@
<ClInclude Include="Sampler.h" /> <ClInclude Include="Sampler.h" />
<ClInclude Include="Texture.h" /> <ClInclude Include="Texture.h" />
<ClInclude Include="TexturedBatch.h" /> <ClInclude Include="TexturedBatch.h" />
<ClInclude Include="TexturedTypes.h" />
<ClInclude Include="TextureFactory.h" /> <ClInclude Include="TextureFactory.h" />
<ClInclude Include="TextureGenerator.h" /> <ClInclude Include="TextureGenerator.h" />
<ClInclude Include="TextureRenderable.h" /> <ClInclude Include="TextureRenderable.h" />

View File

@ -371,6 +371,18 @@
<ClInclude Include="MyBuiltinLitShadowedScene.h"> <ClInclude Include="MyBuiltinLitShadowedScene.h">
<Filter>Header Files\Example\Application</Filter> <Filter>Header Files\Example\Application</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="BasicTypes.h">
<Filter>Header Files\Engine\builtin\Basic</Filter>
</ClInclude>
<ClInclude Include="LitTypes.h">
<Filter>Header Files\Engine\builtin\Lit</Filter>
</ClInclude>
<ClInclude Include="TexturedTypes.h">
<Filter>Header Files\Engine\builtin\Textured</Filter>
</ClInclude>
<ClInclude Include="LitShadowedTypes.h">
<Filter>Header Files\Engine\builtin\LitShadowed</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="MySimpleVS.glsl"> <None Include="MySimpleVS.glsl">

View File

@ -9,22 +9,22 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
template <typename VertexType, typename IndexType, typename Renderable = Renderable<VertexType, IndexType> > template <typename VertexType, typename IndexType, typename RenderableT = RenderableT<VertexType, IndexType> >
class PoseableBatch : public builtin::Batch<VertexType, IndexType, 1, Renderable> class PoseableBatch : public builtin::Batch<VertexType, IndexType, 1, RenderableT>
{ {
public: public:
// TODO: Figure out how to get rid of this typename garbage. If that it figured out, m_element_buffers should get fixed. // TODO: Figure out how to get rid of this typename garbage. If that it figured out, m_element_buffers should get fixed.
PoseableBatch( PoseableBatch(
Renderable* renderable, RenderableT* renderable,
int element_count int element_count
) : PoseableBatch(renderable, element_count, element_count) ) : PoseableBatch(renderable, element_count, element_count)
{} {}
PoseableBatch( PoseableBatch(
Renderable* renderable, RenderableT* renderable,
int element_count, int element_count,
int element_render_count int element_render_count
) : builtin::Batch<VertexType, IndexType, 1, Renderable>(renderable, element_render_count), m_pose_elements(element_count) ) : builtin::Batch<VertexType, IndexType, 1, RenderableT>(renderable, element_render_count), m_pose_elements(element_count)
{} {}
virtual ~PoseableBatch() {} virtual ~PoseableBatch() {}
@ -35,14 +35,14 @@ namespace charcoal
protected: protected:
void setup_element_buffers() void setup_element_buffers()
{ {
glBindBuffer(GL_ARRAY_BUFFER, charcoal::Batch<VertexType, IndexType, 1, Renderable>::m_element_buffers[0]); glBindBuffer(GL_ARRAY_BUFFER, charcoal::Batch<VertexType, IndexType, 1, RenderableT>::m_element_buffers[0]);
glBufferData(GL_ARRAY_BUFFER, m_pose_elements.size() * sizeof(Poseable), NULL, GL_STREAM_DRAW); glBufferData(GL_ARRAY_BUFFER, m_pose_elements.size() * sizeof(Poseable), NULL, GL_STREAM_DRAW);
} }
void update_element_buffers() void update_element_buffers()
{ {
// TODO: There are probably better ways to do this. Should check with the old engine to see what I did there. // TODO: There are probably better ways to do this. Should check with the old engine to see what I did there.
glBindBuffer(GL_ARRAY_BUFFER, charcoal::Batch<VertexType, IndexType, 1, Renderable>::m_element_buffers[0]); glBindBuffer(GL_ARRAY_BUFFER, charcoal::Batch<VertexType, IndexType, 1, RenderableT>::m_element_buffers[0]);
glBufferData(GL_ARRAY_BUFFER, m_pose_elements.size() * sizeof(Poseable), NULL, GL_STREAM_DRAW); glBufferData(GL_ARRAY_BUFFER, m_pose_elements.size() * sizeof(Poseable), NULL, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, m_pose_elements.size() * sizeof(Poseable), m_pose_elements.data()); glBufferSubData(GL_ARRAY_BUFFER, 0, m_pose_elements.size() * sizeof(Poseable), m_pose_elements.data());
} }

View File

@ -11,14 +11,14 @@
namespace charcoal namespace charcoal
{ {
template <typename VertexType, typename IndexType> template <typename VertexType, typename IndexType>
class Renderable class RenderableT
{ {
public: public:
typedef VertexType VertexType; typedef VertexType VertexType;
typedef IndexType IndexType; typedef IndexType IndexType;
typedef Mesh<VertexType, IndexType> MeshType; typedef Mesh<VertexType, IndexType> MeshType;
Renderable(const MeshType* mesh, const DrawMode& draw_mode) RenderableT(const MeshType* mesh, const DrawMode& draw_mode)
: m_p_mesh(mesh), m_draw_mode(draw_mode) : m_p_mesh(mesh), m_draw_mode(draw_mode)
{} {}

View File

@ -7,11 +7,11 @@
namespace charcoal namespace charcoal
{ {
template <typename VertexType, typename IndexType> template <typename VertexType, typename IndexType>
class TextureRenderable : public Renderable<VertexType, IndexType> class TextureRenderable : public RenderableT<VertexType, IndexType>
{ {
public: public:
TextureRenderable(const Renderable<VertexType, IndexType>::MeshType* mesh, Texture* texture, Sampler* sampler, const DrawMode& draw_mode) TextureRenderable(const RenderableT<VertexType, IndexType>::MeshType* mesh, Texture* texture, Sampler* sampler, const DrawMode& draw_mode)
: Renderable<VertexType, IndexType>(mesh, draw_mode), m_p_texture(texture), m_p_sampler(sampler) : RenderableT<VertexType, IndexType>(mesh, draw_mode), m_p_texture(texture), m_p_sampler(sampler)
{} {}
const Texture* get_texture() const { return m_p_texture; } const Texture* get_texture() const { return m_p_texture; }

View File

@ -4,36 +4,39 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
void TexturedBatch::preprender() const namespace textured
{ {
glActiveTexture(GL_TEXTURE0); void Batch::preprender() const
glBindTexture(GL_TEXTURE_2D, m_p_renderable->get_texture()->get_texture()); {
glBindSampler(0, m_p_renderable->get_sampler()->get_sampler()); glActiveTexture(GL_TEXTURE0);
} glBindTexture(GL_TEXTURE_2D, m_p_renderable->get_texture()->get_texture());
glBindSampler(0, m_p_renderable->get_sampler()->get_sampler());
}
void TexturedBatch::setup_vao() void Batch::setup_vao()
{ {
glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo); glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void*)offsetof(TexturedVertex, position)); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void*)offsetof(TexturedVertex, uv)); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, uv));
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[0]); glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[0]);
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3); glEnableVertexAttribArray(3);
glEnableVertexAttribArray(4); glEnableVertexAttribArray(4);
glEnableVertexAttribArray(5); glEnableVertexAttribArray(5);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(0 * sizeof(vec4))); glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(0 * sizeof(vec4)));
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(1 * sizeof(vec4))); glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(1 * sizeof(vec4)));
glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(2 * sizeof(vec4))); glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(2 * sizeof(vec4)));
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(3 * sizeof(vec4))); glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(3 * sizeof(vec4)));
glVertexAttribDivisor(0, 0); // Send the mesh data once glVertexAttribDivisor(0, 0); // Send the mesh data once
glVertexAttribDivisor(1, 0); // Send the mesh data once glVertexAttribDivisor(1, 0); // Send the mesh data once
glVertexAttribDivisor(2, 1); // Send the offset data for each instance drawn glVertexAttribDivisor(2, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(3, 1); // Send the offset data for each instance drawn glVertexAttribDivisor(3, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(4, 1); // Send the offset data for each instance drawn glVertexAttribDivisor(4, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(5, 1); // Send the offset data for each instance drawn glVertexAttribDivisor(5, 1); // Send the offset data for each instance drawn
}
} }
} }
} }

View File

@ -1,32 +1,35 @@
#pragma once #pragma once
#include "PoseableBatch.h" #include "PoseableBatch.h"
#include "BuiltinTypes.h" #include "TexturedTypes.h"
namespace charcoal namespace charcoal
{ {
namespace builtin namespace builtin
{ {
class TexturedBatch : public PoseableBatch<TexturedVertex, TexturedIndex, TexturedRenderable> namespace textured
{ {
public: class Batch : public PoseableBatch<Vertex, Index, Renderable>
TexturedBatch( {
TexturedRenderable* renderable, public:
int element_count Batch(
) : PoseableBatch<TexturedVertex, TexturedIndex, TexturedRenderable>(renderable, element_count) Renderable* renderable,
{} int element_count
) : PoseableBatch<Vertex, Index, Renderable>(renderable, element_count)
{}
TexturedBatch( Batch(
TexturedRenderable* renderable, Renderable* renderable,
int element_count, int element_count,
int element_render_count int element_render_count
) : PoseableBatch<TexturedVertex, TexturedIndex, TexturedRenderable>(renderable, element_count, element_render_count) ) : PoseableBatch<Vertex, Index, Renderable>(renderable, element_count, element_render_count)
{} {}
void preprender() const override; void preprender() const override;
protected: protected:
void setup_vao() override; void setup_vao() override;
}; };
}
} }
} }

View File

@ -9,37 +9,40 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
void TexturedScene::init() namespace textured
{ {
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter) void Scene::init()
{ {
TexturedBatch& batch = *iter; for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
batch.init(); {
add_prerenderable(&batch); Batch& batch = *iter;
batch.init();
add_prerenderable(&batch);
}
} }
}
void TexturedScene::use() void Scene::use()
{
// TODO: move to glutil
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
}
void TexturedScene::unuse()
{
}
void TexturedScene::render()
{
glutil::clear_screen();
m_shader_program.use();
glutil::uniform_matrix(0, m_p_camera->get_world_to_view_matrix());
glutil::uniform_int(4, 0); // The textured batch uses GL_TEXTURE0
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
{ {
iter->render(); // TODO: move to glutil
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
}
void Scene::unuse()
{
}
void Scene::render()
{
glutil::clear_screen();
m_shader_program.use();
glutil::uniform_matrix(0, m_p_camera->get_world_to_view_matrix());
glutil::uniform_int(4, 0); // The textured batch uses GL_TEXTURE0
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
{
iter->render();
}
} }
} }
} }

View File

@ -14,26 +14,29 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
class TexturedScene : public AutoPrerenderingScene, public Batched<TexturedRenderable, TexturedBatch> namespace textured
{ {
public: class Scene : public AutoPrerenderingScene, public Batched<Renderable, Batch>
TexturedScene(Application& application) : AutoPrerenderingScene(application) {} {
virtual ~TexturedScene() {} public:
Scene(Application& application) : AutoPrerenderingScene(application) {}
virtual ~Scene() {}
void init() override; void init() override;
void use() override; void use() override;
void unuse() override; void unuse() override;
void render() override; void render() override;
protected: protected:
void set_camera(const Camera* p_camera) { m_p_camera = p_camera; } void set_camera(const Camera* p_camera) { m_p_camera = p_camera; }
private: private:
TexturedShaderProgram m_shader_program; ShaderProgram m_shader_program;
const Camera* m_p_camera = nullptr; const Camera* m_p_camera = nullptr;
}; };
}
} }
} }

View File

@ -6,10 +6,13 @@ namespace charcoal
{ {
namespace builtin namespace builtin
{ {
class TexturedShaderProgram : public VertexFragmentShaderProgram namespace textured
{ {
public: class ShaderProgram : public VertexFragmentShaderProgram
TexturedShaderProgram() : VertexFragmentShaderProgram(SHADER_PATH "TexturedVS.glsl", SHADER_PATH "TexturedFS.glsl") {} {
}; public:
ShaderProgram() : VertexFragmentShaderProgram(SHADER_PATH "TexturedVS.glsl", SHADER_PATH "TexturedFS.glsl") {}
};
}
} }
} }

View File

@ -0,0 +1,17 @@
#pragma once
#include "TextureRenderable.h"
#include "BuiltinTypes.h"
namespace charcoal
{
namespace builtin
{
namespace textured
{
typedef PTVertex Vertex;
typedef Index Index;
typedef TextureRenderable<Vertex, Index> Renderable;
}
}
}