I started writing a totally new OpenGL game for iOS a few days ago, and I cannot get 2D working to save my life. 3D works fine (using a customized implementation of gluPerspective), but 2D just won't. I've done a 2D game for iOS before, and I didn't have any problems. Now, no matter what I do, nothing renders.
My code:
Initialization:
// 2. Create a Context
EAGLContext *context = [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1] autorelease];
[EAGLContext setCurrentContext:context];
// 3. Create a View
GLView *glView = [[[GLView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
[self.view addSubview:glView];
// 4. Create a Renderbuffer
GLuint renderbuffer;
glGenRenderbuffers(1, &renderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)glView.layer];
// 5. Create a Framebuffer
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8_OES, 320, 480 );
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer);
GLuint depthBuffer;
glGenRenderbuffers( 1, &depthBuffer );
glBindRenderbuffer( GL_RENDERBUFFER, depthBuffer );
glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 320, 480 );
glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer );
glBindRenderbuffer( GL_RENDERBUFFER, renderbuffer );
GLenum status = glCheckFramebufferStatus( GL_FRAMEBUFFER );
if( status != GL_FRAMEBUFFER_COMPLETE )
{
NSLog( @"Error completing FBO! %x", status );
}
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
glClearDepthf( 1.0f );
glDisable( GL_CULL_FACE );
glDisable( GL_LIGHT0 );
Rendering:
-(void) drawView:(id)sender
{
glViewport( 0, 0, 320, 480 );
glMatrixMode( GL_PROJECTION );
glOrthof( 0, 320, 0, 480, -1, 1);
glClearColor( 0.0f, 0.5f, 0.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
draw_quad( 0, 50, 50, 300, 300 );
[[EAGLContext currentContext] presentRenderbuffer:GL_RENDERBUFFER];
}
void draw_quad( GLuint texture, float x, float y, float w, float h )
{
float vertices[] = { x, y, x+w, y, x+w, y+h, x, y+h };
float tex[] = { 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f };
glBindTexture( GL_TEXTURE_2D, texture );
glTexCoordPointer( 2, GL_FLOAT, 0, tex );
glVertexPointer( 2, GL_FLOAT, sizeof(float)*2, vertices );
glDrawArrays( GL_TRIANGLE_FAN, 0, 4 );
spf++;
}
Majority of this code I'm reusing, but I don't see what the problem is. Arrgh, iOS is a complete NIGHTMARE to work with (problems at every corner for me). I hate to say it, but PS2 was more straight forward than iOS. This really sucks. Any ideas? Thanks.