Re:Ask HN: What advice do you have for new CS student?
by EternalCarnage on Monday July 10, 2023 @01:08PM
(#36669331)
Getting the signal from the noise - A demonstration attempt (inspired by @laurieg's comment):
Rendering: how to turn a virtual 3D scene into a (2D) image?
Answer: (affine and perspective) geometry, usually in the form of vectors and points and some physics.
(Matrices, multivectors, are structures for convenience.)
Where Source is your "old" 2D coordinate system interval (e.g., [-1; 1] x [-1; 1]) and Target is your new coordinate system interval (e.g., [0; Width] x [0; Height]).
Although, we usually have to deal with flipped y-coordinates, so:
Rendering: how to turn a virtual 3D scene into a (2D) image? Answer: (affine and perspective) geometry, usually in the form of vectors and points and some physics. (Matrices, multivectors, are structures for convenience.)
The core concepts you need are:
1. Perspective projection (perspective geometry):
X* = H * (X / Z)
Y* = H * (Y / Z)
2. Coordinate mapping (affine geometry):
MapRange(Value, Source, Target) := Value' = Scale * (Value - Source.Min) + Target.Min
Scale := (Target.Max - Target.Min) / (Source.Max - Source.Min)
Where Source is your "old" 2D coordinate system interval (e.g., [-1; 1] x [-1; 1]) and Target is your new coordinate system interval (e.g., [0; Width] x [0; Height]).
Although, we usually have to deal with flipped y-coordinates, so:
[0; Width] x [Height; 0] or for rasterization:
X' = MapRange(X, Source=(-1, 1), Target=(0, Width))
Y' = MapRange(X, Source=(-1, 1), Target=(Height, 0))
... and ray tracing:
X' = MapRange(X, Source=(0, Width), Target=(-1, 1))
Y' = MapRange(Y, Source=(Height, 0), Target=(-1, 1))
3. Containment (affine; rasterization):
w := (1 - u - v), A, B, C from R2.
OP = uAB + vAC + OA <=> AP = uAB + vAC <=> OP = uOB + vOC + (1 - u - v)OA <=> P = uB + vC + (1 - u - v)A = uB + vC + wA
If you solve AP = uAB + vAC for u and v via Cramer's rule or by hand, you may get:
A := Det(AB, AC)
u := Det(AB, AP) / A
v := Det(BC, BP) / A
w := 1 - u - v = Det(CA, CP) / A
P is inside triangle ABC, if u, v, w >= 0 and u + v + w = 1.
4. Intersection + Containment (affine):
<.,.> - the dot product.
t = <PlanePoint - RayOrigin, PlaneNormal> / <RayDirection, PlaneNormal>
If t > 0, the line or ray intersects with the triangle's plane.
X = Ray(t) = RayOrigin + t RayDirection
Now, you can reuse the 2D point containment check (3), to see whether the ray intersects also the triangle (lying on that plane).
---
Filtering the "noise" (overhead/information overload), getting to the "gist", for me, is not easy, but you can get there, if you care enough.