Compare commits
3 Commits
06aa9ce6c1
...
57699deea9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
57699deea9 | ||
|
|
86ebc14924 | ||
|
|
f0bf328957 |
58543
assets/beetle-alt.obj
Normal file
58543
assets/beetle-alt.obj
Normal file
File diff suppressed because it is too large
Load Diff
@ -95,7 +95,7 @@ void draw_line(int x0, int y0, int x1, int y1, uint32_t color) {
|
||||
float current_x = x0;
|
||||
float current_y = y0;
|
||||
|
||||
for (int i = 0; i < longest_side_length; i++) {
|
||||
for (int i = 0; i <= longest_side_length; i++) {
|
||||
draw_pixel(round(current_x), round(current_y), color);
|
||||
current_x += x_inc;
|
||||
current_y += y_inc;
|
||||
|
||||
@ -17,6 +17,18 @@ extern SDL_Texture* color_buffer_texture;
|
||||
extern int window_width;
|
||||
extern int window_height;
|
||||
|
||||
enum render_methods {
|
||||
RENDER_WIRE_VERTEX,
|
||||
RENDER_WIRE,
|
||||
RENDER_FILL_TRIANGLE,
|
||||
RENDER_FILL_TRIANGLE_LINE,
|
||||
};
|
||||
|
||||
enum cull_methods {
|
||||
CULL_NONE,
|
||||
CULL_BACKFACE,
|
||||
};
|
||||
|
||||
bool initialize_window(void);
|
||||
void draw_grid(void);
|
||||
void draw_pixel(int x, int y, uint32_t color);
|
||||
|
||||
113
scripts/main.c
113
scripts/main.c
@ -1,6 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
#include <SDL.h>
|
||||
#include <SDL_ttf.h>
|
||||
#include "display.h"
|
||||
@ -32,6 +33,10 @@ vec3_t camera_position = { 0, 0, 0 };
|
||||
float fov_factor = 640;
|
||||
|
||||
bool is_running = false;
|
||||
|
||||
int cull_method = CULL_BACKFACE;
|
||||
int render_method = RENDER_WIRE_VERTEX;
|
||||
|
||||
uint32_t previous_frame_time = 0;
|
||||
|
||||
void setup(void) {
|
||||
@ -71,6 +76,26 @@ void process_input(void) {
|
||||
if (event.key.keysym.sym == SDLK_RIGHT)
|
||||
mesh.rotation.y += STEP;
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
SDL_Keycode key = event.key.keysym.sym;
|
||||
if (key == SDLK_c) {
|
||||
cull_method = !cull_method;
|
||||
}
|
||||
|
||||
if (key == SDLK_1) {
|
||||
render_method = RENDER_WIRE_VERTEX;
|
||||
}
|
||||
if (key == SDLK_2) {
|
||||
render_method = RENDER_WIRE;
|
||||
}
|
||||
if (key == SDLK_3) {
|
||||
render_method = RENDER_FILL_TRIANGLE;
|
||||
}
|
||||
if (key == SDLK_4) {
|
||||
render_method = RENDER_FILL_TRIANGLE_LINE;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
|
||||
|
||||
@ -82,8 +107,8 @@ void process_input(void) {
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
vec2_t project(vec3_t point) {
|
||||
vec2_t projected_point = {
|
||||
.x = (fov_factor * point.x) / point.z,
|
||||
.y = (fov_factor * point.y) / point.z,
|
||||
.x = round((fov_factor * point.x) / point.z),
|
||||
.y = round((fov_factor * point.y) / point.z),
|
||||
};
|
||||
return projected_point;
|
||||
}
|
||||
@ -131,29 +156,31 @@ void update(void) {
|
||||
transformed_vertices[j] = transformed_vertex;
|
||||
}
|
||||
|
||||
// check backface culling
|
||||
vec3_t vector_a = transformed_vertices[0]; /* A */
|
||||
vec3_t vector_b = transformed_vertices[1]; /* / \ */
|
||||
vec3_t vector_c = transformed_vertices[2]; /* C---B */
|
||||
if (cull_method == CULL_BACKFACE) {
|
||||
// check backface culling
|
||||
vec3_t vector_a = transformed_vertices[0]; /* A */
|
||||
vec3_t vector_b = transformed_vertices[1]; /* / \ */
|
||||
vec3_t vector_c = transformed_vertices[2]; /* C---B */
|
||||
|
||||
// get the vector subtraction of B-A and C-A
|
||||
vec3_t vector_ab = vec3_subtract(vector_b, vector_a);
|
||||
vec3_t vector_ac = vec3_subtract(vector_c, vector_a);
|
||||
vec3_normalize(&vector_ab);
|
||||
vec3_normalize(&vector_ac);
|
||||
// get the vector subtraction of B-A and C-A
|
||||
vec3_t vector_ab = vec3_subtract(vector_b, vector_a);
|
||||
vec3_t vector_ac = vec3_subtract(vector_c, vector_a);
|
||||
vec3_normalize(&vector_ab);
|
||||
vec3_normalize(&vector_ac);
|
||||
|
||||
// compute the face normal using the cross product to find perpindicular
|
||||
vec3_t normal = vec3_cross(vector_ab, vector_ac);
|
||||
vec3_normalize(&normal);
|
||||
// compute the face normal using the cross product to find perpindicular
|
||||
vec3_t normal = vec3_cross(vector_ab, vector_ac);
|
||||
vec3_normalize(&normal);
|
||||
|
||||
// find the vector between a point in the triangle and the camera origin
|
||||
vec3_t camera_ray = vec3_subtract(camera_position, vector_a);
|
||||
// find the vector between a point in the triangle and the camera origin
|
||||
vec3_t camera_ray = vec3_subtract(camera_position, vector_a);
|
||||
|
||||
// calculate how aligned the camera ray is with the face normal
|
||||
float dot_normal_camera = vec3_dot(normal, camera_ray);
|
||||
// calculate how aligned the camera ray is with the face normal
|
||||
float dot_normal_camera = vec3_dot(normal, camera_ray);
|
||||
|
||||
if (dot_normal_camera < 0) {
|
||||
continue;
|
||||
if (dot_normal_camera < 0) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
triangle_t projected_triangle;
|
||||
@ -177,19 +204,6 @@ void update(void) {
|
||||
|
||||
triangle_t normal_triangle;
|
||||
|
||||
for (int j = 0; j < 3; j++) {
|
||||
// project the current vertex
|
||||
vec2_t projected_point = project(transformed_vertices[j]);
|
||||
|
||||
// scale and translate the projected points to the center of the screen
|
||||
projected_point.x += (window_width / 2);
|
||||
projected_point.y += (window_height / 2);
|
||||
|
||||
normal_triangle.points[j] = projected_point;
|
||||
}
|
||||
|
||||
array_push(triangles_to_render, normal_triangle);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -200,18 +214,29 @@ void render(void) {
|
||||
|
||||
for (int i = 0; i < array_length(triangles_to_render); i++) {
|
||||
triangle_t triangle = triangles_to_render[i];
|
||||
draw_filled_triangle(triangle, colors[i]);
|
||||
//draw_triangle(triangle, 0xFFFF0000);
|
||||
//draw_rect(triangle.points[0].x, triangle.points[0].y, 3, 3, 0xFFFFFF00);
|
||||
//draw_rect(triangle.points[1].x, triangle.points[1].y, 3, 3, 0xFFFFFF00);
|
||||
//draw_rect(triangle.points[2].x, triangle.points[2].y, 3, 3, 0xFFFFFF00);
|
||||
}
|
||||
|
||||
triangle_t t;
|
||||
t.points[0] = (vec2_t){ 100, 100 };
|
||||
t.points[1] = (vec2_t){ 100, 200.1 };
|
||||
t.points[2] = (vec2_t){ 300, 200.5};
|
||||
draw_filled_triangle(t, 0xFF00FF00);
|
||||
switch (render_method) {
|
||||
case RENDER_WIRE:
|
||||
draw_triangle(triangle, 0x0000ff);
|
||||
break;
|
||||
case RENDER_WIRE_VERTEX:
|
||||
draw_triangle(triangle, 0x000055);
|
||||
draw_rect(triangle.points[0].x, triangle.points[0].y, 3, 3, 0xFFFFFF00);
|
||||
draw_rect(triangle.points[1].x, triangle.points[1].y, 3, 3, 0xFFFFFF00);
|
||||
draw_rect(triangle.points[2].x, triangle.points[2].y, 3, 3, 0xFFFFFF00);
|
||||
break;
|
||||
case RENDER_FILL_TRIANGLE:
|
||||
draw_filled_triangle(triangle, 0x0000ff);
|
||||
break;
|
||||
case RENDER_FILL_TRIANGLE_LINE:
|
||||
draw_filled_triangle(triangle, 0xff00);
|
||||
draw_triangle(triangle, 0x0000ff);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//vec2_t m = triangle_midpoint(t);
|
||||
//draw_rect(m.x, m.y, 10, 10, 0xFFFF0000);
|
||||
|
||||
@ -75,7 +75,7 @@ void load_file(void) {
|
||||
FILE* fp;
|
||||
char* tok;
|
||||
|
||||
fp = fopen("..\\assets\\cube.obj", "r");
|
||||
fp = fopen("..\\assets\\f22.obj", "r");
|
||||
|
||||
if (fp == NULL) {
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
@ -13,8 +13,8 @@ int compare_vec2_t(const void* a, const void* b) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define FUNC ceilf
|
||||
#define FUNC2 floorf
|
||||
#define FUNC (int)ceilf
|
||||
#define FUNC2 (int)floorf
|
||||
|
||||
uint32_t invert_rgb(uint32_t color) {
|
||||
uint32_t a = color & 0xFF000000; // Alpha channel
|
||||
@ -29,9 +29,9 @@ void fill_flat_bottom_triangle(const triangle_t* triangle, uint32_t color) {
|
||||
|
||||
float x_start = triangle->points[0].x, x_end = triangle->points[0].x;
|
||||
|
||||
for (int x = FUNC(triangle->points[0].y); x < FUNC(triangle->points[2].y); x++) {
|
||||
for (int x = FUNC(triangle->points[0].y); x <= FUNC2(triangle->points[2].y); x++) {
|
||||
//draw_rect((int)triangle->points[0].x, i, 1, 1, 0xFFFF0000);
|
||||
draw_line(FUNC2(x_start), x, FUNC2(x_end), x, color);
|
||||
draw_line(FUNC(x_start), x, FUNC2(x_end), x, color);
|
||||
x_start += inv_slope_1;
|
||||
x_end += inv_slope_2;
|
||||
}
|
||||
@ -43,9 +43,9 @@ void fill_flat_top_triangle(const triangle_t* triangle, uint32_t color) {
|
||||
|
||||
float x_start = triangle->points[2].x, x_end = triangle->points[2].x;
|
||||
|
||||
for (int y = FUNC(triangle->points[2].y); y > FUNC(triangle->points[0].y); y--) {
|
||||
for (int y = FUNC2(triangle->points[2].y); y >= FUNC(triangle->points[0].y); y--) {
|
||||
//draw_rect((int)triangle->points[0].x, i, 1, 1, 0xFFFF0000);
|
||||
draw_line(FUNC2(x_start), y, FUNC2(x_end), y, color);
|
||||
draw_line(FUNC(x_start), y, FUNC2(x_end), y, color);
|
||||
x_start -= inv_slope_1;
|
||||
x_end -= inv_slope_2;
|
||||
}
|
||||
|
||||
@ -10,6 +10,11 @@ typedef struct {
|
||||
int c;
|
||||
} face_t;
|
||||
|
||||
typedef struct {
|
||||
int x;
|
||||
int y;
|
||||
} int_vec2_t;
|
||||
|
||||
typedef struct {
|
||||
vec2_t points[3];
|
||||
} triangle_t;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user