我是iOS中OpenGL ES编程的绝对初学者。
这是我第一次尝试使用OpenGL ES将一些简单的2D基元绘制到视图上。
这是类声明:
@interface OGLGameCanvas : UIView <GameCanvas> {
EAGLContext* context;
Game* game;
GLuint framebuffer, renderbuffer, depthbuffer;
}
这是我的初始化代码:
- (void)initialize {
// Get the layer and set properties
CAEAGLLayer* layer = (CAEAGLLayer*)self.layer;
layer.opaque = NO;
layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
// Set the context
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!context || ![EAGLContext setCurrentContext:context])
DLog(@"Cannot create EAGLContext!");
// Create the color buffer and the render buffer
glGenFramebuffers(1, &framebuffer);
glGenRenderbuffers(1, &renderbuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)layer];
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
NSLog(@"Failed to make complete frame buffer: %x", status);
// Get width and height of the render buffer
GLint width, height;
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
// Create and start animation loop
CADisplayLink* displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawFrame:)];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
和我的绘图代码:
- (void)drawFrame:(CADisplayLink*)sender {
glLoadIdentity();
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
GLfloat vertices[] = { -20.0f, 2.5f, 0.0f, 0.0f, 1.5f, 7.5f };
glVertexPointer(2, GL_FLOAT, 0, vertices);
glColor4f(1.0, 0.0, 0.0, 1.0);
glPointSize(5.0);
glDrawArrays(GL_POINTS, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
[context presentRenderbuffer:GL_RENDERBUFFER];
}
画布被清除(事实上,它变成黑色或红色,或者我设置为glClearColor的任何东西),但没有画出点。
我很确定我忘记了一些基本和必要的东西。
感谢您的帮助。
答案 0 :(得分:2)
问题在于:
GLfloat vertices[] = { -20.0f, 2.5f, 0.0f, 0.0f, 1.5f, 7.5f };
标准化设备坐标位于[-1..1]范围内,因此您将它们绘制在可见区域之外。
在这里:
在绘图之前你必须这样做。
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();