commit 0843fdab7a06297b0dc3ccaa600edf63c5c7e1a0
parent d1aff1f0a60c16d97bf5ce3a14614da3d184e91e
Author: evenfri <evenfri@4plt.ch>
Date: Mon, 2 Nov 2026 21:13:42 +0800
add render_chunk function, fix camera code
Diffstat:
4 files changed, 37 insertions(+), 16 deletions(-)
diff --git a/camera.c b/camera.c
@@ -5,7 +5,7 @@
Camera camera;
-static vec3 vec3_make(float x, float y, float z) {
+vec3 vec3_make(float x, float y, float z) {
vec3 v = {x, y, z};
return v;
}
@@ -16,6 +16,10 @@ static vec3 vec3_make(float x, float y, float 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;
}
@@ -32,10 +36,10 @@ static vec3 vec3_norm(vec3 a) {
return vec3_scale(a, 1.0f / l);
}
-mat4 mat4_identity(void)
+mat4 mat4_identity(void) {
mat4 r = {{0}};
-r.m[0] = r.m[5] = r.m[10] = r.m[15] = 1.0f;
-return r;
+ 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) {
@@ -71,8 +75,8 @@ void camera_update_vectors(void) {
camera.up = vec3_norm(vec3_cross(camera.right, camera.front));
}
-void camera_init(void) {
- camera.position = vec3_make(0.0f, 0.0f, 3.0f);
+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.pitch = 0.0f;
diff --git a/fns.h b/fns.h
@@ -12,14 +12,15 @@ void render_end();
void shader_init();
void shader_end();
-void camera_init(void);
-void camera_update_vectors(void);
-mat4 camera_get_view(void);
-
+vec3 vec3_make(float x, float y, float z);
mat4 mat4_identity(void);
mat4 mat4_perspective(float fovy, float aspect, float znear, float zfar);
mat4 mat4_translate(float x, float y, float z);
+void camera_init(vec3 position);
+void camera_update_vectors(void);
+mat4 camera_get_view(void);
+
void camera_process_keyboard(float dt);
void camera_process_mouse(float dx, float dy);
diff --git a/main.c b/main.c
@@ -2,7 +2,7 @@
int main() {
window_init();
- camera_init();
+ camera_init(vec3_make(0, 2, 0));
shader_init();
render_init();
diff --git a/render.c b/render.c
@@ -6,6 +6,8 @@ unsigned int VBO, VAO, EBO;
unsigned int Texture;
static int u_model, u_view, u_proj;
+#define CHUNK_SIZE 16
+
void render_init() {
float vertices[] = {
/* back */
@@ -97,6 +99,22 @@ void render_block(unsigned int block_id, vec3 pos) {
}
}
+void render_chunk(vec3 position) {
+ int base_x = (int)(position.x * CHUNK_SIZE);
+ int base_z = (int)(position.z * CHUNK_SIZE);
+
+ for (int x = 0; x < CHUNK_SIZE; x++) {
+ for (int z = 0; z < CHUNK_SIZE; z++) {
+ float wx = (float)(base_x + x);
+ float wz = (float)(base_z + z);
+
+ render_block(BLOCK_GRASS, (vec3){ wx, 0.0f, wz });
+ render_block(BLOCK_DIRT, (vec3){ wx, -1.0f, wz });
+ render_block(BLOCK_STONE, (vec3){ wx, -2.0f, wz });
+ }
+ }
+}
+
void render_frame() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -112,11 +130,9 @@ void render_frame() {
glBindVertexArray(VAO);
- for (int x = 0; x < 16; x++) {
- for (int z = 0; z < 16; z++) {
- render_block(BLOCK_GRASS, (vec3){ x, 0.0f, z });
- render_block(BLOCK_DIRT, (vec3){ x, -1.0f, z });
- render_block(BLOCK_STONE, (vec3){ x, -2.0f, z });
+ for (int x = -2; x < 2; x++) {
+ for (int z = -2; z < 2; z++) {
+ render_chunk((vec3){x, 0, z});
}
}