views:

79

answers:

2

Hello, I'm trying to get a depth buffer working so I can start making a game for iOS. I'm fine with OpenGL and Objective-C for OSX but this is my first time making an iphone application.

I removed the ES1Renderer and ES2Renderer classes and moved the ES1 code into the EAGLView class. The default animation worked when I did that. When I tried to use perspective it went wrong. I get a white screen now. Can someone tell me what I'm doing wrong?

In the initialisation method:

// Create default framebuffer object.
    glGenFramebuffersOES(1, &defaultFramebuffer);
    glGenRenderbuffersOES(1, &colorRenderbuffer);
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer);
    //Initialisation code for the game's graphics and to develop the scene
    glEnable(GL_DEPTH_TEST); //Enables Depth Testing
    glDepthFunc(GL_LEQUAL);
    glDepthMask(GL_TRUE);
    //glEnable(GL_CULL_FACE);
    //glCullFace(GL_FRONT);
    //Setup projection matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    double xmax = 0.04142135624 * ((float) backingWidth)/backingHeight;
    glFrustumf(-xmax, xmax, -0.04142135624, 0.04142135624, 0.1, 2000); //The ymax and min have been precalculated
    glMatrixMode(GL_MODELVIEW); //Select The Modelview Matrix
    glLoadIdentity();

In the drawView method:

    static const GLfloat squareVertices[] = {
        1,   1,-0.1f,
        -1,   1,-0.1f,
        1,  -1,0.1f,
        -1,  -1,0.1f,
    };

    static const GLubyte squareColors[] = {
        0,0,200,255,
        40,90,250,255,
        0,0,200,255,
        50,100,230,255,
    };
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClear(GL_DEPTH_BUFFER_BIT);
    glVertexPointer(3, GL_FLOAT, 0, squareVertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
    glEnableClientState(GL_COLOR_ARRAY);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];

In the layoutSubviews method:

// Allocate color buffer backing based on the current layer size
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
    [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*) self.layer];
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
    glGenRenderbuffersOES(1, &depthRenderbuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
    glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

    if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES)  != GL_FRAMEBUFFER_COMPLETE_OES) 
    {
        NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) );
        return NO;
    }

    return YES;
    [self drawView:nil];

I've done everything it seems I have to do. I don't know why it's so much more complicated for iOS.

Thankyou for any help.

A: 

I might be reading this wrong, but you are defining a clipping box with:

glFrustumf(-xmax, xmax, -0.04142135624, 0.04142135624, 0.1, 2000);

And the object that you're trying to render with:

static const GLfloat squareVertices[] = { 
        1,   1,-0.1f, 
        -1,   1,-0.1f, 
        1,  -1,0.1f, 
        -1,  -1,0.1f, 
    };

At the top you are clipping Z values from 0.1 to 2000. But your vertices have Z values from -0.1 to 0.1. These don't see to overlap.

You might want to try moving the object that you want to look at into the area where you can see it.

No one in particular
I tried moving it all over the place. I have found out that I forgot to set the viewport so I'll try again...It still wont work and I have the z values at 10.I have the viewport setup in the init part just before I setup the projection matrix:glViewport(0, 0, backingWidth, backingHeight);Thank you for the answer.
Matthew Mitchell
A: 

Have a look at the code you can find at this page the author inits an EAGLView subclass parametrizing the method in the case he needs a depth buffer

rano
Thank you. I've looked and it seems to setup the depth buffer the same way as mine. I can't run it because it uses the old SDK. How can I change the SDK to the new one so I can run it?Thank you for the reply.
Matthew Mitchell
THank you so much! I figured it out eventually, how to change the SDK.I got something to render after I copied the camera code and the glPushMatrix() bit. I'm not sure which one of those was required. I suspect it's the latter. In normal OpenGL, you should have something shown without translating the matrix and glPushMatrix() is never needed.Nowhere told me it was needed. I'm just glad to have Something rendered. I think I can figure things out from here.Thank you so very much.
Matthew Mitchell
DO not thank me, thank that blogger ^^. BTW, for changing the SDK required Go to Project>Edit Project Settings>Architectures>Base SDK
rano