#ifndef _D3DC3E1_H_
#define _D3DC3E1_H_
 
#include "SD3dBase.h"
 
class D3DC3E1:public SD3dBase
{
public:
	D3DC3E1(const TCHAR* szTitle):SD3dBase(true,szTitle)
	{
		m_pVB = NULL;
		m_pIB = NULL;
	}
	~D3DC3E1(){}
public:
	virtual bool		UserInitRes();
	virtual void		UserDelRes();
	virtual bool		UserFrameFunc(float dt);
	virtual bool		UserRenderFunc();
private:
	//	顶点缓存
	IDirect3DVertexBuffer9*		m_pVB;
	//	索引缓存
	IDirect3DIndexBuffer9*		m_pIB;
};
 
 
//	顶点
struct Vertex
{
	Vertex(){}
	Vertex(float x,float y,float z)
	{
		_x=x;_y=y;_z=z;
	}
	float _x,_y,_z;
	static const DWORD FVF;
};
 
#endif

#include "TestD3d.h"
 
const DWORD Vertex::FVF = D3DFVF_XYZ;
 
bool	D3DC3E1::UserInitRes()
{
	//	初始化资源
 
	//	创建顶点缓存
	m_pDevice->CreateVertexBuffer(
		8 * sizeof(Vertex),
		D3DUSAGE_WRITEONLY,
		Vertex::FVF,
		D3DPOOL_MANAGED,
		&m_pVB,
		0);
 
	//	创建索引缓存
	//	36个三角形 36个索引
	m_pDevice->CreateIndexBuffer(
		36 * sizeof(WORD),
		D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16,
		D3DPOOL_MANAGED,
		&m_pIB,
		0);
 
	//	填充顶点 步骤 锁顶点缓存 得到缓存指针 填充数据 解锁
	Vertex*	pVex = NULL;
	m_pVB->Lock(
		0,
		8 * sizeof(Vertex),
		(void**)&pVex,
		0);
 
	pVex[0] = Vertex(-1.0f,-1.0f,-1.0f);
	pVex[1] = Vertex(-1.0f,1.0f,-1.0f);
	pVex[2] = Vertex(1.0f,1.0f,-1.0f);
	pVex[3] = Vertex(1.0f,-1.0f,-1.0f);
	pVex[4] = Vertex(-1.0f,-1.0f,1.0f);
	pVex[5] = Vertex(-1.0f,1.0f,1.0f);
	pVex[6] = Vertex(1.0f,1.0f,1.0f);
	pVex[7] = Vertex(1.0f,-1.0f,1.0f);
 
	m_pVB->Unlock();
 
	//	填充索引
	WORD* pWord = NULL;
	m_pIB->Lock(
		0,
		0,
		(void**)&pWord,
		0);
 
	//	前面2个三角形
	pWord[0] = 0;pWord[1] = 1;pWord[2] = 2;
	pWord[3] = 0;pWord[4] = 2;pWord[5] = 3;
 
	//	后边
	pWord[6] = 4;pWord[7] = 6;pWord[8] = 5;
	pWord[9] = 4;pWord[10] = 7;pWord[11] = 6;
 
	//	左边
	pWord[12] = 4;pWord[13] = 5;pWord[14] = 1;
	pWord[15] = 4;pWord[16] = 1;pWord[17] = 0;
 
	//	右边
	pWord[18] = 3;pWord[19] = 2;pWord[20] = 6;
	pWord[21] = 3;pWord[22] = 6;pWord[23] = 7;
 
	//	顶部
	pWord[24] = 1;pWord[25] = 5;pWord[26] = 6;
	pWord[27] = 1;pWord[28] = 6;pWord[29] = 2;
 
	//	底部
	pWord[30] = 4;pWord[31] = 0;pWord[32] = 3;
	pWord[33] = 4;pWord[34] = 3;pWord[35] = 7;
 
	m_pIB->Unlock();
 
	//	坐标变换
	//	摄像机坐标
	D3DXVECTOR3 position(0.0f,0.0f,-5.0f);
	//	观察物体坐标
	D3DXVECTOR3 target(0.0f,0.0f,0.0f);
	//	上方向
	D3DXVECTOR3 up(0.0f,1.0f,0.0f);
 
	//	获得观察坐标系的矩阵
	D3DXMATRIX v;
	D3DXMatrixLookAtLH(&v,&position,&target,&up);
 
	m_pDevice->SetTransform(D3DTS_VIEW,&v);
 
	//	获得投影坐标系矩阵
	D3DXMATRIX proj;
	D3DXMatrixPerspectiveFovLH(
		&proj,
		D3DX_PI * 0.5f,
		(float)GetViewWidth()/(float)GetViewHeight(),
		1.0f,
		1000.0f);
	m_pDevice->SetTransform(D3DTS_PROJECTION,&proj);
 
	m_pDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
 
	return SD3dBase::UserInitRes();
}
 
void	D3DC3E1::UserDelRes()
{
	SD3dBase::Release<IDirect3DIndexBuffer9*>(m_pIB);
	SD3dBase::Release<IDirect3DVertexBuffer9*>(m_pVB);
}
 
bool	D3DC3E1::UserFrameFunc(float dt)
{
	//	运算
	static float fTotalTime = 0;
 
	D3DXMATRIX rx,ry;
	fTotalTime += dt;
	if(fTotalTime >= 6.28f)
	{
		fTotalTime = 0.0f;
	}
 
	D3DXMatrixRotationX(&rx,3.14f / 4.0f);
	D3DXMatrixRotationY(&ry,fTotalTime);
 
	D3DXMATRIX r = rx * ry;
	
	m_pDevice->SetTransform(D3DTS_WORLD,&r);
 
	return SD3dBase::UserFrameFunc(dt);
}
 
bool	D3DC3E1::UserRenderFunc()
{
	m_pDevice->Clear(0,0,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
		0xffffffff,
		1.0f,
		0);
 
	m_pDevice->BeginScene();
	m_pDevice->SetStreamSource(0,m_pVB,0,sizeof(Vertex));
	m_pDevice->SetIndices(m_pIB);
	m_pDevice->SetFVF(Vertex::FVF);
	m_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,8,0,12);
	m_pDevice->EndScene();
	m_pDevice->Present(0,0,0,0);
 
	return SD3dBase::UserRenderFunc();
}

共 0 条回复
暂时没有人回复哦,赶紧抢沙发
发表新回复

作者

sryan
today is a good day