diff --git a/ScuffedMinecraft/ScuffedMinecraft.vcxproj b/ScuffedMinecraft/ScuffedMinecraft.vcxproj index 6afb80e..e179226 100644 --- a/ScuffedMinecraft/ScuffedMinecraft.vcxproj +++ b/ScuffedMinecraft/ScuffedMinecraft.vcxproj @@ -71,20 +71,20 @@ - C:\dev\C++\ScuffedMinecraft\Dependencies\include;$(IncludePath) - C:\dev\C++\ScuffedMinecraft\Dependencies\lib;$(LibraryPath) + $(SolutionDir)Dependencies\include;$(IncludePath) + $(SolutionDir)Dependencies\lib;$(LibraryPath) - C:\dev\C++\ScuffedMinecraft\Dependencies\include;$(IncludePath) - C:\dev\C++\ScuffedMinecraft\Dependencies\lib;$(LibraryPath) + $(SolutionDir)Dependencies\include;$(IncludePath) + $(SolutionDir)Dependencies\lib;$(LibraryPath) - C:\dev\C++\ScuffedMinecraft\Dependencies\include;$(IncludePath) - C:\dev\C++\ScuffedMinecraft\Dependencies\lib;$(LibraryPath) + $(SolutionDir)Dependencies\include;$(IncludePath) + $(SolutionDir)Dependencies\lib;$(LibraryPath) - C:\dev\C++\ScuffedMinecraft\Dependencies\include;$(IncludePath) - C:\dev\C++\ScuffedMinecraft\Dependencies\lib;$(LibraryPath) + $(SolutionDir)Dependencies\include;$(IncludePath) + $(SolutionDir)Dependencies\lib;$(LibraryPath) @@ -155,6 +155,7 @@ + @@ -174,6 +175,7 @@ + diff --git a/ScuffedMinecraft/ScuffedMinecraft.vcxproj.filters b/ScuffedMinecraft/ScuffedMinecraft.vcxproj.filters index a06374b..de7702e 100644 --- a/ScuffedMinecraft/ScuffedMinecraft.vcxproj.filters +++ b/ScuffedMinecraft/ScuffedMinecraft.vcxproj.filters @@ -66,6 +66,9 @@ Source Files + + Source Files + @@ -119,6 +122,9 @@ Header Files + + Header Files + diff --git a/ScuffedMinecraft/assets/shaders/fragment_shader.glsl b/ScuffedMinecraft/assets/shaders/fragment_shader.glsl index 89d1523..ed33dfb 100644 --- a/ScuffedMinecraft/assets/shaders/fragment_shader.glsl +++ b/ScuffedMinecraft/assets/shaders/fragment_shader.glsl @@ -1,12 +1,23 @@ #version 330 core in vec2 TexCoord; +in vec3 Normal; out vec4 FragColor; uniform sampler2D tex; +vec3 ambient = vec3(.5); +vec3 lightDirection = vec3(0.8, 1, 0.7); + void main() { - FragColor = texture(tex, TexCoord); + vec3 lightDir = normalize(-lightDirection); + + float diff = max(dot(Normal, lightDir), 0.0); + vec3 diffuse = diff * vec3(1); + + vec4 result = vec4(ambient + diffuse, 1.0); + + FragColor = texture(tex, TexCoord) * result; } \ No newline at end of file diff --git a/ScuffedMinecraft/assets/shaders/vertex_shader.glsl b/ScuffedMinecraft/assets/shaders/vertex_shader.glsl index 56ce3f7..e15e933 100644 --- a/ScuffedMinecraft/assets/shaders/vertex_shader.glsl +++ b/ScuffedMinecraft/assets/shaders/vertex_shader.glsl @@ -1,9 +1,12 @@ #version 330 core + layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; +layout (location = 2) in int aDirection; out vec2 TexCoord; +out vec3 Normal; uniform float texMultiplier; @@ -11,8 +14,20 @@ uniform mat4 model; uniform mat4 view; uniform mat4 projection; +// Array of possible normals based on direction +const vec3 normals[6] = vec3[6]( + vec3(0, 0, 1), // 0 + vec3(0, 0, -1), // 1 + vec3(1, 0, 0), // 2 + vec3(-1, 0, 0), // 3 + vec3(0, 1, 0), // 4 + vec3(0, -1, 0) // 5 +); + void main() { gl_Position = projection * view * model * vec4(aPos, 1.0); TexCoord = aTexCoord * texMultiplier; + + Normal = normals[aDirection]; } \ No newline at end of file diff --git a/ScuffedMinecraft/src/Application.cpp b/ScuffedMinecraft/src/Application.cpp index c65371d..03c8326 100644 --- a/ScuffedMinecraft/src/Application.cpp +++ b/ScuffedMinecraft/src/Application.cpp @@ -52,8 +52,8 @@ int main() glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Create window - GLFWwindow* window = glfwCreateWindow(windowX, windowY, "Scuffed Minecraft", NULL, NULL); - if (window == NULL) + GLFWwindow* window = glfwCreateWindow(windowX, windowY, "Scuffed Minecraft", nullptr, nullptr); + if (window == nullptr) { std::cout << "Failed to create GLFW window\n"; glfwTerminate(); diff --git a/ScuffedMinecraft/src/Chunk.cpp b/ScuffedMinecraft/src/Chunk.cpp index 6b7c237..1ca955e 100644 --- a/ScuffedMinecraft/src/Chunk.cpp +++ b/ScuffedMinecraft/src/Chunk.cpp @@ -44,12 +44,6 @@ void Chunk::GenerateChunk() WorldGen::GenerateChunkData(chunkPos.x - 1, chunkPos.y, chunkPos.z, chunkSize, &westData); WorldGen::GenerateChunkData(chunkPos.x, chunkPos.y + 1, chunkPos.z, chunkSize, &upData); WorldGen::GenerateChunkData(chunkPos.x, chunkPos.y - 1, chunkPos.z, chunkSize, &downData); - //auto northData = Planet::planet->GetChunkData(chunkPos.x, chunkPos.y, chunkPos.z - 1); - //auto southData = Planet::planet->GetChunkData(chunkPos.x, chunkPos.y, chunkPos.z + 1); - //auto eastData = Planet::planet->GetChunkData(chunkPos.x + 1, chunkPos.y, chunkPos.z); - //auto westData = Planet::planet->GetChunkData(chunkPos.x - 1, chunkPos.y, chunkPos.z); - //auto upData = Planet::planet->GetChunkData(chunkPos.x, chunkPos.y + 1, chunkPos.z); - //auto downData = Planet::planet->GetChunkData(chunkPos.x, chunkPos.y - 1, chunkPos.z); //std::cout << "Got chunk data in thread: " << std::this_thread::get_id() << '\n'; @@ -82,10 +76,10 @@ void Chunk::GenerateChunk() if (northBlock == 0) { - vertices.push_back(Vertex(x + 1, y + 0, z + 0, block->sideMinX, block->sideMinY)); - vertices.push_back(Vertex(x + 0, y + 0, z + 0, block->sideMaxX, block->sideMinY)); - vertices.push_back(Vertex(x + 1, y + 1, z + 0, block->sideMinX, block->sideMaxY)); - vertices.push_back(Vertex(x + 0, y + 1, z + 0, block->sideMaxX, block->sideMaxY)); + vertices.push_back(Vertex(x + 1, y + 0, z + 0, block->sideMinX, block->sideMinY, 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)); + vertices.push_back(Vertex(x + 0, y + 1, z + 0, block->sideMaxX, block->sideMaxY, 0)); indices.push_back(currentVertex + 0); indices.push_back(currentVertex + 3); @@ -112,10 +106,10 @@ void Chunk::GenerateChunk() } if (southBlock == 0) { - vertices.push_back(Vertex(x + 0, y + 0, z + 1, block->sideMinX, block->sideMinY)); - vertices.push_back(Vertex(x + 1, y + 0, z + 1, block->sideMaxX, block->sideMinY)); - vertices.push_back(Vertex(x + 0, y + 1, z + 1, block->sideMinX, block->sideMaxY)); - vertices.push_back(Vertex(x + 1, y + 1, z + 1, block->sideMaxX, block->sideMaxY)); + 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); indices.push_back(currentVertex + 3); @@ -142,10 +136,10 @@ void Chunk::GenerateChunk() } if (westBlock == 0) { - vertices.push_back(Vertex(x + 0, y + 0, z + 0, block->sideMinX, block->sideMinY)); - vertices.push_back(Vertex(x + 0, y + 0, z + 1, block->sideMaxX, block->sideMinY)); - vertices.push_back(Vertex(x + 0, y + 1, z + 0, block->sideMinX, block->sideMaxY)); - vertices.push_back(Vertex(x + 0, y + 1, z + 1, block->sideMaxX, block->sideMaxY)); + 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 + 3); @@ -172,10 +166,10 @@ void Chunk::GenerateChunk() } if (eastBlock == 0) { - vertices.push_back(Vertex(x + 1, y + 0, z + 1, block->sideMinX, block->sideMinY)); - vertices.push_back(Vertex(x + 1, y + 0, z + 0, block->sideMaxX, block->sideMinY)); - vertices.push_back(Vertex(x + 1, y + 1, z + 1, block->sideMinX, block->sideMaxY)); - vertices.push_back(Vertex(x + 1, y + 1, z + 0, block->sideMaxX, block->sideMaxY)); + 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); @@ -202,10 +196,10 @@ void Chunk::GenerateChunk() } if (bottomBlock == 0) { - vertices.push_back(Vertex(x + 1, y + 0, z + 1, block->bottomMinX, block->bottomMinY)); - vertices.push_back(Vertex(x + 0, y + 0, z + 1, block->bottomMaxX, block->bottomMinY)); - vertices.push_back(Vertex(x + 1, y + 0, z + 0, block->bottomMinX, block->bottomMaxY)); - vertices.push_back(Vertex(x + 0, y + 0, z + 0, block->bottomMaxX, block->bottomMaxY)); + 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); @@ -232,10 +226,10 @@ void Chunk::GenerateChunk() } if (topBlock == 0) { - vertices.push_back(Vertex(x + 0, y + 1, z + 1, block->topMinX, block->topMinY)); - vertices.push_back(Vertex(x + 1, y + 1, z + 1, block->topMaxX, block->topMinY)); - vertices.push_back(Vertex(x + 0, y + 1, z + 0, block->topMinX, block->topMaxY)); - vertices.push_back(Vertex(x + 1, y + 1, z + 0, block->topMaxX, block->topMaxY)); + 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); @@ -276,6 +270,8 @@ void Chunk::Render(unsigned int modelLoc) glEnableVertexAttribArray(0); glVertexAttribPointer(1, 2, GL_BYTE, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texGridX)); glEnableVertexAttribArray(1); + glVertexAttribIPointer(2, 1, GL_BYTE, sizeof(Vertex), (void*)offsetof(Vertex, direction)); + glEnableVertexAttribArray(2); glGenBuffers(1, &ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); diff --git a/ScuffedMinecraft/src/Chunk.h b/ScuffedMinecraft/src/Chunk.h index ebcace2..549472c 100644 --- a/ScuffedMinecraft/src/Chunk.h +++ b/ScuffedMinecraft/src/Chunk.h @@ -8,8 +8,9 @@ struct Vertex { char posX, posY, posZ; char texGridX, texGridY; + char direction; - Vertex(char _posX, char _posY, char _posZ, char _texGridX, char _texGridY) + Vertex(char _posX, char _posY, char _posZ, char _texGridX, char _texGridY, char _direction) { posX = _posX; posY = _posY; @@ -17,6 +18,8 @@ struct Vertex texGridX = _texGridX; texGridY = _texGridY; + + direction = _direction; } }; diff --git a/ScuffedMinecraft/src/NoiseSettings.cpp b/ScuffedMinecraft/src/NoiseSettings.cpp new file mode 100644 index 0000000..a390e4e --- /dev/null +++ b/ScuffedMinecraft/src/NoiseSettings.cpp @@ -0,0 +1,18 @@ +#include "NoiseSettings.h" + +NoiseSettings::NoiseSettings(float _frequency, float _amplitude, float _offset) + : frequency(_frequency), amplitude(_amplitude), offset(_offset), chance(0), block(0) +{ + +} + +NoiseSettings::NoiseSettings(float _frequency, float _amplitude, float _offset, float _chance, unsigned int _block, int _maxHeight) + : frequency(_frequency), amplitude(_amplitude), offset(_offset), chance(_chance), block(_block), maxHeight(_maxHeight) +{ + +} + +NoiseSettings::~NoiseSettings() +{ + +} \ No newline at end of file diff --git a/ScuffedMinecraft/src/NoiseSettings.h b/ScuffedMinecraft/src/NoiseSettings.h new file mode 100644 index 0000000..3da209b --- /dev/null +++ b/ScuffedMinecraft/src/NoiseSettings.h @@ -0,0 +1,17 @@ +#pragma once + +struct NoiseSettings +{ +public: + float amplitude; + float frequency; + float offset; + + float chance; + unsigned int block; + int maxHeight; + + NoiseSettings(float _frequency, float _amplitude, float _offset); + NoiseSettings(float _frequency, float _amplitude, float _offset, float _chance, unsigned int _block, int _maxHeight); + ~NoiseSettings(); +}; \ No newline at end of file diff --git a/ScuffedMinecraft/src/Planet.cpp b/ScuffedMinecraft/src/Planet.cpp index 8e0306c..9d2d72f 100644 --- a/ScuffedMinecraft/src/Planet.cpp +++ b/ScuffedMinecraft/src/Planet.cpp @@ -55,17 +55,17 @@ void Planet::Update(float camX, float camY, float camZ, unsigned int modelLoc) // Add middle chunks for (int y = 0; y <= renderHeight; y++) { - chunkQueue.push({ camChunkX, y, camChunkZ + r }); - chunkQueue.push({ camChunkX + r, y, camChunkZ }); - chunkQueue.push({ camChunkX, y, camChunkZ - r }); - chunkQueue.push({ camChunkX - r, y, camChunkZ }); + chunkQueue.push({ camChunkX, camChunkY + y, camChunkZ + r }); + chunkQueue.push({ camChunkX + r, camChunkY + y, camChunkZ }); + chunkQueue.push({ camChunkX, camChunkY + y, camChunkZ - r }); + chunkQueue.push({ camChunkX - r, camChunkY + y, camChunkZ }); if (y > 0) { - chunkQueue.push({ camChunkX, -y, camChunkZ + r }); - chunkQueue.push({ camChunkX + r, -y, camChunkZ }); - chunkQueue.push({ camChunkX, -y, camChunkZ - r }); - chunkQueue.push({ camChunkX - r, -y, camChunkZ }); + chunkQueue.push({ camChunkX, camChunkY - y, camChunkZ + r }); + chunkQueue.push({ camChunkX + r, camChunkY - y, camChunkZ }); + chunkQueue.push({ camChunkX, camChunkY - y, camChunkZ - r }); + chunkQueue.push({ camChunkX - r, camChunkY - y, camChunkZ }); } } @@ -74,31 +74,31 @@ void Planet::Update(float camX, float camY, float camZ, unsigned int modelLoc) { for (int y = 0; y <= renderHeight; y++) { - chunkQueue.push({ camChunkX + e, y, camChunkZ + r }); - chunkQueue.push({ camChunkX - e, y, camChunkZ + r }); + chunkQueue.push({ camChunkX + e, camChunkY + y, camChunkZ + r }); + chunkQueue.push({ camChunkX - e, camChunkY + y, camChunkZ + r }); - chunkQueue.push({ camChunkX + r, y, camChunkZ + e }); - chunkQueue.push({ camChunkX + r, y, camChunkZ - e }); + chunkQueue.push({ camChunkX + r, camChunkY + y, camChunkZ + e }); + chunkQueue.push({ camChunkX + r, camChunkY + y, camChunkZ - e }); - chunkQueue.push({ camChunkX + e, y, camChunkZ - r }); - chunkQueue.push({ camChunkX - e, y, camChunkZ - r }); + chunkQueue.push({ camChunkX + e, camChunkY + y, camChunkZ - r }); + chunkQueue.push({ camChunkX - e, camChunkY + y, camChunkZ - r }); - chunkQueue.push({ camChunkX - r, y, camChunkZ + e }); - chunkQueue.push({ camChunkX - r, y, camChunkZ - e }); + chunkQueue.push({ camChunkX - r, camChunkY + y, camChunkZ + e }); + chunkQueue.push({ camChunkX - r, camChunkY + y, camChunkZ - e }); if (y > 0) { - chunkQueue.push({ camChunkX + e, -y, camChunkZ + r }); - chunkQueue.push({ camChunkX - e, -y, camChunkZ + r }); + chunkQueue.push({ camChunkX + e, camChunkY - y, camChunkZ + r }); + chunkQueue.push({ camChunkX - e, camChunkY - y, camChunkZ + r }); - chunkQueue.push({ camChunkX + r, -y, camChunkZ + e }); - chunkQueue.push({ camChunkX + r, -y, camChunkZ - e }); + chunkQueue.push({ camChunkX + r, camChunkY - y, camChunkZ + e }); + chunkQueue.push({ camChunkX + r, camChunkY - y, camChunkZ - e }); - chunkQueue.push({ camChunkX + e, -y, camChunkZ - r }); - chunkQueue.push({ camChunkX - e, -y, camChunkZ - r }); + chunkQueue.push({ camChunkX + e, camChunkY - y, camChunkZ - r }); + chunkQueue.push({ camChunkX - e, camChunkY - y, camChunkZ - r }); - chunkQueue.push({ camChunkX - r, -y, camChunkZ + e }); - chunkQueue.push({ camChunkX - r, -y, camChunkZ - e }); + chunkQueue.push({ camChunkX - r, camChunkY - y, camChunkZ + e }); + chunkQueue.push({ camChunkX - r, camChunkY - y, camChunkZ - e }); } } } @@ -106,17 +106,17 @@ void Planet::Update(float camX, float camY, float camZ, unsigned int modelLoc) // Add corners for (int y = 0; y <= renderHeight; y++) { - chunkQueue.push({ camChunkX + r, y, camChunkZ + r }); - chunkQueue.push({ camChunkX + r, y, camChunkZ - r }); - chunkQueue.push({ camChunkX - r, y, camChunkZ + r }); - chunkQueue.push({ camChunkX - r, y, camChunkZ - r }); + chunkQueue.push({ camChunkX + r, camChunkY + y, camChunkZ + r }); + chunkQueue.push({ camChunkX + r, camChunkY + y, camChunkZ - r }); + chunkQueue.push({ camChunkX - r, camChunkY + y, camChunkZ + r }); + chunkQueue.push({ camChunkX - r, camChunkY + y, camChunkZ - r }); if (y > 0) { - chunkQueue.push({ camChunkX + r, -y, camChunkZ + r }); - chunkQueue.push({ camChunkX + r, -y, camChunkZ - r }); - chunkQueue.push({ camChunkX - r, -y, camChunkZ + r }); - chunkQueue.push({ camChunkX - r, -y, camChunkZ - r }); + chunkQueue.push({ camChunkX + r, camChunkY - y, camChunkZ + r }); + chunkQueue.push({ camChunkX + r, camChunkY - y, camChunkZ - r }); + chunkQueue.push({ camChunkX - r, camChunkY - y, camChunkZ + r }); + chunkQueue.push({ camChunkX - r, camChunkY - y, camChunkZ - r }); } } } diff --git a/ScuffedMinecraft/src/Shader.cpp b/ScuffedMinecraft/src/Shader.cpp index 697dc59..98c80cf 100644 --- a/ScuffedMinecraft/src/Shader.cpp +++ b/ScuffedMinecraft/src/Shader.cpp @@ -46,25 +46,25 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath) // vertex shader vertex = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertex, 1, &vShaderCode, NULL); + glShaderSource(vertex, 1, &vShaderCode, nullptr); glCompileShader(vertex); // print compile errors if any glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); if (!success) { - glGetShaderInfoLog(vertex, 512, NULL, infoLog); + glGetShaderInfoLog(vertex, 512, nullptr, infoLog); std::cout << "Error compiling vertex shader!\n" << infoLog << '\n'; } // fragment shader fragment = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragment, 1, &fShaderCode, NULL); + glShaderSource(fragment, 1, &fShaderCode, nullptr); glCompileShader(fragment); // print compile errors if any glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); if (!success) { - glGetShaderInfoLog(fragment, 512, NULL, infoLog); + glGetShaderInfoLog(fragment, 512, nullptr, infoLog); std::cout << "Error compiling fragment shader!\n" << infoLog << '\n'; } @@ -77,7 +77,7 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath) glGetProgramiv(ID, GL_LINK_STATUS, &success); if (!success) { - glGetProgramInfoLog(ID, 512, NULL, infoLog); + glGetProgramInfoLog(ID, 512, nullptr, infoLog); std::cout << "Error linking shader program!\n" << infoLog << '\n'; } diff --git a/ScuffedMinecraft/src/WorldGen.cpp b/ScuffedMinecraft/src/WorldGen.cpp index b563ba7..f72f70a 100644 --- a/ScuffedMinecraft/src/WorldGen.cpp +++ b/ScuffedMinecraft/src/WorldGen.cpp @@ -1,15 +1,37 @@ #include "WorldGen.h" +#include #include #include "Blocks.h" void WorldGen::GenerateChunkData(int chunkX, int chunkY, int chunkZ, int chunkSize, std::vector* chunkData) { + // Init noise + OSN::Noise<2> noise2D; + OSN::Noise<3> noise3D; + + // Init noise settings + NoiseSettings surfaceSettings[] { + { 0.01f, 20.0f, 0 }, + { 0.1f, 3.0f, 0 } + }; + int surfaceSettingsLength = sizeof(surfaceSettings) / sizeof(*surfaceSettings); + + NoiseSettings caveSettings[] { + { 0.05f, 1.0f, 0, .5f, 0, 100 } + }; + int caveSettingsLength = sizeof(caveSettings) / sizeof(*caveSettings); + + NoiseSettings oreSettings[] { + { 0.075f, 1.0f, 8.54f, .75f, 1, 0 } + }; + int oreSettingsLength = sizeof(oreSettings) / sizeof(*oreSettings); + + // Set vector size chunkData->reserve(chunkSize * chunkSize * chunkSize); - OSN::Noise<2> surfaceNoise; - OSN::Noise<3> caveNoise; + // Account for chunk position int startX = chunkX * chunkSize; int startY = chunkY * chunkSize; int startZ = chunkZ * chunkSize; @@ -18,36 +40,78 @@ void WorldGen::GenerateChunkData(int chunkX, int chunkY, int chunkZ, int chunkSi { for (int z = 0; z < chunkSize; z++) { - int noiseY = (surfaceNoise.eval((float)(x + startX) * .1f, (float)(z + startZ) * .1f) * 3.0f) + 20; + // Surface noise + int noiseY = 20; + for (int i = 0; i < surfaceSettingsLength; i++) + { + noiseY += noise2D.eval( + (float)((x + startX) * surfaceSettings[i].frequency) + surfaceSettings[i].offset, + (float)((z + startZ) * surfaceSettings[i].frequency) + surfaceSettings[i].offset) + * surfaceSettings[i].amplitude; + } + for (int y = 0; y < chunkSize; y++) { - //chunkData.push_back(0); // Empty - //chunkData.push_back(1); // Full - //chunkData.push_back(((x + y + z) % 2 == 0) ? 1 : 0); // Checkerboard - /*chunkData.push_back( // Edges - (x == 0 || x == chunkSize - 1) || - (y == 0 || y == chunkSize - 1) || - (z == 0 || z == chunkSize - 1) - ? 1 : 0 - );*/ + // Cave noise + bool cave = false; + for (int i = 0; i < caveSettingsLength; i++) + { + if (y + startY > caveSettings[i].maxHeight) + continue; - float noiseCaves = caveNoise.eval( - (float)(x + startX) * .1f, - (float)(y + startY) * .1f, - (float)(z + startZ) * .1f); + float noiseCaves = noise3D.eval( + (float)((x + startX) * caveSettings[i].frequency) + caveSettings[i].offset, + (float)((y + startY) * 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; + } + } + + // Step 1: Terrain Shape (surface and caves) and Ores // Sky and Caves if (y + startY > noiseY) chunkData->push_back(0); - else if (noiseCaves > .5f) + else if (cave) chunkData->push_back(0); // Ground - else if (y + startY == noiseY) - chunkData->push_back(Blocks::GRASS_BLOCK); - else if (y + startY > 10) - chunkData->push_back(Blocks::DIRT_BLOCK); else - chunkData->push_back(Blocks::STONE_BLOCK); + { + bool blockSet = false; + for (int i = 0; i < oreSettingsLength; i++) + { + if (y + startY > oreSettings[i].maxHeight) + continue; + + float noiseOre = noise3D.eval( + (float)((x + startX) * oreSettings[i].frequency) + oreSettings[i].offset, + (float)((y + startY) * oreSettings[i].frequency) + oreSettings[i].offset, + (float)((z + startZ) * oreSettings[i].frequency) + oreSettings[i].offset) + * oreSettings[i].amplitude; + + if (noiseOre > oreSettings[i].chance) + { + chunkData->push_back(oreSettings[i].block); + blockSet = true; + break; + } + } + + if (!blockSet) + { + if (y + startY == noiseY) + chunkData->push_back(Blocks::GRASS_BLOCK); + else if (y + startY > 10) + chunkData->push_back(Blocks::DIRT_BLOCK); + else + chunkData->push_back(Blocks::STONE_BLOCK); + } + } } } } diff --git a/ScuffedMinecraft/src/WorldGen.h b/ScuffedMinecraft/src/WorldGen.h index 15f94e1..e198a55 100644 --- a/ScuffedMinecraft/src/WorldGen.h +++ b/ScuffedMinecraft/src/WorldGen.h @@ -1,6 +1,7 @@ #pragma once #include +#include "NoiseSettings.h" namespace WorldGen {