gpt4 book ai didi

ios - 在 Open ES iPhone 应用程序中围绕 VBO 使用 VAO 在调用 glDrawElements 时导致 EXC_BAD_ACCESS

转载 作者:技术小花猫 更新时间:2023-10-29 11:19:48 35 4
gpt4 key购买 nike

我正在努力将我的代码提升到一个新的水平。遵循 Apple 的一些最佳实践,我正在尝试围绕我的顶点缓冲区对象 (VBO) 实现顶点数组对象。我这样设置我的 VBO 和 VAO:

- (void)setupVBOs {  
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArrayOES(0);
{
glGenVertexArraysOES(1, &directArrayObject);
glBindVertexArrayOES(directArrayObject);

// GLuint texCoordBuffer;
glGenBuffers(1, &texCoordBuffer);
glBindBuffer(GL_ARRAY_BUFFER, texCoordBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(DirectVertices), DirectVertices, GL_STATIC_DRAW);

glVertexAttribPointer(directPositionSlot, 2, GL_FLOAT, GL_FALSE, sizeof(DirectVertex), (GLvoid*)offsetof(DirectVertex, position));
glEnableVertexAttribArray(directPositionSlot);
glVertexAttribPointer(texCoordSlot, 2, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(DirectVertex), (GLvoid*)offsetof(DirectVertex, texCoord));
glEnableVertexAttribArray(texCoordSlot);

glGenVertexArraysOES(1, &arrayObject);
glBindVertexArrayOES(arrayObject);

// GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

glVertexAttribPointer(positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glEnableVertexAttribArray(positionSlot);
glVertexAttribPointer(colorSlot, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, Color));
glEnableVertexAttribArray(colorSlot);

// GLuint indexBuffer;
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArrayOES(0);
}

我从 http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=287977 中获取的然后像这样使用它:

- (void) render:(CADisplayLink*)displayLink {

glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glViewport(0, 0, backingWidth, backingHeight);

[directProgram use];
glBindVertexArrayOES(directArrayObject);
glDisable(GL_DEPTH_TEST);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, videoFrameTexture);

// // Update uniform values
glUniform1i(videoFrameUniform, 0);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

[program use];
glBindVertexArrayOES(arrayObject);
glDisable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);

CC3GLMatrix *projection = [CC3GLMatrix matrix];
float h = 4.0f * self.frame.size.height / self.frame.size.width;
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:10];
glUniformMatrix4fv(projectionUniform, 1, 0, projection.glMatrix);

CC3GLMatrix *modelView = [CC3GLMatrix matrix];
[modelView populateFromTranslation:CC3VectorMake(sin(CACurrentMediaTime()), 0, -7)];
currentRotation += displayLink.duration * 90;
[modelView rotateBy:CC3VectorMake(currentRotation, currentRotation, 0)];
glUniformMatrix4fv(modelViewUniform, 1, 0, modelView.glMatrix);

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

glBindVertexArrayOES(0);

BOOL success = [context presentRenderbuffer:GL_RENDERBUFFER];
if(!success)
NSLog(@"present failed");
}

对 glDrawArrays 的调用有效,它填充了我的纹理,但是,对 glDrawElements 的调用失败并返回 EXC_BAD_ACCESS。我的着色器程序(我使用两个)包装在我从 http://iphonedevelopment.blogspot.com/2010/11/opengl-es-20-for-ios-chapter-4.html 中获取的 GLProgram 对象中

最佳答案

从设置函数的末尾删除 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

根据 OES_vertex_array_object 的规范,一个顶点数组对象封装了所有状态除了数组缓冲区绑定(bind)1,所以在你绘制的时候没有元素缓冲区绑定(bind),而你可能想要indexBuffer 进行绑定(bind)。通过在您绑定(bind)远离顶点数组对象时保留 indexBuffer 绑定(bind),您可以确保当您返回到该顶点数组对象时它会被反弹。


1 如果您想知道为什么在顶点数组对象中没有跟踪数组缓冲区绑定(bind),这大概是因为在从中读取顶点数据时没有直接使用当前绑定(bind)的数组缓冲区数组 - 相反,每个顶点属性都有自己的缓冲区绑定(bind),由其各自的 gl*Pointer 函数通过调用函数时查看数组缓冲区绑定(bind)来填充。

关于ios - 在 Open ES iPhone 应用程序中围绕 VBO 使用 VAO 在调用 glDrawElements 时导致 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6240863/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com