summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-08-05 02:15:12 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-08-05 02:15:12 +0200
commitcc3096b4f4649915cf89a5bc4f6b943d667369f5 (patch)
tree4de6e88338b65f3c66c2fdea29a067ec042012d2 /src
parentReplaced usage of old texture atlas (diff)
downloadAltCraft-cc3096b4f4649915cf89a5bc4f6b943d667369f5.tar
AltCraft-cc3096b4f4649915cf89a5bc4f6b943d667369f5.tar.gz
AltCraft-cc3096b4f4649915cf89a5bc4f6b943d667369f5.tar.bz2
AltCraft-cc3096b4f4649915cf89a5bc4f6b943d667369f5.tar.lz
AltCraft-cc3096b4f4649915cf89a5bc4f6b943d667369f5.tar.xz
AltCraft-cc3096b4f4649915cf89a5bc4f6b943d667369f5.tar.zst
AltCraft-cc3096b4f4649915cf89a5bc4f6b943d667369f5.zip
Diffstat (limited to 'src')
-rw-r--r--src/AssetManager.cpp156
-rw-r--r--src/AssetManager.hpp64
-rw-r--r--src/Render.cpp1
-rw-r--r--src/RendererSectionData.cpp3
-rw-r--r--src/RendererWorld.cpp6
-rw-r--r--src/Texture.cpp91
-rw-r--r--src/Texture.hpp14
7 files changed, 6 insertions, 329 deletions
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp
index 0579b5d..7f4bf7d 100644
--- a/src/AssetManager.cpp
+++ b/src/AssetManager.cpp
@@ -9,7 +9,6 @@
#include <SDL.h>
#include <SDL_image.h>
-#include "Texture.hpp"
#include "Utility.hpp"
namespace fs = std::experimental::filesystem::v1;
@@ -34,7 +33,6 @@ AssetManager::AssetManager() {
LoadTextures();
LoadIds();
- LoadTextureResources();
ParseBlockModels();
}
@@ -52,160 +50,6 @@ void AssetManager::LoadIds() {
}
AssetManager::~AssetManager() {
- delete textureAtlas;
-}
-
-//TODO: This function must be replaced with runtime texture atlas generating
-void AssetManager::LoadTextureResources() {
- std::ifstream in(pathToTextureIndex);
- nlohmann::json index;
- in >> index;
- std::string filename = index["meta"]["image"].get<std::string>();
- float textureWidth = index["meta"]["size"]["w"].get<int>();
- float textureHeight = index["meta"]["size"]["h"].get<int>();
- size_t sizeName = 0,sizeTexture = 0;
- for (auto &it:index["frames"]) {
- auto frame = it["frame"];
- TextureCoordinates coord;
- coord.x = frame["x"].get<int>() / textureWidth;
- coord.y = frame["y"].get<int>() / textureHeight;
- coord.w = frame["w"].get<int>() / textureWidth;
- coord.h = frame["h"].get<int>() / textureHeight;
- std::string assetName = it["filename"].get<std::string>();
- assetName.insert(0, "minecraft/textures/");
- assetName.erase(assetName.length() - 4);
- assetName.shrink_to_fit();
- sizeName += sizeof(assetName) + assetName.capacity();
- sizeTexture += sizeof(coord);
- assetTextures[assetName] = coord;
- }
- textureAtlas = new Texture(filename);
- LOG(INFO) << "Texture atlas id is " << textureAtlas->texture;
-}
-
-TextureCoordinates AssetManager::GetTextureByAssetName(const std::string &AssetName) {
- auto it = assetTextures.find(AssetName);
- if (it != assetTextures.end())
- return it->second;
- else
- return TextureCoordinates{-1, -1, -1, -1};
-}
-
-std::string AssetManager::GetTextureAssetNameByBlockId(BlockTextureId block) {
- //Block sides: 0 - bottom, 1 - top, 2 - north, 3 - south, 4 - west, 5 - east 6 - every side
- const std::map<BlockTextureId, std::string> lookupTable = {
- {BlockTextureId(0, 0), "minecraft/textures/blocks/air"},
- {BlockTextureId(1, 0), "minecraft/textures/blocks/stone"},
- {BlockTextureId(1, 1), "minecraft/textures/blocks/stone_granite"},
-
- {BlockTextureId(2, 0, 0), "minecraft/textures/blocks/dirt"},
- {BlockTextureId(2, 0, 1), "minecraft/textures/blocks/grass_top"},
- {BlockTextureId(2, 0, 2), "minecraft/textures/blocks/grass_side"},
- {BlockTextureId(2, 0, 3), "minecraft/textures/blocks/grass_side"},
- {BlockTextureId(2, 0, 4), "minecraft/textures/blocks/grass_side"},
- {BlockTextureId(2, 0, 5), "minecraft/textures/blocks/grass_side"},
-
- {BlockTextureId(3, 0), "minecraft/textures/blocks/dirt"},
- {BlockTextureId(4, 0), "minecraft/textures/blocks/cobblestone"},
- {BlockTextureId(5, 0), "minecraft/textures/blocks/planks"},
-
- {BlockTextureId(7, 0), "minecraft/textures/blocks/bedrock"},
-
- {BlockTextureId(17, 0, 0), "minecraft/textures/blocks/log_oak_top"},
- {BlockTextureId(17, 0, 1), "minecraft/textures/blocks/log_oak_top"},
- {BlockTextureId(17, 0, 2), "minecraft/textures/blocks/log_oak"},
- {BlockTextureId(17, 0, 3), "minecraft/textures/blocks/log_oak"},
- {BlockTextureId(17, 0, 4), "minecraft/textures/blocks/log_oak"},
- {BlockTextureId(17, 0, 5), "minecraft/textures/blocks/log_oak"},
-
- {BlockTextureId(17, 1, 0), "minecraft/textures/blocks/log_spruce_top"},
- {BlockTextureId(17, 1, 1), "minecraft/textures/blocks/log_spruce_top"},
- {BlockTextureId(17, 1, 2), "minecraft/textures/blocks/log_spruce"},
- {BlockTextureId(17, 1, 3), "minecraft/textures/blocks/log_spruce"},
- {BlockTextureId(17, 1, 4), "minecraft/textures/blocks/log_spruce"},
- {BlockTextureId(17, 1, 5), "minecraft/textures/blocks/log_spruce"},
-
- {BlockTextureId(17, 2, 0), "minecraft/textures/blocks/log_birch_top"},
- {BlockTextureId(17, 2, 1), "minecraft/textures/blocks/log_birch_top"},
- {BlockTextureId(17, 2, 2), "minecraft/textures/blocks/log_birch"},
- {BlockTextureId(17, 2, 3), "minecraft/textures/blocks/log_birch"},
- {BlockTextureId(17, 2, 4), "minecraft/textures/blocks/log_birch"},
- {BlockTextureId(17, 2, 5), "minecraft/textures/blocks/log_birch"},
-
- {BlockTextureId(17, 3, 0), "minecraft/textures/blocks/log_jungle_top"},
- {BlockTextureId(17, 3, 1), "minecraft/textures/blocks/log_jungle_top"},
- {BlockTextureId(17, 3, 2), "minecraft/textures/blocks/log_jungle"},
- {BlockTextureId(17, 3, 3), "minecraft/textures/blocks/log_jungle"},
- {BlockTextureId(17, 3, 4), "minecraft/textures/blocks/log_jungle"},
- {BlockTextureId(17, 3, 5), "minecraft/textures/blocks/log_jungle"},
-
- {BlockTextureId(18, 0), "minecraft/textures/blocks/leaves_oak"},
- {BlockTextureId(18, 1), "minecraft/textures/blocks/leaves_spruce"},
- {BlockTextureId(18, 2), "minecraft/textures/blocks/leaves_birch"},
- {BlockTextureId(18, 3), "minecraft/textures/blocks/leaves_jungle"},
-
- {BlockTextureId(61, 0, 0), "minecraft/textures/blocks/furnace_side"},
- {BlockTextureId(61, 0, 1), "minecraft/textures/blocks/furnace_top"},
- {BlockTextureId(61, 0, 2), "minecraft/textures/blocks/furnace_front_off"},
- {BlockTextureId(61, 0, 3), "minecraft/textures/blocks/furnace_side"},
- {BlockTextureId(61, 0, 4), "minecraft/textures/blocks/furnace_side"},
- {BlockTextureId(61, 0, 5), "minecraft/textures/blocks/furnace_side"},
-
- {BlockTextureId(62, 0, 0), "minecraft/textures/blocks/furnace_side"},
- {BlockTextureId(62, 0, 1), "minecraft/textures/blocks/furnace_top"},
- {BlockTextureId(62, 0, 2), "minecraft/textures/blocks/furnace_front_on"},
- {BlockTextureId(62, 0, 3), "minecraft/textures/blocks/furnace_side"},
- {BlockTextureId(62, 0, 4), "minecraft/textures/blocks/furnace_side"},
- {BlockTextureId(62, 0, 5), "minecraft/textures/blocks/furnace_side"},
-
-
- {BlockTextureId(31, 0), "minecraft/textures/blocks/deadbush"},
- {BlockTextureId(31, 1), "minecraft/textures/blocks/tallgrass"},
- {BlockTextureId(31, 2), "minecraft/textures/blocks/fern"},
- };
-
- auto ret = lookupTable.find(block);
- return (ret == lookupTable.end()) ? "" : ret->second;
-}
-
-GLuint AssetManager::GetTextureAtlas() {
- return textureAtlas->texture;
-}
-
-TextureCoordinates AssetManager::GetTextureByBlock(BlockTextureId block) {
- std::string assetName = this->GetTextureAssetNameByBlockId(block);
- return this->GetTextureByAssetName(assetName);
-}
-
-const std::map<BlockTextureId, glm::vec4> &AssetManager::GetTextureAtlasIndexes() {
- if (!textureAtlasIndexes.empty())
- return textureAtlasIndexes;
-
- LOG(INFO) << "Initializing texture atlas...";
- for (int id = 1; id < 128; id++) {
- for (int state = 0; state < 16; state++) {
- BlockTextureId blockTextureId(id, state, 6);
- if (!this->GetTextureByBlock(blockTextureId) &&
- !this->GetTextureByBlock(BlockTextureId(id, state, 0))) {
- continue;
- }
-
- if (this->GetTextureByBlock(blockTextureId)) {
- for (int i = 0; i < 6; i++) {
- TextureCoordinates tc = this->GetTextureByBlock(BlockTextureId(id, state, 6));
- textureAtlasIndexes[BlockTextureId(id, state, i)] = glm::vec4(tc.x, tc.y, tc.w, tc.h);
- }
- } else {
- for (int i = 0; i < 6; i++) {
- TextureCoordinates tc = this->GetTextureByBlock(BlockTextureId(id, state, i));
- textureAtlasIndexes[BlockTextureId(id, state, i)] = glm::vec4(tc.x, tc.y, tc.w, tc.h);
- }
- }
- }
- }
- LOG(INFO) << "Created " << textureAtlasIndexes.size() << " texture indexes";
-
- return textureAtlasIndexes;
}
AssetManager &AssetManager::Instance() {
diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp
index 0663f48..3975b37 100644
--- a/src/AssetManager.hpp
+++ b/src/AssetManager.hpp
@@ -13,53 +13,6 @@
#include "Block.hpp"
#include "TextureAtlas.hpp"
-class Texture;
-
-struct TextureCoordinates {
- TextureCoordinates(float x = -1, float y = -1, float w = -1, float h = -1)
- : x(x), y(y), w(w), h(h) {}
-
- bool operator==(const TextureCoordinates &rhs) const {
- return x == rhs.x &&
- y == rhs.y &&
- w == rhs.w &&
- h == rhs.h;
- }
-
- explicit operator bool() const {
- return !(*this == TextureCoordinates(-1, -1, -1, -1));
- }
-
- double x, y, w, h;
-
- operator glm::vec4() const {
- return glm::vec4(x, y, w, h);
- }
-};
-
-struct BlockTextureId {
- //Block sides: 0 - bottom, 1 - top, 2 - north, 3 - south, 4 - west, 5 - east 6 - every side
- BlockTextureId(int id = 0, int state = 0, int side = 6)
- : id(id), state(state), side(side) {}
-
- int id : 9;
- int state : 4;
- int side : 3;
-
-
- bool operator<(const BlockTextureId &rhs) const {
- if (id < rhs.id)
- return true;
- if (rhs.id < id)
- return false;
- if (state < rhs.state)
- return true;
- if (rhs.state < state)
- return false;
- return side < rhs.side;
- }
-};
-
struct BlockModel {
bool IsBlock = false;
std::string BlockName;
@@ -167,10 +120,7 @@ struct AssetTexture : Asset {
};
class AssetManager {
- Texture *textureAtlas;
std::map<std::string, BlockId> assetIds;
- std::map<std::string, TextureCoordinates> assetTextures;
- std::map<BlockTextureId,glm::vec4> textureAtlasIndexes;
std::map<BlockId, std::string> blockIdToBlockName;
std::unique_ptr<AssetTreeNode> assetTree;
std::unique_ptr<TextureAtlas> atlas;
@@ -178,21 +128,9 @@ public:
AssetManager();
~AssetManager();
-
- void LoadTextureResources();
-
- TextureCoordinates GetTextureByAssetName(const std::string &AssetName);
-
- std::string GetTextureAssetNameByBlockId(BlockTextureId block);
-
- GLuint GetTextureAtlas();
-
- const std::map<BlockTextureId,glm::vec4> &GetTextureAtlasIndexes();
-
+
void LoadIds();
- TextureCoordinates GetTextureByBlock(BlockTextureId block);
-
static AssetManager& Instance();
const BlockModel *GetBlockModelByBlockId(BlockId block);
diff --git a/src/Render.cpp b/src/Render.cpp
index 1bd5273..88fe2cc 100644
--- a/src/Render.cpp
+++ b/src/Render.cpp
@@ -94,7 +94,6 @@ void Render::PrepareToRendering() {
//TextureAtlas texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, AssetManager::Instance().GetTextureAtlasId());
- AssetManager::Instance().GetTextureAtlasIndexes();
ImGui_ImplSdlGL3_Init(window);
}
diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp
index de79da4..fb2135a 100644
--- a/src/RendererSectionData.cpp
+++ b/src/RendererSectionData.cpp
@@ -162,8 +162,7 @@ RendererSectionData ParseSection(const SectionsData &sections)
std::array<BlockId, 4096> blockIdData = SetBlockIdData(sections);
std::array<unsigned char, 4096> blockVisibility = GetBlockVisibilityData(sections, blockIdData, idModels);
std::string textureName;
-
- const std::map<BlockTextureId, glm::vec4> &textureAtlas = AssetManager::Instance().GetTextureAtlasIndexes();
+
data.hash = sections.section.GetHash();
data.sectionPos = sections.section.GetPosition();
diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp
index 5d0d6a8..ed4a4a3 100644
--- a/src/RendererWorld.cpp
+++ b/src/RendererWorld.cpp
@@ -427,12 +427,14 @@ void RendererWorld::PrepareRender() {
skyShader = new Shader("./shaders/sky.vs", "./shaders/sky.fs");
skyShader->Use();
glUniform1i(glGetUniformLocation(skyShader->Program, "textureAtlas"), 0);
- TextureCoordinates sunTexture = AssetManager::Instance().GetTextureByAssetName("minecraft/textures/environment/sun");
+ TextureCoord sunTexture = AssetManager::Instance().GetTexture("/minecraft/textures/environment/sun");
glUniform4f(glGetUniformLocation(skyShader->Program, "sunTexture"), sunTexture.x, sunTexture.y, sunTexture.w, sunTexture.h);
- TextureCoordinates moonTexture = AssetManager::Instance().GetTextureByAssetName("minecraft/textures/environment/moon_phases");
+ glUniform1f(glGetUniformLocation(skyShader->Program, "sunTextureLayer"), sunTexture.layer);
+ TextureCoord moonTexture = AssetManager::Instance().GetTexture("/minecraft/textures/environment/moon_phases");
moonTexture.w /= 4.0f; //First phase will be fine for now
moonTexture.h /= 2.0f;
glUniform4f(glGetUniformLocation(skyShader->Program, "moonTexture"), moonTexture.x, moonTexture.y, moonTexture.w, moonTexture.h);
+ glUniform1f(glGetUniformLocation(skyShader->Program, "moonTextureLayer"), moonTexture.layer);
}
void RendererWorld::Update(double timeToUpdate) {
diff --git a/src/Texture.cpp b/src/Texture.cpp
deleted file mode 100644
index 5693682..0000000
--- a/src/Texture.cpp
+++ /dev/null
@@ -1,91 +0,0 @@
-#include "Texture.hpp"
-
-#include <SDL.h>
-#include <SDL_image.h>
-#include <easylogging++.h>
-
-
-bool IsImgInitialized = false;
-
-Texture::Texture(std::string filename, GLenum textureWrapping, GLenum textureFiltering) {
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
-
- //Texture options
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, textureWrapping);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, textureWrapping);
-
- //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-
- //Initialize SDL2_image
- if (!IsImgInitialized) {
- IsImgInitialized = true;
- int imgFlags = IMG_INIT_PNG;
- if (!(IMG_Init(imgFlags) & imgFlags))
- {
- throw std::runtime_error("IMG Not initialized: " + std::string(IMG_GetError()));
- }
- }
-
- //Load image
- SDL_Surface *image = IMG_Load(filename.c_str());
- if (!image)
- throw std::runtime_error("Image not loaded: " + std::string(IMG_GetError()));
-
- if (image->format->BytesPerPixel != 4)
- throw std::runtime_error("PNG format is not RGBA");
-
- //Flip surface
- SDL_LockSurface(image);
-
- {
- int pitch = image->pitch;
- int height = image->h;
- void* image_pixels = image->pixels;
-
- int index;
- void* temp_row;
- int height_div_2;
-
- temp_row = (void *)malloc(pitch);
- if (!temp_row)
- throw std::runtime_error("Not enough memory for texture flipping");
-
- height_div_2 = (int)(height * .5);
- for (index = 0; index < height_div_2; index++) {
- memcpy((Uint8 *)temp_row,
- (Uint8 *)(image_pixels)+
- pitch * index,
- pitch);
-
- memcpy(
- (Uint8 *)(image_pixels)+
- pitch * index,
- (Uint8 *)(image_pixels)+
- pitch * (height - index - 1),
- pitch);
- memcpy(
- (Uint8 *)(image_pixels)+
- pitch * (height - index - 1),
- temp_row,
- pitch);
- }
- free(temp_row);
- }
-
- //Creating texture
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w,image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE,
- (GLvoid *) image->pixels);
- SDL_UnlockSurface(image);
-
- //glGenerateMipmap(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, 0);
-
- SDL_FreeSurface(image);
-}
-
-Texture::~Texture() {
- glDeleteTextures(1, &texture);
-}
diff --git a/src/Texture.hpp b/src/Texture.hpp
deleted file mode 100644
index cda3e36..0000000
--- a/src/Texture.hpp
+++ /dev/null
@@ -1,14 +0,0 @@
-#pragma once
-
-#include <string>
-
-#include <GL/glew.h>
-
-class Texture {
- Texture(Texture&);
- Texture&operator=(Texture&);
-public:
- GLuint texture;
- Texture(std::string filename, GLenum textureWrapping = GL_CLAMP_TO_BORDER, GLenum textureFiltering = GL_NEAREST);
- ~Texture();
-}; \ No newline at end of file