diff --git a/.vs/samcraft/v17/.suo b/.vs/samcraft/v17/.suo index 9db64b1..6c65e45 100644 Binary files a/.vs/samcraft/v17/.suo and b/.vs/samcraft/v17/.suo differ diff --git a/.vs/samcraft/v17/Browse.VC.db b/.vs/samcraft/v17/Browse.VC.db index 205a924..0c12809 100644 Binary files a/.vs/samcraft/v17/Browse.VC.db and b/.vs/samcraft/v17/Browse.VC.db differ diff --git a/samcraft/chunk.c b/samcraft/chunk.c index f6232db..198757b 100644 --- a/samcraft/chunk.c +++ b/samcraft/chunk.c @@ -14,7 +14,7 @@ int coord_to_index(vector3 coord) { return index; } -int get_block_at(int chunk[WIDTH][HEIGHT][WIDTH], int x, int y, int z) { - if (x < 0 || x >= WIDTH || z < 0 || z >= WIDTH || y < 0 || y >= HEIGHT) return 0; - return chunk[x][y][z]; +int get_block_at(Chunk chunk, vector3 pos) { + if (pos.x < 0 || pos.x >= WIDTH || pos.z < 0 || pos.z >= WIDTH || pos.y < 0 || pos.y >= HEIGHT) return 0; + return chunk[coord_to_index(pos)]; } diff --git a/samcraft/chunk.h b/samcraft/chunk.h index 270d9d9..1d2fdda 100644 --- a/samcraft/chunk.h +++ b/samcraft/chunk.h @@ -2,12 +2,15 @@ #define __CHUNK_H__ #include "vector.h" +#include -#define WIDTH 1024 +#define WIDTH 16 #define HEIGHT 256 +typedef uint8_t Chunk[HEIGHT * WIDTH * WIDTH]; + vector3 index_to_coord(int i); int coord_to_index(vector3 pos); -int get_block_at(int chunk[WIDTH][HEIGHT][WIDTH], int x, int y, int z); +int get_block_at(Chunk chunk, vector3 pos); #endif diff --git a/samcraft/main.c b/samcraft/main.c index ad941c5..4eb3583 100644 --- a/samcraft/main.c +++ b/samcraft/main.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "faces.h" #include "chunk.h" @@ -10,7 +11,7 @@ #define STB_PERLIN_IMPLEMENTATION #include "stb_perlin.h" -Mesh gen_mesh(int chunk[WIDTH][HEIGHT][WIDTH]) { +Mesh gen_mesh(Chunk chunk) { Mesh mesh = { 0 }; mesh.vertices = malloc(0); mesh.vertexCount = 0; @@ -19,27 +20,26 @@ Mesh gen_mesh(int chunk[WIDTH][HEIGHT][WIDTH]) { for(int x = 0; x < WIDTH; x++) { for (int y = 0; y < HEIGHT; y++) { for (int z = 0; z < WIDTH; z++) { - int block_id = chunk[x][y][z]; + vector3 pos = vec3(x, y, z); + int block_id = get_block_at(chunk, pos); if (block_id == 0) continue; - vector3 pos = { x, y, z }; - //vector3 pos = index_to_coord(i); - - if (get_block_at(chunk, x, y - 1, z) == 0) + + if (get_block_at(chunk, vec3(x, y - 1, z)) == 0) add_face(&mesh, bottom, bottom_tex, block_id, pos); - if (get_block_at(chunk, x, y + 1, z) == 0) + if (get_block_at(chunk, vec3(x, y + 1, z)) == 0) add_face(&mesh, top, top_tex, block_id == 1 ? 4 : block_id, pos); - if (get_block_at(chunk, x - 1, y, z) == 0) + if (get_block_at(chunk, vec3(x - 1, y, z)) == 0) add_face(&mesh, left, left_tex, block_id, pos); - if (get_block_at(chunk, x + 1, y, z) == 0) + if (get_block_at(chunk, vec3(x + 1, y, z)) == 0) add_face(&mesh, right, right_tex, block_id, pos); - if (get_block_at(chunk, x, y, z - 1) == 0) + if (get_block_at(chunk, vec3(x, y, z - 1)) == 0) add_face(&mesh, back, back_tex, block_id, pos); - if (get_block_at(chunk, x, y, z + 1) == 0) + if (get_block_at(chunk, vec3(x, y, z + 1)) == 0) add_face(&mesh, front, front_tex, block_id, pos); } } @@ -56,25 +56,29 @@ int main() { InitWindow(800, 600, "samcraft"); //SetTargetFPS(60); - int (*chunk)[HEIGHT][WIDTH] = calloc(WIDTH * HEIGHT * WIDTH, sizeof(int)); - for (int x = 0; x < WIDTH; x++) { - for (int z = 0; z < WIDTH; z++) { - int height = (90 + stb_perlin_fbm_noise3(x / 40.0f, x / 40.0f, z / 40.0f, 2.0f, 0.5f, 6) * 10.0f); - for (int y = 0; y < height; y++) { - int id = 2; - if (height - y == 1) id = 1; - if (height - y == 2) id = 3; - chunk[x][y][z] = id; + Chunk* chunks = calloc(2, sizeof(Chunk)); + Mesh* chunk_meshes = calloc(2, sizeof(Mesh)); + + for (int i = 0; i < 2; i++) { + for (int x = 0; x < WIDTH; x++) { + for (int z = 0; z < WIDTH; z++) { + int height = (90 + stb_perlin_fbm_noise3(((i * WIDTH) + x) / 40.0f, x / 40.0f, z / 40.0f, 2.0f, 0.5f, 6) * 10.0f); + for (int y = 0; y < height; y++) { + int id = 2; + if (height - y == 1) id = 1; + if (height - y == 2) id = 3; + chunks[i][coord_to_index((vector3){x, y, z})] = id; + } } } + + Mesh mesh = gen_mesh(chunks[i]); + chunk_meshes[i] = mesh; } - Mesh chunk_mesh = gen_mesh(chunk); - Material mat = LoadMaterialDefault(); - Matrix matrix = MatrixIdentity(); - Texture atlas = LoadTexture("atlas.png"); + Material mat = LoadMaterialDefault(); mat.maps[MATERIAL_MAP_DIFFUSE].texture = atlas; Camera camera = { 0 }; @@ -95,7 +99,9 @@ int main() { BeginMode3D(camera); { - DrawMesh(chunk_mesh, mat, matrix); + for (int i = 0; i < 2; i++) { + DrawMesh(chunk_meshes[i], mat, MatrixTranslate(i * WIDTH, 0.0f, 0.0f)); + } DrawGrid(10, 10); } EndMode3D(); diff --git a/samcraft/samcraft.vcxproj b/samcraft/samcraft.vcxproj index d87cf34..140c664 100644 --- a/samcraft/samcraft.vcxproj +++ b/samcraft/samcraft.vcxproj @@ -76,6 +76,7 @@ true WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true + stdc11 Console @@ -90,6 +91,7 @@ true WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true + stdc11 Console @@ -104,6 +106,7 @@ true _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true + stdc11 Console @@ -118,6 +121,7 @@ true NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true + stdc11 Console diff --git a/samcraft/vector.h b/samcraft/vector.h index 92cf1df..67333a8 100644 --- a/samcraft/vector.h +++ b/samcraft/vector.h @@ -12,4 +12,6 @@ typedef struct { float y; } vector2; +#define vec3(x, y, z) (vector3){ x, y, z } + #endif \ No newline at end of file