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:
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! =)
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…