Skip to main content TerryFunggg Blog

Orthographic Projection

Merry christmas! Having times to play.

Simple image for expalan Orthographic Projection:

Porject 3D object (x, y , z) just without z.

See example how to make a 3D cube project to screen using Orthographic Projection.

Define common 3D vector first:

c code snippet start


typedef struct {
  float x;
  float y;
  float z;
} vec3_t;

c code snippet end

Define 3D cube size 9x9x9

c code snippet start


vec3_t cube_points[9 * 9 * 9];

c code snippet end

init all vectors

c code snippet start


for(float x = -1; x <= 1; x += 0.25) {
    for(float y = -1; y <= 1; y += 0.25) {
      for(float z = -1; z <= 1; z += 0.25) {
        vec3_t point = { .x = x, .y = y, .z = z}; // c99 format

        cube_points[point_count++] = point;
      }
    }
}

c code snippet end

Projection function, basiclly it just pipe 3D vector and return 2D vector:

c code snippet start


vec2_t project(vec3_t point) {

  vec2_t projected_point = {
    .x = (fov * point.x),  // fov is magic number to zoom in the point, define any number you like
    .y = (fov * point.y)
  };
  return projected_point;
}

c code snippet end

Apply this function to all vectors:

c code snippet start


for(int i = 0; i < (9 * 9 * 9); i++) {
    vec3_t point = cube_points[i];

    // Project the current point
    vec2_t projected_point = project(point);
    // Save the projected 2D vector in the array
    projected_points[i] = projected_point;
}

c code snippet end

Result: