A function used to determine the intersection of rays and triangles

Source: Internet
Author: User
Code
1 //-----------------------------------------------
2 // ----------- detect the intersection of triangles and rays ---------
3 bool cdirectxapp: intersect (const d3dxvector3 & v0, const d3dxvector3 & V1, const d3dxvector3 & V2,
4 const d3dxvector3 & orgin, const d3dxvector3 & VEC, float & U, float & V, float & T)
5 {
6 // determine whether the triangle is used and the direction Vector
7 assert (VEC! = D3dxvector3 (0, 0, 0 )&&
8 V0! = V1 & V1! = V2 & V0! = V2 );
9 d3dxvector3 M = V0-V2;
10 d3dxvector3 n = V1-V2;
11 d3dxvector3 negativevec =-VEC;
12 d3dxvector3 tempvec;
13 d3dxvec3cross (& tempvec, & N, & negativevec );
14 // det is the denominator of the unknown in the Klein Law
15 float det = d3dxvec3dot (& tempvec, & M );
16
17 if (det <1e-6f)
18 {
19 Return false;
20}
21
22 d3dxvector3 vtoorigin = orgin-V2;
23 float UB = d3dxvec3dot (& tempvec, & vtoorigin );
24 U = UB/det;
25 if (U <0 | u> 1)
26 {
27 return false;
28}
29
30 d3dxvec3cross (& tempvec, & vtoorigin, & negativevec );
31 float VB = d3dxvec3dot (& M, & tempvec );
32 V = Vb/det;
33 If (v <0 | u + V> 1)
34 {
35 return false;
36}
37
38 d3dxvec3cross (& tempvec, & N, & vtoorigin );
39 float TB = d3dxvec3dot (& M, & tempvec );
40 t = Tb/det;
41 return true;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.