add chunking system
This commit is contained in:
parent
2b23476c40
commit
c8e5e0e394
Binary file not shown.
Binary file not shown.
|
@ -14,7 +14,7 @@ int coord_to_index(vector3 coord) {
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_block_at(int chunk[WIDTH][HEIGHT][WIDTH], int x, int y, int z) {
|
int get_block_at(Chunk chunk, vector3 pos) {
|
||||||
if (x < 0 || x >= WIDTH || z < 0 || z >= WIDTH || y < 0 || y >= HEIGHT) return 0;
|
if (pos.x < 0 || pos.x >= WIDTH || pos.z < 0 || pos.z >= WIDTH || pos.y < 0 || pos.y >= HEIGHT) return 0;
|
||||||
return chunk[x][y][z];
|
return chunk[coord_to_index(pos)];
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,12 +2,15 @@
|
||||||
#define __CHUNK_H__
|
#define __CHUNK_H__
|
||||||
|
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
#define WIDTH 1024
|
#define WIDTH 16
|
||||||
#define HEIGHT 256
|
#define HEIGHT 256
|
||||||
|
|
||||||
|
typedef uint8_t Chunk[HEIGHT * WIDTH * WIDTH];
|
||||||
|
|
||||||
vector3 index_to_coord(int i);
|
vector3 index_to_coord(int i);
|
||||||
int coord_to_index(vector3 pos);
|
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
|
#endif
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <threads.h>
|
||||||
|
|
||||||
#include "faces.h"
|
#include "faces.h"
|
||||||
#include "chunk.h"
|
#include "chunk.h"
|
||||||
|
@ -10,7 +11,7 @@
|
||||||
#define STB_PERLIN_IMPLEMENTATION
|
#define STB_PERLIN_IMPLEMENTATION
|
||||||
#include "stb_perlin.h"
|
#include "stb_perlin.h"
|
||||||
|
|
||||||
Mesh gen_mesh(int chunk[WIDTH][HEIGHT][WIDTH]) {
|
Mesh gen_mesh(Chunk chunk) {
|
||||||
Mesh mesh = { 0 };
|
Mesh mesh = { 0 };
|
||||||
mesh.vertices = malloc(0);
|
mesh.vertices = malloc(0);
|
||||||
mesh.vertexCount = 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 x = 0; x < WIDTH; x++) {
|
||||||
for (int y = 0; y < HEIGHT; y++) {
|
for (int y = 0; y < HEIGHT; y++) {
|
||||||
for (int z = 0; z < WIDTH; z++) {
|
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;
|
if (block_id == 0) continue;
|
||||||
vector3 pos = { x, y, z };
|
|
||||||
//vector3 pos = index_to_coord(i);
|
if (get_block_at(chunk, vec3(x, y - 1, z)) == 0)
|
||||||
|
|
||||||
if (get_block_at(chunk, x, y - 1, z) == 0)
|
|
||||||
add_face(&mesh, bottom, bottom_tex, block_id, pos);
|
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);
|
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);
|
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);
|
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);
|
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);
|
add_face(&mesh, front, front_tex, block_id, pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,25 +56,29 @@ int main() {
|
||||||
InitWindow(800, 600, "samcraft");
|
InitWindow(800, 600, "samcraft");
|
||||||
|
|
||||||
//SetTargetFPS(60);
|
//SetTargetFPS(60);
|
||||||
int (*chunk)[HEIGHT][WIDTH] = calloc(WIDTH * HEIGHT * WIDTH, sizeof(int));
|
|
||||||
|
|
||||||
for (int x = 0; x < WIDTH; x++) {
|
Chunk* chunks = calloc(2, sizeof(Chunk));
|
||||||
for (int z = 0; z < WIDTH; z++) {
|
Mesh* chunk_meshes = calloc(2, sizeof(Mesh));
|
||||||
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++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
int id = 2;
|
for (int x = 0; x < WIDTH; x++) {
|
||||||
if (height - y == 1) id = 1;
|
for (int z = 0; z < WIDTH; z++) {
|
||||||
if (height - y == 2) id = 3;
|
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);
|
||||||
chunk[x][y][z] = id;
|
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");
|
Texture atlas = LoadTexture("atlas.png");
|
||||||
|
Material mat = LoadMaterialDefault();
|
||||||
mat.maps[MATERIAL_MAP_DIFFUSE].texture = atlas;
|
mat.maps[MATERIAL_MAP_DIFFUSE].texture = atlas;
|
||||||
|
|
||||||
Camera camera = { 0 };
|
Camera camera = { 0 };
|
||||||
|
@ -95,7 +99,9 @@ int main() {
|
||||||
|
|
||||||
BeginMode3D(camera);
|
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);
|
DrawGrid(10, 10);
|
||||||
}
|
}
|
||||||
EndMode3D();
|
EndMode3D();
|
||||||
|
|
|
@ -76,6 +76,7 @@
|
||||||
<SDLCheck>true</SDLCheck>
|
<SDLCheck>true</SDLCheck>
|
||||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
<LanguageStandard_C>stdc11</LanguageStandard_C>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
|
@ -90,6 +91,7 @@
|
||||||
<SDLCheck>true</SDLCheck>
|
<SDLCheck>true</SDLCheck>
|
||||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
<LanguageStandard_C>stdc11</LanguageStandard_C>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
|
@ -104,6 +106,7 @@
|
||||||
<SDLCheck>true</SDLCheck>
|
<SDLCheck>true</SDLCheck>
|
||||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
<LanguageStandard_C>stdc11</LanguageStandard_C>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
|
@ -118,6 +121,7 @@
|
||||||
<SDLCheck>true</SDLCheck>
|
<SDLCheck>true</SDLCheck>
|
||||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
<LanguageStandard_C>stdc11</LanguageStandard_C>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
|
|
|
@ -12,4 +12,6 @@ typedef struct {
|
||||||
float y;
|
float y;
|
||||||
} vector2;
|
} vector2;
|
||||||
|
|
||||||
|
#define vec3(x, y, z) (vector3){ x, y, z }
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in a new issue