Skip to content
Snippets Groups Projects
Commit 41e26d71 authored by Susanne Dötsch's avatar Susanne Dötsch
Browse files

[94] Fixed the reflections and push constants

If the ray should still be reflected is hardcoded right now
parent 31fb4cf7
No related branches found
No related tags found
1 merge request!77Resolve "SAF-R Module"
Pipeline #27105 passed
...@@ -37,9 +37,9 @@ layout(std430, binding = 2) coherent buffer spheres{ ...@@ -37,9 +37,9 @@ layout(std430, binding = 2) coherent buffer spheres{
layout(set=0, binding=3, rgba8) uniform image2D outImage; layout(set=0, binding=3, rgba8) uniform image2D outImage;
layout( push_constant ) uniform constants{ layout( push_constant ) uniform constants{
float lightCount; int lightCount;
float matCount; int matCount;
float sphereCount; int sphereCount;
}; };
...@@ -48,22 +48,22 @@ vec3 safr_reflect(const vec3 dir, const vec3 hit_center) { ...@@ -48,22 +48,22 @@ vec3 safr_reflect(const vec3 dir, const vec3 hit_center) {
} }
bool ray_intersect(const vec3 origin, const vec3 dir, out float t0, const int id){ bool ray_intersect(const vec3 origin, const vec3 dir, out float t0, const int id){
vec3 L = inSpheres[id].center - origin; vec3 L = inSpheres[id].center - origin;
float tca = dot(L, dir); float tca = dot(L, dir);
float d2 = dot(L, L) - tca * tca; float d2 = dot(L, L) - tca * tca;
if (d2 > inSpheres[id].radius * inSpheres[id].radius){ if (d2 > inSpheres[id].radius * inSpheres[id].radius){
return false; return false;
} }
float thc = float(sqrt(inSpheres[id].radius * inSpheres[id].radius - d2)); float thc = float(sqrt(inSpheres[id].radius * inSpheres[id].radius - d2));
t0 = tca - thc; t0 = tca - thc;
float t1 = tca + thc; float t1 = tca + thc;
if (t0 < 0) { if (t0 < 0) {
t0 = t1; t0 = t1;
} }
if (t0 < 0){ if (t0 < 0){
return false; return false;
} }
return true; return true;
} }
bool sceneIntersect(const vec3 orig, const vec3 dir, out vec3 hit, out vec3 hit_center, out Material material) { bool sceneIntersect(const vec3 orig, const vec3 dir, out vec3 hit, out vec3 hit_center, out Material material) {
...@@ -92,6 +92,8 @@ vec3 castRay(const vec3 orig, const vec3 dir, int max_depth) { ...@@ -92,6 +92,8 @@ vec3 castRay(const vec3 orig, const vec3 dir, int max_depth) {
vec3 direction = dir; vec3 direction = dir;
vec3 reflect_dir = direction; vec3 reflect_dir = direction;
vec3 reflect_orig = orig; vec3 reflect_orig = orig;
vec3 reflect_color = result;
bool test = false;
for(int i = 0; i < max_depth; i++){ for(int i = 0; i < max_depth; i++){
depth++; depth++;
...@@ -103,34 +105,35 @@ vec3 castRay(const vec3 orig, const vec3 dir, int max_depth) { ...@@ -103,34 +105,35 @@ vec3 castRay(const vec3 orig, const vec3 dir, int max_depth) {
reflect_dir = normalize(safr_reflect(direction, hit_center)); reflect_dir = normalize(safr_reflect(direction, hit_center));
reflect_orig = (dot(reflect_dir, hit_center) < 0) ? point - hit_center * float(1e-3) : point + hit_center * float(1e-3);// offset the original point to avoid occlusion by the object itself reflect_orig = (dot(reflect_dir, hit_center) < 0) ? point - hit_center * float(1e-3) : point + hit_center * float(1e-3);// offset the original point to avoid occlusion by the object itself
direction = reflect_dir; direction = reflect_dir;
//compute shadows and other light properties for the returned ray color
float diffuse_light_intensity = 0, specular_light_intensity = 0;
for (int i = 0; i < lightCount; i++) {
vec3 light_dir = normalize(inLights[i].position - point);
float light_distance = distance(inLights[i].position, point);
vec3 shadow_orig = (dot(light_dir, hit_center) < 0) ? point - hit_center * float(1e-3) :
point + hit_center * float(1e-3);// checking if the point lies in the shadow of the lights[i]
vec3 shadow_pt, shadow_hit_center;
Material tmpmaterial;
if (sceneIntersect(shadow_orig, light_dir, shadow_pt, shadow_hit_center, tmpmaterial)
&& distance(shadow_pt, shadow_orig) < light_distance){
continue;
}
diffuse_light_intensity += inLights[i].intensity * max(0.f, dot(light_dir, hit_center));
specular_light_intensity += pow(max(0.f, dot(safr_reflect(light_dir, hit_center), dir)), material.specular_exponent) * inLights[i].intensity;
}
result = material.diffuse_color * diffuse_light_intensity * material.albedo[0] +
vec3(1., 1., 1.) * specular_light_intensity * material.albedo[1] + reflect_color * material.albedo[2];
// ToDo: Should not be hardcoded, but I don't have any ideas anymore
if(material.specular_exponent < 500){
return result;
}
} }
if (depth == 1){ if (depth == 1){
return result; return vec3(0.2, 0.7, 0.8);
}
vec3 reflect_color = result;
for(int i = 0; i < depth; i++){
//compute shadows and other light properties for the returned ray color
float diffuse_light_intensity = 0, specular_light_intensity = 0;
for (int i = 0; i < lightCount; i++) {
vec3 light_dir = normalize(inLights[i].position - point);
float light_distance = distance(inLights[i].position, point);
vec3 shadow_orig = (dot(light_dir, hit_center) < 0) ? point - hit_center * float(1e-3) :
point + hit_center * float(1e-3);// checking if the point lies in the shadow of the lights[i]
vec3 shadow_pt, shadow_hit_center;
Material tmpmaterial;
if (sceneIntersect(shadow_orig, light_dir, shadow_pt, shadow_hit_center, tmpmaterial)
&& distance(shadow_pt, shadow_orig) < light_distance){
continue;
}
diffuse_light_intensity += inLights[i].intensity * max(0.f, dot(light_dir, hit_center));
specular_light_intensity += pow(max(0.f, dot(safr_reflect(light_dir, hit_center), dir)), material.specular_exponent) * inLights[i].intensity;
}
return result = material.diffuse_color * diffuse_light_intensity * material.albedo[0] +
vec3(1., 1., 1.) * specular_light_intensity * material.albedo[1] + reflect_color * material.albedo[2];
} }
return result; return result;
} }
......
...@@ -139,8 +139,8 @@ int main(int argc, const char** argv) { ...@@ -139,8 +139,8 @@ int main(int argc, const char** argv) {
); );
materialBuffer.fill(materials); materialBuffer.fill(materials);
glm::vec3 pushData = glm::vec3((lights.size()), (materials.size()), (spheres.size())); glm::ivec3 pushData = glm::ivec3((lights.size()), (materials.size()), (spheres.size()));
vkcv::PushConstants pushConstantsCompute(sizeof(glm::vec3)); vkcv::PushConstants pushConstantsCompute(sizeof(glm::ivec3));
pushConstantsCompute.appendDrawcall(pushData); pushConstantsCompute.appendDrawcall(pushData);
vkcv::DescriptorWrites setWrites; vkcv::DescriptorWrites setWrites;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment