Partially integrated own mathematics library

This commit is contained in:
Eric-Paul Ickhorn 2023-10-15 10:07:33 +02:00
parent 40be22f69b
commit dc3a81b7b4
18 changed files with 434 additions and 202 deletions

View File

@ -7,6 +7,6 @@ gcc -g3 -o dependencies/build/glad.o \
-I dependencies/include/ -Wall -I dependencies/include/ -Wall
gcc -g3 -o techneck.elf \ gcc -g3 -o techneck.elf \
code/source-c/*.c code/source-c/entity/*.c dependencies/build/glad.o \ code/source-c/*.c code/source-c/entity/*.c code/source-c/utility/*.c dependencies/build/glad.o \
-I dependencies/include -lGL -lSDL2 -lm -Wall -I dependencies/include -I code/source-c/ -lGL -lSDL2 -lm -Wall

View File

@ -319,6 +319,5 @@ void tc_draw_all_entities_of_type(char *name)
} }
++entity_index; ++entity_index;
} }
printf("Drew %d out of %d Entities!\n", drawn_entities, registry.num_entities);
} }

View File

@ -2,8 +2,8 @@
#ifndef TC_ENTITY_H #ifndef TC_ENTITY_H
#define TC_ENTITY_H #define TC_ENTITY_H
#include "location.h" #include "utility/location.h"
#include "utility.h" #include "utility/utility.h"
typedef struct tc_entity_event tc_entity_event_s; typedef struct tc_entity_event tc_entity_event_s;
typedef struct tc_entity_type tc_entity_type_s; typedef struct tc_entity_type tc_entity_type_s;

View File

@ -1,69 +1,10 @@
#include "state.h" #include <state.h>
#include <utility/math.h>
#include <glad/glad.h> #include <glad/glad.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <linmath.h> #include <linmath.h>
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) void tc_move_viewer_to(float x, float y, float z)
{ {
if(tc_game_state_g.viewer == NULL) return; 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"); 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() void render()
{ {
float color[4] = { 0.12f, 0.5f, 0.8f, 1.0f }; float color[4] = { 0.12f, 0.5f, 0.8f, 1.0f };
glClearBufferfv(GL_COLOR, 0, color); glClearBufferfv(GL_COLOR, 0, color);
float depth = 10000000.0f; float depth = 1000.0f;
glClearBufferfv(GL_DEPTH, 0, &depth); glClearBufferfv(GL_DEPTH, 0, &depth);
int fb_width = 0; int fb_width = 0;
@ -146,29 +53,35 @@ void render()
SDL_GetWindowSize(tc_game_state_g.renderer.window, &fb_width, &fb_height); SDL_GetWindowSize(tc_game_state_g.renderer.window, &fb_width, &fb_height);
glViewport(0, 0, 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_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); tc_get_viewer_position(&camera_position.x, &camera_position.y, &camera_position.z);
mat4x4_identity(view_matrix); tc_vec3f_s camera_offset;
mat4x4_rotate_X(view_matrix, view_matrix, camera_rotation.x); camera_offset.z = camera_position.z;
mat4x4_rotate_Y(view_matrix, view_matrix, camera_rotation.y); camera_offset.y = -camera_position.y;
mat4x4_rotate_Z(view_matrix, view_matrix, camera_rotation.z); 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); 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"); tc_shader_uniform_mat4f(&tc_game_state_g.renderer.draw_shader, "projection_matrix", projection_matrix);
glUniformMatrix4fv(projection_uniform_location, 1, GL_FALSE, &projection_matrix[0][0]); tc_shader_uniform_mat4f(&tc_game_state_g.renderer.draw_shader, "view_matrix", view_matrix);
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_draw_world(tc_game_state_g.main_world); tc_draw_world(tc_game_state_g.main_world);
@ -188,7 +101,7 @@ uint64_t tc_last_update = 0;
bool update() 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); tc_get_viewer_rotation(&camera_rotation.x, &camera_rotation.y, &camera_rotation.z);
SDL_Event event; SDL_Event event;
@ -299,20 +212,20 @@ bool update()
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
{ {
if(!(event.motion.state & SDL_BUTTON_LMASK)) break; if(!(event.motion.state & SDL_BUTTON_LMASK)) break;
camera_rotation.y += event.motion.xrel / 50.0f; camera_rotation.y += event.motion.xrel / 10.0f;
camera_rotation.x += event.motion.yrel / 50.0f; camera_rotation.x += event.motion.yrel / 10.0f;
if(camera_rotation.x > (3.1415f/2.0f)) if(camera_rotation.x > 90)
camera_rotation.x = (3.1415f/2.0f); camera_rotation.x = 90;
if(camera_rotation.x < -(3.1415f/2.0f)) if(camera_rotation.x < -90)
camera_rotation.x = -(3.1415f/2.0f); camera_rotation.x = -90;
} break; } break;
} }
} }
tc_vec3_s camera_position; tc_vec3f_s camera_position;
tc_get_viewer_position(&camera_position.x, &camera_position.y, &camera_position.z); tc_get_viewer_position(&camera_position.x, &camera_position.y, &camera_position.z);
if(shift_pressed) 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.x = 0.0f;
difference_in_perspective.y = 0.0f; difference_in_perspective.y = 0.0f;
difference_in_perspective.z = 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_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_Z(position_matrix, position_matrix, tc_deg_to_rad(camera_rotation.z));
mat4x4_rotate_Y(position_matrix, position_matrix, camera_rotation.y); mat4x4_rotate_Y(position_matrix, position_matrix, tc_deg_to_rad(camera_rotation.y));
mat4x4_rotate_Z(position_matrix, position_matrix, camera_rotation.z); 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 viewed_difference = { difference_in_perspective.x, 0.0f, difference_in_perspective.z };
vec4 raw_difference = { 0.0f, 0.0f, 0.0f }; 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.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.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); tc_update_world(tc_game_state_g.main_world);

View File

@ -92,3 +92,10 @@ tc_shader_program_s tc_make_shader_program(char *vertex_path, char *fragment_pat
return shader_program; 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]);
}

View File

@ -9,13 +9,18 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <utility/math.h>
typedef struct tc_shader_program typedef struct tc_shader_program
{ {
uint32_t program_id; uint32_t program_id;
} tc_shader_program_s; } 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 #endif

View File

@ -6,17 +6,17 @@
#include <stdint.h> #include <stdint.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "assets.h" #include <assets.h>
#include "shaders.h" #include <shaders.h>
#include "entity.h" #include <entity.h>
#include "blocks.h" #include <blocks.h>
#include "world.h" #include <world.h>
#include "utility.h" #include <utility/utility.h>
typedef struct tc_camera typedef struct tc_camera
{ {
tc_vec3_s position; tc_vec3f_s position;
tc_vec3_s rotation; tc_vec3f_s rotation;
float fov; float fov;
} tc_camera_s; } tc_camera_s;

View File

@ -1,36 +0,0 @@
#ifndef TC_UTILITY_H
#define TC_UTILITY_H
#include <stdint.h>
#include <stdbool.h>
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

View File

@ -1,4 +1,4 @@
#include "location.h" #include <utility/location.h>
#include <stdlib.h> #include <stdlib.h>

View File

@ -2,13 +2,13 @@
#ifndef TC_LOCATION_H #ifndef TC_LOCATION_H
#define TC_LOCATION_H #define TC_LOCATION_H
#include "world.h" #include <world.h>
typedef struct tc_location typedef struct tc_location
{ {
tc_world_s *world; tc_world_s *world;
tc_vec3_s position; tc_vec3f_s position;
tc_vec3_s rotation; tc_vec3f_s rotation;
} tc_location_s; } tc_location_s;

View File

@ -0,0 +1,12 @@
#include <utility/math.h>
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;
}

View File

@ -0,0 +1,96 @@
#ifndef TC_MATH_H
#define TC_MATH_H
#include <utility/utility.h>
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

View File

@ -0,0 +1,169 @@
#include <utility/math.h>
#include <math.h>
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;
}

View File

@ -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

View File

@ -0,0 +1,52 @@
#include <utility/math.h>
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;
}

View File

@ -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;
}

View File

@ -1,6 +1,5 @@
#include "world.h" #include <world.h>
#include "state.h" #include <state.h>
#include <linmath.h>
#include <stdlib.h> #include <stdlib.h>
@ -42,15 +41,18 @@ void tc_draw_chunk_entity(tc_entity_s *entity)
{ {
tc_chunk_s *chunk = entity->specific; tc_chunk_s *chunk = entity->specific;
mat4x4 model_matrix; tc_mat4f_s model_matrix = tc_mat4f_identity();
mat4x4_identity(model_matrix);
mat4x4_translate_in_place(model_matrix, chunk->position.x*32, chunk->position.y*32, -chunk->position.z*32);
int model_matrix_uniform_location = tc_vec3f_s position_3f;
glGetUniformLocation(tc_game_state_g.renderer.draw_shader.program_id, "model_matrix"); position_3f.x = chunk->position.x * 32;
glUniformMatrix4fv(model_matrix_uniform_location, 1, GL_FALSE, &model_matrix[0][0]); 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); glBindBuffer(GL_ARRAY_BUFFER, chunk->vertex_data);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);

View File

@ -5,8 +5,8 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include "blocks.h" #include <blocks.h>
#include "utility.h" #include <utility/math.h>
#define UPDATE_DISTANCE 2 #define UPDATE_DISTANCE 2
@ -30,9 +30,8 @@ struct tc_worldgen
struct tc_block struct tc_block
{ {
uint32_t type_identifier; uint32_t type_identifier;
tc_vec3_s position; tc_vec3f_s position;
tc_vec3_s rotation; tc_vec3f_s rotation;
tc_object_s drawing;
}; };