//****************************************************************************
//说明:
//开发环境:Visual C++.NET 7.1.3091 DirectX 9.0C
//操作系统:WindowsXP SP2
//硬 件:AMD Athlon XP 2500+,DDR400 256MB,Geforce4 MX440 AGP 8X 64MB
//****************************************************************************
#include "Direct3D.h"
CDirect3D::CDirect3D()
{
}
CDirect3D::~CDirect3D()
{
}
bool CDirect3D::InitD3D()
{
if(NULL==(D3d9=Direct3DCreate9(D3D_SDK_VERSION)))
{
::MessageBox(0, "Direct3DCreate9() - FAILED", 0, 0);
return FALSE;
}
//取得当前显示设备的显示模式
D3DDISPLAYMODE d3ddm;
if(D3D_OK!=(D3d9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm)))
{
::MessageBox(0,"GetAdapterDisplayMode() - FAILED",0,0);
return FALSE;
}
D3DPRESENT_PARAMETERS D3dpp;
//设置D3D设备的属性
ZeroMemory(&D3dpp, sizeof(D3dpp));
D3dpp.Windowed=false;//全屏
D3dpp.hDeviceWindow=hWnd;//窗口设备的句柄
D3dpp.SwapEffect=D3DSWAPEFFECT_FLIP;// 设置交换模式
D3dpp.BackBufferCount=1; //有一个后台缓存
D3dpp.BackBufferWidth=d3ddm.Width;//屏幕宽度
D3dpp.BackBufferHeight=d3ddm.Height;//屏幕长度
//立即显示刷新后的图像
D3dpp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE;
D3dpp.BackBufferFormat=D3DFMT_A8R8G8B8;//色彩深度
//开启自动深度缓冲
D3dpp.EnableAutoDepthStencil=true;
D3dpp.AutoDepthStencilFormat=D3DFMT_D24S8;//24位深度缓冲并保留8位模版缓冲(stencil buffer)
//创建D3D设备
if(D3D_OK!=(D3d9->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&D3dpp,&D3dDevice9)))
{
::MessageBox(0, "CreateDevice() - FAILED", 0, 0);
return FALSE;
}
return true;
}
void CDirect3D::AddPicButton(float xPos,float yPos,LPCTSTR PicName,float xSize,float ySize)
{
Pos->x1=xPos;
Pos->x2=yPos;
Pos->y1=xPos+xSize;
Pos->y2=yPos+ySize;
//用蓝色清空背景区域
D3dDevice9->Clear(1,(D3DRECT *)Pos,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0);
void* pVertices;
//建立按钮的数学模型
CUSTOMVERTEX vertices[4] =
{
{xPos, yPos, 0,0.0f,0.0f},
{xPos, yPos+ySize, 0,0.0f,1.0f},
{xPos+xSize, yPos+ySize, 0,1.0f,1.0f},
{xPos+xSize, yPos, 0,1.0f,0.0f}
};
//创建位图矩形的顶点缓存区,填入顶点数据
D3dDevice9->CreateVertexBuffer(sizeof(vertices),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&LPVB,NULL);
LPVB->Lock( 0, sizeof(vertices),(void**)&pVertices, 0 );
memcpy(pVertices,vertices, sizeof(vertices));
LPVB->Unlock();
//创建纹理对象
::D3DXCreateTextureFromFile(D3dDevice9, PicName, &LPTexture );
//因为二维图形使用顶点颜色或纹理渲染,所以关闭光照
D3dDevice9->SetRenderState( D3DRS_LIGHTING, FALSE );
//关闭 Z 缓存
D3dDevice9->SetRenderState( D3DRS_ZENABLE, D3DZB_FALSE );
//加载纹理,纹理混合方式使用缺省值。由于使用顶点颜色渲染,
//缺省的纹理混合方式=纹理×顶点颜色。
D3dDevice9->SetTexture( 0, LPTexture );
//显示位图矩形
D3dDevice9->SetFVF( D3DFVF_CUSTOMVERTEX );
D3dDevice9->SetStreamSource( 0, LPVB, 0, sizeof(CUSTOMVERTEX) );
D3dDevice9->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
D3dDevice9->SetTexture( 0, NULL );
}
void CDirect3D::SetLight()
{
//创建一个白色的平行光
D3DLIGHT9 light;
::ZeroMemory( &light, sizeof(D3DLIGHT9) );
light.Type = D3DLIGHT_DIRECTIONAL; //灯光类型
light.Diffuse.r = 1.0f;
light.Diffuse.g = 1.0f;
light.Diffuse.b = 1.0f;
light.Direction = D3DXVECTOR3( -1.0f, -1.0f, 0.0f );
light.Range = 1000.0f; //灯光的作用范围
D3dDevice9->SetLight( 0, &light ); //设置灯光,参数1为灯光的索引号
D3dDevice9->LightEnable( 0, TRUE ); //打开灯光,参数1为灯光的索引号
//设置环境光
D3dDevice9->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_RGBA(32,32,32,0) );
}
int CDirect3D::LoadXFile(LPCTSTR XFileName)
{
//我们将通过该接口指针访问Mesh对象的材质和纹理
LPD3DXBUFFER pD3DXMtrlBuffer;
//从.x文件创建Mesh对象
if(FAILED(D3DXLoadMeshFromX("XFileName",D3DXMESH_SYSTEMMEM,D3dDevice9,NULL,&pD3DXMtrlBuffer,NULL,&m_dwNumSubsets,&m_pMesh)))
{
::MessageBox(0, "D3DXLoadMeshFromX() - FAILED", 0, 0);
return FALSE;
}
m_pMeshMaterials = new D3DMATERIAL9[m_dwNumSubsets];
m_pMeshTextures = new LPDIRECT3DTEXTURE9[m_dwNumSubsets];
//从接口指针pD3DXMtrlBuffer获得各个子集的材质和纹理图片的文件名
D3DXMATERIAL* d3dxMaterials=(D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
for( DWORD i=0;i<m_dwNumSubsets;i++ )
{
//复制子集的材质
m_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;
//Direct3D在调入Mesh模型时,没有设置材质的环境光颜色,
//这里把它设置成和漫反射一样的颜色。
m_pMeshMaterials[i].Ambient = m_pMeshMaterials[i].Diffuse;
//调入纹理图片
if(FAILED(D3DXCreateTextureFromFile(D3dDevice9,d3dxMaterials[i].pTextureFilename,&m_pMeshTextures[i])))
{
::MessageBox(0, "D3DXCreateTextureFromFile() - FAILED", 0, 0);
return FALSE;
}
}
pD3DXMtrlBuffer->Release(); //释放接口
return 1;
}
void CDirect3D::SetupMatrices()
{
D3DXMATRIX matWorld;
UINT iTime = timeGetTime() % 5000;
FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 5000.0f;//把旋转角换算成弧度
D3DXMatrixRotationY( &matWorld, fAngle );//计算世界变换矩阵
D3dDevice9->SetTransform( D3DTS_WORLD, &matWorld );//把世界变换矩阵设置到渲染环境
D3DXVECTOR3 vEyePt( 0.0f, 15.0f,-25.0f );//观察点
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );//视线目标点
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );//上方向
D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );//计算视角变换矩阵
D3dDevice9->SetTransform( D3DTS_VIEW, &matView );//把视角变换矩阵设置到渲染环境
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );//计算透视投影变换矩阵
D3dDevice9->SetTransform( D3DTS_PROJECTION, &matProj );//把投影变换矩阵设置到渲染环境
}
void CDirect3D::Render()
{
//用蓝色清空背景区域
D3dDevice9->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,255),1.0f,0);
// 开始绘制场景
if(SUCCEEDED(D3dDevice9->BeginScene()))
{
SetupMatrices();//设置变换矩阵
SetLight(); //设置灯光
for( DWORD i=0; i < m_dwNumSubsets; i++ )
{
//设置子集的材质
D3dDevice9->SetMaterial( &m_pMeshMaterials[i] );
//设置子集的纹理,混合方式使用缺省值:纹理 × 材质的漫反射
D3dDevice9->SetTexture( 0, m_pMeshTextures[i] );
//绘制子集
m_pMesh->DrawSubset( i );
D3dDevice9->SetTexture( 0, NULL);
}
}
// 结束场景绘制
D3dDevice9->EndScene();
// 显示到屏幕上
D3dDevice9->Present(NULL,NULL,NULL,NULL);
}
void CDirect3D::CleanPic(void)
{
if(LPTexture!=NULL)
LPTexture->Release(); //释放纹理对象
if(LPVB!=NULL)
LPVB->Release(); //释放位图矩形的顶点缓存区
}
void CDirect3D::Clean3D(void)
{
if(D3dDevice9!=NULL)
D3dDevice9->Release();
if(D3d9!=NULL)
D3d9->Release();
}