Any collision meshes we have should be working, and RawModels should be released after being loaded.

This commit is contained in:
William Moberg
2016-03-14 03:20:36 +01:00
parent f632b45c9b
commit 4c52f55bf0
4 changed files with 38 additions and 50 deletions
-6
View File
@@ -17,13 +17,7 @@ private:
public: public:
~Model(); ~Model();
const std::vector<RawModel::MaterialProperties>& MaterialGroups() const { return m_Materials; } const std::vector<RawModel::MaterialProperties>& MaterialGroups() const { return m_Materials; }
//const RawModel::Vertex* Vertices() const { return m_RawModel->Vertices(); }
unsigned int NumberOfVertices() const { return m_Vertices.size(); } unsigned int NumberOfVertices() const { return m_Vertices.size(); }
//size_t NumberOfVertices() const { return m_RawModel->NumVertices(); }
//const std::vector<unsigned int>& Indices() const { return m_RawModel->Indices(); }
//const RawModel::Vertex* CollisionVertices() const { return m_RawModel->CollisionVertices(); }
//const std::vector<unsigned int>& CollisionIndices() const { return m_RawModel->CollisionIndices(); }
const AABB& Box() const { return m_Box; } const AABB& Box() const { return m_Box; }
bool IsSkinned() const { return m_IsSkinned; } bool IsSkinned() const { return m_IsSkinned; }
+3 -6
View File
@@ -30,9 +30,6 @@ protected:
public: public:
~RawModelCustom(); ~RawModelCustom();
struct Vertex {
glm::vec3 Position;
};
struct RenderVertex { struct RenderVertex {
glm::vec3 Position; glm::vec3 Position;
@@ -121,7 +118,7 @@ public:
bool IsSkinned() const { return hasSkin; }; bool IsSkinned() const { return hasSkin; };
const Vertex* CollisionVertices(); const std::vector<glm::vec3>& CollisionVertices();
size_t NumCollisionVertices() const { size_t NumCollisionVertices() const {
return m_CollisionVertices.size(); return m_CollisionVertices.size();
@@ -146,7 +143,7 @@ private:
bool hasCollisionMesh = false; bool hasCollisionMesh = false;
std::vector<unsigned int> m_Indices; std::vector<unsigned int> m_Indices;
std::vector<unsigned int> m_CollisionIndices; std::vector<unsigned int> m_CollisionIndices;
std::vector<Vertex> m_CollisionVertices; std::vector<glm::vec3> m_CollisionVertices;
std::vector<RenderVertex> m_Vertices; std::vector<RenderVertex> m_Vertices;
std::vector<SkinedVertex> m_SkinedVertices; std::vector<SkinedVertex> m_SkinedVertices;
@@ -175,7 +172,7 @@ private:
void ReadCollisionFile(std::string filePath); void ReadCollisionFile(std::string filePath);
void ReadCollisionFileData(std::size_t& offset, char* fileData, const unsigned int& fileByteSize); void ReadCollisionFileData(std::size_t& offset, char* fileData, const unsigned int& fileByteSize);
const Vertex* ConstructCollisionList(); const std::vector<glm::vec3>& ConstructCollisionList();
//void CreateSkeleton(std::vector<std::tuple<std::string, glm::mat4>> &boneInfo, std::map<std::string, int> &boneNameMapping, aiNode* node, int parentID); //void CreateSkeleton(std::vector<std::tuple<std::string, glm::mat4>> &boneInfo, std::map<std::string, int> &boneNameMapping, aiNode* node, int parentID);
}; };
+20 -20
View File
@@ -2,12 +2,10 @@
Model::Model(std::string fileName) 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. //Try loading the model asyncronously, if it throws any exceptions then let it propagate back to caller.
auto m_RawModel = ResourceManager::Load<RawModel, true>(fileName); auto rawModel = ResourceManager::Load<RawModel, true>(fileName);
//throw FailedLoadingException("Test");
for (auto& materialProperty : m_RawModel->m_Materials) { for (auto& materialProperty : rawModel->m_Materials) {
switch (materialProperty.type) { switch (materialProperty.type) {
case RawModel::MaterialType::SingleTextures: case RawModel::MaterialType::SingleTextures:
{ {
@@ -48,11 +46,11 @@ Model::Model(std::string fileName)
glGenBuffers(1, &buffer); glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, 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); glGenBuffers(1, &ElementBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 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); glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO); glBindVertexArray(VAO);
@@ -60,7 +58,7 @@ Model::Model(std::string fileName)
glBindBuffer(GL_ARRAY_BUFFER, buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer);
std::vector<int> structSizes; std::vector<int> structSizes;
if (m_RawModel->IsSkinned()) { if (rawModel->IsSkinned()) {
structSizes = { 3, 3, 3, 3, 2, 4, 4 }; structSizes = { 3, 3, 3, 3, 2, 4, 4 };
} else { } else {
structSizes = { 3, 3, 3, 3, 2 }; 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++; 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++;
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(2);
glEnableVertexAttribArray(3); glEnableVertexAttribArray(3);
glEnableVertexAttribArray(4); glEnableVertexAttribArray(4);
if (m_RawModel->IsSkinned()) { if (rawModel->IsSkinned()) {
glEnableVertexAttribArray(5); glEnableVertexAttribArray(5);
glEnableVertexAttribArray(6); glEnableVertexAttribArray(6);
} }
@@ -101,27 +99,29 @@ Model::Model(std::string fileName)
glm::vec3 mini(INFINITY); glm::vec3 mini(INFINITY);
glm::vec3 maxi(-INFINITY); glm::vec3 maxi(-INFINITY);
for (unsigned int i = 0; i < m_RawModel->NumVertices(); i++) { for (unsigned int i = 0; i < rawModel->NumVertices(); i++) {
const auto& v = m_RawModel->Vertices()[i]; const auto& v = rawModel->Vertices()[i];
mini = glm::min(mini, v.Position); mini = glm::min(mini, v.Position);
maxi = glm::max(maxi, v.Position); maxi = glm::max(maxi, v.Position);
} }
m_Box = AABB(mini, maxi); m_Box = AABB(mini, maxi);
//m_Skeleton = m_RawModel->m_Skeleton; m_Skeleton = rawModel->m_Skeleton;
delete m_RawModel->m_Skeleton; m_Materials = rawModel->m_Materials;
m_Materials = m_RawModel->m_Materials; m_Indices = rawModel->CollisionIndices();
//m_Indices = m_RawModel->m_Indices; m_IsSkinned = rawModel->IsSkinned();
m_IsSkinned = m_RawModel->IsSkinned();
// Copy vertex positions for collisions later // Copy vertex positions for collisions later
for (auto& v : m_RawModel->m_Vertices) { m_Vertices = rawModel->CollisionVertices();
//m_Vertices.push_back(v.Position);
}
ResourceManager::Release("RawModel", fileName); ResourceManager::Release("RawModel", fileName);
} }
Model::~Model() Model::~Model()
{ {
if (m_Skeleton != nullptr) {
delete m_Skeleton;
}
for (auto material : m_Materials) {
delete material.material;
}
} }
+15 -18
View File
@@ -515,12 +515,14 @@ void RawModelCustom::ReadCollisionFile(std::string filePath) {
delete[] fileData; delete[] fileData;
} }
const RawModelCustom::Vertex* RawModelCustom::CollisionVertices() { const std::vector<glm::vec3>& RawModelCustom::CollisionVertices() {
if (hasCollisionMesh) { if (hasCollisionMesh) {
return m_CollisionVertices.data(); return m_CollisionVertices;
} }
else if (hasSkin) { else if (hasSkin) {
return nullptr; // We don't do collisions against skinned meshes now.
m_CollisionVertices = std::vector<glm::vec3>();
return m_CollisionVertices;
} }
else { else {
return ConstructCollisionList(); return ConstructCollisionList();
@@ -533,11 +535,11 @@ void RawModelCustom::ReadCollisionFileData(std::size_t& offset, char* fileData,
m_CollisionIndices.resize(static_cast<std::size_t>(*(unsigned int*)(fileData + offset))); m_CollisionIndices.resize(static_cast<std::size_t>(*(unsigned int*)(fileData + offset)));
offset += sizeof(unsigned int); 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"); throw Resource::FailedLoadingException("Reading collision vertices failed");
} }
memcpy(&m_CollisionVertices[0], fileData + offset, m_CollisionVertices.size() * sizeof(Vertex)); memcpy(&m_CollisionVertices[0], fileData + offset, m_CollisionVertices.size() * sizeof(glm::vec3));
offset += m_CollisionVertices.size() * sizeof(Vertex); offset += m_CollisionVertices.size() * sizeof(glm::vec3);
if (offset + m_CollisionIndices.size() * sizeof(unsigned int) > fileByteSize) { if (offset + m_CollisionIndices.size() * sizeof(unsigned int) > fileByteSize) {
throw Resource::FailedLoadingException("Reading collision indices failed"); 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); offset += m_CollisionIndices.size() * sizeof(unsigned int);
} }
const RawModelCustom::Vertex* RawModelCustom::ConstructCollisionList() const std::vector<glm::vec3>& RawModelCustom::ConstructCollisionList()
{ {
if (!hasCollisionMesh && m_CollisionVertices.size() == 0) { if (!hasCollisionMesh && m_CollisionVertices.size() == 0) {
if (hasSkin) if (hasSkin) {
{
m_CollisionVertices.reserve(m_SkinedVertices.size()); m_CollisionVertices.reserve(m_SkinedVertices.size());
for (auto vertex : m_SkinedVertices) { for (const auto& vertex : m_SkinedVertices) {
Vertex tmp; m_CollisionVertices.push_back(vertex.Position);
tmp.Position = vertex.Position;
m_CollisionVertices.push_back(tmp);
} }
} else { } else {
m_CollisionVertices.reserve(m_Vertices.size()); m_CollisionVertices.reserve(m_Vertices.size());
for (auto vertex : m_Vertices) { for (const auto& vertex : m_Vertices) {
Vertex tmp; m_CollisionVertices.push_back(vertex.Position);
tmp.Position = vertex.Position;
m_CollisionVertices.push_back(tmp);
} }
} }
} }
return m_CollisionVertices.data(); return m_CollisionVertices;
} }
RawModelCustom::~RawModelCustom() RawModelCustom::~RawModelCustom()