【游戏程序设计】Direct 3D 雪花粒子系统

运行结果:

【游戏程序设计】Direct 3D 雪花粒子系统_第1张图片

源代码:

SnowParticleClass.h:

//=============================================================================
//一个封装了雪花粒子系统的类的头文件
//=============================================================================

#pragma once
#include 
#include 
#include 
#include "D3DUtil.h"

#define PARTICLE_NUMBER 3000										//雪花粒子数量
#define SNOW_SYSTOM_LENGHT_X 20000									//雪花粒子飞扬区域的长度
#define SNOW_SYSTOM_LENGHT_Y 20000									//雪花粒子飞扬区域的宽度
#define SNOW_SYSTOM_LENGHT_Z 20000									//雪花粒子飞扬区域的高度

//-------------------------------------------------------------------------------------------------
//雪花粒子的FVF顶点结构和顶点格式
//-------------------------------------------------------------------------------------------------
struct POINTVERTEX
{
	float x, y, z;													//顶点位置
	float u, v;														//顶点纹理坐标
};
#define D3DFVF_POINTVERTEX (D3DFVF_XYZ | D3DFVF_TEX1)
//-------------------------------------------------------------------------------------------------
//雪花粒子结构体的定义
//-------------------------------------------------------------------------------------------------
struct SNOWPARTICLE
{
	float x, y, z;													//坐标位置
	float rotationY;												//雪花绕自身Y轴旋转角度
	float rotationX;												//雪花绕自身X轴旋转角度
	float fallSpeed;												//雪花下降速度
	float rotationSpeed;											//雪花旋转速度
	int textureIndex;												//纹理索引数
};
//-------------------------------------------------------------------------------------------------
//粒子系统类的定义
//-------------------------------------------------------------------------------------------------
class SnowParticleClass
{
private: 
	LPDIRECT3DDEVICE9 m_pd3dDevice;									//d3d设备对象
	LPDIRECT3DVERTEXBUFFER9 m_pVertexBuffer;						//粒子顶点缓存
	SNOWPARTICLE m_Snows[PARTICLE_NUMBER];							//雪花粒子数组
	LPDIRECT3DTEXTURE9 m_pTextures[6];								//雪花纹理数组
public:
	SnowParticleClass(LPDIRECT3DDEVICE9 pd3dDevice);				//构造函数
	~SnowParticleClass(void);										//析构函数
	HRESULT InitSnowParticle();										//粒子系统初始化函数
	void UpdateSnowParticle(float fElapsedTime);					//粒子系统更新函数
	void RenderSnowParticle();										//粒子系统渲染函数
};

SnowParticleClass.cpp:

//=============================================================================
//一个封装了雪花粒子系统的类的源文件
//=============================================================================

#include "SnowParticleClass.h"
//-------------------------------------------------------------------------------------------------
//构造函数
//-------------------------------------------------------------------------------------------------
SnowParticleClass::SnowParticleClass(LPDIRECT3DDEVICE9 pd3dDevice)
{
	//给各个参数赋值
	m_pd3dDevice = pd3dDevice;
	m_pVertexBuffer = NULL;
	for(int i = 0; i < 6; ++i)
		m_pTextures[i] = NULL;
}
//-------------------------------------------------------------------------------------------------
//粒子系统初始化函数
//-------------------------------------------------------------------------------------------------
HRESULT SnowParticleClass::InitSnowParticle()
{
	//设置随机数种子
	srand(GetTickCount());
	//初始化雪花数组
	for(int i = 0; i < PARTICLE_NUMBER; ++i)
	{
		m_Snows[i].x = (rand() % SNOW_SYSTOM_LENGHT_X) - SNOW_SYSTOM_LENGHT_X/2.0f;
		m_Snows[i].z = (rand() % SNOW_SYSTOM_LENGHT_Z) - SNOW_SYSTOM_LENGHT_Z/2.0f;
		m_Snows[i].y = rand() % SNOW_SYSTOM_LENGHT_Y;
		m_Snows[i].rotationX = (rand() % 10) / 5.0f * D3DX_PI;
		m_Snows[i].rotationY = (rand() % 10) / 5.0f * D3DX_PI;
		m_Snows[i].fallSpeed = 300.0f + rand()%500;
		m_Snows[i].rotationSpeed = 5.0f + (rand() % 10) / 10.0f;
		m_Snows[i].textureIndex = rand() % 6;
	}
	//创建雪花粒子顶点缓存
	HRESULT hr;
	HR(m_pd3dDevice->CreateVertexBuffer(4*sizeof(POINTVERTEX), 0, D3DFVF_POINTVERTEX, 
													D3DPOOL_MANAGED, &m_pVertexBuffer, NULL));
	//填充雪花粒子顶点缓存
	POINTVERTEX vertices[] = 
	{
		{ -30.0f, 0.0f, 0.0f,   0.0f, 1.0f, },
		{ -30.0f, 60.0f, 0.0f,   0.0f, 0.0f, },
		{  30.0f, 0.0f, 0.0f,   1.0f, 1.0f, }, 
		{  30.0f, 60.0f, 0.0f,   1.0f, 0.0f, }
	};
	
	void *pVertices = NULL;
	//加锁
	HR(m_pVertexBuffer->Lock(0, sizeof(vertices), &pVertices, 0)); 
	//访问
	memcpy(pVertices, vertices, sizeof(vertices));
	//解锁
	m_pVertexBuffer->Unlock();
	//创建6种雪花纹理
	wchar_t filename[20] = {0};
	for(int i = 0; i < 6; ++i)
	{
		swprintf_s(filename, L"snow%d.jpg", i+1);
		HR(D3DXCreateTextureFromFile(m_pd3dDevice, filename, &m_pTextures[i]));
	}

	return S_OK;
}
//-------------------------------------------------------------------------------------------------
//粒子系统更新函数
//-------------------------------------------------------------------------------------------------
void SnowParticleClass::UpdateSnowParticle(float fElapsedTime)
{
	//for循环,更新每个粒子设为当前位置和角度	
	for(int i = 0; i < PARTICLE_NUMBER; ++i)
	{
		//如果雪花粒子落到地面,重新将高度设为最大
		m_Snows[i].y -= m_Snows[i].fallSpeed * fElapsedTime;
		if(m_Snows[i].y < 0)
			m_Snows[i].y = SNOW_SYSTOM_LENGHT_Y;
		//更改自旋角度
		m_Snows[i].rotationX += m_Snows[i].rotationSpeed * fElapsedTime;
		m_Snows[i].rotationY += m_Snows[i].rotationSpeed * fElapsedTime;
	}
}
//-------------------------------------------------------------------------------------------------
//粒子系统渲染函数
//-------------------------------------------------------------------------------------------------
void SnowParticleClass::RenderSnowParticle()
{
	//设置纹理状态
	m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);			//将纹理颜色混合的第一个参数用于输出
	m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);				//纹理颜色混合的第一个参数就取纹理颜色值
    m_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);				//缩小过滤状态采用线性过滤
	m_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);				//放大过滤状态采用线性过滤
	//设置Alpha设置系数  将源图像与目标图像颜色值相加,黑色部分(0,0,0)变为目标颜色,白色部分仍然不变  
	m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);							//打开alpha混合  
	m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);							//源混合系数设为1
	m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);						//目标融合系数设为1
	//设置剔除模式为不剔除任何面
	m_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	//渲染雪花
	D3DXMATRIX matRotaY, matRotaX, matTrans, matWorld;
	for(int i = 0; i < PARTICLE_NUMBER; ++i)
	{
		//构造并设置当前雪花粒子的世界矩阵
		D3DXMatrixRotationX(&matRotaX, m_Snows[i].rotationX);
		D3DXMatrixRotationY(&matRotaY, m_Snows[i].rotationY);
		D3DXMatrixTranslation(&matTrans, m_Snows[i].x, m_Snows[i].y, m_Snows[i].z);
		matWorld = matRotaX * matRotaY * matTrans;
		m_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
		m_pd3dDevice->SetTexture(0, m_pTextures[m_Snows[i].textureIndex]);				//设置当前纹理
		//渲染雪花粒子
		m_pd3dDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(POINTVERTEX));		//把包含几何体信息的顶点缓存与渲染流水线关联
		m_pd3dDevice->SetFVF(D3DFVF_POINTVERTEX);										//设置FVF灵活顶点模式
		m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);							//绘制
	}
	//恢复之前渲染模式 alpha混合,剔除状态
	m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
	m_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
}
//-------------------------------------------------------------------------------------------------
//析构函数
//-------------------------------------------------------------------------------------------------
SnowParticleClass::~SnowParticleClass(void)
{
	SAFE_RELEASE(m_pVertexBuffer);
	for(int i = 0; i < 6; ++i)
		SAFE_RELEASE(m_pTextures[i]);
}

WinMain.cpp:

#include 
#include 
#include 
#include "CameraClass.h"
#include "DirectInputClass.h"
#include "TerrainClass.h"
#include "SkyBoxClass.h"
#include "SnowParticleClass.h"

#define WINDOW_TITLE L"Direct 3D 雪花粒子系统"
//---------------------------------------【全局变量声明部分】------------------------------------------------
//描述:全局变量的声明
//-----------------------------------------------------------------------------------------------------------
LPDIRECT3DDEVICE9 g_pd3dDevice;																//Direct 3D设备对象
D3DXMATRIX g_matWorld;																		//世界矩阵
DirectInputClass *g_pDInput;																//一个DirectInput类的指针
LPD3DXFONT g_pTextAdapterName;																//显卡名字的2D文本
LPD3DXFONT g_pTextHelper;																	//帮助文本的2D文本
LPD3DXFONT g_pTextInfor;																	//绘制信息的2D文本
LPD3DXFONT g_pTextFPS;																		//FPS文本的2D文本
wchar_t g_strAdapterName[60];																//包括显卡名字的字符串		
wchar_t g_strFPS[50];																		//包含帧频率的字符数组
CameraClass *g_pCamera;																		//虚拟摄像机指针
TerrainClass *g_pTerrain;																	//地形类的指针实例
SkyBoxClass *g_pSkyBox;																		//天空盒类的指针
SnowParticleClass *g_pSnowParticle;															//雪花粒子系统的指针
LPD3DXMESH g_pMesh;																			//网格对象
D3DMATERIAL9 *g_pMaterials;																	//网格的材质信息
LPDIRECT3DTEXTURE9 *g_pTextures;															//网格的纹理信息
DWORD g_dwNumMtrls;																			//材质的数目
LPD3DXMESH g_pCylinder;																		//柱子网格对象
D3DMATERIAL9 g_cylinderMaterial;															//柱子的材质

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HRESULT Direct3D_Init(HWND);													//在这个函数中继续Direct3D的初始化
HRESULT Objects_Init(HWND);														//在这个函数中进行要绘制的物体的资源初始化
void Direct3D_Render(HWND, float);														//在这个函数中进行Direct3D渲染代码的书写
void Direct3D_ClearUp();														//在这个函数中清理COM资源以及其他资源
float Get_FPS();
void Direct3D_Update();
void HelpText_Render(HWND);
//----------------------------------------【WinMain()函数】-------------------------------------------------
//描述:Windows应用程序的入口函数
//-------------------------------------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wndClass = {0};
	wndClass.cbSize = sizeof(WNDCLASSEX);
	wndClass.style = CS_HREDRAW | CS_VREDRAW;
	wndClass.lpfnWndProc = (WNDPROC)WndProc;
	wndClass.cbClsExtra = 0;
	wndClass.cbWndExtra = 0;
	wndClass.hInstance = hInstance;
	wndClass.hIcon = (HICON)LoadImage(NULL, L"icon.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
	wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndClass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
	wndClass.lpszMenuName = NULL;
	wndClass.lpszClassName = L"3DGameBase";
	if(!RegisterClassEx(&wndClass))
		return -1;

	HWND hWnd = CreateWindow(L"3DGameBase", WINDOW_TITLE, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 
		WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL);
	MoveWindow(hWnd, 250, 80, WINDOW_WIDTH, WINDOW_HEIGHT, true);
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	g_pDInput = new DirectInputClass();
	g_pDInput->Init(hWnd, hInstance, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); 

	PlaySound(L"Final Fantasy XIII.wav", NULL, SND_LOOP | SND_ASYNC | SND_FILENAME);
	
	if(FAILED(Direct3D_Init(hWnd)))
		MessageBox(hWnd, L"Direct3D 初始化失败!", L"消息窗口", 0);
	MSG msg = {0};
	while(msg.message != WM_QUIT)
	{
		static float fLastTime = timeGetTime();										//上帧渲染时间
		static float fCurrTime = timeGetTime();										//此帧渲染时间
		static float fTimeDelta = 0.0f;
		fCurrTime = timeGetTime(); 
		fTimeDelta = (fCurrTime - fLastTime) / 1000.f;								//上帧到此帧经过的时间
		fLastTime = fCurrTime;														//把此次渲染时间赋给上次渲染时间
	
		if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			Direct3D_Update();
			Direct3D_Render(hWnd, fTimeDelta);
		}
	}

	UnregisterClass(L"3DGameBase", wndClass.hInstance);
	return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_PAINT:
		ValidateRect(hWnd, NULL);													//使窗口区域生效
		break;
	case WM_KEYDOWN:
		if(wParam == VK_ESCAPE)
			DestroyWindow(hWnd);
		break;
	case WM_DESTROY:
		//调用自定义的资源清理函数Direct3D_ClearUp();进行退出前的资源清理
		Direct3D_ClearUp();
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}

	return 0;
}
//---------------------------------------------【Direct3D_Init()函数】-----------------------------------------
//描述:Direct3D初始化函数,进行Direct3D的初始化
//---------------------------------------------------------------------------------------------------------------
HRESULT Direct3D_Init(HWND hWnd)
{
	//---------------------------------------------------------------------------------------------------------------
	//【Direct3D初始化步骤一】:创建Direct3D接口对象,以便用该Direct3D对象创建Direct3D设备对象
	//---------------------------------------------------------------------------------------------------------------
	LPDIRECT3D9 pD3D = NULL; //Direct3D接口对象的创建。

	if((pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)						//初始化Direct3D接口对象,并进行DirectX版本协商。
		return E_FAIL;
	//---------------------------------------------------------------------------------------------------------------
	//【Direct3D初始化步骤二】:获取硬件设备信息
	//---------------------------------------------------------------------------------------------------------------
	D3DCAPS9 caps;
	int vp = 0;
	if(FAILED(pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps))) 
		return E_FAIL;
	if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
		vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;								//支持硬件顶点运算,采用硬件顶点运算
	else
		vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;								//不支持硬件顶点运算,采用软件顶点运算

	//---------------------------------------------------------------------------------------------------------------
	//【Direct3D初始化步骤三】:填充D3DPRESENT_PARAMETERS结构体
	//---------------------------------------------------------------------------------------------------------------
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));

	d3dpp.BackBufferWidth = WINDOW_WIDTH;
	d3dpp.BackBufferHeight = WINDOW_HEIGHT;
	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 = 0;
	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
	//---------------------------------------------------------------------------------------------------------------
	//【Direct3D初始化步骤四】:创建Direct3D设备接口。
	//---------------------------------------------------------------------------------------------------------------
	if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, vp, &d3dpp, &g_pd3dDevice)))
		return E_FAIL;

	//获取显卡信息到g_strAdapterName中,并在显卡名称前加上“当前显卡型号:”字符串
	//定义一个D3DADAPTER_IDENTIFIER9结构体,用于存储显卡信息  
	D3DADAPTER_IDENTIFIER9 Adapter;
	//调用GetAdapterIdentifier,获取显卡信息
	if(FAILED(pD3D->GetAdapterIdentifier(0, 0, &Adapter)))
		return E_FAIL;
	//显卡名称现在已经在Adapter.Description中了,但是其为char类型,我们要将其转为wchar_t类型  
	int len = MultiByteToWideChar(CP_ACP, 0, Adapter.Description, -1, NULL, 0);
	//这步操作完成后,g_strAdapterName中就为当前我们的显卡类型名的wchar_t型字符串了 
	MultiByteToWideChar(CP_ACP, 0, Adapter.Description, -1, g_strAdapterName, len);
	//定义一个临时字符串,且方便了把"当前显卡型号:"字符串引入我们的目的字符串中  
	wchar_t tempName[50] = L"当前显卡型号:";
	//把当前我们的显卡名加到“当前显卡型号:”字符串后面,结果存在TempName中  
	wcscat_s(tempName, g_strAdapterName);
	//把TempName中的结果拷贝到全局变量g_strAdapterName中
	wcscpy_s(g_strAdapterName, tempName);

	SAFE_RELEASE(pD3D);														//LPDIRECT3D9接口对象的使命完成,将其释放掉
	if(FAILED(Objects_Init(hWnd)))											// 调用一次Objects_Init,进行渲染资源的初始化
		return E_FAIL;

	return S_OK;
}
//------------------------------------------【Objects_Init()】函数---------------------------------------------
//描述:渲染资源初始化函数,在此函数中进行要被渲染的物体的资源的初始化
//---------------------------------------------------------------------------------------------------------------
HRESULT Objects_Init(HWND hWnd)
{
		//创建字体  
	if(FAILED(D3DXCreateFont(g_pd3dDevice, 36, 0, 0, 1000, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 
													DEFAULT_QUALITY, 0, L"Calibri", &g_pTextFPS)))
		return E_FAIL;
	if(FAILED(D3DXCreateFont(g_pd3dDevice, 20, 0, 0, 1000, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
													DEFAULT_QUALITY, 0, L"华文中宋", &g_pTextAdapterName)))
		return E_FAIL;
	if(FAILED(D3DXCreateFont(g_pd3dDevice, 23, 0, 0, 1000, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
													DEFAULT_QUALITY, 0, L"微软雅黑", &g_pTextHelper)))
		return E_FAIL;
	if(FAILED(D3DXCreateFont(g_pd3dDevice, 26, 0, 0, 1000, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
													DEFAULT_QUALITY, 0, L"黑体", &g_pTextInfor)))
		return E_FAIL;

	//创建并初始化虚拟摄像机
	g_pCamera = new CameraClass(g_pd3dDevice);
	g_pCamera->SetCameraPosition(&D3DXVECTOR3(0.0f, 1400.0f, -1800.0f));  //设置摄像机所在的位置
	g_pCamera->SetTargetPosition(&D3DXVECTOR3(0.0f, 1200.0f, 0.0f));  //设置目标观察点所在的位置
	g_pCamera->SetViewMatrix();													//设置取景变换矩阵
	g_pCamera->SetProjMatrix();				
	//创建并初始化地形
	g_pTerrain = new TerrainClass(g_pd3dDevice);
	g_pTerrain->LoadTerrainFromFile(L"heighmap.raw", L"terrainstone.jpg");				//从文件读取高度图和纹理
	g_pTerrain->InitTerrain(200, 200, 60.0f, 8.0f);							//顶点行数,顶点列数,顶点间间距,缩放系数
	//创建并初始化天空对象
	g_pSkyBox = new SkyBoxClass(g_pd3dDevice);
	//从文件加载前,后,左,右,上的纹理图
	g_pSkyBox->LoadSkyTextureFromFile(L"TropicalSunnyDayFront2048.png", L"TropicalSunnyDayBack2048.png", 
					L"TropicalSunnyDayLeft2048.png", L"TropicalSunnyDayRight2048.png", L"TropicalSunnyDayUp2048.png"); 
	//设置天空盒的边长
	g_pSkyBox->InitSkyBox(50000);
	//创建并初始化雪花粒子系统
	g_pSnowParticle = new SnowParticleClass(g_pd3dDevice);
	g_pSnowParticle->InitSnowParticle();

	//设置投影变换矩阵
	//以下这段代码用于限制鼠标光标移动区域
	POINT lt, rb;
	RECT rect;
	GetClientRect(hWnd, &rect);//取得窗口内部矩形
	//将矩形左上点坐标存入lt中
	lt.x = rect.left;
	lt.y = rect.top;
	//将矩形右下坐标存入rb中
	rb.x = rect.right;
	rb.y = rect.bottom;
	//将lt和rb的窗口坐标转换为屏幕坐标
	ClientToScreen(hWnd, <);
	ClientToScreen(hWnd, &rb);
	//以屏幕坐标重新设定矩形区域
	rect.left = lt.x;
	rect.top = lt.y;
	rect.right = rb.x;
	rect.bottom = rb.y;
	//限制鼠标光标移动区域
	ClipCursor(&rect);
	ShowCursor(false);

	// 从X文件中加载网格数据
	LPD3DXBUFFER pAdjBuffer = NULL;
	LPD3DXBUFFER pMtrlBuffer = NULL;
	// 读取材质和纹理数据
	D3DXLoadMeshFromX(L"angle.X", D3DXMESH_MANAGED, g_pd3dDevice, &pAdjBuffer,  
								&pMtrlBuffer, 0, &g_dwNumMtrls, &g_pMesh);
	g_pMaterials = new D3DMATERIAL9[g_dwNumMtrls];
	g_pTextures = new LPDIRECT3DTEXTURE9[g_dwNumMtrls];
	//创建一个D3DXMATERIAL结构体用于读取材质和纹理信息
	D3DXMATERIAL *pMtrl = (D3DXMATERIAL *)pMtrlBuffer->GetBufferPointer();
	for(DWORD i = 0; i < g_dwNumMtrls; ++i)
	{
		//获取材质,并设置一下环境光的颜色值
		g_pMaterials[i] = pMtrl[i].MatD3D;
		g_pMaterials[i].Ambient = g_pMaterials[i].Diffuse;
		//创建一下纹理对象
		D3DXCreateTextureFromFileA(g_pd3dDevice, pMtrl[i].pTextureFilename, &g_pTextures[i]);
	}
	SAFE_RELEASE(pAdjBuffer);
	SAFE_RELEASE(pMtrlBuffer);
	//创建柱子
	D3DXCreateCylinder(g_pd3dDevice, 280.0f, 10.0f, 3000.0f, 60, 60,  &g_pCylinder, 0);
	g_cylinderMaterial.Ambient = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);  
	g_cylinderMaterial.Diffuse = D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f);  
	g_cylinderMaterial.Specular = D3DXCOLOR(0.5f, 0.0f, 0.3f, 0.3f);  
	g_cylinderMaterial.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
	// 设置光照  
	D3DLIGHT9 light;
	light.Type = D3DLIGHT_DIRECTIONAL;
	light.Ambient       = D3DXCOLOR(0.7f, 0.7f, 0.7f, 1.0f);  
	light.Diffuse       = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);  
	light.Specular      = D3DXCOLOR(0.9f, 0.9f, 0.9f, 1.0f);  
	light.Direction     = D3DXVECTOR3(1.0f, 1.0f, 1.0f);  
	g_pd3dDevice->SetLight(0, &light);
	g_pd3dDevice->LightEnable(0, true);
	g_pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
	g_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, true);

	return S_OK;
}

void Direct3D_Update()
{
	//使用DirectInput类读取数据
	g_pDInput->GetInput();
	// 沿摄像机各分量移动视角
	if (g_pDInput->IsKeyDown(DIK_A))  g_pCamera->MoveAlongRightVec(-3.0f);
	if (g_pDInput->IsKeyDown(DIK_D))  g_pCamera->MoveAlongRightVec(3.0f);
	if (g_pDInput->IsKeyDown(DIK_W)) g_pCamera->MoveAlongLookVec( 3.0f);
	if (g_pDInput->IsKeyDown(DIK_S))  g_pCamera->MoveAlongLookVec(-3.0f);
	if (g_pDInput->IsKeyDown(DIK_R))  g_pCamera->MoveAlongUpVec( 3.0f);
	if (g_pDInput->IsKeyDown(DIK_F))  g_pCamera->MoveAlongUpVec(-3.0f);

	//沿摄像机各分量旋转视角
	if (g_pDInput->IsKeyDown(DIK_LEFT))  g_pCamera->RotationUpVec(-0.003f);
	if (g_pDInput->IsKeyDown(DIK_RIGHT))  g_pCamera->RotationUpVec( 0.003f);
	if (g_pDInput->IsKeyDown(DIK_UP))  g_pCamera->RotationRightVec(-0.003f);
	if (g_pDInput->IsKeyDown(DIK_DOWN))  g_pCamera->RotationRightVec( 0.003f);
	if (g_pDInput->IsKeyDown(DIK_Q)) g_pCamera->RotationLookVec(0.001f);
	if (g_pDInput->IsKeyDown(DIK_E)) g_pCamera->RotationLookVec( -0.001f);

	//鼠标控制右向量和上向量的旋转
	g_pCamera->RotationUpVec(g_pDInput->MouseDX()* 0.001f);
	g_pCamera->RotationRightVec(g_pDInput->MouseDY() * 0.001f);
	//计算并设置取景变换矩阵
	g_pCamera->SetViewMatrix();
	//鼠标滚轮控制观察点收缩操作
	static float fPosZ = 0.0f;
	fPosZ += g_pDInput->MouseDZ() * 0.03f;
	//把正确的世界变换矩阵存到g_matWorld中
	D3DXMatrixTranslation(&g_matWorld, 0, 0, fPosZ);
}

//----------------------------------------【Direct3D_Render()函数】--------------------------------------------
//描述:使用Direct3D进行渲染
//---------------------------------------------------------------------------------------------------------------
void Direct3D_Render(HWND hWnd, float fTimeDelta)
{
	//---------------------------------------------------------------------------------------------------------------
	//【Direct3D渲染步骤一】:清屏操作
	//---------------------------------------------------------------------------------------------------------------
	g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 108, 255), 1.0f, 0);
	//---------------------------------------------------------------------------------------------------------------
	//【Direct3D渲染步骤二】:开始绘制
	//---------------------------------------------------------------------------------------------------------------
	g_pd3dDevice->BeginScene();																				//开始绘制
	//---------------------------------------------------------------------------------------------------------------
	//【Direct3D渲染步骤三】:正式绘制
	//---------------------------------------------------------------------------------------------------------------
	//绘制人物
	D3DXMATRIX mScal,mRot2,mTrans,mFinal;   //定义一些矩阵,准备对大黄蜂进行矩阵变换
	D3DXMatrixTranslation(&mTrans,50.0f,1200.0f,0.0f);
	D3DXMatrixScaling(&mScal,3.0f,3.0f,3.0f);
	mFinal=mScal*mTrans*g_matWorld;
	g_pd3dDevice->SetTransform(D3DTS_WORLD, &mFinal);//设置模型的世界矩阵,为绘制做准备
	// 用一个for循环,进行模型的网格各个部分的绘制
	for (DWORD i = 0; i < g_dwNumMtrls; i++)
	{
		g_pd3dDevice->SetMaterial(&g_pMaterials[i]);  //设置此部分的材质
		g_pd3dDevice->SetTexture(0, g_pTextures[i]);//设置此部分的纹理
		g_pMesh->DrawSubset(i);  //绘制此部分
	}

	//绘制地形
	g_pTerrain->RenderTerrain(&g_matWorld);			//渲染地形,没有第二个参数表示不渲染地形边框

	//绘制柱子
	D3DXMATRIX TransMatrix, RotMatrix, FinalMatrix;
	D3DXMatrixRotationX(&RotMatrix, -D3DX_PI * 0.5f);
	g_pd3dDevice->SetMaterial(&g_cylinderMaterial);
	g_pd3dDevice->SetTexture(0, NULL);//设置此部分的纹理
	for(int i = 0; i < 4; i++)
	{
		D3DXMatrixTranslation(&TransMatrix, -300.0f, 0.0f, -350.0f + (i * 500.0f));
		FinalMatrix = RotMatrix * TransMatrix ;
		g_pd3dDevice->SetTransform(D3DTS_WORLD, &FinalMatrix);
		g_pCylinder->DrawSubset(0);

		D3DXMatrixTranslation(&TransMatrix, 300.0f, 0.0f, -350.0f + (i * 500.0f));
		FinalMatrix = RotMatrix * TransMatrix ;
		g_pd3dDevice->SetTransform(D3DTS_WORLD, &FinalMatrix);
		g_pCylinder->DrawSubset(0);
	}
	//绘制天空
	D3DXMATRIX matSky,matTransSky,matRotSky;
	D3DXMatrixTranslation(&matTransSky,0.0f,-12000.0f,0.0f);
	D3DXMatrixRotationY(&matRotSky, -0.000005f*timeGetTime());   //旋转天空网格, 简单模拟云彩运动效果
	matSky=matTransSky*matRotSky;
	g_pSkyBox->RenderSkyBox(&matSky);
	//绘制雪花粒子系统
	g_pSnowParticle->UpdateSnowParticle(fTimeDelta);
	g_pSnowParticle->RenderSnowParticle();

	//绘制文字信息
	HelpText_Render(hWnd);
	//---------------------------------------------------------------------------------------------------------------
	//【Direct3D渲染步骤四】:结束绘制
	//---------------------------------------------------------------------------------------------------------------
	g_pd3dDevice->EndScene();																				//结束绘制
	//---------------------------------------------------------------------------------------------------------------
	//【Direct3D渲染步骤五】:显示翻转
	//---------------------------------------------------------------------------------------------------------------
	g_pd3dDevice->Present(NULL, NULL, NULL, NULL);															//翻转与显示
}

void HelpText_Render(HWND hWnd)
{
	//定义一个矩形,用来获取主窗口矩形
	RECT formatRect;
	GetClientRect(hWnd, &formatRect);
	//在窗口右上角处,显示每秒帧数  
	swprintf_s(g_strFPS, L"FPS:%.3f", Get_FPS());
	g_pTextFPS->DrawText(0, g_strFPS, -1, &formatRect, DT_TOP | DT_RIGHT, D3DCOLOR_XRGB(255,255,255));
	 //显示显卡类型名  
	g_pTextAdapterName->DrawText(0, g_strAdapterName, -1, &formatRect, DT_TOP | DT_LEFT, D3DXCOLOR(1.0f, 0.5f, 0.0f, 1.0f));
	// 输出帮助信息
	formatRect.left = 0,formatRect.top = 380;
	g_pTextInfor->DrawText(NULL, L"控制说明:", -1, &formatRect, 
		DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(235,123,230,255));
	formatRect.top += 35;
	g_pTextHelper->DrawText(NULL, L"    W:向前飞翔     S:向后飞翔 ", -1, &formatRect, 
		DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255));
	formatRect.top += 25;
	g_pTextHelper->DrawText(NULL, L"    A:向左飞翔     D:向右飞翔", -1, &formatRect, 
		DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255));
	formatRect.top += 25;
	g_pTextHelper->DrawText(NULL, L"    R:垂直向上飞翔     F:垂直向下飞翔", -1, &formatRect, 
		DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255));
	formatRect.top += 25;
	g_pTextHelper->DrawText(NULL, L"    Q:向左倾斜       E:向右倾斜", -1, &formatRect, 
		DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255));
	formatRect.top += 25;
	g_pTextHelper->DrawText(NULL, L"    上、下、左、右方向键、鼠标移动:视角变化 ", -1, &formatRect, 
		DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255));
	formatRect.top += 25;
	g_pTextHelper->DrawText(NULL, L"     鼠标滚轮:人物模型Y轴方向移动", -1, &formatRect, 
		DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255));
	formatRect.top += 25;
	g_pTextHelper->DrawText(NULL, L"    ESC键 : 退出程序", -1, &formatRect, 
		DT_SINGLELINE | DT_NOCLIP | DT_LEFT, D3DCOLOR_RGBA(255,200,0,255));
}

//-----------------------------------------【Get_FPS()函数】---------------------------------------------------
//描述:用于计算帧频率
//---------------------------------------------------------------------------------------------------------------
float Get_FPS()
{
	//定义四个静态变量
	static int frameCount = 0;																	//帧数
	static float currentTime = 0;																//当前时间 
	static float lastTime = 0;																	//上次计算帧频率的时间
	static float fps = 0;																		//需要计算的fps值
	
	++frameCount;																				//每调用一次此函数,帧数加一
	//获取系统时间, timeGetTime() 返回系统时间,以毫秒为单位,乘以0.001得到秒
	currentTime = timeGetTime() * 0.001f;												
	//如果当前时间减去之前计算帧频率的时间大于1秒钟,就进行帧频率的更新,并将帧数归零
	if(currentTime - lastTime > 1.0f)															//将时间控制在1秒钟
	{
		fps = frameCount / (currentTime - lastTime);											//计算这一秒的fps值
		frameCount = 0;																			//将本次帧数清零
		lastTime = currentTime;								//将当前时间赋给上次计算帧频率的时间,作为下一秒的基准时间
	}

	return fps;
}

//------------------------------------------------【 Direct3D_ClearUp函数】------------------------------------------
//描述:资源清理函数,在此函数中进行程序退出前资源的清理工作
//-------------------------------------------------------------------------------------------------------------------
void Direct3D_ClearUp()
{
	//释放COM接口对象
	SAFE_RELEASE(g_pd3dDevice);
	SAFE_RELEASE(g_pTextFPS);
	SAFE_RELEASE(g_pTextHelper);
	SAFE_RELEASE(g_pTextAdapterName);
	SAFE_RELEASE(g_pTextInfor);
	SAFE_DELETE(g_pDInput);
	SAFE_DELETE(g_pTerrain);
	SAFE_DELETE(g_pCamera);
	SAFE_DELETE(g_pSkyBox);
	SAFE_DELETE(g_pSnowParticle);
	SAFE_RELEASE(g_pMesh);
	SAFE_DELETE(g_pMaterials);
	for(DWORD i = 0; i < g_dwNumMtrls; ++i)
		SAFE_RELEASE(g_pTextures[i]);
	SAFE_DELETE(g_pTextures);
	SAFE_RELEASE(g_pCylinder);
}

 

你可能感兴趣的:(游戏程序设计,游戏开发)