From c55e3ec240e3db34fff2b4878939b172c875ce9a Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Mon, 7 Oct 2019 12:10:49 +0100 Subject: [PATCH] Textures work --- resources/shaders/simple.frag | 5 +-- src/config.hpp | 16 +++++++++ src/game.cpp | 12 +++++-- src/game.hpp | 2 +- src/renderer/face.cpp | 18 ++++++++++ src/renderer/renderer.cpp | 1 + src/renderer/texture.cpp | 57 +++++++++++++++++++++++++++++++ src/renderer/texture.hpp | 5 ++- src/util/imgui/imgui_impl_sdl.cpp | 7 ++++ src/world/block.cpp | 14 +++++--- src/world/block.hpp | 7 +++- src/world/world.hpp | 2 ++ 12 files changed, 134 insertions(+), 12 deletions(-) create mode 100644 src/config.hpp diff --git a/resources/shaders/simple.frag b/resources/shaders/simple.frag index 3c768fb..34d93dd 100644 --- a/resources/shaders/simple.frag +++ b/resources/shaders/simple.frag @@ -4,8 +4,9 @@ in vec3 TexCoord; out vec4 outColour; -// uniform sampler2D tex; +uniform sampler2DArray tex; void main() { - outColour = vec4(TexCoord, 1.0); // texture(tex, TexCoord); + // outColour = vec4(TexCoord, 1.0); + outColour = texture(tex, TexCoord); } diff --git a/src/config.hpp b/src/config.hpp new file mode 100644 index 0000000..3fe1a2a --- /dev/null +++ b/src/config.hpp @@ -0,0 +1,16 @@ +#ifndef MINECRAFT_CONFIG_H_ +#define MINECRAFT_CONFIG_H_ + +#include "common.hpp" + +class Config { +public: + + std::string ResourceBase = "/home/ben/programming/Minecraft/resources/"; + // std::string ResourceBase = "E:/Games/minecraft/resources/"; + +}; + +static Config GameConfig; + +#endif diff --git a/src/game.cpp b/src/game.cpp index 7b2d687..ffaf944 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -4,13 +4,16 @@ #include #include "renderer/renderer.hpp" +#include "renderer/texture.hpp" #include "renderer/shader.hpp" #include "renderer/camera.hpp" #include "renderer/face.hpp" #include "world/world.hpp" +#include "world/block.hpp" #include "common.hpp" +#include "config.hpp" Game::Game() { @@ -127,17 +130,20 @@ void Game::Run() { m_renderer = std::make_unique(); m_world = std::make_unique(); - m_world->Faces.push_back(std::make_shared(FaceDirection::Top, 1, 1)); - m_world->Faces.push_back(std::make_shared(FaceDirection::Bottom, 1, 2)); + m_world->Faces.push_back(std::make_shared(FaceDirection::Top, 2, 1)); + m_world->Faces.push_back(std::make_shared(FaceDirection::Bottom, 0, 2)); m_world->Faces.push_back(std::make_shared(FaceDirection::Right, 1, 3)); m_world->Faces.push_back(std::make_shared(FaceDirection::Left, 1, 4)); m_world->Faces.push_back(std::make_shared(FaceDirection::Front, 1, 5)); m_world->Faces.push_back(std::make_shared(FaceDirection::Back, 1, 6)); m_world->Shaders["Basic"] = std::make_shared(); - m_world->Shaders["Basic"]->Load("E:/Games/minecraft/resources/shaders/simple"); + m_world->Shaders["Basic"]->Load(GameConfig.ResourceBase + "shaders/simple"); m_world->Shaders["Basic"]->Link(); + Texture texture; + m_world->TextureID = texture.LoadTextures(TextureIdsAndPaths); + while (IsDisplayOpen) { Input(&e); diff --git a/src/game.hpp b/src/game.hpp index 2c7d5c5..7f56e21 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -24,7 +24,7 @@ class Logger; class Renderer; class Camera; class World; - + class Game { public: Game(); diff --git a/src/renderer/face.cpp b/src/renderer/face.cpp index d591aca..30b1201 100644 --- a/src/renderer/face.cpp +++ b/src/renderer/face.cpp @@ -45,6 +45,15 @@ Face::Face(FaceDirection direction, int textureID, int asdf) { { -0.5f, 0.5f, 0.5f }, { -0.5f, -0.5f, 0.5f }, }; + + m_uvs = { + { 1.0f, 1.0f }, + { 0.0f, 1.0f }, + { 0.0f, 0.0f }, + { 0.0f, 0.0f }, + { 1.0f, 0.0f }, + { 1.0f, 1.0f }, + }; } else if (Direction == FaceDirection::Back) { m_verticies = { @@ -55,6 +64,15 @@ Face::Face(FaceDirection direction, int textureID, int asdf) { { -0.5f, 0.5f, -0.5f }, { -0.5f, -0.5f, -0.5f }, }; + + m_uvs = { + { 1.0f, 1.0f }, + { 0.0f, 1.0f }, + { 0.0f, 0.0f }, + { 0.0f, 0.0f }, + { 1.0f, 0.0f }, + { 1.0f, 1.0f }, + }; } else if (Direction == FaceDirection::Left) { m_verticies = { diff --git a/src/renderer/renderer.cpp b/src/renderer/renderer.cpp index 8685e81..c4191ab 100644 --- a/src/renderer/renderer.cpp +++ b/src/renderer/renderer.cpp @@ -12,6 +12,7 @@ Renderer::Renderer() { void Renderer::Render(std::shared_ptr world, std::shared_ptr camera) { for (int i = 0; i < world->Faces.size(); i++) { + glBindTexture(GL_TEXTURE_2D_ARRAY, world->TextureID); world->Faces[i]->Render(camera, world->Shaders["Basic"]); } diff --git a/src/renderer/texture.cpp b/src/renderer/texture.cpp index 2b550dd..f7ad280 100644 --- a/src/renderer/texture.cpp +++ b/src/renderer/texture.cpp @@ -1,3 +1,60 @@ #include "texture.hpp" +#include +#include "../config.hpp" + +#define STB_IMAGE_IMPLEMENTATION +#include "../util/stb_image.hpp" + +GLuint Texture::LoadTextures(std::vector> textures) { + + Logger logger; + + std::string basePath = GameConfig.ResourceBase + "textures/"; + + int x = 16; + int y = 16; + + GLsizei layers = textures.size(); + GLubyte* texels = (GLubyte*)malloc(x * y * 4 * layers * sizeof(GLubyte)); + + for (int i = 0; i < layers; i++) { + + std::string path = basePath + textures[i].second; + + int xR = 0; + int yR = 0; + int cR = 0; + + unsigned char* texture = stbi_load(path.c_str(), &xR, &yR, &cR, STBI_rgb_alpha); + logger << LOGGER_INFO << "Texture at '" << path << "' Loaded..." << LOGGER_ENDL; + + memcpy(texels + (i * x * y * 4), texture, x * y * 4); + + stbi_image_free(texture); + } + + + GLuint textureArray = 0; + + glGenTextures(1, &textureArray); + glBindTexture(GL_TEXTURE_2D_ARRAY, textureArray); + + glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, x, y, layers, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + + glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, x, y, layers, GL_RGBA, GL_UNSIGNED_BYTE, texels); + + glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + + glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + glBindTexture(GL_TEXTURE_2D_ARRAY, 0); + + free(texels); + + return textureArray; + +} diff --git a/src/renderer/texture.hpp b/src/renderer/texture.hpp index 8c540e2..ff5d077 100644 --- a/src/renderer/texture.hpp +++ b/src/renderer/texture.hpp @@ -1,8 +1,11 @@ #ifndef MINECRAFT_RENDERER_TEXTURE_H_ #define MINECRAFT_RENDERER_TEXTURE_H_ -class Texture { +#include "../common.hpp" +class Texture { +public: + GLuint LoadTextures(std::vector> locations); }; #endif diff --git a/src/util/imgui/imgui_impl_sdl.cpp b/src/util/imgui/imgui_impl_sdl.cpp index e32390f..b4ae734 100644 --- a/src/util/imgui/imgui_impl_sdl.cpp +++ b/src/util/imgui/imgui_impl_sdl.cpp @@ -43,8 +43,15 @@ #include // SDL + + +#if _WIN32 #include #include +#else +#include +#include +#endif #if defined(__APPLE__) #include "TargetConditionals.h" #endif diff --git a/src/world/block.cpp b/src/world/block.cpp index 662e344..dbd7bfa 100644 --- a/src/world/block.cpp +++ b/src/world/block.cpp @@ -1,12 +1,18 @@ #include "block.hpp" -std::vector BlockAtlas() { - static bool Constructed = false; +#include "../config.hpp" - if (!Constructed) { +// Texture winding order - top, bottom, left, right, front, back - } +// std::vector> TextureIdsAndPaths { +// {1, "dirt.png"}, +// {2, "grass_side.png"}, +// {3, "grass_top.png"} +// }; +std::vector> BlockAtlas() { + + } diff --git a/src/world/block.hpp b/src/world/block.hpp index 6a0acb4..a1f7ce9 100644 --- a/src/world/block.hpp +++ b/src/world/block.hpp @@ -11,7 +11,12 @@ struct Block { }; +static std::vector> TextureIdsAndPaths { + {0, "dirt.png"}, + {1, "grass_side.png"}, + {2, "grass_top.png"} +}; + -std::vector BlockAtlas(); #endif diff --git a/src/world/world.hpp b/src/world/world.hpp index 7bccc97..a3c2042 100644 --- a/src/world/world.hpp +++ b/src/world/world.hpp @@ -12,6 +12,8 @@ public: std::map> Shaders; std::vector> Faces; + GLuint TextureID; + }; #endif