parent
31c6f0db92
commit
b5d65bf94e
|
@ -1,22 +1,21 @@
|
||||||
#include "ModelLoader.h"
|
#include "ModelLoader.h"
|
||||||
|
|
||||||
ModelLoader::ModelLoader() :
|
ModelLoader::ModelLoader() :
|
||||||
dev(nullptr),
|
dev(nullptr),
|
||||||
devcon(nullptr),
|
devcon(nullptr),
|
||||||
meshes(),
|
meshes(),
|
||||||
directory(),
|
directory(),
|
||||||
textures_loaded(),
|
textures_loaded(),
|
||||||
hwnd(nullptr)
|
hwnd(nullptr) {
|
||||||
{
|
// empty
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ModelLoader::~ModelLoader()
|
ModelLoader::~ModelLoader() {
|
||||||
{
|
// empty
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ModelLoader::Load(HWND hwnd, ID3D11Device * dev, ID3D11DeviceContext * devcon, std::string filename)
|
bool ModelLoader::Load(HWND hwnd, ID3D11Device * dev, ID3D11DeviceContext * devcon, std::string filename) {
|
||||||
{
|
|
||||||
Assimp::Importer importer;
|
Assimp::Importer importer;
|
||||||
|
|
||||||
const aiScene* pScene = importer.ReadFile(filename,
|
const aiScene* pScene = importer.ReadFile(filename,
|
||||||
|
@ -37,41 +36,37 @@ bool ModelLoader::Load(HWND hwnd, ID3D11Device * dev, ID3D11DeviceContext * devc
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModelLoader::Draw(ID3D11DeviceContext * devcon)
|
void ModelLoader::Draw(ID3D11DeviceContext * devcon) {
|
||||||
{
|
for (int i = 0; i < meshes.size(); ++i ) {
|
||||||
for (int i = 0; i < meshes.size(); i++)
|
|
||||||
{
|
|
||||||
meshes[i].Draw(devcon);
|
meshes[i].Draw(devcon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
string textype;
|
string textype;
|
||||||
|
|
||||||
Mesh ModelLoader::processMesh(aiMesh * mesh, const aiScene * scene)
|
Mesh ModelLoader::processMesh(aiMesh * mesh, const aiScene * scene) {
|
||||||
{
|
|
||||||
// Data to fill
|
// Data to fill
|
||||||
vector<VERTEX> vertices;
|
vector<VERTEX> vertices;
|
||||||
vector<UINT> indices;
|
vector<UINT> indices;
|
||||||
vector<Texture> textures;
|
vector<Texture> textures;
|
||||||
|
|
||||||
if (mesh->mMaterialIndex >= 0)
|
if (mesh->mMaterialIndex >= 0) {
|
||||||
{
|
|
||||||
aiMaterial* mat = scene->mMaterials[mesh->mMaterialIndex];
|
aiMaterial* mat = scene->mMaterials[mesh->mMaterialIndex];
|
||||||
|
|
||||||
if (textype.empty()) textype = determineTextureType(scene, mat);
|
if (textype.empty()) {
|
||||||
|
textype = determineTextureType(scene, mat);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Walk through each of the mesh's vertices
|
// Walk through each of the mesh's vertices
|
||||||
for (UINT i = 0; i < mesh->mNumVertices; i++)
|
for (UINT i = 0; i < mesh->mNumVertices; i++) {
|
||||||
{
|
|
||||||
VERTEX vertex;
|
VERTEX vertex;
|
||||||
|
|
||||||
vertex.X = mesh->mVertices[i].x;
|
vertex.X = mesh->mVertices[i].x;
|
||||||
vertex.Y = mesh->mVertices[i].y;
|
vertex.Y = mesh->mVertices[i].y;
|
||||||
vertex.Z = mesh->mVertices[i].z;
|
vertex.Z = mesh->mVertices[i].z;
|
||||||
|
|
||||||
if (mesh->mTextureCoords[0])
|
if (mesh->mTextureCoords[0]) {
|
||||||
{
|
|
||||||
vertex.texcoord.x = (float)mesh->mTextureCoords[0][i].x;
|
vertex.texcoord.x = (float)mesh->mTextureCoords[0][i].x;
|
||||||
vertex.texcoord.y = (float)mesh->mTextureCoords[0][i].y;
|
vertex.texcoord.y = (float)mesh->mTextureCoords[0][i].y;
|
||||||
}
|
}
|
||||||
|
@ -79,16 +74,14 @@ Mesh ModelLoader::processMesh(aiMesh * mesh, const aiScene * scene)
|
||||||
vertices.push_back(vertex);
|
vertices.push_back(vertex);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (UINT i = 0; i < mesh->mNumFaces; i++)
|
for (UINT i = 0; i < mesh->mNumFaces; i++) {
|
||||||
{
|
|
||||||
aiFace face = mesh->mFaces[i];
|
aiFace face = mesh->mFaces[i];
|
||||||
|
|
||||||
for (UINT j = 0; j < face.mNumIndices; j++)
|
for (UINT j = 0; j < face.mNumIndices; j++)
|
||||||
indices.push_back(face.mIndices[j]);
|
indices.push_back(face.mIndices[j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mesh->mMaterialIndex >= 0)
|
if (mesh->mMaterialIndex >= 0) {
|
||||||
{
|
|
||||||
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
|
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
|
||||||
|
|
||||||
vector<Texture> diffuseMaps = this->loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse", scene);
|
vector<Texture> diffuseMaps = this->loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse", scene);
|
||||||
|
@ -98,35 +91,27 @@ Mesh ModelLoader::processMesh(aiMesh * mesh, const aiScene * scene)
|
||||||
return Mesh(dev, vertices, indices, textures);
|
return Mesh(dev, vertices, indices, textures);
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<Texture> ModelLoader::loadMaterialTextures(aiMaterial * mat, aiTextureType type, string typeName, const aiScene * scene)
|
vector<Texture> ModelLoader::loadMaterialTextures(aiMaterial * mat, aiTextureType type, string typeName, const aiScene * scene) {
|
||||||
{
|
|
||||||
vector<Texture> textures;
|
vector<Texture> textures;
|
||||||
for (UINT i = 0; i < mat->GetTextureCount(type); i++)
|
for (UINT i = 0; i < mat->GetTextureCount(type); i++) {
|
||||||
{
|
|
||||||
aiString str;
|
aiString str;
|
||||||
mat->GetTexture(type, i, &str);
|
mat->GetTexture(type, i, &str);
|
||||||
// Check if texture was loaded before and if so, continue to next iteration: skip loading a new texture
|
// Check if texture was loaded before and if so, continue to next iteration: skip loading a new texture
|
||||||
bool skip = false;
|
bool skip = false;
|
||||||
for (UINT j = 0; j < textures_loaded.size(); j++)
|
for (UINT j = 0; j < textures_loaded.size(); j++) {
|
||||||
{
|
if (std::strcmp(textures_loaded[j].path.c_str(), str.C_Str()) == 0) {
|
||||||
if (std::strcmp(textures_loaded[j].path.c_str(), str.C_Str()) == 0)
|
|
||||||
{
|
|
||||||
textures.push_back(textures_loaded[j]);
|
textures.push_back(textures_loaded[j]);
|
||||||
skip = true; // A texture with the same filepath has already been loaded, continue to next one. (optimization)
|
skip = true; // A texture with the same filepath has already been loaded, continue to next one. (optimization)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!skip)
|
if (!skip) { // If texture hasn't been loaded already, load it
|
||||||
{ // If texture hasn't been loaded already, load it
|
|
||||||
HRESULT hr;
|
HRESULT hr;
|
||||||
Texture texture;
|
Texture texture;
|
||||||
if (textype == "embedded compressed texture")
|
if (textype == "embedded compressed texture") {
|
||||||
{
|
|
||||||
int textureindex = getTextureIndex(&str);
|
int textureindex = getTextureIndex(&str);
|
||||||
texture.texture = getTextureFromModel(scene, textureindex);
|
texture.texture = getTextureFromModel(scene, textureindex);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
string filename = string(str.C_Str());
|
string filename = string(str.C_Str());
|
||||||
filename = directory + '/' + filename;
|
filename = directory + '/' + filename;
|
||||||
wstring filenamews = wstring(filename.begin(), filename.end());
|
wstring filenamews = wstring(filename.begin(), filename.end());
|
||||||
|
@ -143,65 +128,52 @@ vector<Texture> ModelLoader::loadMaterialTextures(aiMaterial * mat, aiTextureTyp
|
||||||
return textures;
|
return textures;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModelLoader::Close()
|
void ModelLoader::Close() {
|
||||||
{
|
|
||||||
for (auto& t : textures_loaded)
|
for (auto& t : textures_loaded)
|
||||||
t.Release();
|
t.Release();
|
||||||
|
|
||||||
for (int i = 0; i < meshes.size(); i++)
|
for (int i = 0; i < meshes.size(); i++) {
|
||||||
{
|
|
||||||
meshes[i].Close();
|
meshes[i].Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModelLoader::processNode(aiNode * node, const aiScene * scene)
|
void ModelLoader::processNode(aiNode * node, const aiScene * scene) {
|
||||||
{
|
for (UINT i = 0; i < node->mNumMeshes; i++) {
|
||||||
for (UINT i = 0; i < node->mNumMeshes; i++)
|
|
||||||
{
|
|
||||||
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
|
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
|
||||||
meshes.push_back(this->processMesh(mesh, scene));
|
meshes.push_back(this->processMesh(mesh, scene));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (UINT i = 0; i < node->mNumChildren; i++)
|
for (UINT i = 0; i < node->mNumChildren; i++) {
|
||||||
{
|
|
||||||
this->processNode(node->mChildren[i], scene);
|
this->processNode(node->mChildren[i], scene);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
string ModelLoader::determineTextureType(const aiScene * scene, aiMaterial * mat)
|
string ModelLoader::determineTextureType(const aiScene * scene, aiMaterial * mat) {
|
||||||
{
|
|
||||||
aiString textypeStr;
|
aiString textypeStr;
|
||||||
mat->GetTexture(aiTextureType_DIFFUSE, 0, &textypeStr);
|
mat->GetTexture(aiTextureType_DIFFUSE, 0, &textypeStr);
|
||||||
string textypeteststr = textypeStr.C_Str();
|
string textypeteststr = textypeStr.C_Str();
|
||||||
if (textypeteststr == "*0" || textypeteststr == "*1" || textypeteststr == "*2" || textypeteststr == "*3" || textypeteststr == "*4" || textypeteststr == "*5")
|
if (textypeteststr == "*0" || textypeteststr == "*1" || textypeteststr == "*2" || textypeteststr == "*3" || textypeteststr == "*4" || textypeteststr == "*5") {
|
||||||
{
|
if (scene->mTextures[0]->mHeight == 0) {
|
||||||
if (scene->mTextures[0]->mHeight == 0)
|
|
||||||
{
|
|
||||||
return "embedded compressed texture";
|
return "embedded compressed texture";
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return "embedded non-compressed texture";
|
return "embedded non-compressed texture";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (textypeteststr.find('.') != string::npos)
|
if (textypeteststr.find('.') != string::npos) {
|
||||||
{
|
|
||||||
return "textures are on disk";
|
return "textures are on disk";
|
||||||
}
|
}
|
||||||
|
|
||||||
return ".";
|
return ".";
|
||||||
}
|
}
|
||||||
|
|
||||||
int ModelLoader::getTextureIndex(aiString * str)
|
int ModelLoader::getTextureIndex(aiString * str) {
|
||||||
{
|
|
||||||
string tistr;
|
string tistr;
|
||||||
tistr = str->C_Str();
|
tistr = str->C_Str();
|
||||||
tistr = tistr.substr(1);
|
tistr = tistr.substr(1);
|
||||||
return stoi(tistr);
|
return stoi(tistr);
|
||||||
}
|
}
|
||||||
|
|
||||||
ID3D11ShaderResourceView * ModelLoader::getTextureFromModel(const aiScene * scene, int textureindex)
|
ID3D11ShaderResourceView * ModelLoader::getTextureFromModel(const aiScene * scene, int textureindex) {
|
||||||
{
|
|
||||||
HRESULT hr;
|
HRESULT hr;
|
||||||
ID3D11ShaderResourceView *texture;
|
ID3D11ShaderResourceView *texture;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue