これは、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);
}
}
あなたが何か間違ったことをしていることを示しているわけではありませんが、誰かがそれを必要とする場合に参照するためだけにここに追加します.
私が持っている1つの説明は、アプリの初期化の間違った瞬間にvaoを作成しようとしているか、初期化が完全に正しくなかったということです(たとえば、コンテキストが設定されていません)。
ここに次のスタックがあります。
- (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];
}