Question

I'm making a 2D videogame. Right now I don't have that many sprites and one texture with no depth buffer works fine. But when I expand to multiple textures I want to use a depth buffer so that I don't have to make multiple passes over the same texture and so that I don't have to organize my textures with respect to any depth constraints.

When I try to get the depth buffer working I can only get a blank screen with the correct clear color. I'm going to explain my working setup without the depth buffer and list questions I have for upgrading to the depth buffer:

  • Right now my vertices only have position(x,y) and texture(x,y) coords. There is nothing else. No lighting, no normals, no color, etc. Is it correct that the only upgrade I have to make here is to add a z coord to my position?

  • Right now I am using:

glOrthof(-2, 2, -3, 3, -1, 1);

this works with no depth buffer. But when I add the depth buffer I think I need to change the near and far values. What should I change them to?

  • Right now for my glTexImage2D() I am using:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

when I add the depth buffer do I have to change any of those arguments?

  • With my call to glClearDepthf();, should I be using one of the near or far values that I use in my call to glOrthof()? which one?
Was it helpful?

Solution

Since your working with 2D and ortho I find that it helps to have a viewport with coordinates that match your resolution, so this will keep things more readable:

CGRect rect = self.view.bounds;
if (ORTHO) {
    if (highRes && (retina == 1)) {
        glOrthof(0.0, rect.size.width/2, 0.0 ,  rect.size.height/2, -1, 1000.0);
    } else {
        glOrthof(0.0, rect.size.width, 0.0 ,  rect.size.height, -1, 1000.0);    
    }
    glViewport(0, 0, rect.size.width*retina, rect.size.height*retina);
} 

Notice that I always use 320x480 coordinates even on retina, this way I can use the same coordinates for both res, and a .5 will give me pixel perfect on retina, but you can go the other way.

Regarding depth I use a -1 to 1000 depth, so I can draw up to -1000 Z.

Make sure you're binding the depth buffer correctly, something like this:

    // Need a depth buffer
    glGenRenderbuffersOES(1, &depthRenderbuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
    glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES,  framebufferWidth, framebufferHeight);
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

Or your problem can be as simple as using a depth that's behind your camera and lights or bigger than your buffer, try to use a depth between 0 and -1 (-0.5 for ex.), with my glOrthof you can go up to -1000;

EDIT

Values in glOrthof for near and far specify a quantity (distance), not coordinates, this can be confusing when specifying depth values. When you specify 1000 for the far parameter, what we are actually saying is the far clipping plane is a 1000 units distant from the viewer, the same with the near field, unfortunately specifying a clipping plane behind the viewer will take negative values, which contributes to the confusion.

So when it comes drawing time we have a clipping plane that's 1000 units from the viewer in front (far or into the screen), in terms of coordinates Z is negative when bellow the viewing plane (into the screen), our actually drawing world is between Z = 1 and Z = -1000, being -1000 the farthest we can go with these parameters.

OTHER TIPS

If you arn't going to use an exisiting library lie Cocos2D for example then you will have to write a manager to manage the Depth buffer yourself based on either

  • Order that they were added to the screen
  • User Customised Z value so you can swap them around as needed
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top