我正在尝试通过网站学习opengl。我先看例子,然后自己尝试而不看,这样我就有信心提高我的学习水平。但是,自从 20 天以来,我一直在看 opengl 教程,我知道基础知识非常基础,当我尝试用它做一个非常基础的程序时,我失败了。
我有一个 UIView 子类,我正在尝试用 opengl 绘制一个简单的三角形。但是,似乎我什至无法将背景颜色设置为某种恒定颜色。我的 UIView 子类是这样的;
@implementation GLView{
GLuint framebuffer;
GLuint renderbuffer;
EAGLContext *context;
}
+(Class)layerClass{
return [CAEAGLLayer class];
}
-(void)drawView{
glLoadIdentity();
glClearColor(0.7, 0.7, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
CAEAGLLayer *layer = (CAEAGLLayer*)self.layer;
layer.opaque = YES;
[EAGLContext setCurrentContext:context];
if(!context){
NSLog(@"Could not create context");
}
glGenFramebuffers(1, &framebuffer);
glGenRenderbuffers(1, &renderbuffer);
glBindFramebuffer(GL_FRAMEBUFFER_OES, framebuffer);
glBindRenderbuffer(GL_RENDERBUFFER_OES, renderbuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, renderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)layer];
glViewport(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame));
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
[self drawView];
}
return self;
}
如果我理解正确,上面的代码应该将我的视图背景设置为灰色。但是,我确信我错过了一些非常简单的概念。如果有人能指出,那就太好了。谢谢