diff --git a/OpenGLEngine/Batched.h b/OpenGLEngine/Batched.h index 1a40f73..31f326e 100644 --- a/OpenGLEngine/Batched.h +++ b/OpenGLEngine/Batched.h @@ -1,5 +1,7 @@ #pragma once +#include + namespace charcoal { namespace builtin @@ -19,7 +21,9 @@ namespace charcoal return m_batches.back(); } - std::vector m_batches; + // Since the intention is to access the batches through the returned references, + // it is better to store them in a list. + std::list m_batches; }; } } \ No newline at end of file diff --git a/OpenGLEngine/MeshGenerator.h b/OpenGLEngine/MeshGenerator.h index 9bdc5e1..4c94fcb 100644 --- a/OpenGLEngine/MeshGenerator.h +++ b/OpenGLEngine/MeshGenerator.h @@ -29,17 +29,19 @@ namespace charcoal } template - Mesh* gen_plane_p(const DrawMode& draw_mode, float width, float depth) + Mesh* gen_plane_pn(const DrawMode& draw_mode, float width, float depth) { Mesh* mesh; - + float x = width / 2.0f; float z = depth / 2.0f; - Position pos0(-x, 0.0f, z); + Position pos0(-x, 0.0f, z); Position pos1(-x, 0.0f, -z); - Position pos2( x, 0.0f, z); - Position pos3( x, 0.0f, -z); + Position pos2(x, 0.0f, z); + Position pos3(x, 0.0f, -z); + + Normal normal(0.0f, 1.0f, 0.0f); switch (draw_mode) { @@ -49,6 +51,10 @@ namespace charcoal mesh->vertices[1].set_position(pos1); mesh->vertices[2].set_position(pos2); mesh->vertices[3].set_position(pos3); + mesh->vertices[0].set_normal(normal); + mesh->vertices[1].set_normal(normal); + mesh->vertices[2].set_normal(normal); + mesh->vertices[3].set_normal(normal); mesh->indices[0] = 0; mesh->indices[1] = 1; mesh->indices[2] = 2; @@ -60,6 +66,10 @@ namespace charcoal mesh->vertices[1].set_position(pos1); mesh->vertices[2].set_position(pos2); mesh->vertices[3].set_position(pos3); + mesh->vertices[0].set_normal(normal); + mesh->vertices[1].set_normal(normal); + mesh->vertices[2].set_normal(normal); + mesh->vertices[3].set_normal(normal); mesh->indices[0] = 0; mesh->indices[1] = 1; mesh->indices[2] = 3; @@ -72,6 +82,10 @@ namespace charcoal mesh->vertices[1].set_position(pos1); mesh->vertices[2].set_position(pos2); mesh->vertices[3].set_position(pos3); + mesh->vertices[0].set_normal(normal); + mesh->vertices[1].set_normal(normal); + mesh->vertices[2].set_normal(normal); + mesh->vertices[3].set_normal(normal); mesh->indices[0] = 0; mesh->indices[1] = 1; mesh->indices[2] = 3; @@ -83,6 +97,10 @@ namespace charcoal mesh->vertices[1].set_position(pos1); mesh->vertices[2].set_position(pos2); mesh->vertices[3].set_position(pos3); + mesh->vertices[0].set_normal(normal); + mesh->vertices[1].set_normal(normal); + mesh->vertices[2].set_normal(normal); + mesh->vertices[3].set_normal(normal); mesh->indices[0] = 0; mesh->indices[1] = 1; mesh->indices[2] = 1; @@ -93,12 +111,30 @@ namespace charcoal mesh->indices[7] = 0; break; case DrawMode::DRAW_TRIANGLE_STRIP: + mesh = MeshFactory::create_mesh(4, 4); + mesh->vertices[0].set_position(pos0); + mesh->vertices[1].set_position(pos1); + mesh->vertices[2].set_position(pos2); + mesh->vertices[3].set_position(pos3); + mesh->vertices[0].set_normal(normal); + mesh->vertices[1].set_normal(normal); + mesh->vertices[2].set_normal(normal); + mesh->vertices[3].set_normal(normal); + mesh->indices[0] = 1; + mesh->indices[1] = 3; + mesh->indices[2] = 0; + mesh->indices[3] = 2; + break; case DrawMode::DRAW_TRIANGLE_FAN: mesh = MeshFactory::create_mesh(4, 4); mesh->vertices[0].set_position(pos0); mesh->vertices[1].set_position(pos1); mesh->vertices[2].set_position(pos2); mesh->vertices[3].set_position(pos3); + mesh->vertices[0].set_normal(normal); + mesh->vertices[1].set_normal(normal); + mesh->vertices[2].set_normal(normal); + mesh->vertices[3].set_normal(normal); mesh->indices[0] = 0; mesh->indices[1] = 1; mesh->indices[2] = 3; @@ -110,11 +146,15 @@ namespace charcoal mesh->vertices[1].set_position(pos1); mesh->vertices[2].set_position(pos2); mesh->vertices[3].set_position(pos3); - mesh->indices[0] = 0; - mesh->indices[1] = 1; - mesh->indices[2] = 3; - mesh->indices[3] = 3; - mesh->indices[4] = 1; + mesh->vertices[0].set_normal(normal); + mesh->vertices[1].set_normal(normal); + mesh->vertices[2].set_normal(normal); + mesh->vertices[3].set_normal(normal); + mesh->indices[0] = 1; + mesh->indices[1] = 3; + mesh->indices[2] = 0; + mesh->indices[3] = 0; + mesh->indices[4] = 3; mesh->indices[5] = 2; break; case DrawMode::DRAW_LINE_STRIP_ADJACENCY: @@ -123,7 +163,7 @@ namespace charcoal case DrawMode::DRAW_TRIANGLES_ADJACENCY: case DrawMode::DRAW_PATCHES: default: - throw EXCEPTION("Unable to gen_plane_p for current draw mode: " + std::string(draw_mode)); + throw EXCEPTION("Unable to gen_plane_p for current draw mode: " + std::to_string(draw_mode)); } return mesh; diff --git a/OpenGLEngine/MyBuiltinLitShadowedScene.cpp b/OpenGLEngine/MyBuiltinLitShadowedScene.cpp index f585032..d5ea760 100644 --- a/OpenGLEngine/MyBuiltinLitShadowedScene.cpp +++ b/OpenGLEngine/MyBuiltinLitShadowedScene.cpp @@ -6,14 +6,21 @@ MyBuiltinLitShadowedScene::MyBuiltinLitShadowedScene(Application& application) : litshadowed::Scene(application), - m_shape( + m_cube( meshgenerator::set_material( meshgenerator::gen_cube_pn(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f), Material(1.0f, 1.0f, 0.2f, 1.0f) ), DrawMode::DRAW_TRIANGLES ), + m_plane( + meshgenerator::set_material( + meshgenerator::gen_plane_pn(DRAW_TRIANGLES, 4.0f, 4.0f), + Material(1.0f, 1.0f, 0.2f, 1.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)) + m_cube_batch(add_batch(&m_cube, 1)), + m_plane_batch(add_batch(&m_plane, 1)) { add_prerenderable(&m_camera); set_camera(&m_camera); @@ -45,7 +52,7 @@ void MyBuiltinLitShadowedScene::update(float delta_time, clock_t clock) radians = (float)TAU * c / intervals; { - Poseable& pose = m_batch.get_pose(0); + Poseable& pose = m_cube_batch.get_pose(0); pose.rotate(glm::normalize(vec3(1.0f, 1.0f, 0.0f)), (float)TAU_1_8 * delta_time); pose.update_position(vec3(3 * (float)cos(radians), 0.0f, 0.0f)); } diff --git a/OpenGLEngine/MyBuiltinLitShadowedScene.h b/OpenGLEngine/MyBuiltinLitShadowedScene.h index 32e2c04..fced45d 100644 --- a/OpenGLEngine/MyBuiltinLitShadowedScene.h +++ b/OpenGLEngine/MyBuiltinLitShadowedScene.h @@ -13,7 +13,9 @@ public: void update(float delta_time, clock_t clock) override; private: - litshadowed::Renderable m_shape; + litshadowed::Renderable m_cube; + litshadowed::Renderable m_plane; builtin::Camera3D m_camera; - litshadowed::Batch& m_batch; + litshadowed::Batch& m_cube_batch; + litshadowed::Batch& m_plane_batch; }; \ No newline at end of file