c - 加载模型 3d Opengl 3.3
问题描述
我正在尝试在 Opengl 3.3 中加载带有 assimp 的 3D 模型,但没有加载任何内容。这是渲染 3d 模型的代码。
void recursive_render(const struct aiScene* sc, const struct aiNode* nd, float scale){
unsigned int i;
unsigned int n = 0, t;
aiMatrix4x4 m = nd->mTransformation;
aiMatrix4x4 m2;
aiMatrix4x4::Scaling(aiVector3D(scale, scale, scale), m2);
m = m * m2;
m.Transpose();
float aux[16];
memcpy(aux, &m, sizeof(float) * 16);
for (; n < nd->mNumMeshes; ++n)
{
const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
glGenVertexArrays(1, &VAO3);
glBindVertexArray(VAO3);
glDrawElements(GL_TRIANGLES, mesh->mNumFaces * 3, GL_UNSIGNED_INT, 0);
}
// draw all children
for (n = 0; n < nd->mNumChildren; ++n)
recursive_render(sc, nd->mChildren[n], scale);
}
顶点数组对象的代码
void genVAOsAndUniformBuffer(const aiScene* sc, const struct aiNode* nd, float scale) {
unsigned int i;
unsigned int n = 0, t;
for (; n < nd->mNumMeshes; ++n)
{
const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
unsigned int* faceArray;
faceArray = (unsigned int*)malloc(sizeof(unsigned int) * mesh->mNumFaces * 3);
unsigned int faceIndex = 0;
for (unsigned int t = 0; t < mesh->mNumFaces; ++t) {
const aiFace* face = &mesh->mFaces[t];
memcpy(&faceArray[faceIndex], face->mIndices, 3 * sizeof(unsigned int));
faceIndex += 3;
}
glGenVertexArrays(1, &VAO3);
glBindVertexArray(VAO3);
// buffer for faces
glGenBuffers(1, &VBO3);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBO3);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(mesh->mNumFaces) * 3, faceArray, GL_STATIC_DRAW);
// buffer for vertex positions
if (mesh->HasPositions()) {
glGenBuffers(1, &VBO3);
glBindBuffer(GL_ARRAY_BUFFER, VBO3);
glBufferData(GL_ARRAY_BUFFER, sizeof(mesh->mNumVertices) * 3, mesh->mVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, 0, 0, 0);
}
if (mesh->HasNormals()) {
glGenBuffers(1, &VBO3);
glBindBuffer(GL_ARRAY_BUFFER, VBO3);
glBufferData(GL_ARRAY_BUFFER, sizeof(mesh->mNumVertices) * 3, mesh->mNormals, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, 0, 0, 0);
}
}
}
最后是从文件中导入 3d 模型的代码。
bool Import3DFromFile(const std::string& pFile){
// Check if file exists
std::ifstream fin(pFile.c_str());
if (!fin.fail())
{
fin.close();
}
else
{
std::cout << "Couldn't open file: " << pFile.c_str() << std::endl;
//logInfo(importer.GetErrorString());
return false;
}
scene = importer.ReadFile(pFile, aiProcessPreset_TargetRealtime_Quality);
// If the import failed, report it
if (!scene)
{
std::cout << "Import of scene 5" << pFile << " succeeded." << std::endl;
//logInfo(importer.GetErrorString());
return false;
}
// Now we can access the file's contents.
std::cout << "Import of scene " << pFile << " succeeded." << std::endl;
// We're done. Everything will be cleaned up by the importer destructor
return true;
}
我不明白错误在哪里。你有什么建议吗?渲染窗口完全是空的。
解决方案
推荐阅读
- ms-access - 我的公司是如何在 Microsoft Access 之上构建程序的?
- html - 将无序列表中的项目符号与上方标题的左边距对齐?
- javascript - IOS 麦克风访问在进行音频通话时不起作用
- python - 使用python,如何读取文件夹中的所有CSV文件并将其内容写入一个新的单个CSV文件?
- ag-grid - ag-grid filter api:当网格没有匹配的行时,filterComponent.selectValue() 不起作用
- visual-studio - 嵌入到 NuGet 包中的图标不会出现在 VS 中
- r - Travis 使用 R 构建文件下载失败
- icons - 编程语言图标的最佳网站
- javascript - 找出文本中有多少个重复的字母
- python - 使用 Python Selenium whataspp bot 隐藏 Chrome 窗口