diff --git a/ScuffedMinecraft/ScuffedMinecraft.vcxproj b/ScuffedMinecraft/ScuffedMinecraft.vcxproj index e179226..da168e1 100644 --- a/ScuffedMinecraft/ScuffedMinecraft.vcxproj +++ b/ScuffedMinecraft/ScuffedMinecraft.vcxproj @@ -158,6 +158,7 @@ + @@ -178,6 +179,7 @@ + @@ -196,8 +198,6 @@ - - diff --git a/ScuffedMinecraft/ScuffedMinecraft.vcxproj.filters b/ScuffedMinecraft/ScuffedMinecraft.vcxproj.filters index de7702e..92cc2ea 100644 --- a/ScuffedMinecraft/ScuffedMinecraft.vcxproj.filters +++ b/ScuffedMinecraft/ScuffedMinecraft.vcxproj.filters @@ -69,6 +69,9 @@ Source Files + + Source Files + @@ -125,18 +128,15 @@ Header Files + + Header Files + - - Resource Files - - - Resource Files - Resource Files diff --git a/ScuffedMinecraft/assets/shaders/fragment_shader.glsl b/ScuffedMinecraft/assets/shaders/fragment_shader.glsl index ed33dfb..890725d 100644 --- a/ScuffedMinecraft/assets/shaders/fragment_shader.glsl +++ b/ScuffedMinecraft/assets/shaders/fragment_shader.glsl @@ -19,5 +19,8 @@ void main() 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; } \ No newline at end of file diff --git a/ScuffedMinecraft/assets/shaders/vertex_shader.glsl b/ScuffedMinecraft/assets/shaders/vertex_shader.glsl index e15e933..46aea5d 100644 --- a/ScuffedMinecraft/assets/shaders/vertex_shader.glsl +++ b/ScuffedMinecraft/assets/shaders/vertex_shader.glsl @@ -15,13 +15,14 @@ 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 +const vec3 normals[] = vec3[]( + 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 + vec3( 0, -1, 0) // 6 ); void main() diff --git a/ScuffedMinecraft/assets/sprites/block_map.png b/ScuffedMinecraft/assets/sprites/block_map.png index b98bf1e..4018519 100644 Binary files a/ScuffedMinecraft/assets/sprites/block_map.png and b/ScuffedMinecraft/assets/sprites/block_map.png differ diff --git a/ScuffedMinecraft/assets/sprites/block_map.psd b/ScuffedMinecraft/assets/sprites/block_map.psd new file mode 100644 index 0000000..95fd05f Binary files /dev/null and b/ScuffedMinecraft/assets/sprites/block_map.psd differ diff --git a/ScuffedMinecraft/assets/sprites/direction_test.png b/ScuffedMinecraft/assets/sprites/direction_test.png deleted file mode 100644 index 6b4bd72..0000000 Binary files a/ScuffedMinecraft/assets/sprites/direction_test.png and /dev/null differ diff --git a/ScuffedMinecraft/assets/sprites/grass_block_side.png b/ScuffedMinecraft/assets/sprites/grass_block_side.png deleted file mode 100644 index 5ed9259..0000000 Binary files a/ScuffedMinecraft/assets/sprites/grass_block_side.png and /dev/null differ diff --git a/ScuffedMinecraft/imgui.ini b/ScuffedMinecraft/imgui.ini deleted file mode 100644 index d800693..0000000 --- a/ScuffedMinecraft/imgui.ini +++ /dev/null @@ -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 - diff --git a/ScuffedMinecraft/src/Application.cpp b/ScuffedMinecraft/src/Application.cpp index 03c8326..9a6c43c 100644 --- a/ScuffedMinecraft/src/Application.cpp +++ b/ScuffedMinecraft/src/Application.cpp @@ -88,7 +88,7 @@ int main() Shader shader("assets/shaders/vertex_shader.glsl", "assets/shaders/fragment_shader.glsl"); shader.use(); - shader.setFloat("texMultiplier", .5f); + shader.setFloat("texMultiplier", .25f); // Create texture unsigned int texture; @@ -181,13 +181,7 @@ int main() 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); - std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); - - std::cout << "Planet Update: " - << std::chrono::duration_cast(end - begin).count() - << "[ms]\n"; // Draw ImGui UI ImGui::Begin("Test"); @@ -237,7 +231,12 @@ void processInput(GLFWwindow* window) escapeDown = false; 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) camera.ProcessKeyboard(BACKWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) diff --git a/ScuffedMinecraft/src/Block.cpp b/ScuffedMinecraft/src/Block.cpp index 6e25ced..0552839 100644 --- a/ScuffedMinecraft/src/Block.cpp +++ b/ScuffedMinecraft/src/Block.cpp @@ -1,6 +1,7 @@ #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; 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, 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->topMinY = topMinY; diff --git a/ScuffedMinecraft/src/Block.h b/ScuffedMinecraft/src/Block.h index fd62de2..051f22d 100644 --- a/ScuffedMinecraft/src/Block.h +++ b/ScuffedMinecraft/src/Block.h @@ -6,9 +6,11 @@ public: char topMinX, topMinY, topMaxX, topMaxY; char bottomMinX, bottomMinY, bottomMaxX, bottomMaxY; 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, 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); }; \ No newline at end of file diff --git a/ScuffedMinecraft/src/Blocks.h b/ScuffedMinecraft/src/Blocks.h index 00c1bd9..f2a58bd 100644 --- a/ScuffedMinecraft/src/Blocks.h +++ b/ScuffedMinecraft/src/Blocks.h @@ -7,14 +7,27 @@ namespace Blocks { const std::vector blocks{ - Block(0, 0, 0, 0), // Air block - Block(0, 0, 1, 1), // Dirt block + Block(0, 0, 0, 0, true), // Air 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, 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 @@ -22,6 +35,15 @@ namespace Blocks AIR = 0, DIRT_BLOCK = 1, 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, }; } \ No newline at end of file diff --git a/ScuffedMinecraft/src/Camera.cpp b/ScuffedMinecraft/src/Camera.cpp index 00836c1..1b2ddf2 100644 --- a/ScuffedMinecraft/src/Camera.cpp +++ b/ScuffedMinecraft/src/Camera.cpp @@ -41,6 +41,13 @@ void Camera::ProcessKeyboard(Camera_Movement direction, float deltaTime) Position += Up * velocity; if (direction == DOWN) 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. diff --git a/ScuffedMinecraft/src/Camera.h b/ScuffedMinecraft/src/Camera.h index dffdb67..2fa7448 100644 --- a/ScuffedMinecraft/src/Camera.h +++ b/ScuffedMinecraft/src/Camera.h @@ -9,7 +9,8 @@ enum Camera_Movement { LEFT, RIGHT, UP, - DOWN + DOWN, + FORWARD_NO_Y }; // Default camera values diff --git a/ScuffedMinecraft/src/Chunk.cpp b/ScuffedMinecraft/src/Chunk.cpp index 1ca955e..9759b22 100644 --- a/ScuffedMinecraft/src/Chunk.cpp +++ b/ScuffedMinecraft/src/Chunk.cpp @@ -60,184 +60,258 @@ void Chunk::GenerateChunk() const Block* block = &Blocks::blocks[chunkData[index]]; - // North + if (block->billboard) { - int northBlock; - if (z > 0) - { - int northIndex = x * chunkSize * chunkSize + (z - 1) * chunkSize + y; - northBlock = chunkData[northIndex]; - } - else - { - int northIndex = x * chunkSize * chunkSize + (chunkSize - 1) * chunkSize + y; - northBlock = northData[northIndex]; - } + vertices.push_back(Vertex(x + .85355f, y + 0, z + .85355f, block->sideMinX, block->sideMinY, 6)); + 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)); + vertices.push_back(Vertex(x + .14645f, y + 1, z + .14645f, block->sideMaxX, block->sideMaxY, 6)); - if (northBlock == 0) - { - 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); + indices.push_back(currentVertex + 1); + indices.push_back(currentVertex + 0); + indices.push_back(currentVertex + 2); + indices.push_back(currentVertex + 3); + currentVertex += 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; - } + vertices.push_back(Vertex(x + .14645f, y + 0, z + .14645f, block->sideMinX, block->sideMinY, 6)); + vertices.push_back(Vertex(x + .85355f, y + 0, z + .85355f, block->sideMaxX, block->sideMinY, 6)); + vertices.push_back(Vertex(x + .14645f, y + 1, z + .14645f, block->sideMinX, block->sideMaxY, 6)); + vertices.push_back(Vertex(x + .85355f, 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; + + 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; } - - // South + else { - int southBlock; - if (z < chunkSize - 1) + // North { - int southIndex = x * chunkSize * chunkSize + (z + 1) * chunkSize + y; - southBlock = chunkData[southIndex]; - } - else - { - int southIndex = x * chunkSize * chunkSize + 0 * chunkSize + y; - southBlock = southData[southIndex]; - } - if (southBlock == 0) - { - 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)); + int northBlock; + if (z > 0) + { + int northIndex = x * chunkSize * chunkSize + (z - 1) * chunkSize + y; + northBlock = chunkData[northIndex]; + } + else + { + int northIndex = x * chunkSize * chunkSize + (chunkSize - 1) * chunkSize + y; + northBlock = northData[northIndex]; + } - 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; - } - } + const Block* northBlockType = &Blocks::blocks[northBlock]; - // West - { - int westBlock; - if (x > 0) - { - int blockIndex = (x - 1) * chunkSize * chunkSize + z * chunkSize + y; - 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)); + if (northBlockType->transparent) + { + 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); - indices.push_back(currentVertex + 1); - indices.push_back(currentVertex + 0); - indices.push_back(currentVertex + 2); - indices.push_back(currentVertex + 3); - currentVertex += 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; + } } - } - // East - { - int eastBlock; - if (x < chunkSize - 1) + // South { - int blockIndex = (x + 1) * chunkSize * chunkSize + z * chunkSize + y; - eastBlock = chunkData[blockIndex]; - } - else - { - int blockIndex = 0 * chunkSize * chunkSize + z * chunkSize + y; - eastBlock = eastData[blockIndex]; - } - if (eastBlock == 0) - { - 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)); + int southBlock; + if (z < chunkSize - 1) + { + int southIndex = x * chunkSize * chunkSize + (z + 1) * chunkSize + y; + southBlock = chunkData[southIndex]; + } + else + { + int southIndex = x * chunkSize * chunkSize + 0 * chunkSize + y; + southBlock = southData[southIndex]; + } - 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; - } - } + const Block* southBlockType = &Blocks::blocks[southBlock]; - // 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]; - } - 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)); + if (southBlockType->transparent) + { + 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); - indices.push_back(currentVertex + 1); - indices.push_back(currentVertex + 0); - indices.push_back(currentVertex + 2); - indices.push_back(currentVertex + 3); - currentVertex += 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) + // West { - int blockIndex = x * chunkSize * chunkSize + z * chunkSize + (y + 1); - topBlock = chunkData[blockIndex]; - } - else - { - int blockIndex = x * chunkSize * chunkSize + z * chunkSize + 0; - topBlock = upData[blockIndex]; - } - if (topBlock == 0) - { - 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)); + int westBlock; + if (x > 0) + { + int blockIndex = (x - 1) * chunkSize * chunkSize + z * chunkSize + y; + westBlock = chunkData[blockIndex]; + } + else + { + int blockIndex = (chunkSize - 1) * chunkSize * chunkSize + z * chunkSize + y; + westBlock = westData[blockIndex]; + } - 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; + const Block* westBlockType = &Blocks::blocks[westBlock]; + + if (westBlockType->transparent) + { + 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); + 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); 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); glVertexAttribPointer(1, 2, GL_BYTE, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texGridX)); glEnableVertexAttribArray(1); diff --git a/ScuffedMinecraft/src/Chunk.h b/ScuffedMinecraft/src/Chunk.h index 549472c..bbf8152 100644 --- a/ScuffedMinecraft/src/Chunk.h +++ b/ScuffedMinecraft/src/Chunk.h @@ -6,11 +6,11 @@ struct Vertex { - char posX, posY, posZ; + float posX, posY, posZ; char texGridX, texGridY; 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; posY = _posY; diff --git a/ScuffedMinecraft/src/SurfaceFeature.cpp b/ScuffedMinecraft/src/SurfaceFeature.cpp new file mode 100644 index 0000000..7657851 --- /dev/null +++ b/ScuffedMinecraft/src/SurfaceFeature.cpp @@ -0,0 +1,11 @@ +#include "SurfaceFeature.h" + +SurfaceFeature::SurfaceFeature(NoiseSettings _noiseSettings, std::vector _blocks, std::vector _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) +{ + +} \ No newline at end of file diff --git a/ScuffedMinecraft/src/SurfaceFeature.h b/ScuffedMinecraft/src/SurfaceFeature.h new file mode 100644 index 0000000..ec14fd7 --- /dev/null +++ b/ScuffedMinecraft/src/SurfaceFeature.h @@ -0,0 +1,17 @@ +#pragma once + +#include "NoiseSettings.h" +#include + +struct SurfaceFeature +{ + NoiseSettings noiseSettings; + std::vector blocks; + std::vector replaceBlock; + int sizeX, sizeY, sizeZ; + int offsetX, offsetY, offsetZ; + + SurfaceFeature(NoiseSettings _noiseSettings, std::vector _blocks, std::vector _replaceBlock, + int _sizeX, int _sizeY, int _sizeZ, + int _offsetX, int _offsetY, int _offsetZ); +}; \ No newline at end of file diff --git a/ScuffedMinecraft/src/WorldGen.cpp b/ScuffedMinecraft/src/WorldGen.cpp index f72f70a..87d9036 100644 --- a/ScuffedMinecraft/src/WorldGen.cpp +++ b/ScuffedMinecraft/src/WorldGen.cpp @@ -1,6 +1,7 @@ #include "WorldGen.h" #include +#include #include #include "Blocks.h" @@ -8,25 +9,197 @@ void WorldGen::GenerateChunkData(int chunkX, int chunkY, int chunkZ, int chunkSize, std::vector* chunkData) { // Init noise - OSN::Noise<2> noise2D; - OSN::Noise<3> noise3D; + static OSN::Noise<2> noise2D; + static OSN::Noise<3> noise3D; // Init noise settings - NoiseSettings surfaceSettings[] { + static NoiseSettings surfaceSettings[]{ { 0.01f, 20.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 } }; - 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 } }; - 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 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; + } + } + } + } } \ No newline at end of file diff --git a/ScuffedMinecraft/src/WorldGen.h b/ScuffedMinecraft/src/WorldGen.h index e198a55..5b09d1b 100644 --- a/ScuffedMinecraft/src/WorldGen.h +++ b/ScuffedMinecraft/src/WorldGen.h @@ -2,8 +2,10 @@ #include #include "NoiseSettings.h" +#include "SurfaceFeature.h" namespace WorldGen { void GenerateChunkData(int chunkX, int chunkY, int chunkZ, int chunkSize, std::vector* chunkData); + } \ No newline at end of file