diff options
Diffstat (limited to '')
-rw-r--r-- | cwd/assets/altcraft/shaders/frag/face.fs | 3 | ||||
-rw-r--r-- | cwd/assets/altcraft/shaders/vert/face.vs | 37 | ||||
-rw-r--r-- | src/RendererSection.cpp | 118 | ||||
-rw-r--r-- | src/RendererSection.hpp | 8 | ||||
-rw-r--r-- | src/RendererSectionData.cpp | 29 | ||||
-rw-r--r-- | src/RendererSectionData.hpp | 10 |
6 files changed, 97 insertions, 108 deletions
diff --git a/cwd/assets/altcraft/shaders/frag/face.fs b/cwd/assets/altcraft/shaders/frag/face.fs index bd2a2d7..314e47b 100644 --- a/cwd/assets/altcraft/shaders/frag/face.fs +++ b/cwd/assets/altcraft/shaders/frag/face.fs @@ -1,7 +1,6 @@ #version 330 core in VS_OUT { - vec2 UvPosition; vec3 Texture; vec3 Color; vec2 Light; @@ -47,4 +46,4 @@ void main() { color = vec4(color.rgb * faceLight, color.a); fragColor = color; -}
\ 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 e054dd8..e1f01fc 100644 --- a/cwd/assets/altcraft/shaders/vert/face.vs +++ b/cwd/assets/altcraft/shaders/vert/face.vs @@ -1,15 +1,13 @@ #version 330 core -layout (location = 3) in vec4 position[4]; -layout (location = 2) in vec2 UvCoordinates; -layout (location = 11) in vec4 Texture; -layout (location = 12) in vec3 color; -layout (location = 13) in vec2 light; -layout (location = 14) in float TextureLayer; -layout (location = 15) in float TextureFrames; +layout (location = 0) in vec4 position[4]; +layout (location = 4) in vec2 uv[4]; +layout (location = 8) in float uvLayer; +layout (location = 9) in vec2 animation; +layout (location = 10) in vec3 color; +layout (location = 11) in vec2 light; out VS_OUT { - vec2 UvPosition; vec3 Texture; vec3 Color; vec2 Light; @@ -18,27 +16,10 @@ out VS_OUT { uniform float GlobalTime; uniform mat4 projView; -vec3 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords, float Layer) { - float x = TextureAtlasCoords.x; - float y = TextureAtlasCoords.y; - float h = TextureAtlasCoords.w; - vec2 transformed = vec2(x, 1 - y - h) + UvCoords * TextureAtlasCoords.zw; - return vec3(transformed.x, transformed.y, Layer); -} - -void main() -{ +void main() { gl_Position = projView * position[gl_VertexID]; - - 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 = UvCoordinates; - vs_out.Texture = TransformTextureCoord(texturePos,UvCoordinates,TextureLayer); vs_out.Color = color; vs_out.Light = light; + vs_out.Texture = vec3(uv[gl_VertexID], uvLayer); + vs_out.Texture.y -= animation.x * trunc(mod(GlobalTime * 4.0f, animation.y)); } diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index e19043d..df8d3c7 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -7,37 +7,17 @@ #include "Renderer.hpp" #include "RendererSectionData.hpp" -const GLfloat uv_coords[] = { - 0.0f, 0.0f, - 1.0f, 0.0f, - 1.0f, 1.0f, - 0.0f, 1.0f, - -}; - -const GLuint magicUniqueConstant = 88375; -GLuint RendererSection::VboUvs = magicUniqueConstant; - RendererSection::RendererSection(const RendererSectionData &data) { OPTICK_EVENT(); - if (VboUvs == magicUniqueConstant) { - glGenBuffers(1, &VboUvs); - - //Cube UVs - glBindBuffer(GL_ARRAY_BUFFER, VboUvs); - glBufferData(GL_ARRAY_BUFFER, sizeof(uv_coords), uv_coords, GL_STATIC_DRAW); - - LOG(INFO) << "Created VBOs with UVs (" << VboUvs << ") for faces"; - } glGenVertexArrays(1, &Vao); - + glGenBuffers(VBOCOUNT, Vbo); glBindVertexArray(Vao); { //Cube vertices - GLuint VertAttribPos = 3; + GLuint VertAttribPos = 0; glBindBuffer(GL_ARRAY_BUFFER, Vbo[POSITIONS]); glVertexAttribPointer(VertAttribPos, 4, GL_FLOAT, GL_FALSE, 4 * 4 * sizeof(GLfloat), nullptr); glEnableVertexAttribArray(VertAttribPos); @@ -54,50 +34,63 @@ RendererSection::RendererSection(const RendererSectionData &data) { glVertexAttribPointer(VertAttribPos + 3, 4, GL_FLOAT, GL_FALSE, 4 * 4 * sizeof(GLfloat), (void*)(3 * 4 * sizeof(GLfloat))); glEnableVertexAttribArray(VertAttribPos + 3); glVertexAttribDivisor(VertAttribPos + 3, 1); + glCheckError(); - //Cube UVs - GLuint UvAttribPos = 2; - glBindBuffer(GL_ARRAY_BUFFER, VboUvs); - glVertexAttribPointer(UvAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr); + //Cube uvs + GLuint UvAttribPos = 4; + glBindBuffer(GL_ARRAY_BUFFER, Vbo[UVS]); + glVertexAttribPointer(UvAttribPos, 2, GL_FLOAT, GL_FALSE, 4 * 2 * sizeof(GLfloat), nullptr); glEnableVertexAttribArray(UvAttribPos); + glVertexAttribDivisor(UvAttribPos, 1); + + glVertexAttribPointer(UvAttribPos + 1, 2, GL_FLOAT, GL_FALSE, 4 * 2 * sizeof(GLfloat), (void*)(1 * 2 * sizeof(GLfloat))); + glEnableVertexAttribArray(UvAttribPos + 1); + glVertexAttribDivisor(UvAttribPos + 1, 1); + + glVertexAttribPointer(UvAttribPos + 2, 2, GL_FLOAT, GL_FALSE, 4 * 2 * sizeof(GLfloat), (void*)(2 * 2 * sizeof(GLfloat))); + glEnableVertexAttribArray(UvAttribPos + 2); + glVertexAttribDivisor(UvAttribPos + 2, 1); - //Textures - GLuint textureAttribPos = 11; - glBindBuffer(GL_ARRAY_BUFFER, Vbo[TEXTURES]); - glVertexAttribPointer(textureAttribPos, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr); - glEnableVertexAttribArray(textureAttribPos); - glVertexAttribDivisor(textureAttribPos, 1); + glVertexAttribPointer(UvAttribPos + 3, 2, GL_FLOAT, GL_FALSE, 4 * 2 * sizeof(GLfloat), (void*)(3 * 2 * sizeof(GLfloat))); + glEnableVertexAttribArray(UvAttribPos + 3); + glVertexAttribDivisor(UvAttribPos + 3, 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(); - - //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(); + //Uv Layer + GLuint uvLayerAttribPos = 8; + glBindBuffer(GL_ARRAY_BUFFER, Vbo[UVLAYERS]); + glVertexAttribPointer(uvLayerAttribPos, 1, GL_FLOAT, GL_FALSE, sizeof(GLfloat), nullptr); + glEnableVertexAttribArray(uvLayerAttribPos); + glVertexAttribDivisor(uvLayerAttribPos, 1); + glCheckError(); + + //Animation + GLuint animationAttribPos = 9; + glCheckError(); + glBindBuffer(GL_ARRAY_BUFFER, Vbo[ANIMATIONS]); + glCheckError(); + glVertexAttribPointer(animationAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr); + glCheckError(); + glEnableVertexAttribArray(animationAttribPos); + glCheckError(); + glVertexAttribDivisor(animationAttribPos, 1); + glCheckError(); //Color - GLuint colorAttribPos = 12; + GLuint colorAttribPos = 10; glBindBuffer(GL_ARRAY_BUFFER, Vbo[COLORS]); glVertexAttribPointer(colorAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr); glEnableVertexAttribArray(colorAttribPos); glVertexAttribDivisor(colorAttribPos, 1); + glCheckError(); //Light - GLuint lightAttribPos = 13; + GLuint lightAttribPos = 11; glBindBuffer(GL_ARRAY_BUFFER, Vbo[LIGHTS]); glVertexAttribPointer(lightAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr); glEnableVertexAttribArray(lightAttribPos); glVertexAttribDivisor(lightAttribPos, 1); + glCheckError(); glBindBuffer(GL_ARRAY_BUFFER, 0); } @@ -150,28 +143,29 @@ size_t RendererSection::GetHash() { void RendererSection::UpdateData(const RendererSectionData & data) { OPTICK_EVENT(); - 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[POSITIONS]); + glBufferData(GL_ARRAY_BUFFER, data.positions.size() * sizeof(glm::vec4), data.positions.data(), GL_DYNAMIC_DRAW); + + glBindBuffer(GL_ARRAY_BUFFER, Vbo[UVS]); + glBufferData(GL_ARRAY_BUFFER, data.uvs.size() * sizeof(glm::vec2), data.uvs.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[UVLAYERS]); + glBufferData(GL_ARRAY_BUFFER, data.uvLayers.size() * sizeof(GLfloat), data.uvLayers.data(), GL_DYNAMIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, Vbo[POSITIONS]); - glBufferData(GL_ARRAY_BUFFER, data.positions.size() * sizeof(glm::vec4), data.positions.data(), GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, Vbo[ANIMATIONS]); + glBufferData(GL_ARRAY_BUFFER, data.animations.size() * sizeof(glm::vec2), data.animations.data(), GL_DYNAMIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, Vbo[COLORS]); - glBufferData(GL_ARRAY_BUFFER, data.colors.size() * sizeof(glm::vec3), data.colors.data(), GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, Vbo[COLORS]); + glBufferData(GL_ARRAY_BUFFER, data.colors.size() * sizeof(glm::vec3), data.colors.data(), GL_DYNAMIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, Vbo[LIGHTS]); - glBufferData(GL_ARRAY_BUFFER, data.lights.size() * sizeof(glm::vec2), data.lights.data(), GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, Vbo[LIGHTS]); + glBufferData(GL_ARRAY_BUFFER, data.lights.size() * sizeof(glm::vec2), data.lights.data(), GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glCheckError(); - numOfFaces = data.textures.size(); + numOfFaces = data.animations.size(); sectionPos = data.sectionPos; hash = data.hash; } diff --git a/src/RendererSection.hpp b/src/RendererSection.hpp index dd01c49..9bb55e3 100644 --- a/src/RendererSection.hpp +++ b/src/RendererSection.hpp @@ -11,9 +11,9 @@ class RendererSectionData; class RendererSection { enum Vbos { POSITIONS = 0, - TEXTURES, - LAYERS, - FRAMES, + UVS, + UVLAYERS, + ANIMATIONS, COLORS, LIGHTS, VBOCOUNT, @@ -21,8 +21,6 @@ class RendererSection { GLuint Vao = { 0 }; GLuint Vbo[VBOCOUNT] = { 0 }; - static GLuint VboUvs; - size_t hash; Vector sectionPos; diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index eabfee2..78e2239 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -15,6 +15,15 @@ inline const BlockId& GetBlockId(int x, int y, int z, const std::array<BlockId, return blockIdData[y * 256 + z * 16 + x]; } +glm::vec2 TransformTextureCoord(glm::vec4 TextureAtlasCoords, glm::vec2 UvCoords, float frames) { + float x = TextureAtlasCoords.x; + float y = TextureAtlasCoords.y; + float w = TextureAtlasCoords.z; + float h = TextureAtlasCoords.w / frames; + glm::vec2 transformed = glm::vec2(x, 1 - y - h) + UvCoords * glm::vec2(w, h); + return transformed; +} + void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, const glm::mat4 &transform, bool visibility[FaceDirection::none], BlockLightness light, BlockLightness skyLight) { for (const auto &face : model.faces) { glm::vec2 lightness; @@ -37,16 +46,22 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, co continue; lightness = glm::vec2(light.face[faceDirection], skyLight.face[faceDirection]); } + glm::mat4 transformed = transform * model.transform * face.transform; data.positions.push_back(transformed * glm::vec4(0, 0, 0, 1)); data.positions.push_back(transformed * glm::vec4(0, 0, 1, 1)); data.positions.push_back(transformed * glm::vec4(1, 0, 1, 1)); data.positions.push_back(transformed * glm::vec4(1, 0, 0, 1)); - data.textures.push_back(face.texture); - data.textureLayers.push_back(face.layer); - data.textureFrames.push_back(face.frames); - data.lights.push_back(lightness); + + data.uvs.push_back(TransformTextureCoord(face.texture, glm::vec2(0, 0), face.frames)); + data.uvs.push_back(TransformTextureCoord(face.texture, glm::vec2(1, 0), face.frames)); + data.uvs.push_back(TransformTextureCoord(face.texture, glm::vec2(1, 1), face.frames)); + data.uvs.push_back(TransformTextureCoord(face.texture, glm::vec2(0, 1), face.frames)); + + data.uvLayers.push_back(face.layer); + data.animations.push_back(glm::vec2(face.texture.w / face.frames, face.frames)); data.colors.push_back(face.color); + data.lights.push_back(lightness); } } @@ -139,10 +154,12 @@ RendererSectionData ParseSection(const SectionsData §ions) { } } } - data.textures.shrink_to_fit(); - data.textureLayers.shrink_to_fit(); data.positions.shrink_to_fit(); + data.uvs.shrink_to_fit(); + data.uvLayers.shrink_to_fit(); + data.animations.shrink_to_fit(); data.colors.shrink_to_fit(); + data.lights.shrink_to_fit(); return data; } diff --git a/src/RendererSectionData.hpp b/src/RendererSectionData.hpp index 3062d0c..7b30e44 100644 --- a/src/RendererSectionData.hpp +++ b/src/RendererSectionData.hpp @@ -31,11 +31,11 @@ struct SectionsData { }; struct RendererSectionData { - std::vector<glm::vec4> positions; - std::vector<glm::vec4> textures; - std::vector<float> textureLayers; - std::vector<float> textureFrames; - std::vector<glm::vec3> colors; + std::vector<glm::vec4> positions; //4 per instance + std::vector<glm::vec2> uvs; //4 per instance + std::vector<float> uvLayers; + std::vector<glm::vec2> animations; + std::vector<glm::vec3> colors; std::vector<glm::vec2> lights; size_t hash = 0; Vector sectionPos; |