Issues with picking

Nibbie
Posts: 1
Joined: 2009.11
Post: #1
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!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  How do I do Color Picking? dockode 2 5,018 Sep 24, 2011 08:02 PM
Last Post: dockode
  Opengl picking problem (zip file) papillon68 1 4,414 Mar 1, 2009 08:49 PM
Last Post: chronus
  Picking and selection... chalamov33 2 3,368 Mar 31, 2008 02:08 PM
Last Post: wyrmmage
  glut picking clashes with drawing wyrmmage 2 3,507 Jan 24, 2008 06:39 PM
Last Post: wyrmmage
  picking glOrtho vs gluPerspective rhiannon 0 3,634 Jun 6, 2005 02:20 PM
Last Post: rhiannon