wireframe cube

This commit is contained in:
Tyrel Souza 2024-04-19 00:02:54 -04:00
parent d38ce455af
commit cec7699ea5
11 changed files with 211 additions and 77 deletions

View File

@ -13,7 +13,7 @@ file(GLOB SOURCES "src/*.c")
add_executable(renderer ${SOURCES}) add_executable(renderer ${SOURCES})
# Link SDL2 library to the executable # Link SDL2 library to the executable
target_link_libraries(renderer PRIVATE ${SDL2_LIBRARIES}) target_link_libraries(renderer PRIVATE ${SDL2_LIBRARIES} m)
# Set compiler options # Set compiler options
target_compile_options(renderer PRIVATE -Wall -g) # -g adds debugging information target_compile_options(renderer PRIVATE -Wall -g) # -g adds debugging information

View File

@ -1,7 +1,7 @@
SRCS = $(shell find ./src -type f -name *.c) SRCS = $(shell find ./src -type f -name *.c)
build: build:
gcc -Wall -std=c99 $(SRCS) -lSDL2 -o renderer gcc -Wall -std=c99 $(SRCS) -lSDL2 -lm -o renderer
./renderer ./renderer
run: build run: build

View File

@ -105,3 +105,25 @@ void destroy_window(void) {
SDL_Quit(); SDL_Quit();
} }
void draw_line(int x0, int y0, int x1, int y1, uint32_t color) {
int delta_x = x1-x0;
int delta_y = y1-y0;
int longest_side_length = abs(delta_x) >= abs(delta_y) ? abs(delta_x) : abs(delta_y);
float xInc = delta_x / (float)longest_side_length;
float yInc = delta_y / (float)longest_side_length;
float current_x = x0;
float current_y = y0;
for (int i = 0; i < longest_side_length; i++) {
draw_pixel(round(current_x), round(current_y), color);
current_x += xInc;
current_y += yInc;
}
}
void draw_triangle(int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color){
draw_line(x0, y0, x1, y1, color);
draw_line(x1, y1, x2, y2, color);
draw_line(x2, y2, x0, y0, color);
}

View File

@ -12,6 +12,8 @@ extern uint32_t *color_buffer;
extern SDL_Texture *color_buffer_texture; extern SDL_Texture *color_buffer_texture;
extern int window_width; extern int window_width;
extern int window_height; extern int window_height;
#define FPS 60
#define FRAME_TARGET_TIME (1000/FPS)
bool initialize_window(void); bool initialize_window(void);
@ -19,6 +21,8 @@ void draw_grid();
void draw_pixel(int x, int y, uint32_t color); void draw_pixel(int x, int y, uint32_t color);
void draw_line(int x0, int y0, int x1, int y1, uint32_t color);
void draw_triangle(int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color);
void draw_rect(int x, int y, int width, int height, uint32_t color); void draw_rect(int x, int y, int width, int height, uint32_t color);
void clear_color_buffer(uint32_t color); void clear_color_buffer(uint32_t color);

View File

@ -3,19 +3,15 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "display.h" #include "display.h"
#include "vector.h" #include "vector.h"
#include "mesh.h"
////////////////////////////////////////////////////////////
// Declare an array of points
////////////////////////////////////////////////////////////
#define N_POINTS (9 * 9 * 9)
vec3_t cube_points[N_POINTS];
vec2_t projected_points[N_POINTS];
vec3_t camera_position = {0, 0, -5}; vec3_t camera_position = {0, 0, -5};
vec3_t cube_rotation = {.x = 0, .y = 0, .z = 0};
triangle_t triangles_to_render[N_MESH_FACES];
float fov_factor = 640; float fov_factor = 640;
bool is_running = false; bool is_running = false;
uint32_t previous_frame_time = 0;
void setup(void) { void setup(void) {
// Allocate the required memory in bytes to hold color buffer // Allocate the required memory in bytes to hold color buffer
@ -29,15 +25,6 @@ void setup(void) {
window_width, window_width,
window_height window_height
); );
int point_count = 0;
for (float x = -1; x <= 1; x += 0.25) {
for (float y = -1; y <= 1; y += 0.25) {
for (float z = -1; z <= 1; z += 0.25) {
vec3_t new_point = { .x = x, .y = y, .z = z };
cube_points[point_count++] = new_point;
}
}
}
} }
void process_input(void) { void process_input(void) {
@ -67,30 +54,60 @@ vec2_t project(vec3_t point) {
return projected_point; return projected_point;
} }
void update(void) { void update(void) {
for (int i = 0; i < N_POINTS; i++){ // lock execution to the next tick
vec3_t point = cube_points[i]; uint32_t time_to_wait = FRAME_TARGET_TIME - (SDL_GetTicks() - previous_frame_time);
point.z -= camera_position.z; if (time_to_wait > 0 && time_to_wait <= FRAME_TARGET_TIME) {
vec2_t projected_point = project(point); SDL_Delay(time_to_wait);
projected_points[i] = projected_point;
} }
previous_frame_time = SDL_GetTicks();
cube_rotation.y += 0.01f;
cube_rotation.z += 0.01f;
cube_rotation.x += 0.01f;
for (int i = 0; i < N_MESH_FACES; i++) {
face_t mesh_face = mesh_faces[i];
vec3_t face_vertices[3];
face_vertices[0] = mesh_vertices[mesh_face.a - 1];
face_vertices[1] = mesh_vertices[mesh_face.b - 1];
face_vertices[2] = mesh_vertices[mesh_face.c - 1];
//loop all three vertices
triangle_t projected_triangle;
for (int j = 0; j < 3; j++) {
vec3_t transformed_vertex = face_vertices[j];
transformed_vertex = vec3_rotate_y(transformed_vertex, cube_rotation.y);
transformed_vertex = vec3_rotate_x(transformed_vertex, cube_rotation.x);
transformed_vertex = vec3_rotate_z(transformed_vertex, cube_rotation.z);
transformed_vertex.z -= camera_position.z;
vec2_t projected_point = project(transformed_vertex);
projected_point.x += (window_width / 2);
projected_point.y += (window_height / 2);
projected_triangle.points[j] = projected_point;
}
// save projected triangle in array of triangles to render
triangles_to_render[i] = projected_triangle;
}
} }
void render(void) { void render(void) {
draw_grid(); draw_grid();
for (int i = 0; i < N_POINTS; i++){ for (int i = 0; i < N_MESH_FACES; i++) {
vec2_t projected_point = projected_points[i]; triangle_t triangle = triangles_to_render[i];
draw_rect( uint32_t color = 0xFFFFFF00;
projected_point.x + (window_width/2), for (int j = 0; j < 3; j++) {
projected_point.y + (window_height/2), draw_rect(triangle.points[j].x, triangle.points[j].y, 3, 3, color);
4, }
4, draw_triangle(triangle.points[0].x, triangle.points[0].y,
0xFFFFFF00 triangle.points[1].x, triangle.points[1].y,
); triangle.points[2].x, triangle.points[2].y,
color);
} }
render_color_buffer(); render_color_buffer();

33
src/mesh.c Normal file
View File

@ -0,0 +1,33 @@
#include "mesh.h"
vec3_t mesh_vertices[N_MESH_VERTICES] = {
{.x = -1, .y = -1, .z = -1},
{.x = -1, .y = 1, .z = -1},
{.x = 1, .y = 1, .z = -1},
{.x = 1, .y = -1, .z = -1},
{.x = 1, .y = 1, .z = 1},
{.x = 1, .y = -1, .z = 1},
{.x = -1, .y = 1, .z = 1},
{.x = -1, .y = -1, .z = 1},
};
face_t mesh_faces[N_MESH_FACES] = {
// Front
{.a = 1, .b = 2, .c = 3},
{.a = 1, .b = 3, .c = 4},
// right
{.a = 4, .b = 3, .c = 5},
{.a = 4, .b = 5, .c = 6},
// back
{.a = 6, .b = 5, .c = 7},
{.a = 6, .b = 7, .c = 8},
// left
{.a = 8, .b = 7, .c = 2},
{.a = 8, .b = 2, .c = 1},
// top
{.a = 2, .b = 7, .c = 5},
{.a = 2, .b = 5, .c = 3},
// bottom
{.a = 6, .b = 8, .c = 1},
{.a = 6, .b = 1, .c = 4},
};

11
src/mesh.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef RENDERER_MESH_H
#define RENDERER_MESH_H
#include "vector.h"
#include "triangle.h"
#define N_MESH_VERTICES 8
#define N_MESH_FACES (6 * 2 )
extern vec3_t mesh_vertices[N_MESH_VERTICES];
extern face_t mesh_faces[N_MESH_FACES];
#endif //RENDERER_MESH_H

3
src/triangle.c Normal file
View File

@ -0,0 +1,3 @@
#include "triangle.h"
// TODO

16
src/triangle.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef RENDERER_TRIANGLE_H
#define RENDERER_TRIANGLE_H
#include "vector.h"
typedef struct {
int a;
int b;
int c;
} face_t;
typedef struct {
vec2_t points[3];
} triangle_t;
#endif //RENDERER_TRIANGLE_H

View File

@ -1,3 +1,28 @@
#include <math.h>
#include "vector.h" #include "vector.h"
// Implement all vector functons // Implement all vector functons
vec3_t vec3_rotate_x(vec3_t v, float angle){
vec3_t rotated_vector = {
.x = v.x,
.y = v.y * cos(angle) - v.z * sin(angle),
.z = v.y * sin(angle) + v.z * cos(angle)
};
return rotated_vector;
}
vec3_t vec3_rotate_y(vec3_t v, float angle) {
vec3_t rotated_vector = {
.x = v.x * cos(angle) - v.z * sin(angle),
.y = v.y,
.z = v.x * sin(angle) + v.z * cos(angle)
};
return rotated_vector;
}
vec3_t vec3_rotate_z(vec3_t v, float angle) {
vec3_t rotated_vector = {
.x = v.x * cos(angle) - v.y * sin(angle),
.y = v.x * sin(angle) + v.y * cos(angle),
.z = v.z
};
return rotated_vector;
}

View File

@ -13,6 +13,9 @@ typedef struct {
float z; float z;
} vec3_t; } vec3_t;
vec3_t vec3_rotate_x(vec3_t v, float angle);
vec3_t vec3_rotate_y(vec3_t v, float angle);
vec3_t vec3_rotate_z(vec3_t v, float angle);
#endif #endif