diff options
-rw-r--r-- | cwd/assets/altcraft/shaders/frag/face.fs | 38 | ||||
-rw-r--r-- | cwd/assets/altcraft/shaders/vert/face.vs | 48 | ||||
-rw-r--r-- | src/AssetManager.cpp | 2 | ||||
-rw-r--r-- | src/RendererSection.cpp | 172 | ||||
-rw-r--r-- | src/RendererSection.hpp | 13 | ||||
-rw-r--r-- | src/RendererSectionData.cpp | 39 | ||||
-rw-r--r-- | src/RendererSectionData.hpp | 17 |
7 files changed, 114 insertions, 215 deletions
diff --git a/cwd/assets/altcraft/shaders/frag/face.fs b/cwd/assets/altcraft/shaders/frag/face.fs index bd2a2d7..30d44d2 100644 --- a/cwd/assets/altcraft/shaders/frag/face.fs +++ b/cwd/assets/altcraft/shaders/frag/face.fs @@ -1,50 +1,18 @@ #version 330 core in VS_OUT { - vec2 UvPosition; 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; -}
\ No newline at end of file + 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 30ae0d7..469ea94 100644 --- a/cwd/assets/altcraft/shaders/vert/face.vs +++ b/cwd/assets/altcraft/shaders/vert/face.vs @@ -1,47 +1,27 @@ #version 330 core -layout (location = 0) in vec3 position; -layout (location = 2) in vec2 UvCoordinates; -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; -layout (location = 15) in float TextureFrames; +layout (location = 0) in vec3 position[4]; +layout (location = 4) in vec2 uv[4]; +layout (location = 8) in float uvLayer; +layout (location = 9) in float animation; +layout (location = 10) in vec3 color; +layout (location = 11) in vec2 light; out VS_OUT { - vec2 UvPosition; vec3 Texture; vec3 Color; - vec2 Light; } vs_out; uniform float GlobalTime; uniform mat4 projView; +uniform float DayTime; +uniform float MinLightLevel; -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); -} - -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; +void main() { + gl_Position = projView * vec4(position[gl_VertexID], 1.0f); + vs_out.Texture = vec3(uv[gl_VertexID], uvLayer); + vs_out.Texture.y -= (uv[2].y - uv[0].y) * trunc(mod(GlobalTime * 4.0f, animation)); - vs_out.UvPosition = UvCoordinates; - vs_out.Texture = TransformTextureCoord(texturePos,UvCoordinates,TextureLayer); - vs_out.Color = color; - vs_out.Light = light; + 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 d5b8d68..9cbb6df 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -582,7 +582,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); diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index d797771..cc58676 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -1,5 +1,7 @@ #include "RendererSection.hpp" +#include <cstddef> + #include <easylogging++.h> #include <optick.h> @@ -7,118 +9,74 @@ #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, - 0.0f, 1.0f, - - 0.0f, 0.0f, - 1.0f, 0.0f, - 1.0f, 1.0f, -}; - -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); - 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"; - } 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, VboVertices); - glVertexAttribPointer(VertAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr); + glVertexAttribPointer(VertAttribPos, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[0])); glEnableVertexAttribArray(VertAttribPos); + glVertexAttribDivisor(VertAttribPos, 1); - //Cube UVs - GLuint UvAttribPos = 2; - glBindBuffer(GL_ARRAY_BUFFER, VboUvs); - glVertexAttribPointer(UvAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr); - glEnableVertexAttribArray(UvAttribPos); + 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, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[2])); + glEnableVertexAttribArray(VertAttribPos + 2); + glVertexAttribDivisor(VertAttribPos + 2, 1); - //Textures - GLuint textureAttribPos = 7; - glBindBuffer(GL_ARRAY_BUFFER, Vbo[TEXTURES]); - glVertexAttribPointer(textureAttribPos, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr); - glEnableVertexAttribArray(textureAttribPos); - glVertexAttribDivisor(textureAttribPos, 1); + glVertexAttribPointer(VertAttribPos + 3, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, positions[3])); + glEnableVertexAttribArray(VertAttribPos + 3); + glVertexAttribDivisor(VertAttribPos + 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(); - - //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); + //Cube uvs + GLuint UvAttribPos = 4; + 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, sizeof(VertexData), (void*)offsetof(VertexData, uvs[1])); + glEnableVertexAttribArray(UvAttribPos + 1); + glVertexAttribDivisor(UvAttribPos + 1, 1); + + 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, sizeof(VertexData), (void*)offsetof(VertexData, uvs[3])); + glEnableVertexAttribArray(UvAttribPos + 3); + glVertexAttribDivisor(UvAttribPos + 3, 1); + + //Uv Layer + GLuint uvLayerAttribPos = 8; + glVertexAttribPointer(uvLayerAttribPos, 1, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, uvLayers)); + glEnableVertexAttribArray(uvLayerAttribPos); + glVertexAttribDivisor(uvLayerAttribPos, 1); + + //Animation + GLuint animationAttribPos = 9; + glVertexAttribPointer(animationAttribPos, 1, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, animations)); + glEnableVertexAttribArray(animationAttribPos); + glVertexAttribDivisor(animationAttribPos, 1); //Color - GLuint colorAttribPos = 12; - glBindBuffer(GL_ARRAY_BUFFER, Vbo[COLORS]); - glVertexAttribPointer(colorAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr); + GLuint colorAttribPos = 10; + glVertexAttribPointer(colorAttribPos, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, colors)); glEnableVertexAttribArray(colorAttribPos); glVertexAttribDivisor(colorAttribPos, 1); //Light - GLuint lightAttribPos = 13; - glBindBuffer(GL_ARRAY_BUFFER, Vbo[LIGHTS]); - glVertexAttribPointer(lightAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr); + GLuint lightAttribPos = 11; + glVertexAttribPointer(lightAttribPos, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)offsetof(VertexData, lights)); glEnableVertexAttribArray(lightAttribPos); glVertexAttribDivisor(lightAttribPos, 1); @@ -139,13 +97,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) { @@ -159,7 +111,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(); } @@ -173,28 +125,14 @@ 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[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[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.textures.size(); + numOfFaces = data.vertices.size(); sectionPos = data.sectionPos; hash = data.hash; } diff --git a/src/RendererSection.hpp b/src/RendererSection.hpp index 29b5db1..3ea1fec 100644 --- a/src/RendererSection.hpp +++ b/src/RendererSection.hpp @@ -9,20 +9,9 @@ class RenderState; class RendererSectionData; class RendererSection { - enum Vbos { - MODELS = 0, - TEXTURES, - LAYERS, - FRAMES, - COLORS, - LIGHTS, - VBOCOUNT, - }; GLuint Vao = { 0 }; - GLuint Vbo[VBOCOUNT] = { 0 }; + GLuint Vbo = { 0 }; - static GLuint VboVertices, VboUvs; - size_t hash; Vector sectionPos; diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index 69ed665..2588fd6 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,12 +46,25 @@ 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); - 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); + + data.vertices.emplace_back(); + VertexData& vertexData = data.vertices.back(); + + glm::mat4 transformed = transform * model.transform * face.transform; + 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 = face.frames; + vertexData.colors = face.color; + vertexData.lights = lightness; } } @@ -135,10 +157,7 @@ RendererSectionData ParseSection(const SectionsData §ions) { } } } - data.textures.shrink_to_fit(); - data.textureLayers.shrink_to_fit(); - data.models.shrink_to_fit(); - data.colors.shrink_to_fit(); + data.vertices.shrink_to_fit(); return data; } diff --git a/src/RendererSectionData.hpp b/src/RendererSectionData.hpp index cedb51b..edd2992 100644 --- a/src/RendererSectionData.hpp +++ b/src/RendererSectionData.hpp @@ -30,13 +30,18 @@ struct SectionsData { BlockLightness GetSkyLight(const Vector &pos) const; }; +struct VertexData { + glm::vec3 positions[4]; + glm::vec2 uvs[4]; + float uvLayers; + float animations; + glm::vec3 colors; + glm::vec2 lights; + uint8_t padding[20]; +}; + struct RendererSectionData { - std::vector<glm::mat4> models; - std::vector<glm::vec4> textures; - std::vector<float> textureLayers; - std::vector<float> textureFrames; - std::vector<glm::vec3> colors; - std::vector<glm::vec2> lights; + std::vector<VertexData> vertices; size_t hash = 0; Vector sectionPos; bool forced = false; |