Загрузить модель 3d Opengl 3.3 - PullRequest
0 голосов
/ 14 июля 2020

Я пытаюсь загрузить 3D-модель с помощью assimp в Opengl 3.3, но ничего не загружается. Вот код для рендеринга 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;
}

Не понимаю, где ошибки. Есть ли у вас какие-либо предложения? Окно рендеринга полностью пусто.

Окно рендеринга полностью пусто

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...