문제

This is my third question in this raytracing thing, but there's been progress :P So, I'm implementing a C++ ray tracer for my object oriented programming class, and so far I've implemented monochromatic spheres and planes with support to reflection and specular shading. This is an example of a thing I've done:

Whee pretty and shiny

Now I'm trying to implement general polyhedrons. I'm using a modified version of this algorithm to compute intersections with an arbitrary polyhedron of nFaces() faces, each of which being contained by a plane defined by Vec Polyhedron::point(int face) and Vec Polyhedron::normal(int face):

Vec Polyhedron::intersect(Vec o, Vec d)
{
    int face = nFaces();
    Vec ni(0,0,0), pi(0,0,0);
    unit te = -1;
    unit tl = -1;
    unit t = 0;
    unit N, D;
    Vec v = d.normal();
    int facein, faceout;
    for(int i = 0; i < face; i++)
    {
        ni = normal(i);
        pi = point(i);
        N = ((pi - o)*ni);
        D = v*ni;
        if(D == 0 && N < 0)
            return o;
        if(D != 0)
        {
            t = N/D;
            if(t > 0)
            {
                if(N < 0)
                {
                    if(t > te){
                        te = t;
                        facein = i;
                    }
                }else{
                    if((tl == -1) || (t < tl)){
                        tl = t;
                        faceout = i;
                    }
                }
                if((tl != -1) && (tl < te))
                    return o;
            }
        }
    }
    if(tl != -1)
    {
        if(te != -1)
        {
            v = v*te + o;
            return (v + normal(facein)*0.000000000001);
        }
        v = v*tl + o;
        return (v + normal(faceout)*0.000000000001);
    }
    return o;
}

So I modified the scene with the balls by removing the balls and adding a red cube (the only type of Polyhedron I implemented was a Cuboid) and ran it. This was the result:

enter image description here

And I have absolutely no idea why. Any clues?

도움이 되었습니까?

해결책

I was being stupid and misunderstood the algorithm. I thought the (tl < te) part would take care of everything that was on the Polyhedron's face, but it doesn't. Silly me.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top