diff --git a/src/camera.c b/src/camera.c new file mode 100644 index 0000000..e69de29 diff --git a/src/camera.h b/src/camera.h new file mode 100644 index 0000000..7e836e2 --- /dev/null +++ b/src/camera.h @@ -0,0 +1,11 @@ +#ifndef CAMERA_H +#define CAMERA_H + + +typedef struct { + vec3_t position; + vec3_t rotation; + float fov_angle; +} camera_t; + +#endif diff --git a/src/display.c b/src/display.c index b1655a0..abb1f84 100644 --- a/src/display.c +++ b/src/display.c @@ -1,3 +1,4 @@ +#include #include "display.h" SDL_Window *window = NULL; @@ -64,26 +65,34 @@ void render_color_buffer(void) { SDL_RenderCopy(renderer, color_buffer_texture, NULL, NULL); } -void draw_grid(int size) { +void draw_grid() { if (color_buffer == NULL) { return; } for (int y = 0; y < window_height; y++) { for (int x = 0; x < window_width; x++) { - if (y % size == 0 || x % size == 0) { - draw_pixel(x, y, 0xFF333333); + if (y % 10 == 0 || x % 10 == 0) { + color_buffer[(window_width * y) + x] = 0xFF444444; } } } } +void draw_pixel(int x, int y, uint32_t color) { + if ( x >= 0 && x < window_width && y >= 0 && y < window_height) { + color_buffer[(window_width * y) + x] = color; + } +} + void draw_rect(int x, int y, int width, int height, uint32_t color) { if (color_buffer == NULL) { return; } - for (int i = y; i < height + y; i++) { - for (int j = x; j < width; j++) { - draw_pixel(x + j, y + i, color); + for (int i = 0; i < width; i++) { + for (int j = 0; j < height; j++) { + int current_x = x + i; + int current_y = y + j; + draw_pixel(current_x, current_y, color); } } } @@ -96,8 +105,3 @@ void destroy_window(void) { SDL_Quit(); } -void draw_pixel(int x, int y, uint32_t color) { - if (x < window_width && y < window_height) { - color_buffer[(window_width * y) + x] = color; - } -} diff --git a/src/display.h b/src/display.h index 4fa093f..0358166 100644 --- a/src/display.h +++ b/src/display.h @@ -15,7 +15,7 @@ extern int window_height; bool initialize_window(void); -void draw_grid(int size); +void draw_grid(); void draw_pixel(int x, int y, uint32_t color); diff --git a/src/main.c b/src/main.c index c6f8d94..7b82d17 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,8 @@ //////////////////////////////////////////////////////////// #define N_POINTS (9 * 9 * 9) vec3_t cube_points[N_POINTS]; +vec2_t projected_points[N_POINTS]; +float fov_factor = 128; bool is_running = false; @@ -52,19 +54,44 @@ void process_input(void) { } } +//////////////////////////////////////////////////////////////////////////////// +// Function that receives a 3d Vector and returns a 2d projected point +//////////////////////////////////////////////////////////////////////////////// +vec2_t project(vec3_t point) { + vec2_t projected_point = { + .x = (fov_factor * point.x), + .y = (fov_factor * point.y), + }; + return projected_point; +} + void update(void) { + for (int i = 0; i < N_POINTS; i++){ + vec3_t point = cube_points[i]; + vec2_t projected_point = project(point); + + projected_points[i] = projected_point; + } } void render(void) { - SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); - SDL_RenderClear(renderer); + draw_grid(); + + for (int i = 0; i < N_POINTS; i++){ + vec2_t projected_point = projected_points[i]; + draw_rect( + projected_point.x + (window_width/2), + projected_point.y + (window_height/2), + 4, + 4, + 0xFFFFFF00 + ); + } - clear_color_buffer(0xFF000000); - draw_grid(10); - draw_rect(50, 50, 640, 480, 0xFFDD00DD); render_color_buffer(); + clear_color_buffer(0xFF000000); SDL_RenderPresent(renderer); } diff --git a/src/vector.c b/src/vector.c new file mode 100644 index 0000000..db96a88 --- /dev/null +++ b/src/vector.c @@ -0,0 +1,3 @@ +#include "vector.h" + +// Implement all vector functons diff --git a/src/vector.h b/src/vector.h new file mode 100644 index 0000000..0b54d31 --- /dev/null +++ b/src/vector.h @@ -0,0 +1,18 @@ +#ifndef VECTOR_H +#define VECTOR_H + +typedef struct { + float x; + float y; +} vec2_t; + + +typedef struct { + float x; + float y; + float z; +} vec3_t; + + + +#endif