I'm trying to make a software renderer, but I want to avoid creating extra triangles with clipping. For this I want to transform lines which compose triangles, instead of individual points. However I'm not sure how to do a perspective transformation on a line equation.
-
$\begingroup$ How is the line represented? $\endgroup$– pmw1234Commented Oct 10, 2021 at 20:58
-
$\begingroup$ Doesn't matter, so long I can easily flatten it to XY and check if a point(pixel) is on its left or right side. That is, the representation should contain the direction of the line(though I could just use an extra bool if needed). Could be a pair of XY coordinates, implicitly at z=0, z=1; or a pair of ax+by=c; anything thats "easy to work" with. $\endgroup$– user369070Commented Oct 11, 2021 at 0:43
1 Answer
There are many methods you could use to go about doing this, here is one using the parametric form of a line which is: $L(t) = p+t\vec v$ Where $p$ is a point on the line and would be stored as a 4 component vector $p = \{x,y,z,1\}$ and $\vec v$ is a vector that would be stored as a 4 components $\vec v = \{x,y,z,0\}$
This form translates with a 4x4 matrix just like any point/vector would by simply transforming both the vector and the point. A 1 is put in the 4th component of a point since it has a location in 3D space and the 0 in the 4th component of vector reflects that a vector has magnitude and direction but no position.
The point can be chosen as one of the ends of the line (it doesn't matter which just be consistent), and the vector is computed just like you would expect. IE if the line has the two end points $a$ and $b$ and the $a$ is chosen as the start, then the vector is computed as $b-a$.
Do not normalize the vector and just vary $t$ between 0 and 1 for every line. Though you may want to calculate the magnitude of the vector (which corresponds to the length of the line) to determine how many points on the line you want to compute WRT the resolution of the image.
Edit: This can be done in 2D and works exactly the same way. The points would just be x,y,1 and the vectors would be x,y,0 and the transformation matrix becomes a 3x3 matrix.