diff --git a/docs/tasks/editor.todo b/docs/tasks/editor.todo index 940caa5..c5e7710 100644 --- a/docs/tasks/editor.todo +++ b/docs/tasks/editor.todo @@ -38,6 +38,7 @@ Editor: ✔ Open existing project @done (2/8/2022, 4:05:42 PM) Content Manager: + ☐ Switch to using LUUIDs for asset ids @critical ✔ Design interface @done (2/24/2022, 3:15:39 PM) ✔ Generate new content file @done (2/24/2022, 3:16:00 PM) ✔ Load existing contents @done (3/3/2022, 3:16:21 PM) @@ -47,7 +48,8 @@ Editor: World View: ✔ Middle Mouse view dragging @done(22-09-08 15:45) ✔ Render the current world and display on view panel @done(22-09-08 15:45) - + ☐ Optional and adjustable grid display + ☐ Toolbar with play/pause/stop buttons World Hierarchy (Tree View): ☐ Handle showing Enities with children @high diff --git a/src/renderer/renderer2D.cpp b/src/renderer/renderer2D.cpp index 4d6a0fb..0051bd2 100644 --- a/src/renderer/renderer2D.cpp +++ b/src/renderer/renderer2D.cpp @@ -110,10 +110,10 @@ namespace lunarium // DRAW METHODS ///////////////////////////////////////////////////////////////////// - void Renderer2D::DrawSprite(Texture* texture, glm::vec2 position, Rectangle sub_texture_area, Color color, float angle) + void Renderer2D::DrawSprite(Texture* texture, glm::vec2 position, Rectangle sub_texture_area, Color color, float angle, glm::mat4 parent_transform) { Rectangle quad = Rectangle::MakeFromTopLeft(position.x, position.y, sub_texture_area.width(), sub_texture_area.height()); - DrawQuad(quad, color, texture, angle, sub_texture_area); + DrawQuad(quad, color, texture, angle, sub_texture_area, parent_transform); } // void Renderer2D::DrawSprite(Texture* texture, Rectangle destination, Rectangle sub_texture_area, Color color = Color::White(), float angle = 0.0f) @@ -121,7 +121,7 @@ namespace lunarium // DrawQuad(destination, color, texture, angle, sub_texture_area); // } - void Renderer2D::DrawQuad(Rectangle quad, Color color, Texture* texture, float angle, Rectangle sub_texture_area) + void Renderer2D::DrawQuad(Rectangle quad, Color color, Texture* texture, float angle, Rectangle sub_texture_area, glm::mat4 parent_transform) { if (!mQuadData.VertexBuffer->WillFit(4)) { @@ -168,6 +168,8 @@ namespace lunarium // Need to invert the offset_y because OpenGL likes being weird offset_y *= -1.0f; + glm::vec3 parent_pos = parent_transform[3]; + // FIRST QuadData::Vertex verts[4]; int vert_size = sizeof(QuadData::Vertex); @@ -179,6 +181,7 @@ namespace lunarium verts[0].angle = angle; verts[0].scale = glm::vec3(quad.HalfWidth * 2, quad.HalfHeight * 2, 1.0f); verts[0].tex_is_grey_scale = tex_is_grey_scale; + verts[0].parent_transform = parent_pos; //memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v1, vert_size); //mQuadData.RawBufferIndex += 1; //mQuadData.VertexBuffer->PushVertices((void*)&v1, 1); @@ -192,6 +195,7 @@ namespace lunarium verts[1].angle = angle; verts[1].scale = glm::vec3(quad.HalfWidth * 2, quad.HalfHeight * 2, 1.0f); verts[1].tex_is_grey_scale = tex_is_grey_scale; + verts[1].parent_transform = parent_pos; //memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v2, vert_size); //mQuadData.RawBufferIndex += 1; // mQuadData.VertexBuffer->PushVertices((void*)&v2, 1); @@ -205,6 +209,7 @@ namespace lunarium verts[2].angle = angle; verts[2].scale = glm::vec3(quad.HalfWidth * 2, quad.HalfHeight * 2, 1.0f); verts[2].tex_is_grey_scale = tex_is_grey_scale; + verts[2].parent_transform = parent_pos; //memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v3, vert_size); //mQuadData.RawBufferIndex += 1; //mQuadData.VertexBuffer->PushVertices((void*)&v3, 1); @@ -218,6 +223,7 @@ namespace lunarium verts[3].angle = angle; verts[3].scale = glm::vec3(quad.HalfWidth * 2, quad.HalfHeight * 2, 1.0f); verts[3].tex_is_grey_scale = tex_is_grey_scale; + verts[3].parent_transform = parent_pos; //memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v4, vert_size); //mQuadData.RawBufferIndex += 1; @@ -498,6 +504,7 @@ namespace lunarium mQuadData.BufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 1 }); // angle mQuadData.BufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 3 }); // Scale mQuadData.BufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 1 }); // tex_is_grey_scale + mQuadData.BufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 3 }); // parent_transform mQuadData.VertexBuffer = VertexBuffer::Create(mQuadData.BufferLayout, mQuadData.MaxVertices, nullptr, mQuadData.MaxIndices, indices); // RAW VERTEX BUFFER diff --git a/src/renderer/renderer2D.h b/src/renderer/renderer2D.h index 03ad1fe..94b77f7 100644 --- a/src/renderer/renderer2D.h +++ b/src/renderer/renderer2D.h @@ -47,11 +47,11 @@ namespace lunarium /// sub_texture_area is the area of the texture to draw /// position is the top-left corner of the quad - void DrawSprite(Texture* texture, glm::vec2 position, Rectangle sub_texture_area, Color color = Color::White(), float angle = 0.0f); + void DrawSprite(Texture* texture, glm::vec2 position, Rectangle sub_texture_area, Color color = Color::White(), float angle = 0.0f, glm::mat4 parent_transform = glm::mat4(1.0f)); //void DrawSprite(Texture* texture, Rectangle destination, Rectangle sub_texture_area, Color color = Color::White(), float angle = 0.0f); /// sub_texture_area is the area of the texture to draw - void DrawQuad(Rectangle quad, Color color, Texture* texture = nullptr, float angle = 0.0f, Rectangle sub_texture_area = Rectangle(-1, -1, -1, -1)); + void DrawQuad(Rectangle quad, Color color, Texture* texture = nullptr, float angle = 0.0f, Rectangle sub_texture_area = Rectangle(-1, -1, -1, -1), glm::mat4 parent_transform = glm::mat4(1.0f)); void DrawLine(glm::vec2 point_a, glm::vec2 point_b, Color color, float thickness = 1.0f, float angle = 0.0f); void DrawBox(Rectangle box, Color color, float thickness = 1.5f, float angle = 0.0f); @@ -142,6 +142,7 @@ namespace lunarium float angle; glm::vec3 scale; float tex_is_grey_scale; // 0 or 1, this is mostly used for text rendering + glm::vec3 parent_transform; }; const u32 indices[6] = { 0, 1, 2, 2, 3, 0 }; diff --git a/src/renderer/shader.cpp b/src/renderer/shader.cpp index d9c34ff..ccfaaa2 100644 --- a/src/renderer/shader.cpp +++ b/src/renderer/shader.cpp @@ -130,7 +130,7 @@ namespace lunarium } - OpRes Shader::SetUniform(Uniform& uniform, void* values) + OpRes Shader::SetUniform(Uniform& uniform, void* values, int num_uniforms) { if (uniform.Location == -1) { @@ -143,18 +143,18 @@ namespace lunarium switch (uniform.Type) { - case UniformType::F1: glUniform1fv(uniform.Location, 1, (GLfloat*)values); break; - case UniformType::F2: glUniform2fv(uniform.Location, 1, (GLfloat*)values); break; - case UniformType::F3: glUniform3fv(uniform.Location, 1, (GLfloat*)values); break; - case UniformType::F4: glUniform4fv(uniform.Location, 1, (GLfloat*)values); break; - - case UniformType::I1: glUniform1iv(uniform.Location, 1, (GLint*)values); break; - case UniformType::I2: glUniform2iv(uniform.Location, 1, (GLint*)values); break; - case UniformType::I3: glUniform3iv(uniform.Location, 1, (GLint*)values); break; - case UniformType::I4: glUniform4iv(uniform.Location, 1, (GLint*)values); break; - - case UniformType::FMAT3: glUniformMatrix3fv(uniform.Location, 1, GL_FALSE, (GLfloat*)values); break; - case UniformType::FMAT4: glUniformMatrix4fv(uniform.Location, 1, GL_FALSE, (GLfloat*)values); break; + case UniformType::F1: glUniform1fv(uniform.Location, num_uniforms, (GLfloat*)values); break; + case UniformType::F2: glUniform2fv(uniform.Location, num_uniforms, (GLfloat*)values); break; + case UniformType::F3: glUniform3fv(uniform.Location, num_uniforms, (GLfloat*)values); break; + case UniformType::F4: glUniform4fv(uniform.Location, num_uniforms, (GLfloat*)values); break; + + case UniformType::I1: glUniform1iv(uniform.Location, num_uniforms, (GLint*)values); break; + case UniformType::I2: glUniform2iv(uniform.Location, num_uniforms, (GLint*)values); break; + case UniformType::I3: glUniform3iv(uniform.Location, num_uniforms, (GLint*)values); break; + case UniformType::I4: glUniform4iv(uniform.Location, num_uniforms, (GLint*)values); break; + + case UniformType::FMAT3: glUniformMatrix3fv(uniform.Location, num_uniforms, GL_FALSE, (GLfloat*)values); break; + case UniformType::FMAT4: glUniformMatrix4fv(uniform.Location, num_uniforms, GL_FALSE, (GLfloat*)values); break; default: return OpRes::Fail("Can not set uniform value - Unknown type"); } diff --git a/src/renderer/shader.h b/src/renderer/shader.h index 142986e..903d623 100644 --- a/src/renderer/shader.h +++ b/src/renderer/shader.h @@ -50,7 +50,7 @@ namespace lunarium OpRes GetAllUniforms(std::vector& uniforms); void GetUniformLocation(Uniform& uniform); - OpRes SetUniform(Uniform& uniform, void* values); + OpRes SetUniform(Uniform& uniform, void* values, int num_uniforms = 1); private: u32 mGLID; diff --git a/src/renderer/shaders/quad.vert b/src/renderer/shaders/quad.vert index 431eb9b..b074cf3 100644 --- a/src/renderer/shaders/quad.vert +++ b/src/renderer/shaders/quad.vert @@ -8,6 +8,7 @@ layout(location = 4) in vec3 translation; layout(location = 5) in float angle; layout(location = 6) in vec3 scale; layout(location = 7) in vec3 tex_is_grey_scale; +layout(location = 8) in vec3 parent_pos; layout (location = 0) out vec2 f_tex_coords; layout (location = 1) out vec4 f_vert_color; @@ -15,6 +16,7 @@ layout (location = 2) flat out int f_tex_index; layout (location = 3) flat out int f_tex_is_grey; uniform mat4 projview; +uniform mat4 parents[16]; void main() { @@ -55,5 +57,11 @@ void main() model = model * Rotation; model = model * Scale; - gl_Position = projview * model * vec4(pos, 1.0); + mat4 Parent_Translation = mat4( + vec4( 1.0, 0.0, 0.0, 0.0), + vec4( 0.0, 1.0, 0.0, 0.0), + vec4( 0.0, 0.0, 1.0, 0.0), + vec4( parent_pos.xyz, 1.0)); + + gl_Position = projview * Parent_Translation * model * vec4(pos, 1.0); } \ No newline at end of file diff --git a/src/renderer/vertex_buffer.cpp b/src/renderer/vertex_buffer.cpp index 74c0a62..4ef6d8e 100644 --- a/src/renderer/vertex_buffer.cpp +++ b/src/renderer/vertex_buffer.cpp @@ -122,6 +122,12 @@ namespace lunarium glBufferData(GL_ARRAY_BUFFER, mSize, nullptr, GL_DYNAMIC_DRAW); } + int err = glGetError(); + if (err > 0) + { + Logger::Error(LogCategory::GRAPHICS, "Failed to set vertex buffer data, error code: %d", err); + } + if (mEBOSize > 0) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mEBO); @@ -131,7 +137,19 @@ namespace lunarium glBufferData(GL_ELEMENT_ARRAY_BUFFER, mEBOSize, indices, mode); } + err = glGetError(); + if (err > 0) + { + Logger::Error(LogCategory::GRAPHICS, "Failed to create EBO, error code: %d", err); + } + mLayout.ImplementLayout(); + + err = glGetError(); + if (err > 0) + { + Logger::Error(LogCategory::GRAPHICS, "Failed to implement vertex layout: error code: %d", err); + } Unbind(); UnbindVBO(); } diff --git a/src/run_modes/testbed/scenes/simple_render_scene.cpp b/src/run_modes/testbed/scenes/simple_render_scene.cpp index db4149c..19f866c 100644 --- a/src/run_modes/testbed/scenes/simple_render_scene.cpp +++ b/src/run_modes/testbed/scenes/simple_render_scene.cpp @@ -18,6 +18,8 @@ #include #include +#include + #include #include @@ -58,6 +60,9 @@ namespace lunarium GenerateQuads(); GenerateLines(); + mParentPos = glm::vec3(300.0f, 300.0f, 0.0f); + mChildPos = glm::vec3(250.0f, 0.0f, 0.0f); + angle = 0.0f; box_angle = 0.0f; mTextDebugPosX = 200; @@ -183,6 +188,7 @@ namespace lunarium case TestMode::String: RenderStringTest(Core::Graphics()); break; case TestMode::Shapes: RenderShapesTest(Core::Graphics()); break; case TestMode::TileMap: RenderTileMapTest(Core::Graphics()); break; + case TestMode::Parent: RenderParentTest(Core::Graphics()); break; } @@ -284,19 +290,27 @@ namespace lunarium mTestMap.Render(&g); } + void SimpleRenderScene::RenderParentTest(Renderer2D& g) + { + g.DrawQuad(Rectangle(mParentPos.x, mParentPos.y, 50.0f, 50.0f), Color::Blue()); + glm::mat4 parent = glm::mat4(1.0f); + parent = glm::translate(parent, mParentPos); + g.DrawQuad(Rectangle(mChildPos.x, mChildPos.y, 50.0f, 50.0f), Color::Green(), nullptr, 0.0f, Rectangle(-1, -1, -1, -1), parent); + } + ///////////////////////////////////////////////////////////////////// // GUI DRAWING GUI DRAWING ///////////////////////////////////////////////////////////////////// void SimpleRenderScene::DrawStatsGUI() { - const char* mode_names[5] = { "Basic", "Stress", "String", "Shapes", "TileMap"}; + const char* mode_names[6] = { "Basic", "Stress", "String", "Shapes", "TileMap", "Parent/Child"}; ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_FirstUseEver); ImGui::SetNextWindowSize(ImVec2(400, 800)); ImGui::Begin("RENDER INFO"); ImGui::BeginChild("Scene Info", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 3.5f), true); ImGui::Text("Scene Info"); ImGui::Separator(); - ImGui::Combo("Scene Mode", (int*)&mTestMode, mode_names, 5); + ImGui::Combo("Scene Mode", (int*)&mTestMode, mode_names, 6); ImGui::EndChild(); ImGui::BeginChild("Per Frame", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 3.5f), true); ImGui::Text("Per Frame"); @@ -383,8 +397,25 @@ namespace lunarium } ImGui::EndChild(); } - - + else if (mTestMode == TestMode::Parent) + { + float parent_pos[3] = { mParentPos.x, mParentPos.y, mParentPos.z }; + float child_pos[3] = { mChildPos.x, mChildPos.y, mChildPos.z }; + + ImGui::Text("Parent Position:"); + ImGui::SameLine(); + if (ImGui::DragFloat3("##ParentPos", parent_pos)) + { + mParentPos = { parent_pos[0], parent_pos[1], parent_pos[2] }; + } + + ImGui::Text("Child Position:"); + ImGui::SameLine(); + if (ImGui::DragFloat3("##ChildPos", child_pos)) + { + mChildPos = { child_pos[0], child_pos[1], child_pos[2] }; + } + } ImGui::EndChild(); ImGui::End(); diff --git a/src/run_modes/testbed/scenes/simple_render_scene.h b/src/run_modes/testbed/scenes/simple_render_scene.h index f3a2c2e..d6a0346 100644 --- a/src/run_modes/testbed/scenes/simple_render_scene.h +++ b/src/run_modes/testbed/scenes/simple_render_scene.h @@ -31,6 +31,7 @@ namespace lunarium String, Shapes, TileMap, + Parent, }; enum ShapeMode @@ -50,6 +51,7 @@ namespace lunarium void RenderStringTest(Renderer2D& g); void RenderShapesTest(Renderer2D& g); void RenderTileMapTest(Renderer2D& g); + void RenderParentTest(Renderer2D& g); private: @@ -117,6 +119,10 @@ namespace lunarium editor::TileMap mTestMap; editor::TileSet mTestSet; + // PARENT CHILD TEST + glm::vec3 mParentPos; + glm::vec3 mChildPos; + struct GridTestObj { int X; diff --git a/src/world/entity.cpp b/src/world/entity.cpp index 1e6e413..33a3f9c 100644 --- a/src/world/entity.cpp +++ b/src/world/entity.cpp @@ -28,7 +28,13 @@ namespace lunarium void Entity::Init() { - mHandle = mWorld.GetEntityRegistry()->create(); + auto prev = mHandle; + mHandle = mWorld.GetEntityRegistry()->create(mHandle); + + if (prev != mHandle && prev != entt::null) + { + Logger::Warn(LogCategory::GAME_SYSTEM, "Requested entity handle was not used"); + } } LUUID Entity::GetUUID() const diff --git a/test_data/engine_state.json b/test_data/engine_state.json index 5004d9f..a338efe 100644 --- a/test_data/engine_state.json +++ b/test_data/engine_state.json @@ -2,7 +2,7 @@ "State": { "DataDirectory": "data/", - "Mode": "editor", + "Mode": "test", "Display": {