+++++++++++++++++++++++++++++++++++++++++++++
+++++++++ 轻松创建天龙八部的场景 ++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++
1. 从这里开始:
①需要的资源:
天龙八部的资源:
一个动态库文件:EditableTerrainManager_d.lib
一个静态库文件:TinyXML.lib 这个是用来对类似XML的文件进行数据操作
相关的头文件:
下载路径:
②创建一个控制台的项目TTLB.
编译成功后,会出出现黑色屏幕,则说明成功。
代码如下:
TutorialApp.h
#ifndef _TuorialApp_H #define _TuorialApp_H #include using namespace std; class TutorialApp : public ExampleApplication { protected: public: TutorialApp(); ~TutorialApp(); protected: void chooseSceneManager(void); void createScene(void); }; #endif
TutorialApp.cpp
#include "TutorialApp.h" TutorialApp::TutorialApp() { } TutorialApp::~TutorialApp() { } void TutorialApp::chooseSceneManager(void) { mSceneMgr = mRoot->createSceneManager("OctreeSceneManager", "ETInstance"); void TutorialApp::createScene(void) { }
Main.cpp
#include "windows.h" #include "TutorialApp.h" #include int main(int argc, char **argv) { // Create application object TutorialApp app; try { app.go(); } catch( Ogre::Exception& e ) { #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 MessageBoxA( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL); #else std::cerr << "An exception has occured: " << e.getFullDescription().c_str() << std::endl; #endif } return 0; }
2. 载入场景模型:
① 首先需要修改资源的路径:
参考网站:http://apps.hi.baidu.com/share/detail/6166638
我在void TutorialApp::createScene(void) 程序中直接用了下面的来设置路径。
(我把资源路径设为,Debug中.exe所在目录(即工作目录)的前两级目录中的一个名叫media2的文件夹,然后是里面的TTLB,再里面的……..
// 支持中文路径,这个一定要加,不然会报错
setlocale( LC_CTYPE, "" );
//增加资源路径 E:/TLBB/天龙八部场景Demo/TTLBSceneDemo/media/TTLB/brush
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../../media2/TTLB/model","FileSystem","test");
Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../../media2/TTLB/brush","FileSystem","test");
②代码:
TutorialApp.h
#ifndef _TuorialApp_H #define _TuorialApp_H #include #include #include #include "tinyxml.h" #include "tinystr.h" #include using namespace std; class TutorialApp : public ExampleApplication { protected: public: TutorialApp(); ~TutorialApp(); void chooseSceneManager(void); void createScene(void); void createTLBBSence( const char* sceneName); ///下面是用到的字符转换工具 static bool IsStrEqual( const char* str1, const char* str2 ) { return ( strcmp( str1, str2) == 0 )?true:false; } char * UnicodeToANSI( const wchar_t* str ) { char* result; int textlen; textlen = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL ); result =(char *)malloc((textlen+1)*sizeof(char)); memset( result, 0, sizeof(char) * ( textlen + 1 ) ); WideCharToMultiByte( CP_ACP, 0, str, -1, result, textlen, NULL, NULL ); return result; } wchar_t * UTF8ToUnicode( const char* str ) { int textlen ; wchar_t * result; textlen = MultiByteToWideChar( CP_UTF8, 0, str,-1, NULL,0 ); result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t)); memset(result,0,(textlen+1)*sizeof(wchar_t)); MultiByteToWideChar(CP_UTF8, 0,str,-1,(LPWSTR)result,textlen ); return result; } char* UTF8ToANSI(const char* str) { wchar_t* temp = UTF8ToUnicode(str); char* res = UnicodeToANSI(temp); delete []temp; return res; } protected: }; #endif
TutorialApp.cpp
#include "TutorialApp.h" TutorialApp::TutorialApp() { } TutorialApp::~TutorialApp() { } void TutorialApp::chooseSceneManager(void) { mSceneMgr = mRoot->createSceneManager("OctreeSceneManager", "ETInstance"); //ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); } void TutorialApp::createScene(void) { // 支持中文路径,这个一定要加,不然会报错 setlocale( LC_CTYPE, "" ); //增加资源路径 E:/TLBB/天龙八部场景Demo/TTLBSceneDemo/media/TTLB/brush Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../../media2/TTLB/model","FileSystem","test"); Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../../media2/TTLB/brush","FileSystem","test"); createTLBBSence( "../../media2/TTLB/scene/shaolin.Scene"); } void TutorialApp::createTLBBSence( const char* sceneName) { TiXmlDocument sceneDoc; bool bLoadSuccess = sceneDoc.LoadFile( sceneName ); //载入文件失败的提示 if( !bLoadSuccess ) { OGRE_EXCEPT(Exception::ERR_INVALID_STATE, "load scene file failed", ""); return; } const char* strTemp; const char* sValue; // Scene TiXmlElement* root = sceneDoc.FirstChildElement(); // terrain TiXmlElement* element = root->FirstChildElement( "Terrain" ); strTemp = element->Attribute("filename"); // // load the terrain and insert into scene. // ETM::TerrainManager will do the insertion while( 1 ) { element = element->NextSiblingElement();//
[注:]但这里面需要设置为多使用多字节字符集,且必须加上静态库TinyXML.lib并设置好相应的路径,还有相应的头文件路径。
③结果:
出现了少林寺的场景模型,没有地图纹理等。