xfile.cpp
资源名称:xfile.zip [点击查看]
上传用户:zhwbfz
上传日期:2014-10-29
资源大小:8269k
文件大小:10k
源码类别:
DirextX编程
开发平台:
Visual C++
- //////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // File: xfile.cpp
- //
- // Author: Frank Luna (C) All Rights Reserved
- //
- // System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0
- //
- // Desc: Demonstrates how to load and render an XFile.
- //
- //////////////////////////////////////////////////////////////////////////////////////////////////
- #include "d3dUtility.h"
- #include <vector>
- #include <math.h>
- //
- // Globals
- //
- IDirect3DDevice9* Device = 0;
- const int Width = 640;
- const int Height = 480;
- ID3DXMesh * house1 = 0,* house2=0,* people1=0;
- std::vector<D3DMATERIAL9> Mtrls(0),Mtrls1(0),Mtrls2(0);
- std::vector<IDirect3DTexture9*> Textures(0),Textures1(0),Textures2(0);
- D3DXMATRIX V;
- D3DXVECTOR3 pos(-16.0f, -30.0f, -80.0f);
- D3DXVECTOR3 target(-0.0f, -30.0f, -120.0f);
- D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
- float startangle=atan2(6,40);
- // Framework functions
- //
- bool Setup()
- {
- HRESULT hr = 0;
- //target.x=pos.x+cos(startangle/D3DX_PI);
- //target.z=pos.z+sin(startangle/D3DX_PI);
- //
- // Load the XFile data.
- //
- ID3DXBuffer* adjBuffer = 0,*adjBuffer1=0,* adjBuffer2 = 0;
- ID3DXBuffer* mtrlBuffer = 0,*mtrlBuffer1=0,* mtrlBuffer2 = 0;
- DWORD numMtrls = 0,numMtrls1=0,numMtrls2=0;
- hr = D3DXLoadMeshFromX(
- "test.X",
- D3DXMESH_MANAGED,
- Device,
- &adjBuffer,
- &mtrlBuffer,
- 0,
- &numMtrls,
- &house1);
- D3DXLoadMeshFromX(
- "仙剑道院.X",
- D3DXMESH_MANAGED,
- Device,
- &adjBuffer1,
- &mtrlBuffer1,
- 0,
- &numMtrls1,
- &house2);
- D3DXLoadMeshFromX(
- "aaa.X",
- D3DXMESH_MANAGED,
- Device,
- &adjBuffer2,
- &mtrlBuffer2,
- 0,
- &numMtrls2,
- &people1);
- 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[i].MatD3D.Ambient = D3DXCOLOR(1.8F,1.8F,1.8F,1.0F);
- //mtrls[i].MatD3D.Diffuse.a=0.5f;
- // save the ith material
- Mtrls.push_back( mtrls[i].MatD3D );
- // check if the ith material has an associative texture
- if( mtrls[i].pTextureFilename != 0 )
- {
- // yes, load the texture for the ith subset
- IDirect3DTexture9* tex = 0;
- D3DXCreateTextureFromFile(
- Device,
- mtrls[i].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
- if( mtrlBuffer1 != 0 && numMtrls1 != 0 )
- {
- D3DXMATERIAL* mtrls1 = (D3DXMATERIAL*)mtrlBuffer1->GetBufferPointer();
- for(int i = 0; i < numMtrls1; i++)
- {
- // the MatD3D property doesn't have an ambient value set
- // when its loaded, so set it now:
- mtrls1[i].MatD3D.Ambient = D3DXCOLOR(1.8F,1.8F,1.8F,1.0F);
- //mtrls[i].MatD3D.Diffuse.a=0.5f;
- // save the ith material
- Mtrls1.push_back( mtrls1[i].MatD3D );
- // check if the ith material has an associative texture
- if( mtrls1[i].pTextureFilename != 0 )
- {
- // yes, load the texture for the ith subset
- IDirect3DTexture9* tex = 0;
- D3DXCreateTextureFromFile(
- Device,
- mtrls1[i].pTextureFilename,
- &tex);
- // save the loaded texture
- Textures1.push_back( tex );
- }
- else
- {
- // no texture for the ith subset
- Textures1.push_back( 0 );
- }
- }
- }
- d3d::Release<ID3DXBuffer*>(mtrlBuffer1); // done w/ buffer
- if( mtrlBuffer2 != 0 && numMtrls2 != 0 )
- {
- D3DXMATERIAL* mtrls2 = (D3DXMATERIAL*)mtrlBuffer2->GetBufferPointer();
- for(int i = 0; i < numMtrls2; i++)
- {
- // the MatD3D property doesn't have an ambient value set
- // when its loaded, so set it now:
- mtrls2[i].MatD3D.Ambient = D3DXCOLOR(1.8F,1.8F,1.8F,1.0F);
- //mtrls[i].MatD3D.Diffuse.a=0.5f;
- // save the ith material
- Mtrls2.push_back( mtrls2[i].MatD3D );
- // check if the ith material has an associative texture
- if( mtrls2[i].pTextureFilename != 0 )
- {
- // yes, load the texture for the ith subset
- IDirect3DTexture9* tex = 0;
- D3DXCreateTextureFromFile(
- Device,
- mtrls2[i].pTextureFilename,
- &tex);
- // save the loaded texture
- Textures2.push_back( tex );
- }
- else
- {
- // no texture for the ith subset
- Textures2.push_back( 0 );
- }
- }
- }
- d3d::Release<ID3DXBuffer*>(mtrlBuffer2); // done w/ buffer
- //
- // Optimize the mesh.
- //
- hr = house1->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, -1.0f, 1.0f);
- 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.
- //
- D3DXMatrixLookAtLH(
- &V,
- &pos,
- &target,
- &up);
- Device->SetTransform(D3DTS_VIEW, &V);
- //
- // Set projection matrix.
- //
- D3DXMATRIX proj;
- D3DXMatrixPerspectiveFovLH(
- &proj,
- D3DX_PI * 0.5f, // 90 - degree
- (float)Width / (float)Height,
- 1.0f,
- 1000.0f);
- Device->SetTransform(D3DTS_PROJECTION, &proj);
- /*Device->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
- Device->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);
- Device->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG1);
- Device->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
- Device->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);*/
- Device->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
- Device->SetRenderState(D3DRS_ALPHATESTENABLE,true);
- Device->SetRenderState(D3DRS_ALPHAFUNC,D3DCMP_GREATER); Device->SetRenderState(D3DRS_ALPHAREF,100);
- return true;
- }
- void Cleanup()
- {
- d3d::Release<ID3DXMesh*>(house1);
- for(int i = 0; i < Textures.size(); i++)
- d3d::Release<IDirect3DTexture9*>( Textures[i] );
- }
- bool Display(float timeDelta)
- {
- if( Device )
- {
- //
- // Update: Rotate the mesh.
- //
- static float angle = startangle;
- //target.x=pos.x;target.z=pos.z;
- bool b_key=false;
- if(::GetAsyncKeyState(VK_LEFT)&0x8000f){angle+=0.3f;b_key=true;}
- if(::GetAsyncKeyState(VK_RIGHT)&0X800f){angle-=0.3f;b_key=true;}
- if(::GetAsyncKeyState(VK_UP)&0X8000F){
- pos.x+=0.08*cos(angle/180.0f*D3DX_PI);
- pos.z+=0.08*sin(angle/180.0f*D3DX_PI);b_key=true;
- }
- if(::GetAsyncKeyState(VK_DOWN)&0X8000F)
- {
- pos.x-=0.08*cos(angle/180.0f*D3DX_PI);
- pos.z-=0.08*sin(angle/180.0f*D3DX_PI);b_key=true;
- }
- if(b_key){
- target.x=pos.x+0.005*cos(angle/180.0f*D3DX_PI);
- target.z=pos.z+0.005*sin(angle/180.0f*D3DX_PI);
- D3DXMatrixLookAtLH(&V,&pos,&target,&up);
- Device->SetTransform(D3DTS_VIEW,&V);
- }
- if( angle >= 360.0f||angle<=-360.0f)
- angle = 0.0f;
- //
- // Render
- //
- Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x608000, 1.0f, 0);
- Device->BeginScene();
- D3DXMATRIX housemat;
- D3DXMATRIX roty;
- D3DXMatrixRotationY(&roty,D3DX_PI);
- D3DXMatrixIdentity(&housemat);
- Device->SetTransform(D3DTS_WORLD,&(roty*housemat));
- for(int i = 0; i < Mtrls.size(); i++)
- {
- Device->SetMaterial( &Mtrls[i] );
- Device->SetTexture(0, Textures[i]);
- house1->DrawSubset(i);
- }
- D3DXMatrixTranslation(&housemat,0.0f,-46.0f,-150.0f);
- Device->SetTransform(D3DTS_WORLD,&(roty*housemat));
- for( i = 0; i < Mtrls1.size(); i++)
- {
- Device->SetMaterial( &Mtrls1[i] );
- Device->SetTexture(0, Textures1[i]);
- house2->DrawSubset(i);
- }
- D3DXMatrixRotationY(&roty,D3DX_PI*0.5f);
- D3DXMatrixTranslation(&housemat,240.0f,-46.0f,-50.0f);
- D3DXMATRIX scal;
- D3DXMatrixScaling(&scal,0,2,1.5);
- Device->SetTransform(D3DTS_WORLD,&(roty*housemat));
- for( i = 0; i < Mtrls1.size(); i++)
- {
- Device->SetMaterial( &Mtrls1[i] );
- Device->SetTexture(0, Textures1[i]);
- house2->DrawSubset(i);
- }
- D3DXMatrixRotationY(&roty,D3DX_PI*0.5f);
- D3DXMatrixTranslation(&housemat,80.0f,-46.0f,-100.0f);
- Device->SetTransform(D3DTS_WORLD,&(roty*housemat));
- for( i = 0; i < Mtrls1.size(); i++)
- {
- Device->SetMaterial( &Mtrls1[i] );
- Device->SetTexture(0, Textures1[i]);
- house2->DrawSubset(i);
- }
- static float autorot=0.0f;
- D3DXMatrixRotationY(&roty,autorot);
- autorot+=0.023f;
- if(autorot>=6.28f)autorot=0.0f;
- D3DXMatrixTranslation(&housemat,-10.0f, -40.0f, -110.0f);
- D3DXMatrixScaling(&scal,0.2f,0.2f,0.2f);
- Device->SetTransform(D3DTS_WORLD,&(scal*roty*housemat));
- for( i = 0; i < Mtrls2.size(); i++)
- {
- Device->SetMaterial( &Mtrls2[i] );
- Device->SetTexture(0, Textures2[i]);
- people1->DrawSubset(i);
- }
- Device->EndScene();
- Device->Present(0, 0, 0, 0);
- }
- return true;
- }
- //
- // WndProc
- //
- LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch( msg )
- {
- case WM_DESTROY:
- ::PostQuitMessage(0);
- break;
- case WM_KEYDOWN:
- if( wParam == VK_ESCAPE )
- ::DestroyWindow(hwnd);
- break;
- }
- return ::DefWindowProc(hwnd, msg, wParam, lParam);
- }
- //
- // WinMain
- //
- int WINAPI WinMain(HINSTANCE hinstance,
- HINSTANCE prevInstance,
- PSTR cmdLine,
- int showCmd)
- {
- if(!d3d::InitD3D(hinstance,
- 1024, 768, true, D3DDEVTYPE_HAL, &Device))
- {
- ::MessageBox(0, "InitD3D() - FAILED", 0, 0);
- return 0;
- }
- if(!Setup())
- {
- ::MessageBox(0, "Setup() - FAILED", 0, 0);
- return 0;
- }
- d3d::EnterMsgLoop( Display );
- Cleanup();
- Device->Release();
- return 0;
- }