diff --git a/OpenGLEngine/BasicBatch.cpp b/OpenGLEngine/BasicBatch.cpp deleted file mode 100644 index d87731d..0000000 --- a/OpenGLEngine/BasicBatch.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "BasicBatch.h" - -namespace charcoal -{ - namespace builtin - { - void BasicBatch::setup_element_buffers() - { - glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[0]); - glBufferData(GL_ARRAY_BUFFER, m_pose_elements.size() * sizeof(Poseable), NULL, GL_STREAM_DRAW); - } - - void BasicBatch::setup_vao() - { - glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(BasicVertex), 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 - } - - void BasicBatch::update_element_buffers() - { - // 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, 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()); - } - } -} \ No newline at end of file diff --git a/OpenGLEngine/BasicBatch.h b/OpenGLEngine/BasicBatch.h deleted file mode 100644 index a954527..0000000 --- a/OpenGLEngine/BasicBatch.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include - -#include "MeshTypes.h" -#include "BuiltinBatch.h" -#include "Poseable.h" - -namespace charcoal -{ - namespace builtin - { - // TODO: Consider namespacing basic - class BasicBatch : public Batch - { - public: - BasicBatch( - BasicRenderable* renderable, - const SizeType& element_count - ) : BasicBatch(renderable, element_count, element_count) {} - - BasicBatch( - BasicRenderable* renderable, - const SizeType& element_count, - const SizeType& element_render_count - ) : Batch(renderable, element_render_count), m_pose_elements(element_count) {} - - virtual ~BasicBatch() {} - - Poseable& get_pose(const SizeType& index) { return m_pose_elements[index]; } - const Poseable& get_pose(const SizeType& index) const { return m_pose_elements[index]; } - - protected: - void setup_element_buffers() override; - - void setup_vao() override; - - void update_element_buffers() override; - - private: - std::vector m_pose_elements; - }; - } -} \ No newline at end of file diff --git a/OpenGLEngine/BasicScene.cpp b/OpenGLEngine/BasicScene.cpp index 0c3525c..31d56d1 100644 --- a/OpenGLEngine/BasicScene.cpp +++ b/OpenGLEngine/BasicScene.cpp @@ -41,16 +41,5 @@ namespace charcoal 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 7b27999..4a91590 100644 --- a/OpenGLEngine/BasicScene.h +++ b/OpenGLEngine/BasicScene.h @@ -5,8 +5,9 @@ #include "AutoPrerenderingScene.h" #include "BasicShaderProgram.h" -#include "BasicBatch.h" +#include "BuiltinTypes.h" #include "Camera2D.h" +#include "Batched.h" #include "constants.h" @@ -14,7 +15,7 @@ namespace charcoal { namespace builtin { - class BasicScene : public AutoPrerenderingScene + class BasicScene : public AutoPrerenderingScene, public Batched { public: BasicScene(Application& application) : AutoPrerenderingScene(application) {} @@ -31,13 +32,8 @@ namespace charcoal 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; - std::vector m_batches; const Camera* m_p_camera = nullptr; }; } diff --git a/OpenGLEngine/Batch.h b/OpenGLEngine/Batch.h index e6454be..4da3aef 100644 --- a/OpenGLEngine/Batch.h +++ b/OpenGLEngine/Batch.h @@ -13,11 +13,9 @@ namespace charcoal class Batch { public: - typedef GLsizei SizeType; - Batch( const Renderable* renderable, - const SizeType& element_render_count + int element_render_count ) : m_p_renderable(renderable), m_element_render_count(element_render_count), m_element_buffers(element_buffer_count) { @@ -91,14 +89,14 @@ namespace charcoal glBindVertexArray(NULL); } - void set_element_render_count(const SizeType& element_render_count) + void set_element_render_count(int element_render_count) { m_element_render_count = element_render_count; } const Renderable* get_renderable() const { return m_p_renderable; } - SizeType get_element_render_count() const { return m_element_render_count; } + int get_element_render_count() const { return m_element_render_count; } protected: virtual void setup_element_buffers() {} @@ -108,7 +106,7 @@ namespace charcoal virtual void update_element_buffers() {} const Renderable* m_p_renderable; - SizeType m_element_render_count; + int m_element_render_count; GLuint m_vertex_vbo; std::vector m_element_buffers; diff --git a/OpenGLEngine/Batched.h b/OpenGLEngine/Batched.h new file mode 100644 index 0000000..1a40f73 --- /dev/null +++ b/OpenGLEngine/Batched.h @@ -0,0 +1,25 @@ +#pragma once + +namespace charcoal +{ + namespace builtin + { + template + class Batched + { + protected: + BatchType& add_batch(RenderableType* renderable, int element_count) + { + return add_batch(renderable, element_count, element_count); + } + + BatchType& add_batch(RenderableType* renderable, int element_count, int element_render_count) + { + m_batches.emplace_back(renderable, element_count, element_render_count); + return m_batches.back(); + } + + std::vector m_batches; + }; + } +} \ No newline at end of file diff --git a/OpenGLEngine/BuiltinBatch.h b/OpenGLEngine/BuiltinBatch.h index a77cbe2..d9cc323 100644 --- a/OpenGLEngine/BuiltinBatch.h +++ b/OpenGLEngine/BuiltinBatch.h @@ -13,7 +13,7 @@ namespace charcoal public: Batch( const Renderable* renderable, - const charcoal::Batch::SizeType& element_render_count + int element_render_count ) : charcoal::Batch(renderable, element_render_count) {} void prerender() override { charcoal::Batch::prerender(); } diff --git a/OpenGLEngine/BuiltinTypes.h b/OpenGLEngine/BuiltinTypes.h new file mode 100644 index 0000000..410b3b7 --- /dev/null +++ b/OpenGLEngine/BuiltinTypes.h @@ -0,0 +1,61 @@ +#pragma once + +#include +#include "Renderable.h" +#include "PoseableBatch.h" +#include "VertexFragmentShaderProgram.h" + +namespace charcoal +{ + namespace builtin + { + using namespace glm; + + typedef vec3 Position; + typedef vec3 Normal; + typedef vec4 ColorRGBA; + typedef vec3 ColorRGB; + typedef vec2 UV; + + typedef unsigned int Index; + + // Generic Vertices + + struct PVertex + { + void set_position(const Position& position) { this->position = position; } + + Position position; + }; + + struct PNVertex + { + void set_position(const Position& position) { this->position = position; } + void set_normal(const Normal& normal) { this->normal = normal; } + + Position position; + Normal normal; + }; + + // Other Data Types + + struct Light + { + Light(const ColorRGB& ambient) : ambient(ambient) {} + + ColorRGB ambient; + }; + + // typedefs for builtin types + + typedef PVertex BasicVertex; + typedef Index BasicIndex; + typedef Renderable BasicRenderable; + typedef PoseableBatch BasicBatch; + + typedef PNVertex LitVertex; + typedef Index LitIndex; + typedef Renderable LitRenderable; + typedef PoseableBatch LitBatch; + } +} \ No newline at end of file diff --git a/OpenGLEngine/GLUtil.cpp b/OpenGLEngine/GLUtil.cpp index 8babb44..3bd3db4 100644 --- a/OpenGLEngine/GLUtil.cpp +++ b/OpenGLEngine/GLUtil.cpp @@ -13,10 +13,31 @@ namespace charcoal glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } + void uniform_int(int uniform_index, int value) + { + glUniform1i(uniform_index, value); + } + + void uniform_uint(int uniform_index, unsigned int value) + { + glUniform1ui(uniform_index, value); + } + void uniform_matrix(int uniform_index, const mat4& matrix, bool transpose) { glUniformMatrix4fv(uniform_index, 1, transpose ? GL_TRUE : GL_FALSE, &matrix[0][0]); } + + void uniform_lights(int uniform_index, const std::vector& lights) + { + const int ambient_size = 1; + int current_location = uniform_index; + for (std::vector::size_type i = 0; i < lights.size(); ++i) + { + glUniform3fv(current_location, 1, &lights[i].ambient[0]); + current_location += ambient_size; + } + } } } } \ No newline at end of file diff --git a/OpenGLEngine/GLUtil.h b/OpenGLEngine/GLUtil.h index d9eacbc..c43c918 100644 --- a/OpenGLEngine/GLUtil.h +++ b/OpenGLEngine/GLUtil.h @@ -1,6 +1,9 @@ #pragma once #include +#include + +#include "BuiltinTypes.h" namespace charcoal { @@ -11,7 +14,10 @@ namespace charcoal using namespace glm; void clear_screen(); + void uniform_int(int uniform_index, int value); + void uniform_uint(int uniform_index, unsigned int 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 } } } \ No newline at end of file diff --git a/OpenGLEngine/LitFS.glsl b/OpenGLEngine/LitFS.glsl new file mode 100644 index 0000000..2ea7300 --- /dev/null +++ b/OpenGLEngine/LitFS.glsl @@ -0,0 +1,20 @@ +#version 430 + +struct Light +{ + vec3 ambient; +}; + +#define MAX_LIGHTS 16 +layout(location = 4) uniform Light lights[MAX_LIGHTS]; +layout(location = 20) uniform uint num_lights; + +out vec4 frag_color; +void main() +{ + vec3 accum = vec3(0.0); + for (uint i = 0; i < MAX_LIGHTS && i < num_lights; ++i) { + accum = accum + lights[i].ambient; + } + frag_color = vec4(accum, 1.0); +} \ No newline at end of file diff --git a/OpenGLEngine/LitScene.cpp b/OpenGLEngine/LitScene.cpp new file mode 100644 index 0000000..c3ad07a --- /dev/null +++ b/OpenGLEngine/LitScene.cpp @@ -0,0 +1,53 @@ +#include "LitScene.h" + +#include "stdafx.h" + +#include "Util.h" +#include "GLUtil.h" +#include "MeshFactory.h" + +namespace charcoal +{ + namespace builtin + { + void LitScene::init() + { + for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter) + { + LitBatch& batch = *iter; + batch.init(); + add_prerenderable(&batch); + } + } + + void LitScene::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(); + CHECK_GL_ERR(); + glutil::uniform_matrix(0, m_p_camera->get_world_to_view_matrix()); + CHECK_GL_ERR(); + glutil::uniform_lights(4, m_lights); + CHECK_GL_ERR(); + glutil::uniform_uint(20, m_lights.size()); + CHECK_GL_ERR(); + for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter) + { + iter->render(); + } + CHECK_GL_ERR(); + } + } +} \ No newline at end of file diff --git a/OpenGLEngine/LitScene.h b/OpenGLEngine/LitScene.h new file mode 100644 index 0000000..8059aad --- /dev/null +++ b/OpenGLEngine/LitScene.h @@ -0,0 +1,40 @@ +#pragma once + +#include + +#include "AutoPrerenderingScene.h" +#include "BuiltinTypes.h" +#include "Camera.h" +#include "Batched.h" +#include "LitShaderProgram.h" + +namespace charcoal +{ + namespace builtin + { + class LitScene : public AutoPrerenderingScene, public Batched + { + public: + LitScene(Application& application) : AutoPrerenderingScene(application) {} + 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; } + + void add_light(const ColorRGB& ambient) { m_lights.emplace_back(ambient); } + + private: + LitShaderProgram m_shader_program; + const Camera* m_p_camera = nullptr; + std::vector m_lights; + }; + } +} \ No newline at end of file diff --git a/OpenGLEngine/LitShaderProgram.h b/OpenGLEngine/LitShaderProgram.h new file mode 100644 index 0000000..12d207c --- /dev/null +++ b/OpenGLEngine/LitShaderProgram.h @@ -0,0 +1,16 @@ +#pragma once + +#include "VertexFragmentShaderProgram.h" + +namespace charcoal +{ + namespace builtin + { + // TODO: Add constatns for the uniform and vertex attribute locations (for all shader programs) + class LitShaderProgram : public VertexFragmentShaderProgram + { + public: + LitShaderProgram() : VertexFragmentShaderProgram(SHADER_PATH "LitVS.glsl", SHADER_PATH "LitFS.glsl") {} + }; + } +} diff --git a/OpenGLEngine/LitVS.glsl b/OpenGLEngine/LitVS.glsl new file mode 100644 index 0000000..70f1cdf --- /dev/null +++ b/OpenGLEngine/LitVS.glsl @@ -0,0 +1,10 @@ +#version 430 +layout(location = 0) in vec3 vertex_position; +layout(location = 1) in mat4 model_to_world; + +layout(location = 0) uniform mat4 world_to_projection; + +void main() +{ + gl_Position = world_to_projection * model_to_world * vec4(vertex_position, 1.0); +} \ No newline at end of file diff --git a/OpenGLEngine/MeshGenerator.h b/OpenGLEngine/MeshGenerator.h index abadceb..bf7f2b9 100644 --- a/OpenGLEngine/MeshGenerator.h +++ b/OpenGLEngine/MeshGenerator.h @@ -3,11 +3,8 @@ #include #include "MeshFactory.h" - #include "Mesh.h" -#include "MeshTypes.h" - // TODO: Consider a mesh generator for every render type (i.e. basic::meshgenerator, lit::meshgenerator, etc.) namespace charcoal diff --git a/OpenGLEngine/MeshTypes.h b/OpenGLEngine/MeshTypes.h deleted file mode 100644 index b75cd33..0000000 --- a/OpenGLEngine/MeshTypes.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include -#include "Renderable.h" - -namespace charcoal -{ - namespace builtin - { - using namespace glm; - - typedef vec3 Position; - typedef vec3 Normal; - typedef vec4 Color; - typedef vec2 UV; - - typedef unsigned int Index; - - // Simple types that implement the interfaces - - struct BasicVertex - { - BasicVertex() : BasicVertex({ 0.0f, 0.0f, 0.0f }) {} - BasicVertex(const Position& position) : position(position) {} - - void set_position(const Position& position) { this->position = position; } - - Position position; - }; - - typedef Index BasicIndex; - - typedef Renderable BasicRenderable; - } -} \ No newline at end of file diff --git a/OpenGLEngine/MyApplication.cpp b/OpenGLEngine/MyApplication.cpp index c3cc5b2..89a409f 100644 --- a/OpenGLEngine/MyApplication.cpp +++ b/OpenGLEngine/MyApplication.cpp @@ -6,7 +6,8 @@ MyApplication::MyApplication(int width, int height) m_simple_2d_scene(*this), m_simple_3d_scene(*this), m_simple_cube_scene(*this), - m_builtin_basic_cube_scene(*this) + m_builtin_basic_cube_scene(*this), + m_builtin_lit_scene(*this) {} void MyApplication::init() @@ -16,6 +17,7 @@ void MyApplication::init() m_simple_3d_scene.init(); m_simple_cube_scene.init(); m_builtin_basic_cube_scene.init(); + m_builtin_lit_scene.init(); m_p_current_scene = &m_basic_scene; m_p_current_scene->use(); @@ -43,6 +45,10 @@ void MyApplication::update(float delta_time, clock_t clock) { swap_scene(&m_builtin_basic_cube_scene); } + else if (m_glfw_input_manager.is_key_pressed(GLFW_KEY_6)) + { + swap_scene(&m_builtin_lit_scene); + } m_p_current_scene->update(delta_time, clock); } diff --git a/OpenGLEngine/MyApplication.h b/OpenGLEngine/MyApplication.h index c54de7e..b7ad209 100644 --- a/OpenGLEngine/MyApplication.h +++ b/OpenGLEngine/MyApplication.h @@ -6,6 +6,7 @@ #include "MySimple3DScene.h" #include "MySimpleCubeScene.h" #include "MyBuiltinCubeScene.h" +#include "MyBuiltinLitScene.h" using namespace charcoal; @@ -35,5 +36,6 @@ private: MySimple3DScene m_simple_3d_scene; MySimpleCubeScene m_simple_cube_scene; MyBuiltinCubeScene m_builtin_basic_cube_scene; + MyBuiltinLitScene m_builtin_lit_scene; }; diff --git a/OpenGLEngine/MyBatch.h b/OpenGLEngine/MyBatch.h index f9786c0..9c3cf96 100644 --- a/OpenGLEngine/MyBatch.h +++ b/OpenGLEngine/MyBatch.h @@ -8,23 +8,23 @@ using namespace charcoal; -class MyBatch : public Batch +class MyBatch : public charcoal::Batch { public: MyBatch( const MySimpleShaderProgram::Renderable* renderable, - const SizeType& element_count + int element_count ) : MyBatch(renderable, element_count, element_count) {} MyBatch( const MySimpleShaderProgram::Renderable* renderable, - const SizeType& element_count, - const SizeType& element_render_count + int element_count, + int element_render_count ) : Batch(renderable, element_render_count), m_color_elements(element_count), m_poseable_elements(element_count) {} - MySimpleShaderProgram::Color& get_color(const SizeType& index) { return m_color_elements[index]; } + MySimpleShaderProgram::Color& get_color(int index) { return m_color_elements[index]; } - Poseable& get_pose(const SizeType& index) { return m_poseable_elements[index]; } + Poseable& get_pose(int index) { return m_poseable_elements[index]; } protected: void setup_element_buffers() override; diff --git a/OpenGLEngine/MyBuiltinCubeScene.cpp b/OpenGLEngine/MyBuiltinCubeScene.cpp index 276b6f3..14b9e09 100644 --- a/OpenGLEngine/MyBuiltinCubeScene.cpp +++ b/OpenGLEngine/MyBuiltinCubeScene.cpp @@ -2,6 +2,8 @@ #include "MeshGenerator.h" +#include "constants.h" + MyBuiltinCubeScene::MyBuiltinCubeScene(Application& application) : BasicScene(application), m_shape(meshgenerator::gen_cube_p(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f), DrawMode::DRAW_TRIANGLES), diff --git a/OpenGLEngine/MyBuiltinLitScene.cpp b/OpenGLEngine/MyBuiltinLitScene.cpp new file mode 100644 index 0000000..cc665a7 --- /dev/null +++ b/OpenGLEngine/MyBuiltinLitScene.cpp @@ -0,0 +1,58 @@ +#include "MyBuiltinLitScene.h" + +#include "MeshGenerator.h" + +#include "constants.h" + +MyBuiltinLitScene::MyBuiltinLitScene(Application& application) + : LitScene(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(add_batch(&m_shape, 1)) +{ + add_prerenderable(&m_camera); + set_camera(&m_camera); + + add_light(ColorRGB(0.0f, 0.0f, 1.0f)); + add_light(ColorRGB(0.0f, 1.0f, 0.0f)); +} + +void MyBuiltinLitScene::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.rotate(glm::normalize(vec3(1.0f, 1.0f, 0.0f)), (float)TAU_1_2 * delta_time); + pose.update_position(vec3(3 * (float)cos(radians), 0.0f, 0.0f)); + } + + vec3 camera_translation(0.0f, 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; + if (m_input_manager.is_key_down(GLFW_KEY_Q)) camera_translation.z -= 1; + if (m_input_manager.is_key_down(GLFW_KEY_E)) camera_translation.z += 1; + + float camera_rotation = 0.0f; + if (m_input_manager.is_key_down(GLFW_KEY_Z)) camera_rotation += 1; + if (m_input_manager.is_key_down(GLFW_KEY_C)) camera_rotation -= 1; + + m_camera.translate(camera_translation * delta_time); + m_camera.rotate(vec3(0.0f, 1.0f, 0.0f), camera_rotation * (float)TAU_1_8 * delta_time); +} + diff --git a/OpenGLEngine/MyBuiltinLitScene.h b/OpenGLEngine/MyBuiltinLitScene.h new file mode 100644 index 0000000..6ca074e --- /dev/null +++ b/OpenGLEngine/MyBuiltinLitScene.h @@ -0,0 +1,19 @@ +#pragma once + +#include "LitScene.h" +#include "BuiltinCamera3D.h" + +using namespace charcoal; +using namespace charcoal::builtin; + +class MyBuiltinLitScene : public LitScene +{ +public: + MyBuiltinLitScene(Application& application); + + void update(float delta_time, clock_t clock) override; +private: + LitRenderable m_shape; + builtin::Camera3D m_camera; + LitBatch& m_batch; +}; \ No newline at end of file diff --git a/OpenGLEngine/OpenGLEngine.vcxproj b/OpenGLEngine/OpenGLEngine.vcxproj index 604876e..35ae1b2 100644 --- a/OpenGLEngine/OpenGLEngine.vcxproj +++ b/OpenGLEngine/OpenGLEngine.vcxproj @@ -153,17 +153,17 @@ - + - + @@ -178,16 +178,17 @@ NotUsing + - - + + @@ -196,7 +197,11 @@ + + + + @@ -220,7 +225,7 @@ - + @@ -229,8 +234,8 @@ - - + + diff --git a/OpenGLEngine/OpenGLEngine.vcxproj.filters b/OpenGLEngine/OpenGLEngine.vcxproj.filters index 4a82b85..ae3961a 100644 --- a/OpenGLEngine/OpenGLEngine.vcxproj.filters +++ b/OpenGLEngine/OpenGLEngine.vcxproj.filters @@ -120,12 +120,6 @@ Source Files\Engine\builtin - - Source Files\Engine\Rendering - - - Source Files\Engine\builtin - Source Files\Engine\builtin @@ -135,6 +129,15 @@ Source Files\Engine\builtin + + Source Files\Engine\builtin + + + Source Files\Example\Application + + + Source Files\Engine\Rendering + @@ -224,9 +227,6 @@ Header Files\Engine\builtin - - Header Files\Engine\builtin - Header Files\Engine\builtin @@ -236,9 +236,6 @@ Header Files\Engine\Rendering - - Header Files\Engine\builtin - Header Files\Engine\builtin @@ -254,12 +251,30 @@ Header Files\Engine\builtin + + Header Files\Engine\builtin + + + Header Files\Engine\builtin + + + Header Files\Engine\builtin + + + Header Files\Engine\builtin + Header Files\Engine\builtin - + Header Files\Engine\builtin + + Header Files\Engine\builtin + + + Header Files\Example\Application + @@ -280,10 +295,10 @@ Source Files\Engine\builtin\Shaders - + Source Files\Engine\builtin\Shaders - + Source Files\Engine\builtin\Shaders diff --git a/OpenGLEngine/PassthroughFS.glsl b/OpenGLEngine/PassthroughFS.glsl deleted file mode 100644 index ce4c83b..0000000 --- a/OpenGLEngine/PassthroughFS.glsl +++ /dev/null @@ -1,2 +0,0 @@ -// TODO -// Could be same as BasicFS.glsl ... \ No newline at end of file diff --git a/OpenGLEngine/PassthroughVS.glsl b/OpenGLEngine/PassthroughVS.glsl deleted file mode 100644 index 0ffdd02..0000000 --- a/OpenGLEngine/PassthroughVS.glsl +++ /dev/null @@ -1 +0,0 @@ -// TODO \ No newline at end of file diff --git a/OpenGLEngine/PoseableBatch.h b/OpenGLEngine/PoseableBatch.h new file mode 100644 index 0000000..86bf62a --- /dev/null +++ b/OpenGLEngine/PoseableBatch.h @@ -0,0 +1,76 @@ +#pragma once + +#include "Batch.h" +#include "BuiltinBatch.h" +#include "Renderable.h" +#include "Poseable.h" + +namespace charcoal +{ + namespace builtin + { + template > + class PoseableBatch : public builtin::Batch + { + public: + // TODO: Figure out how to get rid of this typename garbage. If that it figured out, m_element_buffers should get fixed. + PoseableBatch( + Renderable* renderable, + int element_count + ) : PoseableBatch(renderable, element_count, element_count) + {} + + PoseableBatch( + Renderable* renderable, + int element_count, + int element_render_count + ) : builtin::Batch(renderable, element_render_count), m_pose_elements(element_count) + {} + + virtual ~PoseableBatch() {} + + Poseable& get_pose(int index) { return m_pose_elements[index]; } + const Poseable& get_pose(int index) const { return m_pose_elements[index]; } + + protected: + void setup_element_buffers() override + { + glBindBuffer(GL_ARRAY_BUFFER, charcoal::Batch::m_element_buffers[0]); + glBufferData(GL_ARRAY_BUFFER, m_pose_elements.size() * sizeof(Poseable), NULL, GL_STREAM_DRAW); + } + + void setup_vao() override + { + glBindBuffer(GL_ARRAY_BUFFER, charcoal::Batch::m_vertex_vbo); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(VertexType), NULL); + glBindBuffer(GL_ARRAY_BUFFER, charcoal::Batch::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 + } + + void update_element_buffers() override + { + // 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()); + } + + private: + std::vector m_pose_elements; + }; + } +} \ No newline at end of file