2015-07-02 77 views
0

我想在光線穿過它時得到三角形上的交點。我按照在線教程做了一個功能,但我無法獲得正確的交點座標。例如,如果我使用射線原點(0,2,-1),射線方向(0,1,0),三角形頂點p0(0,3,-1),我應該得到交點(0,3,-1) 0,3,0),p1(-0.5,3,-1),p2(0.5,3,-1)。但是,我得到了交點(0,7,-1),這是不正確的。如何獲得交點? Ray Triangle Intersection C++

我感謝您的時間和幫助。 :-)

float kEpsilon = 0.000001; 

V3f crossProduct(V3f point1, V3f point2){ 

    V3f vector; 

    vector.x = point1.y * point2.z - point2.y * point1.z; 
    vector.y = point2.x * point1.z - point1.x * point2.z; 
    vector.z = point1.x * point2.y - point1.y * point2.x; 

    return vector; 
} 

float dotProduct(V3f dot1, V3f dot2){ 

    float dot = dot1.x * dot2.x + dot1.y * dot2.y + dot1.z * dot2.z; 

    return dot; 
} 

//orig: ray origin, dir: ray direction, Triangle vertices: p0, p1, p2. 
bool rayTriangleIntersect(V3f orig, V3f dir, V3f p0, V3f p1, V3f p2){ 

// compute plane's normal 

    V3f p0p1, p0p2; 

    p0p1.x = p1.x - p0.x; 
    p0p1.y = p1.y - p0.y; 
    p0p1.z = p1.z - p0.z; 

    p0p2.x = p2.x - p0.x; 
    p0p2.y = p2.y - p0.y; 
    p0p2.z = p2.z - p0.z; 

    // no need to normalize 
    V3f N = crossProduct(p0p1, p0p2); // N 

    // Step 1: finding P 

    // check if ray and plane are parallel ? 
    float NdotRayDirection = dotProduct(N, dir); // if the result is 0, the function will return the value false (no intersection). 

    if (fabs(NdotRayDirection) < kEpsilon){ // almost 0 

     return false; // they are parallel so they don't intersect ! 
    } 

    // compute d parameter using equation 2 
    float d = dotProduct(N, p0); 

    // compute t (equation P=O+tR P intersection point ray origin O and its direction R) 

    float t = (dotProduct(N, orig) + d)/NdotRayDirection; 

    // check if the triangle is in behind the ray 
    //if (t < 0){ return false; } // the triangle is behind 

    // compute the intersection point using equation 
    V3f P; 

    //this part should do the work, but it does not work. 
    P.x = orig.x + t * dir.x; 
    P.y = orig.y + t * dir.y; 
    P.z = orig.z + t * dir.z; 


    // Step 2: inside-outside test 
    V3f C; // vector perpendicular to triangle's plane 

    // edge 0 
    V3f edge0; 

    edge0.x = p1.x - p0.x; 
    edge0.y = p1.y - p0.y; 
    edge0.z = p1.z - p0.z; 

    V3f vp0; 

    vp0.x = P.x - p0.x; 
    vp0.y = P.y - p0.y; 
    vp0.z = P.z - p0.z; 

    C = crossProduct(edge0, vp0); 

    if (dotProduct(N, C) < 0) { return false; }// P is on the right side 

    // edge 1 
    V3f edge1; 

    edge1.x = p2.x - p1.x; 
    edge1.y = p2.y - p1.y; 
    edge1.z = p2.z - p1.z; 

    V3f vp1; 

    vp1.x = P.x - p1.x; 
    vp1.y = P.y - p1.y; 
    vp1.z = P.z - p1.z; 

    C = crossProduct(edge1, vp1); 

    if (dotProduct(N, C) < 0) { return false; } // P is on the right side 

    // edge 2 
    V3f edge2; 

    edge2.x = p0.x - p2.x;  
    edge2.y = p0.y - p2.y; 
    edge2.z = p0.z - p2.z; 

    V3f vp2; 

    vp2.x = P.x - p2.x; 
    vp2.y = P.y - p2.y; 
    vp2.z = P.z - p2.z; 

    C = crossProduct(edge2, vp2); 

    if (dotProduct(N, C) < 0) { return false; } // P is on the right side; 

    return true; // this ray hits the triangle 
} 

感謝您的幫助。

+2

如果您不想重新發明輪子,請查看[幾何工具](http://www.geometrictools.com/)。 – Angew

+1

你使用哪個在線教程? – user463035818

+0

嗨Angew和tobi303, 感謝您的答覆。 Angew,我對計算機圖像非常陌生,並且無法在該網站上找到相關的代碼。 tobi303,這是鏈接(http://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/ray-triangle-intersection-geometric-solution)。 感謝您的幫助。 – Ming

回答

1

你得到了t = 5並得到了(0,7,-1),但t的正確數字是1,原因是你有float t = (dotProduct(N, orig) + d)/NdotRayDirection;,而正確的代碼是float t = -((dotProduct(N, orig) - d)/NdotRayDirection);這將有望解決你的問題。

+0

大家好,謝謝你的回覆。我嘗試了你的建議。我使用了射線原點(0,2,-1),射線方向(0,1,0),三角形頂點p0(0,3,0),p1(-0.5,3,-1),p2(0.5,3 ,-1),但是我得到了't = -1',交點是'(0,1,-1)',這是不正確的。你有什麼想法?謝謝。 :-) – Ming

+0

大家好,謝謝你的回覆。它現在可行,但我不清楚爲什麼我們使用這個等式。舊的有什麼問題?在線教程顯示'float t =(dot(N,orig)+ D)/ dot(N,dir);'你能否給我更多關於你答案的細節?我很抱歉,我對計算機圖形學非常陌生。謝謝。 :-) – Ming

+1

嗨,我不完全肯定教程,因爲我還沒有讀過它......我的猜測是,他們考慮一個不同的負係數來增加點....通常,我給了什麼你是包括我自己在內的一個人......如果你的問題得到解決,請接受答案.... –