Depth Buffer for a 2D scene

Started by
-1 comments, last by CirdanValen 12 years, 3 months ago
I'm trying to create a 2D game that uses deferred lighting. I think I have everything figured out except for depth. I need to figure out a way to allow me to render lights behind or in front of objects. An example would be if I have a tree, and I want to render the light behind the tree, the ground and objects behind the tree would be light, but the tree would not. After a bit of looking around, seems like a depth buffer would be my answer but I'm not sure how to create a depth buffer in opengl for a 2D scene. I assume the way I would treat a depth buffer is using the depth of the pixel in conjunction with the z value of the light to decide which pixels get lit.

I don't have anything implemented currently, still just planning things out. I think I am going to sort objects in the scene first by their y position, then by their fake z position.

EDIT: Sorting objects by y and z, I came up with this formulate for calculating the depth clamped to 255. Just need to figure out how to write these values to a depth buffer
depth = y + z
minDepth = objectList.front().y + objectList.front().z
maxDepth = objectList.back().y + objectList.back().z
finalDepth = (255 / maxDepth) * (depth - minDepth)

This topic is closed to new replies.

Advertisement