有了前面的狀態機,我們就可以很方便的擴展出自己的游戲場景狀態的類。
現在我們要寫一個RunState的類,這個就是游戲的主場景所在的地方,從MenuState可以跳轉過來。
一、加入第三人稱控制器
對於人物的控制,新版的OGRE已經給出了一個示例,就是那個Sample_Charater。
里邊是對一個OGRE新模型的控制,操作起來感覺不錯,就是向前跑動的時候沒法看身后的東西,得停下來,Camera才會回到自由模式。
這個地方主要用到兩個類:
SdkCameraMan:封裝了OGRE的像機,可以比較自由的控制像機。
SinbadCharacterController:對於Sinbad的角色的控制以及動畫的播放。
只要聲明並定義好這兩個類就能快速的實現第三人稱的控制。
CRunState.h
class CRunState : public CGameState
{
public:
CRunState();
DECLARE_GAMESTATE_CLASS(CRunState)
void Enter();
void CreateScene();
void Exit();
bool Pause();
void Resume();
bool keyPressed(const OIS::KeyEvent &evt);
bool keyReleased(const OIS::KeyEvent &evt);
bool mouseMoved( const OIS::MouseEvent &evt);
bool mousePressed( const OIS::MouseEvent &evt, OIS::MouseButtonID id );
bool mouseReleased( const OIS::MouseEvent &evt, OIS::MouseButtonID id );
void Update(double timeSinceLastFrame);
protected:
OgreBites::SdkCameraMan* m_pCameraMan;
bool m_bIsQuit;
SinbadCharacterController* m_pChara;
};
在Enter函數中初始化:
void CRunState::Enter()
{
COgreFramework::getSingletonPtr()->m_pLog->logMessage("Entering RunState.h.");
m_pSceneMgr = COgreFramework::getSingletonPtr()->m_pRoot->createSceneManager(ST_GENERIC, "RunSceneMgr");
m_pSceneMgr->setAmbientLight(Ogre::ColourValue(0.3f, 0.3f, 0.3f));
m_pCamera = m_pSceneMgr->createCamera("MainCam");
m_pCamera->setNearClipDistance(5);
m_pCamera->setAspectRatio(Real(COgreFramework::getSingletonPtr()->m_pViewport->getActualWidth())/
Real(COgreFramework::getSingletonPtr()->m_pViewport->getActualHeight()));
COgreFramework::getSingletonPtr()->m_pViewport->setCamera(m_pCamera);
m_pCameraMan = new OgreBites::SdkCameraMan(m_pCamera);
m_pCameraMan->setStyle(OgreBites::CS_MANUAL);
m_pChara = new SinbadCharacterController(m_pCamera);
COgreFramework::getSingletonPtr()->m_pTrayMgr->destroyAllWidgets();
COgreFramework::getSingletonPtr()->m_pTrayMgr->hideCursor();
CreateScene();
}
在那些鍵盤和鼠標消息中響應人物和攝像機的事件。最后要在Update中更新動畫。
void CRunState::Update(double timeSinceLastFrame)
{
if (m_bIsQuit)
{
Shutdown();
return;
}
if (m_pChara)
m_pChara->addTime(timeSinceLastFrame/1000); // from miliseconds to seconds
}
這邊注意,因為我們前面用的是毫秒,這邊動畫更新需要的是秒為單位,所以要除,否則畫面不會動。
二、載入.Scene場景文件
如果要用代碼加入物體,不僅麻煩(得算坐標),而且不易修改(修改后得重新編譯)。於是就有了場景文件的出現,而且也有了開源的場景編輯器(Ogitor)。
Ogitor是一個基於QT的OGRE場景的編輯器,可以到官網上下載:
http://www.ogitor.org/HomePage
在我們的工程中要用到的就是三個文件(在Ogitor的安裝目錄的/SampleApp_Source/下):
DotSceneLoader.h
DotSceneLoader.cpp
rapidxml.h
把他們拷進我們的工程里,我們將要使用他們來載入場景。
這時,會發現程序有錯,DotSceneLoader.cpp里要引用到PagedGeometry的一些東西,這是一個用來植樹造林的工具,我們也可以去下載。
http://www.ogre3d.org/tikiwiki/PagedGeometry+Engine
官方沒有提供已經編譯好的,所以需要自己編譯,要配置OGRE的頭文件和庫的路徑。
新的OGRE中的material的一個方法改了,所以編譯會有錯誤:
需要將:
bestTechnique = material->getBestTechnique(material->getLodIndexSquaredDepth(parent->minDistanceSquared));
轉換為:
bestTechnique = getLodIndex(parent->minDistanceSquared)
這樣就可以編譯出LIB庫了。
回到我們的工程:
在CreateScene函數中載入場景:
void CRunState::CreateScene()
{
DotSceneLoader* pDotSceneLoader = new DotSceneLoader();
pDotSceneLoader->parseDotScene("CubeScene.xml", "General", m_pSceneMgr, m_pSceneMgr->getRootSceneNode());
delete pDotSceneLoader;
// add a bright light above the scene
Light* light = m_pSceneMgr->createLight();
light->setType(Light::LT_POINT);
light->setPosition(-10, 40, 20);
light->setSpecularColour(ColourValue::White);
// create a floor mesh resource
MeshManager::getSingleton().createPlane("floor", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
Plane(Vector3::UNIT_Y, 0), 100, 100, 10, 10, true, 1, 10, 10, Vector3::UNIT_Z);
// create a floor entity, give it a material, and place it at the origin
Entity* floor = m_pSceneMgr->createEntity("Floor", "floor");
floor->setMaterialName("Examples/Rockwall");
floor->setCastShadows(false);
m_pSceneMgr->getRootSceneNode()->attachObject(floor);
}
這邊要注意在資源文件(resource.cfg)中加入CubeScene.xml和Cube模型的資源路徑,否則OGRE不會載入他們,就找不到這些資源。
最后就可以看到結果了:
嘗試着載入了Ogitor自帶的Sample文件,SampleScene3.scene。注意要把資源路徑配好。
人縮小一倍還比房子大。。。有點像綠巨人,人物的位置要根據地形高低來設置好。