"Access Violation Reading Location"顶点缓冲区

"Access Violation Reading Location" with Vertex Buffer

本文关键字:顶点 缓冲区 Location Reading Access Violation      更新时间:2023-10-16

我正试图将以下代码段转换为使用顶点缓冲区的代码:

glBegin (GL_QUADS);
glTexCoord2fv (&_vertex[ci->index_list[7]].uv.x);
glVertex3fv (&_vertex[ci->index_list[7]].position.x);
glVertex3fv (&_vertex[ci->index_list[5]].position.x);
glVertex3fv (&_vertex[ci->index_list[3]].position.x);
glVertex3fv (&_vertex[ci->index_list[1]].position.x);
glEnd ();

我的错误代码部分看起来像这样:

GLfloat * p = (GLfloat *) malloc(sizeof(GLfloat)*14);
//Memcopies vertices into p pointer
memcpy(&p[counter+0], &_vertex[ci->index_list[7]].uv.x, sizeof(GLfloat)*2);
memcpy(&p[counter+2], &_vertex[ci->index_list[7]].position.x, sizeof(GLfloat)*3);
memcpy(&p[counter+5], &_vertex[ci->index_list[5]].position.x, sizeof(GLfloat)*3);
memcpy(&p[counter+8], &_vertex[ci->index_list[3]].position.x, sizeof(GLfloat)*3);
memcpy(&p[counter+11], &_vertex[ci->index_list[1]].position.x, sizeof(GLfloat)*3);
glGenBuffers(1, &vboId1);
glBindBuffer(GL_ARRAY_BUFFER, vboId1);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*14, p, GL_STATIC_DRAW_ARB);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(GLfloat)*14, (GLfloat*)0); 
glVertexPointer(3, GL_FLOAT, 0,  2+(GLfloat*)0);
glDrawArrays(GL_QUADS, 0, 1);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);

然而,我得到一个"访问违反读取位置"的glDrawArrays线错误。你知道这里有什么问题吗?我是OpenGL/graphics的新手,很确定我搞砸了一些明显的东西。

这行不通。即时模式允许您省略在顶点之间重新指定未更改的属性。但是对于顶点数组,你不能这样做。你目前所做的是告诉GL它在缓冲区中找到第i个顶点的texcoord,字节偏移量为14*sizeof(GLfloat)*i。对于第一个顶点,它可以工作,但对于第二个顶点,您将尝试访问缓冲区末尾以外的数据。

你必须复制这些数据,这样每个顶点在相同的布局中都有完全相同的属性。基本上,每个属性需要一个顶点数组,每个顶点都有一个条目,无论值是否改变。

最好将顶点视为不只是位置(由glVertex命令指定),而是相关属性的完整n-tupel。如果任何属性的单个组件不同,则不再将其视为同一顶点。