EDN Admin
Well-known member
Im novice to DirectX.
Im not sure which is the right category to post directX question. But I still hope I can get answers from this community.
This is my code to draw a triangle. But when I execute this, I could not see any triangle drawn where as only my background color is flushed.
Am i wrong with setting my matrice [ world,view and projection]?
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; void CChildView::OnPaint()
{
CPaintDC dc(<span style="color:Blue; this); <span style="color:Green; // device context for painting
InitDirect3D();
DrawTriangle();
<span style="color:Green; // TODO: Add your message handler code here
<span style="color:Green; // Do not call CWnd::OnPaint() for painting messages
}
<span style="color:Blue; int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
<span style="color:Blue; if (CWnd::OnCreate(lpCreateStruct) == -1)
<span style="color:Blue; return -1;
<span style="color:Green; // TODO: Add your specialized creation code here
<span style="color:Blue; return 0;
}
<span style="color:Blue; void CChildView::InitDirect3D()
{
<span style="color:Blue; if(m_bIsDirect3DInitialized)
<span style="color:Blue; return;
<span style="color:Green; //Create the directX object
m_piDirect3D9 = Direct3DCreate9(D3D_SDK_VERSION);
CRect rc;
GetClientRect(&rc);
<span style="color:Green; //D3D Present Parameters
D3DPRESENT_PARAMETERS d3dPresentParam;
ZeroMemory( &d3dPresentParam, <span style="color:Blue; sizeof(D3DPRESENT_PARAMETERS));
d3dPresentParam.BackBufferCount = 1;
d3dPresentParam.BackBufferHeight = rc.Height();
d3dPresentParam.BackBufferWidth = rc.Width();
d3dPresentParam.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dPresentParam.Windowed = TRUE;
d3dPresentParam.hDeviceWindow = <span style="color:Blue; this->GetSafeHwnd();
d3dPresentParam.SwapEffect = D3DSWAPEFFECT_DISCARD;
<span style="color:Green; //Create the DirectX device object
m_piDirect3D9->CreateDevice( D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,<span style="color:Blue; this->GetSafeHwnd(),
D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dPresentParam,&m_piDir3DDev9
);
m_bIsDirect3DInitialized = <span style="color:Blue; true;
CreateVertex();
LoadVertexBuffer();
SetMatrice();
}
<span style="color:Blue; void CChildView::CreateVertex()
{
m_triVertex[0].x = 150;
m_triVertex[0].y = 50;
m_triVertex[0].z = 0.5;
m_triVertex[0].color = D3DCOLOR_XRGB(255,0,0);
m_triVertex[1].x = 250;
m_triVertex[1].y = 250;
m_triVertex[1].z = 0.5;
m_triVertex[1].color = D3DCOLOR_XRGB(0,255,0);
m_triVertex[2].x = 50;
m_triVertex[2].y = 250;
m_triVertex[2].z = 0.5;
m_triVertex[2].color = D3DCOLOR_XRGB(0,0,255);
}
<span style="color:Blue; void CChildView::LoadVertexBuffer()
{
HRESULT hRes;
<span style="color:Green; //First Create the vertex buffer
hRes = m_piDir3DDev9->CreateVertexBuffer( <span style="color:Blue; sizeof(CustomVertex)*3 ,0,D3DCUSTFVF_FORMAT,D3DPOOL_DEFAULT,&m_piVertBuffer,NULL);
<span style="color:Green; //Get the memory from the vertex buffer. First lock and then get the memory
<span style="color:Blue; void* pBytes = NULL;
hRes = m_piVertBuffer->Lock(0,<span style="color:Blue; sizeof(m_triVertex),&pBytes,0);
<span style="color:Green; //Now load the created vertex in the memory obtained from the vertex buffer
memcpy(pBytes,m_triVertex,<span style="color:Blue; sizeof(CustomVertex)*3);
<span style="color:Green; //Now release/unlock the vertex buffer
hRes = m_piVertBuffer->Unlock();
}
<span style="color:Blue; void CChildView::SetMatrice()
{
<span style="color:Green; //Set the transformation matrix
D3DXMATRIX transMat;
D3DXMatrixIdentity(&transMat);
D3DXMatrixTranslation(&transMat,0.0f,0.0f,0.0f);
m_piDir3DDev9->SetTransform(D3DTS_WORLD,&transMat);
<span style="color:Green; //Build the view matrix by providing the eye position, position where eye looks and which is
<span style="color:Green; //the up direction
D3DXMATRIX viewMat;
D3DXVECTOR3 eyePos(0,0.0,-5.0);
D3DXVECTOR3 atVec(0.0,0.0,0.0);
D3DXVECTOR3 upDir(0.0,0.0,1.0);
D3DXMatrixLookAtLH(&viewMat,&eyePos,&atVec,&upDir);
m_piDir3DDev9->SetTransform(D3DTS_VIEW,&viewMat);
<span style="color:Green; //Set the projection matrix
CRect rc;
GetClientRect(&rc);
D3DXMATRIX projMatrix;
D3DXMatrixPerspectiveFovLH(&projMatrix,D3DX_PI/4, (rc.Width()/rc.Height()),1.0f,500.0f);
m_piDir3DDev9->SetTransform(D3DTS_PROJECTION,&projMatrix);
}
<span style="color:Blue; void CChildView::SetStream()
{
HRESULT hr = m_piDir3DDev9->SetStreamSource(0,m_piVertBuffer,0,<span style="color:Blue; sizeof(CustomVertex));
}
<span style="color:Blue; void CChildView:rawTriangle()
{
m_piDir3DDev9->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(40,80,40),1.0f,0);
m_piDir3DDev9->BeginScene();
SetStream();
m_piDir3DDev9->SetFVF(D3DCUSTFVF_FORMAT);
HRESULT hres = m_piDir3DDev9->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
m_piDir3DDev9->EndScene();
m_piDir3DDev9->Present(NULL,NULL,NULL,NULL);
}
[/code]
<br/>
View the full article
Im not sure which is the right category to post directX question. But I still hope I can get answers from this community.
This is my code to draw a triangle. But when I execute this, I could not see any triangle drawn where as only my background color is flushed.
Am i wrong with setting my matrice [ world,view and projection]?
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; void CChildView::OnPaint()
{
CPaintDC dc(<span style="color:Blue; this); <span style="color:Green; // device context for painting
InitDirect3D();
DrawTriangle();
<span style="color:Green; // TODO: Add your message handler code here
<span style="color:Green; // Do not call CWnd::OnPaint() for painting messages
}
<span style="color:Blue; int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
<span style="color:Blue; if (CWnd::OnCreate(lpCreateStruct) == -1)
<span style="color:Blue; return -1;
<span style="color:Green; // TODO: Add your specialized creation code here
<span style="color:Blue; return 0;
}
<span style="color:Blue; void CChildView::InitDirect3D()
{
<span style="color:Blue; if(m_bIsDirect3DInitialized)
<span style="color:Blue; return;
<span style="color:Green; //Create the directX object
m_piDirect3D9 = Direct3DCreate9(D3D_SDK_VERSION);
CRect rc;
GetClientRect(&rc);
<span style="color:Green; //D3D Present Parameters
D3DPRESENT_PARAMETERS d3dPresentParam;
ZeroMemory( &d3dPresentParam, <span style="color:Blue; sizeof(D3DPRESENT_PARAMETERS));
d3dPresentParam.BackBufferCount = 1;
d3dPresentParam.BackBufferHeight = rc.Height();
d3dPresentParam.BackBufferWidth = rc.Width();
d3dPresentParam.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dPresentParam.Windowed = TRUE;
d3dPresentParam.hDeviceWindow = <span style="color:Blue; this->GetSafeHwnd();
d3dPresentParam.SwapEffect = D3DSWAPEFFECT_DISCARD;
<span style="color:Green; //Create the DirectX device object
m_piDirect3D9->CreateDevice( D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,<span style="color:Blue; this->GetSafeHwnd(),
D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dPresentParam,&m_piDir3DDev9
);
m_bIsDirect3DInitialized = <span style="color:Blue; true;
CreateVertex();
LoadVertexBuffer();
SetMatrice();
}
<span style="color:Blue; void CChildView::CreateVertex()
{
m_triVertex[0].x = 150;
m_triVertex[0].y = 50;
m_triVertex[0].z = 0.5;
m_triVertex[0].color = D3DCOLOR_XRGB(255,0,0);
m_triVertex[1].x = 250;
m_triVertex[1].y = 250;
m_triVertex[1].z = 0.5;
m_triVertex[1].color = D3DCOLOR_XRGB(0,255,0);
m_triVertex[2].x = 50;
m_triVertex[2].y = 250;
m_triVertex[2].z = 0.5;
m_triVertex[2].color = D3DCOLOR_XRGB(0,0,255);
}
<span style="color:Blue; void CChildView::LoadVertexBuffer()
{
HRESULT hRes;
<span style="color:Green; //First Create the vertex buffer
hRes = m_piDir3DDev9->CreateVertexBuffer( <span style="color:Blue; sizeof(CustomVertex)*3 ,0,D3DCUSTFVF_FORMAT,D3DPOOL_DEFAULT,&m_piVertBuffer,NULL);
<span style="color:Green; //Get the memory from the vertex buffer. First lock and then get the memory
<span style="color:Blue; void* pBytes = NULL;
hRes = m_piVertBuffer->Lock(0,<span style="color:Blue; sizeof(m_triVertex),&pBytes,0);
<span style="color:Green; //Now load the created vertex in the memory obtained from the vertex buffer
memcpy(pBytes,m_triVertex,<span style="color:Blue; sizeof(CustomVertex)*3);
<span style="color:Green; //Now release/unlock the vertex buffer
hRes = m_piVertBuffer->Unlock();
}
<span style="color:Blue; void CChildView::SetMatrice()
{
<span style="color:Green; //Set the transformation matrix
D3DXMATRIX transMat;
D3DXMatrixIdentity(&transMat);
D3DXMatrixTranslation(&transMat,0.0f,0.0f,0.0f);
m_piDir3DDev9->SetTransform(D3DTS_WORLD,&transMat);
<span style="color:Green; //Build the view matrix by providing the eye position, position where eye looks and which is
<span style="color:Green; //the up direction
D3DXMATRIX viewMat;
D3DXVECTOR3 eyePos(0,0.0,-5.0);
D3DXVECTOR3 atVec(0.0,0.0,0.0);
D3DXVECTOR3 upDir(0.0,0.0,1.0);
D3DXMatrixLookAtLH(&viewMat,&eyePos,&atVec,&upDir);
m_piDir3DDev9->SetTransform(D3DTS_VIEW,&viewMat);
<span style="color:Green; //Set the projection matrix
CRect rc;
GetClientRect(&rc);
D3DXMATRIX projMatrix;
D3DXMatrixPerspectiveFovLH(&projMatrix,D3DX_PI/4, (rc.Width()/rc.Height()),1.0f,500.0f);
m_piDir3DDev9->SetTransform(D3DTS_PROJECTION,&projMatrix);
}
<span style="color:Blue; void CChildView::SetStream()
{
HRESULT hr = m_piDir3DDev9->SetStreamSource(0,m_piVertBuffer,0,<span style="color:Blue; sizeof(CustomVertex));
}
<span style="color:Blue; void CChildView:rawTriangle()
{
m_piDir3DDev9->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(40,80,40),1.0f,0);
m_piDir3DDev9->BeginScene();
SetStream();
m_piDir3DDev9->SetFVF(D3DCUSTFVF_FORMAT);
HRESULT hres = m_piDir3DDev9->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
m_piDir3DDev9->EndScene();
m_piDir3DDev9->Present(NULL,NULL,NULL,NULL);
}
[/code]
<br/>
View the full article