Model loading and texturing
This commit is contained in:
@@ -5,7 +5,9 @@ Mesh::Mesh() {
|
||||
}
|
||||
|
||||
Mesh::~Mesh() {
|
||||
delete material;
|
||||
if (material) {
|
||||
delete material;
|
||||
}
|
||||
}
|
||||
|
||||
Mesh::Mesh(VertexList vertices, IndexList indices) {
|
||||
@@ -38,8 +40,26 @@ void Mesh::SetupMesh() {
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
Mesh Mesh::FromFile(const char *filename) {
|
||||
return Mesh();
|
||||
Mesh* Mesh::FromFile(const char *filename) {
|
||||
// HOCUS: https://assimp-docs.readthedocs.io/en/latest/usage/use_the_lib.html
|
||||
Assimp::Importer importer;
|
||||
|
||||
const aiScene* scene = importer.ReadFile(
|
||||
filename, // Read the file
|
||||
aiProcess_Triangulate | // We only do triangles
|
||||
aiProcess_GenNormals | // We want normals precalculated
|
||||
aiProcess_GenUVCoords // We want UV mappings precalculated
|
||||
);
|
||||
|
||||
if (!scene) {
|
||||
std::cerr << importer.GetErrorString() << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
aiNode *root = scene->mRootNode;
|
||||
aiMesh *mesh_to_import = scene->mMeshes[root->mMeshes[0]];
|
||||
|
||||
return Util::aiMesh2Mesh(mesh_to_import);
|
||||
}
|
||||
|
||||
void Mesh::Draw() {
|
||||
|
||||
10
core/Mesh.h
10
core/Mesh.h
@@ -2,8 +2,16 @@
|
||||
#define MESH_H
|
||||
|
||||
#include <list>
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Thirdpart includes
|
||||
#include <assimp/Importer.hpp>
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
|
||||
#include "types.h"
|
||||
#include "Util.h"
|
||||
#include "Spatial.h"
|
||||
#include "Vertex.h"
|
||||
#include "Index.h"
|
||||
@@ -17,7 +25,7 @@ public:
|
||||
Mesh();
|
||||
~Mesh();
|
||||
Mesh(VertexList vertices, IndexList indices);
|
||||
static Mesh FromFile(const char *filename);
|
||||
static Mesh *FromFile(const char *filename);
|
||||
virtual bool IsDrawable() const {return true;}
|
||||
virtual void Draw();
|
||||
virtual void SetupMesh();
|
||||
|
||||
18
core/Util.cpp
Normal file
18
core/Util.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "Util.h"
|
||||
|
||||
Mesh *Util::aiMesh2Mesh(aiMesh *aimesh){
|
||||
Mesh *mymesh = new Mesh();
|
||||
for (int i = 0; i < aimesh->mNumVertices; i++) {
|
||||
aiVector3D aiPosition = aimesh->mVertices[i];
|
||||
aiVector3D aiUV = aimesh->mTextureCoords[0][i]; // TODO get ALL texture coords
|
||||
Vertex vertex(aiPosition.x, aiPosition.y, aiPosition.z, aiUV.x, aiUV.y);
|
||||
mymesh->vertices.push_back(vertex);
|
||||
}
|
||||
for (int i = 0; i < aimesh->mNumFaces; i++) {
|
||||
// We're importing triangulated meshes, so each face is three indices
|
||||
unsigned int *face = aimesh->mFaces[i].mIndices;
|
||||
Index index(face[0], face[1], face[2]);
|
||||
mymesh->indices.push_back(index);
|
||||
}
|
||||
return mymesh;
|
||||
}
|
||||
15
core/Util.h
Normal file
15
core/Util.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
|
||||
// Thirdparty includes
|
||||
#include <assimp/mesh.h>
|
||||
|
||||
// Gahhhhhh mutual inclusion!
|
||||
#include "Mesh.h"
|
||||
class Mesh;
|
||||
|
||||
namespace Util {
|
||||
Mesh *aiMesh2Mesh(aiMesh *mesh);
|
||||
}
|
||||
|
||||
#endif /* UTIL_H */
|
||||
Reference in New Issue
Block a user