diff --git a/MCGL/MCGL.vcxproj b/MCGL/MCGL.vcxproj index b7e1286..4168d15 100644 --- a/MCGL/MCGL.vcxproj +++ b/MCGL/MCGL.vcxproj @@ -19,13 +19,13 @@ + - @@ -324,10 +324,12 @@ + + @@ -338,6 +340,7 @@ + @@ -481,6 +484,7 @@ + diff --git a/MCGL/MCGL.vcxproj.filters b/MCGL/MCGL.vcxproj.filters index 5dcab20..323640b 100644 --- a/MCGL/MCGL.vcxproj.filters +++ b/MCGL/MCGL.vcxproj.filters @@ -909,9 +909,6 @@ Header Files - - Header Files - Header Files @@ -930,6 +927,12 @@ Header Files + + Header Files + + + Header Files + @@ -968,6 +971,12 @@ Source Files + + Source Files + + + Source Files + @@ -1399,5 +1408,8 @@ Resource Files + + Resource Files + \ No newline at end of file diff --git a/MCGL/res/shaders/shader.glsl b/MCGL/res/shaders/shader.glsl index cbfa47a..3eb9c99 100644 --- a/MCGL/res/shaders/shader.glsl +++ b/MCGL/res/shaders/shader.glsl @@ -2,9 +2,11 @@ #version 330 core layout (location = 0) in vec3 aPos; +//layout (location = 1) in uint aTextureID; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; +//out uint TextureID; uniform mat4 view; uniform mat4 projection; @@ -12,7 +14,7 @@ uniform mat4 model; void main() { - gl_Position = projection * view * model * vec4(aPos, 1.0); + gl_Position = projection * view * model * vec4(aPos.xyz, 1.0); TexCoord = aTexCoord; } @@ -22,15 +24,24 @@ void main() out vec4 FragColor; in vec2 TexCoord; - +//in uint aTextureID; +//in vec3 TexCoord; uniform sampler2D texture1; uniform float transparency; void main() { // FragColor = mix(texture(texture1, vec2(TexCoord.x / 2, TexCoord.y)), texture(texture2, TexCoord), transparency); - FragColor = texture(texture1, TexCoord); + //float texWidth = 384.0f; + //float texHeight = 320.0f; + // + //vec2 xy = vec2(TexCoord.x / texWidth, TexCoord.y / texHeight); + //FragColor = texture(texture1, xy); + + FragColor = texelFetch(texture1, ivec2(TexCoord.x, TexCoord.y), 0); + //FragColor = texture2D((fract(TexCoord.x + TexCoord.z) + TexCoord.w) / 16.0, TexCoord.y); + //FragColor = vec4(TexCoord.xyz, 1); } diff --git a/MCGL/res/textures/blocks/tilemap.png b/MCGL/res/textures/blocks/tilemap.png new file mode 100644 index 0000000..0e8b707 Binary files /dev/null and b/MCGL/res/textures/blocks/tilemap.png differ diff --git a/MCGL/src/MCGL.cpp b/MCGL/src/MCGL.cpp index c07bca9..c769a69 100644 --- a/MCGL/src/MCGL.cpp +++ b/MCGL/src/MCGL.cpp @@ -4,7 +4,7 @@ #define HEIGHT 720 //#------------------Camera---------------------#// -Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); +Camera camera(glm::vec3(0.0f, 0.0f, 0.0f)); float lastX = WIDTH / 2.0f; float lastY = HEIGHT / 2.0f; bool firstMouse = true; @@ -13,6 +13,29 @@ bool firstMouse = true; float deltaTime = 0.0f; float lastFrame = 0.0f; +double lastTime = 0; +int nbFrames = 0; + +void showFPS(GLFWwindow* pWindow) +{ + double currentTime = glfwGetTime(); + double delta = currentTime - lastTime; + + nbFrames++; + if (delta >= 1.0) + { + double fps = double(nbFrames) / delta; + + std::stringstream ss; + ss << "FPS: " << fps; + + glfwSetWindowTitle(pWindow, ss.str().c_str()); + + nbFrames = 0; + lastTime = currentTime; + } +} + void mouseMovementCallback(GLFWwindow* window, double xposIn, double yposIn) { float xpos = static_cast(xposIn); @@ -26,7 +49,7 @@ void mouseMovementCallback(GLFWwindow* window, double xposIn, double yposIn) } float xoffset = xpos - lastX; - float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top + float yoffset = lastY - ypos; //y goes from bottom to top lastX = xpos; lastY = ypos; @@ -37,8 +60,6 @@ void mouseMovementCallback(GLFWwindow* window, double xposIn, double yposIn) MCGL::MCGL(): window("MCGL", 1280, 720) { glfwSetInputMode(window.Get(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); - - //called when cursor is moved glfwSetCursorPosCallback(window.Get(), mouseMovementCallback); } @@ -49,69 +70,71 @@ MCGL::~MCGL() void MCGL::Run() { - //setting up the renderer Shader cubeShader("C:\\Dev\\OpenGL\\MCGL\\MCGL\\res\\shaders\\shader.glsl"); cubeShader.Use(); - //#------------------Shaders and Coordinates/shapes to be rendered-------------------#// - Cube cube; - VertexArray VAO; - VertexBuffer VBO(&(cube.getVertices()[0]), cube.getSizeOfData()); - VertexBufferLayout layout; + Chunk c { 0,0 }; + c.Update(); - layout.PushElement(GL_FLOAT, 3); - layout.PushElement(GL_FLOAT, 2); + std::vector data = c.RetrieveData(); + + //std::ranges::copy(data1, std::back_inserter(data)); + VertexArray va; + VertexBuffer chunkDataBuffer(&data[0], data.size() * sizeof(char)); + VertexBufferLayout blockDataLayout; + + blockDataLayout.PushElementByte(3, false); + blockDataLayout.PushElementByte(2, false); - VAO.AddBuffer(VBO, layout); + va.AddBuffer(chunkDataBuffer, blockDataLayout); - CubeRenderer cubeRenderer(VAO, cubeShader); - //#------------------Textures-------------------#// - Texture blockTex("C:\\Dev\\OpenGL\\MCGL\\MCGL\\res\\textures\\blocks\\grass_block.png"); + Texture blockTex("C:\\Dev\\OpenGL\\MCGL\\MCGL\\res\\textures\\blocks\\tilemap.png"); blockTex.Bind(); + cubeShader.SetUniform1i("texture1", 0); //#------------------Transformations-------------------#// - glm::mat4 viewMat, projectionMat = glm::mat4(1.0f); + glm::mat4 viewMat, projectionMat, modelMat; + modelMat = projectionMat = glm::mat4(1.0f); - projectionMat = glm::perspective(glm::radians(60.0f), (float)WIDTH / (float)HEIGHT, 0.1f, 100.0f); + projectionMat = glm::perspective(glm::radians(60.0f), (float)WIDTH / (float)HEIGHT, 0.1f, 200.0f); cubeShader.SetUniformMat4("projection", projectionMat); + modelMat = glm::translate(modelMat, glm::vec3(0,0,0)); + cubeShader.SetUniformMat4("model", modelMat); + //#------------------Additional Configuration-------------------#// glEnable(GL_DEPTH_TEST); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - while (!window.ShouldClose()) { { - float currentFrame = glfwGetTime(); + double currentFrame = glfwGetTime(); deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; } + showFPS(window.Get()); ProcessInput(); - + glm::vec3 clearColor = glm::normalize(glm::vec3(0, 230, 240)); glClearColor(clearColor.r, clearColor.g, clearColor.b, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); cubeShader.Use(); + va.Bind(); viewMat = camera.GetViewMatrix(); cubeShader.SetUniformMat4("view", viewMat); - for (int y = 0; y < 3; y++) - for (int z = 0; z < 16; z++) - for (int x = 0; x < 16; x++) - cubeRenderer.DrawCube(glm::vec3(0.0f - x, 0.0f - y, 0.0f - z), blockTex); - + c.Render(va, cubeShader); + window.SwapBuffers(); window.PollEvents(); } - - } void MCGL::ProcessInput() diff --git a/MCGL/src/MCGL.h b/MCGL/src/MCGL.h index d7294fd..ddbcb93 100644 --- a/MCGL/src/MCGL.h +++ b/MCGL/src/MCGL.h @@ -3,11 +3,13 @@ #include #include #include +#include #include "Texture.h" #include "graphics/Shader.h" #include "graphics/VertexArray.h" #include "graphics/CubeRenderer.h" +#include "graphics/Chunk.h" #include "camera/Camera.h" #include "Window.h" diff --git a/MCGL/src/Texture.cpp b/MCGL/src/Texture.cpp index d714646..8424f20 100644 --- a/MCGL/src/Texture.cpp +++ b/MCGL/src/Texture.cpp @@ -1,5 +1,7 @@ #include "Texture.h" +uint32_t Texture::m_nOfTextures = 0; + Texture::Texture(const std::string& path) { glGenTextures(1, &m_TextureID); @@ -30,7 +32,10 @@ Texture::Texture(const std::string& path) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE0, m_TextureID); + + glBindTexture(GL_TEXTURE0 + m_nOfTextures, m_TextureID); + + m_nOfTextures++; stbi_image_free(data); } diff --git a/MCGL/src/Texture.h b/MCGL/src/Texture.h index 8c17e1d..e8c5c3e 100644 --- a/MCGL/src/Texture.h +++ b/MCGL/src/Texture.h @@ -27,6 +27,7 @@ class Texture { private: uint32_t m_TextureID; + static uint32_t m_nOfTextures; }; diff --git a/MCGL/src/camera/Camera.h b/MCGL/src/camera/Camera.h index 9b055f7..29f42e8 100644 --- a/MCGL/src/camera/Camera.h +++ b/MCGL/src/camera/Camera.h @@ -18,7 +18,7 @@ enum class Camera_Movement { // Default camera values const float YAW = -90.0f; const float PITCH = 0.0f; -const float MOVEMENT_SPEED = 5.0f; +const float MOVEMENT_SPEED = 10.0f; const float SENSITIVITY = 0.1f; const float ZOOM = 45.0f; diff --git a/MCGL/src/graphics/Chunk.cpp b/MCGL/src/graphics/Chunk.cpp new file mode 100644 index 0000000..15a7345 --- /dev/null +++ b/MCGL/src/graphics/Chunk.cpp @@ -0,0 +1,174 @@ +#include "Chunk.h" +//std::unordered_set Chunk::s_OccupiedChunkPositions; + +Chunk::Chunk(int xStart, int zStart) +{ + //xStart *= XC; + //zStart *= ZC; + + /*s_OccupiedChunkPositions.insert(glm::ivec2(xStart, zStart)); + + if (s_OccupiedChunkPositions.contains(glm::ivec2(xStart, zStart))) + { + std::cout << "Cant load chunk at: (" << xStart << ", " << zStart << ") ALREADY EXISTS!\n"; + return; + }*/ + + memset(m_Blocks, (int)BlockType::DIRT, sizeof(m_Blocks)); + + for (int x = xStart; x < XC; x++) { + for (int y = 0; y < YC; y++) { + for (int z = zStart; z < ZC; z++) { + m_Blocks[x][y][z] = (BlockType)(rand() % 4); + } + } + } + + changed = true; +} + +void Chunk::Render(VertexArray& va, Shader& shader) +{ + if (changed) + Update(); + + va.Bind(); + shader.Use(); + + // 5 bytes for each vertix 3 for position and 2 for texture + glDrawArrays(GL_TRIANGLES, 0, m_BlocksVertices.size() * 5); +} + + + +std::vector Chunk::RetrieveData() +{ + std::cout << m_BlocksVertices.size(); + + if (m_BlocksVertices.size() == 0) + { + std::cout << "Chunk is empty!"; + exit(-1); + } + + std::vector vData; + vData.reserve(m_BlocksVertices.size() * 5); + + for (auto& vertex : m_BlocksVertices) + { + vData.push_back(vertex.x); + vData.push_back(vertex.y); + vData.push_back(vertex.z); + vData.push_back(vertex.texX); + vData.push_back(vertex.texY); + //vData.push_back(vertex.blockText); + } + + return vData; +} + + +void Chunk::Update() +{ + changed = false; + + m_BlocksVertices.clear(); + //36 because each block has 36 vertices, XC * YC * ZC = number of blocks + m_BlocksVertices.reserve(XC * YC * ZC * 36); + + for (int x = 0; x < XC; x++) { + for (int y = 0; y < YC; y++) { + for (int z = 0; z < ZC; z++) { + SetBlockAt(x,y,z); + } + } + } + +} + + +void Chunk::SetBlockAt(int x, int y, int z) +{ + if (x >= XC || y >= YC || z >= ZC) + return; + + if (m_Blocks[x][y][z] == BlockType::AIR) + return; + + int blockTexture = (uint32_t)m_Blocks[x][y][z]; + + //Making sure grass stays on top only + if (blockTexture == (int)BlockType::GRASS) + if ((y < YC - 1 && m_Blocks[x][y + 1][z] != BlockType::AIR)) + blockTexture = (int)BlockType::DIRT; + + //everything under represents vertices for 1 cube (block) + pixel texture coardinates and finally, the texture used + + //left face + if (x == 0 || (x > 0 && m_Blocks[x - 1][y][z] == BlockType::AIR)) + { + m_BlocksVertices.push_back({ x, y, z + 1, 0,0 , blockTexture }); + m_BlocksVertices.push_back({ x, y, z, 32,0 , blockTexture }); + m_BlocksVertices.push_back({ x, y + 1, z, 32,32 , blockTexture }); + m_BlocksVertices.push_back({ x, y + 1, z, 32,32 , blockTexture }); + m_BlocksVertices.push_back({ x, y + 1, z + 1, 0,32 , blockTexture }); + m_BlocksVertices.push_back({ x, y, z + 1, 0,0 , blockTexture }); + } + + //right face + if (x == XC - 1 || (x <= (XC - 1) && m_Blocks[x + 1][y][z] == BlockType::AIR)) + { + m_BlocksVertices.push_back({ x + 1, y, z + 1, 0,0 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y, z, 32,0 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y + 1, z, 32,32 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y + 1, z, 32,32 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y + 1, z + 1, 0,32 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y, z + 1, 0,0 , blockTexture }); + } + + //front face // ISSUE + if (z == 0 || (z > 0 && m_Blocks[x][y][z - 1] == BlockType::AIR)) + { + m_BlocksVertices.push_back({ x, y, z, 0,0 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y, z, 32,0 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y + 1, z, 32,32 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y + 1, z, 32,32 , blockTexture }); + m_BlocksVertices.push_back({ x, y + 1, z, 0,32 , blockTexture }); + m_BlocksVertices.push_back({ x, y, z, 0,0 , blockTexture }); + } + + + //back face + if (z == ZC - 1 || (z < ZC - 1 && m_Blocks[x][y][z + 1] == BlockType::AIR)) + { + m_BlocksVertices.push_back({ x, y, z + 1, 0,0 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y, z + 1, 32,0 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y + 1, z + 1, 32,32 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y + 1, z + 1, 32,32 , blockTexture }); + m_BlocksVertices.push_back({ x, y + 1, z + 1, 0,32 , blockTexture }); + m_BlocksVertices.push_back({ x, y, z + 1, 0,0 , blockTexture }); + + } + + //top face + if (y == YC - 1 || (y < YC - 1 && m_Blocks[x][y + 1][z] == BlockType::AIR)) + { + m_BlocksVertices.push_back({ x, y + 1, z, 64,0 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y + 1, z, 96,0 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y + 1, z + 1, 96,32 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y + 1, z + 1, 96,32 , blockTexture }); + m_BlocksVertices.push_back({ x, y + 1, z + 1, 64,32 , blockTexture }); + m_BlocksVertices.push_back({ x, y + 1, z, 64,0 , blockTexture }); + } + + //bottom face + if (y == 0 || (y > 0 && m_Blocks[x][y - 1][z] == BlockType::AIR)) + { + m_BlocksVertices.push_back({ x, y, z, 32,0 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y, z, 64,0 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y, z + 1, 64,32 , blockTexture }); + m_BlocksVertices.push_back({ x + 1, y, z + 1, 64,32 , blockTexture }); + m_BlocksVertices.push_back({ x, y, z + 1, 32,32 , blockTexture }); + m_BlocksVertices.push_back({ x, y, z, 32,0 , blockTexture }); + } +} \ No newline at end of file diff --git a/MCGL/src/graphics/Chunk.h b/MCGL/src/graphics/Chunk.h new file mode 100644 index 0000000..b33af84 --- /dev/null +++ b/MCGL/src/graphics/Chunk.h @@ -0,0 +1,53 @@ +#pragma once + +#include +#include "CubeRenderer.h" +#include "Shader.h" +#include +#include + +#define XC 16 +#define YC 16 +#define ZC 16 + +enum class BlockType : int { + AIR, GRASS, DIRT, STONE +}; + + +struct bvec5 { + bvec5(int x, int y, int z, int texX, int texY, int blockTexture) + { + this->x = x; + this->y = y; + this->z = z; + + this->texX = texX; + //up by 32 depending on tilemap selected texture + this->texY = texY + (32 * (blockTexture - 1)); + } + + GLbyte x, y, z, texX, texY; +}; + +class Chunk +{ +public: + Chunk(int, int); + + void Update(); + + void Render(VertexArray& va, Shader& shader); + + void SetBlockAt(int x, int y, int z); + + auto& GetVertices() { return m_BlocksVertices; } + std::vector RetrieveData(); + +private: + static std::unordered_set s_OccupiedChunkPositions; + BlockType m_Blocks[XC][YC][ZC]; + std::vector m_BlocksVertices; + bool changed; +}; + diff --git a/MCGL/src/graphics/CubeRenderer.cpp b/MCGL/src/graphics/CubeRenderer.cpp index a7549c4..721713a 100644 --- a/MCGL/src/graphics/CubeRenderer.cpp +++ b/MCGL/src/graphics/CubeRenderer.cpp @@ -1,5 +1,7 @@ #include "CubeRenderer.h" +Cube cube; + CubeRenderer::CubeRenderer(const VertexArray& va, const Shader& shader) : m_CubeVA(va), m_Shader(shader) { } @@ -16,4 +18,9 @@ void CubeRenderer::DrawCube(const glm::vec3& position, Texture& texture) glDrawArrays(GL_TRIANGLES, 0, 36); } +void CubeRenderer::DrawFaceOfCube(const glm::vec3& position, Cube::Face face) +{ + +} + diff --git a/MCGL/src/graphics/CubeRenderer.h b/MCGL/src/graphics/CubeRenderer.h index 2444db7..e494e3a 100644 --- a/MCGL/src/graphics/CubeRenderer.h +++ b/MCGL/src/graphics/CubeRenderer.h @@ -5,7 +5,6 @@ #include "Shader.h" #include "../world/Cube.h" #include "../Texture.h" - #include "../vendor/glm/glm.hpp" class CubeRenderer @@ -14,6 +13,7 @@ class CubeRenderer CubeRenderer(const VertexArray& va, const Shader& shader); void DrawCube(const glm::vec3& position, Texture& texture); + void DrawFaceOfCube(const glm::vec3& position, Cube::Face face); void SetVertexArray(VertexArray& va) { m_CubeVA = va; } void SetShader(const Shader& shader) { m_Shader = shader; } diff --git a/MCGL/src/graphics/VertexArray.cpp b/MCGL/src/graphics/VertexArray.cpp index d626fcd..d33229b 100644 --- a/MCGL/src/graphics/VertexArray.cpp +++ b/MCGL/src/graphics/VertexArray.cpp @@ -5,7 +5,7 @@ VertexArray::VertexArray() glGenVertexArrays(1, &vaoID); this->Bind(); } - +#include void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout) { this->Bind(); @@ -17,16 +17,35 @@ void VertexArray::AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& la for (const auto& element : layout.GetElements()) { glEnableVertexAttribArray(index); - glVertexAttribPointer( - index, - element.countOfValues, - element.valuesType, - element.isNormalized, - layout.GetStride(), - (void*) offset - ); - offset += element.countOfValues * sizeof(element.valuesType); + if (element.valuesType == TYPE::BYTE) + { + glVertexAttribPointer( + index, + element.countOfValues, + GL_BYTE, + element.isNormalized, + layout.GetStride(), + (void*)offset + ); + } + else + { + glVertexAttribPointer( + index, + element.countOfValues, + GL_FLOAT, + element.isNormalized, + layout.GetStride(), + (void*) offset + ); + } + + if (element.valuesType == TYPE::BYTE) + offset += element.countOfValues * sizeof(char); + else + offset += element.countOfValues * sizeof(float); + index++; } } \ No newline at end of file diff --git a/MCGL/src/graphics/VertexArray.h b/MCGL/src/graphics/VertexArray.h index cc55d0c..ae43573 100644 --- a/MCGL/src/graphics/VertexArray.h +++ b/MCGL/src/graphics/VertexArray.h @@ -1,18 +1,16 @@ #pragma once #include "VertexBuffer.h" - class VertexArray { public: VertexArray(); ~VertexArray() { glDeleteVertexArrays(1, &vaoID); } - + void Bind() { glBindVertexArray(vaoID); } void Unbind() { glBindVertexArray(0); } uint32_t Get() { return vaoID; }; void AddBuffer(const VertexBuffer& vb, const VertexBufferLayout& layout); - private: unsigned int vaoID; diff --git a/MCGL/src/graphics/VertexBuffer.cpp b/MCGL/src/graphics/VertexBuffer.cpp index b8762ca..f5efa45 100644 --- a/MCGL/src/graphics/VertexBuffer.cpp +++ b/MCGL/src/graphics/VertexBuffer.cpp @@ -1,6 +1,6 @@ #include "VertexBuffer.h" - -VertexBuffer::VertexBuffer(const void* data, unsigned int size) +#include +VertexBuffer::VertexBuffer(const void* data, unsigned long long size) { glGenBuffers(1, &ID); glBindBuffer(GL_ARRAY_BUFFER, ID); @@ -24,6 +24,6 @@ void VertexBuffer::UnBind() const void VertexBuffer::UpdateData(const void* data, unsigned int size) { - glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, size, data, GL_DYNAMIC_DRAW); } diff --git a/MCGL/src/graphics/VertexBuffer.h b/MCGL/src/graphics/VertexBuffer.h index 2d886e6..2a3808d 100644 --- a/MCGL/src/graphics/VertexBuffer.h +++ b/MCGL/src/graphics/VertexBuffer.h @@ -6,8 +6,8 @@ class VertexBuffer { public: VertexBufferLayout layout; - - VertexBuffer(const void* data, unsigned int size); + VertexBuffer() = default; + VertexBuffer(const void* data, unsigned long long size); ~VertexBuffer(); void Bind() const; diff --git a/MCGL/src/graphics/VertexBufferLayout.h b/MCGL/src/graphics/VertexBufferLayout.h index 8b0c6b4..a96a8f0 100644 --- a/MCGL/src/graphics/VertexBufferLayout.h +++ b/MCGL/src/graphics/VertexBufferLayout.h @@ -1,10 +1,14 @@ #pragma once #include +enum class TYPE { + FLOAT, BYTE +}; + struct VertexBufferElement { unsigned int countOfValues; - unsigned int valuesType; + TYPE valuesType; unsigned int isNormalized; }; @@ -19,12 +23,19 @@ class VertexBufferLayout m_Elements.push_back({layoutIndex, nOfValues, T, GL_FALSE, stride, offset}); }*/ - void PushElement(uint32_t type, uint32_t countOfValues) + void PushElementFloat(uint32_t countOfValues) { - m_Elements.push_back({ countOfValues, type, GL_FALSE }); + m_Elements.push_back({ countOfValues, TYPE::FLOAT, GL_FALSE }); m_Stride += countOfValues * sizeof(float); } + + void PushElementByte(uint32_t countOfValues, bool normalized) + { + m_Elements.push_back({ countOfValues, TYPE::BYTE, normalized }); + m_Stride += countOfValues * sizeof(char); + } + const auto& GetElements() const { return m_Elements; diff --git a/MCGL/src/graphics/Worldchunk.cpp b/MCGL/src/graphics/Worldchunk.cpp new file mode 100644 index 0000000..4a46a5e --- /dev/null +++ b/MCGL/src/graphics/Worldchunk.cpp @@ -0,0 +1 @@ +#include "Worldchunk.h" diff --git a/MCGL/src/graphics/Worldchunk.h b/MCGL/src/graphics/Worldchunk.h new file mode 100644 index 0000000..fe05053 --- /dev/null +++ b/MCGL/src/graphics/Worldchunk.h @@ -0,0 +1,13 @@ +#pragma once +#include "Chunk.h" + +#define MAX_CHUNKS 8 + +class Worldchunk +{ +public: + +private: + std::array chunks; +}; + diff --git a/MCGL/src/world/Chunk.h b/MCGL/src/world/Chunk.h deleted file mode 100644 index 1fe6df4..0000000 --- a/MCGL/src/world/Chunk.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include "vendor/glm/glm.hpp" -#include "vendor/glm/gtc/matrix_transform.hpp" -#include "vendor/glm/gtc/type_ptr.hpp" - -#define CHUNK_XYZ_RANGE 16 - -class Chunk -{ -public: - - //a chunk is a set of blocks with unique positions - //the set is 16 * 16 * 16 size - //a chunk must have a position in the world, -}; \ No newline at end of file diff --git a/MCGL/src/world/Cube.cpp b/MCGL/src/world/Cube.cpp index e0329fa..842a5c7 100644 --- a/MCGL/src/world/Cube.cpp +++ b/MCGL/src/world/Cube.cpp @@ -3,62 +3,59 @@ Cube::Cube() { - - - - m_CubeFacesData[(uint32_t)Face::BACK] = new float[] { - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 1.0f / 3.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 1.0f / 3.0f, 1.0f, //back face - 0.5f, 0.5f, -0.5f, 1.0f / 3.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f + m_CubeFacesData[Face::BACK] = new float[] { + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 1.0f / 3.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f / 3.0f, 1.0f, //back face + 0.5f, 0.5f, -0.5f, 1.0f / 3.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f }; - m_CubeFacesData[(uint32_t)Face::FRONT] = new float[] { - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 1.0f / 3.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f / 3.0f, 1.0f, //front face - 0.5f, 0.5f, 0.5f, 1.0f / 3.0f, 1.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f + m_CubeFacesData[Face::FRONT] = new float[] { + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f / 3.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f / 3.0f, 1.0f, //front face + 0.5f, 0.5f, 0.5f, 1.0f / 3.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f }; - m_CubeFacesData[(uint32_t)Face::LEFT] = new float[] { - -0.5f, 0.5f, 0.5f, 1.0f / 3.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, //left face - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, - -0.5f, -0.5f, 0.5f, 1.0f / 3.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 1.0f / 3.0f, 1.0f + m_CubeFacesData[Face::LEFT] = new float[] { + -0.5f, 0.5f, 0.5f, 1.0f / 3.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, //left face + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + -0.5f, -0.5f, 0.5f, 1.0f / 3.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 1.0f / 3.0f, 1.0f }; - m_CubeFacesData[(uint32_t)Face::RIGHT] = new float[] { - 0.5f, 0.5f, 0.5f, 1.0f / 3.0f, 1.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, //right face - 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 1.0f / 3.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f / 3.0f, 1.0f + m_CubeFacesData[Face::RIGHT] = new float[] { + 0.5f, 0.5f, 0.5f, 1.0f / 3.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, //right face + 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f / 3.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f / 3.0f, 1.0f }; - m_CubeFacesData[(uint32_t)Face::BOTTOM] = new float[] { - -0.5f, -0.5f, -0.5f, 1.0f / 3.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 2.0f / 3.0f, 0.0f, - 0.5f, -0.5f, 0.5f, 2.0f / 3.0f, 1.0f, //bottom face - 0.5f, -0.5f, 0.5f, 2.0f / 3.0f, 1.0f, - -0.5f, -0.5f, 0.5f, 1.0f / 3.0f, 1.0f, - -0.5f, -0.5f, -0.5f, 1.0f / 3.0f, 0.0f + m_CubeFacesData[Face::BOTTOM] = new float[] { + -0.5f, -0.5f, -0.5f, 1.0f / 3.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 2.0f / 3.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 2.0f / 3.0f, 1.0f, //bottom face + 0.5f, -0.5f, 0.5f, 2.0f / 3.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 1.0f / 3.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 1.0f / 3.0f, 0.0f }; - m_CubeFacesData[(uint32_t)Face::TOP] = new float[] { - -0.5f, 0.5f, -0.5f, 2.0f / 3.0f, 0.f, - 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, //top face - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, - -0.5f, 0.5f, 0.5f, 2.0f / 3.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 2.0f / 3.0f, 0.f, + m_CubeFacesData[Face::TOP] = new float[] { + -0.5f, 0.5f, -0.5f, 2.0f / 3.0f, 0.f, + 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, //top face + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 2.0f / 3.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 2.0f / 3.0f, 0.f, }; } @@ -89,9 +86,22 @@ const std::vector Cube::getVertices() const void Cube::removeFace(Face face) { - delete[] m_CubeFacesData[(uint32_t)face]; - m_CubeFacesData.erase((uint32_t)face); + delete[] m_CubeFacesData[face]; + m_CubeFacesData.erase(face); m_NumberOfFaces--; m_NumberOfVertices -= 6; } +float* Cube::GetFaceData(Face face) +{ + switch (face) + { + case Face::BACK: return m_CubeFacesData[Face::BACK]; + case Face::FRONT: return m_CubeFacesData[Face::FRONT]; + case Face::LEFT: return m_CubeFacesData[Face::LEFT]; + case Face::RIGHT: return m_CubeFacesData[Face::RIGHT]; + case Face::BOTTOM: return m_CubeFacesData[Face::BOTTOM]; + case Face::TOP: return m_CubeFacesData[Face::TOP]; + } +} + diff --git a/MCGL/src/world/Cube.h b/MCGL/src/world/Cube.h index 1adc331..bed31db 100644 --- a/MCGL/src/world/Cube.h +++ b/MCGL/src/world/Cube.h @@ -16,12 +16,14 @@ class Cube size_t getSizeOfData() const; const std::vector getVertices() const; void removeFace(Face face); + float* GetFaceData(Face face); + private: uint8_t m_NumberOfFaces = 6; uint32_t m_NumberOfFloatsInEachFace = 30; uint32_t m_NumberOfVertices = 36; private: - std::unordered_map m_CubeFacesData; + std::unordered_map m_CubeFacesData; };