"Access Violation Reading Location"顶点缓冲区
"Access Violation Reading Location" with Vertex Buffer
我正试图将以下代码段转换为使用顶点缓冲区的代码:
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。如果任何属性的单个组件不同,则不再将其视为同一顶点。
相关文章:
- 从返回的顶点缓冲区查询顶点结构
- Vulkan 中的动态顶点缓冲区格式设置
- 在 DirectX 11 中从 GPU 读回顶点缓冲区(并获取顶点)
- DirectX11只绘制填充索引和顶点缓冲区中的一半顶点
- tiny_obj_loader到 Direct3D 顶点缓冲区和索引缓冲区数组
- OpenGL 顶点缓冲区类重定义和模板方法错误
- D3D11:映射顶点缓冲区时E_OUTOFMEMORY
- C DirectX 11从第二个顶点缓冲区呈现麻烦
- 我可以将单个顶点索引与具有所有属性(位置、法线、texCoord)的顶点缓冲区一起使用吗?
- DirectX:如果绑定索引缓冲区但不绑定顶点缓冲区,会发生什么情况
- Directx 11 - 是将顶点缓冲区中的所有顶点推送到顶点着色器,还是仅编制索引
- 自动生成的顶点缓冲区和索引缓冲区不起作用
- 为什么我的顶点缓冲区对象出现访问冲突错误?
- 为什么我的 openGL 顶点缓冲区对象不会绘制任何内容?
- 在OpenGL中,如何从内存中编辑特定的顶点缓冲区属性
- 我是否需要OpenGL中类似对象的多个顶点缓冲区
- (DirectX 11)单个顶点缓冲区是否可以一次绑定到多个 IA 输入槽
- Directx 10:使用CopySubresourceRegion更新顶点缓冲区
- 具有X文件的非相干顶点缓冲区
- 在DX9上为顶点缓冲区创建结构