diffuse raytracing
This commit is contained in:
parent
729fcb8559
commit
86135cb2ce
6 changed files with 57 additions and 24 deletions
shaders
|
@ -134,24 +134,19 @@ vec3 get_light_color(uint light_index) {
|
|||
return vec3(float(scene_info.infos[light_index + 3]) / 255.0, float(scene_info.infos[light_index + 4]) / 255.0, float(scene_info.infos[light_index + 5]) / 255.0);
|
||||
}
|
||||
|
||||
void main() {
|
||||
uint max_length = scene_info.infos[0];
|
||||
uint last = scene_info.infos[max_length];
|
||||
|
||||
uvec4 color_roughness = sample_color_from_scene_info(fragVolumeStart, fragRasterPos, facing);
|
||||
vec4 orig_color_sample = vec4(float(color_roughness.x) / 255.0, float(color_roughness.y) / 255.0, float(color_roughness.z) / 255.0, 1);
|
||||
|
||||
vec3 get_lighting_color(uint volume_start, vec3 starting_pos, vec4 orig_color_sample) {
|
||||
uint max_light_num = scene_info.infos[0];
|
||||
uint light_num = 0;
|
||||
uint volume_index = fragVolumeStart;
|
||||
|
||||
// setup volume info
|
||||
uint volume_index = volume_start;
|
||||
uint volume_pos_x = scene_info.infos[volume_index + 0];
|
||||
uint volume_pos_y = scene_info.infos[volume_index + 1];
|
||||
uint volume_pos_z = scene_info.infos[volume_index + 2];
|
||||
|
||||
// setup light info
|
||||
uint light_index = scene_info.infos[fragVolumeStart + 6 + light_num];
|
||||
vec3 light_direction = get_light_position(light_index) - origPosition;
|
||||
uint light_index = scene_info.infos[volume_start + 6 + light_num];
|
||||
vec3 light_direction = get_light_position(light_index) - starting_pos;
|
||||
vec3 light_color = get_light_color(light_index);
|
||||
|
||||
bool x_pos = light_direction.x > 0.0;
|
||||
|
@ -166,7 +161,7 @@ void main() {
|
|||
// initialize color
|
||||
vec3 color_sum = vec3(0.0, 0.0, 0.0) + (orig_color_sample.xyz * 0.01);
|
||||
|
||||
uint max_iterations = max_light_num * 20;
|
||||
uint max_iterations = max_light_num * scene_info.infos[1];
|
||||
for (int i = 0; i < max_iterations; i++) {
|
||||
float x_border = float(volume_pos_x + (scene_info.infos[volume_index + 3]) * uint(x_pos)) - 0.5;
|
||||
float y_border = float(volume_pos_y + (scene_info.infos[volume_index + 4]) * uint(y_pos)) - 0.5;
|
||||
|
@ -179,18 +174,18 @@ void main() {
|
|||
float y_factor = 2.0;
|
||||
float z_factor = 2.0;
|
||||
if (!x_null) {
|
||||
x_factor = (x_border - origPosition.x) / light_direction.x;
|
||||
x_factor = (x_border - starting_pos.x) / light_direction.x;
|
||||
}
|
||||
if (!y_null) {
|
||||
y_factor = (y_border - origPosition.y) / light_direction.y;
|
||||
y_factor = (y_border - starting_pos.y) / light_direction.y;
|
||||
}
|
||||
if (!z_null) {
|
||||
z_factor = (z_border - origPosition.z) / light_direction.z;
|
||||
z_factor = (z_border - starting_pos.z) / light_direction.z;
|
||||
}
|
||||
|
||||
if ((x_factor >= 1.0) && (y_factor >= 1.0) && (z_factor >= 1.0)) {
|
||||
// no hit, add light color result
|
||||
color_sum += (orig_color_sample.xyz * light_color) / ((0.1 * light_direction.length() * light_direction.length()) + 1.0);
|
||||
color_sum += (orig_color_sample.xyz * light_color) / ((0.01 * length(light_direction) * length(light_direction)) + 1.0);
|
||||
needs_next_light = true;
|
||||
} else {
|
||||
// if there is a border hit before reaching the light
|
||||
|
@ -205,7 +200,7 @@ void main() {
|
|||
} else {
|
||||
hit_facing = 2;
|
||||
}
|
||||
vec3 intersection_pos = origPosition + x_factor * light_direction;
|
||||
vec3 intersection_pos = starting_pos + x_factor * light_direction;
|
||||
u = uint(round(intersection_pos.y)) - volume_pos_y;
|
||||
v = uint(round(intersection_pos.z)) - volume_pos_z;
|
||||
}
|
||||
|
@ -216,7 +211,7 @@ void main() {
|
|||
} else {
|
||||
hit_facing = 4;
|
||||
}
|
||||
vec3 intersection_pos = origPosition + y_factor * light_direction;
|
||||
vec3 intersection_pos = starting_pos + y_factor * light_direction;
|
||||
u = uint(round(intersection_pos.x)) - volume_pos_x;
|
||||
v = uint(round(intersection_pos.z)) - volume_pos_z;
|
||||
}
|
||||
|
@ -227,7 +222,7 @@ void main() {
|
|||
} else {
|
||||
hit_facing = 1;
|
||||
}
|
||||
vec3 intersection_pos = origPosition + z_factor * light_direction;
|
||||
vec3 intersection_pos = starting_pos + z_factor * light_direction;
|
||||
u = uint(round(intersection_pos.x)) - volume_pos_x;
|
||||
v = uint(round(intersection_pos.y)) - volume_pos_y;
|
||||
}
|
||||
|
@ -256,12 +251,12 @@ void main() {
|
|||
break;
|
||||
}
|
||||
// set up the new light
|
||||
light_index = scene_info.infos[fragVolumeStart + 6 + light_num];
|
||||
light_index = scene_info.infos[volume_start + 6 + light_num];
|
||||
if (light_index == 0) {
|
||||
// abort if there is no new light
|
||||
break;
|
||||
}
|
||||
light_direction = get_light_position(light_index) - origPosition;
|
||||
light_direction = get_light_position(light_index) - starting_pos;
|
||||
light_color = get_light_color(light_index);
|
||||
|
||||
x_pos = light_direction.x > 0.0;
|
||||
|
@ -273,11 +268,43 @@ void main() {
|
|||
z_pos = light_direction.z > 0.0;
|
||||
z_null = (light_direction.z == 0.0);
|
||||
// reset volume info
|
||||
volume_index = fragVolumeStart;
|
||||
volume_index = volume_start;
|
||||
volume_pos_x = scene_info.infos[volume_index + 0];
|
||||
volume_pos_y = scene_info.infos[volume_index + 1];
|
||||
volume_pos_z = scene_info.infos[volume_index + 2];
|
||||
}
|
||||
}
|
||||
return color_sum;
|
||||
}
|
||||
|
||||
void main() {
|
||||
uint max_length = scene_info.infos[0];
|
||||
uint last = scene_info.infos[max_length];
|
||||
|
||||
uvec4 color_roughness = sample_color_from_scene_info(fragVolumeStart, fragRasterPos, facing);
|
||||
vec4 orig_color_sample = vec4(float(color_roughness.x) / 255.0, float(color_roughness.y) / 255.0, float(color_roughness.z) / 255.0, 1);
|
||||
|
||||
// singular raytracing
|
||||
//vec3 color_sum = get_lighting_color(fragVolumeStart, origPosition, orig_color_sample);
|
||||
|
||||
// diffuse raytracing using a quadratic raster of rays
|
||||
int raster_half_steps = 0;
|
||||
float raster_distance = 0.01;
|
||||
int raster_points = (2 * raster_half_steps + 1) * (2 * raster_half_steps + 1);
|
||||
|
||||
vec3 color_sum = vec3(0.0, 0.0, 0.0);
|
||||
for (int u_offset = -raster_half_steps; u_offset <= raster_half_steps; u_offset++) {
|
||||
for (int v_offset = -raster_half_steps; v_offset <= raster_half_steps; v_offset++) {
|
||||
float x_offset = raster_distance * float(u_offset) * float(facing == 0 || facing == 1 || facing == 4 || facing == 5);
|
||||
float y_offset = raster_distance * float(u_offset) * float(facing == 2 || facing == 3);
|
||||
y_offset += raster_distance * float(v_offset) * float(facing == 0 || facing == 1);
|
||||
float z_offset = raster_distance * float(v_offset) * float(facing == 4 || facing == 5 || facing == 2 || facing == 3);
|
||||
|
||||
vec3 offset = vec3(x_offset, y_offset, z_offset);
|
||||
|
||||
color_sum += get_lighting_color(fragVolumeStart, origPosition + offset, orig_color_sample) / float(raster_points);
|
||||
}
|
||||
}
|
||||
|
||||
outColor = vec4(color_sum, 1.0);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue