From a73aab63ffe70081dec13e175157d25f3a5b46a2 Mon Sep 17 00:00:00 2001 From: Daniel Mateu <32674639+Nyghor@users.noreply.github.com> Date: Tue, 8 May 2018 20:51:42 +0200 Subject: [PATCH] Solved mesh instantiation bug, added plane mesh. --- LCSEngine/GameObject.cpp | 9 ++++++--- LCSEngine/MeshComponent.cpp | 36 ++++++++++++++++++++---------------- LCSEngine/MeshComponent.h | 3 ++- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/LCSEngine/GameObject.cpp b/LCSEngine/GameObject.cpp index a0f0bee..fb83827 100644 --- a/LCSEngine/GameObject.cpp +++ b/LCSEngine/GameObject.cpp @@ -991,9 +991,12 @@ void GameObject::updateComponents() switch ((*it)->typeComponent) { case MESH: - aabb.Enclose(&((MeshComponent*)(*it))->verticesVBO[0], ((MeshComponent*)(*it))->verticesVBO.size()); - obb = aabb.ToOBB(); - obb.Transform(id.Transposed()); + if (((MeshComponent*)(*it))->validMesh) + { + aabb.Enclose(&((MeshComponent*)(*it))->verticesVBO[0], ((MeshComponent*)(*it))->verticesVBO.size()); + obb = aabb.ToOBB(); + obb.Transform(id.Transposed()); + } break; case CAMERA: ((CameraComponent*)(*it))->frustum.pos = id.Transposed().TranslatePart(); diff --git a/LCSEngine/MeshComponent.cpp b/LCSEngine/MeshComponent.cpp index 140b76f..53c29cc 100644 --- a/LCSEngine/MeshComponent.cpp +++ b/LCSEngine/MeshComponent.cpp @@ -9,7 +9,7 @@ #include "SaveLoadManager.h" static int selected_preset = 1; -const char* presets[] = { "House", "Cube", "Sphere" }; +const char* presets[] = { "Plane", "Cube", "Sphere", "Model" }; MeshComponent::MeshComponent(GameObject* gameObject, bool isEnable, bool isUnique) : Component(gameObject, isEnable, isUnique) { @@ -33,9 +33,13 @@ void MeshComponent::setPreset(PresetType type) void MeshComponent::loadPreset() { verticesVBO.clear(); - currentPreset = MODEL; switch (currentPreset) { + case PLANE: + { + loadPlane(); + break; + } case CUBE: { loadCube(); @@ -124,20 +128,7 @@ void MeshComponent::drawGUI() if (ImGui::Selectable(presets[i])) { selected_preset = i; - //Set Mesh of preset selected - if (i == 0) - { - currentPreset = PresetType::MODEL; - } - else if (i == 1) - { - currentPreset = PresetType::CUBE; - } - else if (i == 2) - { - currentPreset = PresetType::SPHERE; - } - loadPreset(); + setPreset((PresetType)selected_preset); } } ImGui::EndPopup(); @@ -237,6 +228,19 @@ void MeshComponent::loadCube() 4, 6, 7, 4, 5, 6 }; } +void MeshComponent::loadPlane() +{ + verticesVBO.reserve(4); + indicesVAO.reserve(6); + normalsVBO.reserve(4); + texCoordsVBO.reserve(4); + + verticesVBO = { float3(-0.5f, 0.5f, 0.f), float3(0.5f, 0.5f, 0.f), float3(0.5f, -0.5f, 0.f), float3(-0.5f, -0.5f, 0.f) }; + indicesVAO = { 1, 3, 2, 1, 0, 3 }; + normalsVBO = { float3(0.f, 0.f, 1.f), float3(0.f, 0.f, 1.f), float3(0.f, 0.f, 1.f), float3(0.f, 0.f, 1.f) }; + texCoordsVBO = { float2(0.f, 0.f), float2(1.f, 0.f), float2(1.f, 1.f), float2(0.f, 1.f) }; +} + void MeshComponent::loadModel() { for (unsigned i = 0; i < model->scene->mRootNode->mNumChildren; ++i) diff --git a/LCSEngine/MeshComponent.h b/LCSEngine/MeshComponent.h index d3d7820..5a71442 100644 --- a/LCSEngine/MeshComponent.h +++ b/LCSEngine/MeshComponent.h @@ -10,7 +10,7 @@ enum PresetType { - TRIANGLE = 1, + PLANE, CUBE, SPHERE, MODEL @@ -71,6 +71,7 @@ class MeshComponent : public Component void loadModel(); void loadSphere(); void loadCube(); + void loadPlane(); }; #endif //__MESHCOMPONENT_H__ \ No newline at end of file