Jump to content

  • Log In with Google      Sign In   
  • Create Account


iOS: Depth Buffer Not Working


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 blueshogun96   Crossbones+   -  Reputation: 888

Like
0Likes
Like

Posted 08 February 2013 - 09:27 PM

I don't know what's going on here, but right now, I can't get the depth buffer to work properly (that or I'm missing something really obvious).  I made a test example to verify that it works, but so far, no depth sorting is taking place.

 

I hate to just dump code and say "Hey, tell me what's wrong", but I've been struggling with this iOS stuff for a long time now, and never has OpenGL been so complicated.

 

From ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    // 1. Create an Xcode Project
    
    // 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];
    
    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 );
    
    // 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);
    
    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 );
    
    glViewport( 0, 0, 320, 480 );
    glMatrixMode( GL_PROJECTION );
    gluPerspective( 45.0f, 320.0f/480.0f, 0.1f, 1000.0f );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
}

 

And my test code

 

-(void) drawView:(id)sender
{
    glClearColor( 0.0f, 0.5f, 0.0f, 1.0f );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    
    glTranslatef( 0.0f, 0.0f, -5.0f );
    
    float v[9] = { 0, 1.0f, 0,
                  1.0f, -1.0f, 0,
                 -1.0f, -1.0f, 0 };
    
    glEnableClientState( GL_VERTEX_ARRAY );
    glVertexPointer( 3, GL_FLOAT, 0, v );
    glColor4f( 1.0f, 0, 0, 0 );
    glDrawArrays( GL_TRIANGLES, 0, 3 );
    
    glTranslatef( 1, 0, -6.0f );
    glColor4f( 1.0f, 1.0f, 0, 0 );
    glDrawArrays( GL_TRIANGLES, 0, 3 );
    glDisableClientState( GL_VERTEX_ARRAY );
    
    [[EAGLContext currentContext] presentRenderbuffer:GL_RENDERBUFFER];
}

 

The second triangle should be behind the first, but it's not.  I ensured that depth testing was enabled, but this still happens.  Also, depending where I add the depth buffer initialization code, it will cause the screen to be solid white.  No idea what's wrong.  This sucks.  Any ideas?  Thanks.

 

EDIT: Nevermind, I fixed it by initializing the depth buffer last and re-binding the colour buffer.  That solved everything.


Edited by blueshogun96, 08 February 2013 - 09:50 PM.

Follow Shogun3D on the official website: http://shogun3d.net

 

blogger.png twitter.png tumblr_32.png facebook.png


Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS