说明:
*介绍从.irr文件加载场景节点
*三步:
*加载.irr文件
*加载场景节点
*添加碰撞检测,绑定到照相机
详情请看源码及其注释.
源码及注释:
#include <iostream>
#include <irrlicht.h>
#include "driverChoice.h"
using namespace irr;
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "irrlicht.lib")
//#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif
int main(int argc, char** argv)
{
video::E_DRIVER_TYPE driverType = driverChoiceConsole();
IrrlichtDevice *device =
createDevice(driverType, core::dimension2d<u32>(720, 455), 32,
false, true, false, 0);
if (!device)
return 1;
device->setWindowCaption(L"15.LoadIrrFile");
video::IVideoDriver *driver = device->getVideoDriver();
scene::ISceneManager *smgr = device->getSceneManager();
gui::IGUIEnvironment *guiev = device->getGUIEnvironment();
//1.准备
scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS(0, 50, 0.1);
//创建一个meta三角形选择器用于保存一些三角形选择器
//IMetaTriangleSelector多个选择器的集合
scene::IMetaTriangleSelector* meta = smgr->createMetaTriangleSelector();
/*
*2.加载.irr
*/
if (argc > 1)
smgr->loadScene(argv[1]);
else
smgr->loadScene("../media/aexample-mytest.irr");//注意名字,看情况更改
/*
*2.为场景节点添加三角形拾取器
*根据节点类型创建不同拾取器
*/
//获取到所有的场景节点
core::array<scene::ISceneNode*> nodes;
smgr->getSceneNodesFromType(scene::ESNT_ANY, nodes);
for (u32 i = 0; i < nodes.size(); ++i)
{
scene::ISceneNode* node = nodes[i];
scene::ITriangleSelector* selector = 0;
switch (node->getType())
{
case scene::ESNT_CUBE:
case scene::ESNT_ANIMATED_MESH:
//这么用的原因是:只用于检测摄像机,所以只使用包围盒检测
//而不用((scene::IAnimateMeshSceneNode*)node)->getMesh(0);
selector = smgr->createTriangleSelectorFromBoundingBox(node);
break;
case scene::ESNT_MESH:
case scene::ESNT_SPHERE:
selector = smgr->createTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(),node);
break;
case scene::ESNT_TERRAIN:
selector = smgr->createTerrainTriangleSelector((scene::ITerrainSceneNode*)node);
break;
case scene::ESNT_OCTREE:
selector = smgr->createOctreeTriangleSelector(((scene::IMeshSceneNode*)node)->getMesh(), node);
break;
default:
break;
}
if (selector)
{
//加入到meta selector
meta->addTriangleSelector(selector);
selector->drop();
}
}
/*
*3.创建碰撞检测动画
*使用metaselector 创建
*然后绑定到照相机
*/
scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(meta, camera, core::vector3df(5, 5, 5),
core::vector3df(0, 0, 0));
meta->drop();
camera->addAnimator(anim);
anim->drop();
camera->setPosition(core::vector3df(0, 20, 0));
scene::ISceneNode* cube = smgr->getSceneNodeFromType(scene::ESNT_CUBE);
if (cube)
camera->setTarget(cube->getAbsolutePosition());
int lastFPS = -1;
while (device->run())
{
driver->beginScene(true, true, video::SColor(255, 100, 101, 140));
smgr->drawAll();
guiev->drawAll();
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
core::stringw str = L"15.LoadIrrFile [";
str += driver->getName();
str += "]FPS.",
str += fps;
device->setWindowCaption(str.c_str());
lastFPS = fps;
}
}
device->drop();
return 0;
}