3 Attachment(s)
Help on Calculating Depth buffer
Hi Guys,
First post here so please be gentle. I am trying to calulcate z-buffer (or depth buffer) of a polygon/surface. But I am trying to do that a very specific way. I have attached some images that go through the theory of how to do this. It would be nice if you can take some time to through the text. Sorry for the bad image quality, I had to compress the hell out of them. If its too bad I can send better resolution images.
I have gone over this multiple times and cannot understand how to go about it.
The equation seems simple z = (-Ax - YB - D)/C for the first pixel (x,y) and the just use z' = z - A/C for each successessive x. I can follow the theory upto here but then I loose it. There's something about going down the left edge of the polygon, intersection of the scan line with the left edge etc...I can't figure out how to actually program this.
So far I have calculated the (A,B,C,D) the plane coefficents and have the code that detects the left edge of the triangle and calculates the slope. After that I am stumped.
I guess my question is if anyone can help me put this theory into actual code.
Any help will be appreciated.
Re: Help on Calculating Depth buffer
This problems seems interesting.
I went to the Wiki page to learn a more about it and I found that they have an algorithm in psudocode there, so maybe that might help
http://en.wikipedia.org/wiki/Z-buffering#Algorithm
Re: Help on Calculating Depth buffer
You can take a look at how OpenGL does it. OpenGL has been the gold standard in graphics for almost 20 years and they have a very efficient depth buffer. I also know that OpenGL uses an algorithm similar to the one that you're describing. The edge following that you're describing is to make sure that corners fit correctly. If you have two planes coming together, in theory, the left edge of one plane and the right edge of the second plane occupy the same space. Graphics programmers fix this by stating that a polygon owns it's left edge, but not it's right edge. Same for top and bottom (top is owned, bottom is not.)
If I were you, I would just let OpenGL do your depth testing for you, as it's going to be many times faster than anything any of us can produce (it's done in hardware.)
Re: Help on Calculating Depth buffer
hi guys,
Thanks for the replies. I hope you could read those images.
I did look up the wikipedia entry when I was researching for this problem. The problem is not the pseudo-code. I understand that. The problem is the programing itself.
And th problem is that I cannot use the openGL libraries. That's why I am trying to code it myself. I should have stated that earlier.
I am tried to look for the openGl code on how it does this. But i couldn't dig that into openGL. It seems to me that the actual code for this functionality is protected in a library or something.
If I can actually see the openGL code that would be awsome. Otherwise I have to go do it this way
Re: Help on Calculating Depth buffer
OpenGL is completely open source, they wouldn't hide it from you. I don't know how OpenGL is put together, but I'm sure if you downloaded the source and did a search for "DEPTH TEST" you would find the code that you want. It may look very obscure though because, as I said before, depth testing is done on the hardware, so it's going to be all hardware commands.
Re: Help on Calculating Depth buffer
There are open source OpenGL drivers available. That's not the same as OpenGL itself being open source. Some OGL drivers (including most of the ones for Windows) are proprietary.
Mesa probably has a depth-buffer implementation in software.
What's your limitation that doesn't allow you to use OpenGL, anyway?
Re: Help on Calculating Depth buffer
Guys,
I am trying to port some of the openGL functionality directly into a c++ environment. and there are hardware issues also. I was trying to use the above method because I can just calculate (supposedly) one x,y location and all the other pixels are just simple math that doesn't take much clock cycles.
I just need help on the programming part of this. I can't figure out how to actually code this. Please help on the code issue.
Re: Help on Calculating Depth buffer
MESA is a software implementation of OpenGL. You can use that to help you. It's C and assembly, but I'm guessing mostly C.
Re: Help on Calculating Depth buffer
Hi ninja,
i looked in mesa also. It is a c implementation but it is very convoluted, and hard to track all the pieces and frankly my programming knowledge/skill is not upto that level at what the mesa code is written in. That's why i tried to go this particular way using these formula. I have written some code but its obviously not right. I can post that if u would like. Appreciate your help in this
Re: Help on Calculating Depth buffer
Go for it, I've written a depth buffer algorithm before too, most of it was assembly, but the algorithm would be the same.