轻松创建天龙八部的场景
阅读原文时间:2021年04月25日阅读:1

+++++++++++++++++++++++++++++++++++++++++++++

+++++++++ 轻松创建天龙八部的场景 ++++++++++++

+++++++++++++++++++++++++++++++++++++++++++++

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();// strTemp = element->Attribute( "type" ); //Enviroment if( IsStrEqual( strTemp, "StaticEntity" ) ) //当遇到第一个 就跳出该循环 break; TiXmlElement* propriety = element->FirstChildElement( "Property" ); // //设置灯光 if( IsStrEqual( strTemp, "Light" ) ) //type="Light" { const char* lightName = element->Attribute( "name"); // Ogre::Light* pLight = mSceneMgr->createLight( lightName ); Ogre::SceneNode* pLightNode = mSceneMgr->getRootSceneNode()->createChildSceneNode( lightName ); // add light to scene float r,g,b,x,y,z; while( propriety ) { strTemp = propriety->Attribute( "name" ); // sValue = propriety->Attribute( "value" ); if( IsStrEqual( "type", strTemp ) ) { //设置灯光的类型 if( IsStrEqual( sValue, "directional") ) pLight->setType( Ogre::Light::LT_DIRECTIONAL ); else if( IsStrEqual( sValue, "point") ) pLight->setType( Ogre::Light::LT_POINT ) ; else if( IsStrEqual( sValue, "spotlight") ) pLight->setType( Ogre::Light::LT_SPOTLIGHT ); else assert( 0 ); //error } else if( IsStrEqual("diffuse", strTemp ) ) { //设置此渲染通路中漫反射系数 数值的取值范围在0.0到1.0之间。 sscanf( sValue, "%f %f %f", &r,&g,&b ); pLight->setDiffuseColour( r,g,b ); } else if( IsStrEqual( "specular", strTemp ) ) { //设置此渲染通路中镜面反射颜色的反射系数 数值的取值范围在0.0到1.0之间 sscanf( sValue, "%f %f %f", &r,&g,&b ); pLight->setSpecularColour( r,g,b ); } else if( IsStrEqual( "position", strTemp ) ) { //设置光源的位置 sscanf( sValue, "%f %f %f", &x, &y, &z ); pLight->setPosition( x,y,z ); } else if( IsStrEqual( "direction", strTemp ) ) { //设置光源的方向 sscanf( sValue, "%f %f %f", &x,&y,&z ); pLight->setDirection( x,y,z ); } else if( IsStrEqual( "cast shadows", strTemp ) ) { //设置是否投影 if( IsStrEqual( sValue, "true" ) ){ pLight->setCastShadows( true ); } else pLight->setCastShadows( false ); } else assert(0); propriety = propriety->NextSiblingElement(); } //pLightNode->attachObject( pLight ); //pLight->setVisible( false ); }//设置环境 else if( IsStrEqual( strTemp, "Enviroment" ) ) { // add fog to scene Ogre::ColourValue ambient; Ogre::ColourValue color; Ogre::FogMode mode; int linerstart =0,linerend=0; while( propriety ) { strTemp = propriety->Attribute( "name" ); sValue = propriety->Attribute( "value" ); //设置此渲染通路中周围环境的反射系数,数值的取值范围在0.0到1.0之间 if( IsStrEqual( "ambient", strTemp ) ) { sscanf( sValue, "%f %f %f", &ambient.r,&ambient.g,&ambient.b ); }//设置是否雾化模式 else if( IsStrEqual( "fog.mode", strTemp ) ) { //设置雾的类型,此中是线性雾 if( IsStrEqual( sValue, "linear") ) mode = FOG_LINEAR; else if( IsStrEqual( sValue, "exp") ) mode = FOG_EXP; else if( IsStrEqual( sValue, "exp2" ) ) mode = FOG_EXP2; else mode = FOG_NONE; } else if( IsStrEqual( "fog.colour", strTemp ) ) { //设置雾的颜色 sscanf( sValue, "%f %f %f", &color.r, &color.g, &color.b ); } //雾这浓范围的起点 else if( IsStrEqual("fog.linear start", strTemp )) { linerstart = _ttoi( sValue ); } //雾变浓范围的终点 else if( IsStrEqual("fog.linear end", strTemp ) ) { linerend = _ttoi( sValue ); } else assert(0); if (propriety) { propriety = propriety->NextSiblingElement(); } } // mSceneMgr->setAmbientLight( ambient ) ; //设置雾 mSceneMgr->setFog( mode, color, 0.001, linerstart, linerend ); } //设置天空盒 else if( IsStrEqual( strTemp, "SkyDome" ) ) { // add skydome while( propriety ) { strTemp = propriety->Attribute( "name" ); sValue = propriety->Attribute( "value" ); if( IsStrEqual( "material", strTemp ) ) { //mSceneMgr->setSkyDome( true, sValue ); } else {assert( 0 ); } propriety = propriety->NextSiblingElement(); } } else{ assert(0); // error! } } //设置场景中静态的实体 static int StaticEntityIndex = 0; while( element ) { strTemp = element->Attribute( "type" ); { if( IsStrEqual( "StaticEntity", strTemp ) ) //静态实体 { SceneNode* pSsceneNode = NULL; Entity* pEntity = NULL; float w,x,y,z; TiXmlElement* propriety = element->FirstChildElement( "Property" ); std::cout<<"实体个数: "<Attribute( "name" ); sValue = propriety->Attribute( "value" ); if( IsStrEqual("mesh name", strTemp ) ) { sValue = UTF8ToANSI(sValue); pSsceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode( sValue + StringConverter::toString( StaticEntityIndex++ ) ); pEntity = mSceneMgr->createEntity( sValue + StringConverter::toString( StaticEntityIndex++ ), sValue ); pSsceneNode->attachObject( pEntity ); delete []sValue; } else if( IsStrEqual( "position", strTemp ) ) { sscanf( sValue, "%f %f %f", &x, &y, &z ); pSsceneNode->setPosition( x, y, z ); } else if( IsStrEqual( "orientation", strTemp ) ) { sscanf( sValue, "%f %f %f %f", &w, &x, &y, &z ); pSsceneNode->setOrientation( w,x,y,z ); } else if( IsStrEqual( "scale", strTemp ) ) { sscanf( sValue, "%f %f %f", &x, &y, &z ); pSsceneNode->setScale( x,y,z ); } else if( IsStrEqual( "receive shadows", strTemp ) ) { } else if( IsStrEqual( "receive decals", strTemp ) ) { } else assert(0); if (propriety) { propriety = propriety->NextSiblingElement(); } if (!propriety) { break; } // insert the static entity into the scene } } else if( IsStrEqual( "Model", strTemp ) ) { } else if( IsStrEqual( "ParticleSystem", strTemp ) ) { } else if( IsStrEqual( "TerrainLiquid", strTemp ) ) { } else if( IsStrEqual( "Effect", strTemp ) ) { } else assert(0); } if (element) { element = element->NextSiblingElement(); } if (!element) { return; } } }

[注:]但这里面需要设置为多使用多字节字符集,且必须加上静态库TinyXML.lib并设置好相应的路径,还有相应的头文件路径。

结果:

出现了少林寺的场景模型,没有地图纹理等。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章