Episode 3

This commit is contained in:
WSAL Evan 2024-10-04 13:53:11 -04:00
parent 749347981f
commit 5d993ce1b7
21 changed files with 612 additions and 284 deletions

View File

@ -158,6 +158,7 @@
<ClCompile Include="src\NoiseSettings.cpp" /> <ClCompile Include="src\NoiseSettings.cpp" />
<ClCompile Include="src\Planet.cpp" /> <ClCompile Include="src\Planet.cpp" />
<ClCompile Include="src\Shader.cpp" /> <ClCompile Include="src\Shader.cpp" />
<ClCompile Include="src\SurfaceFeature.cpp" />
<ClCompile Include="src\TupleHash.h" /> <ClCompile Include="src\TupleHash.h" />
<ClCompile Include="src\WorldGen.cpp" /> <ClCompile Include="src\WorldGen.cpp" />
<ClCompile Include="src\WorldGen.h" /> <ClCompile Include="src\WorldGen.h" />
@ -178,6 +179,7 @@
<ClInclude Include="src\NoiseSettings.h" /> <ClInclude Include="src\NoiseSettings.h" />
<ClInclude Include="src\Planet.h" /> <ClInclude Include="src\Planet.h" />
<ClInclude Include="src\Shader.h" /> <ClInclude Include="src\Shader.h" />
<ClInclude Include="src\SurfaceFeature.h" />
<ClInclude Include="vendor\imgui\imconfig.h" /> <ClInclude Include="vendor\imgui\imconfig.h" />
<ClInclude Include="vendor\imgui\imgui.h" /> <ClInclude Include="vendor\imgui\imgui.h" />
<ClInclude Include="vendor\imgui\imgui_impl_glfw.h" /> <ClInclude Include="vendor\imgui\imgui_impl_glfw.h" />
@ -196,8 +198,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Image Include="assets\sprites\block_map.png" /> <Image Include="assets\sprites\block_map.png" />
<Image Include="assets\sprites\direction_test.png" />
<Image Include="assets\sprites\grass_block_side.png" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Text Include="vendor\imgui\LICENSE.txt" /> <Text Include="vendor\imgui\LICENSE.txt" />

View File

@ -69,6 +69,9 @@
<ClCompile Include="src\NoiseSettings.cpp"> <ClCompile Include="src\NoiseSettings.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\SurfaceFeature.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\Shader.h"> <ClInclude Include="src\Shader.h">
@ -125,18 +128,15 @@
<ClInclude Include="src\NoiseSettings.h"> <ClInclude Include="src\NoiseSettings.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\SurfaceFeature.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="assets\shaders\vertex_shader.glsl" /> <None Include="assets\shaders\vertex_shader.glsl" />
<None Include="assets\shaders\fragment_shader.glsl" /> <None Include="assets\shaders\fragment_shader.glsl" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Image Include="assets\sprites\grass_block_side.png">
<Filter>Resource Files</Filter>
</Image>
<Image Include="assets\sprites\direction_test.png">
<Filter>Resource Files</Filter>
</Image>
<Image Include="assets\sprites\block_map.png"> <Image Include="assets\sprites\block_map.png">
<Filter>Resource Files</Filter> <Filter>Resource Files</Filter>
</Image> </Image>

View File

@ -19,5 +19,8 @@ void main()
vec4 result = vec4(ambient + diffuse, 1.0); vec4 result = vec4(ambient + diffuse, 1.0);
FragColor = texture(tex, TexCoord) * result; vec4 texResult = texture(tex, TexCoord);
if (texResult.a == 0)
discard;
FragColor = texResult * result;
} }

View File

@ -15,13 +15,14 @@ uniform mat4 view;
uniform mat4 projection; uniform mat4 projection;
// Array of possible normals based on direction // Array of possible normals based on direction
const vec3 normals[6] = vec3[6]( const vec3 normals[] = vec3[](
vec3(0, 0, 1), // 0 vec3( 0, 0, 1), // 0
vec3(0, 0, -1), // 1 vec3( 0, 0, -1), // 1
vec3(1, 0, 0), // 2 vec3( 1, 0, 0), // 2
vec3(-1, 0, 0), // 3 vec3(-1, 0, 0), // 3
vec3(0, 1, 0), // 4 vec3( 0, 1, 0), // 4
vec3(0, -1, 0) // 5 vec3( 0, -1, 0), // 5
vec3( 0, -1, 0) // 6
); );
void main() void main()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 672 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 672 B

View File

@ -1,80 +0,0 @@
[Window][Debug##Default]
Pos=60,60
Size=400,400
[Window][Test]
Pos=22,22
Size=543,95
[Window][Dear ImGui Demo]
Pos=650,20
Size=550,680
[Table][0xC9935533,3]
Column 0 Weight=1.0000
Column 1 Weight=1.0000
Column 2 Weight=1.0000
[Table][0x64418101,3]
RefScale=13
Column 0 Width=63
Column 1 Width=63
Column 2 Width=63
[Table][0x47600645,3]
RefScale=13
Column 0 Width=63
Column 1 Width=63
Column 2 Weight=1.0000
[Table][0xDE6957FF,6]
RefScale=13
Column 0 Width=63
Column 1 Width=63
Column 2 Width=-1
Column 3 Weight=1.0000
Column 4 Weight=1.0000
Column 5 Weight=-1.0000
[Table][0x861D378E,3]
Column 0 Weight=1.0000
Column 1 Weight=1.0000
Column 2 Weight=1.0000
[Table][0x1F146634,3]
RefScale=13
Column 0 Width=63
Column 1 Width=63
Column 2 Width=63
[Table][0x8DFA6E86,2]
Column 0 Weight=1.0000
Column 1 Weight=1.0000
[Table][0xFABAAEF7,2]
Column 0 Weight=1.0000
Column 1 Weight=1.0000
[Table][0xA43C3885,3]
RefScale=13
Column 0 Width=56
Column 1 Width=56
Column 2 Width=56
[Table][0x49F8DCEA,3]
RefScale=13
Column 0 Weight=1.0000
Column 1 Width=84
Column 2 Width=126
[Table][0x82CBB907,3]
Column 0 Weight=1.0000
Column 1 Weight=1.0000
Column 2 Weight=1.0000
[Table][0x49D11DC0,3]
RefScale=13
Column 0 Width=86
Column 1 Width=86
Column 2 Width=86

View File

@ -88,7 +88,7 @@ int main()
Shader shader("assets/shaders/vertex_shader.glsl", "assets/shaders/fragment_shader.glsl"); Shader shader("assets/shaders/vertex_shader.glsl", "assets/shaders/fragment_shader.glsl");
shader.use(); shader.use();
shader.setFloat("texMultiplier", .5f); shader.setFloat("texMultiplier", .25f);
// Create texture // Create texture
unsigned int texture; unsigned int texture;
@ -181,13 +181,7 @@ int main()
unsigned int modelLoc = glGetUniformLocation(shader.ID, "model"); unsigned int modelLoc = glGetUniformLocation(shader.ID, "model");
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
Planet::planet->Update(camera.Position.x, camera.Position.y, camera.Position.z, modelLoc); Planet::planet->Update(camera.Position.x, camera.Position.y, camera.Position.z, modelLoc);
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << "Planet Update: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count()
<< "[ms]\n";
// Draw ImGui UI // Draw ImGui UI
ImGui::Begin("Test"); ImGui::Begin("Test");
@ -237,7 +231,12 @@ void processInput(GLFWwindow* window)
escapeDown = false; escapeDown = false;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime); {
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD_NO_Y, deltaTime);
else
camera.ProcessKeyboard(FORWARD, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime); camera.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)

View File

@ -1,6 +1,7 @@
#include "Block.h" #include "Block.h"
Block::Block(char minX, char minY, char maxX, char maxY) Block::Block(char minX, char minY, char maxX, char maxY, bool transparent, bool billboard)
: transparent(transparent), billboard(billboard)
{ {
topMinX = minX; topMinX = minX;
topMinY = minY; topMinY = minY;
@ -20,7 +21,8 @@ Block::Block(char minX, char minY, char maxX, char maxY)
Block::Block(char topMinX, char topMinY, char topMaxX, char topMaxY, Block::Block(char topMinX, char topMinY, char topMaxX, char topMaxY,
char bottomMinX, char bottomMinY, char bottomMaxX, char bottomMaxY, char bottomMinX, char bottomMinY, char bottomMaxX, char bottomMaxY,
char sideMinX, char sideMinY, char sideMaxX, char sideMaxY) char sideMinX, char sideMinY, char sideMaxX, char sideMaxY, bool transparent, bool billboard)
: transparent(transparent), billboard(billboard)
{ {
this->topMinX = topMinX; this->topMinX = topMinX;
this->topMinY = topMinY; this->topMinY = topMinY;

View File

@ -6,9 +6,11 @@ public:
char topMinX, topMinY, topMaxX, topMaxY; char topMinX, topMinY, topMaxX, topMaxY;
char bottomMinX, bottomMinY, bottomMaxX, bottomMaxY; char bottomMinX, bottomMinY, bottomMaxX, bottomMaxY;
char sideMinX, sideMinY, sideMaxX, sideMaxY; char sideMinX, sideMinY, sideMaxX, sideMaxY;
bool transparent;
bool billboard;
Block(char minX, char minY, char maxX, char maxY); Block(char minX, char minY, char maxX, char maxY, bool transparent = false, bool billboard = false);
Block(char topMinX, char topMinY, char topMaxX, char topMaxY, Block(char topMinX, char topMinY, char topMaxX, char topMaxY,
char bottomMinX, char bottomMinY, char bottomMaxX, char bottomMaxY, char bottomMinX, char bottomMinY, char bottomMaxX, char bottomMaxY,
char sideMinX, char sideMinY, char sideMaxX, char sideMaxY); char sideMinX, char sideMinY, char sideMaxX, char sideMaxY, bool transparent = false, bool billboard = false);
}; };

View File

@ -7,14 +7,27 @@
namespace Blocks namespace Blocks
{ {
const std::vector<Block> blocks{ const std::vector<Block> blocks{
Block(0, 0, 0, 0), // Air block Block(0, 0, 0, 0, true), // Air block
Block(0, 0, 1, 1), // Dirt block Block(0, 0, 1, 1), // Dirt block
Block(1, 1, 2, 2, // Grass block Block(1, 1, 2, 2, // Grass block
0, 0, 1, 1, 0, 0, 1, 1,
1, 0, 2, 1), 1, 0, 2, 1),
Block(0, 1, 1, 2) // Stone block Block(0, 1, 1, 2), // Stone block
Block(2, 1, 3, 2, // Log
2, 1, 3, 2,
2, 0, 3, 1),
Block(0, 2, 1, 3, true), // Leaves
Block(1, 2, 2, 3, true, true), // Grass
Block(3, 0, 4, 1, true, true), // Tall Grass Bottom
Block(3, 1, 4, 2, true, true), // Tall Grass Top
Block(0, 3, 1, 4, true, true), // Poppy
Block(2, 2, 3, 3, true, true), // White Tulip
Block(3, 2, 4, 3, true, true), // Pink Tulip
Block(1, 3, 2, 4, true, true), // Orange Tulip
}; };
enum BLOCKS enum BLOCKS
@ -22,6 +35,15 @@ namespace Blocks
AIR = 0, AIR = 0,
DIRT_BLOCK = 1, DIRT_BLOCK = 1,
GRASS_BLOCK = 2, GRASS_BLOCK = 2,
STONE_BLOCK = 3 STONE_BLOCK = 3,
LOG = 4,
LEAVES = 5,
GRASS = 6,
TALL_GRASS_BOTTOM = 7,
TALL_GRASS_TOP = 8,
POPPY = 9,
WHITE_TULIP = 10,
PINK_TULIP = 11,
ORANGE_TULIP = 12,
}; };
} }

View File

@ -41,6 +41,13 @@ void Camera::ProcessKeyboard(Camera_Movement direction, float deltaTime)
Position += Up * velocity; Position += Up * velocity;
if (direction == DOWN) if (direction == DOWN)
Position -= Up * velocity; Position -= Up * velocity;
if (direction == FORWARD_NO_Y)
{
glm::vec3 moveDir = Front;
moveDir.y = 0;
moveDir = glm::normalize(moveDir);
Position += moveDir * velocity;
}
} }
// processes input received from a mouse input system. Expects the offset value in both the x and y direction. // processes input received from a mouse input system. Expects the offset value in both the x and y direction.

View File

@ -9,7 +9,8 @@ enum Camera_Movement {
LEFT, LEFT,
RIGHT, RIGHT,
UP, UP,
DOWN DOWN,
FORWARD_NO_Y
}; };
// Default camera values // Default camera values

View File

@ -60,184 +60,258 @@ void Chunk::GenerateChunk()
const Block* block = &Blocks::blocks[chunkData[index]]; const Block* block = &Blocks::blocks[chunkData[index]];
// North if (block->billboard)
{ {
int northBlock; vertices.push_back(Vertex(x + .85355f, y + 0, z + .85355f, block->sideMinX, block->sideMinY, 6));
if (z > 0) vertices.push_back(Vertex(x + .14645f, y + 0, z + .14645f, block->sideMaxX, block->sideMinY, 6));
{ vertices.push_back(Vertex(x + .85355f, y + 1, z + .85355f, block->sideMinX, block->sideMaxY, 6));
int northIndex = x * chunkSize * chunkSize + (z - 1) * chunkSize + y; vertices.push_back(Vertex(x + .14645f, y + 1, z + .14645f, block->sideMaxX, block->sideMaxY, 6));
northBlock = chunkData[northIndex];
}
else
{
int northIndex = x * chunkSize * chunkSize + (chunkSize - 1) * chunkSize + y;
northBlock = northData[northIndex];
}
if (northBlock == 0) indices.push_back(currentVertex + 0);
{ indices.push_back(currentVertex + 3);
vertices.push_back(Vertex(x + 1, y + 0, z + 0, block->sideMinX, block->sideMinY, 0)); indices.push_back(currentVertex + 1);
vertices.push_back(Vertex(x + 0, y + 0, z + 0, block->sideMaxX, block->sideMinY, 0)); indices.push_back(currentVertex + 0);
vertices.push_back(Vertex(x + 1, y + 1, z + 0, block->sideMinX, block->sideMaxY, 0)); indices.push_back(currentVertex + 2);
vertices.push_back(Vertex(x + 0, y + 1, z + 0, block->sideMaxX, block->sideMaxY, 0)); indices.push_back(currentVertex + 3);
currentVertex += 4;
indices.push_back(currentVertex + 0); vertices.push_back(Vertex(x + .14645f, y + 0, z + .14645f, block->sideMinX, block->sideMinY, 6));
indices.push_back(currentVertex + 3); vertices.push_back(Vertex(x + .85355f, y + 0, z + .85355f, block->sideMaxX, block->sideMinY, 6));
indices.push_back(currentVertex + 1); vertices.push_back(Vertex(x + .14645f, y + 1, z + .14645f, block->sideMinX, block->sideMaxY, 6));
indices.push_back(currentVertex + 0); vertices.push_back(Vertex(x + .85355f, y + 1, z + .85355f, block->sideMaxX, block->sideMaxY, 6));
indices.push_back(currentVertex + 2);
indices.push_back(currentVertex + 3); indices.push_back(currentVertex + 0);
currentVertex += 4; indices.push_back(currentVertex + 3);
} indices.push_back(currentVertex + 1);
indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 2);
indices.push_back(currentVertex + 3);
currentVertex += 4;
vertices.push_back(Vertex(x + .14645f, y + 0, z + .85355f, block->sideMinX, block->sideMinY, 6));
vertices.push_back(Vertex(x + .85355f, y + 0, z + .14645f, block->sideMaxX, block->sideMinY, 6));
vertices.push_back(Vertex(x + .14645f, y + 1, z + .85355f, block->sideMinX, block->sideMaxY, 6));
vertices.push_back(Vertex(x + .85355f, y + 1, z + .14645f, block->sideMaxX, block->sideMaxY, 6));
indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 3);
indices.push_back(currentVertex + 1);
indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 2);
indices.push_back(currentVertex + 3);
currentVertex += 4;
vertices.push_back(Vertex(x + .85355f, y + 0, z + .14645f, block->sideMinX, block->sideMinY, 6));
vertices.push_back(Vertex(x + .14645f, y + 0, z + .85355f, block->sideMaxX, block->sideMinY, 6));
vertices.push_back(Vertex(x + .85355f, y + 1, z + .14645f, block->sideMinX, block->sideMaxY, 6));
vertices.push_back(Vertex(x + .14645f, y + 1, z + .85355f, block->sideMaxX, block->sideMaxY, 6));
indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 3);
indices.push_back(currentVertex + 1);
indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 2);
indices.push_back(currentVertex + 3);
currentVertex += 4;
} }
else
// South
{ {
int southBlock; // North
if (z < chunkSize - 1)
{ {
int southIndex = x * chunkSize * chunkSize + (z + 1) * chunkSize + y; int northBlock;
southBlock = chunkData[southIndex]; if (z > 0)
} {
else int northIndex = x * chunkSize * chunkSize + (z - 1) * chunkSize + y;
{ northBlock = chunkData[northIndex];
int southIndex = x * chunkSize * chunkSize + 0 * chunkSize + y; }
southBlock = southData[southIndex]; else
} {
if (southBlock == 0) int northIndex = x * chunkSize * chunkSize + (chunkSize - 1) * chunkSize + y;
{ northBlock = northData[northIndex];
vertices.push_back(Vertex(x + 0, y + 0, z + 1, block->sideMinX, block->sideMinY, 1)); }
vertices.push_back(Vertex(x + 1, y + 0, z + 1, block->sideMaxX, block->sideMinY, 1));
vertices.push_back(Vertex(x + 0, y + 1, z + 1, block->sideMinX, block->sideMaxY, 1));
vertices.push_back(Vertex(x + 1, y + 1, z + 1, block->sideMaxX, block->sideMaxY, 1));
indices.push_back(currentVertex + 0); const Block* northBlockType = &Blocks::blocks[northBlock];
indices.push_back(currentVertex + 3);
indices.push_back(currentVertex + 1);
indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 2);
indices.push_back(currentVertex + 3);
currentVertex += 4;
}
}
// West if (northBlockType->transparent)
{ {
int westBlock; vertices.push_back(Vertex(x + 1, y + 0, z + 0, block->sideMinX, block->sideMinY, 0));
if (x > 0) vertices.push_back(Vertex(x + 0, y + 0, z + 0, block->sideMaxX, block->sideMinY, 0));
{ vertices.push_back(Vertex(x + 1, y + 1, z + 0, block->sideMinX, block->sideMaxY, 0));
int blockIndex = (x - 1) * chunkSize * chunkSize + z * chunkSize + y; vertices.push_back(Vertex(x + 0, y + 1, z + 0, block->sideMaxX, block->sideMaxY, 0));
westBlock = chunkData[blockIndex];
}
else
{
int blockIndex = (chunkSize - 1) *chunkSize * chunkSize + z * chunkSize + y;
westBlock = westData[blockIndex];
}
if (westBlock == 0)
{
vertices.push_back(Vertex(x + 0, y + 0, z + 0, block->sideMinX, block->sideMinY, 2));
vertices.push_back(Vertex(x + 0, y + 0, z + 1, block->sideMaxX, block->sideMinY, 2));
vertices.push_back(Vertex(x + 0, y + 1, z + 0, block->sideMinX, block->sideMaxY, 2));
vertices.push_back(Vertex(x + 0, y + 1, z + 1, block->sideMaxX, block->sideMaxY, 2));
indices.push_back(currentVertex + 0); indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 3); indices.push_back(currentVertex + 3);
indices.push_back(currentVertex + 1); indices.push_back(currentVertex + 1);
indices.push_back(currentVertex + 0); indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 2); indices.push_back(currentVertex + 2);
indices.push_back(currentVertex + 3); indices.push_back(currentVertex + 3);
currentVertex += 4; currentVertex += 4;
}
} }
}
// East // South
{
int eastBlock;
if (x < chunkSize - 1)
{ {
int blockIndex = (x + 1) * chunkSize * chunkSize + z * chunkSize + y; int southBlock;
eastBlock = chunkData[blockIndex]; if (z < chunkSize - 1)
} {
else int southIndex = x * chunkSize * chunkSize + (z + 1) * chunkSize + y;
{ southBlock = chunkData[southIndex];
int blockIndex = 0 * chunkSize * chunkSize + z * chunkSize + y; }
eastBlock = eastData[blockIndex]; else
} {
if (eastBlock == 0) int southIndex = x * chunkSize * chunkSize + 0 * chunkSize + y;
{ southBlock = southData[southIndex];
vertices.push_back(Vertex(x + 1, y + 0, z + 1, block->sideMinX, block->sideMinY, 3)); }
vertices.push_back(Vertex(x + 1, y + 0, z + 0, block->sideMaxX, block->sideMinY, 3));
vertices.push_back(Vertex(x + 1, y + 1, z + 1, block->sideMinX, block->sideMaxY, 3));
vertices.push_back(Vertex(x + 1, y + 1, z + 0, block->sideMaxX, block->sideMaxY, 3));
indices.push_back(currentVertex + 0); const Block* southBlockType = &Blocks::blocks[southBlock];
indices.push_back(currentVertex + 3);
indices.push_back(currentVertex + 1);
indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 2);
indices.push_back(currentVertex + 3);
currentVertex += 4;
}
}
// Bottom if (southBlockType->transparent)
{ {
int bottomBlock; vertices.push_back(Vertex(x + 0, y + 0, z + 1, block->sideMinX, block->sideMinY, 1));
if (y > 0) vertices.push_back(Vertex(x + 1, y + 0, z + 1, block->sideMaxX, block->sideMinY, 1));
{ vertices.push_back(Vertex(x + 0, y + 1, z + 1, block->sideMinX, block->sideMaxY, 1));
int blockIndex = x * chunkSize * chunkSize + z * chunkSize + (y - 1); vertices.push_back(Vertex(x + 1, y + 1, z + 1, block->sideMaxX, block->sideMaxY, 1));
bottomBlock = chunkData[blockIndex];
}
else
{
int blockIndex = x * chunkSize * chunkSize + z * chunkSize + (chunkSize - 1);
bottomBlock = downData[blockIndex];
}
if (bottomBlock == 0)
{
vertices.push_back(Vertex(x + 1, y + 0, z + 1, block->bottomMinX, block->bottomMinY, 4));
vertices.push_back(Vertex(x + 0, y + 0, z + 1, block->bottomMaxX, block->bottomMinY, 4));
vertices.push_back(Vertex(x + 1, y + 0, z + 0, block->bottomMinX, block->bottomMaxY, 4));
vertices.push_back(Vertex(x + 0, y + 0, z + 0, block->bottomMaxX, block->bottomMaxY, 4));
indices.push_back(currentVertex + 0); indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 3); indices.push_back(currentVertex + 3);
indices.push_back(currentVertex + 1); indices.push_back(currentVertex + 1);
indices.push_back(currentVertex + 0); indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 2); indices.push_back(currentVertex + 2);
indices.push_back(currentVertex + 3); indices.push_back(currentVertex + 3);
currentVertex += 4; currentVertex += 4;
}
} }
}
// Top // West
{
int topBlock;
if (y < chunkSize - 1)
{ {
int blockIndex = x * chunkSize * chunkSize + z * chunkSize + (y + 1); int westBlock;
topBlock = chunkData[blockIndex]; if (x > 0)
} {
else int blockIndex = (x - 1) * chunkSize * chunkSize + z * chunkSize + y;
{ westBlock = chunkData[blockIndex];
int blockIndex = x * chunkSize * chunkSize + z * chunkSize + 0; }
topBlock = upData[blockIndex]; else
} {
if (topBlock == 0) int blockIndex = (chunkSize - 1) * chunkSize * chunkSize + z * chunkSize + y;
{ westBlock = westData[blockIndex];
vertices.push_back(Vertex(x + 0, y + 1, z + 1, block->topMinX, block->topMinY, 5)); }
vertices.push_back(Vertex(x + 1, y + 1, z + 1, block->topMaxX, block->topMinY, 5));
vertices.push_back(Vertex(x + 0, y + 1, z + 0, block->topMinX, block->topMaxY, 5));
vertices.push_back(Vertex(x + 1, y + 1, z + 0, block->topMaxX, block->topMaxY, 5));
indices.push_back(currentVertex + 0); const Block* westBlockType = &Blocks::blocks[westBlock];
indices.push_back(currentVertex + 3);
indices.push_back(currentVertex + 1); if (westBlockType->transparent)
indices.push_back(currentVertex + 0); {
indices.push_back(currentVertex + 2); vertices.push_back(Vertex(x + 0, y + 0, z + 0, block->sideMinX, block->sideMinY, 2));
indices.push_back(currentVertex + 3); vertices.push_back(Vertex(x + 0, y + 0, z + 1, block->sideMaxX, block->sideMinY, 2));
currentVertex += 4; vertices.push_back(Vertex(x + 0, y + 1, z + 0, block->sideMinX, block->sideMaxY, 2));
vertices.push_back(Vertex(x + 0, y + 1, z + 1, block->sideMaxX, block->sideMaxY, 2));
indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 3);
indices.push_back(currentVertex + 1);
indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 2);
indices.push_back(currentVertex + 3);
currentVertex += 4;
}
}
// East
{
int eastBlock;
if (x < chunkSize - 1)
{
int blockIndex = (x + 1) * chunkSize * chunkSize + z * chunkSize + y;
eastBlock = chunkData[blockIndex];
}
else
{
int blockIndex = 0 * chunkSize * chunkSize + z * chunkSize + y;
eastBlock = eastData[blockIndex];
}
const Block* eastBlockType = &Blocks::blocks[eastBlock];
if (eastBlockType->transparent)
{
vertices.push_back(Vertex(x + 1, y + 0, z + 1, block->sideMinX, block->sideMinY, 3));
vertices.push_back(Vertex(x + 1, y + 0, z + 0, block->sideMaxX, block->sideMinY, 3));
vertices.push_back(Vertex(x + 1, y + 1, z + 1, block->sideMinX, block->sideMaxY, 3));
vertices.push_back(Vertex(x + 1, y + 1, z + 0, block->sideMaxX, block->sideMaxY, 3));
indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 3);
indices.push_back(currentVertex + 1);
indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 2);
indices.push_back(currentVertex + 3);
currentVertex += 4;
}
}
// Bottom
{
int bottomBlock;
if (y > 0)
{
int blockIndex = x * chunkSize * chunkSize + z * chunkSize + (y - 1);
bottomBlock = chunkData[blockIndex];
}
else
{
int blockIndex = x * chunkSize * chunkSize + z * chunkSize + (chunkSize - 1);
bottomBlock = downData[blockIndex];
}
const Block* bottomBlockType = &Blocks::blocks[bottomBlock];
if (bottomBlockType->transparent)
{
vertices.push_back(Vertex(x + 1, y + 0, z + 1, block->bottomMinX, block->bottomMinY, 4));
vertices.push_back(Vertex(x + 0, y + 0, z + 1, block->bottomMaxX, block->bottomMinY, 4));
vertices.push_back(Vertex(x + 1, y + 0, z + 0, block->bottomMinX, block->bottomMaxY, 4));
vertices.push_back(Vertex(x + 0, y + 0, z + 0, block->bottomMaxX, block->bottomMaxY, 4));
indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 3);
indices.push_back(currentVertex + 1);
indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 2);
indices.push_back(currentVertex + 3);
currentVertex += 4;
}
}
// Top
{
int topBlock;
if (y < chunkSize - 1)
{
int blockIndex = x * chunkSize * chunkSize + z * chunkSize + (y + 1);
topBlock = chunkData[blockIndex];
}
else
{
int blockIndex = x * chunkSize * chunkSize + z * chunkSize + 0;
topBlock = upData[blockIndex];
}
const Block* topBlockType = &Blocks::blocks[topBlock];
if (topBlockType->transparent)
{
vertices.push_back(Vertex(x + 0, y + 1, z + 1, block->topMinX, block->topMinY, 5));
vertices.push_back(Vertex(x + 1, y + 1, z + 1, block->topMaxX, block->topMinY, 5));
vertices.push_back(Vertex(x + 0, y + 1, z + 0, block->topMinX, block->topMaxY, 5));
vertices.push_back(Vertex(x + 1, y + 1, z + 0, block->topMaxX, block->topMaxY, 5));
indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 3);
indices.push_back(currentVertex + 1);
indices.push_back(currentVertex + 0);
indices.push_back(currentVertex + 2);
indices.push_back(currentVertex + 3);
currentVertex += 4;
}
} }
} }
} }
@ -266,7 +340,7 @@ void Chunk::Render(unsigned int modelLoc)
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_BYTE, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, posX)); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, posX));
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_BYTE, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texGridX)); glVertexAttribPointer(1, 2, GL_BYTE, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texGridX));
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);

View File

@ -6,11 +6,11 @@
struct Vertex struct Vertex
{ {
char posX, posY, posZ; float posX, posY, posZ;
char texGridX, texGridY; char texGridX, texGridY;
char direction; char direction;
Vertex(char _posX, char _posY, char _posZ, char _texGridX, char _texGridY, char _direction) Vertex(float _posX, float _posY, float _posZ, char _texGridX, char _texGridY, char _direction)
{ {
posX = _posX; posX = _posX;
posY = _posY; posY = _posY;

View File

@ -0,0 +1,11 @@
#include "SurfaceFeature.h"
SurfaceFeature::SurfaceFeature(NoiseSettings _noiseSettings, std::vector<unsigned int> _blocks, std::vector<bool> _replaceBlock,
int _sizeX, int _sizeY, int _sizeZ,
int _offsetX, int _offsetY, int _offsetZ)
: noiseSettings(_noiseSettings), blocks(_blocks), replaceBlock(_replaceBlock),
sizeX(_sizeX), sizeY(_sizeY), sizeZ(_sizeZ),
offsetX(_offsetX), offsetY(_offsetY), offsetZ(_offsetZ)
{
}

View File

@ -0,0 +1,17 @@
#pragma once
#include "NoiseSettings.h"
#include <vector>
struct SurfaceFeature
{
NoiseSettings noiseSettings;
std::vector<unsigned int> blocks;
std::vector<bool> replaceBlock;
int sizeX, sizeY, sizeZ;
int offsetX, offsetY, offsetZ;
SurfaceFeature(NoiseSettings _noiseSettings, std::vector<unsigned int> _blocks, std::vector<bool> _replaceBlock,
int _sizeX, int _sizeY, int _sizeZ,
int _offsetX, int _offsetY, int _offsetZ);
};

View File

@ -1,6 +1,7 @@
#include "WorldGen.h" #include "WorldGen.h"
#include <algorithm> #include <algorithm>
#include <iostream>
#include <OpenSimplexNoise.hh> #include <OpenSimplexNoise.hh>
#include "Blocks.h" #include "Blocks.h"
@ -8,25 +9,197 @@
void WorldGen::GenerateChunkData(int chunkX, int chunkY, int chunkZ, int chunkSize, std::vector<unsigned int>* chunkData) void WorldGen::GenerateChunkData(int chunkX, int chunkY, int chunkZ, int chunkSize, std::vector<unsigned int>* chunkData)
{ {
// Init noise // Init noise
OSN::Noise<2> noise2D; static OSN::Noise<2> noise2D;
OSN::Noise<3> noise3D; static OSN::Noise<3> noise3D;
// Init noise settings // Init noise settings
NoiseSettings surfaceSettings[] { static NoiseSettings surfaceSettings[]{
{ 0.01f, 20.0f, 0 }, { 0.01f, 20.0f, 0 },
{ 0.1f, 3.0f, 0 } { 0.1f, 3.0f, 0 }
}; };
int surfaceSettingsLength = sizeof(surfaceSettings) / sizeof(*surfaceSettings); static int surfaceSettingsLength = sizeof(surfaceSettings) / sizeof(*surfaceSettings);
NoiseSettings caveSettings[] { static NoiseSettings caveSettings[]{
{ 0.05f, 1.0f, 0, .5f, 0, 100 } { 0.05f, 1.0f, 0, .5f, 0, 100 }
}; };
int caveSettingsLength = sizeof(caveSettings) / sizeof(*caveSettings); static int caveSettingsLength = sizeof(caveSettings) / sizeof(*caveSettings);
NoiseSettings oreSettings[] { static NoiseSettings oreSettings[]{
{ 0.075f, 1.0f, 8.54f, .75f, 1, 0 } { 0.075f, 1.0f, 8.54f, .75f, 1, 0 }
}; };
int oreSettingsLength = sizeof(oreSettings) / sizeof(*oreSettings); static int oreSettingsLength = sizeof(oreSettings) / sizeof(*oreSettings);
static SurfaceFeature surfaceFeatures[]{
// Tree
{
{ 4.23f, 1.0f, 8.54f, .8f, 1, 0 },
{
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 5, 5, 0, 0,
0, 0, 0, 5, 5, 0, 0,
0, 0, 0, 5, 5, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 5, 5, 0, 0,
0, 0, 0, 5, 5, 5, 0,
0, 0, 0, 5, 5, 5, 5,
0, 0, 0, 5, 5, 5, 0,
0, 0, 0, 5, 5, 0, 0,
0, 0, 0, 5, 5, 0, 0,
0, 0, 0, 5, 5, 5, 5,
1, 4, 4, 4, 4, 5, 5,
0, 0, 0, 5, 5, 5, 5,
0, 0, 0, 5, 5, 0, 0,
0, 0, 0, 5, 5, 0, 0,
0, 0, 0, 5, 5, 5, 0,
0, 0, 0, 5, 5, 5, 5,
0, 0, 0, 5, 5, 5, 0,
0, 0, 0, 5, 5, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 5, 5, 0, 0,
0, 0, 0, 5, 5, 0, 0,
0, 0, 0, 5, 5, 0, 0,
0, 0, 0, 0, 0, 0, 0,
},
{
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
true, true, true, true, true, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false, false, false,
},
5,
7,
5,
-2,
0,
-2
},
// Grass
{
{ 2.65f, 1.0f, 8.54f, .5f, 1, 0 },
{
2, 6
},
{
false, false
},
1,
2,
1,
0,
0,
0
},
// Tall Grass
{
{ 1.23f, 1.0f, 4.34f, .6f, 1, 0 },
{
2, 7, 8
},
{
false, false, false
},
1,
3,
1,
0,
0,
0
},
// Poppy
{
{ 5.32f, 1.0f, 3.67f, .8f, 1, 0 },
{
2, 9
},
{
false, false
},
1,
2,
1,
0,
0,
0
},
// White Tulip
{
{ 5.57f, 1.0f, 7.654f, .8f, 1, 0 },
{
2, 10
},
{
false, false
},
1,
2,
1,
0,
0,
0
},
// Pink Tulip
{
{ 4.94f, 1.0f, 2.23f, .8f, 1, 0 },
{
2, 11
},
{
false, false
},
1,
2,
1,
0,
0,
0
},
// Orange Tulip
{
{ 6.32f, 1.0f, 8.2f, .85f, 1, 0 },
{
2, 12
},
{
false, false
},
1,
2,
1,
0,
0,
0
},
};
static int surfaceFeaturesLength = sizeof(surfaceFeatures) / sizeof(*surfaceFeatures);
// Set vector size // Set vector size
chunkData->reserve(chunkSize * chunkSize * chunkSize); chunkData->reserve(chunkSize * chunkSize * chunkSize);
@ -115,4 +288,98 @@ void WorldGen::GenerateChunkData(int chunkX, int chunkY, int chunkZ, int chunkSi
} }
} }
} }
// Step 3: Surface Features
for (int i = 0; i < surfaceFeaturesLength; i++)
{
for (int x = -surfaceFeatures[i].sizeX - surfaceFeatures[i].offsetX; x < chunkSize - surfaceFeatures[i].offsetX; x++)
{
for (int z = -surfaceFeatures[i].sizeZ - surfaceFeatures[i].offsetZ; z < chunkSize - surfaceFeatures[i].offsetZ; z++)
{
int noiseY = 20;
for (int s = 0; s < surfaceSettingsLength; s++)
{
noiseY += noise2D.eval(
(float)((x + startX) * surfaceSettings[s].frequency) + surfaceSettings[s].offset,
(float)((z + startZ) * surfaceSettings[s].frequency) + surfaceSettings[s].offset)
* surfaceSettings[s].amplitude;
}
if (noiseY + surfaceFeatures[i].offsetY > startY + 32 || noiseY + surfaceFeatures[i].sizeY + surfaceFeatures[i].offsetY < startY)
continue;
// Check if it's in a cave
bool cave = false;
for (int i = 0; i < caveSettingsLength; i++)
{
if (noiseY + startY > caveSettings[i].maxHeight)
continue;
float noiseCaves = noise3D.eval(
(float)((x + startX) * caveSettings[i].frequency) + caveSettings[i].offset,
(float)((noiseY) * caveSettings[i].frequency) + caveSettings[i].offset,
(float)((z + startZ) * caveSettings[i].frequency) + caveSettings[i].offset)
* caveSettings[i].amplitude;
if (noiseCaves > caveSettings[i].chance)
{
cave = true;
break;
}
}
if (cave)
continue;
float noise = noise2D.eval(
(float)((x + startX) * surfaceFeatures[i].noiseSettings.frequency) + surfaceFeatures[i].noiseSettings.offset,
(float)((z + startZ) * surfaceFeatures[i].noiseSettings.frequency) + surfaceFeatures[i].noiseSettings.offset);
if (noise > surfaceFeatures[i].noiseSettings.chance)
{
int featureX = x + startX;
int featureY = noiseY;
int featureZ = z + startZ;
for (int fX = 0; fX < surfaceFeatures[i].sizeX; fX++)
{
for (int fY = 0; fY < surfaceFeatures[i].sizeY; fY++)
{
for (int fZ = 0; fZ < surfaceFeatures[i].sizeZ; fZ++)
{
int localX = featureX + fX + surfaceFeatures[i].offsetX - startX;
//std::cout << "FeatureX: " << featureX << ", fX: " << fX << ", startX: " << startX << ", localX: " << localX << '\n';
int localY = featureY + fY + surfaceFeatures[i].offsetY - startY;
//std::cout << "FeatureY: " << featureY << ", fY: " << fY << ", startY: " << startY << ", localY: " << localY << '\n';
int localZ = featureZ + fZ + surfaceFeatures[i].offsetZ - startZ;
//std::cout << "FeatureZ: " << featureZ << ", fZ: " << fZ << ", startZ: " << startZ << ", localZ: " << localZ << '\n';
if (localX >= 32 || localX < 0)
continue;
if (localY >= 32 || localY < 0)
continue;
if (localZ >= 32 || localZ < 0)
continue;
int featureIndex = fX * surfaceFeatures[i].sizeZ * surfaceFeatures[i].sizeY +
fZ * surfaceFeatures[i].sizeY +
fY;
//std::cout << "Feature Index: " << featureIndex << '\n';
int localIndex = localX * chunkSize * chunkSize + localZ * chunkSize + localY;
//std::cout << "Local Index: " << localIndex << ", Max Index: " << chunkData->size() << '\n';
if (surfaceFeatures[i].replaceBlock[featureIndex] || chunkData->at(localIndex) == 0)
chunkData->at(localIndex) = surfaceFeatures[i].blocks[featureIndex];
}
}
}
//int index = x * chunkSize * chunkSize + z * chunkSize + noiseY;
//chunkData->at(index) = surfaceFeatures[i].block;
//index = x * chunkSize * chunkSize + z * chunkSize + noiseY + 1;
//chunkData->at(index) = surfaceFeatures[i].block;
}
}
}
}
} }

View File

@ -2,8 +2,10 @@
#include <vector> #include <vector>
#include "NoiseSettings.h" #include "NoiseSettings.h"
#include "SurfaceFeature.h"
namespace WorldGen namespace WorldGen
{ {
void GenerateChunkData(int chunkX, int chunkY, int chunkZ, int chunkSize, std::vector<unsigned int>* chunkData); void GenerateChunkData(int chunkX, int chunkY, int chunkZ, int chunkSize, std::vector<unsigned int>* chunkData);
} }