add chunking system

This commit is contained in:
sam 2024-04-21 10:37:37 +12:00
parent 2b23476c40
commit c8e5e0e394
7 changed files with 45 additions and 30 deletions

Binary file not shown.

Binary file not shown.

View file

@ -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)];
}

View file

@ -2,12 +2,15 @@
#define __CHUNK_H__
#include "vector.h"
#include <stdint.h>
#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

View file

@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <threads.h>
#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();

View file

@ -76,6 +76,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard_C>stdc11</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@ -90,6 +91,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard_C>stdc11</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@ -104,6 +106,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard_C>stdc11</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@ -118,6 +121,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard_C>stdc11</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>

View file

@ -12,4 +12,6 @@ typedef struct {
float y;
} vector2;
#define vec3(x, y, z) (vector3){ x, y, z }
#endif