iDevGames Forums
Issues with picking - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Issues with picking (/thread-575.html)



Issues with picking - FFaqui - Nov 21, 2009 04:37 PM

I'm designing a program so that you can color the model by using picking. However, whenever I draw on the model, it appears on the back of the model at strange places. I don't know if I'm doing the ray-triangle intersection correctly. Here is my code:

Code:
bool isViewIntersect(vertex sPos0, vertex sPos1, vertex v0, vertex v1, vertex v2,vertex &center)
{
    vertex e1,e2,direction,h,s,q;
    float a,u,v,intersect;

    direction.x=sPos1.x-sPos0.x;
    direction.y=sPos1.y-sPos0.y;
    direction.z=sPos1.z-sPos0.z;

    normalize(direction);
    e1.x=v1.x-v0.x;
    e1.y=v1.y-v0.y;
    e1.z=v1.z-v0.z;
    e2.x=v2.x-v0.x;
    e2.y=v2.y-v0.y;
    e2.z=v2.z-v0.z;
    normalize(e1);
    normalize(e2);
    h.x=direction.y*e2.z-direction.z*e2.y;
    h.y=direction.x*e2.z-direction.z*e2.x;
    h.z=direction.y*e2.z-direction.z*e2.y;
    normalize(h);
    a=e1.x*h.x+e1.y*h.y+e1.z*h.z;
    
    if(a>-0.00001 && a<0.00001)
        return false;
    s.x=sPos0.x-v0.x;
    s.y=sPos0.y-v0.y;
    s.z=sPos0.z-v0.z;
    normalize(s);
    u=(s.x*h.x+s.y*h.y+s.z*h.z)/a;
    if(u<0.0 || u>1.0)
        return false;
    q.x=s.y*e1.z-s.z*e1.y;
    q.y=s.x*e1.z-s.z*e1.x;
    q.z=s.x*e1.y-s.y*e1.x;
    normalize(q);
    v=(direction.x*q.x+direction.y*q.y+direction.z*q.z)/a;
    if(v <0.0 || u+v>1.0)
        return false;
    intersect=(e2.x*q.x+e2.y*q.y+e2.z*q.z)/a;
    if(intersect>0.00001)
    {
        center.x=sPos0.x+intersect*direction.x;
        center.y=sPos0.y+intersect*direction.y;
        center.z=sPos0.z+intersect*direction.z;
        cout << center.x << " " << center.y << " " << center.z << endl;
        return true;
    }
    
    return false;
}

Please help! Thank you!