|
bool Setup()
{
HRESULT hr = 0;
//
// Load the XFile data.
//
ID3DXBuffer* adjBuffer = 0;
ID3DXBuffer* mtrlBuffer = 0;
DWORD numMtrls = 0;
hr = D3DXLoadMeshFromX(
"bigship1.x",
D3DXMESH_MANAGED,
Device,
&adjBuffer,
&mtrlBuffer,
0,
&numMtrls,
&Mesh);
if(FAILED(hr))
{
::MessageBox(0, "D3DXLoadMeshFromX() - FAILED", 0, 0);
return false;
}
//
// Extract the materials, and load textures.
//
if( mtrlBuffer != 0 && numMtrls != 0 )
{
D3DXMATERIAL* mtrls = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer();
for(int i = 0; i < numMtrls; i++)
{
// the MatD3D property doesn't have an ambient value set
// when its loaded, so set it now:
mtrls.MatD3D.Ambient = mtrls.MatD3D.Diffuse;
// save the ith material
Mtrls.push_back( mtrls.MatD3D );
// check if the ith material has an associative texture
if( mtrls.pTextureFilename != 0 )
{
// yes, load the texture for the ith subset
IDirect3DTexture9* tex = 0;
D3DXCreateTextureFromFile(
Device,
mtrls.pTextureFilename,
&tex);
// save the loaded texture
Textures.push_back( tex );
}
else
{
// no texture for the ith subset
Textures.push_back( 0 );
}
}
}
d3d::Release<ID3DXBuffer*>(mtrlBuffer); // done w/ buffer
//
// Optimize the mesh.
//
hr = Mesh->OptimizeInplace(
D3DXMESHOPT_ATTRSORT |
D3DXMESHOPT_COMPACT |
D3DXMESHOPT_VERTEXCACHE,
(DWORD*)adjBuffer->GetBufferPointer(),
0, 0, 0);
d3d::Release<ID3DXBuffer*>(adjBuffer); // done w/ buffer
if(FAILED(hr))
{
::MessageBox(0, "OptimizeInplace() - FAILED", 0, 0);
return false;
}
//Set texture filters.
Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
//
// Set Lights.
//
D3DXVECTOR3 dir(1.0f, -0.0f, 0.25f);
D3DXCOLOR col(1.0f, 1.0f, 1.0f, 1.0f);
D3DLIGHT9 light = d3d::InitDirectionalLight(&dir, &col);
Device->SetLight(0, &light);
Device->LightEnable(0, true);
Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
Device->SetRenderState(D3DRS_SPECULARENABLE, true);
//
// Set camera.
////
D3DXVECTOR3 pos(5.0f, 3.0f, -10.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 5.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(
&V,
&pos,
&target,
&up);
Device->SetTransform(D3DTS_VIEW, &V);
//
// Set projection matrix.
//
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.45f, // 45 - degree
(float)Width / (float)Height,
1.0f,
1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);
return true;
}
void Cleanup()
{
d3d::Release<ID3DXMesh*>(Mesh);
for(int i = 0; i < Textures.size(); i++)
d3d::Release<IDirect3DTexture9*>( Textures );
}
bool Display(float timeDelta)
{
if( Device )
{
//
// Update: Update the camera.
//
if( ::GetAsyncKeyState('W') & 0x8000f )
TheCamera.walk(4.0f * timeDelta);
if( ::GetAsyncKeyState('S') & 0x8000f )
TheCamera.walk(-4.0f * timeDelta);
if( ::GetAsyncKeyState('A') & 0x8000f )
TheCamera.strafe(-4.0f * timeDelta);
if( ::GetAsyncKeyState('D') & 0x8000f )
TheCamera.strafe(4.0f * timeDelta);
if( ::GetAsyncKeyState('R') & 0x8000f )
TheCamera.fly(4.0f * timeDelta);
if( ::GetAsyncKeyState('F') & 0x8000f )
TheCamera.fly(-4.0f * timeDelta);
if( ::GetAsyncKeyState(VK_UP) & 0x8000f )
TheCamera.pitch(1.0f * timeDelta);
if( ::GetAsyncKeyState(VK_DOWN) & 0x8000f )
TheCamera.pitch(-1.0f * timeDelta);
if( ::GetAsyncKeyState(VK_LEFT) & 0x8000f )
TheCamera.yaw(-1.0f * timeDelta);
if( ::GetAsyncKeyState(VK_RIGHT) & 0x8000f )
TheCamera.yaw(1.0f * timeDelta);
if( ::GetAsyncKeyState('N') & 0x8000f )
TheCamera.roll(1.0f * timeDelta);
if( ::GetAsyncKeyState('M') & 0x8000f )
TheCamera.roll(-1.0f * timeDelta);
// Update the view matrix representing the cameras
// new position/orientation.
D3DXMATRIX V;
TheCamera.getViewMatrix(&V);
Device->SetTransform(D3DTS_VIEW, &V);
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
Device->BeginScene();
for(int i = 0; i < Mtrls.size(); i++)
{
Device->SetMaterial( &Mtrls );
Device->SetTexture(0, Textures);
Mesh->DrawSubset(i);
}
Device->EndScene();
Device-> resent(0, 0, 0, 0);
}
return true;
}
显示太近,不能全部看清模型。调了D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.45f, // 45 - degree
(float)Width / (float)Height,
1.0f,
1000.0f);也不能看清。
恳求高手指点!!!!由于初级用户无法上传图片,没有贴图 |
|