From b57a26a7112d19fdee27c99028092463e550119c Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 27 Jan 2019 18:57:47 +0500 Subject: More advanced textures animation support --- cwd/assets/altcraft/shaders/face.json | 3 ++- cwd/assets/altcraft/shaders/vert/face.vs | 14 +++++++++++--- src/AssetManager.cpp | 2 +- src/AssetManager.hpp | 3 ++- src/RendererSection.cpp | 11 +++++++++++ src/RendererSection.hpp | 1 + src/RendererSectionData.cpp | 1 + src/RendererSectionData.hpp | 1 + src/RendererWorld.cpp | 5 +++++ src/RendererWorld.hpp | 1 + 10 files changed, 36 insertions(+), 6 deletions(-) diff --git a/cwd/assets/altcraft/shaders/face.json b/cwd/assets/altcraft/shaders/face.json index 1454dcf..840e670 100644 --- a/cwd/assets/altcraft/shaders/face.json +++ b/cwd/assets/altcraft/shaders/face.json @@ -5,6 +5,7 @@ "projView", "textureAtlas", "DayTime", - "MinLightLevel" + "MinLightLevel", + "GlobalTime" ] } \ No newline at end of file diff --git a/cwd/assets/altcraft/shaders/vert/face.vs b/cwd/assets/altcraft/shaders/vert/face.vs index 9c40846..044c012 100644 --- a/cwd/assets/altcraft/shaders/vert/face.vs +++ b/cwd/assets/altcraft/shaders/vert/face.vs @@ -1,4 +1,5 @@ #version 330 core + layout (location = 0) in vec3 position; layout (location = 2) in vec2 UvCoordinates; layout (location = 7) in vec4 Texture; @@ -6,6 +7,7 @@ layout (location = 8) in mat4 model; layout (location = 12) in vec3 color; layout (location = 13) in vec2 light; layout (location = 14) in float TextureLayer; +layout (location = 15) in float TextureFrames; out VS_OUT { vec2 UvPosition; @@ -14,8 +16,7 @@ out VS_OUT { vec2 Light; } vs_out; -//uniform mat4 view; -//uniform mat4 projection; +uniform float GlobalTime; uniform mat4 projView; vec3 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords, float Layer) { @@ -34,8 +35,15 @@ void main() vec4 sourcePosition = vec4(position,1.0f); gl_Position = projView * model * sourcePosition; + vec4 texturePos = Texture; + float frameHeight = texturePos.w / TextureFrames; + float currentFrame = mod(GlobalTime * 4.0f, TextureFrames); + currentFrame = trunc(currentFrame); + texturePos.w = frameHeight; + texturePos.y = texturePos.y + currentFrame * frameHeight; + vs_out.UvPosition = vec2(UvCoordinates.x,UvCoordinates.y); - vs_out.Texture = TransformTextureCoord(Texture,UvCoordinates,TextureLayer); + vs_out.Texture = TransformTextureCoord(texturePos,UvCoordinates,TextureLayer); vs_out.Color = color; vs_out.Light = light; } diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 9fd49bb..71800d7 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -508,7 +508,6 @@ void ParseBlockModels() { AssetTexture *assetTexture = AssetManager::GetAsset(textureName); texture = atlas->GetTexture(assetTexture->id); textureFrames = assetTexture->frames; - texture.h /= textureFrames; 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 })) { @@ -533,6 +532,7 @@ void ParseBlockModels() { } parsedFace.texture = glm::vec4{ texture.x,texture.y,texture.w,texture.h }; parsedFace.layer = texture.layer; + parsedFace.frames = textureFrames; 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 6c5cb49..de3881e 100644 --- a/src/AssetManager.hpp +++ b/src/AssetManager.hpp @@ -41,7 +41,8 @@ struct ParsedFace { glm::mat4 transform; glm::vec4 texture; float layer; - glm::vec3 color; + float frames; + glm::vec3 color; }; struct BlockFaces { diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index bbf24ff..1521c6f 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -81,6 +81,14 @@ RendererSection::RendererSection(const RendererSectionData &data) { glVertexAttribDivisor(layerAttribPos, 1); glCheckError(); + //TextureFrames + GLuint framesAttribPos = 15; + glBindBuffer(GL_ARRAY_BUFFER, Vbo[FRAMES]); + glVertexAttribPointer(framesAttribPos, 1, GL_FLOAT, GL_FALSE, sizeof(GLfloat), nullptr); + glEnableVertexAttribArray(framesAttribPos); + glVertexAttribDivisor(framesAttribPos, 1); + glCheckError(); + //Blocks models GLuint matAttribPos = 8; size_t sizeOfMat4 = 4 * 4 * sizeof(GLfloat); @@ -167,6 +175,9 @@ void RendererSection::UpdateData(const RendererSectionData & data) { 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[FRAMES]); + glBufferData(GL_ARRAY_BUFFER, data.textureFrames.size() * 1 * sizeof(GLfloat), data.textureFrames.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 c760678..29b5db1 100644 --- a/src/RendererSection.hpp +++ b/src/RendererSection.hpp @@ -13,6 +13,7 @@ class RendererSection { MODELS = 0, TEXTURES, LAYERS, + FRAMES, COLORS, LIGHTS, VBOCOUNT, diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index e72a369..3b51809 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -39,6 +39,7 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, co data.models.push_back(transform * model.transform * face.transform); data.textures.push_back(face.texture); data.textureLayers.push_back(face.layer); + data.textureFrames.push_back(face.frames); data.lights.push_back(lightness); data.colors.push_back(face.color); } diff --git a/src/RendererSectionData.hpp b/src/RendererSectionData.hpp index 7153b05..cedb51b 100644 --- a/src/RendererSectionData.hpp +++ b/src/RendererSectionData.hpp @@ -34,6 +34,7 @@ struct RendererSectionData { std::vector models; std::vector textures; std::vector textureLayers; + std::vector textureFrames; std::vector colors; std::vector lights; size_t hash = 0; diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index e0a28a4..7f7c850 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -153,6 +153,8 @@ RendererWorld::RendererWorld(std::shared_ptr ptr) { listener = std::make_unique(); + globalTimeStart = std::chrono::high_resolution_clock::now(); + PrepareRender(); listener->RegisterHandler("DeleteSectionRender", [this](const Event& eventData) { @@ -377,10 +379,13 @@ void RendererWorld::Render(RenderState & renderState) { glCheckError(); //Render sections + auto rawGlobalTime = (std::chrono::high_resolution_clock::now() - globalTimeStart); + float globalTime = rawGlobalTime.count() / 1000000000.0f; Shader *blockShader = AssetManager::GetAsset("/altcraft/shaders/face")->shader.get(); blockShader->Activate(); blockShader->SetUniform("DayTime", mixLevel); blockShader->SetUniform("projView", projView); + blockShader->SetUniform("GlobalTime", globalTime); glCheckError(); Frustum frustum(projView); diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp index 5148964..913e510 100644 --- a/src/RendererWorld.hpp +++ b/src/RendererWorld.hpp @@ -44,6 +44,7 @@ class RendererWorld { std::vector renderList; std::map sections; void UpdateAllSections(VectorF playerPos); + std::chrono::time_point globalTimeStart; //Entities std::vector entities; //Sky -- cgit v1.2.3