diff --git a/.gitignore b/.gitignore index 90a5915..db98520 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ x64/ x86/ samcraft/x64 samcraft/x86 +.vs diff --git a/.vs/samcraft/FileContentIndex/4367ceff-bbb6-4ec7-a16d-b50ebe10ff8b.vsidx b/.vs/samcraft/FileContentIndex/4367ceff-bbb6-4ec7-a16d-b50ebe10ff8b.vsidx deleted file mode 100644 index 4aa34fd..0000000 Binary files a/.vs/samcraft/FileContentIndex/4367ceff-bbb6-4ec7-a16d-b50ebe10ff8b.vsidx and /dev/null differ diff --git a/.vs/samcraft/FileContentIndex/8f08e493-1894-4073-8af8-3a2469b2733c.vsidx b/.vs/samcraft/FileContentIndex/8f08e493-1894-4073-8af8-3a2469b2733c.vsidx deleted file mode 100644 index 8a3580d..0000000 Binary files a/.vs/samcraft/FileContentIndex/8f08e493-1894-4073-8af8-3a2469b2733c.vsidx and /dev/null differ diff --git a/.vs/samcraft/FileContentIndex/aa0fac50-7259-4596-b1d8-b0d17e415219.vsidx b/.vs/samcraft/FileContentIndex/aa0fac50-7259-4596-b1d8-b0d17e415219.vsidx deleted file mode 100644 index c37ce8d..0000000 Binary files a/.vs/samcraft/FileContentIndex/aa0fac50-7259-4596-b1d8-b0d17e415219.vsidx and /dev/null differ diff --git a/.vs/samcraft/FileContentIndex/df194b64-e803-47d4-8313-3c93f130ad8d.vsidx b/.vs/samcraft/FileContentIndex/df194b64-e803-47d4-8313-3c93f130ad8d.vsidx deleted file mode 100644 index 0970398..0000000 Binary files a/.vs/samcraft/FileContentIndex/df194b64-e803-47d4-8313-3c93f130ad8d.vsidx and /dev/null differ diff --git a/.vs/samcraft/v17/.suo b/.vs/samcraft/v17/.suo index aedf629..9db64b1 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 cdec6d6..205a924 100644 Binary files a/.vs/samcraft/v17/Browse.VC.db and b/.vs/samcraft/v17/Browse.VC.db differ diff --git a/.vs/samcraft/v17/ipch/AutoPCH/15b5053cee312fd9/MAIN.ipch b/.vs/samcraft/v17/ipch/AutoPCH/15b5053cee312fd9/MAIN.ipch index b18a12b..4b42752 100644 Binary files a/.vs/samcraft/v17/ipch/AutoPCH/15b5053cee312fd9/MAIN.ipch and b/.vs/samcraft/v17/ipch/AutoPCH/15b5053cee312fd9/MAIN.ipch differ diff --git a/.vs/samcraft/v17/ipch/AutoPCH/eb046027442056c7/CHUNK.ipch b/.vs/samcraft/v17/ipch/AutoPCH/eb046027442056c7/CHUNK.ipch index 2eb894e..9f26ceb 100644 Binary files a/.vs/samcraft/v17/ipch/AutoPCH/eb046027442056c7/CHUNK.ipch and b/.vs/samcraft/v17/ipch/AutoPCH/eb046027442056c7/CHUNK.ipch differ diff --git a/.vs/samcraft/v17/ipch/AutoPCH/fdcae648f8fe8f9b/MAIN.ipch b/.vs/samcraft/v17/ipch/AutoPCH/fdcae648f8fe8f9b/MAIN.ipch index 91e987d..2e57512 100644 Binary files a/.vs/samcraft/v17/ipch/AutoPCH/fdcae648f8fe8f9b/MAIN.ipch and b/.vs/samcraft/v17/ipch/AutoPCH/fdcae648f8fe8f9b/MAIN.ipch differ diff --git a/atlas.png b/atlas.png index 51e2fda..caf947c 100644 Binary files a/atlas.png and b/atlas.png differ diff --git a/samcraft/chunk.c b/samcraft/chunk.c index be81291..f6232db 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, 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)]; +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]; } diff --git a/samcraft/chunk.h b/samcraft/chunk.h index 46b2e39..270d9d9 100644 --- a/samcraft/chunk.h +++ b/samcraft/chunk.h @@ -3,11 +3,11 @@ #include "vector.h" -#define WIDTH 320 +#define WIDTH 1024 #define HEIGHT 256 vector3 index_to_coord(int i); int coord_to_index(vector3 pos); -int get_block_at(int* chunk, vector3 pos); +int get_block_at(int chunk[WIDTH][HEIGHT][WIDTH], int x, int y, int z); #endif diff --git a/samcraft/faces.c b/samcraft/faces.c index 77cf843..714d6e6 100644 --- a/samcraft/faces.c +++ b/samcraft/faces.c @@ -7,149 +7,149 @@ #define BOTTOM_RIGHT { 1.0f, 1.0f } void add_face(Mesh* mesh, vector3* face, vector2* tex, int block_id, vector3 pos) { - int vert_start = mesh->vertexCount * 3; - int tex_start = mesh->vertexCount * 2; + int vert_start = mesh->vertexCount * 3; + int tex_start = mesh->vertexCount * 2; mesh->vertexCount += 6; mesh->triangleCount += 2; mesh->vertices = realloc(mesh->vertices, (mesh->vertexCount * 3) * sizeof(float)); - mesh->texcoords = realloc(mesh->texcoords, (mesh->vertexCount * 2) * sizeof(float)); + mesh->texcoords = realloc(mesh->texcoords, (mesh->vertexCount * 2) * sizeof(float)); for (int i = 0; i < 18; i += 3) { - int v = i / 3; + int v = i / 3; mesh->vertices[vert_start + i + 0] = face[v].x + pos.x; mesh->vertices[vert_start + i + 1] = face[v].y + pos.y; - mesh->vertices[vert_start + i + 2] = face[v].z + pos.z; + mesh->vertices[vert_start + i + 2] = face[v].z + pos.z; } - for (int i = 0; i < 12; i += 2) { - int v = i / 2; + for (int i = 0; i < 12; i += 2) { + int v = i / 2; - int block_x = block_id % ATLAS_FIT; - int block_y = block_id / ATLAS_FIT; + int block_x = block_id % ATLAS_FIT; + int block_y = block_id / ATLAS_FIT; - mesh->texcoords[tex_start + i + 0] = ((block_x + tex[v].x) * TEXTURE_SIZE) / ATLAS_SIZE; - mesh->texcoords[tex_start + i + 1] = ((block_y + tex[v].y) * TEXTURE_SIZE) / ATLAS_SIZE; - } + mesh->texcoords[tex_start + i + 0] = ((block_x + tex[v].x) * TEXTURE_SIZE) / ATLAS_SIZE; + mesh->texcoords[tex_start + i + 1] = ((block_y + tex[v].y) * TEXTURE_SIZE) / ATLAS_SIZE; + } } vector3 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 }, - { 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 } }; vector2 bottom_tex[] = { - BOTTOM_LEFT, - BOTTOM_RIGHT, - TOP_LEFT, + BOTTOM_LEFT, + BOTTOM_RIGHT, + TOP_LEFT, - BOTTOM_RIGHT, - TOP_RIGHT, - TOP_LEFT + BOTTOM_RIGHT, + TOP_RIGHT, + TOP_LEFT }; vector3 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 }, - { 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 } }; vector2 top_tex[] = { - BOTTOM_LEFT, - TOP_LEFT, - BOTTOM_RIGHT, + BOTTOM_LEFT, + TOP_LEFT, + BOTTOM_RIGHT, - BOTTOM_RIGHT, - TOP_LEFT, - TOP_RIGHT + BOTTOM_RIGHT, + TOP_LEFT, + TOP_RIGHT }; vector3 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 }, - { 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 } }; vector2 front_tex[] = { - BOTTOM_LEFT, - BOTTOM_RIGHT, - TOP_LEFT, + BOTTOM_LEFT, + BOTTOM_RIGHT, + TOP_LEFT, - BOTTOM_RIGHT, - TOP_RIGHT, - TOP_LEFT + BOTTOM_RIGHT, + TOP_RIGHT, + TOP_LEFT }; vector3 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 }, - { 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 } }; vector2 back_tex[] = { - BOTTOM_LEFT, - TOP_LEFT, - BOTTOM_RIGHT, + BOTTOM_LEFT, + TOP_LEFT, + BOTTOM_RIGHT, - BOTTOM_RIGHT, - TOP_LEFT, - TOP_RIGHT + BOTTOM_RIGHT, + TOP_LEFT, + TOP_RIGHT }; vector3 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 }, - { 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 } }; vector2 right_tex[] = { - BOTTOM_RIGHT, - TOP_LEFT, - BOTTOM_LEFT, + BOTTOM_RIGHT, + TOP_LEFT, + BOTTOM_LEFT, - BOTTOM_RIGHT, - TOP_RIGHT, - TOP_LEFT + BOTTOM_RIGHT, + TOP_RIGHT, + TOP_LEFT }; vector3 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 }, - { -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 } }; vector2 left_tex[] = { - BOTTOM_LEFT, - BOTTOM_RIGHT, - TOP_RIGHT, - - BOTTOM_LEFT, - TOP_RIGHT, - TOP_LEFT + BOTTOM_LEFT, + BOTTOM_RIGHT, + TOP_RIGHT, + + BOTTOM_LEFT, + TOP_RIGHT, + TOP_LEFT }; diff --git a/samcraft/main.c b/samcraft/main.c index 46d8be2..ad941c5 100644 --- a/samcraft/main.c +++ b/samcraft/main.c @@ -7,36 +7,42 @@ #include "faces.h" #include "chunk.h" #include "vector.h" +#define STB_PERLIN_IMPLEMENTATION #include "stb_perlin.h" -Mesh gen_mesh(int* chunk) { +Mesh gen_mesh(int chunk[WIDTH][HEIGHT][WIDTH]) { Mesh mesh = { 0 }; mesh.vertices = malloc(0); mesh.vertexCount = 0; - mesh.texcoords = malloc(0); + mesh.texcoords = malloc(0); - for (int i = 0; i < WIDTH * WIDTH * HEIGHT; i++) { - int block_id = chunk[i]; - if(block_id == 0) continue; - vector3 pos = index_to_coord(i); + 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]; + if (block_id == 0) continue; + vector3 pos = { x, y, z }; + //vector3 pos = index_to_coord(i); - if(get_block_at(chunk, (vector3){ pos.x, pos.y - 1, pos.z }) == 0) - add_face(&mesh, bottom, bottom_tex, block_id, pos); + if (get_block_at(chunk, x, y - 1, z) == 0) + add_face(&mesh, bottom, bottom_tex, block_id, pos); - if(get_block_at(chunk, (vector3){ pos.x, pos.y + 1, pos.z }) == 0) - add_face(&mesh, top, top_tex, block_id, pos); + if (get_block_at(chunk, x, y + 1, z) == 0) + add_face(&mesh, top, top_tex, block_id == 1 ? 4 : block_id, pos); - if(get_block_at(chunk, (vector3){ pos.x - 1, pos.y, pos.z }) == 0) - add_face(&mesh, left, left_tex, block_id, pos); + if (get_block_at(chunk, x - 1, y, z) == 0) + add_face(&mesh, left, left_tex, block_id, pos); - if(get_block_at(chunk, (vector3){ pos.x + 1, pos.y, pos.z }) == 0) - add_face(&mesh, right, right_tex, block_id, pos); + if (get_block_at(chunk, x + 1, y, z) == 0) + add_face(&mesh, right, right_tex, block_id, pos); - if(get_block_at(chunk, (vector3){ pos.x, pos.y, pos.z - 1 }) == 0) - add_face(&mesh, back, back_tex, block_id, pos); + if (get_block_at(chunk, x, y, z - 1) == 0) + add_face(&mesh, back, back_tex, block_id, pos); - if(get_block_at(chunk, (vector3){ pos.x, pos.y, pos.z + 1 }) == 0) - add_face(&mesh, front, front_tex, block_id, pos); + if (get_block_at(chunk, x, y, z + 1) == 0) + add_face(&mesh, front, front_tex, block_id, pos); + } + } } UploadMesh(&mesh, false); @@ -50,27 +56,26 @@ int main() { InitWindow(800, 600, "samcraft"); //SetTargetFPS(60); + int (*chunk)[HEIGHT][WIDTH] = calloc(WIDTH * HEIGHT * WIDTH, sizeof(int)); - int* chunk = malloc(WIDTH * WIDTH * HEIGHT * sizeof(int)); - for(int i = 0; i < WIDTH * WIDTH * HEIGHT; i++) { - chunk[i] = 0; + 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; + } + } } - for(int x = 0; x < WIDTH; x++) { - for(int z = 0; z < WIDTH; z++) { - for(int y = 0; y < (10 + stb_perlin_fbm_noise3(x / 40.0f, x / 40.0f, z / 40.0f, 2.0f, 0.5f, 6) * 10.0f); y++) { - chunk[coord_to_index((vector3){ x, y, z })] = GetRandomValue(1, 2); - } - } - } - - //chunk[0] = 1; Mesh chunk_mesh = gen_mesh(chunk); Material mat = LoadMaterialDefault(); Matrix matrix = MatrixIdentity(); - Texture atlas = LoadTexture("atlas.png"); - mat.maps[MATERIAL_MAP_DIFFUSE].texture = atlas; + Texture atlas = LoadTexture("atlas.png"); + mat.maps[MATERIAL_MAP_DIFFUSE].texture = atlas; Camera camera = { 0 }; camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; @@ -86,7 +91,7 @@ int main() { BeginDrawing(); { - ClearBackground(BLACK); + ClearBackground(SKYBLUE); BeginMode3D(camera); { @@ -95,7 +100,7 @@ int main() { } EndMode3D(); - DrawFPS(10, 10); + DrawFPS(10, 10); } EndDrawing(); } diff --git a/samcraft/samcraft.vcxproj b/samcraft/samcraft.vcxproj index b4b6660..d87cf34 100644 --- a/samcraft/samcraft.vcxproj +++ b/samcraft/samcraft.vcxproj @@ -128,12 +128,14 @@ + - + + diff --git a/samcraft/samcraft.vcxproj.filters b/samcraft/samcraft.vcxproj.filters index 7fbe2ac..b1d5739 100644 --- a/samcraft/samcraft.vcxproj.filters +++ b/samcraft/samcraft.vcxproj.filters @@ -21,6 +21,9 @@ Source Files + + Source Files + @@ -29,7 +32,10 @@ Header Files - + + Header Files + + Header Files diff --git a/samcraft/samcraft.vcxproj.user b/samcraft/samcraft.vcxproj.user index 88a5509..0c53ecc 100644 --- a/samcraft/samcraft.vcxproj.user +++ b/samcraft/samcraft.vcxproj.user @@ -1,4 +1,19 @@  - + + $(SolutionDir) + WindowsLocalDebugger + + + $(SolutionDir) + WindowsLocalDebugger + + + $(SolutionDir) + WindowsLocalDebugger + + + $(SolutionDir) + WindowsLocalDebugger + \ No newline at end of file diff --git a/samcraft/vector.h b/samcraft/vector.h index 63a4183..92cf1df 100644 --- a/samcraft/vector.h +++ b/samcraft/vector.h @@ -12,4 +12,4 @@ typedef struct { float y; } vector2; -#endif +#endif \ No newline at end of file