DirectX画圆及圆角矩形的简单实现[zz]
(2012-12-10 09:40:05)
标签:
杂谈 |
DirectX画圆及圆角矩形的简单实现
[csharp] view plaincopy
-
SCREEN_VERTEX_UNTEX -
-
{ -
- float
x, y, z, h; -
-
D3DCOLOR
color; -
-
-
- static
DWORD FVF; -
-
}; -
-
SCREEN_VERTEX_UNTEX::FVF = D3DFVF_XYZRHW | D3DFVF_DIFFUSE
[csharp] view plaincopy
- void
DrawCircle( intIDirect3DDevice9* pd3dDevice, xCenter, intyCenter, intnRadius, D3DCOLOR FrameColor) -
-
{
-
-
SCREEN_VERTEX_UNTEX *pVertices = new SCREEN_VERTEX_UNTEX[2 * D3DX_PI * nRadius]; -
-
//Bresenham algorithm -
-
int x=0, y=nRadius, d=1-nRadius, i=0; -
-
while(x <= y) -
-
{ -
-
//get eight points -
-
//(x,y) -
-
pVertices[i].x = x + xCenter; -
-
pVertices[i].y = y + yCenter; -
-
pVertices[i].z = 0.5f; -
-
pVertices[i].h = 1.0f; -
-
pVertices[i].color = FrameColor; -
-
//(x,-y) -
-
++i; -
-
-
-
pVertices[i].x = x + xCenter; -
-
pVertices[i].y = -y + yCenter; -
-
pVertices[i].z = 0.5f; -
-
pVertices[i].h = 1.0f; -
-
pVertices[i].color = FrameColor; -
-
//(-x, y) -
-
++i; -
-
pVertices[i].x = -x + xCenter; -
-
pVertices[i].y = y + yCenter; -
-
pVertices[i].z = 0.5f; -
-
pVertices[i].h = 1.0f; -
-
pVertices[i].color = FrameColor; -
-
//(-x, -y) -
-
++i; -
-
pVertices[i].x = -x + xCenter; -
-
pVertices[i].y = -y + yCenter; -
-
pVertices[i].z = 0.5f; -
-
pVertices[i].h = 1.0f; -
-
pVertices[i].color = FrameColor; -
-
//(y, x) -
-
++i; -
-
pVertices[i].x = y + xCenter; -
-
pVertices[i].y = x + yCenter; -
-
pVertices[i].z = 0.5f; -
-
pVertices[i].h = 1.0f; -
-
pVertices[i].color = FrameColor; -
-
//(-y, x) -
-
++i; -
-
pVertices[i].x = -y + xCenter; -
-
pVertices[i].y = x + yCenter; -
-
pVertices[i].z = 0.5f; -
-
pVertices[i].h = 1.0f; -
-
pVertices[i].color = FrameColor; -
-
//(y, -x) -
-
++i; -
-
pVertices[i].x = y + xCenter; -
-
pVertices[i].y = -x + yCenter; -
-
pVertices[i].z = 0.5f; -
-
pVertices[i].h = 1.0f; -
-
pVertices[i].color = FrameColor; -
-
//(-y,-x) -
-
++i; -
-
pVertices[i].x = -y + xCenter; -
-
pVertices[i].y = -x + yCenter; -
-
pVertices[i].z = 0.5f; -
-
pVertices[i].h = 1.0f; -
-
pVertices[i].color = FrameColor; -
-
++i; -
-
-
-
if(d>0) -
-
{ -
-
d+=2*(x-y)+5; -
-
--y; -
-
} -
-
else -
-
{ -
-
d+=2*x+3; -
-
} -
-
++x; -
-
} -
-
-
-
pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE ); -
-
pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ); -
-
pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ); -
-
pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, FALSE ); -
-
pd3dDevice->SetRenderState( D3DRS_SEPARATEALPHABLENDENABLE , FALSE ); -
-
pd3dDevice->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD ); -
-
pd3dDevice->SetRenderState( D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA|D3DCOLORWRITEENABLE_BLUE|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_RED ); -
-
pd3dDevice->SetRenderState( D3DRS_SHADEMODE, D3DSHADE_GOURAUD ); -
-
pd3dDevice->SetRenderState( D3DRS_FOGENABLE, FALSE ); -
-
pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, FALSE ); -
-
pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE); -
-
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG2 ); -
-
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); -
-
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ); -
-
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE ); -
-
pd3dDevice->SetFVF(SCREEN_VERTEX_UNTEX::FVF); -
-
pd3dDevice->DrawPrimitiveUP(D3DPT_POINTLIST, i, pVertices, sizeof(SCREEN_VERTEX_UNTEX)); -
-
delete [] pVertices; -
-
}
[csharp] view plaincopy
- void
DrawRoundRect( intIDirect3DDevice9 * pd3dDevice, nLeftRect, intnTopRect, intnRightRect, intnBottomRect, intnRadius, D3DCOLOR FrameColor ) -
-
{
-
-
SCREEN_VERTEX_UNTEX *pVertices = new SCREEN_VERTEX_UNTEX[2 * D3DX_PI * nRadius]; -
-
-
-
//Bresenham algorithm -
-
int x=0, y=nRadius, d=1-nRadius, i=0; -
-
while(x <= y) -
-
{ -
-
//get eight points -
-
//right bottom -
-
//(x,y) -
-
pVertices[i].x = x + nRightRect - nRadius; -
-
pVertices[i].y = y + nBottomRect - nRadius; -
-
pVertices[i].z = 0.5f; -
-
pVertices[i].h = 1.0f; -
-
pVertices[i].color = FrameColor; -
-
//(y, x) -
-
++i; -
-
pVertices[i].x = y + nRightRect - nRadius; -
-
pVertices[i].y = x + nBottomRect - nRadius; -
-
pVertices[i].z = 0.5f; -
-
pVertices[i].h = 1.0f; -
-
pVertices[i].color = FrameColor; -
-
-
-
//right top -
-
//(x,-y) -
-
++i; -
-
pVertices[i].x = x + nRightRect - nRadius; -
-
pVertices[i].y = -y + nTopRect + nRadius; -
-
pVertices[i].z = 0.5f; -
-
pVertices[i].h = 1.0f; -
-
pVertices[i].color = FrameColor; -
-
//(y, -x) -
-
++i; -
-
pVertices[i].x = y + nRightRect - nRadius; -
-
pVertices[i].y = -x + nTopRect + nRadius; -
-
pVertices[i].z = 0.5f; -
-
pVertices[i].h = 1.0f; -
-
pVertices[i].color = FrameColor; -
-
-
-
//left bottom -
-
//(-x, y) -
-
++i; -
-
pVertices[i].x = -x + nLeftRect + nRadius; -
-
pVertices[i].y = y + nBottomRect - nRadius; -
-
pVertices[i].z = 0.5f; -
-
pVertices[i].h = 1.0f; -
-
pVertices[i].color = FrameColor; -
-
-
-
//(-y, x) -
-
++i; -
-
pVertices[i].x = -y + nLeftRect + nRadius; -
-
pVertices[i].y = x + nBottomRect - nRadius; -
-
pVertices[i].z = 0.5f; -
-
pVertices[i].h = 1.0f; -
-
pVertices[i].color = FrameColor; -
-
-
-
//left top -
-
//(-x, -y) -
-
++i; -
-
pVertices[i].x = -x + nLeftRect + nRadius; -
-
pVertices[i].y = -y + nTopRect + nRadius; -
-
pVertices[i].z = 0.5f; -
-
pVertices[i].h = 1.0f; -
-
pVertices[i].color = FrameColor; -
-
//(-y,-x) -
-
++i; -
-
pVertices[i].x = -y + nLeftRect + nRadius; -
-
pVertices[i].y = -x + nTopRect + nRadius; -
-
pVertices[i].z = 0.5f; -
-
pVertices[i].h = 1.0f; -
-
pVertices[i].color = FrameColor; -
-
++i; -
-
-
-
if(d>0) -
-
{ -
-
d+=2*(x-y)+5; -
-
--y; -
-
} -
-
else -
-
{ -
-
d+=2*x+3; -
-
} -
-
++x; -
-
} -
-
-
-
static DXUT_SCREEN_VERTEX_UNTEX lineVertices[8] = {0}; -
-
//top line -
-
lineVertices[0].x = nLeftRect + nRadius; -
-
lineVertices[0].y = nTopRect; -
-
lineVertices[0].z = 0.5f; -
-
lineVertices[0].h = 1.0f; -
-
lineVertices[0].color = FrameColor; -
-
lineVertices[1].x = nRightRect - nRadius; -
-
lineVertices[1].y = nTopRect; -
-
lineVertices[1].z = 0.5f; -
-
lineVertices[1].h = 1.0f; -
-
lineVertices[1].color = FrameColor; -
-
-
-
//right line -
-
lineVertices[2].x = nRightRect; -
-
lineVertices[2].y = nTopRect + nRadius; -
-
lineVertices[2].z = 0.5f; -
-
lineVertices[2].h = 1.0f; -
-
lineVertices[2].color = FrameColor; -
-
lineVertices[3].x = nRightRect; -
-
lineVertices[3].y = nBottomRect - nRadius; -
-
lineVertices[3].z = 0.5f; -
-
lineVertices[3].h = 1.0f; -
-
lineVertices[3].color = FrameColor; -
-
//bottom line -
-
lineVertices[4].x = nRightRect - nRadius; -
-
lineVertices[4].y = nBottomRect; -
-
lineVertices[4].z = 0.5f; -
-
lineVertices[4].h = 1.0f; -
-
lineVertices[4].color = FrameColor; -
-
lineVertices[5].x = nLeftRect + nRadius; -
-
lineVertices[5].y = nBottomRect; -
-
lineVertices[5].z = 0.5f; -
-
lineVertices[5].h = 1.0f; -
-
lineVertices[5].color = FrameColor; -
-
//left line -
-
lineVertices[6].x = nLeftRect; -
-
lineVertices[6].y = nBottomRect - nRadius; -
-
lineVertices[6].z = 0.5f; -
-
lineVertices[6].h = 1.0f; -
-
lineVertices[6].color = FrameColor; -
-
lineVertices[7].x = nLeftRect; -
-
lineVertices[7].y = nTopRect + nRadius; -
-
lineVertices[7].z = 0.5f; -
-
lineVertices[7].h = 1.0f; -
-
lineVertices[7].color = FrameColor; -
-
-
-
-
-
pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE ); -
-
pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ); -
-
pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ); -
-
pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, FALSE ); -
-
pd3dDevice->SetRenderState( D3DRS_SEPARATEALPHABLENDENABLE , FALSE ); -
-
pd3dDevice->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD ); -
-
pd3dDevice->SetRenderState( D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA|D3DCOLORWRITEENABLE_BLUE|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_RED ); -
-
pd3dDevice->SetRenderState( D3DRS_SHADEMODE, D3DSHADE_GOURAUD ); -
-
pd3dDevice->SetRenderState( D3DRS_FOGENABLE, FALSE ); -
-
pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, FALSE ); -
-
pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE); -
-
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG2 ); -
-
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); -
-
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ); -
-
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE ); -
-
pd3dDevice->SetFVF(DXUT_SCREEN_VERTEX_UNTEX::FVF); -
-
pd3dDevice->DrawPrimitiveUP(D3DPT_POINTLIST, i, pVertices, sizeof(SCREEN_VERTEX_UNTEX)); -
-
pd3dDevice->DrawPrimitiveUP(D3DPT_LINELIST, 4, lineVertices, sizeof(SCREEN_VERTEX_UNTEX)); -
-
delete [] pVertices; -
-
}