这是从 GLKViewController 派生的视图类。
@interface IncubeViewController : GLKViewController {
...
}
这就是我加载vao的方式:
- (void) loadGeometry
{
/* Pieces */
for (int i = ptFirst; i < ptLast; i++) {
glGenVertexArraysOES(1, &pieceObject[i].array);
glBindVertexArrayOES(pieceObject[i].array);
glGenBuffers(1, &pieceObject[i].buffer);
glBindBuffer(GL_ARRAY_BUFFER, pieceObject[i].buffer);
glBufferData(GL_ARRAY_BUFFER, vertextData[i].vertNumber * sizeof(Vertex),
vertextData[i].vertArray, GL_STATIC_DRAW);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_TRUE,
sizeof(Vertex), (void *)offsetof(Vertex, position));
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_TRUE,
sizeof(Vertex), (void *)offsetof(Vertex, normal));
glEnableVertexAttribArray(GLKVertexAttribNormal);
}
/* Cube */
glGenVertexArraysOES(1, &cubeObject.array);
glBindVertexArrayOES(cubeObject.array);
glGenBuffers(1, &cubeObject.buffer);
glBindBuffer(GL_ARRAY_BUFFER, cubeObject.buffer);
glBufferData(GL_ARRAY_BUFFER, CubeDataNumberOfVertices * sizeof(Vector3),
CubeData, GL_STATIC_DRAW);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE,
0, (void *)0);
glEnableVertexAttribArray(GLKVertexAttribPosition);
/* Sphere */
glGenVertexArraysOES(1, &sphereObject.array);
glBindVertexArrayOES(sphereObject.array);
glGenBuffers(1, &sphereObject.buffer);
glBindBuffer(GL_ARRAY_BUFFER, sphereObject.buffer);
glBufferData(GL_ARRAY_BUFFER, SphereDataNumberOfVertices * sizeof(Vertex),
SphereData, GL_STATIC_DRAW);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (void *)offsetof(Vertex, position));
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (void *)offsetof(Vertex, normal));
glEnableVertexAttribArray(GLKVertexAttribNormal);
/* Diagonal grid */
glGenVertexArraysOES(1, &diagonalGridObject.array);
glBindVertexArrayOES(diagonalGridObject.array);
glGenBuffers(1, &diagonalGridObject.buffer);
glBindBuffer(GL_ARRAY_BUFFER, diagonalGridObject.buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(diagonalGridData),
diagonalGridData, GL_STATIC_DRAW);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE,
sizeof(Vector3), (void *)0);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glBindVertexArrayOES(0);
}
- (void) unloadGeometry
{
glDeleteBuffers(1, &diagonalGridObject.buffer);
glDeleteVertexArraysOES(1, &diagonalGridObject.array);
glDeleteBuffers(1, &cubeObject.buffer);
glDeleteVertexArraysOES(1, &cubeObject.array);
glDeleteBuffers(1, &sphereObject.buffer);
glDeleteVertexArraysOES(1, &sphereObject.array);
for (int i = ptFirst; i < ptLast; i++) {
glDeleteBuffers(1, &pieceObject[i].buffer);
glDeleteVertexArraysOES(1, &pieceObject[i].array);
}
}
虽然它没有显示你做错了什么,但如果有人需要,我在这里添加它只是为了参考。
我的一种解释是,您试图在应用程序初始化的错误时刻创建您的 vao,或者 init 不太正确(例如,未设置上下文)。
我在这里有以下堆栈:
- (void)setupGL
{
[EAGLContext setCurrentContext:self.context];
self.effect = [[[GLKBaseEffect alloc] init] autorelease];
if (self.effect) {
self.effect.useConstantColor = GL_TRUE;
self.effect.colorMaterialEnabled = GL_TRUE;
self.effect.light0.enabled = GL_TRUE;
self.effect.light0.diffuseColor = GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f);
}
((GLKView *)self.view).drawableMultisample = GLKViewDrawableMultisample4X;
self.pauseOnWillResignActive = YES;
self.resumeOnDidBecomeActive = YES;
self.preferredFramesPerSecond = 30;
glDisable(GL_DITHER);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glLineWidth(2.0f);
[self loadGeometry];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.context = [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2] autorelease];
if (!self.context)
NSLog(@"Failed to create ES context");
GLKView *view = (GLKView *)self.view;
view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
[self setupGL];
}