Merge pull request #1 from plane000/dev

Textures work
This commit is contained in:
Benjamin Kyd
2019-10-07 12:10:29 +01:00
committed by GitHub
12 changed files with 134 additions and 12 deletions

View File

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

16
src/config.hpp Normal file
View File

@@ -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

View File

@@ -4,13 +4,16 @@
#include <logger.h>
#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<Renderer>();
m_world = std::make_unique<World>();
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Top, 1, 1));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Bottom, 1, 2));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Top, 2, 1));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Bottom, 0, 2));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Right, 1, 3));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Left, 1, 4));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Front, 1, 5));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Back, 1, 6));
m_world->Shaders["Basic"] = std::make_shared<Shader>();
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);

View File

@@ -24,7 +24,7 @@ class Logger;
class Renderer;
class Camera;
class World;
class Game {
public:
Game();

View File

@@ -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 = {

View File

@@ -12,6 +12,7 @@ Renderer::Renderer() {
void Renderer::Render(std::shared_ptr<World> world, std::shared_ptr<Camera> 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"]);
}

View File

@@ -1,3 +1,60 @@
#include "texture.hpp"
#include <logger.h>
#include "../config.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "../util/stb_image.hpp"
GLuint Texture::LoadTextures(std::vector<std::pair<int, std::string>> 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;
}

View File

@@ -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<std::pair<int, std::string>> locations);
};
#endif

View File

@@ -43,8 +43,15 @@
#include <imgui/imgui_impl_sdl.h>
// SDL
#if _WIN32
#include <SDL.h>
#include <SDL_syswm.h>
#else
#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
#endif
#if defined(__APPLE__)
#include "TargetConditionals.h"
#endif

View File

@@ -1,12 +1,18 @@
#include "block.hpp"
std::vector<std::shared_ptr> BlockAtlas() {
static bool Constructed = false;
#include "../config.hpp"
if (!Constructed) {
// Texture winding order - top, bottom, left, right, front, back
}
// std::vector<std::pair<int, std::string>> TextureIdsAndPaths {
// {1, "dirt.png"},
// {2, "grass_side.png"},
// {3, "grass_top.png"}
// };
std::vector<std::shared_ptr<Block>> BlockAtlas() {
}

View File

@@ -11,7 +11,12 @@ struct Block {
};
static std::vector<std::pair<int, std::string>> TextureIdsAndPaths {
{0, "dirt.png"},
{1, "grass_side.png"},
{2, "grass_top.png"}
};
std::vector<std::shared_ptr> BlockAtlas();
#endif

View File

@@ -12,6 +12,8 @@ public:
std::map<std::string, std::shared_ptr<Shader>> Shaders;
std::vector<std::shared_ptr<Face>> Faces;
GLuint TextureID;
};
#endif