加载一个mesh三维模型文件,竟然弄了好几天。原来没有添加有关Viewport的代码,当前程序仅仅作为一个坑爹的参考学习笔记,后面会改进的。今天问了旭华兄,他提供了一个学习Ogre的官方地址。之后学习得专注这个地址吧,之前的Ogre3D pro概念太多。一边学习,一边记录!
修改的resources_d.cfg文件,只修改了FileSystem=models和FileSystem=materials/scripts,将materials\scripts下的Ogre.material和models下的ogrehead.mesh拷贝到项目之下(项目之下,它们依旧在materials/scripts文件夹下,以及models文件夹里)。[不添加到项目里面,会发生错误。目前resources_d.cfg文件的载入方式还不会(后面的参考文章中会说),这些天得学会使用!]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# Resources required by the sample browser and most samples. [Essential] Zip=../../media/packs/SdkTrays.zip Zip=../../media/packs/profiler.zip FileSystem=../../media/thumbnails # Common sample resources needed by many of the samples. # Rarely used resources should be separately loaded by the # samples which require them. [Popular] FileSystem=../../media/fonts FileSystem=../../media/materials/programs FileSystem=materials/scripts FileSystem=../../media/materials/textures FileSystem=../../media/materials/textures/nvidia FileSystem=models FileSystem=../../media/particle FileSystem=../../media/DeferredShadingMedia FileSystem=../../media/PCZAppMedia FileSystem=../../media/RTShaderLib FileSystem=../../media/RTShaderLib/materials FileSystem=../../media/materials/scripts/SSAO FileSystem=../../media/materials/textures/SSAO FileSystem=../../media/volumeTerrain Zip=../../media/packs/cubemap.zip Zip=../../media/packs/cubemapsJS.zip Zip=../../media/packs/dragon.zip Zip=../../media/packs/fresneldemo.zip Zip=../../media/packs/ogretestmap.zip Zip=../../media/packs/ogredance.zip Zip=../../media/packs/Sinbad.zip Zip=../../media/packs/skybox.zip Zip=../../media/volumeTerrain/volumeTerrainBig.zip [General] FileSystem=../../media # Materials for visual tests [Tests] FileSystem=../../media/../../Tests/Media |
以下是项目代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
// Ogre_Pure.cpp : 定义控制台应用程序的入口点。 // /************************ 场景管理器(SceneManager) 摄像机(Camera) *************************/ #include "stdafx.h" #include <iostream> #include <Ogre.h> using namespace Ogre; using namespace std; int main() { Root *root = new Root("plugins_d.cfg"); if(!root->restoreConfig())//如果没有ogre.cfg文件时 { bool rtn = root->showConfigDialog(); root->saveConfig(); } //root->loadPlugin("RenderSystem_GL_d");//使用插件管理的loadPlugin()方法来载入所需的API,(我使用了debug版本) String rName("OpenGL Rendering Subsystem"); RenderSystemList rList = root->getAvailableRenderers(); RenderSystemList::iterator it = rList.begin(); RenderSystem *rSys = 0; while(it != rList.end()) { rSys = *(it++); if(rSys->getName() == rName) { //设置渲染器,并结束循环 root->setRenderSystem(rSys); break; } } //true---是(否)自动建立一个渲染窗口给用户使用 //"My Render Window"---程序标题,默认使用"OGRE Render Window" root->initialise(false,"My Render Window");//完成Root的初始化,建立相应的渲染窗口 //得到自动创建的渲染窗口实例的指针 NameValuePairList params; params["left"] = "0"; params["top"] = "0"; params["title"] = "Alternate Window Title"; RenderWindow *window = rSys->_createRenderWindow(//函数名字竟然有下划线,囧,和教材果然不一样 "MainWindow",//窗口的名字 800,//窗口的宽度(像素) 600,//窗口的高度(像素) false,//是否全屏显示 ¶ms);//其他参数,使用默认值 //上面的代码创建了一个在屏幕的左上角现实的窗口,名称为“MainWindow”而标题是 //“Alternate Window Title”。 root->setRenderSystem(rSys); SceneManager *sceneMgr = root->createSceneManager(ST_GENERIC,"MySceneManager"); //mSceneMgr = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC); //我们在这里构建名称为―MainCam‖的摄像机。 sceneMgr->setAmbientLight(ColourValue(0.7, 0.7, 0.7)); sceneMgr->createLight()->setPosition(0, 0, 0); //载入模型 //下面两句成功了,说明resources_d.cfg失败了 root->addResourceLocation("D:/Ogre/MyProjects/Ogre_Pure/Ogre_Pure/models","FileSystem"); root->addResourceLocation("D:/Ogre/MyProjects/Ogre_Pure/Ogre_Pure/materials/scripts","FileSystem"); ResourceGroupManager::getSingleton().initialiseAllResourceGroups();//必须有这句话 sceneMgr->getRootSceneNode()->attachObject(sceneMgr->createEntity("Head", "ogrehead.mesh")); //ent->getMesh()->setAutoBuildEdgeLists(true); //ent->setMaterialName("Examples/EnvMappedRustySteel"); //sceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent); Camera *camera = sceneMgr->createCamera("MainCam"); camera->setFixedYawAxis(true); camera->setPosition(sceneMgr->getRootSceneNode()->_getDerivedPosition()); camera->setOrientation(sceneMgr->getRootSceneNode()->_getDerivedOrientation()); camera->yaw(Radian(0)); camera->pitch(Radian(-0)); camera->moveRelative(Ogre::Vector3(0, 0, 150)); Viewport * vp = window->addViewport(camera); vp->setBackgroundColour(ColourValue(0, 0, 0)); root->startRendering(); //最后就出现一个黑色窗口吧~ return 0; } |
运行结果截图:
参考:
Ogre程序加载mesh文件原理(解释了resources.cfg的作用和重要性)