투영행렬로 변환 한 후 다시 카메라 공간의 위치,벡터(norm) 을 구하는 방법
D3DXVECTOR3 p1InCamera(33,-24,-2),p2InCamera(0,0,2);
D3DXVECTOR3 normC,norm(10,0,-10),normP;
D3DXVECTOR4 outP1InProj,outP1InProj_w,outP2InProj,outP2InProj_w,outNorm,resultCameraP;
D3DXVec3Normalize( &norm,&norm );
D3DXVECTOR3 outP1InProj_( outP1InProj.x,outP1InProj.y,outP1InProj.z ),cameraP,projP;
/*
* D3DXVec3Transform 는 w 로 결과 벡터를 나누지 않고 그대로 D3DXVECTOR4 를 만들지만
* D3DXVec3TransformCoord 는 결과 벡터를 w 로 나누고 w 값을 1 로 채우는 형식이다
* D3DXVec3TransformCoord 로써 투영행럴의 역행렬 을 적용하여 카메라공간에서의 를 점을 얻을 수 있다
**/
D3DXVec3Transform( &outP1InProj, &p1InCamera, &matProjection );
projP.x=outP1InProj.x;
projP.y=outP1InProj.y;
projP.z=outP1InProj.z;
projP/=outP1InProj.w;
D3DXVec3Transform( &resultCameraP, &projP, &invProj );
resultCameraP/=resultCameraP.w;
필요한경우 노멀벡터또한 투영행렬로 변환했다가 역투영행렬로 변환 할 수 있는데 다음과 같다
D3DXVec3TransformCoord( &normP, &norm, &matProjection ); //normP 는 내부적트로 계산된 normPInner/=normPInner.w 의 형태임을 주의해야한다!!!
D3DXVec3TransformCoord( &normP, &normP, &invProj ); // 아래또한 같다
D3DXVec3TransformNormal 은 투영행렬의 특성상 z 값이 소실될 수 있음으로 z 값이 제대로 나오지 못한다
한가지 주의사항은 normal 을 투영행렬 공간에서 어떠한 연산을 하면 안된다는 것이다
왜냐하면 D3DXVec3TransformCoord 를 적용한 투영공간상의 z 는 0 < z < 1 의 값으로 그려지는 오브젝트에 대해서 z값이
항상 양수임으로
아래는 d3dx9math.h 내용
언젠가 올려놓은적이 있는것 같은데 긁어옴
http://cafe.naver.com/jzsdn/6192
Matematika v Direct3DX
V této kapitole je seznam matematických funkcí Direct3DX (což je jakýsi toolkit pro Direct3D (DirectXGraphics), jistá obdoba knihovny GLUT pro OpenGL), spolu s popisem matematiky, kterou tyto funkce provádí.
Upozorňuji, že toto není oficiální dokumentace k Direct3DX (ta se nalézá někde na MSDN).
Vektory
D3DXVecNAdd(Out, V1, V2)
Out = V1 + V2
D3DXVecNBaryCentric(Out, V1, V2, V3, f, g)
Out = V1 + f⋅(V2−V1) + g⋅(V3−V1)
D3DXVecNCatmullRom(Out, V1, V2, V3, V4, s)
/ | 0 | 1 | 0 | 0 | \ | / | V1 | \ | |
| | | | | | | | ||||||
| | −0,5 | 0 | 0,5 | 0 | | | | | V2 | | | |
Out = (1,s,s²,s³) | | | | | | | | | |||||
| | 1 | −2,5 | 2 | −0,5 | | | | | V3 | | | |
| | | | | | | | ||||||
\ | −0,5 | 1,5 | −1,5 | 0,5 | / | \ | V4 | / |
D3DXVecNDot(U, V)
Result = U ⋅ V = ∑iUiVi
D3DXVecNHermite(Out, V1, T1, V2, T2, s)
/ | 1 | 0 | 0 | 0 | \ | / | V1 | \ | |
| | | | | | | | ||||||
| | 0 | 0 | 1 | 0 | | | | | V2 | | | |
Out = (1,s,s²s³) | | | | | | | | | |||||
| | −3 | 3 | −2 | −1 | | | | | T1 | | | |
| | | | | | | | ||||||
\ | 2 | −2 | 1 | 1 | / | \ | T2 | / |
D3DXVecNLength(V)
Result = |V| = √(∑iVi²)
D3DXVecNLengthSq(V)
Result = |V|² = ∑iVi²
D3DXVecNLerp(Out, V1, V2, s)
Out = V1 + s(V2−V1);
D3DXVecNMaximize(Out, U, V)
Outi = max(Ui, Vi)
D3DXVecNMinimize(Out, U, V)
Outi = min(Ui, Vi)
D3DXVecNNormalize(Out, V)
Out = 1⁄|V|⋅V
D3DXVecNScale(Out, V, s)
Out = sV
D3DXVecNSubtract(Out, V1, V2)
Out = V1 − V2
2D Vektory
D3DXVec2CCW(U, V)
Result = UxVy − UyVx
D3DXVec2Transform(Out, V, M)
a = (Vx, Vy, 0, 1)
b = (a×M)T
Out = (bx, by)
D3DXVec2TransformCoord(Out, V, M)
a = (Vx, Vy, 0, 1)
b = (a×M)T
Out = 1⁄bw(bx, by)
D3DXVec2TransformNormal(Out, V, M)
a = (Vx, Vy, 0, 0)
b = (a×M)T
Out = (bx, by)
3D Vektory
D3DXVec3Cross(Out, V1, V2)
Out = V1 × V2
D3DXVec3Project(Out, V, Viewport, Proj, View, World)
a = (Vx, Vy, Vz, 1)
b = a×World×View×Proj
c = b⁄bw
Outx = ViewportX + ViewportWidth*(1+cx)/2
Outy = ViewportY + ViewportHeight*(1-cy)/2
Outz = ViewportMinZ + cz*(ViewportMaxZ-ViewportMinZ)
D3DXVec3Transform(Out, V, M)
a = (Vx, Vy, Vz, 1)
b = (a×M)T
Out = (bx, by, bz)
D3DXVec3TransformCoord(Out, V, M)
a = (Vx, Vy, Vz, 1)
b = (a×M)T
Out = 1⁄bw(bx, by, bz)
D3DXVec3TransformNormal(Out, V, M)
a = (Vx, Vy, Vz, 0)
b = (a×M)T
Out = (bx, by, bz)
D3DXVec3Unproject(Out, V, Viewport, Proj, View, World)
M = (World×View×Proj)−1
ax = (Vx-Viewportx)*2/ViewportWidth - 1
ay = 1 - (Vy-Viewporty)*2/ViewportHeight
az = (Vz-ViewportMinZ)/(ViewportMaxZ-ViewportMinZ)
aw = 1
b = (a×M)T
Out = 1⁄bw(bx, by, bz)
4D Vektory
D3DXVec4Cross(Out, U, V, W)
a = VxWy − VyWx
b = VxWz − VzWx
c = VxWw − VwWx
d = VyWz − VzWy
e = VyWw − VwWy
f = VzWw − VwWz
Outx = fUy − eUz + dUw
Outy = fUx + cUz − bUw
Outz = eUx − cUy + aUw
Outw = dUx + bUy − aUz
D3DXVec4Transform(Out, V, M)
Out = (V×M)T
Matice (4×4)
D3DXMatrixAffineTransformation(Out, s, c, r, t)
/ | s(2(ry² + rz²) − 1) | 2s(rxry + rzrw) | 2s(rxrz − ryrw) | 0 | \ | |
| | | | |||||
| | 2s(rxry − rzrw) | s(2(rx² + rz²) − 1) | 2s(ryrz + rxrw) | 0 | | | |
Out = | | | | | ||||
| | 2s(rxrz + ryrw) | 2s(ryrz − rxrw) | s(2(rx² + ry²) − 1) | 0 | | | |
| | | | |||||
\ | tx+2(cxry²+cxrz²−cyrxry+cyrzrw−czrxrz−czryrw) | ty+2(cyrx²+cyrz²−cxrxry−cxrzrw−czryrz+czrxrw) | tz+2(czrx²+czry²−cxrxrz+cxryrw−cyryrz−cyrxrw) | 1 | / |
D3DXMatrixfDeterminant(M)
Result = det M
D3DXMatrixIdentity(Out)
/ | 1 | 0 | 0 | 0 | \ | |
| | | | |||||
| | 0 | 1 | 0 | 0 | | | |
Out = E = | | | | | ||||
| | 0 | 0 | 1 | 0 | | | |
| | | | |||||
\ | 0 | 0 | 0 | 1 | / |
D3DXMatrixInverse(Out, D, M)
D = det M
Out = M−1
D3DXMatrixIsIdentity(M)
Result = M≡E
D3DXMatrixLookAtRH(Out, Eye, At, Up)
v = Normalized(Eye−At)
l = Up×v
u = v×l
/ | lx | ux | vx | 0 | \ | |
| | | | |||||
| | ly | uy | vy | 0 | | | |
Out = | | | | | ||||
| | lz | uz | vz | 0 | | | |
| | | | |||||
\ | −l⋅Eye | −u⋅Eye | −v⋅Eye | 1 | / |
D3DXMatrixLookAtLH(Out, Eye, At, Up)
v = Normalized(At−Eye)
r = Up×v
u = v×r
/ | rx | ux | vx | 0 | \ | |
| | | | |||||
| | ry | uy | vy | 0 | | | |
Out = | | | | | ||||
| | rz | uz | vz | 0 | | | |
| | | | |||||
\ | −r⋅Eye | −u⋅Eye | −v⋅Eye | 1 | / |
D3DXMatrixMultiply(Out, M1, M2)
Out = M1×M2
D3DXMatrixOrthoRH(Out, w, h, n, f)
Q = (f−n)−1
/ | 2⁄w | 0 | 0 | 0 | \ | |
| | | | |||||
| | 0 | 2⁄h | 0 | 0 | | | |
Out = | | | | | ||||
| | 0 | 0 | −Q | 0 | | | |
| | | | |||||
\ | 0 | 0 | −Qn | 1 | / |
D3DXMatrixOrthoLH(Out, w, h, n, f)
Q = (f−n)−1
/ | 2⁄w | 0 | 0 | 0 | \ | |
| | | | |||||
| | 0 | 2⁄h | 0 | 0 | | | |
Out = | | | | | ||||
| | 0 | 0 | Q | 0 | | | |
| | | | |||||
\ | 0 | 0 | −Qn | 1 | / |
D3DXMatrixOrthoOffCenterRH(Out, l, r, t, b, n, f)
Q = (f−n)−1
w = r−l
h = b−t
/ | 2⁄w | 0 | 0 | 0 | \ | |
| | | | |||||
| | 0 | 2⁄h | 0 | 0 | | | |
Out = | | | | | ||||
| | 0 | 0 | −Q | 0 | | | |
| | | | |||||
\ | −(r+l)⁄2 | −(t+b)⁄2 | −Qn | 1 | / |
D3DXMatrixOrthoOffCenterLH(Out, l, r, t, b, n, f)
Q = (f−n)−1
w = r−l
h = b−t
/ | 2⁄w | 0 | 0 | 0 | \ | |
| | | | |||||
| | 0 | 2⁄h | 0 | 0 | | | |
Out = | | | | | ||||
| | 0 | 0 | Q | 0 | | | |
| | | | |||||
\ | −(r+l)⁄2 | −(t+b)⁄2 | −Qn | 1 | / |
D3DXMatrixPerspectiveRH(Out, w, h, n, f)
Q = (f−n)−1
/ | 2⁄w | 0 | 0 | 0 | \ | |
| | | | |||||
| | 0 | 2⁄h | 0 | 0 | | | |
Out = | | | | | ||||
| | 0 | 0 | −Qf | −1 | | | |
| | | | |||||
\ | 0 | 0 | −Qnf | 0 | / |
D3DXMatrixPerspectiveLH(Out, w, h, n, f)
Q = (f−n)−1
/ | 2⁄w | 0 | 0 | 0 | \ | |
| | | | |||||
| | 0 | 2⁄h | 0 | 0 | | | |
Out = | | | | | ||||
| | 0 | 0 | Qf | 1 | | | |
| | | | |||||
\ | 0 | 0 | −Qnf | 0 | / |
D3DXMatrixPerspectiveFovRH(Out, Fovy, Aspect, n, f)
Q = (f−n)−1
y = cotg(Fovy)⁄2
/ | y⁄Aspect | 0 | 0 | 0 | \ | |
| | | | |||||
| | 0 | y | 0 | 0 | | | |
Out = | | | | | ||||
| | 0 | 0 | −Qf | −1 | | | |
| | | | |||||
\ | 0 | 0 | −Qnf | 0 | / |
D3DXMatrixPerspectiveFovLH(Out, Fovy, Aspect, n, f)
Q = (f−n)−1
y = cotg(Fovy)⁄2
/ | y⁄Aspect | 0 | 0 | 0 | \ | |
| | | | |||||
| | 0 | y | 0 | 0 | | | |
Out = | | | | | ||||
| | 0 | 0 | Qf | 1 | | | |
| | | | |||||
\ | 0 | 0 | −Qnf | 0 | / |
D3DXMatrixPerspectiveOffCenterRH(Out, l, r, t, b, n, f)
Q = (f−n)−1
w = r−l
h = b−t
/ | 2⁄w | 0 | 0 | 0 | \ | |
| | | | |||||
| | 0 | 2⁄h | 0 | 0 | | | |
Out = | | | | | ||||
| | (r+l)⁄w | (t+b)⁄h | −Qf | −1 | | | |
| | | | |||||
\ | 0 | 0 | −Qnf | 0 | / |
D3DXMatrixPerspectiveOffCenterLH(Out, l, r, t, b, n, f)
Q = (f−n)−1
w = r−l
h = b−t
/ | 2⁄w | 0 | 0 | 0 | \ | |
| | | | |||||
| | 0 | 2⁄h | 0 | 0 | | | |
Out = | | | | | ||||
| | −(r+l)⁄w | −(t+b)⁄h | Qf | 1 | | | |
| | | | |||||
\ | 0 | 0 | −Qnf | 0 | / |
D3DXMatrixReflect(Out, Plane)
(a,b,c,d) = (Planea,Planeb,Planec,Planed)⁄√(Planea²+Planeb²+Planec²)
/ | 1−2a² | −2ba | −2ca | 0 | \ | |
| | | | |||||
| | −2ab | 1−2b² | −2cb | 0 | | | |
Out = | | | | | ||||
| | −2ac | −2bc | 1−2c² | 0 | | | |
| | | | |||||
\ | −2ad | −2bd | −2cd | 1 | / |
D3DXMatrixRotationAxis(Out, V, Angle)
s = sin Angle
c = cos Angle
d = 1−c
(x,y,z) = V
/ | dx²+c | dxy+zs | dxz−ys | 0 | \ | |
| | | | |||||
| | dxy−zs | dy²+c | dyz+xs | 0 | | | |
Out = | | | | | ||||
| | dxy+ys | dyz−xs | dz²+c | 0 | | | |
| | | | |||||
\ | 0 | 0 | 0 | 1 | / |
D3DXMatrixRotationQuaternion(Out, Q)
(x,y,z,w) = Q
/ | 1−2y²−2z² | 2xy+2zw | 2xz−2yw | 0 | \ | |
| | | | |||||
| | 2xy−2zw | 1−2x²−2z² | 2yz+2xw | 0 | | | |
Out = | | | | | ||||
| | 2xz+2yw | 2yz−2xw | 1−2x²−2y² | 0 | | | |
| | | | |||||
\ | 0 | 0 | 0 | 1 | / |
D3DXMatrixRotationX(Out, Angle)
s = sin Angle
c = cos Angle
/ | 1 | 0 | 0 | 0 | \ | |
| | | | |||||
| | 0 | c | s | 0 | | | |
Out = | | | | | ||||
| | 0 | −s | c | 0 | | | |
| | | | |||||
\ | 0 | 0 | 0 | 1 | / |
D3DXMatrixRotationY(Out, Angle)
s = sin Angle
c = cos Angle
/ | c | 0 | −s | 0 | \ | |
| | | | |||||
| | 0 | 1 | 0 | 0 | | | |
Out = | | | | | ||||
| | s | 0 | c | 0 | | | |
| | | | |||||
\ | 0 | 0 | 0 | 1 | / |
D3DXMatrixRotationYawPitchRoll(Out, Yaw, Pitch, Roll)
(sa,sb,sc) = sin (Roll, Pitch, Yaw)
(ca,cb,cc) = cos (Roll, Pitch, Yaw)
/ | ca⋅cc+sa⋅sb⋅sc | −sa⋅cc+ca⋅sb⋅sc | cb⋅sc | 0 | \ | |
| | | | |||||
| | sa⋅cb | ca⋅cb | −sb | 0 | | | |
Out = | | | | | ||||
| | −ca⋅sc+sa⋅sb⋅cc | sa⋅sc+ca⋅sb⋅cc | cb⋅cc | 0 | | | |
| | | | |||||
\ | 0 | 0 | 0 | 1 | / |
D3DXMatrixRotationZ(Out, Angle)
s = sin Angle
c = cos Angle
/ | c | s | 0 | 0 | \ | |
| | | | |||||
| | −s | c | 0 | 0 | | | |
Out = | | | | | ||||
| | 0 | 0 | 1 | 0 | | | |
| | | | |||||
\ | 0 | 0 | 0 | 1 | / |
D3DXMatrixScaling(Out, x, y, z)
/ | x | 0 | 0 | 0 | \ | |
| | | | |||||
| | 0 | y | 0 | 0 | | | |
Out = | | | | | ||||
| | 0 | 0 | z | 0 | | | |
| | | | |||||
\ | 0 | 0 | 0 | 1 | / |
D3DXMatrixShadow(Out, Light, Plane)
(a,b,c,d) = (Planea,Planeb,Planec,Planed)⁄√(Planea²+Planeb²+Planec²)
(x,y,z,w) = Light
f = Lightx⋅Planea + Lighty⋅Planeb + Lightz⋅Planec + Lightw⋅Planed
/ | f−xa | −ya | −za | −wa | \ | |
| | | | |||||
| | −xb | f−yb | −zb | −wb | | | |
Out = | | | | | ||||
| | −xc | −yc | f−zc | −wc | | | |
| | | | |||||
\ | −xd | −yd | −zd | f−wd | / |
D3DXMatrixTransformation(Out, Scenter, Srot, Scaling, Rotcenter, Rot, Trans)
D3DXMatrixTranslation(A, -Scenterx, -Scentery, -Scenterz)
D3DXMatrixScaling(B, Scalingx, Scalingy, Scalingz)
D3DXMatrixRotationQuaternion(C, Srot)
u = Scenter − Rotcenter
D3DXMatrixTranslation(D, ux, uy, uz)
D3DXMatrixRotationQuaternion(E, Rot)
v = Rotcenter + Trans
D3DXMatrixTranslation(F, vx, vy, vz)
Out = A×CT×B×C×D×E×F
D3DXMatrixTranslation(Out, x, y, z)
/ | 1 | 0 | 0 | 0 | \ | |
| | | | |||||
| | 0 | 1 | 0 | 0 | | | |
Out = | | | | | ||||
| | 0 | 0 | 1 | 0 | | | |
| | | | |||||
\ | x | y | z | 1 | / |
D3DXMatrixTranspose(Out, M)
Out = MT
Roviny
D3DXPlaneDot(P, V)
Result = (Pa, Pb, Pc, Pd)⋅V
D3DXPlaneDotCoord(P, V)
Result = (Pa, Pb, Pc)⋅V + Pd
D3DXPlaneDotNormal(P, V)
Result = (Pa, Pb, Pc)⋅V
D3DXPlaneIntersectLine(Out, P, U, V)
n = (Planea, Planeb, Planec)
d = V − U
Out = U − d⋅(Pd + n⋅U)⁄(d⋅n) [iff d⋅n ≠ 0]
D3DXPlaneFromPointNormal(Out, P, N)
Planea = Nx
Planeb = Ny
Planec = Nz
Planed = −N⋅P
D3DXPlaneNormalize(Out, P)
q = 1⁄√(Pa² + Pb² + Pc²)
Outa = q⋅Pa
Outb = q⋅Pb
Outc = q⋅Pc
Outd = q⋅Pd
D3DXPlaneFromPoints(Out, A, B, C)
v = (B − A) × (C − A)
n = 1⁄|v| v
Outa = nx
Outb = ny
Outc = nz
Outd = −n⋅A
D3DXPlaneTransform(Out, P, M)
Q = P⁄|P|
u = (Qa, Qb, Qc, 0)
D = Qd
A = (−Dux, −Duy, −Duz, 1)
B = A×M
v = u×M
q = 1⁄|v|
Outa = qvx
Outb = qvy
Outc = qvz
Outd = −qv⋅B
'수학 (Mathematics) > 3D수학' 카테고리의 다른 글
구현한 범프 매핑 설명(탄젠트 공간) (0) | 2014.12.09 |
---|---|
입체각(스테라디안) (0) | 2013.07.25 |
점,벡터 구분의 미묘함 (0) | 2013.05.08 |
쿼터니온을 사용해 오브젝트 회전시키기 (0) | 2013.04.24 |
Catmull-Rom 스플라인 곡선 D3DXVec3CatmullRom (0) | 2013.03.21 |