DX骨骼动画,地形简单的演示
来源:互联网 发布:数据结构出栈算法程序 编辑:程序博客网 时间:2024/04/30 08:37
给出主文件
头文件:
- //
- //D3D主文件类
- //作者:CYM
- //
- #pragma once
- #include <d3dx9.h>
- #include <string>
- #include "D3DFrame.h"
- #include "Model.h"
- #include "Camera.h"
- #include "Light.h"
- #include "Material.h"
- #include "Text.h"
- #include "AllocateHierarchy.h"
- #include "WorldMatrix.h"
- #include "Terrain.h"
- class CMain
- {
- public:
- CMain(void);
- ~CMain(void);
- bool Setup(void);
- bool Display(float fTimeDelta);
- bool Input(float timeDelta);
- void Cleanup(void);
- public:
- CModel* _Ship; //模型飞船类
- Camera* _Camera; //摄像机类
- CLight* _Light1; //灯光类1
- CMaterial* _Material1; //材质类1
- CText* _Text1; //文字类1
- CTerrain* _pTerrain; //地形对象1
- //关于骨骼动画的数据
- LPD3DXFRAME _pFrameRoot; //根框架
- D3DXMATRIX* _pBoneMatrices; //骨骼的变化矩阵
- CAllocateHierarchy* _pAllocateHier; //CAllocateHierarchy对象
- LPD3DXANIMATIONCONTROLLER _pAnimController; //动画控制器对象
- CWorldMatrix* _pRoleMatrix; //人物骨骼动画的矩阵
- };
CPP文件
- //
- //D3D主文件类
- //作者:CYM
- //
- #include <d3d9.h>
- #include <d3dx9.h>
- #include <string>
- #include "Main.h"
- #include "D3DFrame.h"
- #ifndef SAFE_DELETE
- #define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
- #endif
- #ifndef SAFE_DELETE_ARRAY
- #define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p); (p)=NULL; } }
- #endif
- #ifndef SAFE_RELEASE
- #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
- #endif
- //全局设置
- IDirect3DDevice9* Device=NULL;
- CD3DFrame D3DFrame;
- CMain Main;
- D3DMATERIAL9 Material1; //材质
- D3DLIGHT9 Light1; //灯光
- D3DXMATRIX V; //视图矩阵
- static D3DXVECTOR3 position( 0.0f, 100.0f, -140.0f ); //摄像机位置
- static D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); //摄像机观察目标
- static D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); //摄像机高度*/
- const int Width = 640; //窗口的宽高
- const int Height = 480;
- CMain::CMain(void)
- {
- _Ship=NULL;
- _Camera=NULL;
- _Light1=NULL;
- _Material1=NULL;
- _Text1=NULL;
- _pTerrain=NULL;
- _pAllocateHier=NULL;
- _pRoleMatrix=NULL;
- }
- CMain::~CMain(void)
- {
- D3DFrame.Delete<CModel*>(_Ship);
- D3DFrame.Delete<Camera*>(_Camera);
- D3DFrame.Delete<CLight*>(_Light1);
- D3DFrame.Delete<CMaterial*>(_Material1);
- D3DFrame.Delete<CText*>(_Text1);
- D3DFrame.Delete<CAllocateHierarchy*>(_pAllocateHier);
- D3DFrame.Delete<CWorldMatrix*>(_pRoleMatrix);
- D3DFrame.Delete<CTerrain*>(_pTerrain);
- }
- bool CMain::Setup()
- {
- _Ship = new CModel(Device); //创建_Ship
- _Camera = new Camera(Camera::AIRCRAFT); //创建_Camera
- _Light1 = new CLight(); //创建_Light1
- _Material1 = new CMaterial(); //创建_Material(暂时无效,看不出效果因为被覆盖)
- _Text1= new CText(Device); //创建_Text1
- _pAllocateHier= new CAllocateHierarchy(); //创建_pAllocateHier
- _pRoleMatrix= new CWorldMatrix(); //创建_pRoleMatrix世界矩阵用于骨骼
- _pTerrain= new CTerrain(Device); //创建_pTerrain地形对象
- //载入模型
- _Ship->LoadMesh("Media//bigship1.x",D3DXMESH_MANAGED);
- //设置摄像机的初始位置
- _Camera->SetPosition(&position);
- _Camera->SetLookAt(&target);
- _Camera->SetUp(&up);
- //设置平行光
- _Light1->SetLightType(D3DLIGHT_DIRECTIONAL);
- //_Light1->SetLightPos(0.0f,3.0f,0.0f);
- _Light1->SetLightDir(1.0f,-1.0f,0.0f);
- _Light1->SetLightDiffuseCol(1.0f,1.0f,1.0f,1.0f);
- _Light1->SetLightSpecularCol(1.0f,1.0f,1.0f,1.0f);
- _Light1->SetLightAmbientCol(1.0f,1.0f,1.0f,1.0f);
- _Light1->GetLightStru(&Light1);
- Device->SetLight(0,&Light1);
- Device->LightEnable(0, true);
- //设置材质(暂时无效,看不出效果因为被覆盖)
- _Material1->SetMaterialDiffuseCol(1.0f,1.0f,1.0f,1.0f);
- _Material1->SetMaterialAmbientCol(1.0f,1.0f,1.0f,1.0f);
- _Material1->SetMaterialEmissiveCol(1.0f,1.0f,1.0f,1.0f);
- _Material1->SetMaterialPower(1.0f);
- _Material1->GetMaterialStru(&Material1);
- //创建字体
- RECT rect={10,10,110,110};
- _Text1->SetDrawRect(&rect);
- _Text1->SetFont(20,10,FW_BOLD);
- _Text1->SetFontCol(D3DXCOLOR(1.0,0.0f,0.0f,1.0f));
- _Text1->CreateFontA();
- //载入X文件
- D3DXLoadMeshHierarchyFromX("Model//warrior.x",D3DXMESH_MANAGED,Device,
- _pAllocateHier,NULL,&_pFrameRoot,&_pAnimController);
- //设置好各级框架的组合矩阵
- SetupBoneMatrixPointers(_pFrameRoot,_pFrameRoot);
- //加载地形的高度图,和纹理
- _pTerrain->LoadTerrain("Media//mountain.raw","Media//desert.bmp");
- //初始化地形
- _pTerrain->InitTerrain(64,64,10.0f,0.4f);
- //设置投影矩阵
- D3DXMATRIX proj;
- D3DXMatrixPerspectiveFovLH(
- &proj,
- D3DX_PI * 0.5f, // 90 - degree
- (float)Width / (float)Height,
- 1.0f,
- 1000.0f);
- Device->SetTransform(D3DTS_PROJECTION, &proj);
- //设置光照
- Device->SetRenderState(D3DRS_LIGHTING, true);
- Device->SetRenderState(D3DRS_AMBIENT, D3DXCOLOR(0.4f, 0.4f, 0.4f, 1.0f));
- Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW/*D3DCULL_NONE*/); // 剔除背面
- Device->SetRenderState(D3DRS_NORMALIZENORMALS, true); //正常的渲染等级
- Device->SetRenderState(D3DRS_SPECULARENABLE, true); //镜面高光
- return true;
- }
- void CMain::Cleanup()
- {
- D3DFrame.Release<IDirect3DDevice9*>(Device);
- D3DXFrameDestroy(_pFrameRoot,_pAllocateHier);
- D3DFrame.Release<LPD3DXANIMATIONCONTROLLER>(_pAnimController);
- }
- bool CMain::Display(float fTimeDelta)
- {
- if( Device )
- {
- //外部输入设备消息处理
- Input(fTimeDelta);
- // Instruct the device to set each pixel on the back buffer black -
- // D3DCLEAR_TARGET: 0x00000000 (black) - and to set each pixel on
- // the depth buffer to a value of 1.0 - D3DCLEAR_ZBUFFER: 1.0f.
- Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DXCOLOR(0.0,0.0,0.0,1.0), 1.0f, 0);
- Device->BeginScene();
- //*灯光效果测试
- //Device->SetLight(0,&Light1);
- //***********
- //*材质类效果测试
- Device->SetMaterial(&Material1);
- //***********
- //*绘制文本
- RECT Rect={10,10,110,110};
- _Text1->SetDrawRect(&Rect);
- _Text1->DrawTextA("dx简单的测试程序(角色已死亡)");
- Rect.top=30;
- _Text1->SetDrawRect(&Rect);
- _Text1->DrawTextA("按1:站立");
- Rect.top=50;
- _Text1->SetDrawRect(&Rect);
- _Text1->DrawTextA(" 2:行走");
- Rect.top=70;
- _Text1->SetDrawRect(&Rect);
- _Text1->DrawTextA(" 3:进攻");
- Rect.top=90;
- _Text1->SetDrawRect(&Rect);
- _Text1->DrawTextA(" 4:受伤");
- Rect.top=110;
- _Text1->SetDrawRect(&Rect);
- _Text1->DrawTextA(" 5:死亡瞬间");
- Rect.top=130;
- _Text1->SetDrawRect(&Rect);
- _Text1->DrawTextA(" 6:死亡");
- //**********
- //绘制飞船
- _Ship->DrawMesh(D3DXVECTOR3(20.0f,10.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f),D3DXVECTOR3(5.0f,5.0f,5.0f));
- //角色的世界矩阵操作变化
- // 控制运动方向
- static FLOAT fMoveAngle = 180.0f;
- static D3DXVECTOR3 vRolePos = D3DXVECTOR3(0.0f, 2.0f, 0.0f);
- if (::GetAsyncKeyState('A') & 0x8000f) fMoveAngle += 0.01f;
- if (::GetAsyncKeyState('D') & 0x8000f) fMoveAngle -= 0.01f;
- // 设置世界变换矩阵
- D3DXMATRIX matRole;
- D3DXMatrixIdentity(&matRole);
- _pRoleMatrix->Scale(0.5f,0.5f,0.5f);
- _pRoleMatrix->Rotate(0,fMoveAngle,0);
- matRole=*_pRoleMatrix->GetMatrix();
- // 更新骨骼动画
- Device->SetTransform(D3DTS_WORLD, &matRole);
- _pAnimController->AdvanceTime(fTimeDelta, NULL);
- UpdateFrameMatrices(_pFrameRoot, &matRole);
- // 绘制蒙皮网格
- DrawFrame(Device, _pFrameRoot);
- //渲染地形(暂时关闭灯光)
- Device->SetRenderState(D3DRS_LIGHTING, false);
- D3DXMATRIX matTerrain;
- //D3DXMatrixIdentity(&matTerrain);
- D3DXMatrixTranslation(&matTerrain,0.0f,-50.0f,0.0f);
- _pTerrain->DrawTerrain(&matTerrain,FALSE);
- Device->SetRenderState(D3DRS_LIGHTING,true);
- //设置视觉矩阵
- _Camera->GetViewMatrix(&V);
- Device->SetTransform(D3DTS_VIEW, &V);
- Device->EndScene();
- // Swap the back and front buffers.
- Device->Present(0, 0, 0, 0);
- }
- return true;
- }
- //外部设备输出处理函数
- bool CMain::Input(float timeDelta)
- {
- // Update: Update the camera.
- //移动摄像机
- if( ::GetAsyncKeyState('W') & 0x8000f )
- _Camera->Walk(0.04f);
- if( ::GetAsyncKeyState('S') & 0x8000f )
- _Camera->Walk(-0.04f);
- if( ::GetAsyncKeyState('A') & 0x8000f )
- _Camera->Strafe(-0.04f);
- if( ::GetAsyncKeyState('D') & 0x8000f )
- _Camera->Strafe(0.04f);
- if( ::GetAsyncKeyState('R') & 0x8000f )
- _Camera->Fly(0.04f);
- if( ::GetAsyncKeyState('F') & 0x8000f )
- _Camera->Fly(-0.04f);
- //旋转摄像机
- if( ::GetAsyncKeyState(VK_UP) & 0x8000f )
- _Camera->Pitch(0.0004f);
- if( ::GetAsyncKeyState(VK_DOWN) & 0x8000f )
- _Camera->Pitch(-0.0004f);
- if( (::GetAsyncKeyState('Q') & 0x8000f) || (::GetAsyncKeyState(VK_LEFT) & 0x8000f) )
- _Camera->Yaw(-0.0008f);
- if( (::GetAsyncKeyState('E') & 0x8000f) || (::GetAsyncKeyState(VK_RIGHT) & 0x8000f) )
- _Camera->Yaw(0.0008f);
- if( ::GetAsyncKeyState('N') & 0x8000f )
- _Camera->Roll(-0.0004f);
- if( ::GetAsyncKeyState('M') & 0x8000f )
- _Camera->Roll(0.0004f);
- // Update the view matrix representing the cameras
- // new position/orientation.
- //控制骨骼动画的播放
- static float fCurrTime=0.0f;
- fCurrTime+=timeDelta;
- LPD3DXANIMATIONSET pAnimationSet = NULL;
- if (::GetAsyncKeyState(0x31) & 0x8000f)
- {
- //fMoveSpeed = 0.0f;
- _pAnimController->GetAnimationSetByName("stay", &pAnimationSet);
- SmoothChangeAnimation(_pAnimController, pAnimationSet, fCurrTime);
- }
- if (::GetAsyncKeyState(0x32) & 0x8000f)
- {
- //fMoveSpeed = 0.8f;
- _pAnimController->GetAnimationSetByName("walk", &pAnimationSet);
- SmoothChangeAnimation(_pAnimController, pAnimationSet, fCurrTime);
- }
- if (::GetAsyncKeyState(0x33) & 0x8000f)
- {
- //fMoveSpeed = 0.0f;
- _pAnimController->GetAnimationSetByName("attack", &pAnimationSet);
- SmoothChangeAnimation(_pAnimController, pAnimationSet, fCurrTime);
- }
- if (::GetAsyncKeyState(0x34) & 0x8000f)
- {
- //fMoveSpeed = 0.0f;
- _pAnimController->GetAnimationSetByName("behit", &pAnimationSet);
- SmoothChangeAnimation(_pAnimController, pAnimationSet, fCurrTime);
- }
- if (::GetAsyncKeyState(0x35) & 0x8000f)
- {
- //fMoveSpeed = 0.0f;
- _pAnimController->GetAnimationSetByName("dieing", &pAnimationSet);
- SmoothChangeAnimation(_pAnimController, pAnimationSet, fCurrTime);
- }
- if (::GetAsyncKeyState(0x36) & 0x8000f)
- {
- //fMoveSpeed = 0.0f;
- _pAnimController->GetAnimationSetByName("dead", &pAnimationSet);
- SmoothChangeAnimation(_pAnimController, pAnimationSet, fCurrTime);
- }
- SAFE_RELEASE(pAnimationSet);
- return true;
- }
- //
- //应用程序入口函数
- //
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
- int WINAPI WinMain(HINSTANCE hinstance,
- HINSTANCE prevInstance,
- PSTR cmdLine,
- int showCmd)
- {
- //定义和初始化窗口类
- WNDCLASS wc;
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = WndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hinstance;
- wc.hIcon = LoadIcon(0, IDI_APPLICATION);
- wc.hCursor = LoadCursor(0, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- wc.lpszMenuName = 0;
- wc.lpszClassName = "Direct3D9App";
- //初始化窗口
- if(!D3DFrame.InitD3D(wc,
- 640, 480, true, D3DDEVTYPE_HAL, &Device))
- {
- ::MessageBox(0, "InitD3D() - FAILED", 0, 0);
- return 0;
- }
- //加载需要用到的资源
- if(!Main.Setup())
- {
- ::MessageBox(0, "Setup() - FAILED", 0, 0);
- return 0;
- }
- //进入消息循环
- MSG msg;
- ZeroMemory(&msg, sizeof(msg));
- while(msg.message != WM_QUIT)
- {
- if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else
- {
- static FLOAT fLastTime = (float)::timeGetTime();
- static FLOAT fCurrTime = (float)::timeGetTime();
- static FLOAT fTimeDelta = 0.0f;
- fCurrTime = (float)::timeGetTime();
- fTimeDelta = (fCurrTime - fLastTime) / 1000.0f;
- fLastTime = fCurrTime;
- Main.Display(fTimeDelta); //渲染要显示的动画
- }
- }
- //结束释放资源
- Main.Cleanup();
- return 0;
- }
- //
- // 消息响应函数
- //
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch( msg )
- {
- case WM_DESTROY:
- ::PostQuitMessage(0);
- break;
- case WM_KEYDOWN:
- if( wParam == VK_ESCAPE )
- ::DestroyWindow(hwnd);
- break;
- }
- return ::DefWindowProc(hwnd, msg, wParam, lParam);
- }
- DX骨骼动画,地形简单的演示
- DirectX骨骼动画,地形,级球形天空体的演示
- DX 骨骼动画
- DX 骨骼动画
- 简单的骨骼动画、骨骼动画理论
- 最新骨骼动画的演示程序。
- dx雾特效,旋转的立方体,文字动画显示,底面简单演示
- 简单骨骼动画编写
- DirectX骨骼动画演示,和渐进网格和增进网格的演示
- Ogre的骨骼动画
- 骨骼动画的解释
- 骨骼动画的实现
- 骨骼动画的研究
- 骨骼动画的加载
- cocos2dx 新版本骨骼动画.csb文件的简单使用
- Android简单而强大的骨骼动画视图
- 我的骨骼动画系统 。
- 蒙皮骨骼动画的理解
- java-javaweb-dao案例3
- 搞笑dota
- 数据结构(C#)-排序
- 生活琐事
- 万一感情有了变故,要能够承受。
- DX骨骼动画,地形简单的演示
- may 4, 2011
- 配置Spring2.0声明式事务
- Oracle用户、权限、角色管理
- 原创:spring入门介绍第一讲
- Hibernate主键生成策略
- 我们这些渐渐的习惯
- 题
- Struts2小结