diff --git a/OpenGLEngine/AutoPrerenderingScene.cpp b/OpenGLEngine/AutoPrerenderingScene.cpp new file mode 100644 index 0000000..ebbff3b --- /dev/null +++ b/OpenGLEngine/AutoPrerenderingScene.cpp @@ -0,0 +1,20 @@ +#include "AutoPrerenderingScene.h" + +namespace charcoal +{ + namespace builtin + { + void AutoPrerenderingScene::prerender() + { + for (auto iter = m_prerenderables.begin(); iter != m_prerenderables.end(); ++iter) + { + (*iter)->prerender(); + } + } + + void AutoPrerenderingScene::add_prerenderable(Prerenderable* prerenderable) + { + m_prerenderables.push_back(prerenderable); + } + } +} \ No newline at end of file diff --git a/OpenGLEngine/AutoPrerenderingScene.h b/OpenGLEngine/AutoPrerenderingScene.h new file mode 100644 index 0000000..48d6b6f --- /dev/null +++ b/OpenGLEngine/AutoPrerenderingScene.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +#include "Application.h" +#include "Scene.h" +#include "Prerenderable.h" + +namespace charcoal +{ + namespace builtin + { + class AutoPrerenderingScene : public Scene + { + public: + AutoPrerenderingScene(Application& application) : Scene(application) {} + virtual ~AutoPrerenderingScene() {} + + void prerender() override; + + protected: + void add_prerenderable(Prerenderable* p_prerenderable); + + private: + std::vector m_prerenderables; + }; + } +} \ No newline at end of file diff --git a/OpenGLEngine/BasicBatch.h b/OpenGLEngine/BasicBatch.h index 817b480..a954527 100644 --- a/OpenGLEngine/BasicBatch.h +++ b/OpenGLEngine/BasicBatch.h @@ -3,7 +3,7 @@ #include #include "MeshTypes.h" -#include "Batch.h" +#include "BuiltinBatch.h" #include "Poseable.h" namespace charcoal diff --git a/OpenGLEngine/BasicScene.cpp b/OpenGLEngine/BasicScene.cpp index 3ebeb98..0c3525c 100644 --- a/OpenGLEngine/BasicScene.cpp +++ b/OpenGLEngine/BasicScene.cpp @@ -9,26 +9,14 @@ namespace charcoal { namespace builtin { - BasicScene::BasicScene(Application& application) - : Scene(application), - m_shape(MeshFactory::gen( - DrawMode::DRAW_TRIANGLES, - BasicVertex({ -50.0f, 50.0f, 0.0f }), - BasicVertex({ 50.0f, 150.0f, 0.0f }), - BasicVertex({ -100.0f, -50.0f, 0.0f }), - BasicVertex({ 100.0f, -50.0f, 0.0f }) - ), DrawMode::DRAW_TRIANGLES), - m_batch(&m_shape, 2), - m_camera(m_screen_size) - {} - - BasicScene::~BasicScene() {} - void BasicScene::init() { - sizeof(BasicVertex); - sizeof(vec3); - m_batch.init(); + for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter) + { + BasicBatch& batch = *iter; + batch.init(); + add_prerenderable(&batch); + } } void BasicScene::use() @@ -43,55 +31,26 @@ namespace charcoal } - void BasicScene::update(float delta_time, clock_t clock) - { - float brightness; - float radians; - - clock_t c; - const clock_t intervals = 512 * CLOCKS_PER_SEC / 100; - const clock_t half_interval = 256 * CLOCKS_PER_SEC / 100; - c = clock % intervals; - if (c < half_interval) - brightness = (float)c / half_interval; - else - brightness = (float)(intervals - c) / half_interval; - - radians = (float)TAU * c / intervals; - - { - Poseable& pose = m_batch.get_pose(0); - pose.update_position(vec3(cos(radians) * 50, 0.0f, 0.0f)); - } - - { - Poseable& pose = m_batch.get_pose(1); - pose.update_position(vec3(0.0f, sin(radians) * 50, 0.0f)); - } - - vec2 camera_translation(0.0f, 0.0f); - - if (m_input_manager.is_key_down(GLFW_KEY_W)) camera_translation.y += 1; - if (m_input_manager.is_key_down(GLFW_KEY_S)) camera_translation.y -= 1; - if (m_input_manager.is_key_down(GLFW_KEY_A)) camera_translation.x -= 1; - if (m_input_manager.is_key_down(GLFW_KEY_D)) camera_translation.x += 1; - - m_camera.translate(camera_translation * delta_time * 100.0f); - } - - void BasicScene::prerender() - { - m_camera.prerender(); - m_batch.prerender(); - } - void BasicScene::render() { - // TODO: This is not rendering :( glutil::clear_screen(); m_shader_program.use(); - glutil::uniform_matrix(0, m_camera.get_world_to_view_matrix()); - m_batch.render(); + 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(); + } + } + + BasicBatch& BasicScene::add_batch(BasicRenderable* renderable, int element_count) + { + return add_batch(renderable, element_count, element_count); + } + + BasicBatch& BasicScene::add_batch(BasicRenderable* renderable, int element_count, int element_render_count) + { + m_batches.emplace_back(renderable, element_count, element_render_count); + return m_batches.back(); } } } \ No newline at end of file diff --git a/OpenGLEngine/BasicScene.h b/OpenGLEngine/BasicScene.h index 7145ec9..7b27999 100644 --- a/OpenGLEngine/BasicScene.h +++ b/OpenGLEngine/BasicScene.h @@ -1,6 +1,8 @@ #pragma once -#include "Scene.h" +#include + +#include "AutoPrerenderingScene.h" #include "BasicShaderProgram.h" #include "BasicBatch.h" @@ -12,11 +14,11 @@ namespace charcoal { namespace builtin { - class BasicScene : public Scene + class BasicScene : public AutoPrerenderingScene { public: - BasicScene(Application& application); - ~BasicScene(); + BasicScene(Application& application) : AutoPrerenderingScene(application) {} + virtual ~BasicScene() {} void init() override; @@ -24,17 +26,19 @@ namespace charcoal void unuse() override; - void update(float delta_time, clock_t clock) override; - - void prerender() override; - void render() override; + protected: + void set_camera(const Camera* p_camera) { m_p_camera = p_camera; } + + BasicBatch& add_batch(BasicRenderable* renderable, int element_count); + + BasicBatch& add_batch(BasicRenderable* renderable, int element_count, int element_render_count); + private: BasicShaderProgram m_shader_program; - BasicRenderable m_shape; - BasicBatch m_batch; - Camera2D m_camera; + std::vector m_batches; + const Camera* m_p_camera = nullptr; }; } } \ No newline at end of file diff --git a/OpenGLEngine/BuiltinBatch.h b/OpenGLEngine/BuiltinBatch.h new file mode 100644 index 0000000..a77cbe2 --- /dev/null +++ b/OpenGLEngine/BuiltinBatch.h @@ -0,0 +1,22 @@ +#pragma once + +#include "Prerenderable.h" +#include "Batch.h" + +namespace charcoal +{ + namespace builtin + { + template > + class Batch : public Prerenderable, public charcoal::Batch + { + public: + Batch( + const Renderable* renderable, + const charcoal::Batch::SizeType& element_render_count + ) : charcoal::Batch(renderable, element_render_count) {} + + void prerender() override { charcoal::Batch::prerender(); } + }; + } +} \ No newline at end of file diff --git a/OpenGLEngine/BuiltinCamera2D.h b/OpenGLEngine/BuiltinCamera2D.h new file mode 100644 index 0000000..41ab0fa --- /dev/null +++ b/OpenGLEngine/BuiltinCamera2D.h @@ -0,0 +1,21 @@ +#pragma once + +#include "Prerenderable.h" +#include "Camera2D.h" + +namespace charcoal +{ + namespace builtin + { + class Camera2D : public Prerenderable, public charcoal::Camera2D + { + public: + Camera2D(const vec2& size, const vec2& position = vec2(0.0f, 0.0f)) + : charcoal::Camera2D(size, position) {} + Camera2D(const vec3& size = vec3(2.0f, 2.0f, 2.0f), const vec3& position = vec3(0.0f, 0.0f, 0.0f)) + : charcoal::Camera2D(size, position) {} + + void prerender() override { charcoal::Camera2D::prerender(); } + }; + } +} \ No newline at end of file diff --git a/OpenGLEngine/MeshTypes.h b/OpenGLEngine/MeshTypes.h index 523096d..b75cd33 100644 --- a/OpenGLEngine/MeshTypes.h +++ b/OpenGLEngine/MeshTypes.h @@ -31,25 +31,5 @@ namespace charcoal typedef Index BasicIndex; typedef Renderable BasicRenderable; - - struct PNVertex - { - void set_position(const Position& position) { this->position = position; } - void set_normal(const Normal& normal) { this->normal = normal; } - - Position position; - Normal normal; - }; - - struct PNTVertex - { - void set_position(const Position& position) { this->position = position; } - void set_normal(const Normal& normal) { this->normal = normal; } - void set_uv(const UV& uv) { this->uv = uv; } - - Position position; - Normal normal; - UV uv; - }; } } \ No newline at end of file diff --git a/OpenGLEngine/MyApplication.cpp b/OpenGLEngine/MyApplication.cpp index 10e9789..c3cc5b2 100644 --- a/OpenGLEngine/MyApplication.cpp +++ b/OpenGLEngine/MyApplication.cpp @@ -6,7 +6,7 @@ MyApplication::MyApplication(int width, int height) m_simple_2d_scene(*this), m_simple_3d_scene(*this), m_simple_cube_scene(*this), - m_builtin_basic_scene(*this) + m_builtin_basic_cube_scene(*this) {} void MyApplication::init() @@ -15,7 +15,7 @@ void MyApplication::init() m_simple_2d_scene.init(); m_simple_3d_scene.init(); m_simple_cube_scene.init(); - m_builtin_basic_scene.init(); + m_builtin_basic_cube_scene.init(); m_p_current_scene = &m_basic_scene; m_p_current_scene->use(); @@ -41,7 +41,7 @@ void MyApplication::update(float delta_time, clock_t clock) } else if (m_glfw_input_manager.is_key_pressed(GLFW_KEY_5)) { - swap_scene(&m_builtin_basic_scene); + swap_scene(&m_builtin_basic_cube_scene); } m_p_current_scene->update(delta_time, clock); } diff --git a/OpenGLEngine/MyApplication.h b/OpenGLEngine/MyApplication.h index 517ba87..c54de7e 100644 --- a/OpenGLEngine/MyApplication.h +++ b/OpenGLEngine/MyApplication.h @@ -5,7 +5,7 @@ #include "MySimple2DScene.h" #include "MySimple3DScene.h" #include "MySimpleCubeScene.h" -#include "BasicScene.h" +#include "MyBuiltinCubeScene.h" using namespace charcoal; @@ -34,6 +34,6 @@ private: MySimple2DScene m_simple_2d_scene; MySimple3DScene m_simple_3d_scene; MySimpleCubeScene m_simple_cube_scene; - builtin::BasicScene m_builtin_basic_scene; + MyBuiltinCubeScene m_builtin_basic_cube_scene; }; diff --git a/OpenGLEngine/MyBuiltinCubeScene.cpp b/OpenGLEngine/MyBuiltinCubeScene.cpp new file mode 100644 index 0000000..400f7f8 --- /dev/null +++ b/OpenGLEngine/MyBuiltinCubeScene.cpp @@ -0,0 +1,55 @@ +#include "MyBuiltinCubeScene.h" + +#include "MeshFactory.h" + +MyBuiltinCubeScene::MyBuiltinCubeScene(Application& application) + : BasicScene(application), + m_shape(MeshFactory::gen( + DrawMode::DRAW_TRIANGLES, + BasicVertex({ -50.0f, 50.0f, 0.0f }), + BasicVertex({ 50.0f, 150.0f, 0.0f }), + BasicVertex({ -100.0f, -50.0f, 0.0f }), + BasicVertex({ 100.0f, -50.0f, 0.0f }) + ), DrawMode::DRAW_TRIANGLES), + m_camera(m_screen_size), + m_batch(add_batch(&m_shape, 2)) +{ + set_camera(&m_camera); +} + +void MyBuiltinCubeScene::update(float delta_time, clock_t clock) +{ + float brightness; + float radians; + + clock_t c; + const clock_t intervals = 512 * CLOCKS_PER_SEC / 100; + const clock_t half_interval = 256 * CLOCKS_PER_SEC / 100; + c = clock % intervals; + if (c < half_interval) + brightness = (float)c / half_interval; + else + brightness = (float)(intervals - c) / half_interval; + + radians = (float)TAU * c / intervals; + + { + Poseable& pose = m_batch.get_pose(0); + pose.update_position(vec3(cos(radians) * 50, 0.0f, 0.0f)); + } + + { + Poseable& pose = m_batch.get_pose(1); + pose.update_position(vec3(0.0f, sin(radians) * 50, 0.0f)); + } + + vec2 camera_translation(0.0f, 0.0f); + + if (m_input_manager.is_key_down(GLFW_KEY_W)) camera_translation.y += 1; + if (m_input_manager.is_key_down(GLFW_KEY_S)) camera_translation.y -= 1; + if (m_input_manager.is_key_down(GLFW_KEY_A)) camera_translation.x -= 1; + if (m_input_manager.is_key_down(GLFW_KEY_D)) camera_translation.x += 1; + + m_camera.translate(camera_translation * delta_time * 100.0f); +} + diff --git a/OpenGLEngine/MyBuiltinCubeScene.h b/OpenGLEngine/MyBuiltinCubeScene.h new file mode 100644 index 0000000..504d7d4 --- /dev/null +++ b/OpenGLEngine/MyBuiltinCubeScene.h @@ -0,0 +1,19 @@ +#pragma once + +#include "BasicScene.h" +#include "Camera2D.h" + +using namespace charcoal; +using namespace charcoal::builtin; + +class MyBuiltinCubeScene : public BasicScene +{ +public: + MyBuiltinCubeScene(Application& application); + + void update(float delta_time, clock_t clock) override; +private: + BasicRenderable m_shape; + Camera2D m_camera; + BasicBatch m_batch; +}; \ No newline at end of file diff --git a/OpenGLEngine/OpenGLEngine.vcxproj b/OpenGLEngine/OpenGLEngine.vcxproj index 9b6a5bd..535b4a9 100644 --- a/OpenGLEngine/OpenGLEngine.vcxproj +++ b/OpenGLEngine/OpenGLEngine.vcxproj @@ -152,11 +152,13 @@ + + @@ -179,10 +181,13 @@ + + + @@ -190,6 +195,8 @@ + + diff --git a/OpenGLEngine/OpenGLEngine.vcxproj.filters b/OpenGLEngine/OpenGLEngine.vcxproj.filters index 8c87244..c0556b7 100644 --- a/OpenGLEngine/OpenGLEngine.vcxproj.filters +++ b/OpenGLEngine/OpenGLEngine.vcxproj.filters @@ -129,6 +129,12 @@ Source Files\Engine\builtin + + Source Files\Example\Application + + + Source Files\Engine\builtin + @@ -236,6 +242,21 @@ Header Files\Engine\builtin + + Header Files\Example\Application + + + Header Files\Engine\builtin + + + Header Files\Engine\builtin + + + Header Files\Engine\builtin + + + Header Files\Engine\builtin + diff --git a/OpenGLEngine/Prerenderable.h b/OpenGLEngine/Prerenderable.h new file mode 100644 index 0000000..c40c6d3 --- /dev/null +++ b/OpenGLEngine/Prerenderable.h @@ -0,0 +1,13 @@ +#pragma once + +namespace charcoal +{ + namespace builtin + { + class Prerenderable + { + public: + virtual void prerender() = 0; + }; + } +}