SlideShare a Scribd company logo
1 of 20
픽킹 나철환
Mouse Picking 처리 단계 스크린 좌표계 마우스 포인터를 얻어 투영좌표계로 변환한다. 변환된 투영좌표를 뷰좌표로 변환하여 카메라 원점을 시작점으로 하는 광선을 계산한다. 광선을 피킹 데이터와 동일한 좌표계로 변환한다. 픽킹광선과 교차 판정을 한다.
목차 삼각형과 반직선 교차 원과 반직선 교차
삼각형 교차 반직선과 삼각형을 포함하고 있는 평면과의 충돌을 한다  그 다음에 P 점이 삼각형 안에 있지는 판별을 한다.
평면  방정식 이용
무게 중심 좌표 이용
삼각형 교차점 x(u, v)= (1 – u – v)a + ub + vc ab = b – a, ac = c – a x(u, v)= a + u ab + v ac x(u, v)= o + t d o + t d = a + u ab + v ac o – a = oa = -td + uab + vac d c o a x t t b 무게 중심 좌표 v u c 0  u  1 0  v  1 u+v  1 (0,0,0) x a b x = wa + ub + vc w + u + v = 1 w = 1 - u - v
최적화 다이렉트에서 지원하는 IntersectTriangle()함수를 따로 보면 위에 것을 요약해 놓아서 조금 다른 수학적 방법이 들어가 있어서 그것을 설명 하도록 하겠습니다.
Triangle Intersection // Calculate U parameter and test bounds     *u = D3DXVec3Dot( &tvec, &pvec );     if( *u < 0.0f || *u > det )         return false;     // Prepare to test V parameter     D3DXVECTOR3 qvec;     D3DXVec3Cross( &qvec, &tvec, &edge1 );     // Calculate V parameter and test bounds     *v = D3DXVec3Dot( &dir, &qvec );     if( *v < 0.0f || *u + *v > det )         return false;     // Calculate t, scale parameters, ray intersects triangle     *t = D3DXVec3Dot( &edge2, &qvec );     FLOAT fInvDet = 1.0f / det;     *t *= fInvDet;     *u *= fInvDet;     *v *= fInvDet;      return true; IntersectTriangle(const D3DXVECTOR3& orig,                              const D3DXVECTOR3& dir, D3DXVECTOR3& v0,                              D3DXVECTOR3& v1,                              D3DXVECTOR3& v2, 		         FLOAT* t, FLOAT* u, FLOAT* v ) // Find vectors for two edges sharing vert0     D3DXVECTOR3 edge1 = v1 - v0;     D3DXVECTOR3 edge2 = v2 - v0;     // Begin calculating determinant     //- also used to calculate U parameter     D3DXVECTOR3 pvec;     D3DXVec3Cross( &pvec, &dir, &edge2 );     // If determinant is near zero, ray lies in plane of triangle     FLOAT det = D3DXVec3Dot( &edge1, &pvec );     D3DXVECTOR3 tvec;     if( det > 0 ) {        tvec = orig - v0;}     else {         tvec = v0 – orig;det = -det; }     if( det < 0.0001f )        return false;
삼각형 점 포함 테스트 반직선과 삼각형의 교차판정을 하는 방법은 2차원적으로 해석 될 수 있다. OA,OB는 고정된 벡터이고 OP는 임의의 벡터입니다. OP = u * OA + v * OB ( u,v는 임의의 실수 ) u,v에 어떤 조건을 붙여서 P가 OAB영역을 표현하기위한 조건은, 삼각형의 변(①②③)에대해 각각 하나씩 있습니다.① : 0 ≤ u② : 0 ≤ v③ : u + v ≤ 1
Triangle Intersection // Calculate U parameter and test bounds     *u = D3DXVec3Dot( &tvec, &pvec );     if( *u < 0.0f || *u > det )         return false;     // Prepare to test V parameter     D3DXVECTOR3 qvec;     D3DXVec3Cross( &qvec, &tvec, &edge1 );     // Calculate V parameter and test bounds     *v = D3DXVec3Dot( &dir, &qvec );     if( *v < 0.0f || *u + *v > det )         return false;     // Calculate t, scale parameters, ray intersects triangle     *t = D3DXVec3Dot( &edge2, &qvec );     FLOAT fInvDet = 1.0f / det;     *t *= fInvDet;     *u *= fInvDet;     *v *= fInvDet;      return true; IntersectTriangle(const D3DXVECTOR3& orig,                              const D3DXVECTOR3& dir, D3DXVECTOR3& v0,                              D3DXVECTOR3& v1,                              D3DXVECTOR3& v2, 		         FLOAT* t, FLOAT* u, FLOAT* v ) // Find vectors for two edges sharing vert0     D3DXVECTOR3 edge1 = v1 - v0;     D3DXVECTOR3 edge2 = v2 - v0;     // Begin calculating determinant     //- also used to calculate U parameter     D3DXVECTOR3 pvec;     D3DXVec3Cross( &pvec, &dir, &edge2 );     // If determinant is near zero, ray lies in plane of triangle     FLOAT det = D3DXVec3Dot( &edge1, &pvec );     D3DXVECTOR3 tvec;     if( det > 0 ) {        tvec = orig - v0;}     else {         tvec = v0 – orig;det = -det; }     if( det < 0.0001f )        return false;
u,v을 어떻게 구하나? v = |Ob|/|OB|= Ob•nA/OB•nA Ob•nA = OP•nA∴v = Ob•nA/OB•nA = OP•nA/OB•nA u = OP•nB/OA•nB
그림에 표시된 법선(N)은 V0,V2를 포함하고 바라보는 방향과 평행한 평면의 법선벡터입니다. 			N = dir ⅹ ( V2 - V0 )이때, 점 P와 orig는 이 벡터(N)에대해'같은 정사영'을 갖는 점들임을 알수 있습니다. 즉 다음이 성립한다는 말이죠.(orig-V0)•N == (P-V0)•N
u = OP•nB/OA•nB u = tvec • ( dir ⅹ e2 ) /        e1 • ( dir ⅹ e2 ) v =Ob•nA/OB•nA v = tvec • ( e1 ⅹ dir ) /        e2 • ( e1 ⅹ dir )  e1 = V1 - V0e2 = V2 - V0tvec = orig - V0
(방법1)Triangle Intersection-4- // Calculate U parameter and test bounds     *u = D3DXVec3Dot( &tvec, &pvec );     if( *u < 0.0f || *u > det )         return false;     // Prepare to test V parameter     D3DXVECTOR3 qvec;     D3DXVec3Cross( &qvec, &tvec, &edge1 );     // Calculate V parameter and test bounds     *v = D3DXVec3Dot( &dir, &qvec );     if( *v < 0.0f || *u + *v > det )         return false;     // Calculate t, scale parameters, ray intersects triangle     *t = D3DXVec3Dot( &edge2, &qvec );     FLOAT fInvDet = 1.0f / det;     *t *= fInvDet;     *u *= fInvDet;     *v *= fInvDet;      return true; IntersectTriangle(const D3DXVECTOR3& orig,                              const D3DXVECTOR3& dir, D3DXVECTOR3& v0,                              D3DXVECTOR3& v1,                              D3DXVECTOR3& v2, 		         FLOAT* t, FLOAT* u, FLOAT* v ) // Find vectors for two edges sharing vert0     D3DXVECTOR3 edge1 = v1 - v0;     D3DXVECTOR3 edge2 = v2 - v0;     // Begin calculating determinant     //- also used to calculate U parameter     D3DXVECTOR3 pvec;     D3DXVec3Cross( &pvec, &dir, &edge2 );     // If determinant is near zero, ray lies in plane of triangle     FLOAT det = D3DXVec3Dot( &edge1, &pvec );     D3DXVECTOR3 tvec;     if( det > 0 ) {        tvec = orig - v0;}     else {         tvec = v0 – orig;det = -det; }     if( det < 0.0001f )        return false;
구와 선 교차 //sphere 체크할 구 //ray 체크할 광선 //intersection 교차 시 교차 지점. //distance 광선의 시작점으로부터 교차 점까지의 거리 bool intersect_ray_and_sphere( const Sphere& sphere, const Ray& ray, vector3& intersection, double& distance) {       vector3 l = sphere.center - ray.origin;                double s = dot( l, ray.direction );     double l2 = dot( l, l );     double r2 = pow( sphere.radius, 2 );       if( s < 0 && l2 > r2 )  {                       return false;                       // 광선이 구의 반대 방향을 향하거나 구를 지나친 경우    }      double m2 = l2 - pow( s, 2 );        if( m2 > r2 ) {       return false;                      // 광선이 구를 비껴가는 경우    }      double q = sqrt( r2 - m2 );      // 두 개의 교차점 중 어느것을 구하는가?       if( l2 > r2 ) {                        distance = s - q;    }else {         distance = s + q;    }      intersection = o + distance * ray.direction;      return true;      }
구와 선 교차 //sphere 체크할 구 //ray 체크할 광선 //intersection 교차 시 교차 지점. //distance 광선의 시작점으로부터 교차 점까지의 거리 bool intersect_ray_and_sphere( const Sphere& sphere, const Ray& ray, vector3& intersection, double& distance) {       vector3 l = sphere.center - ray.origin;                double s = dot( l, ray.direction );     double l2 = dot( l, l );     double r2 = pow( sphere.radius, 2 );       if( s < 0 && l2 > r2 )  {                       return false;                       // 광선이 구의 반대 방향을 향하거나 구를 지나친 경우    }      double m2 = l2 - pow( s, 2 );        if( m2 > r2 ) {       return false;                      // 광선이 구를 비껴가는 경우    }      double q = sqrt( r2 - m2 );      // 두 개의 교차점 중 어느것을 구하는가?       if( l2 > r2 ) {                        distance = s - q;    }else {         distance = s + q;    }      intersection = o + distance * ray.direction;      return true;      }
구와 선 교차 //sphere 체크할 구 //ray 체크할 광선 //intersection 교차 시 교차 지점. //distance 광선의 시작점으로부터 교차 점까지의 거리 bool intersect_ray_and_sphere( const Sphere& sphere, const Ray& ray, vector3& intersection, double& distance) {       vector3 l = sphere.center - ray.origin;                double s = dot( l, ray.direction );     double l2 = dot( l, l );     double r2 = pow( sphere.radius, 2 );       if( s < 0 && l2 > r2 )  {                       return false;                       // 광선이 구의 반대 방향을 향하거나 구를 지나친 경우    }      double m2 = l2 - pow( s, 2 );        if( m2 > r2 ) {       return false;                      // 광선이 구를 비껴가는 경우    }      double q = sqrt( r2 - m2 );      // 두 개의 교차점 중 어느것을 구하는가?       if( l2 > r2 ) {                        distance = s - q;    }else {         distance = s + q;    }      intersection = o + distance * ray.direction;      return true;      }
참고자료 C:sersjfghksesktopickntersectTriangle 분석.htm 리얼-타임 렌더링(제 2판) 3D 게임 프로그래밍 & 컴퓨터 그래픽을 위한 수학 (제 2판) http://hermet.pe.kr/68084286

More Related Content

What's hot

기본 회전 공식
기본 회전 공식 기본 회전 공식
기본 회전 공식 cancan21st
 
3D 컴퓨터 그래픽스 기초
3D 컴퓨터 그래픽스 기초3D 컴퓨터 그래픽스 기초
3D 컴퓨터 그래픽스 기초Seung Joon Choi
 
13장 연산자 오버로딩
13장 연산자 오버로딩13장 연산자 오버로딩
13장 연산자 오버로딩유석 남
 
2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdfkd19h
 
TestBCD2018-2(answer)
TestBCD2018-2(answer)TestBCD2018-2(answer)
TestBCD2018-2(answer)Yong Heui Cho
 
Unity Surface Shader for Artist 03
Unity Surface Shader for Artist 03Unity Surface Shader for Artist 03
Unity Surface Shader for Artist 03SangYun Yi
 
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexityskku_npc
 
Unity Surface Shader for Artist 04
Unity Surface Shader for Artist 04Unity Surface Shader for Artist 04
Unity Surface Shader for Artist 04SangYun Yi
 
Mathematics
MathematicsMathematics
Mathematicsskku_npc
 
Direct Sparse Odometry (DSO) Review
Direct Sparse Odometry (DSO) ReviewDirect Sparse Odometry (DSO) Review
Direct Sparse Odometry (DSO) ReviewEdwardIm1
 
12.05.26 roam algorithm
12.05.26 roam algorithm12.05.26 roam algorithm
12.05.26 roam algorithm준섭 김
 
Pose Graph based SLAM
Pose Graph based SLAMPose Graph based SLAM
Pose Graph based SLAMEdwardIm1
 
Unity Surface Shader for Artist 02
Unity Surface Shader for Artist 02Unity Surface Shader for Artist 02
Unity Surface Shader for Artist 02SangYun Yi
 
자료구조 프로젝트
자료구조 프로젝트자료구조 프로젝트
자료구조 프로젝트hyungoh kim
 

What's hot (16)

4. loop
4. loop4. loop
4. loop
 
기본 회전 공식
기본 회전 공식 기본 회전 공식
기본 회전 공식
 
3D 컴퓨터 그래픽스 기초
3D 컴퓨터 그래픽스 기초3D 컴퓨터 그래픽스 기초
3D 컴퓨터 그래픽스 기초
 
13장 연산자 오버로딩
13장 연산자 오버로딩13장 연산자 오버로딩
13장 연산자 오버로딩
 
이산치2번
이산치2번이산치2번
이산치2번
 
2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf
 
TestBCD2018-2(answer)
TestBCD2018-2(answer)TestBCD2018-2(answer)
TestBCD2018-2(answer)
 
Unity Surface Shader for Artist 03
Unity Surface Shader for Artist 03Unity Surface Shader for Artist 03
Unity Surface Shader for Artist 03
 
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexity
 
Unity Surface Shader for Artist 04
Unity Surface Shader for Artist 04Unity Surface Shader for Artist 04
Unity Surface Shader for Artist 04
 
Mathematics
MathematicsMathematics
Mathematics
 
Direct Sparse Odometry (DSO) Review
Direct Sparse Odometry (DSO) ReviewDirect Sparse Odometry (DSO) Review
Direct Sparse Odometry (DSO) Review
 
12.05.26 roam algorithm
12.05.26 roam algorithm12.05.26 roam algorithm
12.05.26 roam algorithm
 
Pose Graph based SLAM
Pose Graph based SLAMPose Graph based SLAM
Pose Graph based SLAM
 
Unity Surface Shader for Artist 02
Unity Surface Shader for Artist 02Unity Surface Shader for Artist 02
Unity Surface Shader for Artist 02
 
자료구조 프로젝트
자료구조 프로젝트자료구조 프로젝트
자료구조 프로젝트
 

Similar to 픽킹

[0528 석재호]게임을위한기초수학과물리
[0528 석재호]게임을위한기초수학과물리[0528 석재호]게임을위한기초수학과물리
[0528 석재호]게임을위한기초수학과물리Jaeho Seok
 
3D Graphics Transform
3D Graphics Transform3D Graphics Transform
3D Graphics Transform문익 장
 
Bs webgl소모임002
Bs webgl소모임002Bs webgl소모임002
Bs webgl소모임002Seonki Paik
 
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard LibraryDongMin Choi
 
2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdfjinwookhong
 
자료구조 그래프 보고서
자료구조 그래프 보고서자료구조 그래프 보고서
자료구조 그래프 보고서mil23
 
이산치수학 Project5
이산치수학 Project5이산치수학 Project5
이산치수학 Project5KoChungWook
 
프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기Jongwook Choi
 

Similar to 픽킹 (10)

[0528 석재호]게임을위한기초수학과물리
[0528 석재호]게임을위한기초수학과물리[0528 석재호]게임을위한기초수학과물리
[0528 석재호]게임을위한기초수학과물리
 
3D Graphics Transform
3D Graphics Transform3D Graphics Transform
3D Graphics Transform
 
Picking
PickingPicking
Picking
 
Bs webgl소모임002
Bs webgl소모임002Bs webgl소모임002
Bs webgl소모임002
 
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
 
2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf
 
점, 선, 면
점, 선, 면점, 선, 면
점, 선, 면
 
자료구조 그래프 보고서
자료구조 그래프 보고서자료구조 그래프 보고서
자료구조 그래프 보고서
 
이산치수학 Project5
이산치수학 Project5이산치수학 Project5
이산치수학 Project5
 
프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기
 

More from samagu0030

자연스러운 세부 수준 변화
자연스러운 세부 수준 변화자연스러운 세부 수준 변화
자연스러운 세부 수준 변화samagu0030
 
카툰 렌더링
카툰 렌더링카툰 렌더링
카툰 렌더링samagu0030
 
[Gpg1권 나철] 2.0 예측 가능한 난수
[Gpg1권 나철] 2.0 예측 가능한 난수[Gpg1권 나철] 2.0 예측 가능한 난수
[Gpg1권 나철] 2.0 예측 가능한 난수samagu0030
 
4장 테스트 자동화의 철학
4장 테스트 자동화의 철학4장 테스트 자동화의 철학
4장 테스트 자동화의 철학samagu0030
 

More from samagu0030 (6)

자연스러운 세부 수준 변화
자연스러운 세부 수준 변화자연스러운 세부 수준 변화
자연스러운 세부 수준 변화
 
카툰 렌더링
카툰 렌더링카툰 렌더링
카툰 렌더링
 
Fsm
FsmFsm
Fsm
 
클로저 1
클로저 1클로저 1
클로저 1
 
[Gpg1권 나철] 2.0 예측 가능한 난수
[Gpg1권 나철] 2.0 예측 가능한 난수[Gpg1권 나철] 2.0 예측 가능한 난수
[Gpg1권 나철] 2.0 예측 가능한 난수
 
4장 테스트 자동화의 철학
4장 테스트 자동화의 철학4장 테스트 자동화의 철학
4장 테스트 자동화의 철학
 

픽킹

  • 2. Mouse Picking 처리 단계 스크린 좌표계 마우스 포인터를 얻어 투영좌표계로 변환한다. 변환된 투영좌표를 뷰좌표로 변환하여 카메라 원점을 시작점으로 하는 광선을 계산한다. 광선을 피킹 데이터와 동일한 좌표계로 변환한다. 픽킹광선과 교차 판정을 한다.
  • 3. 목차 삼각형과 반직선 교차 원과 반직선 교차
  • 4. 삼각형 교차 반직선과 삼각형을 포함하고 있는 평면과의 충돌을 한다 그 다음에 P 점이 삼각형 안에 있지는 판별을 한다.
  • 7.
  • 8. 삼각형 교차점 x(u, v)= (1 – u – v)a + ub + vc ab = b – a, ac = c – a x(u, v)= a + u ab + v ac x(u, v)= o + t d o + t d = a + u ab + v ac o – a = oa = -td + uab + vac d c o a x t t b 무게 중심 좌표 v u c 0  u  1 0  v  1 u+v  1 (0,0,0) x a b x = wa + ub + vc w + u + v = 1 w = 1 - u - v
  • 9. 최적화 다이렉트에서 지원하는 IntersectTriangle()함수를 따로 보면 위에 것을 요약해 놓아서 조금 다른 수학적 방법이 들어가 있어서 그것을 설명 하도록 하겠습니다.
  • 10. Triangle Intersection // Calculate U parameter and test bounds *u = D3DXVec3Dot( &tvec, &pvec ); if( *u < 0.0f || *u > det ) return false; // Prepare to test V parameter D3DXVECTOR3 qvec; D3DXVec3Cross( &qvec, &tvec, &edge1 ); // Calculate V parameter and test bounds *v = D3DXVec3Dot( &dir, &qvec ); if( *v < 0.0f || *u + *v > det ) return false; // Calculate t, scale parameters, ray intersects triangle *t = D3DXVec3Dot( &edge2, &qvec ); FLOAT fInvDet = 1.0f / det; *t *= fInvDet; *u *= fInvDet; *v *= fInvDet; return true; IntersectTriangle(const D3DXVECTOR3& orig, const D3DXVECTOR3& dir, D3DXVECTOR3& v0, D3DXVECTOR3& v1, D3DXVECTOR3& v2, FLOAT* t, FLOAT* u, FLOAT* v ) // Find vectors for two edges sharing vert0 D3DXVECTOR3 edge1 = v1 - v0; D3DXVECTOR3 edge2 = v2 - v0; // Begin calculating determinant //- also used to calculate U parameter D3DXVECTOR3 pvec; D3DXVec3Cross( &pvec, &dir, &edge2 ); // If determinant is near zero, ray lies in plane of triangle FLOAT det = D3DXVec3Dot( &edge1, &pvec ); D3DXVECTOR3 tvec; if( det > 0 ) { tvec = orig - v0;} else { tvec = v0 – orig;det = -det; } if( det < 0.0001f ) return false;
  • 11. 삼각형 점 포함 테스트 반직선과 삼각형의 교차판정을 하는 방법은 2차원적으로 해석 될 수 있다. OA,OB는 고정된 벡터이고 OP는 임의의 벡터입니다. OP = u * OA + v * OB ( u,v는 임의의 실수 ) u,v에 어떤 조건을 붙여서 P가 OAB영역을 표현하기위한 조건은, 삼각형의 변(①②③)에대해 각각 하나씩 있습니다.① : 0 ≤ u② : 0 ≤ v③ : u + v ≤ 1
  • 12. Triangle Intersection // Calculate U parameter and test bounds *u = D3DXVec3Dot( &tvec, &pvec ); if( *u < 0.0f || *u > det ) return false; // Prepare to test V parameter D3DXVECTOR3 qvec; D3DXVec3Cross( &qvec, &tvec, &edge1 ); // Calculate V parameter and test bounds *v = D3DXVec3Dot( &dir, &qvec ); if( *v < 0.0f || *u + *v > det ) return false; // Calculate t, scale parameters, ray intersects triangle *t = D3DXVec3Dot( &edge2, &qvec ); FLOAT fInvDet = 1.0f / det; *t *= fInvDet; *u *= fInvDet; *v *= fInvDet; return true; IntersectTriangle(const D3DXVECTOR3& orig, const D3DXVECTOR3& dir, D3DXVECTOR3& v0, D3DXVECTOR3& v1, D3DXVECTOR3& v2, FLOAT* t, FLOAT* u, FLOAT* v ) // Find vectors for two edges sharing vert0 D3DXVECTOR3 edge1 = v1 - v0; D3DXVECTOR3 edge2 = v2 - v0; // Begin calculating determinant //- also used to calculate U parameter D3DXVECTOR3 pvec; D3DXVec3Cross( &pvec, &dir, &edge2 ); // If determinant is near zero, ray lies in plane of triangle FLOAT det = D3DXVec3Dot( &edge1, &pvec ); D3DXVECTOR3 tvec; if( det > 0 ) { tvec = orig - v0;} else { tvec = v0 – orig;det = -det; } if( det < 0.0001f ) return false;
  • 13. u,v을 어떻게 구하나? v = |Ob|/|OB|= Ob•nA/OB•nA Ob•nA = OP•nA∴v = Ob•nA/OB•nA = OP•nA/OB•nA u = OP•nB/OA•nB
  • 14. 그림에 표시된 법선(N)은 V0,V2를 포함하고 바라보는 방향과 평행한 평면의 법선벡터입니다. N = dir ⅹ ( V2 - V0 )이때, 점 P와 orig는 이 벡터(N)에대해'같은 정사영'을 갖는 점들임을 알수 있습니다. 즉 다음이 성립한다는 말이죠.(orig-V0)•N == (P-V0)•N
  • 15. u = OP•nB/OA•nB u = tvec • ( dir ⅹ e2 ) / e1 • ( dir ⅹ e2 ) v =Ob•nA/OB•nA v = tvec • ( e1 ⅹ dir ) / e2 • ( e1 ⅹ dir ) e1 = V1 - V0e2 = V2 - V0tvec = orig - V0
  • 16. (방법1)Triangle Intersection-4- // Calculate U parameter and test bounds *u = D3DXVec3Dot( &tvec, &pvec ); if( *u < 0.0f || *u > det ) return false; // Prepare to test V parameter D3DXVECTOR3 qvec; D3DXVec3Cross( &qvec, &tvec, &edge1 ); // Calculate V parameter and test bounds *v = D3DXVec3Dot( &dir, &qvec ); if( *v < 0.0f || *u + *v > det ) return false; // Calculate t, scale parameters, ray intersects triangle *t = D3DXVec3Dot( &edge2, &qvec ); FLOAT fInvDet = 1.0f / det; *t *= fInvDet; *u *= fInvDet; *v *= fInvDet; return true; IntersectTriangle(const D3DXVECTOR3& orig, const D3DXVECTOR3& dir, D3DXVECTOR3& v0, D3DXVECTOR3& v1, D3DXVECTOR3& v2, FLOAT* t, FLOAT* u, FLOAT* v ) // Find vectors for two edges sharing vert0 D3DXVECTOR3 edge1 = v1 - v0; D3DXVECTOR3 edge2 = v2 - v0; // Begin calculating determinant //- also used to calculate U parameter D3DXVECTOR3 pvec; D3DXVec3Cross( &pvec, &dir, &edge2 ); // If determinant is near zero, ray lies in plane of triangle FLOAT det = D3DXVec3Dot( &edge1, &pvec ); D3DXVECTOR3 tvec; if( det > 0 ) { tvec = orig - v0;} else { tvec = v0 – orig;det = -det; } if( det < 0.0001f ) return false;
  • 17. 구와 선 교차 //sphere 체크할 구 //ray 체크할 광선 //intersection 교차 시 교차 지점. //distance 광선의 시작점으로부터 교차 점까지의 거리 bool intersect_ray_and_sphere( const Sphere& sphere, const Ray& ray, vector3& intersection, double& distance) {       vector3 l = sphere.center - ray.origin;               double s = dot( l, ray.direction );     double l2 = dot( l, l );     double r2 = pow( sphere.radius, 2 );       if( s < 0 && l2 > r2 )  {                      return false;                       // 광선이 구의 반대 방향을 향하거나 구를 지나친 경우    }      double m2 = l2 - pow( s, 2 );       if( m2 > r2 ) {       return false;                      // 광선이 구를 비껴가는 경우    }      double q = sqrt( r2 - m2 );      // 두 개의 교차점 중 어느것을 구하는가?       if( l2 > r2 ) {                        distance = s - q;    }else {         distance = s + q;    }      intersection = o + distance * ray.direction;      return true;     }
  • 18. 구와 선 교차 //sphere 체크할 구 //ray 체크할 광선 //intersection 교차 시 교차 지점. //distance 광선의 시작점으로부터 교차 점까지의 거리 bool intersect_ray_and_sphere( const Sphere& sphere, const Ray& ray, vector3& intersection, double& distance) {       vector3 l = sphere.center - ray.origin;               double s = dot( l, ray.direction );     double l2 = dot( l, l );     double r2 = pow( sphere.radius, 2 );       if( s < 0 && l2 > r2 )  {                      return false;                       // 광선이 구의 반대 방향을 향하거나 구를 지나친 경우    }      double m2 = l2 - pow( s, 2 );       if( m2 > r2 ) {       return false;                      // 광선이 구를 비껴가는 경우    }      double q = sqrt( r2 - m2 );      // 두 개의 교차점 중 어느것을 구하는가?       if( l2 > r2 ) {                        distance = s - q;    }else {         distance = s + q;    }      intersection = o + distance * ray.direction;      return true;     }
  • 19. 구와 선 교차 //sphere 체크할 구 //ray 체크할 광선 //intersection 교차 시 교차 지점. //distance 광선의 시작점으로부터 교차 점까지의 거리 bool intersect_ray_and_sphere( const Sphere& sphere, const Ray& ray, vector3& intersection, double& distance) {       vector3 l = sphere.center - ray.origin;               double s = dot( l, ray.direction );     double l2 = dot( l, l );     double r2 = pow( sphere.radius, 2 );       if( s < 0 && l2 > r2 )  {                      return false;                       // 광선이 구의 반대 방향을 향하거나 구를 지나친 경우    }      double m2 = l2 - pow( s, 2 );       if( m2 > r2 ) {       return false;                      // 광선이 구를 비껴가는 경우    }      double q = sqrt( r2 - m2 );      // 두 개의 교차점 중 어느것을 구하는가?       if( l2 > r2 ) {                        distance = s - q;    }else {         distance = s + q;    }      intersection = o + distance * ray.direction;      return true;     }
  • 20. 참고자료 C:sersjfghksesktopickntersectTriangle 분석.htm 리얼-타임 렌더링(제 2판) 3D 게임 프로그래밍 & 컴퓨터 그래픽을 위한 수학 (제 2판) http://hermet.pe.kr/68084286