diff --git a/src/renderer/renderer2D.cpp b/src/renderer/renderer2D.cpp index 0051bd2..ceeb0da 100644 --- a/src/renderer/renderer2D.cpp +++ b/src/renderer/renderer2D.cpp @@ -128,6 +128,16 @@ namespace lunarium Flush(FlushMode::Quads); } + float parent_idx = -1.0f; + if (parent_transform != glm::mat4(1.0f)) + { + if (mQuadData.Parents.size() > 15) + Flush(FlushMode::Quads); + + mQuadData.Parents.push_back(parent_transform); + parent_idx = mQuadData.Parents.size() - 1; + } + int texture_slot = -1; if (texture) { @@ -168,7 +178,7 @@ namespace lunarium // Need to invert the offset_y because OpenGL likes being weird offset_y *= -1.0f; - glm::vec3 parent_pos = parent_transform[3]; + //glm::vec3 parent_pos = parent_transform[3]; // FIRST QuadData::Vertex verts[4]; @@ -181,7 +191,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; + verts[0].parent_idx = parent_idx; //memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v1, vert_size); //mQuadData.RawBufferIndex += 1; //mQuadData.VertexBuffer->PushVertices((void*)&v1, 1); @@ -195,7 +205,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; + verts[1].parent_idx = parent_idx; //memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v2, vert_size); //mQuadData.RawBufferIndex += 1; // mQuadData.VertexBuffer->PushVertices((void*)&v2, 1); @@ -209,7 +219,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; + verts[2].parent_idx = parent_idx; //memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v3, vert_size); //mQuadData.RawBufferIndex += 1; //mQuadData.VertexBuffer->PushVertices((void*)&v3, 1); @@ -223,7 +233,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; + verts[3].parent_idx = parent_idx; //memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v4, vert_size); //mQuadData.RawBufferIndex += 1; @@ -396,6 +406,12 @@ namespace lunarium mQuadData.QuadShader->SetUniform(uprojview, (void*)glm::value_ptr(mpCamera->GetViewProjection())).LogIfFailed(LogCategory::GRAPHICS); + Uniform uParents; + uParents.Type = UniformType::FMAT4; + uParents.Location = -1; + uParents.Name = "parents"; + mQuadData.QuadShader->SetUniform(uParents, (void*)mQuadData.Parents.data(), mQuadData.Parents.size()).LogIfFailed(LogCategory::GRAPHICS); + //mQuadData.VertexBuffer->Bind(); mQuadData.VertexBuffer->DrawArray(mQuadData.NumQuads * 6); @@ -504,7 +520,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.BufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 1 }); // parent_idx mQuadData.VertexBuffer = VertexBuffer::Create(mQuadData.BufferLayout, mQuadData.MaxVertices, nullptr, mQuadData.MaxIndices, indices); // RAW VERTEX BUFFER @@ -625,6 +641,7 @@ namespace lunarium mQuadData.VertexBuffer->Clear(); mQuadData.NumQuads = 0; //mQuadData.RawBufferIndex = 0; + mQuadData.Parents.clear(); mQuadData.MarkedForReset = false; } diff --git a/src/renderer/renderer2D.h b/src/renderer/renderer2D.h index 94b77f7..a606d55 100644 --- a/src/renderer/renderer2D.h +++ b/src/renderer/renderer2D.h @@ -142,7 +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; + float parent_idx; // -1 for no parent }; const u32 indices[6] = { 0, 1, 2, 2, 3, 0 }; @@ -154,6 +154,7 @@ namespace lunarium VertexBuffer* VertexBuffer; Shader* QuadShader; Shader* WireFrameShader; + std::vector Parents; } mQuadData; ///////////////////////////////////////////////////////////////////// diff --git a/src/renderer/shaders/quad.vert b/src/renderer/shaders/quad.vert index b074cf3..e7cd7fd 100644 --- a/src/renderer/shaders/quad.vert +++ b/src/renderer/shaders/quad.vert @@ -8,7 +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 = 8) in float parent_idx; layout (location = 0) out vec2 f_tex_coords; layout (location = 1) out vec4 f_vert_color; @@ -57,11 +57,19 @@ void main() model = model * Rotation; model = model * Scale; - 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)); + // 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)); + + mat4 parent = 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(0.0, 0.0, 0.0, 1.0)); + + int ipidx = int(parent_idx); + if (ipidx >= 0) + { + parent = parents[ipidx]; + } - gl_Position = projview * Parent_Translation * model * vec4(pos, 1.0); + gl_Position = projview * parent * model * vec4(pos, 1.0); } \ No newline at end of file diff --git a/src/run_modes/testbed/scenes/simple_render_scene.cpp b/src/run_modes/testbed/scenes/simple_render_scene.cpp index 19f866c..a494818 100644 --- a/src/run_modes/testbed/scenes/simple_render_scene.cpp +++ b/src/run_modes/testbed/scenes/simple_render_scene.cpp @@ -62,6 +62,14 @@ namespace lunarium mParentPos = glm::vec3(300.0f, 300.0f, 0.0f); mChildPos = glm::vec3(250.0f, 0.0f, 0.0f); + mParentAngle = 0.0f; + mChildAngle = 0.0f; + + mParentPos2 = glm::vec3(700.0f, 200.0f, 0.0f); + mChildPos2 = glm::vec3(-300.0f, 50.0f, 0.0f); + + mParentPos3 = glm::vec3(200.0f, 600.0f, 0.0f); + mChildPos3 = glm::vec3(150.0f, 100.0f, 0.0f); angle = 0.0f; box_angle = 0.0f; @@ -292,10 +300,24 @@ namespace lunarium void SimpleRenderScene::RenderParentTest(Renderer2D& g) { - g.DrawQuad(Rectangle(mParentPos.x, mParentPos.y, 50.0f, 50.0f), Color::Blue()); + g.DrawQuad(Rectangle(mParentPos.x, mParentPos.y, 50.0f, 50.0f), Color::Blue(), nullptr, mParentAngle); 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); + parent = glm::rotate(parent, mParentAngle, glm::vec3(0.0f, 0.0f, 1.0f)); + g.DrawQuad(Rectangle(mChildPos.x, mChildPos.y, 50.0f, 50.0f), Color::Green(), nullptr, mChildAngle, Rectangle(-1, -1, -1, -1), parent); + g.DrawLine(glm::vec2(mParentPos.x, mParentPos.y), glm::vec2(mParentPos.x + mChildPos.x, mParentPos.y + mChildPos.y), Color::Blue(), 1.5f); + + g.DrawQuad(Rectangle(mParentPos2.x, mParentPos2.y, 50.0f, 50.0f), Color::Red()); + parent = glm::mat4(1.0f); + parent = glm::translate(parent, mParentPos2); + g.DrawQuad(Rectangle(mChildPos2.x, mChildPos2.y, 50.0f, 50.0f), Color::White(), nullptr, mChildAngle, Rectangle(-1, -1, -1, -1), parent); + g.DrawLine(glm::vec2(mParentPos2.x, mParentPos2.y), glm::vec2(mParentPos2.x + mChildPos2.x, mParentPos2.y + mChildPos2.y), Color::Red(), 1.5f); + + g.DrawQuad(Rectangle(mParentPos3.x, mParentPos3.y, 50.0f, 50.0f), Color::Black()); + parent = glm::mat4(1.0f); + parent = glm::translate(parent, mParentPos3); + g.DrawQuad(Rectangle(mChildPos3.x, mChildPos3.y, 50.0f, 50.0f), Color(0.75f, 0.15f, 0.5f, 1.0f), nullptr, mChildAngle, Rectangle(-1, -1, -1, -1), parent); + g.DrawLine(glm::vec2(mParentPos3.x, mParentPos3.y), glm::vec2(mParentPos3.x + mChildPos3.x, mParentPos3.y + mChildPos3.y), Color::Black(), 1.5f); } ///////////////////////////////////////////////////////////////////// @@ -325,7 +347,7 @@ namespace lunarium ImGui::Text("FPS: %d", Core::GetInstance().GetFrameData().CurrentFPS); ImGui::Text("Frame Number: %d", mNumFrames); ImGui::EndChild(); - ImGui::BeginChild("Settings", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 6.5f), true); + ImGui::BeginChild("Settings", ImVec2(ImGui::GetWindowSize().x - 15, ImGui::GetFrameHeightWithSpacing() * 10.5f), true); ImGui::Text("Settings"); ImGui::Separator(); if (mTestMode == TestMode::Stress) @@ -399,6 +421,8 @@ namespace lunarium } else if (mTestMode == TestMode::Parent) { + // GROUP ONE + ImGui::Text("Group One:"); float parent_pos[3] = { mParentPos.x, mParentPos.y, mParentPos.z }; float child_pos[3] = { mChildPos.x, mChildPos.y, mChildPos.z }; @@ -409,12 +433,59 @@ namespace lunarium mParentPos = { parent_pos[0], parent_pos[1], parent_pos[2] }; } + ImGui::Text("Parent Angle:"); + ImGui::SameLine(); + ImGui::DragFloat("##ParentAngle", &mParentAngle); + ImGui::Text("Child Position:"); ImGui::SameLine(); if (ImGui::DragFloat3("##ChildPos", child_pos)) { mChildPos = { child_pos[0], child_pos[1], child_pos[2] }; } + + ImGui::Text("Child Angle:"); + ImGui::SameLine(); + ImGui::DragFloat("##ChildAngle", &mChildAngle); + + + // GROUP TWO + ImGui::Text("Group two:"); + float parent_pos2[3] = { mParentPos2.x, mParentPos2.y, mParentPos2.z }; + float child_pos2[3] = { mChildPos2.x, mChildPos2.y, mChildPos2.z }; + + ImGui::Text("Parent Position:"); + ImGui::SameLine(); + if (ImGui::DragFloat3("##ParentPos2", parent_pos2)) + { + mParentPos2 = { parent_pos2[0], parent_pos2[1], parent_pos2[2] }; + } + + ImGui::Text("Child Position:"); + ImGui::SameLine(); + if (ImGui::DragFloat3("##ChildPos2", child_pos2)) + { + mChildPos2 = { child_pos2[0], child_pos2[1], child_pos2[2] }; + } + + // GROUP THREE + ImGui::Text("Group three:"); + float parent_pos3[3] = { mParentPos3.x, mParentPos3.y, mParentPos3.z }; + float child_pos3[3] = { mChildPos3.x, mChildPos3.y, mChildPos3.z }; + + ImGui::Text("Parent Position:"); + ImGui::SameLine(); + if (ImGui::DragFloat3("##ParentPos3", parent_pos3)) + { + mParentPos3 = { parent_pos3[0], parent_pos3[1], parent_pos3[2] }; + } + + ImGui::Text("Child Position:"); + ImGui::SameLine(); + if (ImGui::DragFloat3("##ChildPos3", child_pos3)) + { + mChildPos3 = { child_pos3[0], child_pos3[1], child_pos3[2] }; + } } ImGui::EndChild(); diff --git a/src/run_modes/testbed/scenes/simple_render_scene.h b/src/run_modes/testbed/scenes/simple_render_scene.h index d6a0346..223c46c 100644 --- a/src/run_modes/testbed/scenes/simple_render_scene.h +++ b/src/run_modes/testbed/scenes/simple_render_scene.h @@ -121,7 +121,15 @@ namespace lunarium // PARENT CHILD TEST glm::vec3 mParentPos; + float mParentAngle; glm::vec3 mChildPos; + float mChildAngle; + + glm::vec3 mParentPos2; + glm::vec3 mChildPos2; + + glm::vec3 mParentPos3; + glm::vec3 mChildPos3; struct GridTestObj {