Skip to content

Commit

Permalink
Optimized chunk generation, fixed block textures
Browse files Browse the repository at this point in the history
  • Loading branch information
laithtawabini committed Dec 2, 2022
1 parent d8fa3af commit 79021a1
Show file tree
Hide file tree
Showing 23 changed files with 453 additions and 122 deletions.
6 changes: 5 additions & 1 deletion MCGL/MCGL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\graphics\Chunk.h" />
<ClInclude Include="src\graphics\VertexBufferLayout.h" />
<ClInclude Include="src\graphics\CubeRenderer.h" />
<ClInclude Include="src\MCGL.h" />
<ClInclude Include="src\Window.h" />
<ClInclude Include="src\world\Block.h" />
<ClInclude Include="src\camera\Camera.h" />
<ClInclude Include="src\world\Chunk.h" />
<ClInclude Include="src\world\Cube.h" />
<ClInclude Include="src\graphics\IndexBuffer.h" />
<ClInclude Include="src\graphics\Shader.h" />
Expand Down Expand Up @@ -324,10 +324,12 @@
<ClInclude Include="src\vendor\stb_image.h" />
<ClInclude Include="src\graphics\VertexBuffer.h" />
<ClInclude Include="src\graphics\VertexArray.h" />
<ClInclude Include="src\graphics\Worldchunk.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\application.cpp" />
<ClCompile Include="src\graphics\CubeRenderer.cpp" />
<ClCompile Include="src\graphics\Chunk.cpp" />
<ClCompile Include="src\MCGL.cpp" />
<ClCompile Include="src\Texture.cpp" />
<ClCompile Include="src\Window.cpp" />
Expand All @@ -338,6 +340,7 @@
<ClCompile Include="src\Source.cpp" />
<ClCompile Include="src\graphics\VertexBuffer.cpp" />
<ClCompile Include="src\graphics\VertexArray.cpp" />
<ClCompile Include="src\graphics\Worldchunk.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="res\shaders\shader.glsl" />
Expand Down Expand Up @@ -481,6 +484,7 @@
<ItemGroup>
<Image Include="res\textures\awesomeface.png" />
<Image Include="res\textures\blocks\grass_block.png" />
<Image Include="res\textures\blocks\tilemap.png" />
<Image Include="res\textures\container.jpg" />
<Image Include="res\textures\dirt.png" />
<Image Include="res\textures\grass.png" />
Expand Down
18 changes: 15 additions & 3 deletions MCGL/MCGL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -909,9 +909,6 @@
<ClInclude Include="src\camera\Camera.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\world\Chunk.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\world\Block.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand All @@ -930,6 +927,12 @@
<ClInclude Include="src\Window.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\graphics\Chunk.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\graphics\Worldchunk.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\vendor\glad.c">
Expand Down Expand Up @@ -968,6 +971,12 @@
<ClCompile Include="src\Texture.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\graphics\Chunk.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\graphics\Worldchunk.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="res\shaders\shader.glsl" />
Expand Down Expand Up @@ -1399,5 +1408,8 @@
<Image Include="res\textures\blocks\grass_block.png">
<Filter>Resource Files</Filter>
</Image>
<Image Include="res\textures\blocks\tilemap.png">
<Filter>Resource Files</Filter>
</Image>
</ItemGroup>
</Project>
17 changes: 14 additions & 3 deletions MCGL/res/shaders/shader.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
#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;
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;
}

Expand All @@ -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);
}


Binary file added MCGL/res/textures/blocks/tilemap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 51 additions & 28 deletions MCGL/src/MCGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<float>(xposIn);
Expand All @@ -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;
Expand All @@ -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);
}

Expand All @@ -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<GLbyte> 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()
Expand Down
2 changes: 2 additions & 0 deletions MCGL/src/MCGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#include <iostream>
#include <vector>
#include <unordered_map>
#include <ranges>

#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"

Expand Down
7 changes: 6 additions & 1 deletion MCGL/src/Texture.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Texture.h"

uint32_t Texture::m_nOfTextures = 0;

Texture::Texture(const std::string& path)
{
glGenTextures(1, &m_TextureID);
Expand Down Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions MCGL/src/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Texture {

private:
uint32_t m_TextureID;
static uint32_t m_nOfTextures;
};


2 changes: 1 addition & 1 deletion MCGL/src/camera/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading

0 comments on commit 79021a1

Please sign in to comment.