summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-08-05 01:40:01 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-08-05 01:40:01 +0200
commitc0b81150298ce9f2f21da42f26a57fe59a93bb77 (patch)
tree15304e4c36093a5beb524e67c976a241c5c556f3
parentImplemented texture atlas (diff)
downloadAltCraft-c0b81150298ce9f2f21da42f26a57fe59a93bb77.tar
AltCraft-c0b81150298ce9f2f21da42f26a57fe59a93bb77.tar.gz
AltCraft-c0b81150298ce9f2f21da42f26a57fe59a93bb77.tar.bz2
AltCraft-c0b81150298ce9f2f21da42f26a57fe59a93bb77.tar.lz
AltCraft-c0b81150298ce9f2f21da42f26a57fe59a93bb77.tar.xz
AltCraft-c0b81150298ce9f2f21da42f26a57fe59a93bb77.tar.zst
AltCraft-c0b81150298ce9f2f21da42f26a57fe59a93bb77.zip
-rw-r--r--cwd/shaders/face.fs4
-rw-r--r--cwd/shaders/face.vs10
-rw-r--r--src/AssetManager.cpp18
-rw-r--r--src/AssetManager.hpp7
-rw-r--r--src/Render.cpp4
-rw-r--r--src/RendererSection.cpp11
-rw-r--r--src/RendererSection.hpp1
-rw-r--r--src/RendererSectionData.cpp2
-rw-r--r--src/RendererSectionData.hpp1
9 files changed, 42 insertions, 16 deletions
diff --git a/cwd/shaders/face.fs b/cwd/shaders/face.fs
index a4e020c..62a2d37 100644
--- a/cwd/shaders/face.fs
+++ b/cwd/shaders/face.fs
@@ -2,12 +2,12 @@
in VS_OUT {
vec2 UvPosition;
- vec2 Texture;
+ vec3 Texture;
vec3 Color;
vec2 Light;
} fs_in;
-uniform sampler2D textureAtlas;
+uniform sampler2DArray textureAtlas;
uniform vec2 windowSize;
uniform float DayTime;
diff --git a/cwd/shaders/face.vs b/cwd/shaders/face.vs
index 5431ff2..9c40846 100644
--- a/cwd/shaders/face.vs
+++ b/cwd/shaders/face.vs
@@ -5,10 +5,11 @@ layout (location = 7) in vec4 Texture;
layout (location = 8) in mat4 model;
layout (location = 12) in vec3 color;
layout (location = 13) in vec2 light;
+layout (location = 14) in float TextureLayer;
out VS_OUT {
vec2 UvPosition;
- vec2 Texture;
+ vec3 Texture;
vec3 Color;
vec2 Light;
} vs_out;
@@ -17,14 +18,15 @@ out VS_OUT {
//uniform mat4 projection;
uniform mat4 projView;
-vec2 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords) {
+vec3 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords, float Layer) {
float x = TextureAtlasCoords.x;
float y = TextureAtlasCoords.y;
float w = TextureAtlasCoords.z;
float h = TextureAtlasCoords.w;
vec2 A = vec2(x, 1 - y - h);
vec2 B = vec2(x + w, 1 - y);
- return A + UvCoords * (B - A);
+ vec2 transformed = A + UvCoords * (B - A);
+ return vec3(transformed.x, transformed.y, Layer);
}
void main()
@@ -33,7 +35,7 @@ void main()
gl_Position = projView * model * sourcePosition;
vs_out.UvPosition = vec2(UvCoordinates.x,UvCoordinates.y);
- vs_out.Texture = TransformTextureCoord(Texture,UvCoordinates);
+ vs_out.Texture = TransformTextureCoord(Texture,UvCoordinates,TextureLayer);
vs_out.Color = color;
vs_out.Light = light;
}
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp
index 9e360c0..0579b5d 100644
--- a/src/AssetManager.cpp
+++ b/src/AssetManager.cpp
@@ -366,10 +366,10 @@ void AssetManager::ParseBlockModels() {
break;
}
parsedFace.transform = faceTransform;
- glm::vec4 texture;
+ TextureCoord texture;
textureName = face.second.texture;
if (model.Textures.empty()) {
- texture = GetTextureByAssetName("minecraft/texture/blocks/tnt_side");
+ texture = GetTexture("minecraft/texture/blocks/tnt_side");
}
else {
while (textureName[0] == '#') {
@@ -378,7 +378,7 @@ void AssetManager::ParseBlockModels() {
textureName = textureIt != model.Textures.end() ? textureIt->second : "minecraft/texture/blocks/tnt_side";
}
textureName.insert(0, "minecraft/textures/");
- texture = GetTextureByAssetName(textureName);
+ texture = GetTexture(textureName);
if (!(face.second.uv == BlockModel::ElementData::FaceData::Uv{ 0,16,0,16 }) && !(face.second.uv == BlockModel::ElementData::FaceData::Uv{ 0,0,0,0 })
&& !(face.second.uv == BlockModel::ElementData::FaceData::Uv{ 0,0,16,16 })) {
@@ -392,13 +392,17 @@ void AssetManager::ParseBlockModels() {
h /= 16.0;
double X = texture.x;
double Y = texture.y;
- double W = texture.z;
- double H = texture.w;
+ double W = texture.w;
+ double H = texture.h;
- texture = glm::vec4{ X + x * W, Y + y * H, w * W , h * H };
+ texture.x = X + x * W;
+ texture.y = Y + y * H;
+ texture.w = w * W;
+ texture.h = h * H;
}
}
- parsedFace.texture = texture;
+ parsedFace.texture = glm::vec4{ texture.x,texture.y,texture.w,texture.h };
+ parsedFace.layer = texture.layer;
if (face.second.tintIndex)
parsedFace.color = glm::vec3(0.275, 0.63, 0.1);
else
diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp
index 1b64215..0663f48 100644
--- a/src/AssetManager.hpp
+++ b/src/AssetManager.hpp
@@ -132,6 +132,7 @@ struct BlockModel {
ElementData::FaceDirection visibility;
glm::mat4 transform;
glm::vec4 texture;
+ float layer;
glm::vec3 color;
};
@@ -204,7 +205,11 @@ public:
template <typename T>
T *GetAsset(const std::string &assetName) {
- AssetTreeNode *node = GetAssetByAssetName(assetName);
+ AssetTreeNode *node;
+ if (assetName[0] != '/')
+ node = GetAssetByAssetName('/'+assetName);
+ else
+ node = GetAssetByAssetName(assetName);
if (!node)
return nullptr;
return dynamic_cast<T*>(node->asset.get());
diff --git a/src/Render.cpp b/src/Render.cpp
index ba8d0ed..1bd5273 100644
--- a/src/Render.cpp
+++ b/src/Render.cpp
@@ -92,8 +92,8 @@ void Render::InitGlew() {
void Render::PrepareToRendering() {
//TextureAtlas texture
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, AssetManager::Instance().GetTextureAtlas());
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D_ARRAY, AssetManager::Instance().GetTextureAtlasId());
AssetManager::Instance().GetTextureAtlasIndexes();
ImGui_ImplSdlGL3_Init(window);
diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp
index 21c2714..bbf24ff 100644
--- a/src/RendererSection.cpp
+++ b/src/RendererSection.cpp
@@ -73,6 +73,14 @@ RendererSection::RendererSection(const RendererSectionData &data) {
glVertexAttribDivisor(textureAttribPos, 1);
glCheckError();
+ //TextureLayers
+ GLuint layerAttribPos = 14;
+ glBindBuffer(GL_ARRAY_BUFFER, Vbo[LAYERS]);
+ glVertexAttribPointer(layerAttribPos, 1, GL_FLOAT, GL_FALSE, sizeof(GLfloat), nullptr);
+ glEnableVertexAttribArray(layerAttribPos);
+ glVertexAttribDivisor(layerAttribPos, 1);
+ glCheckError();
+
//Blocks models
GLuint matAttribPos = 8;
size_t sizeOfMat4 = 4 * 4 * sizeof(GLfloat);
@@ -156,6 +164,9 @@ void RendererSection::UpdateData(const RendererSectionData & data) {
glBindBuffer(GL_ARRAY_BUFFER, Vbo[TEXTURES]);
glBufferData(GL_ARRAY_BUFFER, data.textures.size() * sizeof(glm::vec4), data.textures.data(), GL_DYNAMIC_DRAW);
+ glBindBuffer(GL_ARRAY_BUFFER, Vbo[LAYERS]);
+ glBufferData(GL_ARRAY_BUFFER, data.textureLayers.size() * 1* sizeof(GLfloat), data.textureLayers.data(), GL_DYNAMIC_DRAW);
+
glBindBuffer(GL_ARRAY_BUFFER, Vbo[MODELS]);
glBufferData(GL_ARRAY_BUFFER, data.models.size() * sizeof(glm::mat4), data.models.data(), GL_DYNAMIC_DRAW);
diff --git a/src/RendererSection.hpp b/src/RendererSection.hpp
index f172a12..c760678 100644
--- a/src/RendererSection.hpp
+++ b/src/RendererSection.hpp
@@ -12,6 +12,7 @@ class RendererSection {
enum Vbos {
MODELS = 0,
TEXTURES,
+ LAYERS,
COLORS,
LIGHTS,
VBOCOUNT,
diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp
index ab3812d..de79da4 100644
--- a/src/RendererSectionData.cpp
+++ b/src/RendererSectionData.cpp
@@ -48,6 +48,7 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockModel &model, co
data.models.push_back(transform * face.transform);
data.textures.push_back(face.texture);
+ data.textureLayers.push_back(face.layer);
data.colors.push_back(face.color);
data.lights.push_back(glm::vec2(light, skyLight));
}
@@ -188,6 +189,7 @@ RendererSectionData ParseSection(const SectionsData &sections)
}
}
data.textures.shrink_to_fit();
+ data.textureLayers.shrink_to_fit();
data.models.shrink_to_fit();
data.colors.shrink_to_fit();
diff --git a/src/RendererSectionData.hpp b/src/RendererSectionData.hpp
index 134eef6..a60f671 100644
--- a/src/RendererSectionData.hpp
+++ b/src/RendererSectionData.hpp
@@ -26,6 +26,7 @@ struct SectionsData {
struct RendererSectionData {
std::vector<glm::mat4> models;
std::vector<glm::vec4> textures;
+ std::vector<float> textureLayers;
std::vector<glm::vec3> colors;
std::vector<glm::vec2> lights;
size_t hash = 0;