minecraft

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

commit 5f726bff1320e104721e4d01a8e24aa8800cd746
parent 8f13d38ecb67d9d63b663b199c6c1dfcf1199b3c
Author: evenfri <evenfri@4plt.ch>
Date:   Mon,  2 Nov 2026 22:42:39 +0800

culling faces, fix faces for ccw

Diffstat:
Mrender.c | 73+++++++++++++++++++++++++++++++++++++------------------------------------
Mwindow.c | 3+++
Mworld.c | 15+++++----------
3 files changed, 45 insertions(+), 46 deletions(-)

diff --git a/render.c b/render.c @@ -9,52 +9,52 @@ static int u_model, u_view, u_proj; void render_init() { float vertices[] = { /* back */ - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, /* front */ - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, /* left */ - -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, - -0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + -0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, /* right */ - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, /* bottom */ - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, /* top */ - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f }; glGenVertexArrays(1, &VAO); @@ -91,6 +91,7 @@ void render_block(unsigned int block_id, vec3 pos, uint8_t r_right, uint8_t r_bottom, uint8_t r_top) { + if (!(r_back || r_front || r_left || r_right || r_bottom || r_top)) return; mat4 model = mat4_translate(pos.x, pos.y, pos.z); glUniformMatrix4fv(u_model, 1, GL_FALSE, model.m); diff --git a/window.c b/window.c @@ -47,4 +47,7 @@ void window_init() { glfwSetInputMode(Window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + glFrontFace(GL_CCW); } diff --git a/world.c b/world.c @@ -6,19 +6,15 @@ Chunk_t* world_generate_chunk(vec3 position) { Chunk_t *ret = calloc(1, sizeof(*ret)); if (!ret) { perror("calloc"); exit(1); } - int block_id = 0; + int block_id = BLOCK_AIR; + const int surface = CHUNK_HEIGHT / 2; for (int x = 0; x < CHUNK_SIZE; x++) { for (int z = 0; z < CHUNK_SIZE; z++) { for (int y = 0; y < CHUNK_HEIGHT/2; y++) { - if (y >= CHUNK_HEIGHT/2) { - } else if (y == (CHUNK_HEIGHT/2) - 1) { - block_id = BLOCK_GRASS; - } else if (y >= (CHUNK_HEIGHT/2) - 3) { - block_id = BLOCK_DIRT; - } else { - block_id = BLOCK_STONE; - } + if (y == surface - 1) block_id = BLOCK_GRASS; + else if (y >= surface - 3) block_id = BLOCK_DIRT; + else if (y < surface) block_id = BLOCK_STONE; ret->blocks[x][y][z] = block_id; } @@ -26,7 +22,6 @@ Chunk_t* world_generate_chunk(vec3 position) { } ret->position = position; - return ret; }