BlocksRenderer light conpressing & fix

This commit is contained in:
@clasher113 2023-11-22 19:15:26 +02:00
parent b472626d8d
commit 13a9641392
3 changed files with 28 additions and 11 deletions

View File

@ -2,7 +2,7 @@
layout (location = 0) in vec3 v_position; layout (location = 0) in vec3 v_position;
layout (location = 1) in vec2 v_texCoord; 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 vec4 a_color;
out vec2 a_texCoord; out vec2 a_texCoord;
@ -18,18 +18,27 @@ uniform float u_gamma;
uniform vec3 u_torchlightColor; uniform vec3 u_torchlightColor;
uniform float u_torchlightDistance; 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(){ void main(){
vec2 pos2d = (u_model * vec4(v_position, 1.0)).xz-u_cameraPos.xz; 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 modelpos = u_model * vec4(v_position+vec3(0,pow(length(pos2d)*0.0, 3.0),0), 1.0);
vec4 viewmodelpos = u_view * modelpos; 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); float torchlight = max(0.0, 1.0-distance(u_cameraPos, modelpos.xyz)/u_torchlightDistance);
light += torchlight * u_torchlightColor; light += torchlight * u_torchlightColor;
a_color = vec4(pow(light, vec3(u_gamma)),1.0f); a_color = vec4(pow(light, vec3(u_gamma)),1.0f);
a_texCoord = v_texCoord; a_texCoord = v_texCoord;
a_color.r = max(a_color.r, u_skyLightColor.r*v_light.a); a_color.rgb = max(a_color.rgb, u_skyLightColor.rgb*decomp_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_distance = length(viewmodelpos); a_distance = length(viewmodelpos);
gl_Position = u_proj * viewmodelpos; gl_Position = u_proj * viewmodelpos;
} }

View File

@ -17,7 +17,7 @@ using glm::ivec3;
using glm::vec3; using glm::vec3;
using glm::vec4; using glm::vec4;
#define VERTEX_SIZE 9 #define VERTEX_SIZE 6
BlocksRenderer::BlocksRenderer(size_t capacity, BlocksRenderer::BlocksRenderer(size_t capacity,
const Content* content, const Content* content,
@ -50,10 +50,17 @@ void BlocksRenderer::vertex(const vec3& coord,
vertexBuffer[vertexOffset++] = u; vertexBuffer[vertexOffset++] = u;
vertexBuffer[vertexOffset++] = v; vertexBuffer[vertexOffset++] = v;
vertexBuffer[vertexOffset++] = light.r; union {
vertexBuffer[vertexOffset++] = light.g; float floating;
vertexBuffer[vertexOffset++] = light.b; uint32_t integer;
vertexBuffer[vertexOffset++] = light.a; } 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) { 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; const voxel* voxels = chunk->voxels;
render(voxels, atlas_size); 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); Mesh* mesh = new Mesh(vertexBuffer, vertexOffset / VERTEX_SIZE, indexBuffer, indexSize, attrs);
return mesh; return mesh;
} }

View File

@ -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*/) { void key_callback(GLFWwindow*, int key, int scancode, int action, int /*mode*/) {
if (key == GLFW_KEY_UNKNOWN) return;
if (action == GLFW_PRESS) { if (action == GLFW_PRESS) {
Events::_keys[key] = true; Events::_keys[key] = true;
Events::_frames[key] = Events::_current; Events::_frames[key] = Events::_current;