Jump to content
  • Advertisement
Sign in to follow this  
CirdanValen

OpenGL Depth Buffer for a 2D scene

This topic is 2524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!