我非常怀疑有人可以帮助我,但是嘿嘿。
我有一个我一直在研究的 3D 框架,世界上大多数东西都是立方体(36 个顶点)。
我遵循了关于将搅拌器对象引入 OpenGL 的精彩教程:http://www.raywenderlich.com/48293/how-to-export-blender-models-to-opengl-es-part-1
我把立方体放进去,一切都很好,然后我试着把一个球体放进去,立方体,圆柱体等,它爆炸了 ios opengl 绘图调用超出数组缓冲区边界
我知道这些对象很好,因为它们可以与演示应用程序一起使用,但它只呈现 1 个对象并且没有绑定(bind)数组和顶点等
无论如何
我确信这段代码是垃圾,但我已经尝试了一切
- (void)setupSphere;
{
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);
//////
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(&sphereVertices), &sphereVertices, GL_STATIC_DRAW);
////
glGenBuffers(1, &_position);
glBindBuffer(GL_ARRAY_BUFFER, _position);
glBufferData(GL_ARRAY_BUFFER, sizeof(spherePositions),spherePositions, GL_STATIC_DRAW);
////
glGenBuffers(1, &_texture);
glBindBuffer(GL_ARRAY_BUFFER, _texture);
glBufferData(GL_ARRAY_BUFFER, sizeof(sphereTexels),sphereTexels, GL_STATIC_DRAW);
////
glGenBuffers(1, &_normal);
glBindBuffer(GL_ARRAY_BUFFER, _normal);
glBufferData(GL_ARRAY_BUFFER, sizeof(sphereNormals),sphereNormals, GL_STATIC_DRAW);
//////
// glBindVertexArrayOES(0);
// Positions
//glBindBuffer(GL_ARRAY_BUFFER, _position);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, spherePositions);
// Texels
//glBindBuffer(GL_ARRAY_BUFFER, _texture);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, sphereTexels);
// Normals
//glBindBuffer(GL_ARRAY_BUFFER, _normal);
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 0, sphereNormals);
//glBindVertexArrayOES(_vertexArray);
glBindVertexArrayOES(0);
}
- (void)drawSphere:(float)eyeOffset
{
// Calculate the per-eye model view matrix:
GLKMatrix4 temp = GLKMatrix4MakeTranslation(eyeOffset, 0.0f, 0.0f);
GLKMatrix4 eyeBaseModelViewMatrix = GLKMatrix4Multiply(temp, self.baseModelViewMatrix);
if (self.isTransparant)
{
glEnable (GL_BLEND);
glDisable(GL_CULL_FACE);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
if (self.textureInfo)
{
glBindTexture(self.textureInfo.target, self.textureInfo.name);
}
glBindVertexArrayOES(_vertexArray);
glUseProgram(_program);
self.modelViewMatrix = GLKMatrix4MakeTranslation(self.position.x,self.position.y, self.position.z );//(float)x, (float)y, -1.5f)
self.modelViewMatrix = GLKMatrix4Scale(self.modelViewMatrix, self.scale.x, self.scale.y, self.scale.z);
//rotation +=0.01;
self.modelViewMatrix = GLKMatrix4Rotate(self.modelViewMatrix,rotation, 0.0 ,0.0 ,1.0);
self.modelViewMatrix = GLKMatrix4Multiply(eyeBaseModelViewMatrix, self.modelViewMatrix);
GLKMatrix3 normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3(self.modelViewMatrix), NULL);
GLKMatrix4 modelViewProjectionMatrix = GLKMatrix4Multiply(self.projectionMatrix, self.modelViewMatrix);
glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, modelViewProjectionMatrix.m);
glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, normalMatrix.m);
_colorSlot = glGetUniformLocation(_program, "color");
GLfloat color[] = {
self.color.x, self.color.y, self.color.z, self.color.a};
glUniform4fv(_colorSlot, 1, color);
glDrawArrays(GL_TRIANGLES, 0, sphereVertices);
if (self.isTransparant)
{
glEnable(GL_CULL_FACE);
//glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
}
}
最佳答案
您正在尝试将 VAO 与客户端顶点数组一起使用。这是不支持的。引用自 extension spec :
Should a vertex array object be allowed to encapsulate client vertex arrays?
RESOLVED: No. The OpenGL ES working group agreed that compatibility with OpenGL and the ability to to guide developers to more performant drawing by enforcing VBO usage were more important than the possibility of hurting adoption of VAOs.
如果您想使用 VAO,您将需要启用 VBO 代码,该代码似乎主要出现在您的代码中,但目前已被注释掉。
关于ios - OpenGL GLKit : rendering different size vertices objects together. ios opengl 绘制调用超出数组缓冲区边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23177366/