#pragma once
#include "stdafx.h"
#include <d3d9.h>
#include <d3dx9.h>
#define SafeRelease(pObject) if(pObject!=NULL){pObject->Release();pObject=NULL;}
extern g_hWnd;
//3D的基类
class CGameEngine_D3D
{
public:
IDirect3D9* IDirect3D ;
IDirect3DDevice9* m_pIDirect3DDevice;
IDirect3DVertexBuffer9* m_pVertexBuffer;
IDirect3DIndexBuffer9* m_pIndexBuffer;
public:
CGameEngine_D3D();
~CGameEngine_D3D();
bool CreateD3Device(HWND hWnd,bool bFullScreen);
virtual bool BufferSet(); //定点缓冲区和定点索引缓冲区的建立
virtual void SetWorldPosition(float x,float y,float z); //世界矩阵的变换
virtual void SetCamera();
virtual void SetObjectMaterial(const D3DCOLORVALUE& dif,const D3DCOLORVALUE& amb,const D3DCOLORVALUE& spe,
const D3DCOLORVALUE& ami,float power ); //设置物体的材质
virtual void ComputeTriNormal(const D3DXVECTOR3& V1,const D3DXVECTOR3& V2,const D3DXVECTOR3& V3,D3DVECTOR& normolVec); //计算法向量
virtual void SetPointLight(D3DCOLORVALUE& dif,D3DCOLORVALUE& amb,D3DCOLORVALUE& spe,D3DVECTOR& pos);//设置摄影机的位置
virtual void Render();
void Release();
};
///
//
#include "stdafx.h"
#include "GameEngine_D3D.h"
CGameEngine_D3D::CGameEngine_D3D()
{
}
CGameEngine_D3D::~CGameEngine_D3D()
{
}
bool CGameEngine_D3D::CreateD3Device(HWND hWnd,bool bFullScreen)
{
// Init D3D:
//
// D3DDEVTYPE deviceType;
HRESULT hr = 0;
// Step 1: Create the IDirect3D9 object.
IDirect3D = Direct3DCreate9(D3D_SDK_VERSION);
if( !IDirect3D )
{
MessageBox(0, "Direct3DCreate9() - FAILED", 0, 0);
return false;
}
// Step 2: Check for hardware vp.
D3DCAPS9 caps;
IDirect3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
int vp = 0;
if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
// Step 3: Fill out the D3DPRESENT_PARAMETERS structure.
D3DPRESENT_PARAMETERS d3dpp;
d3dpp.BackBufferWidth = 800;
d3dpp.BackBufferHeight = 600;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.Windowed = true;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
// Step 4: Create the device.
hr = IDirect3D->CreateDevice(
D3DADAPTER_DEFAULT, // primary adapter
D3DDEVTYPE_HAL, // device type
hWnd, // window associated with device
vp, // vertex processing
&d3dpp, // present parameters
&m_pIDirect3DDevice); // return created device
if( FAILED(hr) )
{
// try again using a 16-bit depth buffer
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
hr = IDirect3D->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
vp,
&d3dpp,
&m_pIDirect3DDevice);
if( FAILED(hr) )
{
IDirect3D->Release(); // done with IDirect3D object
MessageBox(0, "CreateDevice() - FAILED", 0, 0);
return false;
}
}
return true;
}
bool CGameEngine_D3D::BufferSet()
{
return true;
}
void CGameEngine_D3D::SetWorldPosition(float x, float y, float z)
{
}
void CGameEngine_D3D::SetCamera()
{
}
void CGameEngine_D3D::ComputeTriNormal(const D3DXVECTOR3& V1,const D3DXVECTOR3& V2,const D3DXVECTOR3& V3,D3DVECTOR& normalVec)
{
D3DXVECTOR3 tempV1=V1-V2;
D3DXVECTOR3 tempV2=V1-V3;
D3DXVECTOR3 tempNormalVec;
D3DXVec3Cross(&tempNormalVec,&tempV1,&tempV2);
D3DXVec3Normalize(&tempNormalVec,&tempNormalVec);
normalVec=(D3DVECTOR)tempNormalVec;
}
void CGameEngine_D3D::SetPointLight(D3DCOLORVALUE& dif,D3DCOLORVALUE& amb,D3DCOLORVALUE& spe,D3DVECTOR& pos)
{
}
void CGameEngine_D3D::SetObjectMaterial(const D3DCOLORVALUE& dif,const D3DCOLORVALUE& amb,const D3DCOLORVALUE& spe,const D3DCOLORVALUE& emi,float power)
{
D3DMATERIAL9 d3dMaterial;
d3dMaterial.Diffuse=dif;
d3dMaterial.Ambient=amb;
d3dMaterial.Specular=spe;
d3dMaterial.Emissive=emi;
d3dMaterial.Power=power;
m_pIDirect3DDevice->SetMaterial(&d3dMaterial);
}
//compute the
void CGameEngine_D3D::Render()
{
}
void CGameEngine_D3D::Release()
{
SafeRelease(m_pIDirect3DDevice);
SafeRelease(IDirect3D);
}