From dc3a81b7b4e82eedc9852e13db6a03b8f9fb2c1c Mon Sep 17 00:00:00 2001 From: Eric-Paul Ickhorn Date: Sun, 15 Oct 2023 10:07:33 +0200 Subject: [PATCH] Partially integrated own mathematics library --- build.bash | 4 +- code/source-c/entity.c | 1 - code/source-c/entity.h | 4 +- code/source-c/main.c | 161 ++++++----------------- code/source-c/shaders.c | 7 + code/source-c/shaders.h | 7 +- code/source-c/state.h | 16 +-- code/source-c/utility.h | 36 ------ code/source-c/{ => utility}/location.c | 2 +- code/source-c/{ => utility}/location.h | 6 +- code/source-c/utility/math.c | 12 ++ code/source-c/utility/math.h | 96 ++++++++++++++ code/source-c/utility/matrix.c | 169 +++++++++++++++++++++++++ code/source-c/utility/utility.h | 23 ++++ code/source-c/utility/vector.c | 52 ++++++++ code/source-c/vector.c | 9 -- code/source-c/world.c | 22 ++-- code/source-c/world.h | 9 +- 18 files changed, 434 insertions(+), 202 deletions(-) delete mode 100644 code/source-c/utility.h rename code/source-c/{ => utility}/location.c (91%) rename code/source-c/{ => utility}/location.h (67%) create mode 100644 code/source-c/utility/math.c create mode 100644 code/source-c/utility/math.h create mode 100644 code/source-c/utility/matrix.c create mode 100644 code/source-c/utility/utility.h create mode 100644 code/source-c/utility/vector.c delete mode 100644 code/source-c/vector.c diff --git a/build.bash b/build.bash index 773ce5c..aa06dde 100644 --- a/build.bash +++ b/build.bash @@ -7,6 +7,6 @@ gcc -g3 -o dependencies/build/glad.o \ -I dependencies/include/ -Wall gcc -g3 -o techneck.elf \ - code/source-c/*.c code/source-c/entity/*.c dependencies/build/glad.o \ - -I dependencies/include -lGL -lSDL2 -lm -Wall + code/source-c/*.c code/source-c/entity/*.c code/source-c/utility/*.c dependencies/build/glad.o \ + -I dependencies/include -I code/source-c/ -lGL -lSDL2 -lm -Wall diff --git a/code/source-c/entity.c b/code/source-c/entity.c index 59ad76d..7e30d2a 100644 --- a/code/source-c/entity.c +++ b/code/source-c/entity.c @@ -319,6 +319,5 @@ void tc_draw_all_entities_of_type(char *name) } ++entity_index; } - printf("Drew %d out of %d Entities!\n", drawn_entities, registry.num_entities); } diff --git a/code/source-c/entity.h b/code/source-c/entity.h index b10b49e..aba1a54 100644 --- a/code/source-c/entity.h +++ b/code/source-c/entity.h @@ -2,8 +2,8 @@ #ifndef TC_ENTITY_H #define TC_ENTITY_H -#include "location.h" -#include "utility.h" +#include "utility/location.h" +#include "utility/utility.h" typedef struct tc_entity_event tc_entity_event_s; typedef struct tc_entity_type tc_entity_type_s; diff --git a/code/source-c/main.c b/code/source-c/main.c index ad95164..2fbaf5a 100644 --- a/code/source-c/main.c +++ b/code/source-c/main.c @@ -1,69 +1,10 @@ -#include "state.h" +#include +#include #include #include #include -mat4x4 projection_matrix; -mat4x4 view_matrix; - -float triangle_vertices[108] = -{ - // Front - -0.5f, -0.5f, -0.5f, - -0.5f, 0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, - - 0.5f, 0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, - -0.5f, 0.5f, -0.5f, - - // Back - 0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, - -0.5f, -0.5f, 0.5f, - - -0.5f, 0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - 0.5f, 0.5f, 0.5f, - - // Left - -0.5f, 0.5f, -0.5f, - -0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, 0.5f, - - -0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, - -0.5f, 0.5f, -0.5f, - - // Right - 0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, -0.5f, - 0.5f, 0.5f, -0.5f, - - 0.5f, 0.5f, -0.5f, - 0.5f, 0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - - // Top - -0.5f, 0.5f, -0.5f, - -0.5f, 0.5f, 0.5f, - 0.5f, 0.5f, -0.5f, - - -0.5f, 0.5f, 0.5f, - 0.5f, 0.5f, 0.5f, - 0.5f, 0.5f, -0.5f, - - // Bottom - -0.5f, -0.5f, 0.5f, - -0.5f, -0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, - - 0.5f, -0.5f, 0.5f, - -0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, -0.5f -}; - void tc_move_viewer_to(float x, float y, float z) { if(tc_game_state_g.viewer == NULL) return; @@ -100,45 +41,11 @@ void tc_get_viewer_rotation(float *x, float *y, float *z) if(z != NULL) (*z) = tc_get_float_from_entity(tc_game_state_g.viewer, "rot_z"); } -void render_block(tc_block_s block) -{ - mat4x4 model_matrix; - mat4x4_identity(model_matrix); - mat4x4_rotate_X(view_matrix, model_matrix, block.rotation.x); - mat4x4_rotate_Y(view_matrix, model_matrix, block.rotation.y); - mat4x4_rotate_Z(view_matrix, model_matrix, block.rotation.z); - mat4x4_translate_in_place(model_matrix, block.position.x, block.position.y, block.position.z); - - int model_matrix_uniform_location = glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "model_matrix"); - glUniformMatrix4fv(model_matrix_uniform_location, 1, GL_FALSE, &model_matrix[0][0]); - - glBindBuffer(GL_ARRAY_BUFFER, block.drawing.vbo); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0); - glEnableVertexAttribArray(0); - glDrawArrays(GL_TRIANGLES, 0, 36); -} - -tc_block_s tc_new_block_at_3f(float x, float y, float z) -{ - tc_block_s block; - block.position.x = x; - block.position.y = y; - block.position.z = z; - block.rotation.x = 0.0f; - block.rotation.y = 0.0f; - block.rotation.z = 0.0f; - glGenBuffers(1, &block.drawing.vbo); - glBindBuffer(GL_ARRAY_BUFFER, block.drawing.vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 108, &triangle_vertices[0], GL_STATIC_DRAW); - - return block; -} - void render() { float color[4] = { 0.12f, 0.5f, 0.8f, 1.0f }; glClearBufferfv(GL_COLOR, 0, color); - float depth = 10000000.0f; + float depth = 1000.0f; glClearBufferfv(GL_DEPTH, 0, &depth); int fb_width = 0; @@ -146,29 +53,35 @@ void render() SDL_GetWindowSize(tc_game_state_g.renderer.window, &fb_width, &fb_height); glViewport(0, 0, fb_width, fb_height); - mat4x4_perspective(projection_matrix, 120, ((float) fb_width) / ((float) fb_height), 0.0001f, 1000.0f); - tc_vec3_s camera_rotation; + tc_vec3f_s camera_rotation; + tc_vec3f_s camera_position; + tc_get_viewer_rotation(&camera_rotation.x, &camera_rotation.y, &camera_rotation.z); - - tc_vec3_s camera_position; tc_get_viewer_position(&camera_position.x, &camera_position.y, &camera_position.z); - mat4x4_identity(view_matrix); - mat4x4_rotate_X(view_matrix, view_matrix, camera_rotation.x); - mat4x4_rotate_Y(view_matrix, view_matrix, camera_rotation.y); - mat4x4_rotate_Z(view_matrix, view_matrix, camera_rotation.z); + tc_vec3f_s camera_offset; + camera_offset.z = camera_position.z; + camera_offset.y = -camera_position.y; + camera_offset.x = -camera_position.x; - mat4x4_translate_in_place(view_matrix, -camera_position.x, -camera_position.y, camera_position.z); + tc_mat4f_s view_matrix = tc_mat4f_identity(); + tc_mat4f_s projection_matrix = + tc_mat4f_perspective(120, + ((float) fb_width) / ((float) fb_height), + 0.0001f, 1000.0f + ); + + view_matrix = tc_mat4f_translate(view_matrix, camera_offset); + + view_matrix = tc_mat4_rotate_z(view_matrix, tc_deg_to_rad(camera_rotation.z)); + view_matrix = tc_mat4_rotate_y(view_matrix, tc_deg_to_rad(camera_rotation.y)); + view_matrix = tc_mat4_rotate_x(view_matrix, tc_deg_to_rad(camera_rotation.x)); glUseProgram(tc_game_state_g.renderer.draw_shader.program_id); - int projection_uniform_location = glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "projection_matrix"); - glUniformMatrix4fv(projection_uniform_location, 1, GL_FALSE, &projection_matrix[0][0]); - - int view_uniform_location = glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "view_matrix"); - glUniformMatrix4fv(view_uniform_location, 1, GL_FALSE, &view_matrix[0][0]); - // printf("%d\n", view_uniform_location); + tc_shader_uniform_mat4f(&tc_game_state_g.renderer.draw_shader, "projection_matrix", projection_matrix); + tc_shader_uniform_mat4f(&tc_game_state_g.renderer.draw_shader, "view_matrix", view_matrix); tc_draw_world(tc_game_state_g.main_world); @@ -188,7 +101,7 @@ uint64_t tc_last_update = 0; bool update() { - tc_vec3_s camera_rotation; + tc_vec3f_s camera_rotation; tc_get_viewer_rotation(&camera_rotation.x, &camera_rotation.y, &camera_rotation.z); SDL_Event event; @@ -299,20 +212,20 @@ bool update() case SDL_MOUSEMOTION: { if(!(event.motion.state & SDL_BUTTON_LMASK)) break; - camera_rotation.y += event.motion.xrel / 50.0f; - camera_rotation.x += event.motion.yrel / 50.0f; + camera_rotation.y += event.motion.xrel / 10.0f; + camera_rotation.x += event.motion.yrel / 10.0f; - if(camera_rotation.x > (3.1415f/2.0f)) - camera_rotation.x = (3.1415f/2.0f); + if(camera_rotation.x > 90) + camera_rotation.x = 90; - if(camera_rotation.x < -(3.1415f/2.0f)) - camera_rotation.x = -(3.1415f/2.0f); + if(camera_rotation.x < -90) + camera_rotation.x = -90; } break; } } - tc_vec3_s camera_position; + tc_vec3f_s camera_position; tc_get_viewer_position(&camera_position.x, &camera_position.y, &camera_position.z); if(shift_pressed) @@ -327,7 +240,7 @@ bool update() - tc_vec3_s difference_in_perspective; + tc_vec3f_s difference_in_perspective; difference_in_perspective.x = 0.0f; difference_in_perspective.y = 0.0f; difference_in_perspective.z = 0.0f; @@ -357,9 +270,9 @@ bool update() mat4x4_translate(position_matrix, difference_in_perspective.x, 0.0f, difference_in_perspective.z); - mat4x4_rotate_X(position_matrix, position_matrix, camera_rotation.x); - mat4x4_rotate_Y(position_matrix, position_matrix, camera_rotation.y); - mat4x4_rotate_Z(position_matrix, position_matrix, camera_rotation.z); + mat4x4_rotate_Z(position_matrix, position_matrix, tc_deg_to_rad(camera_rotation.z)); + mat4x4_rotate_Y(position_matrix, position_matrix, tc_deg_to_rad(camera_rotation.y)); + mat4x4_rotate_X(position_matrix, position_matrix, tc_deg_to_rad(camera_rotation.x)); vec4 viewed_difference = { difference_in_perspective.x, 0.0f, difference_in_perspective.z }; vec4 raw_difference = { 0.0f, 0.0f, 0.0f }; @@ -373,7 +286,7 @@ bool update() tc_game_state_g.main_world->spawn_loader->center.x = ((int32_t) camera_position.x) / 32 - UPDATE_DISTANCE; tc_game_state_g.main_world->spawn_loader->center.y = ((int32_t) camera_position.y) / 32 - UPDATE_DISTANCE; - tc_game_state_g.main_world->spawn_loader->center.z = ((int32_t) camera_position.z) / 32 - UPDATE_DISTANCE; + tc_game_state_g.main_world->spawn_loader->center.z = - ((int32_t) camera_position.z) / 32 - UPDATE_DISTANCE; tc_update_world(tc_game_state_g.main_world); diff --git a/code/source-c/shaders.c b/code/source-c/shaders.c index 7d736d0..64cda36 100644 --- a/code/source-c/shaders.c +++ b/code/source-c/shaders.c @@ -92,3 +92,10 @@ tc_shader_program_s tc_make_shader_program(char *vertex_path, char *fragment_pat return shader_program; } +void tc_shader_uniform_mat4f(tc_shader_program_s *program, char *location, tc_mat4f_s matrix) +{ + int location_id = + glGetUniformLocation(program->program_id, location); + glUniformMatrix4fv(location_id, 1, GL_TRUE, &matrix.values[0][0]); +} + diff --git a/code/source-c/shaders.h b/code/source-c/shaders.h index 792fc1f..cdaf2f8 100644 --- a/code/source-c/shaders.h +++ b/code/source-c/shaders.h @@ -9,13 +9,18 @@ #include +#include + typedef struct tc_shader_program { uint32_t program_id; } tc_shader_program_s; -tc_shader_program_s tc_make_shader_program(char *vertex_path, char *fragment_path); +tc_shader_program_s tc_make_shader_program (char *vertex_path, char *fragment_path); + +void tc_shader_uniform_mat4f (tc_shader_program_s *program, char *location, tc_mat4f_s matrix); + #endif diff --git a/code/source-c/state.h b/code/source-c/state.h index 9a65520..923128e 100644 --- a/code/source-c/state.h +++ b/code/source-c/state.h @@ -6,17 +6,17 @@ #include #include -#include "assets.h" -#include "shaders.h" -#include "entity.h" -#include "blocks.h" -#include "world.h" -#include "utility.h" +#include +#include +#include +#include +#include +#include typedef struct tc_camera { - tc_vec3_s position; - tc_vec3_s rotation; + tc_vec3f_s position; + tc_vec3f_s rotation; float fov; } tc_camera_s; diff --git a/code/source-c/utility.h b/code/source-c/utility.h deleted file mode 100644 index 8cf0a69..0000000 --- a/code/source-c/utility.h +++ /dev/null @@ -1,36 +0,0 @@ - -#ifndef TC_UTILITY_H -#define TC_UTILITY_H - -#include -#include - -typedef struct tc_vec3 -{ - float x; - float y; - float z; - -} tc_vec3_s; - -typedef struct tc_vec3i -{ - int32_t x; - int32_t y; - int32_t z; - -} tc_vec3i_s; - -bool tc_vec3i_equ(tc_vec3i_s first, tc_vec3i_s second); - -typedef struct tc_object -{ - uint32_t vbo; - uint32_t vao; - tc_vec3_s position; - tc_vec3_s rotation; - -} tc_object_s; - -#endif - diff --git a/code/source-c/location.c b/code/source-c/utility/location.c similarity index 91% rename from code/source-c/location.c rename to code/source-c/utility/location.c index 850d42f..aac35cb 100644 --- a/code/source-c/location.c +++ b/code/source-c/utility/location.c @@ -1,4 +1,4 @@ -#include "location.h" +#include #include diff --git a/code/source-c/location.h b/code/source-c/utility/location.h similarity index 67% rename from code/source-c/location.h rename to code/source-c/utility/location.h index 4445e86..a09f9df 100644 --- a/code/source-c/location.h +++ b/code/source-c/utility/location.h @@ -2,13 +2,13 @@ #ifndef TC_LOCATION_H #define TC_LOCATION_H -#include "world.h" +#include typedef struct tc_location { tc_world_s *world; - tc_vec3_s position; - tc_vec3_s rotation; + tc_vec3f_s position; + tc_vec3f_s rotation; } tc_location_s; diff --git a/code/source-c/utility/math.c b/code/source-c/utility/math.c new file mode 100644 index 0000000..a06e98d --- /dev/null +++ b/code/source-c/utility/math.c @@ -0,0 +1,12 @@ +#include + +f32_t tc_deg_to_rad(f32_t degrees) +{ + return (degrees / 360.0f) * (3.1415 * 2.0f); +} + +f32_t tc_rad_to_deg(f32_t radians) +{ + return radians / (3.1415*2.0f) * 360.0f; +} + diff --git a/code/source-c/utility/math.h b/code/source-c/utility/math.h new file mode 100644 index 0000000..44182f9 --- /dev/null +++ b/code/source-c/utility/math.h @@ -0,0 +1,96 @@ + +#ifndef TC_MATH_H +#define TC_MATH_H + +#include + +typedef struct tc_vec4f tc_vec4f_s; +typedef struct tc_vec3f tc_vec3f_s; +typedef struct tc_vec2f tc_vec2f_s; + +typedef struct tc_vec4i tc_vec4i_s; +typedef struct tc_vec3i tc_vec3i_s; +typedef struct tc_vec2i tc_vec2i_s; + +typedef struct tc_mat4f tc_mat4f_s; +typedef struct tc_mat3f tc_mat3f_s; + +struct tc_vec4f +{ + f32_t x; + f32_t y; + f32_t z; + f32_t w; +}; + +struct tc_vec3f +{ + f32_t x; + f32_t y; + f32_t z; +}; + +struct tc_vec2f +{ + f32_t x; + f32_t y; +}; + +struct tc_vec4i +{ + i32_t x; + i32_t y; + i32_t z; + i32_t w; +}; + +struct tc_vec3i +{ + i32_t x; + i32_t y; + i32_t z; +}; + +struct tc_vec2i +{ + i32_t x; + i32_t y; +}; + + + +struct tc_mat4f +{ + f32_t values[4][4]; +}; + +struct tc_mat3f +{ + f32_t values[3][3]; +}; + +f32_t tc_deg_to_rad (f32_t degrees); +f32_t tc_rad_to_deg (f32_t radians); + +bool_t tc_vec4f_equ (tc_vec4f_s vector_1, tc_vec4f_s vector_2); +bool_t tc_vec3f_equ (tc_vec3f_s vector_1, tc_vec3f_s vector_2); +bool_t tc_vec2f_equ (tc_vec2f_s vector_1, tc_vec2f_s vector_2); + +bool_t tc_vec4i_equ (tc_vec4i_s vector_1, tc_vec4i_s vector_2); +bool_t tc_vec3i_equ (tc_vec3i_s vector_1, tc_vec3i_s vector_2); +bool_t tc_vec2i_equ (tc_vec2i_s vector_1, tc_vec2i_s vector_2); + +tc_mat4f_s tc_mat4f_identity (); +tc_mat3f_s tc_mat3f_identity (); + +tc_mat4f_s tc_mat4_rotate_x (tc_mat4f_s matrix, float radians); +tc_mat4f_s tc_mat4_rotate_y (tc_mat4f_s matrix, float radians); +tc_mat4f_s tc_mat4_rotate_z (tc_mat4f_s matrix, float radians); + +tc_mat4f_s tc_mat4f_translate (tc_mat4f_s matrix, tc_vec3f_s translation); +tc_mat3f_s tc_mat3f_translate (tc_mat3f_s matrix, tc_vec3f_s translation); + +tc_mat4f_s tc_mat4f_perspective (float fov, float aspect_ratio, float near, float far); + +#endif // TC_MATH_H + diff --git a/code/source-c/utility/matrix.c b/code/source-c/utility/matrix.c new file mode 100644 index 0000000..5ebc409 --- /dev/null +++ b/code/source-c/utility/matrix.c @@ -0,0 +1,169 @@ +#include + +#include + +tc_mat4f_s tc_mat4f_zero() +{ + tc_mat4f_s matrix; + matrix.values[0][0] = 0.0f; + matrix.values[0][1] = 0.0f; + matrix.values[0][2] = 0.0f; + matrix.values[0][3] = 0.0f; + + matrix.values[1][0] = 0.0f; + matrix.values[1][1] = 0.0f; + matrix.values[1][2] = 0.0f; + matrix.values[1][3] = 0.0f; + + matrix.values[2][0] = 0.0f; + matrix.values[2][1] = 0.0f; + matrix.values[2][2] = 0.0f; + matrix.values[2][3] = 0.0f; + + matrix.values[3][0] = 0.0f; + matrix.values[3][1] = 0.0f; + matrix.values[3][2] = 0.0f; + matrix.values[3][3] = 0.0f; + + return matrix; +} + +tc_mat4f_s tc_mat4f_identity() +{ + tc_mat4f_s matrix; + matrix.values[0][0] = 1.0f; + matrix.values[0][1] = 0.0f; + matrix.values[0][2] = 0.0f; + matrix.values[0][3] = 0.0f; + + matrix.values[1][0] = 0.0f; + matrix.values[1][1] = 1.0f; + matrix.values[1][2] = 0.0f; + matrix.values[1][3] = 0.0f; + + matrix.values[2][0] = 0.0f; + matrix.values[2][1] = 0.0f; + matrix.values[2][2] = 1.0f; + matrix.values[2][3] = 0.0f; + + matrix.values[3][0] = 0.0f; + matrix.values[3][1] = 0.0f; + matrix.values[3][2] = 0.0f; + matrix.values[3][3] = 1.0f; + + return matrix; +} + +tc_mat3f_s tc_mat3f_identity() +{ + tc_mat3f_s matrix; + matrix.values[0][0] = 1.0f; + matrix.values[0][1] = 0.0f; + matrix.values[0][2] = 0.0f; + + matrix.values[1][0] = 0.0f; + matrix.values[1][1] = 1.0f; + matrix.values[1][2] = 0.0f; + + matrix.values[2][0] = 0.0f; + matrix.values[2][1] = 0.0f; + matrix.values[2][2] = 1.0f; + + return matrix; +} + +tc_mat4f_s tc_mat4f_multiply(tc_mat4f_s first, tc_mat4f_s second) +{ + tc_mat4f_s result; + + u32_t k, r, c; + for(c = 0; c < 4; ++c) + { + for(r = 0; r < 4; ++r) + { + result.values[c][r] = 0.f; + for(k=0; k<4; ++k) + { + result.values[c][r] += first.values[k][r] * second.values[c][k]; + } + } + } + + return result; +} + + + +tc_mat4f_s tc_mat4_rotate_x(tc_mat4f_s matrix, float radians) +{ + tc_mat4f_s rotation = tc_mat4f_identity(); + rotation.values[1][1] = cosf(radians); + rotation.values[1][2] = -sinf(radians); + rotation.values[2][1] = sinf(radians); + rotation.values[2][2] = cosf(radians); + + return tc_mat4f_multiply(matrix, rotation); +} + +tc_mat4f_s tc_mat4_rotate_y(tc_mat4f_s matrix, float radians) +{ + tc_mat4f_s rotation = tc_mat4f_identity(); + rotation.values[0][0] = cosf(radians); + rotation.values[0][2] = sinf(radians); + rotation.values[2][0] = -sinf(radians); + rotation.values[2][2] = cosf(radians); + + return tc_mat4f_multiply(matrix, rotation); +} + +tc_mat4f_s tc_mat4_rotate_z(tc_mat4f_s matrix, float radians) +{ + tc_mat4f_s rotation = tc_mat4f_identity(); + rotation.values[0][0] = cosf(radians); + rotation.values[0][1] = -sinf(radians); + rotation.values[1][0] = sinf(radians); + rotation.values[1][1] = cosf(radians); + + return tc_mat4f_multiply(matrix, rotation); +} + + + +tc_mat4f_s tc_mat4f_translate(tc_mat4f_s matrix, tc_vec3f_s translation) +{ + matrix.values[0][3] += translation.x; + matrix.values[1][3] += translation.y; + matrix.values[2][3] += translation.z; + + return matrix; +} + +tc_mat3f_s tc_mat3f_translate(tc_mat3f_s matrix, tc_vec3f_s translation) +{ + matrix.values[0][2] += translation.x; + matrix.values[1][2] += translation.y; + matrix.values[2][2] += translation.z; + + return matrix; +} + + + +tc_mat4f_s tc_mat4f_perspective(float fov, float aspect_ratio, float near, float far) +{ + float range = tanf(fov / 2) * near; + float scale_x = (2 * near) / (range * aspect_ratio * 2); + float scale_y = near / range; + float scale_z = -(far + near) / (far - near); + float position_z = -(2 * far * near) / (far - near); + + tc_mat4f_s matrix = tc_mat4f_zero(); + matrix.values[0][0] = scale_x; + matrix.values[1][1] = scale_y; + matrix.values[2][2] = scale_z; + matrix.values[3][2] = -1.0f; + matrix.values[2][3] = position_z; + + return matrix; +} + diff --git a/code/source-c/utility/utility.h b/code/source-c/utility/utility.h new file mode 100644 index 0000000..0af7410 --- /dev/null +++ b/code/source-c/utility/utility.h @@ -0,0 +1,23 @@ + +#ifndef TC_UTILITY_H +#define TC_UTILITY_H + +typedef unsigned char u8_t; +typedef unsigned short u16_t; +typedef unsigned int u32_t; +typedef unsigned long u64_t; + +typedef signed char i8_t; +typedef signed short i16_t; +typedef signed int i32_t; +typedef signed long i64_t; + +typedef float f32_t; +typedef double f64_t; + +typedef unsigned char bool_t; +#define TRUE 1 +#define FALSE 0 + +#endif + diff --git a/code/source-c/utility/vector.c b/code/source-c/utility/vector.c new file mode 100644 index 0000000..58c7e7d --- /dev/null +++ b/code/source-c/utility/vector.c @@ -0,0 +1,52 @@ +#include + +bool_t tc_vec4f_equ(tc_vec4f_s vector_1, tc_vec4f_s vector_2) +{ + if(vector_1.x != vector_2.x) return FALSE; + if(vector_1.y != vector_2.y) return FALSE; + if(vector_1.z != vector_2.z) return FALSE; + if(vector_1.w != vector_2.w) return FALSE; + return TRUE; +} + +bool_t tc_vec3f_equ(tc_vec3f_s vector_1, tc_vec3f_s vector_2) +{ + if(vector_1.x != vector_2.x) return FALSE; + if(vector_1.y != vector_2.y) return FALSE; + if(vector_1.z != vector_2.z) return FALSE; + return TRUE; +} + +bool_t tc_vec2f_equ(tc_vec2f_s vector_1, tc_vec2f_s vector_2) +{ + if(vector_1.x != vector_2.x) return FALSE; + if(vector_1.y != vector_2.y) return FALSE; + return TRUE; +} + + + +bool_t tc_vec4i_equ(tc_vec4i_s vector_1, tc_vec4i_s vector_2) +{ + if(vector_1.x != vector_2.x) return FALSE; + if(vector_1.y != vector_2.y) return FALSE; + if(vector_1.z != vector_2.z) return FALSE; + if(vector_1.w != vector_2.w) return FALSE; + return TRUE; +} + +bool_t tc_vec3i_equ(tc_vec3i_s vector_1, tc_vec3i_s vector_2) +{ + if(vector_1.x != vector_2.x) return FALSE; + if(vector_1.y != vector_2.y) return FALSE; + if(vector_1.z != vector_2.z) return FALSE; + return TRUE; +} + +bool_t tc_vec2i_equ(tc_vec2i_s vector_1, tc_vec2i_s vector_2) +{ + if(vector_1.x != vector_2.x) return FALSE; + if(vector_1.y != vector_2.y) return FALSE; + return TRUE; +} + diff --git a/code/source-c/vector.c b/code/source-c/vector.c deleted file mode 100644 index e665ede..0000000 --- a/code/source-c/vector.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "utility.h" - -bool tc_vec3i_equ(tc_vec3i_s first, tc_vec3i_s second) -{ - if(first.x != second.x) return false; - if(first.y != second.y) return false; - if(first.z != second.z) return false; - return true; -} diff --git a/code/source-c/world.c b/code/source-c/world.c index babc6ab..36c58c5 100644 --- a/code/source-c/world.c +++ b/code/source-c/world.c @@ -1,6 +1,5 @@ -#include "world.h" -#include "state.h" -#include +#include +#include #include @@ -42,15 +41,18 @@ void tc_draw_chunk_entity(tc_entity_s *entity) { tc_chunk_s *chunk = entity->specific; - mat4x4 model_matrix; - mat4x4_identity(model_matrix); - mat4x4_translate_in_place(model_matrix, chunk->position.x*32, chunk->position.y*32, -chunk->position.z*32); + tc_mat4f_s model_matrix = tc_mat4f_identity(); - int model_matrix_uniform_location = - glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "model_matrix"); - glUniformMatrix4fv(model_matrix_uniform_location, 1, GL_FALSE, &model_matrix[0][0]); + tc_vec3f_s position_3f; + position_3f.x = chunk->position.x * 32; + position_3f.y = chunk->position.y * 32; + position_3f.z = chunk->position.z * 32; + + model_matrix = + tc_mat4f_translate(model_matrix, position_3f); + + tc_shader_uniform_mat4f(&tc_game_state_g.renderer.draw_shader, "model_matrix", model_matrix); - // glBindVertexArray(chunk->vao); glBindBuffer(GL_ARRAY_BUFFER, chunk->vertex_data); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0); glEnableVertexAttribArray(0); diff --git a/code/source-c/world.h b/code/source-c/world.h index 970e520..9ef8b72 100644 --- a/code/source-c/world.h +++ b/code/source-c/world.h @@ -5,8 +5,8 @@ #include #include -#include "blocks.h" -#include "utility.h" +#include +#include #define UPDATE_DISTANCE 2 @@ -30,9 +30,8 @@ struct tc_worldgen struct tc_block { uint32_t type_identifier; - tc_vec3_s position; - tc_vec3_s rotation; - tc_object_s drawing; + tc_vec3f_s position; + tc_vec3f_s rotation; };