ortho projection

This commit is contained in:
Tyrel Souza 2024-04-13 00:20:42 -04:00
parent f783db8a45
commit 2d7da84f8d
7 changed files with 80 additions and 17 deletions

0
src/camera.c Normal file
View File

11
src/camera.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef CAMERA_H
#define CAMERA_H
typedef struct {
vec3_t position;
vec3_t rotation;
float fov_angle;
} camera_t;
#endif

View File

@ -1,3 +1,4 @@
#include <stdio.h>
#include "display.h"
SDL_Window *window = NULL;
@ -64,26 +65,34 @@ void render_color_buffer(void) {
SDL_RenderCopy(renderer, color_buffer_texture, NULL, NULL);
}
void draw_grid(int size) {
void draw_grid() {
if (color_buffer == NULL) {
return;
}
for (int y = 0; y < window_height; y++) {
for (int x = 0; x < window_width; x++) {
if (y % size == 0 || x % size == 0) {
draw_pixel(x, y, 0xFF333333);
if (y % 10 == 0 || x % 10 == 0) {
color_buffer[(window_width * y) + x] = 0xFF444444;
}
}
}
}
void draw_pixel(int x, int y, uint32_t color) {
if ( x >= 0 && x < window_width && y >= 0 && y < window_height) {
color_buffer[(window_width * y) + x] = color;
}
}
void draw_rect(int x, int y, int width, int height, uint32_t color) {
if (color_buffer == NULL) {
return;
}
for (int i = y; i < height + y; i++) {
for (int j = x; j < width; j++) {
draw_pixel(x + j, y + i, color);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int current_x = x + i;
int current_y = y + j;
draw_pixel(current_x, current_y, color);
}
}
}
@ -96,8 +105,3 @@ void destroy_window(void) {
SDL_Quit();
}
void draw_pixel(int x, int y, uint32_t color) {
if (x < window_width && y < window_height) {
color_buffer[(window_width * y) + x] = color;
}
}

View File

@ -15,7 +15,7 @@ extern int window_height;
bool initialize_window(void);
void draw_grid(int size);
void draw_grid();
void draw_pixel(int x, int y, uint32_t color);

View File

@ -9,6 +9,8 @@
////////////////////////////////////////////////////////////
#define N_POINTS (9 * 9 * 9)
vec3_t cube_points[N_POINTS];
vec2_t projected_points[N_POINTS];
float fov_factor = 128;
bool is_running = false;
@ -52,19 +54,44 @@ void process_input(void) {
}
}
////////////////////////////////////////////////////////////////////////////////
// Function that receives a 3d Vector and returns a 2d projected point
////////////////////////////////////////////////////////////////////////////////
vec2_t project(vec3_t point) {
vec2_t projected_point = {
.x = (fov_factor * point.x),
.y = (fov_factor * point.y),
};
return projected_point;
}
void update(void) {
for (int i = 0; i < N_POINTS; i++){
vec3_t point = cube_points[i];
vec2_t projected_point = project(point);
projected_points[i] = projected_point;
}
}
void render(void) {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderClear(renderer);
draw_grid();
for (int i = 0; i < N_POINTS; i++){
vec2_t projected_point = projected_points[i];
draw_rect(
projected_point.x + (window_width/2),
projected_point.y + (window_height/2),
4,
4,
0xFFFFFF00
);
}
clear_color_buffer(0xFF000000);
draw_grid(10);
draw_rect(50, 50, 640, 480, 0xFFDD00DD);
render_color_buffer();
clear_color_buffer(0xFF000000);
SDL_RenderPresent(renderer);
}

3
src/vector.c Normal file
View File

@ -0,0 +1,3 @@
#include "vector.h"
// Implement all vector functons

18
src/vector.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef VECTOR_H
#define VECTOR_H
typedef struct {
float x;
float y;
} vec2_t;
typedef struct {
float x;
float y;
float z;
} vec3_t;
#endif