diff --git a/shaders/compute.glsl b/shaders/compute.glsl index a17020b..febe279 100644 --- a/shaders/compute.glsl +++ b/shaders/compute.glsl @@ -202,8 +202,8 @@ vec3 pathtrace(Ray ray, inout uint rng_state) GPUMaterial mat = materials[hit.mat_index]; calculateLightColor(mat, hit, color, light, rng_state); - // if (mat.emission > 0.0 && mat.emission_texture_index == -1) - // break; + if (mat.emission > 0.0 && mat.emission_texture_index == -1) + break; ray = newRay(hit, ray, rng_state); ray.inv_direction = 1.0 / ray.direction; diff --git a/shaders/intersect.glsl b/shaders/intersect.glsl index b62ce3d..543ca42 100644 --- a/shaders/intersect.glsl +++ b/shaders/intersect.glsl @@ -1,27 +1,28 @@ bool intersectSphere(Ray ray, GPUObject obj, out hitInfo hit) { vec3 oc = ray.origin - obj.position; - float b = dot(oc, ray.direction); float c = dot(oc, oc) - obj.radius * obj.radius; - float h = b * b - c; - - float t = -b - sqrt(h); - float last_t = -b + sqrt(h); + float discriminant = b * b - c; - if (t > last_t) - { - float temp = t; - t = last_t; - last_t = temp; - } + float sqrtD = sqrt(max(0.0, discriminant)); + float t0 = -b - sqrtD; + float t1 = -b + sqrtD; - hit.t = t; - hit.last_t = last_t; - hit.position = ray.origin + ray.direction * t; + float temp = min(t0, t1); + t1 = max(t0, t1); + t0 = temp; + + bool isInside = c < 0.0; + t0 = isInside ? t1 : t0; + + hit.t = t0; + hit.last_t = t1; + hit.position = ray.origin + ray.direction * t0; hit.normal = normalize(hit.position - obj.position); - - return (h >= 0.0 && t > 0.0); + hit.normal *= (isInside ? -1.0 : 1.0); + + return (discriminant >= 0.0) && (t0 > 0.0); } bool intersectPlane(Ray ray, GPUObject obj, out hitInfo hit) diff --git a/shaders/light.glsl b/shaders/light.glsl index 6b5d1ed..1bf4147 100644 --- a/shaders/light.glsl +++ b/shaders/light.glsl @@ -90,11 +90,9 @@ vec3 sampleLights(vec3 position, inout uint rng_state) vec2 getSphereUV(vec3 surfacePoint) { - // Convert 3D point to spherical coordinates float phi = atan(surfacePoint.z, surfacePoint.x); float theta = acos(surfacePoint.y); - // Map to [0, 1] UV space float u = (phi + M_PI) / (2.0 * M_PI); float v = theta / M_PI; @@ -113,11 +111,9 @@ vec2 getTextureColor(hitInfo hit) else if (hit.obj_type == 3) { GPUTriangle tri = triangles[hit.obj_index]; - uv = hit.u * tri.texture_vertex2 + hit.v * tri.texture_vertex3 + (1 - (hit.u + hit.v)) * tri.texture_vertex1; - uv = vec2(uv.x, 1 - uv.y); } - return (uv); + return (vec2(uv.x, 1 - uv.y)); } void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inout vec3 light, inout uint rng_state) @@ -136,8 +132,8 @@ void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inou } else { - light += mat.emission * mat.color; color *= mat.color; + light += mat.emission * mat.color; } // light += sampleLights(hit.position, rng_state); } \ No newline at end of file diff --git a/skymap.hdr b/skymap.hdr new file mode 100644 index 0000000..32c2f91 Binary files /dev/null and b/skymap.hdr differ diff --git a/srcs/class/ObjParser.cpp b/srcs/class/ObjParser.cpp index 5debd8c..04b3bf7 100644 --- a/srcs/class/ObjParser.cpp +++ b/srcs/class/ObjParser.cpp @@ -333,11 +333,13 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene) throw std::runtime_error("OBJ: syntax error while getting material texture"); mat->emission_texture_index = scene.getEmissionTextures().size(); - std::cout << "path " << mat->emission_texture_index << " : " << getFilePath(_filename) + path << std::endl; scene.addEmissionTexture(getFilePath(_filename) + path); + + if (mat->emission == 0) + mat->emission = 1; } - else - std::cerr << "unsupported material setting : " << identifier << std::endl; + // else + // std::cerr << "unsupported material setting : " << identifier << std::endl; } if(mat) {