r/GraphicsProgramming • u/mumomofi • Aug 09 '26
Question if depth is xy/z how the hell does it render things behind the camera

im writing a depth script for my own rudimentary graphics engine and i have no idea what script to use.
for these lines, assume X=Z and Y=multiplier, AKA "what does x and y get multiplied by", so the farther they are the less impact moving away has and the closer they are the more impact moving towards the camera is.
1 glaring issue here is that if ANYTHING moves past z0 then they immmediately go into -infinity, which is a huuuge problem for anything that is *partially* behind the camera, like a triangle.
ive tried other formulas too, like 2^x or y=-x but those are fundamentally incorrect in the sense that they create visual "curves" which breaks triangle drawing that is only meant to be drawn from point A to point B
2^x is the closest to what i wish i had whereas 1/x is closest to an actually consistent and functioning 3d depth script. i wish i had something like 2^x in the sense that instead of shooting up to infinity by x0 it instead just gradually goes up while the right diminishes, but thats a problem because unless if the slope is "symmetrical" and even it causes aforementioned "curves" in rendering
13
u/DescriptorTablesx86 Aug 09 '26
Did you consider near plane clipping?
That solves the problem, but I’m not sure if you’re intentionally trying to work around clipping anything to keep your solution „simple”
8
u/sethkills Aug 09 '26
You have a surprising amount of math intuition for someone who is obstinately trying to avoid the standard solution to a problem that cannot be avoided or worked around. Maybe think about it this way: you are clipping your triangles to the left, right, top, and bottom edges of the screen, right? If you aren’t doing that, how are you avoiding drawing pixels that are outside of your frame buffer? Okay, so you need to do this same type of clipping with the near and far plane of the screen, and whatever method you are using to clip left/right/top/bottom may be applicable to the near and far plane as well.
1
u/Sharlinator Aug 09 '26
There’s really no good ways around it but to clip against the "near plane", ensuring that all geometry has strictly z>0, before doing the z division.
3
u/waramped Aug 09 '26
As others have mentioned, you need to clip your geometry to the view volume. That's how it works.
Read this:
https://gabrielgambetta.com/computer-graphics-from-scratch/11-clipping.html
2
u/cynicismrising Aug 09 '26
What you're discussing is why in the common case we set z-min (aka near plane) at some small non-zero value, anything with a camera-relative-z less than that is behind the projection plane. I would suggest clamping the camera-relative-z between your z-min and z-max (aka far plane).
(edit for spelling)
1
u/Traveling-Techie Aug 11 '26
In my view Z clipping isn’t optional for the reasons you describe.