change to 3d array
This commit is contained in:
parent
07038da071
commit
2b23476c40
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,3 +2,4 @@ x64/
|
||||||
x86/
|
x86/
|
||||||
samcraft/x64
|
samcraft/x64
|
||||||
samcraft/x86
|
samcraft/x86
|
||||||
|
.vs
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
atlas.png
BIN
atlas.png
Binary file not shown.
Before Width: | Height: | Size: 3.4 KiB After Width: | Height: | Size: 4.8 KiB |
|
@ -14,7 +14,7 @@ int coord_to_index(vector3 coord) {
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_block_at(int* chunk, vector3 pos) {
|
int get_block_at(int chunk[WIDTH][HEIGHT][WIDTH], int x, int y, int z) {
|
||||||
if (pos.x < 0 || pos.x >= WIDTH || pos.z < 0 || pos.z >= WIDTH || pos.y < 0 || pos.y >= HEIGHT) return 0;
|
if (x < 0 || x >= WIDTH || z < 0 || z >= WIDTH || y < 0 || y >= HEIGHT) return 0;
|
||||||
return chunk[coord_to_index(pos)];
|
return chunk[x][y][z];
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,11 @@
|
||||||
|
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
|
|
||||||
#define WIDTH 320
|
#define WIDTH 1024
|
||||||
#define HEIGHT 256
|
#define HEIGHT 256
|
||||||
|
|
||||||
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, vector3 pos);
|
int get_block_at(int chunk[WIDTH][HEIGHT][WIDTH], int x, int y, int z);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -7,37 +7,43 @@
|
||||||
#include "faces.h"
|
#include "faces.h"
|
||||||
#include "chunk.h"
|
#include "chunk.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
|
#define STB_PERLIN_IMPLEMENTATION
|
||||||
#include "stb_perlin.h"
|
#include "stb_perlin.h"
|
||||||
|
|
||||||
Mesh gen_mesh(int* chunk) {
|
Mesh gen_mesh(int chunk[WIDTH][HEIGHT][WIDTH]) {
|
||||||
Mesh mesh = { 0 };
|
Mesh mesh = { 0 };
|
||||||
mesh.vertices = malloc(0);
|
mesh.vertices = malloc(0);
|
||||||
mesh.vertexCount = 0;
|
mesh.vertexCount = 0;
|
||||||
mesh.texcoords = malloc(0);
|
mesh.texcoords = malloc(0);
|
||||||
|
|
||||||
for (int i = 0; i < WIDTH * WIDTH * HEIGHT; i++) {
|
for(int x = 0; x < WIDTH; x++) {
|
||||||
int block_id = chunk[i];
|
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;
|
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);
|
add_face(&mesh, bottom, bottom_tex, block_id, pos);
|
||||||
|
|
||||||
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, top, top_tex, block_id, pos);
|
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);
|
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);
|
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);
|
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);
|
add_face(&mesh, front, front_tex, block_id, pos);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
UploadMesh(&mesh, false);
|
UploadMesh(&mesh, false);
|
||||||
|
|
||||||
|
@ -50,21 +56,20 @@ int main() {
|
||||||
InitWindow(800, 600, "samcraft");
|
InitWindow(800, 600, "samcraft");
|
||||||
|
|
||||||
//SetTargetFPS(60);
|
//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 x = 0; x < WIDTH; x++) {
|
||||||
for (int z = 0; z < WIDTH; z++) {
|
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++) {
|
int height = (90 + stb_perlin_fbm_noise3(x / 40.0f, x / 40.0f, z / 40.0f, 2.0f, 0.5f, 6) * 10.0f);
|
||||||
chunk[coord_to_index((vector3){ x, y, z })] = GetRandomValue(1, 2);
|
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);
|
Mesh chunk_mesh = gen_mesh(chunk);
|
||||||
Material mat = LoadMaterialDefault();
|
Material mat = LoadMaterialDefault();
|
||||||
Matrix matrix = MatrixIdentity();
|
Matrix matrix = MatrixIdentity();
|
||||||
|
@ -86,7 +91,7 @@ int main() {
|
||||||
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
{
|
{
|
||||||
ClearBackground(BLACK);
|
ClearBackground(SKYBLUE);
|
||||||
|
|
||||||
BeginMode3D(camera);
|
BeginMode3D(camera);
|
||||||
{
|
{
|
||||||
|
|
|
@ -128,12 +128,14 @@
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="chunk.c" />
|
<ClCompile Include="chunk.c" />
|
||||||
|
<ClCompile Include="faces.c" />
|
||||||
<ClCompile Include="main.c" />
|
<ClCompile Include="main.c" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="chunk.h" />
|
<ClInclude Include="chunk.h" />
|
||||||
<ClInclude Include="faces.h" />
|
<ClInclude Include="faces.h" />
|
||||||
<ClInclude Include="vector3.h" />
|
<ClInclude Include="stb_perlin.h" />
|
||||||
|
<ClInclude Include="vector.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
|
|
@ -21,6 +21,9 @@
|
||||||
<ClCompile Include="chunk.c">
|
<ClCompile Include="chunk.c">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="faces.c">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="faces.h">
|
<ClInclude Include="faces.h">
|
||||||
|
@ -29,7 +32,10 @@
|
||||||
<ClInclude Include="chunk.h">
|
<ClInclude Include="chunk.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="vector3.h">
|
<ClInclude Include="vector.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="stb_perlin.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
@ -1,4 +1,19 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
<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>
|
</Project>
|
Loading…
Reference in a new issue