【问题标题】:glDrawElements EXC_BAD_ACCESS using VBO'sglDrawElements EXC_BAD_ACCESS 使用 VBO
【发布时间】:2011-12-27 16:14:39
【问题描述】:

我对 VBO 有这个问题,我不太了解。我的问题是通过意外更改 glVertexAttribPointer 方法调用中的最后一个参数来解决的。因此,我正在寻找关于 为什么 这突然起作用或为什么我以前的代码不再起作用的答案。 (只是为了更好地了解opengl)

结构:

typedef struct 
{
    float position[3];
    float color[4];
} Vertex;

没有 VBO:

// Get pointers to the data
GLsizei stride = sizeof(Vertex);
const GLvoid *pCoords = &squareVertices[0].position[0];
const GLvoid *pColors = &squareVertices[0].color[0];

// Setup pointers to positions and colors
glVertexAttribPointer(positionSlot, 3, GL_FLOAT, GL_FALSE, stride, pCoords);
glVertexAttribPointer(colorSlot, 4, GL_FLOAT, GL_FALSE, stride, pColors);

// Draw
glDrawArrays(GL_TRIANGLES, 0, sizeof(squareVertices) / sizeof(Vertex));

使用 VBO(这给了我一个 EXC_BAD_ACCESS)

// Stide
GLsizei stride = sizeof(Vertex);

// Setup pointers to positions and colors
glVertexAttribPointer(positionSlot, 3, GL_FLOAT, GL_FALSE, stride, 0);
glVertexAttribPointer(colorSlot, 4, GL_FLOAT, GL_FALSE, stride, (GLvoid*) (sizeof(float) * 3));

// Draw
glDrawElements(GL_TRIANGLES, sizeof(squareIndices) / sizeof(squareIndices[0]), GL_UNSIGNED_BYTE, 0);

所以,唯一真正不同的是 glVertexAttribPointer 调用,它有一个指向数据的指针,现在有固定的数字。任何人都可以详细说明这一点吗?提前致谢。

【问题讨论】:

    标签: exc-bad-access opengl-es-2.0 vbo


    【解决方案1】:

    当 VBO 处于活动状态时,glVertexAttribPointer 的最后一个参数实际上不是指针,而是顶点缓冲区对象内的偏移量类型转换为指针。这就是 VBO 的工作方式:数据已被复制到由图形驱动程序管理的某些内存中,您不知道它的地址。事实上,当它存储在显卡的某个位置时,它甚至可能没有正常的地址。

    剩下的就是围绕 C/C++ 类型系统作弊。类型系统远远不够聪明,无法理解glVertexAttribPointer 的最后一个参数在 VBO 处于活动状态时应该是一个指针大小的整数偏移量,否则是一个指针。所以对于 C++ 编译器来说,这必须一直看起来像一个指针,因为这就是 glVertexAttribPointer(或者更确切地说,它是前辈 glVertexPointer 和朋友们)最初定义的方式。当它真的只是一个数字时,我们只是使用类型转换来欺骗编译器。

    【讨论】:

    • 哇!那是一个快速的答案。非常感谢。晶莹剔透 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    相关资源
    最近更新 更多