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,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
};

View file

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

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>

View file

@ -12,4 +12,4 @@ typedef struct {
float y;
} vector2;
#endif
#endif