Formatting changes; added vectors.

This commit is contained in:
Tijani Lawal 2024-08-30 17:37:59 -05:00
parent 6536ec9afb
commit c821949354
4 changed files with 57 additions and 18 deletions

View File

@ -11,8 +11,9 @@
#include "renderer/display.c" #include "renderer/display.c"
#include "renderer/vector.c" #include "renderer/vector.c"
Vec3 cube_points[9 * 9 * 9] = {0}; // 9 x 9 x 9 cube #define POINTS (9 * 9 * 9)
Vec2 projected_points[9 * 9 * 9]; Vec3F32 cube_points[POINTS];
Vec2F32 projected_points[POINTS];
int is_running; int is_running;
@ -21,12 +22,14 @@ void setup(void) {
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);
// Load vector array
int point_count = 0; int point_count = 0;
for (f32 x = -1; x <= 1; x += 0.25) {
for (f32 y = -1; y <= 1; y += 0.25) { // Start loading an array of vectors
for (f32 z = -1; z <= 1; z += 0.25) { // from -1 to 1 (in the 9*9*9 cube)
Vec3 new_point = {.x = x, .y = y, .z = z}; 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_point = {.x = x, .y = y, .z = z};
cube_points[point_count++] = new_point; cube_points[point_count++] = new_point;
} }
} }
@ -34,10 +37,10 @@ void setup(void) {
} }
// Orthographic projection // Orthographic projection
Vec2 project(Vec3 point) { // Vec2F32 project(Vec3F32 point) {
Vec2 projected_point = {.x = point.x, .y = point.y}; // Vec2F32 projected_point = {.x = point.x, .y = point.y};
return projected_point; // return projected_point;
} // }
void process_input(void) { void process_input(void) {
SDL_Event event; SDL_Event event;
@ -57,10 +60,20 @@ void process_input(void) {
} }
} }
// Convert 3D projection -> 2D
Vec2F32 project(Vec3F32 point) {
Vec2F32 projected_point = {.x = point.x, .y = point.y};
return projected_point;
}
void update(void) { void update(void) {
for (int i = 0; i < (9*9*9); i++) { for (int i = 0; i < POINTS; ++i) {
Vec3 point = cube_points[i]; Vec3F32 point = cube_points[i];
Vec2 projected_point = project(point);
// Project the current point
Vec2F32 projected_point = project(point);
// save the projected 2D vector in the array of projected_points
projected_points[i] = projected_point; projected_points[i] = projected_point;
} }
} }
@ -68,9 +81,15 @@ void update(void) {
void render(void) { void render(void) {
// FrameMarkStart("render"); // FrameMarkStart("render");
draw_grid(0xFFFFFFFF); draw_grid(0xFFFFFFFF);
SDL_SetRenderDrawColor(renderer, 28, 450, 560, 255); // Loop all projected points and render them
SDL_RenderClear(renderer); for (int i = 0; i < POINTS; i++) {
Vec2F32 projected_point = projected_points[i];
draw_rect(projected_point.x, projected_point.y, 4, 4, 0xFFFFFF00);
}
// SDL_SetRenderDrawColor(renderer, 28, 450, 560, 255);
// SDL_RenderClear(renderer);
render_colour_buffer(); render_colour_buffer();
// clear_colour_buffer(0xFF384893); // clear_colour_buffer(0xFF384893);

View File

@ -71,7 +71,10 @@ void draw_rect(s32 x, s32 y, s32 width, s32 height, s32 colour) {
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;
colour_buffer[(window_width * l_column) + l_row] = colour;
draw_pixel(l_row, l_column, colour);
// colour_buffer[(window_width * l_column) + l_row] = colour;
} }
} }
} }

0
src/renderer/vector.c Normal file
View File

17
src/renderer/vector.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef VECTOR_H
#define VECTOR_H
typedef struct Vec2F32 Vec2F32;
struct Vec2F32 {
f32 x;
f32 y;
};
typedef struct Vec3F32 Vec3F32;
struct Vec3F32 {
f32 x;
f32 y;
f32 z;
};
#endif // VECTOR_H