minecraft

minecraft
git clone git://git.evenfri.pw/minecraft.git
Log | Files | Refs | README

commit bfebd713b7ecf23cbd93ac8f0ab26121947b72a1
parent 5f726bff1320e104721e4d01a8e24aa8800cd746
Author: evenfri <evenfri@4plt.ch>
Date:   Tue,  3 Nov 2026 00:24:52 +0800

separate vec3 and mat4 from camera.c, add blocks_end execution in main()

Diffstat:
Mcamera.c | 66++++--------------------------------------------------------------
Aconfig.h | 16++++++++++++++++
Mdat.h | 22+++++++---------------
Mfns.h | 6++++++
Mmain.c | 1+
Amat4.c | 28++++++++++++++++++++++++++++
Mminecraft.h | 1+
Mmkfile | 5++++-
Avec3.c | 32++++++++++++++++++++++++++++++++
9 files changed, 99 insertions(+), 78 deletions(-)

diff --git a/camera.c b/camera.c @@ -1,66 +1,8 @@ -#include "minecraft.h" #include <math.h> - -#define PI 3.14159265358979323846f +#include "minecraft.h" Camera camera; -vec3 vec3_make(float x, float y, float z) { - vec3 v = {x, y, z}; - return v; -} - -/* - * static vec3 vec3_sub (vec3 a, vec3 b) { - * return vec3_make(a.x - b.x, a.y - b.y, a.z - b.z); - * } - */ - -static vec3 vec3_scale(vec3 a, float s) { - return vec3_make(a.x * s, a.y * s, a.z * s); -} - -static float vec3_dot (vec3 a, vec3 b) { - return a.x * b.x + a.y * b.y + a.z * b.z; -} - -static vec3 vec3_cross(vec3 a, vec3 b) { - return vec3_make(a.y * b.z - a.z * b.y, - a.z * b.x - a.x * b.z, - a.x * b.y - a.y * b.x); -} - -static vec3 vec3_norm(vec3 a) { - float l = sqrtf(vec3_dot(a, a)); - if (l < 1e-6f) return vec3_make(0, 0, 0); - return vec3_scale(a, 1.0f / l); -} - -mat4 mat4_identity(void) { - mat4 r = {{0}}; - r.m[0] = r.m[5] = r.m[10] = r.m[15] = 1.0f; - return r; -} - -mat4 mat4_perspective(float fovy, float aspect, float znear, float zfar) { - mat4 r = {{0}}; - float f = 1.0f / tanf(0.5f * fovy * PI / 180.0f); - r.m[0] = f / aspect; - r.m[5] = f; - r.m[10] = (zfar + znear) / (znear - zfar); - r.m[11] = -1.0f; - r.m[14] = (2.0f * zfar * znear) / (znear - zfar); - return r; -} - -mat4 mat4_translate(float x, float y, float z) { - mat4 r = mat4_identity(); - r.m[12] = x; - r.m[13] = y; - r.m[14] = z; - return r; -} - void camera_update_vectors(void) { float yr = camera.yaw * PI / 180.0f; float pr = camera.pitch * PI / 180.0f; @@ -78,10 +20,10 @@ void camera_update_vectors(void) { void camera_init(vec3 position) { camera.position = position; camera.world_up = vec3_make(0.0f, 1.0f, 0.0f); - camera.yaw = -90.0f; /* look to -90 z */ + camera.yaw = 0.0f; camera.pitch = 0.0f; - camera.speed = 2.5f; - camera.sensitivity = 0.1f; + camera.speed = CAMERA_SPEED; + camera.sensitivity = CAMERA_SENSITIVITY; camera.fov = CAMERA_FOV; camera_update_vectors(); } diff --git a/config.h b/config.h @@ -0,0 +1,16 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#define WINDOW_WIDTH 800 +#define WINDOW_HEIGHT 600 +#define WINDOW_TITLE "window" + +#define CHUNK_SIZE 16 +#define CHUNK_HEIGHT 64 +#define WORLD_SIZE 4 + +#define CAMERA_FOV 60 +#define CAMERA_SPEED 3.0f +#define CAMERA_SENSITIVITY 0.1f + +#endif /* CONFIG_H */ diff --git a/dat.h b/dat.h @@ -3,13 +3,7 @@ #include "minecraft.h" -#define WINDOW_WIDTH 800 -#define WINDOW_HEIGHT 600 -#define WINDOW_TITLE "window" - -#define CHUNK_SIZE 16 -#define CHUNK_HEIGHT 64 -#define WORLD_SIZE 4 +#define PI 3.14159265358979323846f typedef struct { float x, y, z; } vec3; typedef struct { float m[16]; } mat4; @@ -27,8 +21,6 @@ typedef struct { float fov; } Camera; -#define CAMERA_FOV 60 - #define FACE_BACK 0 #define FACE_FRONT 1 #define FACE_LEFT 2 @@ -37,6 +29,12 @@ typedef struct { #define FACE_TOP 5 #define FACE_COUNT 6 +#define BLOCK_AIR 0 +#define BLOCK_GRASS 1 +#define BLOCK_STONE 2 +#define BLOCK_DIRT 3 +#define BLOCK_COBBLESTONE 4 + typedef struct { unsigned int id; char *texture_paths[FACE_COUNT]; @@ -51,12 +49,6 @@ typedef struct { Chunk_t *chunks[WORLD_SIZE][WORLD_SIZE]; } World_t; -#define BLOCK_AIR 0 -#define BLOCK_GRASS 1 -#define BLOCK_STONE 2 -#define BLOCK_DIRT 3 -#define BLOCK_COBBLESTONE 4 - extern GLFWwindow *Window; extern unsigned int Shader; extern Camera camera; diff --git a/fns.h b/fns.h @@ -13,6 +13,12 @@ void shader_init(); void shader_end(); vec3 vec3_make(float x, float y, float z); +vec3 vec3_sub(vec3 a, vec3 b); +vec3 vec3_scale(vec3 a, float s); +float vec3_dot(vec3 a, vec3 b); +vec3 vec3_cross(vec3 a, vec3 b); +vec3 vec3_norm(vec3 a); + mat4 mat4_identity(void); mat4 mat4_perspective(float fovy, float aspect, float znear, float zfar); mat4 mat4_translate(float x, float y, float z); diff --git a/main.c b/main.c @@ -17,6 +17,7 @@ int main() { process_input(dt); render_frame(); } + blocks_end(); shader_end(); render_end(); glfwTerminate(); diff --git a/mat4.c b/mat4.c @@ -0,0 +1,28 @@ +#include <math.h> +#include "minecraft.h" + +mat4 mat4_identity(void) { + mat4 r = {{0}}; + r.m[0] = r.m[5] = r.m[10] = r.m[15] = 1.0f; + return r; +} + +mat4 mat4_perspective(float fovy, float aspect, float znear, float zfar) { + mat4 r = {{0}}; + float f = 1.0f / tanf(0.5f * fovy * PI / 180.0f); + r.m[0] = f / aspect; + r.m[5] = f; + r.m[10] = (zfar + znear) / (znear - zfar); + r.m[11] = -1.0f; + r.m[14] = (2.0f * zfar * znear) / (znear - zfar); + return r; +} + +mat4 mat4_translate(float x, float y, float z) { + mat4 r = mat4_identity(); + r.m[12] = x; + r.m[13] = y; + r.m[14] = z; + return r; +} + diff --git a/minecraft.h b/minecraft.h @@ -5,6 +5,7 @@ #include <stdio.h> #define GL_GLEXT_PROTOTYPES #include <GLFW/glfw3.h> +#include "config.h" #include "dat.h" #include "fns.h" diff --git a/mkfile b/mkfile @@ -5,6 +5,8 @@ ld = gcc ldflags = -lglfw -lGL -lm ofiles = \ + vec3.$o \ + mat4.$o \ window.$o \ input.$o \ render.$o \ @@ -17,7 +19,8 @@ ofiles = \ hfiles = \ minecraft.h \ fns.h \ - dat.h + dat.h \ + config.h targ = minecraft diff --git a/vec3.c b/vec3.c @@ -0,0 +1,32 @@ +#include <math.h> +#include "minecraft.h" + +vec3 vec3_make(float x, float y, float z) { + vec3 v = {x, y, z}; + return v; +} + +vec3 vec3_sub(vec3 a, vec3 b) { + return vec3_make(a.x - b.x, a.y - b.y, a.z - b.z); +} + +vec3 vec3_scale(vec3 a, float s) { + return vec3_make(a.x * s, a.y * s, a.z * s); +} + +float vec3_dot(vec3 a, vec3 b) { + return a.x * b.x + a.y * b.y + a.z * b.z; +} + +vec3 vec3_cross(vec3 a, vec3 b) { + return vec3_make(a.y * b.z - a.z * b.y, + a.z * b.x - a.x * b.z, + a.x * b.y - a.y * b.x); +} + +vec3 vec3_norm(vec3 a) { + float l = sqrtf(vec3_dot(a, a)); + if (l < 1e-6f) return vec3_make(0, 0, 0); + return vec3_scale(a, 1.0f / l); +} +