-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.cpp
More file actions
114 lines (106 loc) · 3.76 KB
/
Copy pathmodel.cpp
File metadata and controls
114 lines (106 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "model.h"
void Model::loadModel(QString path)
{
Assimp::Importer import;
const aiScene *scene = import.ReadFile(path.toStdString(), aiProcess_Triangulate | aiProcess_FlipUVs);
if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
qDebug() << "ERROR::ASSIMP::" << import.GetErrorString() << endl;
return;
}
directory = path.mid(0, path.lastIndexOf('/'));
processNode(scene->mRootNode, scene);
}
void Model::processNode(aiNode *node, const aiScene *scene)
{
for(unsigned int i = 0; i < node->mNumMeshes; i++)
{
aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(processMesh(mesh, scene));
}
for(unsigned int i = 0; i < node->mNumChildren; i++)
{
processNode(node->mChildren[i], scene);
}
}
Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene)
{
QVector<Vertex> vertices;
QVector<unsigned int> indices;
QVector<Texture> textures;
for(unsigned int i = 0; i < mesh->mNumVertices; i++)
{
Vertex vertex;
vertex.Position = QVector3D(mesh->mVertices[i].x,mesh->mVertices[i].y,mesh->mVertices[i].z);
vertex.Normal = QVector3D(mesh->mNormals[i].x,mesh->mNormals[i].y,mesh->mNormals[i].z);
if(mesh->mTextureCoords[0])//only process one texture
vertex.TexCoords = QVector2D(mesh->mTextureCoords[0][i].x,mesh->mTextureCoords[0][i].y);
else
vertex.TexCoords = QVector2D(0.0f,0.0f);
vertices.push_back(vertex);
}
for(unsigned int i = 0; i < mesh->mNumFaces; i++)
{
aiFace face = mesh->mFaces[i];
for(unsigned int j = 0; j < face.mNumIndices; j++)
indices.push_back(face.mIndices[j]);
}
if(mesh->mMaterialIndex >= 0)
{
aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex];
textures.append(loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse"));
textures.append(loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular"));
textures.append(loadMaterialTextures(material, aiTextureType_NORMALS, "texture_normal"));
textures.append(loadMaterialTextures(material, aiTextureType_HEIGHT, "texture_height"));
}
return Mesh(glFunc,vertices,indices, textures);
}
QVector<Texture> Model::loadMaterialTextures(aiMaterial *mat, aiTextureType type, QString typeName)
{
QVector<Texture> textures;
for(unsigned int i = 0; i < mat->GetTextureCount(type); i++)
{
aiString str;
mat->GetTexture(type, i, &str);
QString fileName(str.C_Str());
bool skip = false;
for(int j = 0; j < textures_loaded.size(); j++)
{
if(textures_loaded[j].path == fileName)
{
textures.push_back(textures_loaded[j]);
skip = true;
break;
}
}
if(!skip)
{
Texture texture;
texture.ptr = TextureFromFile(fileName, directory);
texture.type = typeName;
texture.path = fileName;
textures.push_back(texture);
textures_loaded.push_back(texture);
}
}
return textures;
}
QOpenGLTexture *Model::TextureFromFile(QString fileName, QString directory)
{
QString filePath = directory + '/' + fileName;
qDebug() << filePath;
QOpenGLTexture * res = new QOpenGLTexture(QOpenGLTexture::Target2D);
QImage img(filePath);
if (!img.isNull())
{
res->setData(img);
res->setWrapMode(QOpenGLTexture::Repeat);
res->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
res->setMagnificationFilter(QOpenGLTexture::Linear);
}
else
{
qDebug() << "Texture failed to load at path: " << filePath;
}
return res;
}