无法在OpenGLES2中使用glDrawElements渲染对象

时间:2014-02-20 00:26:51

标签: ios objective-c opengl-es-2.0

我正在使用XCode(适用于iOS)的简单OpenGLES测试应用程序,我正在尝试使用glVertexPointer和glDrawElements渲染基本几何体。到目前为止,无论我的相机的位置如何,都没有呈现任何内容,而且我不知道自己可能做错了什么。

更新:尝试使用framebuffer对象绘制几何体,但它仍然无法创建一个。这里有一些额外的代码来生成framebuffer对象和renderbuffer对象:

- (BOOL)createFrameBuffer {
glGenFramebuffersOES(1, &viewFramebuffer);
glGenRenderbuffersOES(1, &viewRenderbuffer);

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[self.context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)((GLKView *)self.view).layer];
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);

glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);

glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
    NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
    return NO;
}

return YES;
}

- (void)destroyFrameBuffer {
glDeleteFramebuffersOES(1, &viewFramebuffer);
viewFramebuffer = 0;
glDeleteRenderbuffersOES(1, &viewRenderbuffer);
viewRenderbuffer = 0;

if(depthRenderbuffer) {
    glDeleteRenderbuffersOES(1, &depthRenderbuffer);
    depthRenderbuffer = 0;
}
}

以下是调用的绘图函数:

-(void)glkView: (GLKView *)view drawInRect: (CGRect)rect {

float vertices[18] = {
    0.5f, -0.5f, -0.5f,
    0.5f, 0.5f, -0.5f,
    0.5f, -0.5f, 0.5f,
    0.5f, -0.5f, 0.5f,
    0.5f, 0.5f, -0.5f,
    0.5f, 0.5f, 0.5f
};

short indices[6] = {
    0, 1, 2,
    2, 1, 3
};

Byte colors[24] = {
    255, 255, 255, 255,
    255, 255, 255, 255,
    255, 255, 255, 255,
    255, 255, 255, 255,
    255, 255, 255, 255,
    255, 255, 255, 255
};

[self destroyFrameBuffer];
if([self createFrameBuffer] == NO) {
    return;
}

[EAGLContext setCurrentContext: self.context];

glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);

glDisable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//glViewport(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
glViewport(0, 0, backingWidth, backingHeight);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(_rotation, 1, 1, 1); // rotate object around
glTranslatef(0, 0, -8);

glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);

// draw
glDrawElements(GL_TRIANGLES, 2, GL_UNSIGNED_SHORT, indices);

glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[self.context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

请注意,这会禁用所有效果(灯光,着色器等)。此代码也是从继承GLKViewController的视图控制器中调用的。

0 个答案:

没有答案