diff --git a/CharcoalBuiltin/CharcoalBuiltin.vcxproj b/CharcoalBuiltin/CharcoalBuiltin.vcxproj index 3d4cc9a..c1ba263 100644 --- a/CharcoalBuiltin/CharcoalBuiltin.vcxproj +++ b/CharcoalBuiltin/CharcoalBuiltin.vcxproj @@ -148,6 +148,8 @@ copy "$(ProjectDir)*.h" "$(SolutionDir)include\charcoal-builtin\" + + @@ -178,6 +180,11 @@ copy "$(ProjectDir)*.h" "$(SolutionDir)include\charcoal-builtin\" + + + + + diff --git a/CharcoalBuiltin/CharcoalBuiltin.vcxproj.filters b/CharcoalBuiltin/CharcoalBuiltin.vcxproj.filters index 21883e1..d6b066a 100644 --- a/CharcoalBuiltin/CharcoalBuiltin.vcxproj.filters +++ b/CharcoalBuiltin/CharcoalBuiltin.vcxproj.filters @@ -55,6 +55,12 @@ {5f931ae6-23b1-43df-a4d8-e7f134d755c8} + + {d1604903-a209-427a-a879-3278b9fe8088} + + + {eecad623-46fa-4c24-b93d-c8d77002608e} + @@ -93,6 +99,12 @@ Source Files\Scenes\Textured + + Source Files\Scenes\Specified + + + Source Files\Scenes\Specified + @@ -191,5 +203,20 @@ Header Files\Scenes\Lit + + Header Files\General + + + Header Files\General + + + Header Files\Scenes\Specified + + + Header Files\Scenes\Specified + + + Header Files\Scenes\Specified + \ No newline at end of file diff --git a/CharcoalBuiltin/Poseable2DBatch.h b/CharcoalBuiltin/Poseable2DBatch.h index 6970909..38b63ff 100644 --- a/CharcoalBuiltin/Poseable2DBatch.h +++ b/CharcoalBuiltin/Poseable2DBatch.h @@ -4,7 +4,7 @@ #include #include -#include "BuiltinBatch.h" +#include "PoseableBatch.h" namespace charcoal { @@ -13,43 +13,25 @@ namespace charcoal // This has to be made completely seperately in order to avoid the vtable that gets added if Poseable is abstracted between // 2D and 3D template > - class Poseable2DBatch : public builtin::Batch + class Poseable2DBatch : public PoseableBatch { public: - Poseable2DBatch( - RenderableT* renderable, - int element_count - ) : Poseable2DBatch(renderable, element_count, element_count) - {} + using PoseableBatch::PoseableBatch; - Poseable2DBatch( - RenderableT* renderable, - int element_count, - int element_render_count - ) : builtin::Batch(renderable, element_render_count), m_pose_elements(element_count) - {} - - virtual ~Poseable2DBatch() {} - - Poseable2D& get_pose(int index) { return m_pose_elements[index]; } - const Poseable2D& get_pose(int index) const { return m_pose_elements[index]; } - - protected: - void setup_element_buffers() + Poseable2D& get_pose(int index) { - glBindBuffer(GL_ARRAY_BUFFER, charcoal::Batch::m_element_buffers[0]); - glBufferData(GL_ARRAY_BUFFER, m_pose_elements.size() * sizeof(Poseable), NULL, GL_STREAM_DRAW); + Poseable& pose = PoseableBatch::get_pose(index); + Poseable* p_pose = &pose; + Poseable2D* p_pose_2d = reinterpret_cast(p_pose); // Can do this since both are just mat4 wrappers + return *p_pose_2d; } - - void update_element_buffers() + const Poseable2D& get_pose(int index) const { - // 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::m_element_buffers[0]); - 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()); + Poseable& pose = PoseableBatch::get_pose(index); + Poseable* p_pose = &pose; + Poseable2D* p_pose_2d = reinterpret_cast(p_pose); // Can do this since both are just mat4 wrappers + return *p_pose_2d; } - - std::vector m_pose_elements; }; } } \ No newline at end of file diff --git a/CharcoalBuiltin/SpecifiedBatch.cpp b/CharcoalBuiltin/SpecifiedBatch.cpp new file mode 100644 index 0000000..939f204 --- /dev/null +++ b/CharcoalBuiltin/SpecifiedBatch.cpp @@ -0,0 +1,32 @@ +#include "SpecifiedBatch.h" + +namespace charcoal +{ + namespace builtin + { + namespace specified + { + void Batch::setup_vao() + { + glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), NULL); + glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[0]); + glEnableVertexAttribArray(1); + glEnableVertexAttribArray(2); + glEnableVertexAttribArray(3); + glEnableVertexAttribArray(4); + glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(0 * 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(1, 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(4, 1); // Send the offset data for each instance drawn + } + } + } +} \ No newline at end of file diff --git a/CharcoalBuiltin/SpecifiedBatch.h b/CharcoalBuiltin/SpecifiedBatch.h new file mode 100644 index 0000000..91cc724 --- /dev/null +++ b/CharcoalBuiltin/SpecifiedBatch.h @@ -0,0 +1,19 @@ +#pragma once + +#include "SpecifiedPoseableBatch.h" +#include "SpecifiedTypes.h" + +namespace charcoal +{ + namespace builtin + { + namespace specified + { + class Batch : public SpecifiedPoseableBatch + { + protected: + void setup_vao() override; + }; + } + } +} \ No newline at end of file diff --git a/CharcoalBuiltin/SpecifiedPoseable.h b/CharcoalBuiltin/SpecifiedPoseable.h new file mode 100644 index 0000000..bb9a715 --- /dev/null +++ b/CharcoalBuiltin/SpecifiedPoseable.h @@ -0,0 +1,25 @@ +#pragma once + +#include + +namespace charcoal +{ + namespace builtin + { + template + class SpecifiedPoseable : public Poseable + { + public: + SpecifiedPoseable(SpecifiedPoseableBatchType& batch) : m_batch(batch) + {} + + void render() + { + m_batch.add_specified_poseable(this); + } + + private: + SpecifiedPoseableBatchType& m_batch; + }; + } +} \ No newline at end of file diff --git a/CharcoalBuiltin/SpecifiedPoseableBatch.h b/CharcoalBuiltin/SpecifiedPoseableBatch.h new file mode 100644 index 0000000..4b42307 --- /dev/null +++ b/CharcoalBuiltin/SpecifiedPoseableBatch.h @@ -0,0 +1,29 @@ +#pragma once + +#include "PoseableBatch.h" + +#include "SpecifiedPoseable.h" + +namespace charcoal +{ + namespace builtin + { + // Note: If anything is changed in this file, it must also be changed in Poseable2DBatch + template > + class SpecifiedPoseableBatch : public PoseableBatch + { + public: + using PoseableBatch::PoseableBatch; + + void reset() + { + m_element_render_count = 0; + } + + void add_specified_poseable(SpecifiedPoseable >* poseable) + { + PoseableBatch::m_pose_elements[m_element_render_count++].set_orientation_matrix(poseable->get_orientation_matrix()); + } + }; + } +} diff --git a/CharcoalBuiltin/SpecifiedScene.cpp b/CharcoalBuiltin/SpecifiedScene.cpp new file mode 100644 index 0000000..eafab8c --- /dev/null +++ b/CharcoalBuiltin/SpecifiedScene.cpp @@ -0,0 +1,47 @@ +#include "SpecifiedScene.h" + +#include + +#include "GLUtil.h" + +namespace charcoal +{ + namespace builtin + { + namespace specified + { + void Scene::init() + { + for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter) + { + Batch& batch = *iter; + batch.init(); + add_prerenderable(&batch); + } + } + + void Scene::use() + { + // 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, get_camera()->get_world_to_view_matrix()); + for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter) + { + iter->render(); + } + } + } + } +} \ No newline at end of file diff --git a/CharcoalBuiltin/SpecifiedScene.h b/CharcoalBuiltin/SpecifiedScene.h new file mode 100644 index 0000000..7ab96c4 --- /dev/null +++ b/CharcoalBuiltin/SpecifiedScene.h @@ -0,0 +1,34 @@ +#pragma once + +#include "SpecifiedTypes.h" +#include "SpecifiedBatch.h" + +#include "AutoPrerenderingScene.h" +#include "Batched.h" +#include "WithCamera.h" + +namespace charcoal +{ + namespace builtin + { + namespace specified + { + class Scene : public AutoPrerenderingScene, public Batched, public WithCamera + { + public: + Scene(Application& application) : AutoPrerenderingScene(application) {} + + void init() override; + + void use() override; + + void unuse() override; + + void render() override; + + private: + ShaderProgram m_shader_program; + }; + } + } +} \ No newline at end of file diff --git a/CharcoalBuiltin/SpecifiedTypes.h b/CharcoalBuiltin/SpecifiedTypes.h new file mode 100644 index 0000000..a26050c --- /dev/null +++ b/CharcoalBuiltin/SpecifiedTypes.h @@ -0,0 +1,23 @@ +#pragma once + +#include "BuiltinTypes.h" + +#include "BasicTypes.h" +#include "BasicShaderProgram.h" + +namespace charcoal +{ + namespace builtin + { + namespace specified + { + // Just use the basic types + + typedef basic::Vertex Vertex; + typedef basic::Index Index; + typedef basic::Renderable Renderable; + + typedef basic::ShaderProgram ShaderProgram; + } + } +} \ No newline at end of file diff --git a/Example/Example.vcxproj b/Example/Example.vcxproj index f10a66a..c6edfb5 100644 --- a/Example/Example.vcxproj +++ b/Example/Example.vcxproj @@ -31,6 +31,7 @@ + @@ -46,6 +47,7 @@ + 15.0 diff --git a/Example/Example.vcxproj.filters b/Example/Example.vcxproj.filters index 0b1d25a..312ff1d 100644 --- a/Example/Example.vcxproj.filters +++ b/Example/Example.vcxproj.filters @@ -51,6 +51,9 @@ Source Files + + Source Files + @@ -92,5 +95,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/Example/MySpecifiedScene.cpp b/Example/MySpecifiedScene.cpp new file mode 100644 index 0000000..9fbb296 --- /dev/null +++ b/Example/MySpecifiedScene.cpp @@ -0,0 +1,26 @@ +#include "MySpecifiedScene.h" + +#include + +#include + +MyBuiltinSpecifiedScene::MyBuiltinSpecifiedScene(Application& application) + : specified::Scene(application), + m_shape(meshgenerator::gen_cube_p(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f), 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(add_batch(&m_shape, 2)), + m_pose_a(m_batch), + m_pose_b(m_batch) +{ + add_prerenderable(&m_camera); + set_camera(&m_camera); + + m_pose_a.update_position(glm::vec3(-4.0f, 0.0f, 0.0f)); + m_pose_a.update_position(glm::vec3(4.0f, 0.0f, 0.0f)); +} + +void MyBuiltinSpecifiedScene::update(float delta_time, clock_t clock) +{ + m_batch.reset(); + m_pose_a.render(); +} \ No newline at end of file diff --git a/Example/MySpecifiedScene.h b/Example/MySpecifiedScene.h new file mode 100644 index 0000000..8c2b980 --- /dev/null +++ b/Example/MySpecifiedScene.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +#include + +using namespace charcoal; +using namespace charcoal::builtin; + +class MyBuiltinSpecifiedScene : public specified::Scene +{ +public: + MyBuiltinSpecifiedScene(Application& application); + + void update(float delta_time, clock_t clock) override; + +private: + basic::Renderable m_shape; + builtin::Camera3D m_camera; + specified::Batch& m_batch; + + SpecifiedPoseable m_pose_a; + SpecifiedPoseable m_pose_b; +}; \ No newline at end of file diff --git a/OpenGLEngine/Poseable.h b/OpenGLEngine/Poseable.h index 062efd4..775e058 100644 --- a/OpenGLEngine/Poseable.h +++ b/OpenGLEngine/Poseable.h @@ -26,6 +26,7 @@ namespace charcoal void translate(const vec3& translation); void rotate(const vec3& axis, float angle); + void set_orientation_matrix(const mat4& orientation_matrix) { m_orientation_matrix = orientation_matrix; } const mat4& get_orientation_matrix() const { return m_orientation_matrix; } protected: