iOS GL ES exc_bad_access on glDrawElements

iOS GL ES exc_bad_access on glDrawElements

本文关键字:access on glDrawElements bad iOS ES exc GL      更新时间:2023-10-16

我正在尝试使用Assimp和gl es制作一个小型应用程序,该应用程序在iOS上加载并呈现模型。我以前曾与OpenGL合作,但未与GLES合作;而且iOS上从来没有任何东西。

目前,我在调用gldrawelements时会遇到exc_bad_access错误;我看不到我在做什么错。

这是我的顶点类型:

typedef struct {
    float Position[3];
} Vertex;

这是我的网格类型:

class Mesh {
public:
    Vertex* vertices;
    GLuint verticesCount;
    GLubyte* indices;
    GLuint indicesCount;
    std::vector<Texture> textures;
    GLuint vertexBuffer, indexBuffer;
    Mesh(Vertex* vertices, GLubyte* indices, std::vector<Texture> textures);
};

我非常有信心,我像以前那样通过Assimp正确地加载模型,并从另一个项目中提起此代码。因此,我正在使用该数据传递到网格构造函数,并且我正在使用此数据填充VBO:

    glGenBuffers(1, &this->vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
    int sizeOfVerts = sizeof(Vertex) * this->verticesCount;
    std::cout << "Size of verts in bytes " << unsigned(sizeOfVerts) << std::endl;
    glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)sizeOfVerts, &this->vertices[0], GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glGenBuffers(1, &this->indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
    int sizeOfInd = sizeof(GLubyte) * this->indicesCount;
    std::cout << "Size of inds in bytes " << unsigned(sizeOfInd) << std::endl;
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)sizeOfInd, &this->indices[0], GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

使用预测和查看模型矩阵已经在预渲染函数中设置了

- (void)renderMesh:(Mesh*)mesh
{
    glBindBuffer(GL_ARRAY_BUFFER, mesh->vertexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->indexBuffer);
    glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
    int meshSize = mesh->indicesCount;
    glDrawElements(GL_TRIANGLES, meshSize, GL_UNSIGNED_BYTE, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}

_positionSlot是我的着色器中的属性位置。我知道我的着色器工作起作用,因为我已经用它来绘制无聊的平方等等 - 因此,错误肯定是在上述代码中 - 某处

预先感谢,我会确保我既投票又接受:)

发生这种情况的原因是因为我将glubyte用于索引类型;对于我的高层模型来说太小了。不确定为什么该错误在此错误消息中表现出来;在其他平台上,相同的代码将渲染垃圾模型。也许是特定于iOS的?希望这可以帮助别人。