updating changes
This commit is contained in:
2
Makefile
2
Makefile
@@ -1,5 +1,5 @@
|
|||||||
build:
|
build:
|
||||||
gcc -Wall -std=c99 ./src/*.c -lSDL2 -o renderer
|
gcc -Wall -std=c99 ./src/*.c -lSDL2 -lm -o renderer
|
||||||
|
|
||||||
run:
|
run:
|
||||||
./renderer
|
./renderer
|
||||||
|
|||||||
@@ -70,15 +70,18 @@ void clear_color_buffer(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){
|
||||||
for (int j = x; j <= x + width; j++){
|
for (int j = 0; j < width; j++){
|
||||||
for (int k = y; k <= y + height; k++){
|
for (int k = 0; k < height; k++){
|
||||||
color_buffer[(window_width * j) + k] = color;
|
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){
|
void draw_pixel(int x, int y, uint32_t color){
|
||||||
if (x < window_width && y < window_height){
|
if (x >= 0 && x < window_width && y >= 0 && y < window_height){
|
||||||
color_buffer[(window_width * y) + x] = color;
|
color_buffer[(window_width * y) + x] = color;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -91,6 +94,26 @@ void draw_grid(void){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void destroy_window(void){
|
void destroy_window(void){
|
||||||
free(color_buffer);
|
free(color_buffer);
|
||||||
SDL_DestroyRenderer(renderer);
|
SDL_DestroyRenderer(renderer);
|
||||||
|
|||||||
@@ -5,6 +5,9 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
#define FPS 30
|
||||||
|
#define FRAME_TARGET_TIME (1000 / FPS)
|
||||||
|
|
||||||
|
|
||||||
extern SDL_Window* window;
|
extern SDL_Window* window;
|
||||||
extern SDL_Renderer* renderer;
|
extern SDL_Renderer* renderer;
|
||||||
@@ -14,6 +17,7 @@ extern int window_width;
|
|||||||
extern int window_height;
|
extern int window_height;
|
||||||
|
|
||||||
bool initialize_window(void);
|
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_pixel(int x, int y, 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 draw_grid(void);
|
void draw_grid(void);
|
||||||
|
|||||||
99
src/main.c
99
src/main.c
@@ -1,9 +1,25 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include "display.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;
|
bool is_running = false;
|
||||||
|
int previous_frame_time = 0;
|
||||||
|
|
||||||
void setup(void){
|
void setup(void){
|
||||||
// Allocates the memory in bytes required to hold for color_buffer, using sizeof to allocate the # of bytes required
|
// 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_width,
|
||||||
window_height
|
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){
|
void process_input(void){
|
||||||
@@ -33,19 +62,77 @@ void process_input(void){
|
|||||||
break;
|
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){
|
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){
|
void render(void){
|
||||||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
|
||||||
SDL_RenderClear(renderer);
|
|
||||||
|
|
||||||
draw_grid();
|
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();
|
render_color_buffer();
|
||||||
|
|
||||||
clear_color_buffer(0xFF000000);
|
clear_color_buffer(0xFF000000);
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
|
|||||||
61
src/vector.c
Normal file
61
src/vector.c
Normal 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
23
src/vector.h
Normal 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
|
||||||
Reference in New Issue
Block a user