re-enable lights

This commit is contained in:
Zack Buhman 2026-03-19 17:17:33 -05:00
parent 9da3ffa0bb
commit c299825859
9 changed files with 42 additions and 25 deletions

View File

@ -9,7 +9,6 @@ namespace lighting {
extern light_parameters global; extern light_parameters global;
void load_program(); void load();
void load_light_uniform_buffer(); void draw(unsigned int light_uniform_buffer, int light_count);
void draw();
} }

View File

@ -8,7 +8,7 @@ extern "C" {
const char * geometry_path, const char * geometry_path,
const char * fragment_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); void load_dds_texture_2D(char const * const path);

View File

@ -42,8 +42,9 @@ namespace world {
struct state { struct state {
world::descriptor const * descriptor; world::descriptor const * descriptor;
world::region * region; // malloc region_count world::region * region; // malloc region_count
unsigned int light_uniform_buffer;
entry_table::global_entry_t * entry_table; entry_table::global_entry_t * entry_table;
unsigned int light_uniform_buffer;
int light_count;
int entry_table_length; int entry_table_length;
}; };

View File

@ -7,6 +7,7 @@ uniform sampler2D ColorSampler;
uniform float Linear; uniform float Linear;
uniform float Quadratic; uniform float Quadratic;
uniform vec3 Eye; uniform vec3 Eye;
uniform int LightCount;
layout (location = 0) out vec4 Color; layout (location = 0) out vec4 Color;
@ -24,23 +25,21 @@ void main()
vec4 color = texture(ColorSampler, PixelTexture.xy); vec4 color = texture(ColorSampler, PixelTexture.xy);
vec3 out_color = color.xyz * 0.1; vec3 out_color = color.xyz * 0.1;
for (int i = 0; i < 82; i++) { for (int i = 0; i < LightCount; i++) {
vec3 light_position = light[i].xzy; vec3 light_position = light[i].xzy + vec3(0, 0, 0.5);
float light_distance = length(light_position - position.xyz); float light_distance = length(light_position - position.xyz);
vec3 light_direction = normalize(light_position - position.xyz); vec3 light_direction = normalize(light_position - position.xyz);
float diffuse = max(dot(normal.xyz, light_direction), 0.0); float diffuse = max(dot(normal.xyz, light_direction), 0.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);
}
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 if (normal.w == 1.0) // two-sided
diffuse = 1.0; diffuse = 1.0;
out_color = color.xyz * diffuse; //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;
}
//vec3 light_direction = normalize(Eye.xyz - position.xyz);
//float diffuse = max(dot(normal.xyz, light_direction), 0.0);
Color = vec4(out_color, 1.0); Color = vec4(out_color, 1.0);
} }

View File

@ -37,7 +37,8 @@ void main()
float two_sided = float(fs_in.Special == -1); // special 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); Normal = vec4(normalize(fs_in.Normal.xzy), two_sided);
Color = texture_color.xyz; Color = texture_color.xyz;
Block = vec4(fs_in.BlockID, fs_in.Data, fs_in.TextureID, fs_in.Special); Block = vec4(fs_in.BlockID, fs_in.Data, fs_in.TextureID, fs_in.Special);

View File

@ -28,6 +28,7 @@ namespace lighting {
unsigned int quadratic; unsigned int quadratic;
unsigned int linear; unsigned int linear;
unsigned int eye; unsigned int eye;
unsigned int light_count;
unsigned int lights; unsigned int lights;
} uniform; } uniform;
@ -38,7 +39,7 @@ namespace lighting {
static unsigned int program; static unsigned int program;
static location location; static location location;
void load_program() static void load_program()
{ {
program = compile_from_files("shader/quad.vert", program = compile_from_files("shader/quad.vert",
NULL, NULL,
@ -50,25 +51,35 @@ namespace lighting {
location.uniform.quadratic = glGetUniformLocation(program, "Quadratic"); location.uniform.quadratic = glGetUniformLocation(program, "Quadratic");
location.uniform.linear = glGetUniformLocation(program, "Linear"); location.uniform.linear = glGetUniformLocation(program, "Linear");
location.uniform.eye = glGetUniformLocation(program, "Eye"); location.uniform.eye = glGetUniformLocation(program, "Eye");
location.uniform.light_count = glGetUniformLocation(program, "LightCount");
location.uniform.lights = glGetUniformBlockIndex(program, "Lights"); location.uniform.lights = glGetUniformBlockIndex(program, "Lights");
fprintf(stderr, "lighting program:\n"); 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.position_sampler,
location.uniform.normal_sampler, location.uniform.normal_sampler,
location.uniform.color_sampler, location.uniform.color_sampler,
location.uniform.quadratic,
location.uniform.linear,
location.uniform.eye,
location.uniform.light_count,
location.uniform.lights); location.uniform.lights);
location.binding.lights = 0; location.binding.lights = 0;
glUniformBlockBinding(program, location.uniform.lights, location.binding.lights); glUniformBlockBinding(program, location.uniform.lights, location.binding.lights);
} }
void load()
{
load_program();
}
static inline bool near_zero(float a) static inline bool near_zero(float a)
{ {
return (fabsf(a) < 0.00001f); return (fabsf(a) < 0.00001f);
} }
void draw() void draw(unsigned int light_uniform_buffer, int light_count)
{ {
glUseProgram(program); glUseProgram(program);
glDepthFunc(GL_ALWAYS); glDepthFunc(GL_ALWAYS);
@ -89,12 +100,12 @@ namespace lighting {
glUniform1f(location.uniform.quadratic, quadratic); glUniform1f(location.uniform.quadratic, quadratic);
glUniform1f(location.uniform.linear, linear); glUniform1f(location.uniform.linear, linear);
XMFLOAT3 eye; XMFLOAT3 eye;
XMStoreFloat3(&eye, view::state.eye); XMStoreFloat3(&eye, view::state.eye);
glUniform3fv(location.uniform.eye, 1, (float*)&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); glBindVertexArray(empty_vertex_array_object);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_index_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_index_buffer);

View File

@ -223,7 +223,9 @@ namespace minecraft {
state.region = New<world::region>(descriptor->region_count); state.region = New<world::region>(descriptor->region_count);
load_regions(descriptor, state.region); 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 // collision data
world::entry_table::load_entry_table(descriptor->entry_table_path, world::entry_table::load_entry_table(descriptor->entry_table_path,

View File

@ -109,7 +109,7 @@ unsigned int compile_from_files(char const * vertex_path,
return program; 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; unsigned int buffer;
glGenBuffers(1, &buffer); glGenBuffers(1, &buffer);
@ -124,6 +124,8 @@ unsigned int load_uniform_buffer(char const * const path)
glBindBuffer(GL_UNIFORM_BUFFER, 0); glBindBuffer(GL_UNIFORM_BUFFER, 0);
*out_size = data_size;
return buffer; return buffer;
} }

View File

@ -164,7 +164,7 @@ void load(const char * source_path)
// lighting // lighting
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
lighting::load_program(); lighting::load();
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// non_block // non_block
@ -424,7 +424,9 @@ void draw()
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 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(); //draw_quad();
hud::draw(); hud::draw();
} else { } else {