orthographic projection.

This commit is contained in:
Tijani Lawal 2024-08-31 15:31:33 -05:00
parent af0033cace
commit 7e65bd9ab6
4 changed files with 53 additions and 24 deletions

View File

@ -1,4 +1,3 @@
// clang-format off
// clang-format on

View File

@ -7,14 +7,34 @@
#include "renderer/display.c"
#define POINTS (9 * 9 * 9)
Vec3F32 cube_points[POINTS];
Vec2F32 g_projected_points[POINTS];
int is_running;
// Orthographic Projection - convert 3D points into a 2D projected point
Vec2F32 orthographic_projection(Vec3F32 points) {
Vec2F32 projected_point = {.x = (field_of_view_factor * points.x), .y = (field_of_view_factor * points.y)};
return projected_point;
}
void setup(void) {
colour_buffer = (u32 *)malloc(sizeof(u32) * window_width * window_height);
colour_buffer_texture =
SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, window_width, window_height);
}
int point_count = 0;
// Load array of vectors, from -1 to 1 in the 9*9*9 cube
for (f32 x = -1; x <= 1; x += 0.25) {
for (f32 y = -1; y <= 1; y += 0.25) {
for (f32 z = -1; z <= 1; z += 0.25) {
Vec3F32 new_points = {.x = x, .y = y, .z = z};
cube_points[point_count++] = new_points;
}
}
}
}
void process_input(void) {
SDL_Event event;
@ -35,16 +55,27 @@ void process_input(void) {
}
void update(void) {
for (s32 i = 0; i < POINTS; i++) {
Vec3F32 point = cube_points[i];
// Project the current point
Vec2F32 projected_point = orthographic_projection(point);
// Save the projected 2D vector in the global array of projected points
g_projected_points[i] = projected_point;
}
}
void render(void) {
draw_dots(0xFFFFFFFF);
draw_grid(0xFF000000);
SDL_SetRenderDrawColor(renderer, 28, 450, 560, 255);
SDL_RenderClear(renderer);
// Loop through all projected points and render them
for (u32 i = 0; i < POINTS; i++) {
Vec2F32 projected_point = g_projected_points[i];
draw_rect(projected_point.x + (window_width / 2), projected_point.y + (window_height / 2), 4, 4, 0xFFFFFF00);
}
render_colour_buffer();
clear_colour_buffer(0xFF384893);
clear_colour_buffer(0xFF000000);
SDL_RenderPresent(renderer);

View File

@ -10,8 +10,8 @@ int initialize_window(void) {
// window_width = display_mode.w;
// window_height = display_mode.h;
window = SDL_CreateWindow("Ragar: 3D Software Renderer", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, window_width,
window_height, 0);
window = SDL_CreateWindow("Ragar: 3D Software Renderer", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
window_width, window_height, 0);
if (!window) {
fprintf(stderr, "SDL could not create a window.\n");
return false;
@ -36,9 +36,9 @@ void destroy_window(void) {
}
void clear_colour_buffer(u32 colour) {
for (int i = 0; i < window_height; i++) {
for (int j = 0; j < window_width; j++) {
colour_buffer[(window_width * i) + j] = colour;
for (int y = 0; y < window_height; y++) {
for (int x = 0; x < window_width; x++) {
colour_buffer[(window_width * y) + x] = colour;
}
}
}
@ -59,26 +59,24 @@ void draw_dots(u32 colour) {
void draw_grid(u32 colour) {
for (s32 column = 0; column < window_height; column++) {
for (s32 row = 0; row < window_width; row++) {
if ((row % 10) == 0 || (column % 100) == 0) {
colour_buffer[(window_width * column) + row] = colour;
}
}
}
}
void draw_rect(s32 x, s32 y, s32 width, s32 height, s32 colour) {
void draw_rect(s32 x, s32 y, s32 width, s32 height, u32 colour) {
for (int row = 0; row < width; row++) {
for (int column = 0; column < height; column++) {
s32 l_row = row + x;
s32 l_column = column + y;
draw_pixel(l_row, l_column, colour);
// colour_buffer[(window_width * l_column) + l_row] = colour;
}
}
}
void draw_pixel(u32 x, u32 y, u32 colour) {
void draw_pixel(s32 x, s32 y, u32 colour) {
if (((x >= 0) && (x < window_width)) && ((y >= 0) && (y < window_height))) {
colour_buffer[(window_width * y) + x] = colour;
}
}

View File

@ -7,19 +7,20 @@ SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *colour_buffer_texture = NULL;
u32 *colour_buffer;
s32 window_width = 800;
s32 window_height = 600;
global s32 window_width = 800;
global s32 window_height = 600;
global f32 field_of_view_factor = 128;
global u32 *colour_buffer;
int initialize_window(void);
void destroy_window(void);
void clear_colour_buffer(u32 colour);
void render_colour_buffer(void);
void draw_pixel(u32 x, u32 y, u32 colour);
void draw_pixel(s32 x, s32 y, u32 colour);
void draw_grid(u32 colour);
void draw_dots(u32 colour);
void draw_rect(s32 x, s32 y, s32 width, s32 height, s32 colour);
void draw_rect(s32 x, s32 y, s32 width, s32 height, u32 colour);
#endif // RAGAR_DISPLAY_H