|
继上一篇加入摄像机后,接下来加入输入控制功能,控制摄像机的自由移动。
- #include "Input.h"
- bool CInput::Init(HWND hwnd)
- {
- //窗口句柄
- m_Hwnd=hwnd;
- //创建dxinput接口对象
- if (FAILED(DirectInput8Create(GetModuleHandle(NULL),DIRECTINPUT_VERSION,IID_IDirectInput8,(void**)&m_Input,NULL)))
- {
- return false;
- }
- //创建键盘对象
- if (FAILED(m_Input->CreateDevice(GUID_SysKeyboard,&m_KeyBoard,NULL)))
- {
- return false;
- }
- if (m_KeyBoard==NULL)
- {
- return false;
- }
- if (FAILED(m_KeyBoard->SetDataFormat(&c_dfDIKeyboard)))
- {
- return false;
- }
- //设置数据格式
- m_KeyBoard->SetCooperativeLevel(m_Hwnd,DISCL_FOREGROUND|DISCL_NONEXCLUSIVE); //设置协作级别
- //创建鼠标对象
- if (FAILED(m_Input->CreateDevice(GUID_SysMouse,&m_Mouse,NULL)))
- {
- return false;
- }
- m_Mouse->SetDataFormat(&c_dfDIMouse); //设置鼠标接受数据格式
- m_Mouse->SetCooperativeLevel(m_Hwnd,DISCL_FOREGROUND|DISCL_NONEXCLUSIVE); //设置非独占模式
- return true;
- }
- void CInput::Update()
- {
- m_KeyBoard->Acquire();
- m_KeyBoard->Poll();
- m_Mouse->Acquire();
- m_Mouse->Poll();
- m_KeyBoard->GetDeviceState(sizeof(keys),(LPVOID)&keys);
- m_Mouse->GetDeviceState(sizeof(m_MouseState),(LPVOID)&m_MouseState);
- }
- bool CInput::IsKeyDown( char key )
- {
- if (keys[key] & 0x80)
- {
- return true;
- }
- return false;
- }
- CInput::CInput()
- {
- m_Hwnd=NULL;
- m_Input=NULL;
- m_KeyBoard=NULL;
- m_Mouse=NULL;
- }
- CInput::~CInput()
- {
- }
- void CInput::GetMouseMoveState( long* plX,long* plY )
- {
- *plX=m_MouseState.lX;
- *plY=m_MouseState.lY;
- }
复制代码- #include "Engine.h"
- #define WIN_CLASS "dx01"//窗体类名
- #define WIN_NAME "3D摄像机自动导航" //窗体标题
- #define WIN_WIDTH 800 //窗体宽度
- #define WIN_HEIGHT 600 //窗体高度
- #define WIN_FULLSCREEN false //是否全屏
- Engine engine; //引擎对象
- Model pModel; //模型
- LPDIRECT3DDEVICE9 dev=NULL;
- CCamera* g_pCamera=NULL; //摄像机
- CInput* g_pInput=NULL; //输入设备
- float angle=0.0f;
- UINT uStep=1;
- float RotationSpeed=0.005f;
- float ForwardSpeed=1.0f;
- //游戏初始化
- bool GameInit(HWND hwnd);
- //游戏循环
- void GameLoop(HWND hwnd);
- //更新
- void GameUpdate(HWND hwnd);
- //游戏结束
- void GameEnd();
- //Windows消息回调函数
- LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
- {
- switch(msg)
- {
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_KEYUP:
- if (wparam==VK_ESCAPE)
- {
- PostQuitMessage(0);
- }
- break;
- }
- return DefWindowProc(hwnd,msg,wparam,lparam);
- }
- //Window入口函数
- int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
- {
- //设置窗口结构类属性
- WNDCLASSEX wc={sizeof(WNDCLASSEX),CS_CLASSDC,WndProc,0,0,hInstance,NULL,NULL,NULL,NULL,WIN_CLASS,NULL};
- //注册窗口类型
- RegisterClassEx(&wc);
- //创建窗口
- HWND hwnd= CreateWindow(WIN_CLASS,WIN_NAME,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,WIN_WIDTH,WIN_HEIGHT,NULL,NULL,hInstance,NULL);
- if (hwnd)
- {
- ShowWindow(hwnd,nShowCmd); //显示窗口
- UpdateWindow(hwnd); //更新窗口
- if (GameInit(hwnd))
- {
- MSG msg;
- ZeroMemory(&msg,sizeof(msg));
- //窗口消息循环
- while (msg.message!=WM_QUIT)
- {
- if (PeekMessage(&msg,NULL,0U,0U,PM_REMOVE))
- {
- TranslateMessage(&msg); //转换消息
- DispatchMessage(&msg); //分派消息
- }
- else
- {
- GameLoop(hwnd);
- }
- }
- GameEnd();
- }
- }
- UnregisterClass(WIN_CLASS,wc.hInstance);
- return 0;
- }
- //Game循环
- void GameLoop(HWND hwnd)
- {
- engine.ClearScreen();
- GameUpdate(hwnd);
- engine.StartRender(); //开始渲染
- engine.DrawModel(&pModel);
- engine.EndRender(); //结束渲染
- }
- //Game开始
- bool GameInit(HWND hwnd)
- {
- if (!engine.Init(hwnd,WIN_WIDTH,WIN_HEIGHT))
- {
- return false;
- }
- if (!engine.LoadModel("edificio1.X",&pModel))
- {
- ShowMessage("载入模型失败!");
- return false;
- }
- dev= engine.GetDev();
- dev->SetRenderState(D3DRS_LIGHTING,false); //关闭光照
- dev->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE); //背面消隐
- g_pCamera=new CCamera(dev);
- g_pCamera->SetCameraPosition(&D3DXVECTOR3(0.0f,50.0f,-300.0f)); //设置初始摄像机位置
- g_pCamera->SetLookPosition(&D3DXVECTOR3(0.0f,50.0f,0.0f)); //设置初始观察点位置
- g_pCamera->SetViewMatrix(); //初始化取景变换矩阵
- g_pCamera->SetProjMatrix(WIN_WIDTH,WIN_HEIGHT); //初始化投影变换矩阵
- g_pInput=new CInput();
- if (!g_pInput->Init(hwnd))
- {
- return false;
- }
- return true;
- }
- //GameOver
- void GameEnd()
- {
- //模型释放
- pModel.Release();
- //关闭引擎
- engine.Shutdown();
- g_pCamera->~CCamera();
- SAFE_DELETE(g_pCamera);
- }
- void GameUpdate(HWND hwnd)
- {
- g_pInput->Update(); //更新键盘鼠标缓冲区数据
- if (g_pInput->IsKeyDown(DIK_W)) g_pCamera->MoveAlongLook(ForwardSpeed); //向前
- if(g_pInput->IsKeyDown(DIK_S)) g_pCamera->MoveAlongLook(-ForwardSpeed); //向后
- if(g_pInput->IsKeyDown(DIK_A)) g_pCamera->MoveAlongRight(-ForwardSpeed); //向左
- if(g_pInput->IsKeyDown(DIK_D)) g_pCamera->MoveAlongRight(ForwardSpeed); //向右
- long x,y;
- g_pInput->GetMouseMoveState(&x,&y); //获取鼠标数据
- g_pCamera->RotationUp(x*0.001f); //左右旋转
- g_pCamera->RotationRight(y*0.001f); //上下旋转
- D3DXMATRIX MatrixView;
- g_pCamera->CalcViewMatrix(&MatrixView);
- dev->SetTransform(D3DTS_VIEW,&MatrixView);
- //控制鼠标坐标
- POINT pos1;
- pos1.x=WIN_WIDTH/2;
- pos1.y=WIN_HEIGHT/2;
- SetCursorPos(pos1.x,pos1.y);
- }
复制代码
运行截图:
同步博客:http://blog.csdn.net/qq578023708/article/details/46536247
交流群:87341183
源码:
dx01.rar
(937.53 KB, 下载次数: 1)
|
|