pikuma_graphics_from_scratch/src/main.c

132 lines
3.8 KiB
C
Raw Normal View History

2024-04-12 04:27:14 +00:00
#include <stdint.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include "display.h"
2024-04-13 03:41:06 +00:00
#include "vector.h"
2024-04-19 04:02:54 +00:00
#include "mesh.h"
2024-04-13 04:51:28 +00:00
vec3_t camera_position = {0, 0, -5};
2024-04-19 04:02:54 +00:00
vec3_t cube_rotation = {.x = 0, .y = 0, .z = 0};
triangle_t triangles_to_render[N_MESH_FACES];
2024-04-13 04:51:28 +00:00
float fov_factor = 640;
2024-04-12 04:27:14 +00:00
bool is_running = false;
2024-04-19 04:02:54 +00:00
uint32_t previous_frame_time = 0;
2024-04-12 04:27:14 +00:00
void setup(void) {
2024-04-19 04:02:54 +00:00
// Allocate the required memory in bytes to hold color buffer
color_buffer = (uint32_t *) malloc(sizeof(uint32_t) * window_width * window_height);
// Creating the SDL Texture that is uses to display the color buffer
color_buffer_texture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
window_width,
window_height
);
2024-04-12 04:27:14 +00:00
}
void process_input(void) {
2024-04-19 04:02:54 +00:00
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT:
is_running = false;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) {
is_running = false;
}
break;
}
2024-04-12 04:27:14 +00:00
}
2024-04-13 04:20:42 +00:00
////////////////////////////////////////////////////////////////////////////////
// Function that receives a 3d Vector and returns a 2d projected point
////////////////////////////////////////////////////////////////////////////////
vec2_t project(vec3_t point) {
2024-04-19 04:02:54 +00:00
vec2_t projected_point = {
.x = (fov_factor * point.x) / point.z,
.y = (fov_factor * point.y) / point.z,
};
return projected_point;
2024-04-13 04:20:42 +00:00
}
2024-04-12 04:27:14 +00:00
void update(void) {
2024-04-19 04:02:54 +00:00
// lock execution to the next tick
uint32_t time_to_wait = FRAME_TARGET_TIME - (SDL_GetTicks() - previous_frame_time);
if (time_to_wait > 0 && time_to_wait <= FRAME_TARGET_TIME) {
SDL_Delay(time_to_wait);
}
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;
}
2024-04-13 04:51:28 +00:00
2024-04-12 04:27:14 +00:00
}
void render(void) {
2024-04-19 04:02:54 +00:00
draw_grid();
for (int i = 0; i < N_MESH_FACES; i++) {
triangle_t triangle = triangles_to_render[i];
uint32_t color = 0xFFFFFF00;
for (int j = 0; j < 3; j++) {
draw_rect(triangle.points[j].x, triangle.points[j].y, 3, 3, color);
}
draw_triangle(triangle.points[0].x, triangle.points[0].y,
triangle.points[1].x, triangle.points[1].y,
triangle.points[2].x, triangle.points[2].y,
color);
}
render_color_buffer();
clear_color_buffer(0xFF000000);
SDL_RenderPresent(renderer);
2024-04-12 04:27:14 +00:00
}
int main(void) {
2024-04-19 04:02:54 +00:00
is_running = initialize_window();
setup();
while (is_running) {
process_input();
update();
render();
}
destroy_window();
return 0;
2024-04-12 04:27:14 +00:00
}