rudimentary orthographic projection.
This commit is contained in:
parent
af0033cace
commit
8166494994
@ -1,4 +1,3 @@
|
|||||||
// clang-format off
|
// clang-format off
|
||||||
|
|
||||||
|
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
@ -7,14 +7,34 @@
|
|||||||
|
|
||||||
#include "renderer/display.c"
|
#include "renderer/display.c"
|
||||||
|
|
||||||
|
#define POINTS (9 * 9 * 9)
|
||||||
|
Vec3F32 cube_points[POINTS];
|
||||||
|
Vec2F32 g_projected_points[POINTS];
|
||||||
|
|
||||||
int is_running;
|
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) {
|
void setup(void) {
|
||||||
colour_buffer = (u32 *)malloc(sizeof(u32) * window_width * window_height);
|
colour_buffer = (u32 *)malloc(sizeof(u32) * window_width * window_height);
|
||||||
colour_buffer_texture =
|
colour_buffer_texture =
|
||||||
SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, window_width, window_height);
|
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) {
|
void process_input(void) {
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
@ -35,16 +55,27 @@ void process_input(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void update(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) {
|
void render(void) {
|
||||||
draw_dots(0xFFFFFFFF);
|
draw_grid(0xFF000000);
|
||||||
|
|
||||||
SDL_SetRenderDrawColor(renderer, 28, 450, 560, 255);
|
// Loop through all projected points and render them
|
||||||
SDL_RenderClear(renderer);
|
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();
|
render_colour_buffer();
|
||||||
clear_colour_buffer(0xFF384893);
|
|
||||||
clear_colour_buffer(0xFF000000);
|
clear_colour_buffer(0xFF000000);
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
|
@ -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_width,
|
window = SDL_CreateWindow("Ragar: 3D Software Renderer", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||||||
window_height, 0);
|
window_width, 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;
|
||||||
@ -36,9 +36,9 @@ void destroy_window(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void clear_colour_buffer(u32 colour) {
|
void clear_colour_buffer(u32 colour) {
|
||||||
for (int i = 0; i < window_height; i++) {
|
for (int y = 0; y < window_height; y++) {
|
||||||
for (int j = 0; j < window_width; j++) {
|
for (int x = 0; x < window_width; x++) {
|
||||||
colour_buffer[(window_width * i) + j] = colour;
|
colour_buffer[(window_width * y) + x] = colour;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,26 +59,24 @@ void draw_dots(u32 colour) {
|
|||||||
void draw_grid(u32 colour) {
|
void draw_grid(u32 colour) {
|
||||||
for (s32 column = 0; column < window_height; column++) {
|
for (s32 column = 0; column < window_height; column++) {
|
||||||
for (s32 row = 0; row < window_width; row++) {
|
for (s32 row = 0; row < window_width; row++) {
|
||||||
if ((row % 10) == 0 || (column % 100) == 0) {
|
|
||||||
colour_buffer[(window_width * column) + row] = colour;
|
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 row = 0; row < width; row++) {
|
||||||
for (int column = 0; column < height; column++) {
|
for (int column = 0; column < height; column++) {
|
||||||
s32 l_row = row + x;
|
s32 l_row = row + x;
|
||||||
s32 l_column = column + y;
|
s32 l_column = column + y;
|
||||||
|
|
||||||
draw_pixel(l_row, l_column, colour);
|
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;
|
colour_buffer[(window_width * y) + x] = colour;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -7,19 +7,20 @@ SDL_Window *window = NULL;
|
|||||||
SDL_Renderer *renderer = NULL;
|
SDL_Renderer *renderer = NULL;
|
||||||
SDL_Texture *colour_buffer_texture = NULL;
|
SDL_Texture *colour_buffer_texture = NULL;
|
||||||
|
|
||||||
u32 *colour_buffer;
|
global s32 window_width = 800;
|
||||||
s32 window_width = 800;
|
global s32 window_height = 600;
|
||||||
s32 window_height = 600;
|
global f32 field_of_view_factor = 128;
|
||||||
|
global u32 *colour_buffer;
|
||||||
|
|
||||||
int initialize_window(void);
|
int initialize_window(void);
|
||||||
void destroy_window(void);
|
void destroy_window(void);
|
||||||
void clear_colour_buffer(u32 colour);
|
void clear_colour_buffer(u32 colour);
|
||||||
void render_colour_buffer(void);
|
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_grid(u32 colour);
|
||||||
void draw_dots(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
|
#endif // RAGAR_DISPLAY_H
|
||||||
|
Loading…
Reference in New Issue
Block a user