From c2998258593c47874e91931be283d96697aa7351 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Thu, 19 Mar 2026 17:17:33 -0500 Subject: [PATCH] re-enable lights --- include/lighting.h | 5 ++--- include/opengl.h | 2 +- include/world/world.h | 3 ++- shader/lighting.frag | 19 +++++++++---------- shader/minecraft.frag | 3 ++- src/lighting.cpp | 21 ++++++++++++++++----- src/minecraft.cpp | 4 +++- src/opengl.cpp | 4 +++- src/test.cpp | 6 ++++-- 9 files changed, 42 insertions(+), 25 deletions(-) diff --git a/include/lighting.h b/include/lighting.h index 7e9d4ba..4ae3fde 100644 --- a/include/lighting.h +++ b/include/lighting.h @@ -9,7 +9,6 @@ namespace lighting { extern light_parameters global; - void load_program(); - void load_light_uniform_buffer(); - void draw(); + void load(); + void draw(unsigned int light_uniform_buffer, int light_count); } diff --git a/include/opengl.h b/include/opengl.h index 23ca694..1285562 100644 --- a/include/opengl.h +++ b/include/opengl.h @@ -8,7 +8,7 @@ extern "C" { const char * geometry_path, const char * fragment_path); - unsigned int load_uniform_buffer(char const * const path); + unsigned int load_uniform_buffer(char const * const path, int * out_size); void load_dds_texture_2D(char const * const path); diff --git a/include/world/world.h b/include/world/world.h index a1e8ff0..eb1b3fa 100644 --- a/include/world/world.h +++ b/include/world/world.h @@ -42,8 +42,9 @@ namespace world { struct state { world::descriptor const * descriptor; world::region * region; // malloc region_count - unsigned int light_uniform_buffer; entry_table::global_entry_t * entry_table; + unsigned int light_uniform_buffer; + int light_count; int entry_table_length; }; diff --git a/shader/lighting.frag b/shader/lighting.frag index 3f4e4c2..71e1f57 100644 --- a/shader/lighting.frag +++ b/shader/lighting.frag @@ -7,6 +7,7 @@ uniform sampler2D ColorSampler; uniform float Linear; uniform float Quadratic; uniform vec3 Eye; +uniform int LightCount; layout (location = 0) out vec4 Color; @@ -24,23 +25,21 @@ void main() vec4 color = texture(ColorSampler, PixelTexture.xy); vec3 out_color = color.xyz * 0.1; - for (int i = 0; i < 82; i++) { - vec3 light_position = light[i].xzy; + for (int i = 0; i < LightCount; i++) { + vec3 light_position = light[i].xzy + vec3(0, 0, 0.5); float light_distance = length(light_position - position.xyz); vec3 light_direction = normalize(light_position - position.xyz); float diffuse = max(dot(normal.xyz, light_direction), 0.0); + if (normal.w == 1.0) // two-sided + diffuse = 1.0; + //float attenuation = 1.0 / (1.0 + Linear * light_distance + Quadratic * light_distance * light_distance); - float attenuation = 1.0 / (1.0 + Quadratic * light_distance * light_distance); - //out_color += color.xyz * attenuation * diffuse; - //out_color = vec3(diffuse); + out_color += color.xyz * attenuation * diffuse; } - vec3 light_direction = normalize(Eye.xyz - position.xyz); - float diffuse = max(dot(normal.xyz, light_direction), 0.0); - if (normal.w == 1.0) // two-sided - diffuse = 1.0; + //vec3 light_direction = normalize(Eye.xyz - position.xyz); + //float diffuse = max(dot(normal.xyz, light_direction), 0.0); - out_color = color.xyz * diffuse; Color = vec4(out_color, 1.0); } diff --git a/shader/minecraft.frag b/shader/minecraft.frag index 9d46d3f..f2f1872 100644 --- a/shader/minecraft.frag +++ b/shader/minecraft.frag @@ -37,7 +37,8 @@ void main() float two_sided = float(fs_in.Special == -1); // special - Position = fs_in.BlockPosition.xzy; + //Position = fs_in.BlockPosition.xzy; + Position = fs_in.Position.xzy; Normal = vec4(normalize(fs_in.Normal.xzy), two_sided); Color = texture_color.xyz; Block = vec4(fs_in.BlockID, fs_in.Data, fs_in.TextureID, fs_in.Special); diff --git a/src/lighting.cpp b/src/lighting.cpp index aa86303..efd21fc 100644 --- a/src/lighting.cpp +++ b/src/lighting.cpp @@ -28,6 +28,7 @@ namespace lighting { unsigned int quadratic; unsigned int linear; unsigned int eye; + unsigned int light_count; unsigned int lights; } uniform; @@ -38,7 +39,7 @@ namespace lighting { static unsigned int program; static location location; - void load_program() + static void load_program() { program = compile_from_files("shader/quad.vert", NULL, @@ -50,25 +51,35 @@ namespace lighting { location.uniform.quadratic = glGetUniformLocation(program, "Quadratic"); location.uniform.linear = glGetUniformLocation(program, "Linear"); location.uniform.eye = glGetUniformLocation(program, "Eye"); + location.uniform.light_count = glGetUniformLocation(program, "LightCount"); location.uniform.lights = glGetUniformBlockIndex(program, "Lights"); fprintf(stderr, "lighting program:\n"); - fprintf(stderr, " uniforms:\n position_sampler %u normal_sampler %u color_sampler %u lights %u\n", + fprintf(stderr, " uniforms:\n position_sampler %u normal_sampler %u color_sampler %u quadratic %u\n linear %u\n eye %u\n light_count %u\n lights %u\n", location.uniform.position_sampler, location.uniform.normal_sampler, location.uniform.color_sampler, + location.uniform.quadratic, + location.uniform.linear, + location.uniform.eye, + location.uniform.light_count, location.uniform.lights); location.binding.lights = 0; glUniformBlockBinding(program, location.uniform.lights, location.binding.lights); } + void load() + { + load_program(); + } + static inline bool near_zero(float a) { return (fabsf(a) < 0.00001f); } - void draw() + void draw(unsigned int light_uniform_buffer, int light_count) { glUseProgram(program); glDepthFunc(GL_ALWAYS); @@ -89,12 +100,12 @@ namespace lighting { glUniform1f(location.uniform.quadratic, quadratic); glUniform1f(location.uniform.linear, linear); - XMFLOAT3 eye; XMStoreFloat3(&eye, view::state.eye); glUniform3fv(location.uniform.eye, 1, (float*)&eye); - //glBindBufferBase(GL_UNIFORM_BUFFER, location.binding.lights, light_uniform_buffer); + glUniform1i(location.uniform.light_count, light_count); + glBindBufferBase(GL_UNIFORM_BUFFER, location.binding.lights, light_uniform_buffer); glBindVertexArray(empty_vertex_array_object); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_index_buffer); diff --git a/src/minecraft.cpp b/src/minecraft.cpp index 7bf8033..760e86b 100644 --- a/src/minecraft.cpp +++ b/src/minecraft.cpp @@ -223,7 +223,9 @@ namespace minecraft { state.region = New(descriptor->region_count); load_regions(descriptor, state.region); - state.light_uniform_buffer = load_uniform_buffer(descriptor->lights_path); + int light_buffer_size; + state.light_uniform_buffer = load_uniform_buffer(descriptor->lights_path, &light_buffer_size); + state.light_count = light_buffer_size / ((sizeof (float)) * 4); // collision data world::entry_table::load_entry_table(descriptor->entry_table_path, diff --git a/src/opengl.cpp b/src/opengl.cpp index 0806181..77c4d2c 100644 --- a/src/opengl.cpp +++ b/src/opengl.cpp @@ -109,7 +109,7 @@ unsigned int compile_from_files(char const * vertex_path, return program; } -unsigned int load_uniform_buffer(char const * const path) +unsigned int load_uniform_buffer(char const * const path, int * out_size) { unsigned int buffer; glGenBuffers(1, &buffer); @@ -124,6 +124,8 @@ unsigned int load_uniform_buffer(char const * const path) glBindBuffer(GL_UNIFORM_BUFFER, 0); + *out_size = data_size; + return buffer; } diff --git a/src/test.cpp b/src/test.cpp index a168c45..37df43d 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -164,7 +164,7 @@ void load(const char * source_path) // lighting ////////////////////////////////////////////////////////////////////// - lighting::load_program(); + lighting::load(); ////////////////////////////////////////////////////////////////////// // non_block @@ -424,7 +424,9 @@ void draw() glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - lighting::draw(); + + lighting::draw(minecraft::current_world->light_uniform_buffer, + minecraft::current_world->light_count); //draw_quad(); hud::draw(); } else {