DirectX c++ problem
i was working perfectly with direct3d under c# with no problem,and very quick, until i came accross switching to unmanaged c++.
so i started from scratch creating device and so forth.
the problem is, the device is created but it draws nothing, nomatter wat i do to it.
here r some sample code i did:
// Creating the device
if( NULL == ( m_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return;
// Set up the structure used to create the D3DDevice
ZeroMemory( &m_d3dpp, sizeof(m_d3dpp) );
m_d3dpp.Windowed = TRUE;
m_d3dpp.BackBufferWidth = m_iWidth;
m_d3dpp.BackBufferHeight = m_iHeight;
m_d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
m_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
m_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
m_d3dpp.EnableAutoDepthStencil = true;
// Create the D3DDevice
if( FAILED( m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&m_d3dpp, &m_pd3dDevice ) ) )
{
return;
}
//Begin Frame
DWORD state;
m_pd3dDevice->GetRenderState(D3DRS_ZENABLE, &state);
if( state )
{
m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_COLORVALUE(0.0f,0.0f,1.0f,1.0f), m_fZDepth, m_dwStencil );
}
else
{
m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_COLORVALUE(0.0f,0.0f,1.0f,1.0f), m_fZDepth, m_dwStencil );
}
m_pd3dDevice->BeginScene();
// EndFrame
m_pd3dDevice->EndScene();
m_pd3dDevice->Present( NULL, NULL, NULL, NULL );
// Creating vertex struct
struct VertexSimple
{
float x, y, z;
DWORD diffuse;
};
#define FVF_SIMPLE (D3DFVF_XYZ | D3DFVF_DIFFUSE)
// Initializing geometry
g_Device->CreateDevice();
g_Device->m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, false);
VertexSimple *vert = new VertexSimple[3];
vert[0].x = 50;
vert[0].y = 50;
vert[0].z = 0;
vert[0].diffuse = D3DCOLOR_XRGB(10, 0, 0);
vert[1].x = 150;
vert[1].y = 00;
vert[1].z = 0;
vert[1].diffuse = D3DCOLOR_XRGB(10, 0, 0);
vert[2].x = 0;
vert[2].y = 0;
vert[2].z = 0;
vert[2].diffuse = D3DCOLOR_XRGB(10, 0, 0);
g_Device->m_pd3dDevice->CreateVertexBuffer( 3*sizeof(VertexSimple),
D3DUSAGE_WRITEONLY,
FVF_SIMPLE,
D3DPOOL_DEFAULT,
&VertexBuffer,
NULL );
void *pVertices = NULL;
VertexBuffer->Lock( 0, sizeof( 3*sizeof(VertexSimple) ), (void**)&pVertices, 0 );
memcpy( pVertices, vert, sizeof(3*sizeof(VertexSimple) ) );
VertexBuffer->Unlock();
// My camera is initialized as follows:
m_pd3dDevice = dev.m_pd3dDevice;
D3DXMATRIXA16 Projection;
D3DXMatrixPerspectiveFovLH(&Projection, 45.0f, 4/3, 1, 10000);
m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &Projection);
// Look function called while rendering
D3DXMATRIXA16 mView;
D3DXMatrixLookAtLH(&mView, &Pos, &(View + Pos), &Up);
m_pd3dDevice->SetTransform(D3DTS_VIEW, &mView);
// in main, i initalize the position view and up o fthe camera as follows:
pos.x = 50;
pos.y = 0;
pos.z = -10;
view.x = 0;
view.y = 0;
view.z = 1;
up.x = 0;
up.y = 1;
up.z = 0;
//In my render loop i draw the three verts as follows:
D3DXMATRIX world;
D3DXMatrixIdentity(&world);
g_Device->m_pd3dDevice->SetTransform(D3DTS_WORLD, &world);
g_Device->m_pd3dDevice->SetFVF( FVF_SIMPLE );
g_Device->m_pd3dDevice->SetStreamSource( 0, VertexBuffer, 0, sizeof(VertexSimple) );
g_Device->m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
where is my problem? i setup the camera correct, and looks in right direction, and setup the vertices correctly.
why does this same piece of code works under Managed direct3D (c#) and not under unmaned c++ Direct3D?
very weird, anybody has a clue?
Just for future reference, you can make your posts look a lot more appealing by using the [ source ] - [ / source ] tags around your code fragment(s) [smile]
Anyway...
As a general rule of thumb, native/unmanaged code tends to be a little less forgiving/helpful than the managed equivalent.
You haven't got any particular information on whether anything is failing - you're not checking any return codes that I can see.
I suggest you follow the steps found here about how to debug a Direct3D application. If it's a "proper" error, the aforementioned articles methods will tell you exactly what it is [smile]
hth
Jack
Anyway...
As a general rule of thumb, native/unmanaged code tends to be a little less forgiving/helpful than the managed equivalent.
You haven't got any particular information on whether anything is failing - you're not checking any return codes that I can see.
I suggest you follow the steps found here about how to debug a Direct3D application. If it's a "proper" error, the aforementioned articles methods will tell you exactly what it is [smile]
hth
Jack
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement