diff --git a/include/Engine/Collision/Collision.h b/include/Engine/Collision/Collision.h index 5431df4d..1fd24cc6 100644 --- a/include/Engine/Collision/Collision.h +++ b/include/Engine/Collision/Collision.h @@ -48,13 +48,13 @@ bool RayVsTriangle(const Ray& ray, bool trueOnNegativeDistance = false); //Return true if the ray hits any of the triangles in the model. Stops checking when a hit is detected. bool RayVsModel(const Ray& ray, - const RawModel::Vertex* modelVertices, + const std::vector& modelVertices, const std::vector& modelIndices, const glm::mat4& modelMatrix); //Return true if the ray hits any of the triangles in the model. //Also returns the position of the intersection point. Will loop through all the whole model indices. bool RayVsModel(const Ray& ray, - const RawModel::Vertex* modelVertices, + const std::vector& modelVertices, const std::vector& modelIndices, const glm::mat4& modelMatrix, glm::vec3& outHitPosition); @@ -62,7 +62,7 @@ bool RayVsModel(const Ray& ray, //Also returns the distance from the ray origin to the closest //intersection point, and the barycentric u,v-coordinates. Will loop through all the whole model indices. bool RayVsModel(const Ray& ray, - const RawModel::Vertex* modelVertices, + const std::vector& modelVertices, const std::vector& modelIndices, const glm::mat4& modelMatrix, float& outDistance, @@ -70,7 +70,7 @@ bool RayVsModel(const Ray& ray, float& outVCoord); bool AABBvsTriangles(const AABB& box, - const RawModel::Vertex* modelVertices, + const std::vector& modelVertices, const std::vector& modelIndices, const glm::mat4& modelMatrix, glm::vec3& boxVelocity, @@ -80,7 +80,7 @@ bool AABBvsTriangles(const AABB& box, //Detects collision, but does not resolve. bool AABBvsTriangles(const AABB& box, - const RawModel::Vertex* modelVertices, + const std::vector& modelVertices, const std::vector& modelIndices, const glm::mat4& modelMatrix); @@ -92,7 +92,7 @@ enum Output }; //Detects intersection and containment. Output AABBvsTrianglesWContainment(const AABB& box, - const RawModel::Vertex* modelVertices, + const std::vector& modelVertices, const std::vector& modelIndices, const glm::mat4& modelMatrix); diff --git a/include/Engine/Rendering/Model.h b/include/Engine/Rendering/Model.h index 4c63b922..01752eb3 100644 --- a/include/Engine/Rendering/Model.h +++ b/include/Engine/Rendering/Model.h @@ -16,15 +16,18 @@ private: public: ~Model(); - const std::vector& MaterialGroups() const { return m_RawModel->m_Materials; } - const glm::mat4& Matrix() const { return m_RawModel->m_Matrix; } - const RawModel::Vertex* Vertices() const { return m_RawModel->Vertices(); } - unsigned int NumberOfVertices() const { return m_RawModel->NumVertices(); } + const std::vector& MaterialGroups() const { return m_Materials; } + //const RawModel::Vertex* Vertices() const { return m_RawModel->Vertices(); } + unsigned int NumberOfVertices() const { return m_Vertices.size(); } const AABB& Box() const { return m_Box; } - bool IsSkinned() const { return m_RawModel->IsSkinned(); } + bool IsSkinned() const { return m_IsSkinned; } GLuint VAO; GLuint ElementBuffer; - RawModel* m_RawModel; + //RawModel* m_RawModel; + + Skeleton* m_Skeleton = nullptr; + std::vector m_Vertices; + std::vector m_Indices; private: AABB m_Box; @@ -34,6 +37,9 @@ private: GLuint TangentNormalsBuffer; GLuint BiTangentNormalsBuffer; GLuint TextureCoordBuffer; + + std::vector m_Materials; + bool m_IsSkinned; }; #endif diff --git a/include/Engine/Rendering/ModelJob.h b/include/Engine/Rendering/ModelJob.h index 0bb3d1c8..11e93f61 100644 --- a/include/Engine/Rendering/ModelJob.h +++ b/include/Engine/Rendering/ModelJob.h @@ -105,7 +105,7 @@ struct ModelJob : RenderJob IsShielded = isShielded; if (model->IsSkinned()) { - Skeleton = Model->m_RawModel->m_Skeleton; + Skeleton = Model->m_Skeleton; if (Skeleton != nullptr) { diff --git a/include/Engine/Rendering/RawModelCustom.h b/include/Engine/Rendering/RawModelCustom.h index f9fd18ef..ea573cba 100644 --- a/include/Engine/Rendering/RawModelCustom.h +++ b/include/Engine/Rendering/RawModelCustom.h @@ -13,6 +13,7 @@ #include +#include #include "../Common.h" #include "../GLM.h" #include "../Core/ResourceManager.h" @@ -20,14 +21,10 @@ #include "Skeleton.h" #include "ShaderProgram.h" -#include "boost\endian\buffers.hpp" - - - class RawModelCustom : public Resource { friend class ResourceManager; - + friend class Model; protected: RawModelCustom(std::string fileName); diff --git a/include/Game/Systems/SpawnerSystem.h b/include/Game/Systems/SpawnerSystem.h index c5b5b7b8..5c5ca2db 100644 --- a/include/Game/Systems/SpawnerSystem.h +++ b/include/Game/Systems/SpawnerSystem.h @@ -8,6 +8,7 @@ #include "Events/ESpawnerSpawn.h" #include "Core/TransformSystem.h" #include "Core/EntityFile.h" +#include "Rendering/Model.h" class SpawnerSystem : public System { diff --git a/src/Engine/Collision/Collision.cpp b/src/Engine/Collision/Collision.cpp index dc04e8cf..ae09aeaf 100644 --- a/src/Engine/Collision/Collision.cpp +++ b/src/Engine/Collision/Collision.cpp @@ -146,14 +146,14 @@ bool RayVsTriangle(const Ray& ray, } bool RayVsModel(const Ray& ray, - const RawModel::Vertex* modelVertices, + const std::vector& modelVertices, const std::vector& modelIndices, const glm::mat4& modelMatrix) { for (int i = 0; i < modelIndices.size();) { - glm::vec3 v0 = TransformSystem::TransformPoint(modelVertices[modelIndices[i++]].Position, modelMatrix); - glm::vec3 v1 = TransformSystem::TransformPoint(modelVertices[modelIndices[i++]].Position, modelMatrix); - glm::vec3 v2 = TransformSystem::TransformPoint(modelVertices[modelIndices[i++]].Position, modelMatrix); + glm::vec3 v0 = TransformSystem::TransformPoint(modelVertices[modelIndices[i++]], modelMatrix); + glm::vec3 v1 = TransformSystem::TransformPoint(modelVertices[modelIndices[i++]], modelMatrix); + glm::vec3 v2 = TransformSystem::TransformPoint(modelVertices[modelIndices[i++]], modelMatrix); if (RayVsTriangle(ray, v0, v1, v2)) { return true; } @@ -194,7 +194,7 @@ bool RayVsTriangle(const Ray& ray, } bool RayVsModel(const Ray& ray, - const RawModel::Vertex* modelVertices, + const std::vector& modelVertices, const std::vector& modelIndices, const glm::mat4& modelMatrix, float& outDistance, @@ -204,9 +204,9 @@ bool RayVsModel(const Ray& ray, outDistance = INFINITY; bool hit = false; for (int i = 0; i < modelIndices.size();) { - glm::vec3 v0 = TransformSystem::TransformPoint(modelVertices[modelIndices[i++]].Position, modelMatrix); - glm::vec3 v1 = TransformSystem::TransformPoint(modelVertices[modelIndices[i++]].Position, modelMatrix); - glm::vec3 v2 = TransformSystem::TransformPoint(modelVertices[modelIndices[i++]].Position, modelMatrix); + glm::vec3 v0 = TransformSystem::TransformPoint(modelVertices[modelIndices[i++]], modelMatrix); + glm::vec3 v1 = TransformSystem::TransformPoint(modelVertices[modelIndices[i++]], modelMatrix); + glm::vec3 v2 = TransformSystem::TransformPoint(modelVertices[modelIndices[i++]], modelMatrix); float dist = outDistance; float u; float v; @@ -221,7 +221,7 @@ bool RayVsModel(const Ray& ray, } bool RayVsModel(const Ray& ray, - const RawModel::Vertex* modelVertices, + const std::vector& modelVertices, const std::vector& modelIndices, const glm::mat4& modelMatrix, glm::vec3& outHitPosition) @@ -548,7 +548,7 @@ BoxTriRes AABBvsTriangle(const AABB& box, } Output AABBvsTriangles(const AABB& box, - const RawModel::Vertex* modelVertices, + const std::vector& modelVertices, const std::vector& modelIndices, const glm::mat4& modelMatrix, glm::vec3& boxVelocity, @@ -565,9 +565,9 @@ Output AABBvsTriangles(const AABB& box, glm::vec3 originalBoxVelocity(boxVelocity); for (int i = 0; i < modelIndices.size(); ) { std::array triVertices = { - TransformSystem::TransformPoint(modelVertices[modelIndices[i++]].Position, modelMatrix), - TransformSystem::TransformPoint(modelVertices[modelIndices[i++]].Position, modelMatrix), - TransformSystem::TransformPoint(modelVertices[modelIndices[i++]].Position, modelMatrix) + TransformSystem::TransformPoint(modelVertices[modelIndices[i++]], modelMatrix), + TransformSystem::TransformPoint(modelVertices[modelIndices[i++]], modelMatrix), + TransformSystem::TransformPoint(modelVertices[modelIndices[i++]], modelMatrix) }; glm::vec3 outVec; bool collideWithGround = isOnGround; @@ -595,7 +595,7 @@ Output AABBvsTriangles(const AABB& box, } bool AABBvsTriangles(const AABB& box, - const RawModel::Vertex* modelVertices, + const std::vector& modelVertices, const std::vector& modelIndices, const glm::mat4& modelMatrix, glm::vec3& boxVelocity, @@ -615,7 +615,7 @@ bool AABBvsTriangles(const AABB& box, } bool AABBvsTriangles(const AABB& box, - const RawModel::Vertex* modelVertices, + const std::vector& modelVertices, const std::vector& modelIndices, const glm::mat4& modelMatrix) { @@ -633,7 +633,7 @@ bool AABBvsTriangles(const AABB& box, } Output AABBvsTrianglesWContainment(const AABB& box, - const RawModel::Vertex* modelVertices, + const std::vector& modelVertices, const std::vector& modelIndices, const glm::mat4& modelMatrix) { @@ -741,7 +741,7 @@ boost::optional EntityFirstHitByRay(const Ray& ray, std::vectorVertices(), model->m_RawModel->m_Indices, TransformSystem::ModelMatrix(entityBox.Entity), outDistance, u, v)) { + if (RayVsModel(ray, model->m_Vertices, model->m_Indices, TransformSystem::ModelMatrix(entityBox.Entity), outDistance, u, v)) { outIntersectPos = ray.Origin() + outDistance * ray.Direction(); return entityBox; } diff --git a/src/Engine/Collision/CollisionSystem.cpp b/src/Engine/Collision/CollisionSystem.cpp index 433af482..e53143ac 100644 --- a/src/Engine/Collision/CollisionSystem.cpp +++ b/src/Engine/Collision/CollisionSystem.cpp @@ -41,15 +41,15 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c // Don't collide against invisible models. continue; } - RawModel* model; + Model* model; std::string res = (std::string)boxB.Entity["Model"]["Resource"]; try { - model = ResourceManager::Load(res); + model = ResourceManager::Load(res); } catch (const std::exception&) { continue; } float u, v; - hit = Collision::RayVsModel(ray, model->Vertices(), model->m_Indices, TransformSystem::ModelMatrix(boxB.Entity), dist, u, v); + hit = Collision::RayVsModel(ray, model->m_Vertices, model->m_Indices, TransformSystem::ModelMatrix(boxB.Entity), dist, u, v); } else { hit = Collision::RayVsAABB(ray, boxB, dist); } @@ -86,9 +86,9 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c // Don't collide against invisible models. continue; } - RawModel* model; + Model* model; try { - model = ResourceManager::Load(boxB.Entity["Model"]["Resource"]); + model = ResourceManager::Load(boxB.Entity["Model"]["Resource"]); } catch (const std::exception&) { continue; } @@ -98,7 +98,7 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c glm::vec3 inOutVelocity = (glm::vec3)cPhysics["Velocity"]; bool isOnGround = (bool)cPhysics["IsOnGround"]; float verticalStepHeight = (float)(double)cPhysics["VerticalStepHeight"]; - if (Collision::AABBvsTriangles(boxA, model->Vertices(), model->m_Indices, modelMatrix, inOutVelocity, verticalStepHeight, isOnGround, resolutionVector)) { + if (Collision::AABBvsTriangles(boxA, model->m_Vertices, model->m_Indices, modelMatrix, inOutVelocity, verticalStepHeight, isOnGround, resolutionVector)) { //Move the position to previous position if it is not moving in the xz-plane, else resolve with the resolution vector. (Field)cTransform["Position"] += resolutionVector; boxA = *Collision::EntityAbsoluteAABB(entity); diff --git a/src/Engine/Collision/TriggerSystem.cpp b/src/Engine/Collision/TriggerSystem.cpp index c9bc18dc..ef790bc9 100644 --- a/src/Engine/Collision/TriggerSystem.cpp +++ b/src/Engine/Collision/TriggerSystem.cpp @@ -10,11 +10,11 @@ void TriggerSystem::UpdateComponent(EntityWrapper& triggerEntity, ComponentWrapp return; } - RawModel* triggerModel = nullptr; + Model* triggerModel = nullptr; glm::mat4 triggerModelMat; if (triggerEntity.HasComponent("Model")) { try { - triggerModel = ResourceManager::Load(triggerEntity["Model"]["Resource"]); + triggerModel = ResourceManager::Load(triggerEntity["Model"]["Resource"]); triggerModelMat = TransformSystem::ModelMatrix(triggerEntity); } catch (const std::exception&) { } @@ -38,7 +38,7 @@ void TriggerSystem::UpdateComponent(EntityWrapper& triggerEntity, ComponentWrapp ? Collision::Output::OutContained : Collision::AABBvsTrianglesWContainment( colliderBox, - triggerModel->Vertices(), + triggerModel->m_Vertices, triggerModel->m_Indices, triggerModelMat); diff --git a/src/Engine/Rendering/AnimationSystem.cpp b/src/Engine/Rendering/AnimationSystem.cpp index 729ed672..63b47625 100644 --- a/src/Engine/Rendering/AnimationSystem.cpp +++ b/src/Engine/Rendering/AnimationSystem.cpp @@ -37,7 +37,7 @@ void AnimationSystem::CreateBlendTrees() continue;; } - Skeleton* skeleton = model->m_RawModel->m_Skeleton; + Skeleton* skeleton = model->m_Skeleton; if (skeleton == nullptr) { continue; } @@ -79,7 +79,7 @@ void AnimationSystem::UpdateAnimations(double dt) return; } - Skeleton* skeleton = model->m_RawModel->m_Skeleton; + Skeleton* skeleton = model->m_Skeleton; if (skeleton == nullptr) { return; } @@ -175,7 +175,7 @@ bool AnimationSystem::OnAutoAnimationBlend(Events::AutoAnimationBlend& e) return false; } - Skeleton* skeleton = model->m_RawModel->m_Skeleton; + Skeleton* skeleton = model->m_Skeleton; if (skeleton == nullptr) { return false; } @@ -294,7 +294,7 @@ bool AnimationSystem::OnEntityDeleted(Events::EntityDeleted& e) return false; } - Skeleton* skeleton = model->m_RawModel->m_Skeleton; + Skeleton* skeleton = model->m_Skeleton; if (skeleton == nullptr) { return false; } @@ -325,7 +325,7 @@ bool AnimationSystem::OnSetBlendWeight(Events::SetBlendWeight& e) return false; } - Skeleton* skeleton = model->m_RawModel->m_Skeleton; + Skeleton* skeleton = model->m_Skeleton; if (skeleton == nullptr) { return false; } diff --git a/src/Engine/Rendering/AutoBlendQueue.cpp b/src/Engine/Rendering/AutoBlendQueue.cpp index f4a854ec..016ae420 100644 --- a/src/Engine/Rendering/AutoBlendQueue.cpp +++ b/src/Engine/Rendering/AutoBlendQueue.cpp @@ -22,7 +22,7 @@ void AutoBlendQueue::Insert(AutoBlendJob autoBlendJob) return; } - Skeleton* skeleton = model->m_RawModel->m_Skeleton; + Skeleton* skeleton = model->m_Skeleton; if (skeleton == nullptr) { return; } @@ -130,7 +130,7 @@ bool AutoBlendQueue::HasActiveBlendJob() return HasActiveBlendJob(); } - Skeleton* skeleton = model->m_RawModel->m_Skeleton; + Skeleton* skeleton = model->m_Skeleton; if (skeleton == nullptr) { m_BlendQueue.pop_front(); return HasActiveBlendJob(); @@ -173,7 +173,7 @@ std::shared_ptr AutoBlendQueue::GetBlendTree() return nullptr; } - Skeleton* skeleton = model->m_RawModel->m_Skeleton; + Skeleton* skeleton = model->m_Skeleton; if (skeleton == nullptr) { return nullptr; } diff --git a/src/Engine/Rendering/BoneAttachmentSystem.cpp b/src/Engine/Rendering/BoneAttachmentSystem.cpp index 440bb65c..82fd9507 100644 --- a/src/Engine/Rendering/BoneAttachmentSystem.cpp +++ b/src/Engine/Rendering/BoneAttachmentSystem.cpp @@ -26,7 +26,7 @@ void BoneAttachmentSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapp return; } - Skeleton* skeleton = model->m_RawModel->m_Skeleton; + Skeleton* skeleton = model->m_Skeleton; if(skeleton == nullptr) { return; diff --git a/src/Engine/Rendering/DrawFinalPass.cpp b/src/Engine/Rendering/DrawFinalPass.cpp index 6ab4bb3c..d75898ea 100644 --- a/src/Engine/Rendering/DrawFinalPass.cpp +++ b/src/Engine/Rendering/DrawFinalPass.cpp @@ -530,7 +530,7 @@ void DrawFinalPass::DrawModelRenderQueues(std::list>& std::vector frameBones; if (modelJob->BlendTree != nullptr) { frameBones = modelJob->BlendTree->GetFinalPose(); - } else { + } else if (modelJob->Skeleton != nullptr) { frameBones = modelJob->Skeleton->GetTPose(); } glUniformMatrix4fv(glGetUniformLocation(forwardSkinnedHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); diff --git a/src/Engine/Rendering/Model.cpp b/src/Engine/Rendering/Model.cpp index d34ce809..fce9d32f 100644 --- a/src/Engine/Rendering/Model.cpp +++ b/src/Engine/Rendering/Model.cpp @@ -2,8 +2,10 @@ Model::Model(std::string fileName) { + //fileName = "Models/Core/ScreenQuad.mesh"; //Try loading the model asyncronously, if it throws any exceptions then let it propagate back to caller. - m_RawModel = ResourceManager::Load(fileName); + auto m_RawModel = ResourceManager::Load(fileName); + //throw FailedLoadingException("Test"); for (auto& materialProperty : m_RawModel->m_Materials) { switch (materialProperty.type) { @@ -99,14 +101,24 @@ Model::Model(std::string fileName) glm::vec3 mini(INFINITY); glm::vec3 maxi(-INFINITY); - for (unsigned int i = 0; i < m_RawModel->NumVertices(); i++) { const auto& v = m_RawModel->Vertices()[i]; mini = glm::min(mini, v.Position); maxi = glm::max(maxi, v.Position); } - m_Box = AABB(mini, maxi); + + //m_Skeleton = m_RawModel->m_Skeleton; + delete m_RawModel->m_Skeleton; + m_Materials = m_RawModel->m_Materials; + //m_Indices = m_RawModel->m_Indices; + m_IsSkinned = m_RawModel->IsSkinned(); + // Copy vertex positions for collisions later + for (auto& v : m_RawModel->m_Vertices) { + //m_Vertices.push_back(v.Position); + } + + ResourceManager::Release("RawModel", fileName); } Model::~Model() diff --git a/src/Engine/Rendering/PickingPass.cpp b/src/Engine/Rendering/PickingPass.cpp index 820c184e..c3b6bdd4 100644 --- a/src/Engine/Rendering/PickingPass.cpp +++ b/src/Engine/Rendering/PickingPass.cpp @@ -110,7 +110,7 @@ void PickingPass::Draw(RenderScene& scene) std::vector frameBones; if (modelJob->BlendTree != nullptr) { frameBones = modelJob->BlendTree->GetFinalPose(); - } else { + } else if (modelJob->Skeleton != nullptr) { frameBones = modelJob->Skeleton->GetTPose(); } glUniformMatrix4fv(glGetUniformLocation(shaderSkinnedHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); diff --git a/src/Engine/Rendering/RawModelCustom.cpp b/src/Engine/Rendering/RawModelCustom.cpp index 94a824f0..8ea3e2b1 100644 --- a/src/Engine/Rendering/RawModelCustom.cpp +++ b/src/Engine/Rendering/RawModelCustom.cpp @@ -493,12 +493,13 @@ void RawModelCustom::ReadAnimationKeyFrame(std::size_t& offset, char* fileData, RawModelCustom::~RawModelCustom() { - if (m_Skeleton != nullptr) { - delete m_Skeleton; - } - for (auto material : m_Materials) { - delete material.material; - } + // Ownership of skeleton and materials get transferred to Model + // if (m_Skeleton != nullptr) { + // delete m_Skeleton; + // } + //for (auto material : m_Materials) { + // delete material.material; + //} } #endif \ No newline at end of file diff --git a/src/Engine/Rendering/ShadowPass.cpp b/src/Engine/Rendering/ShadowPass.cpp index ed9ccb7b..0258f8b6 100644 --- a/src/Engine/Rendering/ShadowPass.cpp +++ b/src/Engine/Rendering/ShadowPass.cpp @@ -270,7 +270,7 @@ void ShadowPass::Draw(RenderScene & scene) std::vector frameBones; if (modelJob->BlendTree != nullptr) { frameBones = modelJob->BlendTree->GetFinalPose(); - } else { + } else if (modelJob->Skeleton != nullptr) { frameBones = modelJob->Skeleton->GetTPose(); } glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); diff --git a/src/Game/Systems/SpawnerSystem.cpp b/src/Game/Systems/SpawnerSystem.cpp index 825c9e99..583cc91b 100644 --- a/src/Game/Systems/SpawnerSystem.cpp +++ b/src/Game/Systems/SpawnerSystem.cpp @@ -103,15 +103,15 @@ bool SpawnerSystem::spawnedEntityIsColliding(EntityWrapper spawnedEntity, Entity if (!spawnedBox.Entity.HasComponent("Model")) { return true; } - RawModel* model = nullptr; + Model* model = nullptr; try { - model = ResourceManager::Load(otherEntity["Model"]["Resource"]); + model = ResourceManager::Load(otherEntity["Model"]["Resource"]); } catch (const std::exception&) { } if (model != nullptr && Collision::AABBvsTriangles( spawnedBox, - model->Vertices(), + model->m_Vertices, model->m_Indices, TransformSystem::ModelMatrix(otherEntity))) { return true;