void
ClipPolys( stPolys* pPolys, float
near_clip_z)
{
stPolys PolysTemp;
PolysTemp.iNumTris = 0;
for(int i=0;
i<pPolys->iNumTris; i++)
{
stTri* pCurTri = &(pPolys->pTri[i]);
D3DXVECTOR3* vIn[3];
int iNumVertsIn=0;
bool bV1=false;
bool bV2=false;
bool bV3=false;
if( pCurTri->v1.z > near_clip_z
)
{
iNumVertsIn++;
bV1=true;
}
if( pCurTri->v2.z > near_clip_z
)
{
iNumVertsIn++;
bV2=true;
}
if( pCurTri->v3.z > near_clip_z
)
{
iNumVertsIn++;
bV3=true;
}
//If iNumVertsIn is zero here, we
should reject the whole triangle
/* add later */
if( iNumVertsIn == 1 )
{
if( bV1 ){ vIn[0]=&(pCurTri->v1);
vIn[1]=&(pCurTri->v2); vIn[2]=&(pCurTri->v3); }
if( bV2 ){ vIn[0]=&(pCurTri->v2);
vIn[1]=&(pCurTri->v1); vIn[2]=&(pCurTri->v3); }
if( bV3 ){ vIn[0]=&(pCurTri->v3);
vIn[1]=&(pCurTri->v2); vIn[2]=&(pCurTri->v1); }
//Parametric line stuff
// p = v0 + v01*t
D3DXVECTOR3 v01 = *vIn[1] - *vIn[0];
float t1 = ((near_clip_z -
(*vIn[0]).z)/v01.z );
float newz1 = near_clip_z;
float newx1 = (*vIn[0]).x
+ v01.x * t1;
float newy1 = (*vIn[0]).y
+ v01.y * t1;
// Second vert point
D3DXVECTOR3 v02 = *vIn[2] - *vIn[0];
float t2 = ((near_clip_z -
(*vIn[0]).z)/v02.z );
float newz2 = near_clip_z;
float newx2 = (*vIn[0]).x
+ v02.x * t2;
float newy2 = (*vIn[0]).y
+ v02.y * t2;
vIn[2]->x = newx2;
vIn[2]->y = newy2;
vIn[2]->z = newz2;
vIn[1]->x = newx1;
vIn[1]->y = newy1;
vIn[1]->z = newz1;
PolysTemp.pTri[ PolysTemp.iNumTris ] = (*pCurTri);
PolysTemp.iNumTris++;
}
// Two of the verts are outside the
clip range, so we need to clip and add
// an extra triangle.
else if
(iNumVertsIn == 2 )
{
if( !bV1 ){ vIn[0]=&(pCurTri->v2);
vIn[1]=&(pCurTri->v3); vIn[2]=&(pCurTri->v1); }
if( !bV2 ){ vIn[0]=&(pCurTri->v1);
vIn[1]=&(pCurTri->v3); vIn[2]=&(pCurTri->v2); }
if( !bV3 ){ vIn[0]=&(pCurTri->v1);
vIn[1]=&(pCurTri->v2); vIn[2]=&(pCurTri->v3); }
//Parametric line stuff
// p = v0 + v01*t
D3DXVECTOR3 v01 = *vIn[2] - *vIn[0];
float t1 = ((near_clip_z -
(*vIn[0]).z)/v01.z );
float newz1 = near_clip_z;
float newx1 = (*vIn[0]).x
+ v01.x * t1;
float newy1 = (*vIn[0]).y
+ v01.y * t1;
// Second point
D3DXVECTOR3 v02 = *vIn[2] - *vIn[1];
float t2 = ((near_clip_z -
(*vIn[1]).z)/v02.z );
float newz2 = near_clip_z;
float newx2 = (*vIn[1]).x
+ v02.x * t2;
float newy2 = (*vIn[1]).y
+ v02.y * t2;
vIn[2]->x = newx1;
vIn[2]->y = newy1;
vIn[2]->z = newz1;
PolysTemp.pTri[ PolysTemp.iNumTris ] = (*pCurTri);
PolysTemp.iNumTris++;
stTri TriTemp;
TriTemp.v1 = D3DXVECTOR3(newx1, newy1, newz1);
TriTemp.v2 = D3DXVECTOR3(newx2, newy2, newz2);
TriTemp.v3 = (*vIn[1]);
PolysTemp.pTri[ PolysTemp.iNumTris ] = TriTemp;
PolysTemp.iNumTris++;
}
// All the verts are in the triangle -
hence the whole triangle is within
// the clip range, and doesn't need
clipping.
else if
(iNumVertsIn == 3 )
{
PolysTemp.pTri[ PolysTemp.iNumTris ] = (*pCurTri);
PolysTemp.iNumTris++;
}
}//
End for loop i
memcpy(pPolys, &PolysTemp, sizeof(stPolys));
}//
End of ClipPolys(..) |