perspective projection is in baby, with a rudimentary camera in place.

This commit is contained in:
Tijani Lawal 2024-08-31 23:31:09 -05:00
parent 7f69338b88
commit b045bf1858
3 changed files with 19 additions and 4 deletions

View File

@ -10,9 +10,21 @@
#define POINTS (9 * 9 * 9) #define POINTS (9 * 9 * 9)
Vec3F32 cube_points[POINTS]; Vec3F32 cube_points[POINTS];
Vec2F32 g_projected_points[POINTS]; Vec2F32 g_projected_points[POINTS];
Vec3F32 camera_position = {.x = 0, .y = 0, .z = -5};
int is_running; int is_running;
// Perspective Projection
// Perspective divide formula
// P'x = Px/Pz
//
// P'y = Py/Pz
Vec2F32 perspective_projection(Vec3F32 points) {
Vec2F32 projected_point = {.x = (field_of_view_factor * points.x) / points.z,
.y = (field_of_view_factor * points.y) / points.z};
return projected_point;
}
// Orthographic Projection - convert 3D points into a 2D projected point // Orthographic Projection - convert 3D points into a 2D projected point
Vec2F32 orthographic_projection(Vec3F32 points) { Vec2F32 orthographic_projection(Vec3F32 points) {
Vec2F32 projected_point = {.x = (field_of_view_factor * points.x), .y = (field_of_view_factor * points.y)}; Vec2F32 projected_point = {.x = (field_of_view_factor * points.x), .y = (field_of_view_factor * points.y)};
@ -58,8 +70,11 @@ void update(void) {
for (s32 i = 0; i < POINTS; i++) { for (s32 i = 0; i < POINTS; i++) {
Vec3F32 point = cube_points[i]; Vec3F32 point = cube_points[i];
// Move things away from the camera
point.z -= camera_position.z;
// Project the current point // Project the current point
Vec2F32 projected_point = orthographic_projection(point); Vec2F32 projected_point = perspective_projection(point);
// Save the projected 2D vector in the global array of projected points // Save the projected 2D vector in the global array of projected points
g_projected_points[i] = projected_point; g_projected_points[i] = projected_point;

View File

@ -10,8 +10,8 @@ int initialize_window(void) {
// window_width = display_mode.w; // window_width = display_mode.w;
// window_height = display_mode.h; // window_height = display_mode.h;
window = SDL_CreateWindow("Ragar: 3D Software Renderer", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, window = SDL_CreateWindow("Ragar: 3D Software Renderer", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, window_width,
window_width, window_height, 0); window_height, 0);
if (!window) { if (!window) {
fprintf(stderr, "SDL could not create a window.\n"); fprintf(stderr, "SDL could not create a window.\n");
return false; return false;

View File

@ -9,7 +9,7 @@ SDL_Texture *colour_buffer_texture = NULL;
global s32 window_width = 800; global s32 window_width = 800;
global s32 window_height = 600; global s32 window_height = 600;
global f32 field_of_view_factor = 128; global f32 field_of_view_factor = 940;
global u32 *colour_buffer; global u32 *colour_buffer;
int initialize_window(void); int initialize_window(void);