0
我是iOS中OpenGL ES編程的絕對初學者。iPhone上的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的任何東西),但不繪製點。
我很確定我忘記了一些基本的和必不可少的東西。
感謝您的幫助。
謝謝。移動我的第一步......我仍然不理解變換矩陣背後的整個概念。我玩他們,但是,例如,我不能集中「設備座標」的概念...... – gd1
嗯,你真的只需要時間和實踐來理解所有這些東西。我建議你從理解數學開始,因爲它對圖形至關重要。請閱讀本書:http://books.google.com/books/about/Mathematics_for_3D_game_programming_and.html?id=bfcLeqRUsm8C – Max