HRESULT InitD3D( void )
{
g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE,
&d3dpp, &g_pd3dDevice );
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_AMBIENT,
D3DCOLOR_COLORVALUE( 0.6f, 0.6f, 0.6f, 1.0 ) );
g_pd3dDevice->LightEnable( 0, TRUE);
D3DMATERIAL9 mtrl;
ZeroMemory( &mtrl, sizeof(mtrl) );
mtrl.Diffuse.r = mtrl.Ambient.r = 140.0f / 255.0f;
mtrl.Diffuse.g = mtrl.Ambient.g = 200.0f / 255.0f;
mtrl.Diffuse.b = mtrl.Ambient.b = 255.0f / 255.0f;
mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;
g_pd3dDevice->SetMaterial( &mtrl );
return S_OK;
}
void BeforePaint( void )
{
D3DLIGHT9 light;
ZeroMemory( &light, sizeof(light) );
light.Position = D3DXVECTOR3( 30.0f, 30.0f, 30.0f );
light.Attenuation1 = 0.05f;
light.Diffuse.r = 1.0f;
light.Diffuse.g = 1.0f;
light.Diffuse.b = 1.0f;
light.Range = 1000.0f;
light.Type = D3DLIGHT_POINT;
g_pd3dDevice->SetLight( 0, &light );
g_pd3dDevice->LightEnable( 0, TRUE);
}
HRESULT CreateObject( void )
{
D3DVECTOR SrcBox[] = {
{ 5.0f, 5.0f, 0.0f }, { 5.0f, 5.0f, 10.0f },
{ 5.0f, -5.0f, 0.0f }, { 5.0f, -5.0f, 10.0f },
{-5.0f, -5.0f, 0.0f }, {-5.0f, -5.0f, 10.0f },
{-5.0f, 5.0f, 0.0f }, {-5.0f, 5.0f, 10.0f },
};
WORD wIndex[] ={
0, 4, 6, 0, 2, 4,
0, 6, 7, 0, 7, 1,
0, 3, 2, 0, 1, 3,
5, 2, 3, 5, 4, 2,
5, 6, 4, 5, 7, 6,
5, 1, 7, 5, 3, 1,
};
CUSTOMVERTEX ExpandBox[sizeof(wIndex) / sizeof(WORD)];
for ( int i = 0; i < 36; i++ )
ExpandBox[i].pos = SrcBox[ wIndex[i] ];
for ( i = 0; i < 12; i++ )
{
D3DVECTOR Tri[3];
Tri[0] = ExpandBox[ i * 3 + 0 ].pos;
Tri[1] = ExpandBox[ i * 3 + 1 ].pos;
Tri[2] = ExpandBox[ i * 3 + 2 ].pos;
ExpandBox[ i * 3 + 0 ].normal.x = 0.0f;
ExpandBox[ i * 3 + 0 ].normal.y = 0.0f;
ExpandBox[ i * 3 + 0 ].normal.z = 1.0f;
CalcNormal( Tri, &(ExpandBox[ i * 3 + 0 ].normal) );
ExpandBox[ i * 3 + 1 ].normal = ExpandBox[ i * 3 + 0 ].normal;
ExpandBox[ i * 3 + 2 ].normal = ExpandBox[ i * 3 + 0 ].normal;
}
g_pd3dDevice->CreateVertexBuffer( sizeof(ExpandBox),
0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL );
VOID* pVertices;
g_pVB->Lock( 0, sizeof(ExpandBox), (void**)&pVertices, 0 );
MoveMemory( pVertices, ExpandBox, sizeof(ExpandBox) );
g_pVB->Unlock();
return S_OK;
}
void OnIdle( void )
{
static CTimer t;
static double dt = t.End();
double temp = t.End();
char szValue[256];
sprintf( szValue, "当前帧率:%f", 1 / ( temp - dt ) );
SetWindowText( g_hWnd, szValue );
dt = temp;
if ( g_pd3dDevice != NULL )
{
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,200), 1.0f, 0 );
if ( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
BeforePaint();
if ( FAILED( SetModalMatrix() ) )
{
return;
}
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 12 );
g_pd3dDevice->EndScene();
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
}
}
HRESULT SetModalMatrix( void )
{
static float fRadius = 0.5f;
fRadius -= 0.003f;
if ( fRadius < 0)
{
fRadius = D3DX_PI * 2 ;
}
D3DXMATRIX matWorld;
D3DXMatrixRotationZ( &matWorld, 0.0f );
if ( FAILED( g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ) ) )
{
return E_FAIL;
}
D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( cosf( fRadius ) * 40.0f, sinf( fRadius ) * 40.0f, 30.0 ),
&D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
&D3DXVECTOR3( 0.0f, 0.0f, 1.0f ) );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
return S_OK;
}
HRESULT SetProjMatrix( WORD wWidth, WORD wHeight )
{
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, (float)wWidth / (float)wHeight, 1.0f, 100.0f );
return g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
看看和OpenGL做出来的哪个效果更好一些呢?我想使用D3D来做这些事会很方便吧。这一篇文章拖了很久,国庆长假是一个主要原因,当然还有在单位的工作很忙之类的因素,总而言之现在只好在这里向大家说抱歉。不过我要在此感谢我们的大版主ckacka和steedhorse,是他们一直在默默的认真审核着一个又一个的FAQ,让我能够腾出空闲来写一点东东。另外要感谢李学峰,我们公司的OpenGL专家,顾问级人物,是他为我的上篇文章提供了强大有力的技术技持和保障,还有感谢我的小狗狗Anny,带给我无尽的欢乐;当然最需要感谢的还是大家,是你们的精神力量支持着我继续着写作。
Creamdog于2003年10月10日凌晨21点46分完成