|
我在我的d3d程序中加上了一个 CD3DMesh* 模型,按照DXSDK中pick例子的方法,检测出摄像机与模型的位置关
系。
具体实现如下:
struct D3DVERTEX
{
D3DXVECTOR3 p;
D3DXVECTOR3 n; //定点法线?
FLOAT tu, tv;
};
创建:
CD3DMesh->Create(d3dDevice,_T("*.x") )
SetFVF( d3dDevice, D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 );
pd3dDevice->CreateVertexBuffer( dwNumVertices*sizeof(D3DVERTEX),
D3DUSAGE_WRITEONLY,D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1,D3DPOOL_MANAGED,
&LPDIRECT3DVERTEXBUFFER9, NULL ) )
渲染:(每帧执行)
Pick() //pick例子中函数
{
...
LPD3DXBASEMESH pMesh = jizn4zhu4_1->GetLocalMesh();
LPDIRECT3DVERTEXBUFFER9 pVB;
LPDIRECT3DINDEXBUFFER9 pIB;
pMesh->GetVertexBuffer( &pVB );
pMesh->GetIndexBuffer( &pIB );
WORD* pIndices;
D3DVERTEX* pVertices;
pIB->Lock( 0, 0, (void**)&pIndices, 0 );
pVB->Lock( 0, 0, (void**)&pVertices, 0 );
if( m_bUseD3DX )
{
// When calling D3DXIntersect, one can get just the closest intersection and not
// need to work with a D3DXBUFFER. Or, to get all intersections between the ray and
// the mesh, one can use a D3DXBUFFER to receive all intersections. We show both
// methods.
if( m_bClosestOnly )
{
// Collect only the closest intersection
BOOL bHit;
DWORD dwFace;
FLOAT fBary1, fBary2, fDist;
D3DXIntersect(pMesh, &vPickRayOrig, &vPickRayDir, &bHit, &dwFace, &fBary1, &fBary2,
&fDist,
NULL, NULL);
if( bHit )
{
m_dwNumIntersections = 1;
m_IntersectionArray[0].dwFace = dwFace;
m_IntersectionArray[0].fBary1 = fBary1;
m_IntersectionArray[0].fBary2 = fBary2;
m_IntersectionArray[0].fDist = fDist;
}
else
{
m_dwNumIntersections = 0;
}
}
else
{
// Collect all intersections
BOOL bHit;
LPD3DXBUFFER pBuffer = NULL;
D3DXINTERSECTINFO* pIntersectInfoArray;
if( FAILED( hr = D3DXIntersect(pMesh, &vPickRayOrig, &vPickRayDir, &bHit, NULL,
NULL, NULL, NULL,
&pBuffer, &m_dwNumIntersections) ) )
{
return hr;
}
if( m_dwNumIntersections > 0 )
{
pIntersectInfoArray = (D3DXINTERSECTINFO*)pBuffer->GetBufferPointer();
if( m_dwNumIntersections > MAX_INTERSECTIONS )
m_dwNumIntersections = MAX_INTERSECTIONS;
for( DWORD iIntersection = 0; iIntersection < m_dwNumIntersections;
iIntersection++ )
{
m_IntersectionArray[iIntersection].dwFace = pIntersectInfoArray
[iIntersection].FaceIndex;
m_IntersectionArray[iIntersection].fBary1 = pIntersectInfoArray
[iIntersection].U;
m_IntersectionArray[iIntersection].fBary2 = pIntersectInfoArray
[iIntersection].V;
m_IntersectionArray[iIntersection].fDist = pIntersectInfoArray
[iIntersection].Dist;
}
}
SAFE_RELEASE( pBuffer );
}
}
else
{
// Not using D3DX
DWORD dwNumFaces = jizn4zhu4_1->GetLocalMesh()->GetNumFaces();
FLOAT fBary1, fBary2;
FLOAT fDist;
for( DWORD i=0; i<dwNumFaces; i++ )
{
D3DXVECTOR3 v0 = pVertices[pIndices[3*i+0]].p;
D3DXVECTOR3 v1 = pVertices[pIndices[3*i+1]].p;
D3DXVECTOR3 v2 = pVertices[pIndices[3*i+2]].p;
// Check if the pick ray passes through this point
if( IntersectTriangle( vPickRayOrig, vPickRayDir, v0, v1, v2,
&fDist, &fBary1, &fBary2 ) )
{
if( !m_bClosestOnly || m_dwNumIntersections == 0 || fDist <
m_IntersectionArray[0].fDist )
{
if( m_bClosestOnly )
m_dwNumIntersections = 0;
m_IntersectionArray[m_dwNumIntersections].dwFace = i;
m_IntersectionArray[m_dwNumIntersections].fBary1 = fBary1;
m_IntersectionArray[m_dwNumIntersections].fBary2 = fBary2;
m_IntersectionArray[m_dwNumIntersections].fDist = fDist;
m_dwNumIntersections++;
if( m_dwNumIntersections == MAX_INTERSECTIONS )
break;
}
}
}
}
// Now, for each intersection, add a triangle to m_pVB and compute texture coordinates
if( m_dwNumIntersections > 0 )
{
D3DVERTEX* v;
D3DVERTEX* vThisTri;
WORD* iThisTri;
D3DVERTEX v1, v2, v3;
INTERSECTION* pIntersection;
fff->Lock( 0, 0, (void**)&v, 0 );
for( DWORD iIntersection = 0; iIntersection < m_dwNumIntersections; iIntersection++ )
{
pIntersection = &m_IntersectionArray[iIntersection];
vThisTri = &v[iIntersection * 3];
iThisTri = &pIndices[3*pIntersection->dwFace];
// get vertices hit
vThisTri[0] = pVertices[iThisTri[0]];
vThisTri[1] = pVertices[iThisTri[1]];
vThisTri[2] = pVertices[iThisTri[2]];
//!!!!!!!!!!!!!!!!!!!!!!!
//这是被选中的三角形面法线角度吗?
//!!!!!!!!!!!!!!!!!!!!!!!
vThisTri[1].n.x;
vThisTri[1].n.y;
vThisTri[1].n.z;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// If all you want is the vertices hit, then you are done. In this sample, we
// want to show how to infer texture coordinates as well, using the BaryCentric
// coordinates supplied by D3DXIntersect
FLOAT dtu1 = vThisTri[1].tu - vThisTri[0].tu;
FLOAT dtu2 = vThisTri[2].tu - vThisTri[0].tu;
FLOAT dtv1 = vThisTri[1].tv - vThisTri[0].tv;
FLOAT dtv2 = vThisTri[2].tv - vThisTri[0].tv;
pIntersection->tu = vThisTri[0].tu + pIntersection->fBary1 * dtu1 + pIntersection-
>fBary2 * dtu2;
pIntersection->tv = vThisTri[0].tv + pIntersection->fBary1 * dtv1 + pIntersection-
>fBary2 * dtv2;
}
fff->Unlock();
}
pVB->Unlock();
pIB->Unlock();
pVB->Release();
pIB->Release();
...
}
CD3DMesh->Render(d3dDevice);
我想问vThisTri[1].n是被选中的三角形面法线角度吗?
[em4] |
|