I need to create this software rasterizer that given the projection (P
), view (V
) and model (M
) matrices, can create the 2D image of a point cloud (pc
) from the given point of view in a bitmap format (a monochrome bitmap).
I've got the math down (and things seem to be working for the most part):
- Transform the point cloud's points
pc' = (P x V x M) x pc
(note that the point cloud is already in homogeneous system) - For each point, divide all components by its
w
(while being careful to discard points that havew
close to zero. - Discard points that fall outside the view frustum (by extracting the frustum planes from the
P
using the method described here) - Transform
x
andy
coordinates of each point to screen coordinates using(x + 1) * imageWidth / 2
and(-y + 1) * imageHeight / 2
(to have the correct y coordinate). - Map the resulting
x
andy
coordinates to bitmap linear index using(int)y * imageWidth + (int)x
(with bound-checking).
It seems that everything works fine: I get the exact bitmap as if I were rendering it with OpenGL, rotating the point cloud by an arbitrary quaternion still gives valid results.
Things are good until I have a translation component in matrix M
! As soon as I have the slightest amount of translation, the image breaks: the point cloud gets heavily distorted (as if a non-affine transform has been applied to it). It doesn't matter along which direction the translation is applied, ANY translation messes everything up to the point that the point cloud is not recognizable anymore. At first I though my model matrix was transposed (resulting in a non-affine transformation), but that doesn't appear to be the case.
I could post some code if needed, but given the above overview, am I missing anything?? Is there any special consideration that may be needed??