From 13a96413922ce46e32e03bb1606a8512117521bf Mon Sep 17 00:00:00 2001 From: "@clasher113" Date: Wed, 22 Nov 2023 19:15:26 +0200 Subject: [PATCH] BlocksRenderer light conpressing & fix --- res/shaders/main.glslv | 19 ++++++++++++++----- src/graphics/BlocksRenderer.cpp | 19 +++++++++++++------ src/window/Window.cpp | 1 + 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/res/shaders/main.glslv b/res/shaders/main.glslv index b1412148..ccd89eaa 100644 --- a/res/shaders/main.glslv +++ b/res/shaders/main.glslv @@ -2,7 +2,7 @@ layout (location = 0) in vec3 v_position; layout (location = 1) in vec2 v_texCoord; -layout (location = 2) in vec4 v_light; +layout (location = 2) in float v_light; out vec4 a_color; out vec2 a_texCoord; @@ -18,18 +18,27 @@ uniform float u_gamma; uniform vec3 u_torchlightColor; uniform float u_torchlightDistance; +vec4 decompress_light(float compressed_light) { + vec4 result; + int compressed = floatBitsToInt(compressed_light); + result.r = ((compressed >> 24) & 0xFF) / 255.f; + result.g = ((compressed >> 16) & 0xFF) / 255.f; + result.b = ((compressed >> 8) & 0xFF) / 255.f; + result.a = (compressed & 0xFF) / 255.f; + return result; +} + void main(){ vec2 pos2d = (u_model * vec4(v_position, 1.0)).xz-u_cameraPos.xz; vec4 modelpos = u_model * vec4(v_position+vec3(0,pow(length(pos2d)*0.0, 3.0),0), 1.0); vec4 viewmodelpos = u_view * modelpos; - vec3 light = v_light.rgb * 1.6; + vec4 decomp_light = decompress_light(v_light); + vec3 light = decomp_light.rgb * 1.6; float torchlight = max(0.0, 1.0-distance(u_cameraPos, modelpos.xyz)/u_torchlightDistance); light += torchlight * u_torchlightColor; a_color = vec4(pow(light, vec3(u_gamma)),1.0f); a_texCoord = v_texCoord; - a_color.r = max(a_color.r, u_skyLightColor.r*v_light.a); - a_color.g = max(a_color.g, u_skyLightColor.g*v_light.a); - a_color.b = max(a_color.b, u_skyLightColor.b*v_light.a); + a_color.rgb = max(a_color.rgb, u_skyLightColor.rgb*decomp_light.a); a_distance = length(viewmodelpos); gl_Position = u_proj * viewmodelpos; } diff --git a/src/graphics/BlocksRenderer.cpp b/src/graphics/BlocksRenderer.cpp index 4caabf92..a11ea203 100644 --- a/src/graphics/BlocksRenderer.cpp +++ b/src/graphics/BlocksRenderer.cpp @@ -17,7 +17,7 @@ using glm::ivec3; using glm::vec3; using glm::vec4; -#define VERTEX_SIZE 9 +#define VERTEX_SIZE 6 BlocksRenderer::BlocksRenderer(size_t capacity, const Content* content, @@ -50,10 +50,17 @@ void BlocksRenderer::vertex(const vec3& coord, vertexBuffer[vertexOffset++] = u; vertexBuffer[vertexOffset++] = v; - vertexBuffer[vertexOffset++] = light.r; - vertexBuffer[vertexOffset++] = light.g; - vertexBuffer[vertexOffset++] = light.b; - vertexBuffer[vertexOffset++] = light.a; + union { + float floating; + uint32_t integer; + } compressed; + + compressed.integer = (uint32_t(light.r * 255) & 0xff) << 24; + compressed.integer |= (uint32_t(light.g * 255) & 0xff) << 16; + compressed.integer |= (uint32_t(light.b * 255) & 0xff) << 8; + compressed.integer |= (uint32_t(light.a * 255) & 0xff); + + vertexBuffer[vertexOffset++] = compressed.floating; } void BlocksRenderer::index(int a, int b, int c, int d, int e, int f) { @@ -353,7 +360,7 @@ Mesh* BlocksRenderer::render(const Chunk* chunk, int atlas_size, const ChunksSto const voxel* voxels = chunk->voxels; render(voxels, atlas_size); - const vattr attrs[]{ {3}, {2}, {4}, {0} }; + const vattr attrs[]{ {3}, {2}, {1}, {0} }; Mesh* mesh = new Mesh(vertexBuffer, vertexOffset / VERTEX_SIZE, indexBuffer, indexSize, attrs); return mesh; } diff --git a/src/window/Window.cpp b/src/window/Window.cpp index c60be618..562e6f67 100644 --- a/src/window/Window.cpp +++ b/src/window/Window.cpp @@ -38,6 +38,7 @@ void mouse_button_callback(GLFWwindow*, int button, int action, int) { } void key_callback(GLFWwindow*, int key, int scancode, int action, int /*mode*/) { + if (key == GLFW_KEY_UNKNOWN) return; if (action == GLFW_PRESS) { Events::_keys[key] = true; Events::_frames[key] = Events::_current;