From 4c52f55bf077b1b2c7376271dd8dab6f826df6bc Mon Sep 17 00:00:00 2001 From: William Moberg Date: Mon, 14 Mar 2016 03:20:36 +0100 Subject: [PATCH] Any collision meshes we have should be working, and RawModels should be released after being loaded. --- include/Engine/Rendering/Model.h | 6 ---- include/Engine/Rendering/RawModelCustom.h | 9 ++--- src/Engine/Rendering/Model.cpp | 40 +++++++++++------------ src/Engine/Rendering/RawModelCustom.cpp | 33 +++++++++---------- 4 files changed, 38 insertions(+), 50 deletions(-) diff --git a/include/Engine/Rendering/Model.h b/include/Engine/Rendering/Model.h index 95afa931..acdf64db 100644 --- a/include/Engine/Rendering/Model.h +++ b/include/Engine/Rendering/Model.h @@ -17,13 +17,7 @@ private: public: ~Model(); 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(); } - //size_t NumberOfVertices() const { return m_RawModel->NumVertices(); } - //const std::vector& Indices() const { return m_RawModel->Indices(); } - - //const RawModel::Vertex* CollisionVertices() const { return m_RawModel->CollisionVertices(); } - //const std::vector& CollisionIndices() const { return m_RawModel->CollisionIndices(); } const AABB& Box() const { return m_Box; } bool IsSkinned() const { return m_IsSkinned; } diff --git a/include/Engine/Rendering/RawModelCustom.h b/include/Engine/Rendering/RawModelCustom.h index 8e476c7f..12e54d3f 100644 --- a/include/Engine/Rendering/RawModelCustom.h +++ b/include/Engine/Rendering/RawModelCustom.h @@ -30,9 +30,6 @@ protected: public: ~RawModelCustom(); - struct Vertex { - glm::vec3 Position; - }; struct RenderVertex { glm::vec3 Position; @@ -121,7 +118,7 @@ public: bool IsSkinned() const { return hasSkin; }; - const Vertex* CollisionVertices(); + const std::vector& CollisionVertices(); size_t NumCollisionVertices() const { return m_CollisionVertices.size(); @@ -146,7 +143,7 @@ private: bool hasCollisionMesh = false; std::vector m_Indices; std::vector m_CollisionIndices; - std::vector m_CollisionVertices; + std::vector m_CollisionVertices; std::vector m_Vertices; std::vector m_SkinedVertices; @@ -175,7 +172,7 @@ private: void ReadCollisionFile(std::string filePath); void ReadCollisionFileData(std::size_t& offset, char* fileData, const unsigned int& fileByteSize); - const Vertex* ConstructCollisionList(); + const std::vector& ConstructCollisionList(); //void CreateSkeleton(std::vector> &boneInfo, std::map &boneNameMapping, aiNode* node, int parentID); }; diff --git a/src/Engine/Rendering/Model.cpp b/src/Engine/Rendering/Model.cpp index fe6b7819..596e254b 100644 --- a/src/Engine/Rendering/Model.cpp +++ b/src/Engine/Rendering/Model.cpp @@ -2,12 +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. - auto m_RawModel = ResourceManager::Load(fileName); - //throw FailedLoadingException("Test"); + auto rawModel = ResourceManager::Load(fileName); - for (auto& materialProperty : m_RawModel->m_Materials) { + for (auto& materialProperty : rawModel->m_Materials) { switch (materialProperty.type) { case RawModel::MaterialType::SingleTextures: { @@ -48,11 +46,11 @@ Model::Model(std::string fileName) glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); - glBufferData(GL_ARRAY_BUFFER, m_RawModel->NumVertices() * m_RawModel->VertexSize(), m_RawModel->Vertices(), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, rawModel->NumVertices() * rawModel->VertexSize(), rawModel->Vertices(), GL_STATIC_DRAW); glGenBuffers(1, &ElementBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ElementBuffer); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_RawModel->Indices().size() * sizeof(unsigned int), m_RawModel->Indices().data(), GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, rawModel->Indices().size() * sizeof(unsigned int), rawModel->Indices().data(), GL_STATIC_DRAW); glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); @@ -60,7 +58,7 @@ Model::Model(std::string fileName) glBindBuffer(GL_ARRAY_BUFFER, buffer); std::vector structSizes; - if (m_RawModel->IsSkinned()) { + if (rawModel->IsSkinned()) { structSizes = { 3, 3, 3, 3, 2, 4, 4 }; } else { structSizes = { 3, 3, 3, 3, 2 }; @@ -79,7 +77,7 @@ Model::Model(std::string fileName) glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; - if (m_RawModel->IsSkinned()) { + if (rawModel->IsSkinned()) { glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; glVertexAttribPointer(element, structSizes[element], GL_FLOAT, GL_FALSE, stride, (GLvoid*)(sizeof(GLfloat) * (offset += structSizes[element - 1]))); element++; } @@ -91,7 +89,7 @@ Model::Model(std::string fileName) glEnableVertexAttribArray(2); glEnableVertexAttribArray(3); glEnableVertexAttribArray(4); - if (m_RawModel->IsSkinned()) { + if (rawModel->IsSkinned()) { glEnableVertexAttribArray(5); glEnableVertexAttribArray(6); } @@ -101,27 +99,29 @@ 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]; + for (unsigned int i = 0; i < rawModel->NumVertices(); i++) { + const auto& v = 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(); + m_Skeleton = rawModel->m_Skeleton; + m_Materials = rawModel->m_Materials; + m_Indices = rawModel->CollisionIndices(); + m_IsSkinned = rawModel->IsSkinned(); // Copy vertex positions for collisions later - for (auto& v : m_RawModel->m_Vertices) { - //m_Vertices.push_back(v.Position); - } + m_Vertices = rawModel->CollisionVertices(); ResourceManager::Release("RawModel", fileName); } Model::~Model() { - + if (m_Skeleton != nullptr) { + delete m_Skeleton; + } + for (auto material : m_Materials) { + delete material.material; + } } diff --git a/src/Engine/Rendering/RawModelCustom.cpp b/src/Engine/Rendering/RawModelCustom.cpp index 1f1dc4c0..2106a699 100644 --- a/src/Engine/Rendering/RawModelCustom.cpp +++ b/src/Engine/Rendering/RawModelCustom.cpp @@ -515,12 +515,14 @@ void RawModelCustom::ReadCollisionFile(std::string filePath) { delete[] fileData; } -const RawModelCustom::Vertex* RawModelCustom::CollisionVertices() { +const std::vector& RawModelCustom::CollisionVertices() { if (hasCollisionMesh) { - return m_CollisionVertices.data(); + return m_CollisionVertices; } else if (hasSkin) { - return nullptr; + // We don't do collisions against skinned meshes now. + m_CollisionVertices = std::vector(); + return m_CollisionVertices; } else { return ConstructCollisionList(); @@ -533,11 +535,11 @@ void RawModelCustom::ReadCollisionFileData(std::size_t& offset, char* fileData, m_CollisionIndices.resize(static_cast(*(unsigned int*)(fileData + offset))); offset += sizeof(unsigned int); - if (offset + m_CollisionVertices.size() * sizeof(Vertex) > fileByteSize) { + if (offset + m_CollisionVertices.size() * sizeof(glm::vec3) > fileByteSize) { throw Resource::FailedLoadingException("Reading collision vertices failed"); } - memcpy(&m_CollisionVertices[0], fileData + offset, m_CollisionVertices.size() * sizeof(Vertex)); - offset += m_CollisionVertices.size() * sizeof(Vertex); + memcpy(&m_CollisionVertices[0], fileData + offset, m_CollisionVertices.size() * sizeof(glm::vec3)); + offset += m_CollisionVertices.size() * sizeof(glm::vec3); if (offset + m_CollisionIndices.size() * sizeof(unsigned int) > fileByteSize) { throw Resource::FailedLoadingException("Reading collision indices failed"); @@ -546,28 +548,23 @@ void RawModelCustom::ReadCollisionFileData(std::size_t& offset, char* fileData, offset += m_CollisionIndices.size() * sizeof(unsigned int); } -const RawModelCustom::Vertex* RawModelCustom::ConstructCollisionList() +const std::vector& RawModelCustom::ConstructCollisionList() { if (!hasCollisionMesh && m_CollisionVertices.size() == 0) { - if (hasSkin) - { + if (hasSkin) { m_CollisionVertices.reserve(m_SkinedVertices.size()); - for (auto vertex : m_SkinedVertices) { - Vertex tmp; - tmp.Position = vertex.Position; - m_CollisionVertices.push_back(tmp); + for (const auto& vertex : m_SkinedVertices) { + m_CollisionVertices.push_back(vertex.Position); } } else { m_CollisionVertices.reserve(m_Vertices.size()); - for (auto vertex : m_Vertices) { - Vertex tmp; - tmp.Position = vertex.Position; - m_CollisionVertices.push_back(tmp); + for (const auto& vertex : m_Vertices) { + m_CollisionVertices.push_back(vertex.Position); } } } - return m_CollisionVertices.data(); + return m_CollisionVertices; } RawModelCustom::~RawModelCustom()