//=============================================================================
// Desc: 简单灯光范例程序
// 使用VC6.0,需要工具->选项->目录添加include和lib
//C:\PROGRAM FILES\MICROSOFT DIRECTX SDK (AUGUST 2008)\INCLUDE
//C:\PROGRAM FILES\MICROSOFT DIRECTX SDK (AUGUST 2008)\LIB\X86
//=============================================================================
#include <d3d9.h>
#include <d3dx9.h>
#include <D3dx9math.h>
#include <Windows.h>
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
#pragma comment(lib, "Winmm.lib")
//-----------------------------------------------------------------------------
// Desc: 全局变量
//-----------------------------------------------------------------------------
LPDIRECT3D9 g_pD3D = NULL; //Direct3D对象
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; //Direct3D设备对象
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; //顶点缓冲区对象
LPD3DXFONT g_pFont = 0; //字体对象
RECT rect;
UINT Select_Light =1; //灯光选择,初始选择点光源
BOOL Select_RoAndMyMtrl[2]={FALSE,TRUE}; //旋转和材质使用开关
D3DLIGHT9 light; //灯光
D3DMATERIAL9 mtrl; //材质
D3DXMATRIX matWorld; //世界矩阵
D3DXMATRIX matView; //观察矩阵
BOOL MK[128]; //BOOL开关集合
//-----------------------------------------------------------------------------
// Desc: 顶点结构
//-----------------------------------------------------------------------------
struct CUSTOMVERTEX
{
D3DXVECTOR3 position; //顶点位置
D3DXVECTOR3 normal; //顶点法线
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL)
//
//-----------------------------------------------------------------------------
// Desc: 设置变换矩阵
//-----------------------------------------------------------------------------
VOID SetD3DView(D3DXMATRIX &matView)
{
//建立并设置观察矩阵
D3DXVECTOR3 vEyePt( 0.0f, 20.0f,-20.0f ); //摄像机的位置
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
//////////////
D3DXVECTOR3 CameraPosition=vEyePt; //摄像机的位置CameraPosition=vEyePt;
D3DXVECTOR3 CameraZ=vLookatPt-vEyePt; //注意是左手系
D3DXVec3Normalize(&CameraZ, &CameraZ); //摄像机的Z方向CameraZ=vLookatPt-vEyePt;
D3DXVECTOR3 CameraX;
D3DXVec3Cross(&CameraX, &vUpVec, &CameraZ); //摄像机的X方向CameraX=cross(vUpVec,CameraZ);
D3DXVec3Normalize(&CameraX, &CameraX);
D3DXVECTOR3 CameraY;
D3DXVec3Cross(&CameraY, &CameraZ, &CameraX); //摄像机的X方向CameraY=cross(CameraZ,CameraX);
D3DXVec3Normalize(&CameraY, &CameraY);
///////////////////////////////////////////////////
float posx = -D3DXVec3Dot(&CameraX, &CameraPosition);
float posy = -D3DXVec3Dot(&CameraY, &CameraPosition);
float posz = -D3DXVec3Dot(&CameraZ, &CameraPosition);
matView = D3DXMATRIX( CameraX.x, CameraY.x, CameraZ.x, 0.0f,
CameraX.y, CameraY.y, CameraZ.y, 0.0f,
CameraX.z, CameraY.z, CameraZ.z, 0.0f,
posx, posy, posz, 1.0f );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
}
/////////////////////////////////////
//实现观察矩阵的全方位移动:方法一
//////////////////////////////////////////////
VOID MoveMyMatrix(D3DXMATRIX &MyMatrix, BOOL MK[128], float RotaW=0.04, float moveV=0.08)
{
//创建并设置世界矩阵
float fRoll=0.0f;
float fPitch=0.0f;
float fYaw=0.0f;
if(MK[0X57]) fPitch +=RotaW; //'W', 绕X轴旋转 fPitch
if(MK[0X53]) fPitch -=RotaW; //'s', 绕X轴旋转 fPitch
if(MK[0X41]) fRoll +=RotaW;//'A', 绕Z轴旋转 fRoll
if(MK[0X44]) fRoll -=RotaW; //'D', 绕Z轴旋转 fRoll
if(MK[0X51]) fYaw +=RotaW; //'Q', 绕Y轴旋转 fYaw
if(MK[0X45]) fYaw -=RotaW; //'E', 绕Y轴旋转 fYaw
///////////////
D3DXVECTOR3 MyMatrixX=D3DXVECTOR3(MyMatrix._11, MyMatrix._21, MyMatrix._31);
D3DXVECTOR3 MyMatrixY=D3DXVECTOR3(MyMatrix._12, MyMatrix._22, MyMatrix._32);
D3DXVECTOR3 MyMatrixZ=D3DXVECTOR3(MyMatrix._13, MyMatrix._23, MyMatrix._33);
if(0.0f!=fYaw || 0.0f!=fRoll || 0.0f!=fPitch)
{
if(0.0f!=fYaw)
{
D3DXMATRIX matYaw;
D3DXMatrixRotationAxis(&matYaw, &MyMatrixY, fYaw);
D3DXVec3TransformCoord(&MyMatrixZ, &MyMatrixZ, &matYaw);
D3DXVec3TransformCoord(&MyMatrixX, &MyMatrixX, &matYaw);
}
if(0.0f!=fRoll)
{
D3DXMATRIX matRoll;
D3DXMatrixRotationAxis(&matRoll, &MyMatrixZ, fRoll);
D3DXVec3TransformCoord(&MyMatrixX, &MyMatrixX, &matRoll);
D3DXVec3TransformCoord(&MyMatrixY, &MyMatrixY, &matRoll);
}
if(0.0f!=fPitch)
{
D3DXMATRIX matPitch;
D3DXMatrixRotationAxis(&matPitch, &MyMatrixX, fPitch);
D3DXVec3TransformCoord(&MyMatrixY, &MyMatrixY, &matPitch);
D3DXVec3TransformCoord(&MyMatrixZ, &MyMatrixZ, &matPitch);
}
D3DXVec3Normalize(&MyMatrixZ, &MyMatrixZ); //单位化Z向量
D3DXVec3Cross(&MyMatrixX, &MyMatrixY, &MyMatrixZ);
D3DXVec3Normalize(&MyMatrixX, &MyMatrixX); //单位化X向量
D3DXVec3Cross(&MyMatrixY, &MyMatrixZ, &MyMatrixX);
D3DXVec3Normalize(&MyMatrixY, &MyMatrixY); //单位化Y向量
MyMatrix._31=MyMatrixX.z; MyMatrix._32=MyMatrixY.z; MyMatrix._33=MyMatrixZ.z;
MyMatrix._21=MyMatrixX.y; MyMatrix._22=MyMatrixY.y; MyMatrix._23=MyMatrixZ.y;
MyMatrix._11=MyMatrixX.x; MyMatrix._12=MyMatrixY.x; MyMatrix._13=MyMatrixZ.x;
}
///////////
D3DXVECTOR3 MyMatrixPosition=D3DXVECTOR3(MyMatrix._41, MyMatrix._42, MyMatrix._43);
BOOL IsPos=FALSE;
if(MK[VK_PRIOR]) //'PageUp', MoveY
{
MyMatrixPosition +=moveV*MyMatrixY;
IsPos=TRUE;
}
if(MK[VK_NEXT]) //'PageDown', MoveY
{
MyMatrixPosition -=moveV*MyMatrixY;
IsPos=TRUE;
}
if(MK[VK_LEFT]) //'LEFT', MoveX
{
MyMatrixPosition +=moveV*MyMatrixX;
IsPos=TRUE;
}
if(MK[VK_RIGHT]) //'RIGHT', MoveX
{
MyMatrixPosition -=moveV*MyMatrixX;
IsPos=TRUE;
}
if(MK[VK_UP]) //'UP', MoveZ
{
MyMatrixPosition +=moveV*MyMatrixZ;
IsPos=TRUE;
}
if(MK[VK_DOWN]) //'DOWN', MoveZ
{
MyMatrixPosition -=moveV*MyMatrixZ;
IsPos=TRUE;
}
if(TRUE==IsPos)
{
MyMatrix._41=MyMatrixPosition.x; MyMatrix._42=MyMatrixPosition.y; MyMatrix._43=MyMatrixPosition.z;
}
}
/////////////////////////////////////
//实现观察矩阵的全方位移动:方法二
//////////////////////////////////////////////
VOID MoveMyMatrix(D3DXMATRIX &MyMatrix, float RotaW=0.04, float moveV=0.08)
{
//创建并设置世界矩阵
float fRoll=0.0f;
float fPitch=0.0f;
float fYaw=0.0f;
if(GetAsyncKeyState(0X57) & 0x8000f) //'W', 绕X轴旋转 fPitch
{
fPitch +=RotaW;
}
if(GetAsyncKeyState(0X53) & 0x8000f) //'s', 绕X轴旋转 fPitch
{
fPitch -=RotaW;
}
if(GetAsyncKeyState(0X41) & 0x8000f) //'A', 绕Z轴旋转 fRoll
{
fRoll +=RotaW;
}
if(GetAsyncKeyState(0X44) & 0x8000f) //'D', 绕Z轴旋转 fRoll
{
fRoll -=RotaW;
}
if(GetAsyncKeyState(0X51) & 0x8000f) //'Q', 绕Y轴旋转 fYaw
{
fYaw +=RotaW;
}
if(GetAsyncKeyState(0X45) & 0x8000f) //'E', 绕Y轴旋转 fYaw
{
fYaw -=RotaW;
}
///////////////////
D3DXVECTOR3 MyMatrixX=D3DXVECTOR3(MyMatrix._11, MyMatrix._21, MyMatrix._31);
D3DXVECTOR3 MyMatrixY=D3DXVECTOR3(MyMatrix._12, MyMatrix._22, MyMatrix._32);
D3DXVECTOR3 MyMatrixZ=D3DXVECTOR3(MyMatrix._13, MyMatrix._23, MyMatrix._33);
if(0.0f!=fYaw || 0.0f!=fRoll || 0.0f!=fPitch)
{
if(0.0f!=fYaw)
{
D3DXMATRIX matYaw;
D3DXMatrixRotationAxis(&matYaw, &MyMatrixY, fYaw);
D3DXVec3TransformCoord(&MyMatrixZ, &MyMatrixZ, &matYaw);
D3DXVec3TransformCoord(&MyMatrixX, &MyMatrixX, &matYaw);
}
if(0.0f!=fRoll)
{
D3DXMATRIX matRoll;
D3DXMatrixRotationAxis(&matR