updating changes

This commit is contained in:
2025-06-04 13:39:34 -05:00
parent 4339b90811
commit 0dfe510fab
6 changed files with 230 additions and 32 deletions

View File

@@ -1,5 +1,5 @@
build:
gcc -Wall -std=c99 ./src/*.c -lSDL2 -o renderer
gcc -Wall -std=c99 ./src/*.c -lSDL2 -lm -o renderer
run:
./renderer

View File

@@ -27,23 +27,23 @@ bool initialize_window(void) {
// Create a SDL window
window = SDL_CreateWindow(
NULL,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
window_width,
window_height,
SDL_WINDOW_BORDERLESS
NULL,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
window_width,
window_height,
SDL_WINDOW_BORDERLESS
);
if (!window) {
fprintf(stderr, "Error creating SDL window.\n");
return false;
fprintf(stderr, "Error creating SDL window.\n");
return false;
}
// Create a SDL renderer
renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer) {
fprintf(stderr, "Error creating SDL error.\n");
return false;
fprintf(stderr, "Error creating SDL error.\n");
return false;
}
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
@@ -63,31 +63,54 @@ void render_color_buffer(void){
void clear_color_buffer(uint32_t color){
for (int y = 0; y < window_height; y++){
for (int x = 0; x < window_width; x++){
color_buffer[(window_width * y) + x] = color;
}
for (int x = 0; x < window_width; x++){
color_buffer[(window_width * y) + x] = color;
}
}
}
void draw_rect(int x, int y, int width, int height, uint32_t color){
for (int j = x; j <= x + width; j++){
for (int k = y; k <= y + height; k++){
color_buffer[(window_width * j) + k] = color;
for (int j = 0; j < width; j++){
for (int k = 0; k < height; k++){
int current_x = x + j;
int current_y = y + k;
draw_pixel(current_x, current_y, color);
//color_buffer[(window_width * j) + k] = color;
}
}
}
void draw_pixel(int x, int y, uint32_t color){
if (x < window_width && y < window_height){
color_buffer[(window_width * y) + x] = color;
if (x >= 0 && x < window_width && y >= 0 && y < window_height){
color_buffer[(window_width * y) + x] = color;
}
}
void draw_grid(void){
for (int y = 0; y < window_height; y += 10){
for (int x = 0; x < window_width; x += 10){
color_buffer[(window_width * y) + x ] = 0xFF133343;
}
for (int x = 0; x < window_width; x += 10){
color_buffer[(window_width * y) + x ] = 0xFF133343;
}
}
}
void draw_line(int x0, int y0, int x1, int y1) {
int delta_x = (x1 - x0);
int delta_y = (y1 - y0);
int side_length = abs(delta_x) >= abs(delta_y) ? abs(delta_x) : abs(delta_y);
// Find how much we should increment in both x and y each step
float x_inc = delta_x / (float)side_length;
float y_inc = delta_y / (float)side_length;
float current_x = x0;
float current_y = y0;
for (int i = 0; i <=side_length; i++) {
draw_pixel(round(current_x), round(current_y), 0xFFFFFF00);
current_x += x_inc;
current_y += y_inc;
}
}

View File

@@ -5,6 +5,9 @@
#include <stdbool.h>
#include <SDL2/SDL.h>
#define FPS 30
#define FRAME_TARGET_TIME (1000 / FPS)
extern SDL_Window* window;
extern SDL_Renderer* renderer;
@@ -14,6 +17,7 @@ extern int window_width;
extern int window_height;
bool initialize_window(void);
void draw_line(int x0, int y0, int x1, int y1);
void draw_pixel(int x, int y, uint32_t color);
void draw_rect(int x, int y, int width, int height, uint32_t color);
void draw_grid(void);

View File

@@ -1,9 +1,25 @@
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include "display.h"
#include "vector.h"
#define N_POINTS (9 * 9 * 9)
// Declare an array of vectors/points
vec3_t cube_points[N_POINTS]; // 9x9x9 cube
vec2_t projected_points[N_POINTS];
vec3_t camera_position = { .x = 0, .y = 0, .z = -5};
vec3_t cube_rotation = { .x = 0, .y = 0, .z = 0};
float fov_factor = 640;
bool is_running = false;
int previous_frame_time = 0;
void setup(void){
// Allocates the memory in bytes required to hold for color_buffer, using sizeof to allocate the # of bytes required
@@ -17,6 +33,19 @@ void setup(void){
window_width,
window_height
);
int point_count = 0;
// Start loading my array of vectors
// From -1 to 1 (in this 9x9x9 cube)
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){
@@ -32,20 +61,78 @@ void process_input(void){
is_running = false;
break;
}
}
}
/*
* Function that receives a 3D vector and returns a 2D point
*/
vec2_t project(vec3_t point){
vec2_t projected_point = {
.x = (fov_factor * point.x) / point.z,
.y = (fov_factor * point.y) / point.z
};
return projected_point;
}
void update(void){
//TODO
//while (!SDL_TICKS_PASSED(SDL_GetTicks(), previous_frame_time + FRAME_TARGET_TIME));
int 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.x += 0.01;
cube_rotation.y += 0.01;
cube_rotation.z += 0.01;
for (int i = 0; i < N_POINTS; i++) {
vec3_t point = cube_points[i];
vec3_t transformed_point = vec3_rotate_x(point, cube_rotation.x);
transformed_point = vec3_rotate_y(transformed_point, cube_rotation.y);
transformed_point = vec3_rotate_z(transformed_point, cube_rotation.z);
// Translate point away from the camera
transformed_point.z -= camera_position.z;
// Project the current point
vec2_t projected_point = project(transformed_point);
// Save the projected 2D vector in the array of projected points
projected_points[i] = projected_point;
}
}
void render(void){
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderClear(renderer);
draw_grid();
draw_pixel(20, 20, 0xFFFFFF00);
//draw_rect(100, 200, 200, 400, 0xFF998500);
//Loop all projected points and render them
for (int i = 0; i < N_POINTS; i++){
vec2_t projected_point = projected_points[i];
draw_rect(
projected_point.x + (window_width / 2.0),
projected_point.y + (window_height / 2.0),
4,
4,
0xFFFFFF00
);
draw_line(projected_point.x,
projected_point.y,
projected_point.x + projected_point.x, projected_point.y + projected_point.y);
}
draw_line(75, 75, 25, 25);
// draw_pixel(20, 20, 0xFFFFFF00);
// draw_rect(300, 200, 300, 150, 0xFF998500);
render_color_buffer();
clear_color_buffer(0xFF000000);
SDL_RenderPresent(renderer);
@@ -58,9 +145,9 @@ int main(void) {
setup();
while (is_running) {
process_input();
update();
render();
process_input();
update();
render();
}
destroy_window();

61
src/vector.c Normal file
View File

@@ -0,0 +1,61 @@
#include "vector.h"
#include <math.h>
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;
}
vec3_t cube_vertice[8] = {
{ .x = -1, .y = -1, .z = -1 }, // 1
{ .x = -1, .y = 1, .z = -1 }, // 2
{ .x = 1, .y = 1, .z = -1 }, // 3
{ .x = 1, .y = -1, .z = -1 }, // 4
{ .x = 1, .y = 1, .z = 1 }, // 5
{ .x = 1, .y = -1, .z = 1 }, // 6
{ .x = -1, .y = 1, .z = 1 }, // 7
{ .x = -1, .y = -1, .z = 1 } // 8
};
face_t cube_faces[12] = {
// 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 }
};

23
src/vector.h Normal file
View File

@@ -0,0 +1,23 @@
#ifndef VECTOR_H
#define VECTOR_H
typedef struct {
float x;
float y;
} vec2_t;
typedef struct {
float x;
float y;
float z;
} vec3_t;
typedef struct {
int a, b, c;
} face_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