#include "model.h"
#include <QDir>
void Model::loadModel(string path)
{
Assimp::Importer import;
const aiScene *scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);
if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
qDebug() << "ERROR::ASSIMP::" << import.GetErrorString();
return;
}
directory = path.substr(0, path.find_last_of('/'));
modelDirectory = directory + "/model";
QString modelStr = QString::fromStdString(modelDirectory);
QDir dir;
if (!dir.exists(modelStr))
dir.mkdir(modelStr);
for (int i = 0; i < scene->mNumTextures; i++)
{
aiTexture* texture = scene->mTextures[i];
char fileName[100];
sprintf(fileName, "%s/%s.%s", modelDirectory.c_str(), texture->mFilename.C_Str(), texture->achFormatHint);
QFile file(fileName);
if (file.exists())
{
m_mapPath.insert(i, QString(fileName));
continue;
}
if (!file.open(QIODevice::WriteOnly))
break;
unsigned char* buffer = new unsigned char[texture->mWidth * 4];
memset(buffer, 0, texture->mWidth * 4);
for (int x = 0; x < texture->mWidth; ++x)
{
//拷贝RGBA数据到缓冲区
int index = x * 4;
buffer[index] = texture->pcData[x].b; // Blue
buffer[index + 1] = texture->pcData[x].g; // Green
buffer[index + 2] = texture->pcData[x].r; // Red
buffer[index + 3] = texture->pcData[x].a; // Alpha
}
file.write((char*)buffer, texture->mWidth * 4);
file.close();
m_mapPath.insert(i, QString(fileName));
delete[]buffer;
}
processNode(scene->mRootNode, scene);
}
void Model::processNode(aiNode *node, const aiScene *scene)
{
// process all the node's meshes (if any)
for(unsigned int i = 0; i < node->mNumMeshes; i++) {
aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(processMesh(mesh, scene));
}
// then do the same for each of its children
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<Texture> textures;
for(unsigned int i = 0; i < mesh->mNumVertices; i++) {
if(m_maxX<mesh->mVertices[i].x) m_maxX=mesh->mVertices[i].x;
if(m_maxY<mesh->mVertices[i].y) m_maxY=mesh->mVertices[i].y;
if(m_minX>mesh->mVertices[i].x) m_minX=mesh->mVertices[i].x;
if(m_minY>mesh->mVertices[i].y) m_minY=mesh->mVertices[i].y;
Vertex vertex;
// 处理顶点位置、法线和纹理坐标
QVector3D vector;
vector.setX(mesh->mVertices[i].x);
vector.setY(mesh->mVertices[i].y);
vector.setZ(mesh->mVertices[i].z);
vertex.Position = vector;
vector.setX(mesh->mNormals[i].x);
vector.setY(mesh->mNormals[i].y);
vector.setZ(mesh->mNormals[i].z);
vertex.Normal = vector;
if(mesh->mTextureCoords[0]) // 有纹理坐标?
{
QVector2D vec;
vec.setX(mesh->mTextureCoords[0][i].x);
vec.setY(mesh->mTextureCoords[0][i].y);
vertex.TexCoords = vec;
} 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];
vector<Texture> diffuseMaps =
loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse");
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
vector<Texture> specularMaps =
loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular");
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
}
return Mesh(m_glFuns,vertices, indices, textures);
}
vector<Texture> Model::loadMaterialTextures(aiMaterial *mat, aiTextureType type, string typeName)
{
vector<Texture> 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){
Texture texture;
//texture.id = TextureFromFile(str.C_Str(), directory);
texture.texture = TextureFromFile(str.C_Str(), directory);
texture.type = typeName;
texture.path = str.C_Str();
textures.push_back(texture);
textures_loaded.push_back(texture);
}
}
return textures;
}
QOpenGLTexture *Model::TextureFromFile(const char *path, const string &directory)
{
QOpenGLTexture * texture = nullptr;
string filename = string(path);
if (filename.find('*') != std::string::npos)
{
QString qs = QString::fromStdString(filename);
QStringList strList = qs.split('*');
QString fileName = m_mapPath.value(strList.at(1).toInt());
QImage image(fileName);
texture = new QOpenGLTexture(image);
texture->setMinificationFilter(QOpenGLTexture::Nearest);
texture->setMinificationFilter(QOpenGLTexture::Linear);
texture->setWrapMode(QOpenGLTexture::Repeat);
//if (texture == NULL)
// qDebug() << "texture is NULL";
//else
// qDebug() << filename.c_str() << "loaded";
}
else
{
filename = directory + '/' + filename;
qDebug() << "filename = " << QString::fromStdString(filename);
QImage image(filename.c_str());
texture = new QOpenGLTexture(image.mirrored());
//if (texture == NULL)
// qDebug() << "texture is NULL";
//else
// qDebug() << filename.c_str() << "loaded";
}
return texture;
}
没有合适的资源?快使用搜索试试~ 我知道了~
Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+opengl加载各种类型的3D模型,.glb .obj
共118个文件
h:75个
hpp:9个
inl:8个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 9 下载量 188 浏览量
2024-01-15
21:31:34
上传
评论 1
收藏 9.94MB ZIP 举报
温馨提示
Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+op
资源推荐
资源详情
资源评论
收起资源包目录
Qt+opengl加载各种类型的3D模型,.glb .obj 等;Qt+opengl加载各种类型的3D模型,.glb .obj (118个子文件)
model.cpp 7KB
axbopemglwidget.cpp 4KB
mesh.cpp 2KB
mainwindow.cpp 546B
main.cpp 183B
assimp-vc141-mtd.dll 24.12MB
assimp-vc141-mt.dll 5.54MB
.editorconfig 175B
shapes.frag 1KB
light.frag 124B
202312348163455_657abe2f67f122d1f8223a82_j15.glb 1.82MB
material.h 59KB
cimport.h 48KB
config.h 47KB
postprocess.h 33KB
mesh.h 32KB
pstdint.h 30KB
anim.h 17KB
BaseImporter.h 16KB
metadata.h 16KB
types.h 16KB
scene.h 15KB
XmlParser.h 15KB
SceneCombiner.h 14KB
cexport.h 13KB
StreamReader.h 12KB
fast_atof.h 12KB
matrix4x4.h 11KB
Vertex.h 11KB
defs.h 11KB
StringUtils.h 10KB
StreamWriter.h 10KB
BlobIOSystem.h 10KB
ParsingUtils.h 9KB
IOStreamBuffer.h 9KB
light.h 9KB
ByteSwapper.h 9KB
SpatialSort.h 9KB
LineSplitter.h 8KB
MemoryIOWrapper.h 8KB
camera.h 8KB
texture.h 8KB
Exceptional.h 7KB
StringComparison.h 7KB
StandardShapes.h 7KB
matrix3x3.h 6KB
importerdesc.h 6KB
SGSpatialSort.h 6KB
qnan.h 5KB
TinyFormatter.h 5KB
pbrmaterial.h 5KB
vector3.h 5KB
SmallVector.h 5KB
Subdivision.h 5KB
DefaultIOStream.h 5KB
version.h 5KB
cfileio.h 5KB
camera.h 4KB
quaternion.h 4KB
GenericProperty.h 4KB
SkeletonMeshBuilder.h 4KB
Bitmap.h 4KB
Hash.h 4KB
LogAux.h 4KB
SmoothingGroups.h 4KB
DefaultIOSystem.h 4KB
RemoveComments.h 3KB
color4.h 3KB
BundledAssetIOSystem.h 3KB
ZipArchiveIOSystem.h 3KB
vector2.h 3KB
AndroidJNIIOSystem.h 3KB
Profiler.h 3KB
MathFunctions.h 3KB
XMLTools.h 3KB
CreateAnimMesh.h 3KB
commonMetaData.h 3KB
ai_assert.h 3KB
aabb.h 3KB
ColladaMetaData.h 2KB
pushpack1.h 1KB
model.h 1KB
axbopemglwidget.h 1KB
mesh.h 870B
poppack1.h 793B
mainwindow.h 370B
Importer.hpp 31KB
Exporter.hpp 22KB
IOSystem.hpp 12KB
Logger.hpp 11KB
DefaultLogger.hpp 8KB
ProgressHandler.hpp 6KB
IOStream.hpp 5KB
LogStream.hpp 4KB
NullLogger.hpp 3KB
config.h.in 46KB
matrix4x4.inl 24KB
material.inl 13KB
matrix3x3.inl 13KB
quaternion.inl 12KB
共 118 条
- 1
- 2
Mr.codeee
- 粉丝: 3w+
- 资源: 61
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页