MFC程序集成Direct3D的有关问题

MFC程序集成Direct3D的问题
大家好,我实现了一个类AView,想把Direct3D集成到MFC程序里,实现如下,然后在MyView: public CView, AView里的OnDraw()调render()函数,在OnInitialUpdate里调create和fillVB,结果只有背景被刷成想要的白色,但三角形却没有出来。请大家帮我看一下,问题出在哪里呢?谢谢。 


bool AView::create(HWND hwnd, int width, int height, bool windowed)
{
// Create the D3D object.
if( NULL == ( m_pD3d = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;

// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

// Create the D3DDevice
if( FAILED( m_pD3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &m_pDevice ) ) )
{
return E_FAIL;
}

// Device state would normally be set here

return true;
}

bool AView::fillVB()
{

// Initialize three vertices for rendering a triangle
CUSTOMVERTEX vertices[] =
{
{ 150.0f, 50.0f, -0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
{ 250.0f, 250.0f, -0.5f, 1.0f, 0xff00ff00, },
{ 50.0f, 250.0f, -0.5f, 1.0f, 0xff00ffff, },
};

// Create the vertex buffer. Here we are allocating enough memory
// (from the default pool) to hold all our 3 custom vertices. We also
// specify the FVF, so the vertex buffer knows what data it contains.
if( FAILED( m_pDevice->CreateVertexBuffer( 3 * sizeof( CUSTOMVERTEX ),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &m_pVB, NULL ) ) )
{
return E_FAIL;
}

// Now we fill the vertex buffer. To do this, we need to Lock() the VB to
// gain access to the vertices. This mechanism is required becuase vertex
// buffers may be in device memory.
VOID* pVertices;
if( FAILED( m_pVB->Lock( 0, sizeof( vertices ), ( void** )&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, vertices, sizeof( vertices ) );
m_pVB->Unlock();

return true;
}

bool AView::clean()
{
if( m_pDevice != NULL )
m_pDevice->Release();

if( m_pD3d != NULL )
m_pD3d->Release();

if (NULL != m_pVB)
m_pVB->Release();

return true;
}
bool AView::render()
{
if( NULL == m_pDevice )
return false;

// Clear the backbuffer to a white color
m_pDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 255, 255, 255 ), 1.0f, 0 );

// Begin the scene
if( SUCCEEDED( m_pDevice->BeginScene() ) )
{
// Rendering of scene objects can happen here
m_pDevice->SetStreamSource( 0, m_pVB, 0, sizeof( CUSTOMVERTEX ) );
m_pDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
m_pDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
// End the scene
m_pDevice->EndScene();
}

// Present the backbuffer contents to the display
m_pDevice->Present( NULL, NULL, NULL, NULL );

return true;
}

------解决方案--------------------
View::OnEraseBkgnd()函数直接返回FALSE,不然的话,D3D的渲染会被覆盖掉。
------解决方案--------------------
然后Render最好是写在重载的CWinApp::OnIdle()里面。

一篇英文的参考文章:
Using Direct3D with MFC
http://www.cs.wisc.edu/~tlabonne/d3dmfc.html
------解决方案--------------------
AView::OnEraseBkgnd 直接return TRUE
------解决方案--------------------
探讨
多谢了。

Silenker,我也是参考这个文章来做的。不过没有打算animation.所以也没有调用OnEraseBkgnd。
问题出在我定义的CUSTOMVERTEX,我本来是double类型的,改成float类型的就好了。真奇怪。。。其他啥都没改。