Hacker News new | ask | show | jobs
by falsifian 1614 days ago
The 4D coordinates are almost treated as ratios. (x,y,z,w) = (tx,ty,tz,tw) when t is positive, but if t is negative it's a different point.

This happens to work with (Open/Web)GL. Here's my understanding of why. Say you tell WebGL to draw a triangle with points at clip space coordinates (i.e. after all the transforming is done) (1,0,0,1), (0,1,0,1), (0,0,0,1). If you multiply all those by a positive number, e.g. (10, 0, 0, 10), (0, 10, 0, 10), (0, 0, 0, 10), you get the same triangle. But if you multiply by a negative number, they will not be drawn at all: (-1, 0, 0, -1), (0, -1, 0, -1), (0, 0, 0, -1) is not drawn. I think this is because of the rules for clipping: only points where -w <= x,y,z <= w survive the clipping step. If w is negative, that's impossible.

Related: my code draws the universe twice on each frame. First I draw things that are more than halfway around the sphere from you, and then I reset the z buffer and draw things that are closer to you. https://git.sr.ht/~falsifian/s3d/tree/main/item/src/S3D/Draw...

1 comments

Another thing maybe worth mentioning: the magnitudes matter when GL is doing interpolation, which is especially noticeable when there are textures.

That is, if you scale just one of the three points of the triangle, e.g. (1, 0, 0, 1), (0, 1, 0, 1), (0, 0, 0, 1000), then while the triangle will appear in the same place as before, interpolation (e.g. deciding which part of the texture to draw in each pixel of the triangle) changes.

The current version of the demo doesn't have any textures but that's mostly laziness on my part. For a while I gave the rooms "ceilings" which were each a checkerboard-pattern square, which forced me to figure out how to get the interpolation thing right. So I can confirm it can be done. But I dropped the ceilings because they just got in the way.