View Full Version : World Coordinates -> Depth Buffer Coordinates
06-12-2006, 05:26 PM
I was wondering if there's a nice way to convert world coordinates to depth buffer coordinnates. Right now I'm doing this:
float depthBufferZ(float worldZ)
glReadPixels(viewport+viewport/2, viewport+viewport/2, 1,1, GL_DEPTH_COMPONENT,GL_FLOAT, &r);
}But there has to be a better way... doesn't there?
06-12-2006, 09:33 PM
There absolutely is a better way (btw, this is definately not advanced). Math is your friend!
Honestly, I'm not sure if I should tell you the answer or have you go get a math book. I'm going to give you the answer because I'm "nice", but I would highly recommend you investing in some graphics related math books big time.
First things first, you cannot calculate depth by just giving a Z component. If any of your matrices contain a rotation or a shear, then your calculation is wrong. To do this correctly, you need a full coordinate in object space.
Here is some psuedo-code to express what you should do mathematically:
matrix4x4 modelToProj = proj * modelToView;
vector4 temp = modelToProj * vector4( xyzInModelSpace, 1.0f );
float depth = temp.z / temp.w;
This is the same calculation that the currently installed GL renderer is doing for you (note that there will be some small differences due to the fact that GPUs and CPUs handle floating point math in a different manner with different precision errors/limitations).
Now then, go get a graphics book and happy learning/coding! =)
- Kevin B
06-12-2006, 09:48 PM
Oh, I didn't even pay attention to which board I was posting in.
By "modelToView" do you just mean the modelview matrix?
I have a copy of "Mathematics for 3D Game Programming & Computer Graphics." Maybe I should read through it.
Originally posted by Tokage:
I have a copy of "Mathematics for 3D Game Programming & Computer Graphics." Maybe I should read through it. Yes, you surely should. Books aren't just decorative stuffs to show to your (girl) friends in order to impress them... :D
Powered by vBulletin® Version 4.2.2 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.