change to 3d array

This commit is contained in:
sam 2024-04-12 15:44:04 +12:00
parent 07038da071
commit 2b23476c40
19 changed files with 157 additions and 128 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ x64/
x86/
samcraft/x64
samcraft/x86
.vs

Binary file not shown.

Binary file not shown.

BIN
atlas.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.4 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

View file

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

View file

@ -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

View file

@ -7,37 +7,43 @@
#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);
for (int i = 0; i < WIDTH * WIDTH * HEIGHT; i++) {
int block_id = chunk[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 = index_to_coord(i);
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)
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)
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)
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)
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)
if (get_block_at(chunk, x, y, z + 1) == 0)
add_face(&mesh, front, front_tex, block_id, pos);
}
}
}
UploadMesh(&mesh, false);
@ -50,21 +56,20 @@ 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++) {
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);
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[0] = 1;
Mesh chunk_mesh = gen_mesh(chunk);
Material mat = LoadMaterialDefault();
Matrix matrix = MatrixIdentity();
@ -86,7 +91,7 @@ int main() {
BeginDrawing();
{
ClearBackground(BLACK);
ClearBackground(SKYBLUE);
BeginMode3D(camera);
{

View file

@ -128,12 +128,14 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="chunk.c" />
<ClCompile Include="faces.c" />
<ClCompile Include="main.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="chunk.h" />
<ClInclude Include="faces.h" />
<ClInclude Include="vector3.h" />
<ClInclude Include="stb_perlin.h" />
<ClInclude Include="vector.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View file

@ -21,6 +21,9 @@
<ClCompile Include="chunk.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="faces.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="faces.h">
@ -29,7 +32,10 @@
<ClInclude Include="chunk.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="vector3.h">
<ClInclude Include="vector.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="stb_perlin.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>

View file

@ -1,4 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerWorkingDirectory>$(SolutionDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LocalDebuggerWorkingDirectory>$(SolutionDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerWorkingDirectory>$(SolutionDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LocalDebuggerWorkingDirectory>$(SolutionDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>