ASSIMP模型加载器泄漏问题
创始人
2024-09-20 19:00:35
0

该问题的根本原因是由于assimp库中的代码在处理顶点数据时可能会造成内存泄漏。为了避免这个问题,我们可以通过自己管理内存来解决这个问题。

以下是解决方法的示例代码:

#include 
#include 
#include 
#include 

struct MeshData
{
    unsigned int VAO;
    unsigned int VBO;
    unsigned int EBO;
    unsigned int numIndices;

    MeshData() : VAO(0), VBO(0), EBO(0), numIndices(0) { }

    ~MeshData()
    {
        if (VAO) glDeleteVertexArrays(1, &VAO);
        if (VBO) glDeleteBuffers(1, &VBO);
        if (EBO) glDeleteBuffers(1, &EBO);
    }
};

void LoadMesh(const char *filename)
{
    Assimp::Importer importer;
    const aiScene *scene = importer.ReadFile(filename, aiProcess_Triangulate | aiProcess_FlipUVs);

    if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
        throw std::runtime_error("Failed to load mesh");

    aiMesh *mesh = scene->mMeshes[0];
    MeshData data;

    glGenVertexArrays(1, &data.VAO);
    glGenBuffers(1, &data.VBO);
    glGenBuffers(1, &data.EBO);

    glBindVertexArray(data.VAO);
    glBindBuffer(GL_ARRAY_BUFFER, data.VBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.EBO);

    glBufferData(GL_ARRAY_BUFFER, mesh->mNumVertices * sizeof(aiVector3D), &mesh->mVertices[0][0], GL_STATIC_DRAW);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->mNumFaces * 3 * sizeof(unsigned int), &mesh->mFaces[0].mIndices[0], GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(aiVector3D), 0

相关内容

热门资讯

透视揭幕"wepok... 透视揭幕"wepoker能不能透视"切实存在有辅助工具(哔哩哔哩)进入游戏-大厅左侧-新手福利-激活...
透视辅助"wepok... 透视辅助"wepoker免费脚本咨询"好像存在有辅助方法(哔哩哔哩)1、许多玩家不知道wepoker...
透视教你"wepok... 透视教你"wepoker好友房开挂"都是真的是有辅助神器(哔哩哔哩)1、首先打开wepoker好友房...
透视解密"智星菠萝可... 透视解密"智星菠萝可以辅助吗"好像是真的辅助插件(哔哩哔哩)1、这是跨平台的智星菠萝可以辅助吗轻量版...
透视有挂"wejok... 透视有挂"wejoker辅助机器人"都是真的是有辅助工具(哔哩哔哩)1、许多玩家不知道wejoker...
透视解谜"hhpok... 透视解谜"hhpoker是真的假的"好像存在有辅助技巧(哔哩哔哩)hhpoker是真的假的破解侠是真...
透视科普"wepok... 透视科普"wepoker免费脚本弱密码"果然是真的辅助神器(哔哩哔哩)wepoker免费脚本弱密码辅...
透视曝光"pokem... 透视曝光"pokemmo手机版脚本免费"都是是真的辅助脚本(哔哩哔哩)小薇(辅助器软件下载)致您一封...
透视专业"wepok... 透视专业"wepoker透视脚本"一直是真的辅助方法(哔哩哔哩)wepoker透视脚本辅助器是一种具...
透视解密"wepok... 透视解密"wepoker透视辅助下载"一贯真的是有辅助方法(哔哩哔哩)1、下载好wepoker透视辅...