diff --git a/res/shaders/main.glslv b/res/shaders/main.glslv index 1ebe9472..6e1303fb 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; + vec4 decomp_light = decompress_light(v_light); + vec3 light = decomp_light.rgb; 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..fd10826f 100644 --- a/src/graphics/BlocksRenderer.cpp +++ b/src/graphics/BlocksRenderer.cpp @@ -17,17 +17,17 @@ 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, - const ContentGfxCache* cache) - : content(content), - vertexOffset(0), - indexOffset(0), - indexSize(0), - capacity(capacity), - cache(cache) { +BlocksRenderer::BlocksRenderer(size_t capacity, + const Content* content, + const ContentGfxCache* cache) + : content(content), + vertexOffset(0), + indexOffset(0), + indexSize(0), + capacity(capacity), + cache(cache) { vertexBuffer = new float[capacity]; indexBuffer = new int[capacity]; voxelsBuffer = new VoxelsVolume(CHUNK_W + 2, CHUNK_H, CHUNK_D + 2); @@ -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) { @@ -72,7 +79,7 @@ void BlocksRenderer::face(const vec3& coord, float w, float h, const UVRegion& region, const vec4(&lights)[4], const vec4& tint) { - if (vertexOffset + VERTEX_SIZE * 6 > capacity) { + if (vertexOffset + VERTEX_SIZE * 4 > capacity) { overflow = true; return; } @@ -90,7 +97,7 @@ void BlocksRenderer::face(const vec3& coord, float w, float h, const vec4(&lights)[4], const vec4& tint, bool rotated) { - if (vertexOffset + VERTEX_SIZE * 6 > capacity) { + if (vertexOffset + VERTEX_SIZE * 4 > capacity) { overflow = true; return; } @@ -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;