#include "Model.h"
#include <stb/stb_image.h>
using namespace std;
void Model::Draw(Shader shader)
{
for (unsigned int i = 0; i < meshes.size(); i++)
meshes[i].Draw(shader);
}
void Model::loadModel(string const &path)
{
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) // if is Not Zero
{
cout << "ERROR::ASSIMP:: " << importer.GetErrorString() << endl;
return;
}
directory = path.substr(0, path.find_last_of('/'));
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)
{
vector<Vertex> vertices;
vector<unsigned int> indices;
vector<MeshTexture> textures;
//顶点&&索引
for (unsigned int i = 0; i < mesh->mNumVertices; i++)
{
Vertex vertex;
glm::vec3 vector;
vector.x = mesh->mVertices[i].x;
vector.y = mesh->mVertices[i].y;
vector.z = mesh->mVertices[i].z;
vertex.Position = vector;
vector.x = mesh->mNormals[i].x;
vector.y = mesh->mNormals[i].y;
vector.z = mesh->mNormals[i].z;
vertex.Normal = vector;
if (mesh->mTextureCoords[0])
{
glm::vec2 vec;
vec.x = mesh->mTextureCoords[0][i].x;
vec.y = mesh->mTextureCoords[0][i].y;
vertex.TexCoords = vec;
}
else
vertex.TexCoords = glm::vec2(0.0f, 0.0f);
vector.x = mesh->mTangents[i].x;
vector.y = mesh->mTangents[i].y;
vector.z = mesh->mTangents[i].z;
vertex.Tangent = vector;
vector.x = mesh->mBitangents[i].x;
vector.y = mesh->mBitangents[i].y;
vector.z = mesh->mBitangents[i].z;
vertex.Bitangent = vector;
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]);
}
//材质
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
vector<MeshTexture> diffuseMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse");
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
vector<MeshTexture> specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular");
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
std::vector<MeshTexture> normalMaps = loadMaterialTextures(material, aiTextureType_HEIGHT, "texture_normal");
textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
std::vector<MeshTexture> heightMaps = loadMaterialTextures(material, aiTextureType_AMBIENT, "texture_height");
textures.insert(textures.end(), heightMaps.begin(), heightMaps.end());
return Mesh(vertices, indices, textures);
}
std::vector<MeshTexture> Model::loadMaterialTextures(aiMaterial *mat, aiTextureType type, string typeName)
{
vector<MeshTexture> textures;
for (unsigned int i = 0; i < mat->GetTextureCount(type); i++)
{
aiString str;
mat->GetTexture(type, i, &str);
bool skip = false;
for (unsigned int j = 0; j < textures_loaded.size(); j++)
{
if (std::strcmp(textures_loaded[j].path.data(), str.C_Str()) == 0)
{
textures.push_back(textures_loaded[j]);
skip = true;
break;
}
}
//优化方案:如果纹理还没有被加载,则加载它
if (!skip)
{
MeshTexture texture;
texture.id = TextureFromFile(str.C_Str(), this->directory);
texture.type = typeName;
texture.path = str.C_Str();
textures.push_back(texture);
textures_loaded.push_back(texture);
}
}
return textures;
}
unsigned int TextureFromFile(const char *path, const string &directory, bool gamma)
{
string filename = string(path);
filename = directory + '/' + filename;
unsigned int textureID;
glGenTextures(1, &textureID);
int width, height, nrComponents;
unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 0);
if (data)
{
GLenum format;
if (nrComponents == 1)
format = GL_RED;
else if (nrComponents == 3)
format = GL_RGB;
else if (nrComponents == 4)
format = GL_RGBA;
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_image_free(data);
}
else
{
std::cout << "Texture failed to load at path: " << path << std::endl;
stbi_image_free(data);
}
return textureID;
}

UestcXiye
- 粉丝: 8665
最新资源
- 北京交通大学计算机科学与技术专业2022级本科大三上学期操作系统课程实验项目-包含进程管理内存管理文件系统设备驱动等核心操作系统功能实现-用于深入理解操作系统原理和内核开发技.zip
- 徽菜馆饭店管理系统-基于SpringBoot和MySQL的餐饮行业数字化解决方案-包含菜品管理-订单处理-库存管理-员工排班-会员积分-财务报表-在线预订-外卖配送-后厨管理-收银.zip
- hello-java-maker-JavaInterview-58184-1752994681263.zip
- STM32单片机应用与全案例实践 (沈红卫 等)
- 山东大学计算机科学与技术学院操作系统课程设计项目-基于WSL2和Ubuntu20046环境开发的操作系统课程设计参考实现-包含实验一到实验八的完整代码和详细报告-重点实现进程管.zip
- 四川大学综合教务系统优化助手-一个基于用户脚本技术的教务系统功能增强工具-包含课程表美化-成绩查询优化-考试安排提醒-课表导出-自动评教-一键选课-快速登录-界面美化-数据可视化-.zip
- vscode插件gtags
- 深入理解计算机系统第二版中文学习笔记与代码实践-计算机系统原理-信息表示处理-程序机器级表示-处理器体系结构-程序性能优化-存储器层次结构-链接机制-异常控制流-虚拟内存-系统级I.zip
- 【扣子COZE AI编程平台】基于Python的智能天气查询应用开发教程:从创建到部署的全流程指南
- howie6879-nand2tetris-57108-1753002971923.zip
- 【生产力工具领域】Cursor快捷键与工作流组合技:提升编程写作及日常办公效率的方法汇总Cursor这款生产力工具
- Linux(imx6ull)-uboot
- 毕业设计项目-基于BS架构的计算机网络水平在线测评系统-包含用户注册登录-题库管理-随机组卷-在线考试-自动评分-成绩统计-错题分析-学习路径推荐-多终端适配-管理员后台-数据可.zip
- 【COZE-AI编程新手入门指南】COZE AI平台简介与编程环境搭建:从零开始的AI模型创建、训练和部署教程介绍了COZE AI
- carroll1118-Notes-52540-1752999558457.zip
- 【数据科学与软件开发】使用Cursor进行销售数据分析:数据查询与可视化实战案例教程
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


