Skip to content

Commit

Permalink
Added trees
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanNikkel committed Dec 26, 2019
1 parent 356b7c8 commit d294bf7
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 36 deletions.
2 changes: 2 additions & 0 deletions Voxel.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ xcopy "$(ProjectDir)shaders\*" "$(OutDir)shaders\" /s /r /y /q</Command>
<AdditionalIncludeDirectories>$(SolutionDir)\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<TreatWarningAsError>true</TreatWarningAsError>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down Expand Up @@ -172,6 +173,7 @@ xcopy "$(ProjectDir)shaders\*" "$(OutDir)shaders\" /s /r /y /q</Command>
<AdditionalIncludeDirectories>$(SolutionDir)\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<TreatWarningAsError>true</TreatWarningAsError>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down
Binary file removed resources/test_tileset.png
Binary file not shown.
Binary file modified resources/tileset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion src/Block.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct Block
BLOCK_GRASS,
BLOCK_DIRT,
BLOCK_STONE,
BLOCK_LOG,
BLOCK_LEAVES,

BLOCK_COUNT
};
Expand All @@ -40,6 +42,8 @@ namespace BlockData
0,
{ 2, 1, 0 },
0,
3
3,
{ 5, 4, 5 },
6,
};
}
34 changes: 31 additions & 3 deletions src/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ void Chunk::Generate(TerrainGenerator &gen)
{
glm::ivec3 chunk_pos = GetWorldPos();

treePoints_ = gen.GenerateTreePoints(GetCoord());

// Terrain
for (int z = 0; z < World::chunkSize; z++)
{
for (int x = 0; x < World::chunkSize; x++)
Expand All @@ -35,7 +34,36 @@ void Chunk::Generate(TerrainGenerator &gen)
}
}
}
//for (int i = 0; i < )

// Trees
glm::ivec3 treeSize = { _countof(*TerrainGenerator::tree), _countof(TerrainGenerator::tree), _countof(**TerrainGenerator::tree) };
glm::ivec2 treeRad = { (treeSize.x - 1) / 2, (treeSize.z - 1) / 2 };
glm::ivec2 chunkPos2d = { chunk_pos.x, chunk_pos.z };
std::vector<glm::ivec2> treePoints = gen.GenerateTreePoints(chunkPos2d - treeRad, chunkPos2d + glm::ivec2(World::chunkSize, World::chunkSize) + treeRad);

for (unsigned i = 0; i < treePoints.size(); i++)
{
for (int y = 0; y < treeSize.y; y++)
{
for (int x = -treeRad.x; x <= treeRad.x; x++)
{
for (int z = -treeRad.y; z <= treeRad.y; z++)
{
Block newBlock = { Block::BlockType(TerrainGenerator::tree[y][x + treeRad.x][z + treeRad.y]) };

if (newBlock.type != Block::BLOCK_AIR)
{
glm::ivec3 blockPos = { treePoints[i].x + x, gen.GetHeight(treePoints[i]) + y, treePoints[i].y + z };

const Block &oldBlock = GetBlock(blockPos);

if (!(newBlock.type == Block::BLOCK_LEAVES && oldBlock.type != Block::BLOCK_AIR))
SetBlock(blockPos, newBlock);
}
}
}
}
}
}

void Chunk::BuildMesh()
Expand Down
1 change: 0 additions & 1 deletion src/Chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class Chunk
float heightTimer_; // 0: down, 1: up
bool heightTimerIncreasing_;
int highestSolidBlock_; // Currently stores highest ever existed
std::vector<glm::ivec2> treePoints_;

// low to high: x, z, y
std::array<Block, World::chunkSize * World::chunkSize * World::chunkHeight> blocks_ = {};
Expand Down
2 changes: 1 addition & 1 deletion src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

Player::Player() : Entity(), camera_(GetPosition()), canJump_(false), noclip_(false)
{
Teleport(glm::vec3(519.0f, 159.0f, -315.5f));
Teleport(glm::vec3(520.5f, 170.0f, -320.5f));
}

void Player::Update(float dt)
Expand Down
2 changes: 0 additions & 2 deletions src/Stub.cpp

This file was deleted.

2 changes: 0 additions & 2 deletions src/Stub.h

This file was deleted.

126 changes: 104 additions & 22 deletions src/TerrainGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,99 @@

namespace Gen = World::Generation;

const int TerrainGenerator::tree[10][7][7] = {
{
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 4, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
},
{
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 4, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
},
{
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 4, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
},
{
{ 0, 0, 0, 5, 0, 0, 0 },
{ 0, 0, 5, 5, 5, 0, 0 },
{ 0, 5, 5, 5, 5, 5, 0 },
{ 5, 5, 5, 4, 5, 5, 5 },
{ 0, 5, 5, 5, 5, 5, 0 },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
{ 0, 0, 0, 5, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0 },
},
};

int TerrainGenerator::GetHeight(glm::vec2 pos)
{
glm::vec2 scaled = pos / Gen::terrainInterpGrid;
Expand Down Expand Up @@ -46,39 +139,28 @@ int TerrainGenerator::GetHeight(glm::vec2 pos)
return static_cast<int>(glm::lerp(minH, maxH, (pos.x - min.x) / (max.x - min.x)));
}

std::vector<glm::ivec2> TerrainGenerator::GenerateTreePoints(glm::ivec2 chunkCoord)
std::vector<glm::ivec2> TerrainGenerator::GenerateTreePoints(glm::ivec2 startCorner, glm::ivec2 endCorner)
{
std::vector<glm::ivec2> points;
std::default_random_engine rng(std::hash<glm::ivec2>()(chunkCoord));
std::uniform_int_distribution<int> dist(0, World::chunkSize - 1);
std::hash<glm::ivec2> hash;
std::uniform_real_distribution<float> dist(0.0f, 1.0f);

unsigned attempts = 0;

while (attempts <= 10)
for (int z = startCorner.y; z < endCorner.y; z++)
{
glm::ivec2 point = { dist(rng), dist(rng) };

bool withinDist = false;
for (unsigned i = 0; i < points.size(); i++)
for (int x = startCorner.x; x < endCorner.x; x++)
{
glm::ivec2 dif = point - points[i];
if (dif.x * dif.x + dif.y * dif.y < Gen::minTreeDistance * Gen::minTreeDistance)
glm::ivec2 pos = { x, z };
std::default_random_engine rng(hash(pos));

if (dist(rng) <= Gen::treeDensity)
{
withinDist = true;
break;
points.push_back(pos);
}
}

if (withinDist)
{
attempts++;
}
else
{
attempts = 0;
points.push_back(point);
}
}

return points;
}

Expand Down
8 changes: 7 additions & 1 deletion src/TerrainGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ class TerrainGenerator
public:
int GetHeight(glm::vec2 pos);

std::vector<glm::ivec2> GenerateTreePoints(glm::ivec2 chunkCoord);
// [start, end)
std::vector<glm::ivec2> GenerateTreePoints(glm::ivec2 startCorner, glm::ivec2 endCorner);

// This should be deserialized
// y, x, z
static const int tree[10][7][7];

private:
static const unsigned cacheCapacity = 128;


std::array<HeightCache, cacheCapacity> cache_;
unsigned cacheSize_ = 0;

Expand Down
6 changes: 3 additions & 3 deletions src/WorldConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ namespace World
// Mountain noise
const float heightScale = 128.0f;
const float heightWeight = 0.8f;
const unsigned heightMaxHeight = 255;
const unsigned heightMaxHeight = 250;

// Hill noise
const float detailScale = 16.0f;
const unsigned detailMaxHeight = 63;
const unsigned detailMaxHeight = 100;

// Trees
const unsigned minTreeDistance = 4;
const float treeDensity = 0.03f;

// Interpolation grid size
const float terrainInterpGrid = 4.0f;
Expand Down

0 comments on commit d294bf7

Please sign in to comment.