D3D 绘制多个矩形

分类: C/C++ 发布时间: 2019-01-20 19:18

在D3D界面上绘制多个矩形

bool InitObject()
{
intnVertexNum = 6;
HRESULT hRe = NULL;
hRe = g_pDevice->CreateVertexBuffer(nVertexNum*sizeof(D3DVertex),0,
D3DFVF_VERTEX,D3DPOOL_MANAGED,&g_pVertex,NULL);
if(FAILED(hRe) )
{
MessageBox(NULL,TEXT(“Failed to create Vertex buffer”),TEXT(“ERROR”),MB_OK);
returnfalse;
}

D3DCOLOR dcol =D3DCOLOR_XRGB(255,255,255);
D3DVertex *Vertexs = NULL;
g_pVertex->Lock(0,0,(void**)&Vertexs,0);
Vertexs[0] =D3DVertex(320.0f,150.0f,0.5f,1.0f,D3DCOLOR_XRGB(255,255,0));
Vertexs[1] =D3DVertex(420.0f,350.0f,0.5f,1.0f,D3DCOLOR_XRGB(255,0,0));
Vertexs[2] =D3DVertex(220.0f,150.0f,0.5f,1.0f,D3DCOLOR_XRGB(0,255,0));
Vertexs[3] =D3DVertex(320.0f,200.0f,0.5f,1.0f,D3DCOLOR_XRGB(255,255,0));
Vertexs[4] =D3DVertex(420.0f,400.0f,0.5f,1.0f,D3DCOLOR_XRGB(255,0,0));
Vertexs[5] = D3DVertex(220.0f,200.0f,0.5f,1.0f,D3DCOLOR_XRGB(0,255,0));
g_pVertex->Unlock();
///———————————————————————
nVertexNum = 4;
hRe = g_pDevice->CreateVertexBuffer(nVertexNum*sizeof(D3DVertex),0,
D3DFVF_VERTEX,D3DPOOL_MANAGED,&g_pVertex2,NULL);
if(FAILED(hRe) )
{
MessageBox(NULL,TEXT(“Failed to create Vertex buffer”),TEXT(“ERROR”),MB_OK);
returnfalse;
}

D3DVertex *Vertexs2 = NULL;
g_pVertex2->Lock(0,0,(void**)&Vertexs2,0);
Vertexs2[0] =D3DVertex(440.0f,150.0f,0.5f,1.0f,D3DCOLOR_XRGB(255,255,0));
Vertexs2[1] =D3DVertex(540.0f,350.0f,0.5f,1.0f,D3DCOLOR_XRGB(255,0,0));
Vertexs2[2] =D3DVertex(340.0f,150.0f,0.5f,1.0f,D3DCOLOR_XRGB(0,255,0));
Vertexs2[3] = D3DVertex(600.0f,150.0f,0.5f,1.0f,D3DCOLOR_XRGB(0,255,0));
g_pVertex2->Unlock();

D3DXVECTOR3 vPosition(0.0f, 0.0f,-5.0f);
D3DXVECTOR3 vTarget(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vUp(0.0f, 1.0f, 0.0f);
D3DXMATRIX vView;
D3DXMatrixLookAtLH(&vView,&vPosition,&vTarget,&vUp);
g_pDevice->SetTransform(D3DTS_VIEW, &vView);

D3DXMATRIX mProj;
//D3DXMatrixOrthoLH(&vProj,(FLOAT)g_nWidth,(FLOAT)g_nHeight,1.0f,1000.0f);
D3DXMatrixPerspectiveFovLH(&mProj,D3DX_PI* 0.5f,
(float)g_nWidth/ (float)g_nHeight, 1.0f,1000.0f);
g_pDevice->SetTransform(D3DTS_PROJECTION,&mProj);
g_pDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);
g_pDevice->SetRenderState(D3DRS_LIGHTING,TRUE);
return true;
}

void RenderScene()
{
DWORD DDXCLEAR =D3DCLEAR_TARGET|D3DCLEAR_STENCIL|D3DCLEAR_ZBUFFER;
g_pDevice->Clear(0,NULL,DDXCLEAR,D3DCOLOR_XRGB(0,0,0),1.0f,0);
g_pDevice->BeginScene();
HRESULT hr;
hr =g_pDevice->SetStreamSource(0,g_pVertex,0,2*sizeof(D3DVertex));
//g_pDevice->SetStreamSource(0,g_pVertex2,3*sizeof(D3DVertex),sizeof(D3DVertex));
g_pDevice->SetFVF(D3DFVF_VERTEX);
g_pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);

hr =g_pDevice->SetStreamSource(0,g_pVertex2,sizeof(D3DVertex),sizeof(D3DVertex) );
g_pDevice->SetFVF(D3DFVF_VERTEX);
g_pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);

g_pDevice->EndScene();
g_pDevice->Present(NULL,NULL,NULL,NULL);
}

扫描下方二维码,关注业余草微信公众号,回复“FFmpeg”关键词,获取 FFmpeg 视频教程!

关注公众号获取视频教程

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!