Orthographic Projection

Merry christmas! Have sometimes to play. So let's go.

Simple image for expalan Orthographic Projection:

It is a way that using 2D views representing 3D object. In case we're using 3D vector and show the front view using 2D for simple fun.

Define 3D vector

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

We're going to create an 3D cube as 9 x 9 x 9 cube by using 3D vector.

vec3_t cube_points[9 * 9 * 9];

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;        
      }
    }
}

orthogaphic projection function:

vec2_t project(vec3_t point) {

  vec2_t projected_point = {
    .x = (fov * point.x),
    .y = (fov * point.y)
  };
  return projected_point;
}


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;
}

Really simple, just take out 3D vector x,y and then print out those points.

Result:

Resources: pikuma course

Date: 2023-12-25 Mon 00:00

Author: Terry Fung

Created: 2024-11-10 Sun 14:09

Emacs 29.4 (Org mode 9.6.15)

Validate