📄 c3_shape.cpp
字号:
SetTextureStageState ( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
SetTextureStageState ( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
SetTextureStageState ( 0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR );
SetTextureStageState ( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
SetTextureStageState ( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
SetTextureStageState ( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
SetTextureStageState ( 1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
SetTextureStageState ( 1, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
SetTextureStageState ( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
SetTextureStageState ( 1, D3DTSS_MINFILTER, D3DTEXF_NONE );
SetTextureStageState ( 1, D3DTSS_MAGFILTER, D3DTEXF_NONE );
SetTextureStageState ( 1, D3DTSS_MIPFILTER, D3DTEXF_NONE );
}
C3_CORE_DLL_API
void Shape_ChangeTexture ( C3Shape *lpShape, int nTexID )
{
lpShape->nTex = nTexID;
}
C3_CORE_DLL_API
void Shape_Muliply ( C3Shape *lpShape, D3DXMATRIX *matrix )
{
D3DXMatrixMultiply ( &lpShape->lpMotion->matrix,
&lpShape->lpMotion->matrix,
matrix );
}
C3_CORE_DLL_API
void Shape_NextFrame ( C3Shape *lpShape, int nStep )
{
lpShape->lpMotion->nFrame = ( lpShape->lpMotion->nFrame + nStep ) % lpShape->lpMotion->dwFrames;
}
C3_CORE_DLL_API
void Shape_SetFrame ( C3Shape *lpShape, DWORD dwFrame )
{
lpShape->lpMotion->nFrame = dwFrame;
}
C3_CORE_DLL_API
void Shape_ClearMatrix ( C3Shape *lpShape )
{
// 清除运动矩阵
D3DXMatrixIdentity ( &lpShape->lpMotion->matrix );
}
C3_CORE_DLL_API
BOOL Shape_DrawAlpha(C3Shape *lpShape, BOOL bLocal)
{
// 首先转换线顶点
D3DXMATRIX mm;
if ( bLocal )
mm = lpShape->lpMotion->lpFrames[lpShape->lpMotion->nFrame];
else
D3DXMatrixMultiply( &mm,
&lpShape->lpMotion->lpFrames[lpShape->lpMotion->nFrame],
&lpShape->lpMotion->matrix );
D3DXVECTOR3 vec[2];
D3DXVec3TransformCoord( &vec[0],
(D3DXVECTOR3*)&lpShape->lpLine[0].lpVB[0],
&mm );
D3DXVec3TransformCoord( &vec[1],
(D3DXVECTOR3*)&lpShape->lpLine[0].lpVB[1],
&mm );
if ( !lpShape->bFrist )
{
// 计算长度
D3DXVECTOR3 sub;
D3DXVec3Subtract ( &sub, &vec[0], &vec[1] );
float len = D3DXVec3Length ( &sub );
D3DXVECTOR3 last[2];
last[0] = lpShape->last[0];
last[1] = lpShape->last[1];
D3DXVECTOR3 vPos;
if ( bLocal )
mm = lpShape->lpMotion->matrix;
else
D3DXMatrixIdentity ( &mm );
// 计算插值的点
for ( DWORD nn = 0; nn < lpShape->dwSmooth; nn++ )
{
D3DXVECTOR3 smooths[2];
D3DXVec3Lerp ( &smooths[0],
&lpShape->last[0],
&vec[0],
1.0f / ( lpShape->dwSmooth + 1 ) * ( nn + 1 ) );
D3DXVec3Lerp ( &smooths[1],
&lpShape->last[1],
&vec[1],
1.0f / ( lpShape->dwSmooth + 1 ) * ( nn + 1 ) );
// 计算长度
D3DXVec3Subtract ( &sub,
&smooths[0],
&smooths[1] );
float lennow = D3DXVec3Length ( &sub );
D3DXVec3Lerp ( &smooths[0],
&smooths[1],
&smooths[0],
len / lennow );
D3DXVec3Subtract ( &sub,
&smooths[0],
&smooths[1] );
lennow = D3DXVec3Length ( &sub );
// 算刀光
int cur = lpShape->dwSegmentCur * 6;
lpShape->vb[cur + 0].x = smooths[0].x;
lpShape->vb[cur + 0].y = smooths[0].y;
lpShape->vb[cur + 0].z = smooths[0].z;
lpShape->vb[lpShape->dwSegmentCur * 6 + 0].color = D3DCOLOR_ARGB ( 255, 255, 255, 255 );
lpShape->vb[cur + 0].u = 0;
lpShape->vb[cur + 0].v = 0;
lpShape->vb[cur + 1].x = smooths[1].x;
lpShape->vb[cur + 1].y = smooths[1].y;
lpShape->vb[cur + 1].z = smooths[1].z;
lpShape->vb[lpShape->dwSegmentCur * 6 + 1].color = D3DCOLOR_ARGB ( 255, 255, 255, 255 );
lpShape->vb[cur + 1].u = 0;
lpShape->vb[cur + 1].v = 0;
lpShape->vb[cur + 2].x = last[1].x;
lpShape->vb[cur + 2].y = last[1].y;
lpShape->vb[cur + 2].z = last[1].z;
lpShape->vb[lpShape->dwSegmentCur * 6 + 2].color = D3DCOLOR_ARGB ( 255, 255, 255, 255 );
lpShape->vb[cur + 2].u = 0;
lpShape->vb[cur + 2].v = 0;
lpShape->vb[cur + 3].x = last[0].x;
lpShape->vb[cur + 3].y = last[0].y;
lpShape->vb[cur + 3].z = last[0].z;
lpShape->vb[lpShape->dwSegmentCur * 6 + 3].color = D3DCOLOR_ARGB ( 255, 255, 255, 255 );
lpShape->vb[cur + 3].u = 0;
lpShape->vb[cur + 3].v = 0;
lpShape->vb[cur + 4].x = last[1].x;
lpShape->vb[cur + 4].y = last[1].y;
lpShape->vb[cur + 4].z = last[1].z;
lpShape->vb[lpShape->dwSegmentCur * 6 + 4].color = D3DCOLOR_ARGB ( 255, 255, 255, 255 );
lpShape->vb[cur + 4].u = 0;
lpShape->vb[cur + 4].v = 0;
lpShape->vb[cur + 5].x = smooths[0].x;
lpShape->vb[cur + 5].y = smooths[0].y;
lpShape->vb[cur + 5].z = smooths[0].z;
lpShape->vb[lpShape->dwSegmentCur * 6 + 5].color = D3DCOLOR_ARGB ( 255, 255, 255, 255 );
lpShape->vb[cur + 5].u = 0;
lpShape->vb[cur + 5].v = 0;
lpShape->dwSegmentCur++;
if ( lpShape->dwSegmentCur == lpShape->dwSegment )
lpShape->dwSegmentCur = 0;
last[0] = smooths[0];
last[1] = smooths[1];
if ( nn == lpShape->dwSmooth - 1 )
{
lpShape->last[0] = smooths[0];
lpShape->last[1] = smooths[1];
}
for ( int i = 0; i < 6; i++ )
{
vPos.x = lpShape->vb[cur + i].x;
vPos.y = lpShape->vb[cur + i].y;
vPos.z = lpShape->vb[cur + i].z;
D3DXVec3Project( &lpShape->pScreenPnt[cur + i],
&vPos,
&g_Viewport,
&g_ProjectMatrix,
&g_ViewMatrix,
&mm );
if ( lpShape->pScreenPnt[cur + i].x < lpShape->TearAirTexRect.left )
lpShape->TearAirTexRect.left = (int)lpShape->pScreenPnt[cur + i].x;
else if ( lpShape->pScreenPnt[cur + i].x > lpShape->TearAirTexRect.right )
lpShape->TearAirTexRect.right = (int)lpShape->pScreenPnt[cur + i].x;
if ( lpShape->pScreenPnt[cur + i].y < lpShape->TearAirTexRect.top )
lpShape->TearAirTexRect.top = (int)lpShape->pScreenPnt[cur + i].y;
else if ( lpShape->pScreenPnt[cur + i].y > lpShape->TearAirTexRect.bottom )
lpShape->TearAirTexRect.bottom = (int)lpShape->pScreenPnt[cur + i].y;
}
}
int cur = lpShape->dwSegmentCur * 6;
lpShape->vb[cur + 0].x = vec[0].x;
lpShape->vb[cur + 0].y = vec[0].y;
lpShape->vb[cur + 0].z = vec[0].z;
lpShape->vb[lpShape->dwSegmentCur * 6 + 0].color = D3DCOLOR_ARGB ( 255, 255, 255, 255 );
lpShape->vb[cur + 0].u = 0;
lpShape->vb[cur + 0].v = 0;
lpShape->vb[cur + 1].x = vec[1].x;
lpShape->vb[cur + 1].y = vec[1].y;
lpShape->vb[cur + 1].z = vec[1].z;
lpShape->vb[lpShape->dwSegmentCur * 6 + 1].color = D3DCOLOR_ARGB ( 255, 255, 255, 255 );
lpShape->vb[cur + 1].u = 0;
lpShape->vb[cur + 1].v = 0;
lpShape->vb[cur + 2].x = lpShape->last[1].x;
lpShape->vb[cur + 2].y = lpShape->last[1].y;
lpShape->vb[cur + 2].z = lpShape->last[1].z;
lpShape->vb[lpShape->dwSegmentCur * 6 + 2].color = D3DCOLOR_ARGB ( 255, 255, 255, 255 );
lpShape->vb[cur + 2].u = 0;
lpShape->vb[cur + 2].v = 0;
lpShape->vb[cur + 3].x = lpShape->last[0].x;
lpShape->vb[cur + 3].y = lpShape->last[0].y;
lpShape->vb[cur + 3].z = lpShape->last[0].z;
lpShape->vb[lpShape->dwSegmentCur * 6 + 3].color = D3DCOLOR_ARGB ( 255, 255, 255, 255 );
lpShape->vb[cur + 3].u = 0;
lpShape->vb[cur + 3].v = 0;
lpShape->vb[cur + 4].x = lpShape->last[1].x;
lpShape->vb[cur + 4].y = lpShape->last[1].y;
lpShape->vb[cur + 4].z = lpShape->last[1].z;
lpShape->vb[lpShape->dwSegmentCur * 6 + 4].color = D3DCOLOR_ARGB ( 255, 255, 255, 255 );
lpShape->vb[cur + 4].u = 0;
lpShape->vb[cur + 4].v = 0;
lpShape->vb[cur + 5].x = vec[0].x;
lpShape->vb[cur + 5].y = vec[0].y;
lpShape->vb[cur + 5].z = vec[0].z;
lpShape->vb[lpShape->dwSegmentCur * 6 + 5].color = D3DCOLOR_ARGB ( 255, 255, 255, 255);
lpShape->vb[cur + 5].u = 0;
lpShape->vb[cur + 5].v = 0;
for ( DWORD i = 0; i < 6; i++ )
{
vPos.x = lpShape->vb[cur + i].x;
vPos.y = lpShape->vb[cur + i].y;
vPos.z = lpShape->vb[cur + i].z;
D3DXVec3Project( &lpShape->pScreenPnt[cur + i],
&vPos,
&g_Viewport,
&g_ProjectMatrix,
&g_ViewMatrix,
&mm );
if ( lpShape->pScreenPnt[cur + i].x < lpShape->TearAirTexRect.left )
lpShape->TearAirTexRect.left = (int)lpShape->pScreenPnt[cur + i].x;
else if ( lpShape->pScreenPnt[cur + i].x > lpShape->TearAirTexRect.right )
lpShape->TearAirTexRect.right = (int)lpShape->pScreenPnt[cur + i].x;
if ( lpShape->pScreenPnt[cur + i].y < lpShape->TearAirTexRect.top )
lpShape->TearAirTexRect.top = (int)lpShape->pScreenPnt[cur + i].y;
else if ( lpShape->pScreenPnt[cur + i].y > lpShape->TearAirTexRect.bottom )
lpShape->TearAirTexRect.bottom = (int)lpShape->pScreenPnt[cur + i].y;
}
// Set the texcoord
for ( i = 0; i < lpShape->dwSegment; i++ )
{
for ( int j = 0; j < 6; j++ )
{
if ( lpShape->vb[i * 6 + j].x == 0 )
{
lpShape->vb[i * 6 + j].u = 0;
lpShape->vb[i * 6 + j].v = 0;
}
else
{
float u = ( lpShape->pScreenPnt[i * 6 + j].x - lpShape->TearAirTexRect.left ) / TEARAIR_TEX_SIZE;
float v = ( lpShape->pScreenPnt[i * 6 + j].y - lpShape->TearAirTexRect.top ) / TEARAIR_TEX_SIZE;
lpShape->vb[i * 6 + j].u = u - u / 10;
lpShape->vb[i * 6 + j].v = v - v / 10;
}
}
}
lpShape->dwSegmentCur++;
if ( lpShape->dwSegmentCur == lpShape->dwSegment )
lpShape->dwSegmentCur = 0;
LPDIRECT3DSURFACE8 pBackBufferSurface;
LPDIRECT3DSURFACE8 pTextureSurface;
POINT point = { 0, 0 };
lpShape->pTearAirTex->GetSurfaceLevel( 0, &pTextureSurface );
g_D3DDevice->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO, &pBackBufferSurface );
g_D3DDevice->CopyRects( pBackBufferSurface, &lpShape->TearAirTexRect, 1, pTextureSurface, &point );
lpShape->LastTearAirTexRect.left = lpShape->TearAirTexRect.left;
lpShape->LastTearAirTexRect.right = lpShape->TearAirTexRect.right;
lpShape->LastTearAirTexRect.top = lpShape->TearAirTexRect.top;
lpShape->LastTearAirTexRect.bottom = lpShape->TearAirTexRect.bottom;
char strText[64];
sprintf( strText, "Capture the backbuffer, Width = %d, Height = %d!\n", lpShape->LastTearAirTexRect.right - lpShape->LastTearAirTexRect.left, lpShape->LastTearAirTexRect.bottom - lpShape->LastTearAirTexRect.top );
::OutputDebugString( strText );
g_D3DDevice->SetTexture( 0, lpShape->pTearAirTex );
g_D3DDevice->SetTransform ( D3DTS_WORLD, &mm );
if ( FAILED ( g_D3DDevice->SetVertexShader ( SHAPE_OUT_VERTEX ) ) )
return false;
if ( FAILED ( g_D3DDevice->DrawPrimitiveUP ( D3DPT_TRIANGLELIST,
lpShape->dwSegment * 2,
lpShape->vb,
sizeof ( ShapeOutVertex ) ) ) )
return false;
}
else
{
// Tear air require less segment
Shape_SetSegment( lpShape, 4 );
ZeroMemory ( lpShape->vb, sizeof ( ShapeOutVertex ) * lpShape->dwSegment * 6 );
lpShape->bFrist = false;
// Create the tearair texture only the user invoke the Shape_DrawAlpha()
if ( lpShape->pTearAirTex == NULL )
{
LPDIRECT3DSURFACE8 pBackBufferSurface;
D3DSURFACE_DESC BackBufferDesc;
if ( g_D3DDevice->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO, &pBackBufferSurface ) == D3D_OK )
{
pBackBufferSurface->GetDesc( &BackBufferDesc );
g_D3DDevice->CreateTexture( TEARAIR_TEX_SIZE, TEARAIR_TEX_SIZE, 1, 0, BackBufferDesc.Format, D3DPOOL_MANAGED, &lpShape->pTearAirTex );
::OutputDebugString( "Create the tear air texture!\n" );
}
}
// Create the screen point buffer only the user invoke the Shape_DrawAlpha()
if ( lpShape->pScreenPnt == NULL )
{
lpShape->pScreenPnt = new D3DXVECTOR3[lpShape->dwSegment * 6];
ZeroMemory( lpShape->pScreenPnt, sizeof(D3DXVECTOR3) * lpShape->dwSegment * 6 );
}
lpShape->TearAirTexRect.left = 1280;
lpShape->TearAirTexRect.top = 1280;
lpShape->TearAirTexRect.right = 0;
lpShape->TearAirTexRect.bottom = 0;
lpShape->LastTearAirTexRect.left = 0;
lpShape->LastTearAirTexRect.top = 0;
lpShape->LastTearAirTexRect.right = 0;
lpShape->LastTearAirTexRect.bottom = 0;
}
lpShape->last[0] = vec[0];
lpShape->last[1] = vec[1];
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -