Basic cube
I started by drawing a simple cube to test my 3D transformation algorithm.
void Renderer_DrawPoints(Mesh *mesh)
{
// Render mesh points
for (int i = 0; i < mesh->points_count; ++i) {
double x = mesh->points[i].x;
double y = mesh->points[i].y;
double z = mesh->points[i].z;
// Model-space rotation
{
double rx = x * mesh_rot_cos - z * mesh_rot_sin;
double rz = x * mesh_rot_sin + z * mesh_rot_cos;
x = rx;
z = rz;
}
// Model-space transform
x += pos_x;
y += pos_y;
z += pos_z;
// Camera-space transform
x += camera.x;
y += camera.y;
z += camera.z;
// Camera-space rotation
{
double rx = x * camera_rot_cos - z * camera_rot_sin;
double rz = x * camera_rot_sin + z * camera_rot_cos;
x = rx;
z = rz;
}
// Culling
if (z <= 0) {
continue;
}
// Perspective divide
x /= z;
y /= z;
// Screen-space transform (remap from [-0.5, 0.5] to [0, screen_dimension]
x = (0.5 + x) * screen_dimension + screen_dimension_offset_x;
y = (0.5 + y) * screen_dimension + screen_dimension_offset_y;
// Draw points
DrawPoint(screen_buffer, screen_width, screen_height, x, y, 0xFFFF);
}
}