From 5a6aaf633ce217f828d981cd8c55951ada67e2df Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 27 Jun 2021 21:31:00 +0500 Subject: Moved block face model matrix transformation to CPU --- cwd/assets/altcraft/shaders/vert/face.vs | 9 ++-- src/RendererSection.cpp | 73 +++++++++++++------------------- src/RendererSection.hpp | 4 +- src/RendererSectionData.cpp | 10 ++++- src/RendererSectionData.hpp | 2 +- 5 files changed, 44 insertions(+), 54 deletions(-) diff --git a/cwd/assets/altcraft/shaders/vert/face.vs b/cwd/assets/altcraft/shaders/vert/face.vs index 30ae0d7..424e215 100644 --- a/cwd/assets/altcraft/shaders/vert/face.vs +++ b/cwd/assets/altcraft/shaders/vert/face.vs @@ -1,9 +1,8 @@ #version 330 core -layout (location = 0) in vec3 position; +layout (location = 3) in vec4 position[6]; layout (location = 2) in vec2 UvCoordinates; -layout (location = 7) in vec4 Texture; -layout (location = 8) in mat4 model; +layout (location = 11) in vec4 Texture; layout (location = 12) in vec3 color; layout (location = 13) in vec2 light; layout (location = 14) in float TextureLayer; @@ -22,7 +21,6 @@ uniform mat4 projView; 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 transformed = vec2(x, 1 - y - h) + UvCoords * TextureAtlasCoords.zw; return vec3(transformed.x, transformed.y, Layer); @@ -30,8 +28,7 @@ vec3 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords, float Layer) void main() { - vec4 sourcePosition = vec4(position,1.0f); - gl_Position = projView * model * sourcePosition; + gl_Position = projView * position[gl_VertexID]; vec4 texturePos = Texture; float frameHeight = texturePos.w / TextureFrames; diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index d797771..1e0a1d0 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -7,16 +7,6 @@ #include "Renderer.hpp" #include "RendererSectionData.hpp" -const GLfloat vertices[] = { - 0, 0, 0, - 1, 0, 1, - 1, 0, 0, - - 0, 0, 0, - 0, 0, 1, - 1, 0, 1, -}; - const GLfloat uv_coords[] = { 0.0f, 0.0f, 1.0f, 1.0f, @@ -28,25 +18,18 @@ const GLfloat uv_coords[] = { }; const GLuint magicUniqueConstant = 88375; -GLuint RendererSection::VboVertices = magicUniqueConstant; GLuint RendererSection::VboUvs = magicUniqueConstant; RendererSection::RendererSection(const RendererSectionData &data) { OPTICK_EVENT(); - if (VboVertices == magicUniqueConstant) { - glGenBuffers(1, &VboVertices); + if (VboUvs == magicUniqueConstant) { glGenBuffers(1, &VboUvs); - //Cube vertices - glBindBuffer(GL_ARRAY_BUFFER, VboVertices); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - //Cube UVs glBindBuffer(GL_ARRAY_BUFFER, VboUvs); glBufferData(GL_ARRAY_BUFFER, sizeof(uv_coords), uv_coords, GL_STATIC_DRAW); - LOG(INFO) << "Created VBOs with vertices (" << VboVertices << ") and UVs (" << VboUvs - << ") for faces"; + LOG(INFO) << "Created VBOs with UVs (" << VboUvs << ") for faces"; } glGenVertexArrays(1, &Vao); @@ -56,10 +39,31 @@ RendererSection::RendererSection(const RendererSectionData &data) { glBindVertexArray(Vao); { //Cube vertices - GLuint VertAttribPos = 0; - glBindBuffer(GL_ARRAY_BUFFER, VboVertices); - glVertexAttribPointer(VertAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr); + GLuint VertAttribPos = 3; + glBindBuffer(GL_ARRAY_BUFFER, Vbo[POSITIONS]); + glVertexAttribPointer(VertAttribPos, 4, GL_FLOAT, GL_FALSE, 6 * 4 * sizeof(GLfloat), nullptr); glEnableVertexAttribArray(VertAttribPos); + glVertexAttribDivisor(VertAttribPos, 1); + + glVertexAttribPointer(VertAttribPos + 1, 4, GL_FLOAT, GL_FALSE, 6 * 4 * sizeof(GLfloat), (void*)(1 * 4 * sizeof(GLfloat))); + glEnableVertexAttribArray(VertAttribPos + 1); + glVertexAttribDivisor(VertAttribPos + 1, 1); + + glVertexAttribPointer(VertAttribPos + 2, 4, GL_FLOAT, GL_FALSE, 6 * 4 * sizeof(GLfloat), (void*)(2 * 4 * sizeof(GLfloat))); + glEnableVertexAttribArray(VertAttribPos + 2); + glVertexAttribDivisor(VertAttribPos + 2, 1); + + glVertexAttribPointer(VertAttribPos + 3, 4, GL_FLOAT, GL_FALSE, 6 * 4 * sizeof(GLfloat), (void*)(3 * 4 * sizeof(GLfloat))); + glEnableVertexAttribArray(VertAttribPos + 3); + glVertexAttribDivisor(VertAttribPos + 3, 1); + + glVertexAttribPointer(VertAttribPos + 4, 4, GL_FLOAT, GL_FALSE, 6 * 4 * sizeof(GLfloat), (void*)(4 * 4 * sizeof(GLfloat))); + glEnableVertexAttribArray(VertAttribPos + 4); + glVertexAttribDivisor(VertAttribPos + 4, 1); + + glVertexAttribPointer(VertAttribPos + 5, 4, GL_FLOAT, GL_FALSE, 6 * 4 * sizeof(GLfloat), (void*)(5 * 4 * sizeof(GLfloat))); + glEnableVertexAttribArray(VertAttribPos + 5); + glVertexAttribDivisor(VertAttribPos + 5, 1); //Cube UVs GLuint UvAttribPos = 2; @@ -68,7 +72,7 @@ RendererSection::RendererSection(const RendererSectionData &data) { glEnableVertexAttribArray(UvAttribPos); //Textures - GLuint textureAttribPos = 7; + GLuint textureAttribPos = 11; glBindBuffer(GL_ARRAY_BUFFER, Vbo[TEXTURES]); glVertexAttribPointer(textureAttribPos, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr); glEnableVertexAttribArray(textureAttribPos); @@ -91,23 +95,6 @@ RendererSection::RendererSection(const RendererSectionData &data) { glVertexAttribDivisor(framesAttribPos, 1); glCheckError(); - //Blocks models - GLuint matAttribPos = 8; - size_t sizeOfMat4 = 4 * 4 * sizeof(GLfloat); - glBindBuffer(GL_ARRAY_BUFFER, Vbo[MODELS]); - glVertexAttribPointer(matAttribPos + 0, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, nullptr); - glVertexAttribPointer(matAttribPos + 1, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *)(1 * 4 * sizeof(GLfloat))); - glVertexAttribPointer(matAttribPos + 2, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *)(2 * 4 * sizeof(GLfloat))); - glVertexAttribPointer(matAttribPos + 3, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *)(3 * 4 * sizeof(GLfloat))); - glEnableVertexAttribArray(matAttribPos + 0); - glEnableVertexAttribArray(matAttribPos + 1); - glEnableVertexAttribArray(matAttribPos + 2); - glEnableVertexAttribArray(matAttribPos + 3); - glVertexAttribDivisor(matAttribPos + 0, 1); - glVertexAttribDivisor(matAttribPos + 1, 1); - glVertexAttribDivisor(matAttribPos + 2, 1); - glVertexAttribDivisor(matAttribPos + 3, 1); - //Color GLuint colorAttribPos = 12; glBindBuffer(GL_ARRAY_BUFFER, Vbo[COLORS]); @@ -159,7 +146,7 @@ void swap(RendererSection & lhs, RendererSection & rhs) { void RendererSection::Render(RenderState &renderState) { OPTICK_EVENT(); renderState.SetActiveVao(Vao); - glDrawArraysInstanced(GL_TRIANGLES, 0, 6, numOfFaces); + glDrawArraysInstanced(GL_TRIANGLES, 0, 6, numOfFaces); glCheckError(); } @@ -182,8 +169,8 @@ void RendererSection::UpdateData(const RendererSectionData & data) { 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); + 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[COLORS]); glBufferData(GL_ARRAY_BUFFER, data.colors.size() * sizeof(glm::vec3), data.colors.data(), GL_DYNAMIC_DRAW); diff --git a/src/RendererSection.hpp b/src/RendererSection.hpp index 29b5db1..dd01c49 100644 --- a/src/RendererSection.hpp +++ b/src/RendererSection.hpp @@ -10,7 +10,7 @@ class RendererSectionData; class RendererSection { enum Vbos { - MODELS = 0, + POSITIONS = 0, TEXTURES, LAYERS, FRAMES, @@ -21,7 +21,7 @@ class RendererSection { GLuint Vao = { 0 }; GLuint Vbo[VBOCOUNT] = { 0 }; - static GLuint VboVertices, VboUvs; + static GLuint VboUvs; size_t hash; Vector sectionPos; diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index 69ed665..cf05528 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -37,7 +37,13 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, co continue; lightness = glm::vec2(light.face[faceDirection], skyLight.face[faceDirection]); } - data.models.push_back(transform * model.transform * face.transform); + 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(1, 0, 1, 1)); + data.positions.push_back(transformed * glm::vec4(1, 0, 0, 1)); + 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.textures.push_back(face.texture); data.textureLayers.push_back(face.layer); data.textureFrames.push_back(face.frames); @@ -137,7 +143,7 @@ RendererSectionData ParseSection(const SectionsData §ions) { } data.textures.shrink_to_fit(); data.textureLayers.shrink_to_fit(); - data.models.shrink_to_fit(); + data.positions.shrink_to_fit(); data.colors.shrink_to_fit(); return data; diff --git a/src/RendererSectionData.hpp b/src/RendererSectionData.hpp index cedb51b..3062d0c 100644 --- a/src/RendererSectionData.hpp +++ b/src/RendererSectionData.hpp @@ -31,7 +31,7 @@ struct SectionsData { }; struct RendererSectionData { - std::vector models; + std::vector positions; std::vector textures; std::vector textureLayers; std::vector textureFrames; -- cgit v1.2.3 From f324c49d896eae159b7499ad6467f03dbcc900ca Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Mon, 28 Jun 2021 16:25:32 +0500 Subject: More compact faces vertecies format --- cwd/assets/altcraft/shaders/vert/face.vs | 2 +- src/RendererSection.cpp | 28 +++++++++------------------- src/RendererSectionData.cpp | 4 +--- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/cwd/assets/altcraft/shaders/vert/face.vs b/cwd/assets/altcraft/shaders/vert/face.vs index 424e215..e054dd8 100644 --- a/cwd/assets/altcraft/shaders/vert/face.vs +++ b/cwd/assets/altcraft/shaders/vert/face.vs @@ -1,6 +1,6 @@ #version 330 core -layout (location = 3) in vec4 position[6]; +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; diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index 1e0a1d0..e19043d 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -9,12 +9,10 @@ const GLfloat uv_coords[] = { 0.0f, 0.0f, - 1.0f, 1.0f, - 0.0f, 1.0f, - - 0.0f, 0.0f, - 1.0f, 0.0f, - 1.0f, 1.0f, + 1.0f, 0.0f, + 1.0f, 1.0f, + 0.0f, 1.0f, + }; const GLuint magicUniqueConstant = 88375; @@ -41,30 +39,22 @@ RendererSection::RendererSection(const RendererSectionData &data) { //Cube vertices GLuint VertAttribPos = 3; glBindBuffer(GL_ARRAY_BUFFER, Vbo[POSITIONS]); - glVertexAttribPointer(VertAttribPos, 4, GL_FLOAT, GL_FALSE, 6 * 4 * sizeof(GLfloat), nullptr); + glVertexAttribPointer(VertAttribPos, 4, GL_FLOAT, GL_FALSE, 4 * 4 * sizeof(GLfloat), nullptr); glEnableVertexAttribArray(VertAttribPos); glVertexAttribDivisor(VertAttribPos, 1); - glVertexAttribPointer(VertAttribPos + 1, 4, GL_FLOAT, GL_FALSE, 6 * 4 * sizeof(GLfloat), (void*)(1 * 4 * sizeof(GLfloat))); + glVertexAttribPointer(VertAttribPos + 1, 4, GL_FLOAT, GL_FALSE, 4 * 4 * sizeof(GLfloat), (void*)(1 * 4 * sizeof(GLfloat))); glEnableVertexAttribArray(VertAttribPos + 1); glVertexAttribDivisor(VertAttribPos + 1, 1); - glVertexAttribPointer(VertAttribPos + 2, 4, GL_FLOAT, GL_FALSE, 6 * 4 * sizeof(GLfloat), (void*)(2 * 4 * sizeof(GLfloat))); + glVertexAttribPointer(VertAttribPos + 2, 4, GL_FLOAT, GL_FALSE, 4 * 4 * sizeof(GLfloat), (void*)(2 * 4 * sizeof(GLfloat))); glEnableVertexAttribArray(VertAttribPos + 2); glVertexAttribDivisor(VertAttribPos + 2, 1); - glVertexAttribPointer(VertAttribPos + 3, 4, GL_FLOAT, GL_FALSE, 6 * 4 * sizeof(GLfloat), (void*)(3 * 4 * sizeof(GLfloat))); + glVertexAttribPointer(VertAttribPos + 3, 4, GL_FLOAT, GL_FALSE, 4 * 4 * sizeof(GLfloat), (void*)(3 * 4 * sizeof(GLfloat))); glEnableVertexAttribArray(VertAttribPos + 3); glVertexAttribDivisor(VertAttribPos + 3, 1); - glVertexAttribPointer(VertAttribPos + 4, 4, GL_FLOAT, GL_FALSE, 6 * 4 * sizeof(GLfloat), (void*)(4 * 4 * sizeof(GLfloat))); - glEnableVertexAttribArray(VertAttribPos + 4); - glVertexAttribDivisor(VertAttribPos + 4, 1); - - glVertexAttribPointer(VertAttribPos + 5, 4, GL_FLOAT, GL_FALSE, 6 * 4 * sizeof(GLfloat), (void*)(5 * 4 * sizeof(GLfloat))); - glEnableVertexAttribArray(VertAttribPos + 5); - glVertexAttribDivisor(VertAttribPos + 5, 1); - //Cube UVs GLuint UvAttribPos = 2; glBindBuffer(GL_ARRAY_BUFFER, VboUvs); @@ -146,7 +136,7 @@ void swap(RendererSection & lhs, RendererSection & rhs) { void RendererSection::Render(RenderState &renderState) { OPTICK_EVENT(); renderState.SetActiveVao(Vao); - glDrawArraysInstanced(GL_TRIANGLES, 0, 6, numOfFaces); + glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, numOfFaces); glCheckError(); } diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index cf05528..eabfee2 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -39,11 +39,9 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, co } 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(1, 0, 1, 1)); - data.positions.push_back(transformed * glm::vec4(1, 0, 0, 1)); - 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); -- cgit v1.2.3 From 04ad0ef50d7eb362fd13272fd1d610740b3ede5e Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Mon, 28 Jun 2021 20:20:49 +0500 Subject: Embed UV to vertex data --- cwd/assets/altcraft/shaders/frag/face.fs | 3 +- cwd/assets/altcraft/shaders/vert/face.vs | 37 +++------- src/RendererSection.cpp | 118 +++++++++++++++---------------- src/RendererSection.hpp | 8 +-- src/RendererSectionData.cpp | 29 ++++++-- 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 positions; - std::vector textures; - std::vector textureLayers; - std::vector textureFrames; - std::vector colors; + std::vector positions; //4 per instance + std::vector uvs; //4 per instance + std::vector uvLayers; + std::vector animations; + std::vector colors; std::vector lights; size_t hash = 0; Vector sectionPos; -- cgit v1.2.3 From 7ff7b88cf66131a559ea0ee931bddce4785507a7 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Mon, 28 Jun 2021 20:55:59 +0500 Subject: Implemented vertex data iterleaving --- src/RendererSection.cpp | 76 ++++++++++++++------------------------------- src/RendererSection.hpp | 11 +------ src/RendererSectionData.cpp | 38 +++++++++++------------ src/RendererSectionData.hpp | 16 ++++++---- 4 files changed, 52 insertions(+), 89 deletions(-) diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index df8d3c7..397a660 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -1,5 +1,7 @@ #include "RendererSection.hpp" +#include + #include #include @@ -12,85 +14,74 @@ RendererSection::RendererSection(const RendererSectionData &data) { glGenVertexArrays(1, &Vao); - glGenBuffers(VBOCOUNT, Vbo); + glGenBuffers(1, &Vbo); + glBindBuffer(GL_ARRAY_BUFFER, Vbo); glBindVertexArray(Vao); { //Cube vertices GLuint VertAttribPos = 0; - glBindBuffer(GL_ARRAY_BUFFER, Vbo[POSITIONS]); - glVertexAttribPointer(VertAttribPos, 4, GL_FLOAT, GL_FALSE, 4 * 4 * sizeof(GLfloat), nullptr); + glVertexAttribPointer(VertAttribPos, 4, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[0])); glEnableVertexAttribArray(VertAttribPos); glVertexAttribDivisor(VertAttribPos, 1); - glVertexAttribPointer(VertAttribPos + 1, 4, GL_FLOAT, GL_FALSE, 4 * 4 * sizeof(GLfloat), (void*)(1 * 4 * sizeof(GLfloat))); + glVertexAttribPointer(VertAttribPos + 1, 4, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[1])); glEnableVertexAttribArray(VertAttribPos + 1); glVertexAttribDivisor(VertAttribPos + 1, 1); - glVertexAttribPointer(VertAttribPos + 2, 4, GL_FLOAT, GL_FALSE, 4 * 4 * sizeof(GLfloat), (void*)(2 * 4 * sizeof(GLfloat))); + glVertexAttribPointer(VertAttribPos + 2, 4, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[2])); glEnableVertexAttribArray(VertAttribPos + 2); glVertexAttribDivisor(VertAttribPos + 2, 1); - glVertexAttribPointer(VertAttribPos + 3, 4, GL_FLOAT, GL_FALSE, 4 * 4 * sizeof(GLfloat), (void*)(3 * 4 * sizeof(GLfloat))); + glVertexAttribPointer(VertAttribPos + 3, 4, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[3])); glEnableVertexAttribArray(VertAttribPos + 3); glVertexAttribDivisor(VertAttribPos + 3, 1); glCheckError(); //Cube uvs GLuint UvAttribPos = 4; - glBindBuffer(GL_ARRAY_BUFFER, Vbo[UVS]); - glVertexAttribPointer(UvAttribPos, 2, GL_FLOAT, GL_FALSE, 4 * 2 * sizeof(GLfloat), nullptr); + glVertexAttribPointer(UvAttribPos, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, uvs[0])); glEnableVertexAttribArray(UvAttribPos); glVertexAttribDivisor(UvAttribPos, 1); - glVertexAttribPointer(UvAttribPos + 1, 2, GL_FLOAT, GL_FALSE, 4 * 2 * sizeof(GLfloat), (void*)(1 * 2 * sizeof(GLfloat))); + glVertexAttribPointer(UvAttribPos + 1, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, uvs[1])); glEnableVertexAttribArray(UvAttribPos + 1); glVertexAttribDivisor(UvAttribPos + 1, 1); - glVertexAttribPointer(UvAttribPos + 2, 2, GL_FLOAT, GL_FALSE, 4 * 2 * sizeof(GLfloat), (void*)(2 * 2 * sizeof(GLfloat))); + glVertexAttribPointer(UvAttribPos + 2, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, uvs[2])); glEnableVertexAttribArray(UvAttribPos + 2); glVertexAttribDivisor(UvAttribPos + 2, 1); - glVertexAttribPointer(UvAttribPos + 3, 2, GL_FLOAT, GL_FALSE, 4 * 2 * sizeof(GLfloat), (void*)(3 * 2 * sizeof(GLfloat))); + glVertexAttribPointer(UvAttribPos + 3, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, uvs[3])); glEnableVertexAttribArray(UvAttribPos + 3); glVertexAttribDivisor(UvAttribPos + 3, 1); - glCheckError(); //Uv Layer GLuint uvLayerAttribPos = 8; - glBindBuffer(GL_ARRAY_BUFFER, Vbo[UVLAYERS]); - glVertexAttribPointer(uvLayerAttribPos, 1, GL_FLOAT, GL_FALSE, sizeof(GLfloat), nullptr); + glVertexAttribPointer(uvLayerAttribPos, 1, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, uvLayers)); 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(); + glVertexAttribPointer(animationAttribPos, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, animations)); glEnableVertexAttribArray(animationAttribPos); - glCheckError(); glVertexAttribDivisor(animationAttribPos, 1); - glCheckError(); //Color GLuint colorAttribPos = 10; - glBindBuffer(GL_ARRAY_BUFFER, Vbo[COLORS]); - glVertexAttribPointer(colorAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr); + glVertexAttribPointer(colorAttribPos, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, colors)); glEnableVertexAttribArray(colorAttribPos); glVertexAttribDivisor(colorAttribPos, 1); - glCheckError(); + + size_t m = sizeof(VertexData); + size_t d = offsetof(VertexData, lights); //Light GLuint lightAttribPos = 11; - glBindBuffer(GL_ARRAY_BUFFER, Vbo[LIGHTS]); - glVertexAttribPointer(lightAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr); + glVertexAttribPointer(lightAttribPos, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, lights)); glEnableVertexAttribArray(lightAttribPos); glVertexAttribDivisor(lightAttribPos, 1); - glCheckError(); glBindBuffer(GL_ARRAY_BUFFER, 0); } @@ -109,13 +100,7 @@ RendererSection::~RendererSection() { if (Vao != 0) glDeleteVertexArrays(1, &Vao); - for (int i = 0; i < VBOCOUNT; i++) - if (Vbo[i] != 0) { - glBindBuffer(GL_ARRAY_BUFFER, Vbo[i]); - glBufferData(GL_ARRAY_BUFFER, 0, 0, GL_STATIC_DRAW); - } - - glDeleteBuffers(VBOCOUNT, Vbo); + glDeleteBuffers(1, &Vbo); } void swap(RendererSection & lhs, RendererSection & rhs) { @@ -144,28 +129,13 @@ size_t RendererSection::GetHash() { void RendererSection::UpdateData(const RendererSectionData & data) { OPTICK_EVENT(); - 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[UVLAYERS]); - glBufferData(GL_ARRAY_BUFFER, data.uvLayers.size() * sizeof(GLfloat), data.uvLayers.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[LIGHTS]); - glBufferData(GL_ARRAY_BUFFER, data.lights.size() * sizeof(glm::vec2), data.lights.data(), GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, Vbo); + glBufferData(GL_ARRAY_BUFFER, data.vertices.size() * sizeof(VertexData), data.vertices.data(), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glCheckError(); - numOfFaces = data.animations.size(); + numOfFaces = data.vertices.size(); sectionPos = data.sectionPos; hash = data.hash; } diff --git a/src/RendererSection.hpp b/src/RendererSection.hpp index 9bb55e3..3ea1fec 100644 --- a/src/RendererSection.hpp +++ b/src/RendererSection.hpp @@ -9,17 +9,8 @@ class RenderState; class RendererSectionData; class RendererSection { - enum Vbos { - POSITIONS = 0, - UVS, - UVLAYERS, - ANIMATIONS, - COLORS, - LIGHTS, - VBOCOUNT, - }; GLuint Vao = { 0 }; - GLuint Vbo[VBOCOUNT] = { 0 }; + GLuint Vbo = { 0 }; size_t hash; Vector sectionPos; diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index 78e2239..e74590a 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -47,21 +47,24 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, co lightness = glm::vec2(light.face[faceDirection], skyLight.face[faceDirection]); } + data.vertices.emplace_back(); + VertexData& vertexData = data.vertices.back(); + 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.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); + vertexData.positions[0] = transformed * glm::vec4(0, 0, 0, 1); + vertexData.positions[1] = transformed * glm::vec4(0, 0, 1, 1); + vertexData.positions[2] = transformed * glm::vec4(1, 0, 1, 1); + vertexData.positions[3] = transformed * glm::vec4(1, 0, 0, 1); + + vertexData.uvs[0] = TransformTextureCoord(face.texture, glm::vec2(0, 0), face.frames); + vertexData.uvs[1] = TransformTextureCoord(face.texture, glm::vec2(1, 0), face.frames); + vertexData.uvs[2] = TransformTextureCoord(face.texture, glm::vec2(1, 1), face.frames); + vertexData.uvs[3] = TransformTextureCoord(face.texture, glm::vec2(0, 1), face.frames); + + vertexData.uvLayers = face.layer; + vertexData.animations = glm::vec2(face.texture.w / face.frames, face.frames); + vertexData.colors = face.color; + vertexData.lights = lightness; } } @@ -154,12 +157,7 @@ RendererSectionData ParseSection(const SectionsData §ions) { } } } - 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(); + data.vertices.shrink_to_fit(); return data; } diff --git a/src/RendererSectionData.hpp b/src/RendererSectionData.hpp index 7b30e44..e2fd190 100644 --- a/src/RendererSectionData.hpp +++ b/src/RendererSectionData.hpp @@ -30,13 +30,17 @@ struct SectionsData { BlockLightness GetSkyLight(const Vector &pos) const; }; +struct VertexData { + glm::vec4 positions[4]; + glm::vec2 uvs[4]; + float uvLayers; + glm::vec2 animations; + glm::vec3 colors; + glm::vec2 lights; +}; + struct RendererSectionData { - std::vector positions; //4 per instance - std::vector uvs; //4 per instance - std::vector uvLayers; - std::vector animations; - std::vector colors; - std::vector lights; + std::vector vertices; size_t hash = 0; Vector sectionPos; bool forced = false; -- cgit v1.2.3 From 08337925fe048d2e8b746bbc82493f4c2b8603d6 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Mon, 28 Jun 2021 21:31:26 +0500 Subject: Compacted vertices format --- cwd/assets/altcraft/shaders/vert/face.vs | 8 ++++---- src/RendererSection.cpp | 13 +++++-------- src/RendererSectionData.cpp | 2 +- src/RendererSectionData.hpp | 5 +++-- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/cwd/assets/altcraft/shaders/vert/face.vs b/cwd/assets/altcraft/shaders/vert/face.vs index e1f01fc..bea9228 100644 --- a/cwd/assets/altcraft/shaders/vert/face.vs +++ b/cwd/assets/altcraft/shaders/vert/face.vs @@ -1,9 +1,9 @@ #version 330 core -layout (location = 0) in vec4 position[4]; +layout (location = 0) in vec3 position[4]; layout (location = 4) in vec2 uv[4]; layout (location = 8) in float uvLayer; -layout (location = 9) in vec2 animation; +layout (location = 9) in float animation; layout (location = 10) in vec3 color; layout (location = 11) in vec2 light; @@ -17,9 +17,9 @@ uniform float GlobalTime; uniform mat4 projView; void main() { - gl_Position = projView * position[gl_VertexID]; + gl_Position = projView * vec4(position[gl_VertexID], 1.0f); 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)); + vs_out.Texture.y -= (uv[2].y - uv[0].y) * trunc(mod(GlobalTime * 4.0f, animation)); } diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index 397a660..cc58676 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -21,19 +21,19 @@ RendererSection::RendererSection(const RendererSectionData &data) { { //Cube vertices GLuint VertAttribPos = 0; - glVertexAttribPointer(VertAttribPos, 4, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[0])); + glVertexAttribPointer(VertAttribPos, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[0])); glEnableVertexAttribArray(VertAttribPos); glVertexAttribDivisor(VertAttribPos, 1); - glVertexAttribPointer(VertAttribPos + 1, 4, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[1])); + glVertexAttribPointer(VertAttribPos + 1, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[1])); glEnableVertexAttribArray(VertAttribPos + 1); glVertexAttribDivisor(VertAttribPos + 1, 1); - glVertexAttribPointer(VertAttribPos + 2, 4, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[2])); + glVertexAttribPointer(VertAttribPos + 2, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[2])); glEnableVertexAttribArray(VertAttribPos + 2); glVertexAttribDivisor(VertAttribPos + 2, 1); - glVertexAttribPointer(VertAttribPos + 3, 4, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[3])); + glVertexAttribPointer(VertAttribPos + 3, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[3])); glEnableVertexAttribArray(VertAttribPos + 3); glVertexAttribDivisor(VertAttribPos + 3, 1); glCheckError(); @@ -64,7 +64,7 @@ RendererSection::RendererSection(const RendererSectionData &data) { //Animation GLuint animationAttribPos = 9; - glVertexAttribPointer(animationAttribPos, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, animations)); + glVertexAttribPointer(animationAttribPos, 1, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, animations)); glEnableVertexAttribArray(animationAttribPos); glVertexAttribDivisor(animationAttribPos, 1); @@ -74,9 +74,6 @@ RendererSection::RendererSection(const RendererSectionData &data) { glEnableVertexAttribArray(colorAttribPos); glVertexAttribDivisor(colorAttribPos, 1); - size_t m = sizeof(VertexData); - size_t d = offsetof(VertexData, lights); - //Light GLuint lightAttribPos = 11; glVertexAttribPointer(lightAttribPos, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, lights)); diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index e74590a..2588fd6 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -62,7 +62,7 @@ void AddFacesByBlockModel(RendererSectionData &data, const BlockFaces &model, co vertexData.uvs[3] = TransformTextureCoord(face.texture, glm::vec2(0, 1), face.frames); vertexData.uvLayers = face.layer; - vertexData.animations = glm::vec2(face.texture.w / face.frames, face.frames); + vertexData.animations = face.frames; vertexData.colors = face.color; vertexData.lights = lightness; } diff --git a/src/RendererSectionData.hpp b/src/RendererSectionData.hpp index e2fd190..edd2992 100644 --- a/src/RendererSectionData.hpp +++ b/src/RendererSectionData.hpp @@ -31,12 +31,13 @@ struct SectionsData { }; struct VertexData { - glm::vec4 positions[4]; + glm::vec3 positions[4]; glm::vec2 uvs[4]; float uvLayers; - glm::vec2 animations; + float animations; glm::vec3 colors; glm::vec2 lights; + uint8_t padding[20]; }; struct RendererSectionData { -- cgit v1.2.3 From 2db3f2a415f20f1224e277cef93e6d9bb2a1244b Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Tue, 29 Jun 2021 07:53:04 +0500 Subject: Tint calculation moved to vertex shader --- cwd/assets/altcraft/shaders/frag/face.fs | 35 ++------------------------------ cwd/assets/altcraft/shaders/vert/face.vs | 8 +++++--- src/AssetManager.cpp | 2 +- 3 files changed, 8 insertions(+), 37 deletions(-) diff --git a/cwd/assets/altcraft/shaders/frag/face.fs b/cwd/assets/altcraft/shaders/frag/face.fs index 314e47b..30d44d2 100644 --- a/cwd/assets/altcraft/shaders/frag/face.fs +++ b/cwd/assets/altcraft/shaders/frag/face.fs @@ -3,47 +3,16 @@ in VS_OUT { vec3 Texture; vec3 Color; - vec2 Light; } fs_in; out vec4 fragColor; uniform sampler2DArray textureAtlas; -uniform float DayTime; -uniform float MinLightLevel; - -vec3 rgb2hsv(vec3 c) -{ - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) -{ - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} void main() { - vec4 color = texture(textureAtlas,fs_in.Texture); + vec4 color = texture(textureAtlas,fs_in.Texture); if (color.a < 0.3) discard; - vec3 hsvColor = rgb2hsv(color.xyz); - hsvColor+=fs_in.Color; - color = vec4(hsv2rgb(hsvColor),1); - - float light = fs_in.Light.x / 15.0; - float skyLight = (fs_in.Light.y / 15.0) * DayTime; - - float faceLight = clamp(light + skyLight,MinLightLevel,1.0); - - color = vec4(color.rgb * faceLight, color.a); - fragColor = color; + fragColor = vec4(color.rgb * fs_in.Color, 1.0); } diff --git a/cwd/assets/altcraft/shaders/vert/face.vs b/cwd/assets/altcraft/shaders/vert/face.vs index bea9228..469ea94 100644 --- a/cwd/assets/altcraft/shaders/vert/face.vs +++ b/cwd/assets/altcraft/shaders/vert/face.vs @@ -10,16 +10,18 @@ layout (location = 11) in vec2 light; out VS_OUT { vec3 Texture; vec3 Color; - vec2 Light; } vs_out; uniform float GlobalTime; uniform mat4 projView; +uniform float DayTime; +uniform float MinLightLevel; void main() { gl_Position = projView * vec4(position[gl_VertexID], 1.0f); - vs_out.Color = color; - vs_out.Light = light; vs_out.Texture = vec3(uv[gl_VertexID], uvLayer); vs_out.Texture.y -= (uv[2].y - uv[0].y) * trunc(mod(GlobalTime * 4.0f, animation)); + + float faceLight = clamp(light.x / 15.0 + (light.y / 15.0) * DayTime, MinLightLevel, 1.0); + vs_out.Color = mix(color.rgb * faceLight, vec3(1,1,1) * faceLight, (color == vec3(0,0,0))); } diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 63dc596..5f24404 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -598,7 +598,7 @@ void ParseBlockModels() { parsedFace.layer = texture.layer; parsedFace.frames = textureFrames; if (face.second.tintIndex) - parsedFace.color = glm::vec3(0.275, 0.63, 0.1); + parsedFace.color = glm::vec3(0.486, 0.745, 0.423); else parsedFace.color = glm::vec3(0, 0, 0); -- cgit v1.2.3