0

I've implemented a simple ray tracer and now I'm trying to implement reflections but objects are behaving as transparent. Here is my code for getting the reflected ray.

        ray* reflected = new ray();
        reflected->direction = rayIn.direction - (2 * glm::dot(rayIn.direction, normal)) * normal;
        reflected->origin = int_point + epsilon * normal;
        outColor += ((int_object->reflectivity)*intersectray(*reflected, depth - 1));

Here are images With code:

img

Without code:

img

I'll edit the post if more code is needed.

Edit : It seems the problem is when I'm iterating through the objects in the scene. I insert the objects as

    scene->add(sphere1);
    scene->add(sphere2);

But when I change this to :

    scene->add(sphere2);
    scene->add(sphere1);

the output is correct. Sphere 1 is at closer to camera than sphere 2 and they are not overlapping.

Community
  • 1
  • 1
curs0r
  • 21
  • 1
  • 7
  • it might be anything so some hints: The spheres are intersecting? Are all of your normals pointing out ? are your ray directions correct? Have you moved the ray origin by some very small step after each contact with surface? You should add debug draw to see what is happening see: [Reflection and refraction impossible without recursive ray tracing?](https://stackoverflow.com/a/45140313/2521214) for some inspiration how it could look/work. – Spektre May 03 '20 at 07:55
  • @Spektre Thanks for the inputs, but none worked. Source code is a public repo so maybe you can look into it https://github.com/The-curs0r/RayTracer – curs0r May 03 '20 at 08:20
  • Analyzing bug in foreign un-commented code is a nightmare ... there are not many of us willing to do so ... anyway your best bet is to create the debug draw / overlay for specific ray so you can trace it where it goes wrong and why inside debugger. So add a vector based wireframe render (not ray tracer) that will match your ray tracing and select a single casted ray (fixed position and direction) and render it as lines (both paths and normals) then just rotate/move your view to see where it goes wrong ... that will reveal hidden problems right away. – Spektre May 03 '20 at 15:12
  • @Spektre okay, I'll try that – curs0r May 03 '20 at 15:16
  • @Spektre Hey, i think some problem is in loop iterations and not in ray reflection. I add sphere as scene.add(sphere1); scene.add(sphere2); But when i change the order , there is no such problem as seen in first image. – curs0r May 03 '20 at 17:42
  • @Spektre I solved the problem. The code for reflection etc. was correct, problem was while updating normals. Thanks for your help. – curs0r May 03 '20 at 19:10
  • When order is changing the outcome it also might be worth checking if your ray/objects intersection is picking up the closest hit instead of first in the list of objects ... – Spektre May 04 '20 at 05:26

1 Answers1

0

Problem was this part of code

for (objects in scene){
    double intersection = (*objIterator)->intersect(rayIn,normal);

    if (intersection < minDistance && intersection > epsilon  )
    {
        minDistance = intersection;
        int_object = *objIterator;
        int_point = rayIn.origin + intersection * rayIn.direction + (epsilon * normal);
    }}

Here normal is used later for other calculations but the first line update normal for current object intersection (Even if its not close). So I added a vector to store normal of the intersection object and used it later.

curs0r
  • 21
  • 1
  • 7