2017-06-03 07:25:20 +00:00
|
|
|
#include "ModelLoader.h"
|
|
|
|
|
2021-07-29 11:28:51 +00:00
|
|
|
ModelLoader::ModelLoader() :
|
2020-04-03 13:43:01 +00:00
|
|
|
dev_(nullptr),
|
|
|
|
devcon_(nullptr),
|
|
|
|
meshes_(),
|
|
|
|
directory_(),
|
|
|
|
textures_loaded_(),
|
|
|
|
hwnd_(nullptr) {
|
2020-03-07 11:22:57 +00:00
|
|
|
// empty
|
2017-06-03 07:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-07 11:22:57 +00:00
|
|
|
ModelLoader::~ModelLoader() {
|
|
|
|
// empty
|
2017-06-03 07:25:20 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 11:22:57 +00:00
|
|
|
bool ModelLoader::Load(HWND hwnd, ID3D11Device * dev, ID3D11DeviceContext * devcon, std::string filename) {
|
2017-06-03 07:25:20 +00:00
|
|
|
Assimp::Importer importer;
|
|
|
|
|
|
|
|
const aiScene* pScene = importer.ReadFile(filename,
|
|
|
|
aiProcess_Triangulate |
|
|
|
|
aiProcess_ConvertToLeftHanded);
|
|
|
|
|
2020-04-04 19:37:50 +00:00
|
|
|
if (pScene == nullptr)
|
2017-06-03 07:25:20 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-03 13:43:01 +00:00
|
|
|
this->directory_ = filename.substr(0, filename.find_last_of("/\\"));
|
2017-06-03 07:25:20 +00:00
|
|
|
|
2020-04-03 13:43:01 +00:00
|
|
|
this->dev_ = dev;
|
|
|
|
this->devcon_ = devcon;
|
|
|
|
this->hwnd_ = hwnd;
|
2017-06-03 07:25:20 +00:00
|
|
|
|
|
|
|
processNode(pScene->mRootNode, pScene);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:22:57 +00:00
|
|
|
void ModelLoader::Draw(ID3D11DeviceContext * devcon) {
|
2020-04-03 14:07:44 +00:00
|
|
|
for (size_t i = 0; i < meshes_.size(); ++i ) {
|
2020-04-03 13:43:01 +00:00
|
|
|
meshes_[i].Draw(devcon);
|
2017-06-03 07:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:22:57 +00:00
|
|
|
Mesh ModelLoader::processMesh(aiMesh * mesh, const aiScene * scene) {
|
2017-06-03 07:25:20 +00:00
|
|
|
// Data to fill
|
2020-03-08 14:50:18 +00:00
|
|
|
std::vector<VERTEX> vertices;
|
|
|
|
std::vector<UINT> indices;
|
|
|
|
std::vector<Texture> textures;
|
2017-06-03 07:25:20 +00:00
|
|
|
|
|
|
|
// Walk through each of the mesh's vertices
|
2020-03-07 11:22:57 +00:00
|
|
|
for (UINT i = 0; i < mesh->mNumVertices; i++) {
|
2017-06-03 07:25:20 +00:00
|
|
|
VERTEX vertex;
|
|
|
|
|
|
|
|
vertex.X = mesh->mVertices[i].x;
|
|
|
|
vertex.Y = mesh->mVertices[i].y;
|
|
|
|
vertex.Z = mesh->mVertices[i].z;
|
|
|
|
|
2020-03-07 11:22:57 +00:00
|
|
|
if (mesh->mTextureCoords[0]) {
|
2017-06-03 07:25:20 +00:00
|
|
|
vertex.texcoord.x = (float)mesh->mTextureCoords[0][i].x;
|
|
|
|
vertex.texcoord.y = (float)mesh->mTextureCoords[0][i].y;
|
|
|
|
}
|
|
|
|
|
|
|
|
vertices.push_back(vertex);
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:22:57 +00:00
|
|
|
for (UINT i = 0; i < mesh->mNumFaces; i++) {
|
2017-06-03 07:25:20 +00:00
|
|
|
aiFace face = mesh->mFaces[i];
|
|
|
|
|
|
|
|
for (UINT j = 0; j < face.mNumIndices; j++)
|
|
|
|
indices.push_back(face.mIndices[j]);
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:22:57 +00:00
|
|
|
if (mesh->mMaterialIndex >= 0) {
|
2017-06-03 07:25:20 +00:00
|
|
|
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
|
|
|
|
|
2020-03-08 14:50:18 +00:00
|
|
|
std::vector<Texture> diffuseMaps = this->loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse", scene);
|
2017-06-03 07:25:20 +00:00
|
|
|
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
|
|
|
|
}
|
|
|
|
|
2020-04-03 13:43:01 +00:00
|
|
|
return Mesh(dev_, vertices, indices, textures);
|
2017-06-03 07:25:20 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 14:50:18 +00:00
|
|
|
std::vector<Texture> ModelLoader::loadMaterialTextures(aiMaterial * mat, aiTextureType type, std::string typeName, const aiScene * scene) {
|
|
|
|
std::vector<Texture> textures;
|
2020-03-07 11:22:57 +00:00
|
|
|
for (UINT i = 0; i < mat->GetTextureCount(type); i++) {
|
2017-06-03 07:25:20 +00:00
|
|
|
aiString str;
|
|
|
|
mat->GetTexture(type, i, &str);
|
|
|
|
// Check if texture was loaded before and if so, continue to next iteration: skip loading a new texture
|
|
|
|
bool skip = false;
|
2020-04-03 13:43:01 +00:00
|
|
|
for (UINT j = 0; j < textures_loaded_.size(); j++) {
|
|
|
|
if (std::strcmp(textures_loaded_[j].path.c_str(), str.C_Str()) == 0) {
|
|
|
|
textures.push_back(textures_loaded_[j]);
|
2017-06-03 07:25:20 +00:00
|
|
|
skip = true; // A texture with the same filepath has already been loaded, continue to next one. (optimization)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 11:22:57 +00:00
|
|
|
if (!skip) { // If texture hasn't been loaded already, load it
|
2017-06-03 07:25:20 +00:00
|
|
|
HRESULT hr;
|
|
|
|
Texture texture;
|
2021-04-24 20:13:51 +00:00
|
|
|
|
|
|
|
const aiTexture* embeddedTexture = scene->GetEmbeddedTexture(str.C_Str());
|
|
|
|
if (embeddedTexture != nullptr) {
|
|
|
|
texture.texture = loadEmbeddedTexture(embeddedTexture);
|
2020-03-07 11:22:57 +00:00
|
|
|
} else {
|
2020-03-08 14:50:18 +00:00
|
|
|
std::string filename = std::string(str.C_Str());
|
2020-04-03 13:43:01 +00:00
|
|
|
filename = directory_ + '/' + filename;
|
2020-03-08 14:50:18 +00:00
|
|
|
std::wstring filenamews = std::wstring(filename.begin(), filename.end());
|
2020-04-03 13:43:01 +00:00
|
|
|
hr = CreateWICTextureFromFile(dev_, devcon_, filenamews.c_str(), nullptr, &texture.texture);
|
2017-06-03 07:25:20 +00:00
|
|
|
if (FAILED(hr))
|
2020-04-03 13:43:01 +00:00
|
|
|
MessageBox(hwnd_, "Texture couldn't be loaded", "Error!", MB_ICONERROR | MB_OK);
|
2017-06-03 07:25:20 +00:00
|
|
|
}
|
|
|
|
texture.type = typeName;
|
|
|
|
texture.path = str.C_Str();
|
|
|
|
textures.push_back(texture);
|
2020-04-03 13:43:01 +00:00
|
|
|
this->textures_loaded_.push_back(texture); // Store it as texture loaded for entire model, to ensure we won't unnecesery load duplicate textures.
|
2017-06-03 07:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return textures;
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:22:57 +00:00
|
|
|
void ModelLoader::Close() {
|
2020-04-03 13:43:01 +00:00
|
|
|
for (auto& t : textures_loaded_)
|
2020-03-06 15:26:51 +00:00
|
|
|
t.Release();
|
|
|
|
|
2020-04-03 14:07:44 +00:00
|
|
|
for (size_t i = 0; i < meshes_.size(); i++) {
|
2020-04-03 13:43:01 +00:00
|
|
|
meshes_[i].Close();
|
2017-06-03 07:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:22:57 +00:00
|
|
|
void ModelLoader::processNode(aiNode * node, const aiScene * scene) {
|
|
|
|
for (UINT i = 0; i < node->mNumMeshes; i++) {
|
2017-06-03 07:25:20 +00:00
|
|
|
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
|
2020-04-03 13:43:01 +00:00
|
|
|
meshes_.push_back(this->processMesh(mesh, scene));
|
2017-06-03 07:25:20 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 11:22:57 +00:00
|
|
|
for (UINT i = 0; i < node->mNumChildren; i++) {
|
2017-06-03 07:25:20 +00:00
|
|
|
this->processNode(node->mChildren[i], scene);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-24 20:13:51 +00:00
|
|
|
ID3D11ShaderResourceView * ModelLoader::loadEmbeddedTexture(const aiTexture* embeddedTexture) {
|
2017-06-03 07:25:20 +00:00
|
|
|
HRESULT hr;
|
2021-04-24 20:13:51 +00:00
|
|
|
ID3D11ShaderResourceView *texture = nullptr;
|
|
|
|
|
|
|
|
if (embeddedTexture->mHeight != 0) {
|
|
|
|
// Load an uncompressed ARGB8888 embedded texture
|
|
|
|
D3D11_TEXTURE2D_DESC desc;
|
|
|
|
desc.Width = embeddedTexture->mWidth;
|
|
|
|
desc.Height = embeddedTexture->mHeight;
|
|
|
|
desc.MipLevels = 1;
|
|
|
|
desc.ArraySize = 1;
|
|
|
|
desc.SampleDesc.Count = 1;
|
|
|
|
desc.SampleDesc.Quality = 0;
|
|
|
|
desc.Usage = D3D11_USAGE_DEFAULT;
|
|
|
|
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
|
|
|
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
|
|
|
desc.CPUAccessFlags = 0;
|
|
|
|
desc.MiscFlags = 0;
|
|
|
|
|
|
|
|
D3D11_SUBRESOURCE_DATA subresourceData;
|
|
|
|
subresourceData.pSysMem = embeddedTexture->pcData;
|
|
|
|
subresourceData.SysMemPitch = embeddedTexture->mWidth * 4;
|
|
|
|
subresourceData.SysMemSlicePitch = embeddedTexture->mWidth * embeddedTexture->mHeight * 4;
|
|
|
|
|
|
|
|
ID3D11Texture2D *texture2D = nullptr;
|
|
|
|
hr = dev_->CreateTexture2D(&desc, &subresourceData, &texture2D);
|
|
|
|
if (FAILED(hr))
|
|
|
|
MessageBox(hwnd_, "CreateTexture2D failed!", "Error!", MB_ICONERROR | MB_OK);
|
|
|
|
|
|
|
|
hr = dev_->CreateShaderResourceView(texture2D, nullptr, &texture);
|
|
|
|
if (FAILED(hr))
|
|
|
|
MessageBox(hwnd_, "CreateShaderResourceView failed!", "Error!", MB_ICONERROR | MB_OK);
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
2017-06-03 07:25:20 +00:00
|
|
|
|
2021-04-24 20:13:51 +00:00
|
|
|
// mHeight is 0, so try to load a compressed texture of mWidth bytes
|
|
|
|
const size_t size = embeddedTexture->mWidth;
|
2017-06-03 07:25:20 +00:00
|
|
|
|
2021-04-24 20:13:51 +00:00
|
|
|
hr = CreateWICTextureFromMemory(dev_, devcon_, reinterpret_cast<const unsigned char*>(embeddedTexture->pcData), size, nullptr, &texture);
|
2017-06-03 07:25:20 +00:00
|
|
|
if (FAILED(hr))
|
2020-04-03 13:43:01 +00:00
|
|
|
MessageBox(hwnd_, "Texture couldn't be created from memory!", "Error!", MB_ICONERROR | MB_OK);
|
2017-06-03 07:25:20 +00:00
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|