add a cube
This commit is contained in:
parent
2d7da84f8d
commit
d38ce455af
3
Makefile
3
Makefile
@ -2,11 +2,10 @@ SRCS = $(shell find ./src -type f -name *.c)
|
|||||||
|
|
||||||
build:
|
build:
|
||||||
gcc -Wall -std=c99 $(SRCS) -lSDL2 -o renderer
|
gcc -Wall -std=c99 $(SRCS) -lSDL2 -o renderer
|
||||||
|
./renderer
|
||||||
|
|
||||||
run: build
|
run: build
|
||||||
./renderer
|
./renderer
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm renderer
|
rm renderer
|
||||||
|
|
||||||
all: clean build run
|
|
||||||
|
133
src/main.c
133
src/main.c
@ -10,102 +10,105 @@
|
|||||||
#define N_POINTS (9 * 9 * 9)
|
#define N_POINTS (9 * 9 * 9)
|
||||||
vec3_t cube_points[N_POINTS];
|
vec3_t cube_points[N_POINTS];
|
||||||
vec2_t projected_points[N_POINTS];
|
vec2_t projected_points[N_POINTS];
|
||||||
float fov_factor = 128;
|
|
||||||
|
vec3_t camera_position = {0, 0, -5};
|
||||||
|
float fov_factor = 640;
|
||||||
|
|
||||||
|
|
||||||
bool is_running = false;
|
bool is_running = false;
|
||||||
|
|
||||||
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
|
||||||
color_buffer = (uint32_t *) malloc(sizeof(uint32_t) * window_width * window_height);
|
color_buffer = (uint32_t *) malloc(sizeof(uint32_t) * window_width * window_height);
|
||||||
|
|
||||||
// Creating the SDL Texture that is uses to display the color buffer
|
// Creating the SDL Texture that is uses to display the color buffer
|
||||||
color_buffer_texture = SDL_CreateTexture(
|
color_buffer_texture = SDL_CreateTexture(
|
||||||
renderer,
|
renderer,
|
||||||
SDL_PIXELFORMAT_ARGB8888,
|
SDL_PIXELFORMAT_ARGB8888,
|
||||||
SDL_TEXTUREACCESS_STREAMING,
|
SDL_TEXTUREACCESS_STREAMING,
|
||||||
window_width,
|
window_width,
|
||||||
window_height
|
window_height
|
||||||
);
|
);
|
||||||
int point_count = 0;
|
int point_count = 0;
|
||||||
for (float x = -1; x <= 1; x += 0.25) {
|
for (float x = -1; x <= 1; x += 0.25) {
|
||||||
for (float y = -1; y <= 1; y += 0.25) {
|
for (float y = -1; y <= 1; y += 0.25) {
|
||||||
for (float z = -1; z <= 1; z += 0.25) {
|
for (float z = -1; z <= 1; z += 0.25) {
|
||||||
vec3_t new_point = { .x = x, .y = y, .z = z };
|
vec3_t new_point = { .x = x, .y = y, .z = z };
|
||||||
cube_points[point_count++] = new_point;
|
cube_points[point_count++] = new_point;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void process_input(void) {
|
void process_input(void) {
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
SDL_PollEvent(&event);
|
SDL_PollEvent(&event);
|
||||||
|
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case SDL_QUIT:
|
case SDL_QUIT:
|
||||||
is_running = false;
|
is_running = false;
|
||||||
break;
|
break;
|
||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
if (event.key.keysym.sym == SDLK_ESCAPE) {
|
if (event.key.keysym.sym == SDLK_ESCAPE) {
|
||||||
is_running = false;
|
is_running = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// Function that receives a 3d Vector and returns a 2d projected point
|
// Function that receives a 3d Vector and returns a 2d projected point
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
vec2_t project(vec3_t point) {
|
vec2_t project(vec3_t point) {
|
||||||
vec2_t projected_point = {
|
vec2_t projected_point = {
|
||||||
.x = (fov_factor * point.x),
|
.x = (fov_factor * point.x) / point.z,
|
||||||
.y = (fov_factor * point.y),
|
.y = (fov_factor * point.y) / point.z,
|
||||||
};
|
};
|
||||||
return projected_point;
|
return projected_point;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void update(void) {
|
void update(void) {
|
||||||
for (int i = 0; i < N_POINTS; i++){
|
for (int i = 0; i < N_POINTS; i++){
|
||||||
vec3_t point = cube_points[i];
|
vec3_t point = cube_points[i];
|
||||||
vec2_t projected_point = project(point);
|
point.z -= camera_position.z;
|
||||||
|
vec2_t projected_point = project(point);
|
||||||
|
|
||||||
projected_points[i] = projected_point;
|
projected_points[i] = projected_point;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void render(void) {
|
void render(void) {
|
||||||
draw_grid();
|
draw_grid();
|
||||||
|
|
||||||
for (int i = 0; i < N_POINTS; i++){
|
for (int i = 0; i < N_POINTS; i++){
|
||||||
vec2_t projected_point = projected_points[i];
|
vec2_t projected_point = projected_points[i];
|
||||||
draw_rect(
|
draw_rect(
|
||||||
projected_point.x + (window_width/2),
|
projected_point.x + (window_width/2),
|
||||||
projected_point.y + (window_height/2),
|
projected_point.y + (window_height/2),
|
||||||
4,
|
4,
|
||||||
4,
|
4,
|
||||||
0xFFFFFF00
|
0xFFFFFF00
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
render_color_buffer();
|
render_color_buffer();
|
||||||
clear_color_buffer(0xFF000000);
|
clear_color_buffer(0xFF000000);
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
is_running = initialize_window();
|
is_running = initialize_window();
|
||||||
setup();
|
setup();
|
||||||
|
|
||||||
while (is_running) {
|
while (is_running) {
|
||||||
process_input();
|
process_input();
|
||||||
update();
|
update();
|
||||||
render();
|
render();
|
||||||
}
|
}
|
||||||
destroy_window();
|
destroy_window();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user