首页 > 解决方案 > 加载模型 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;
}

我不明白错误在哪里。你有什么建议吗?渲染窗口完全是空的。

渲染窗口完全为空

标签: c

解决方案


推荐阅读