这个例程涉及:如何创建一个定制场景节点.
Vertices[0] = video::S3DVertex( x,y,z,//Pos nx,ny,nz,//Normal video::SColor(a,r,g,b),//Color tu,tv//Tcoord );
Box.reset(Vertices[0].Pos); for(s32 i=1;i<4;i++) Box.addInternalPoint(Vertices[i].Pos);
virtual void OnRegisterScene() { if(IsVisible) SceneManager->registerNodeForRendering(this); ISceneNode::OnRegisterSceneNode();//这个方法内部实现的是为所有子节点注册, //当内部调用这个回调函数的时候会调用registerNodeForRendering(this); }
virtual void render() { u16 indices[] = {0,2,3,/*第一个面*/ 2,1,3,/*第二个面*/ 1,0,3,/*第三个面*/ 2,0,1/*第四个面*/}; video::IVideoDriver* driver = SceneManager->getVideoDriver();//保存在smgr中的driver driver->setMaterial(Material);//启用当前材质,内部根据渲染器不同 //设置变换 driver->setTransform(video::ETS_WORLD,/*view, world, projection*/ AbsoluteTransformation/*scene::ISceneNode内: core::matrix AbsoluteTransformation,保存矩阵用*/ ); //绘制 dirver->drawVertexPrimitiveList( &Vertices[0],/*最多65535个顶点*/ 4,/*顶点总数*/ &indices[0],/*绘制顶点的顺序表*/ 4,/* 图元数量*/ v video::EVT_STANDARD,/*顶点格式*/ scene::EPT_TRIANGLES,/*绘制的图元种类*/ EIT_16BIT/*indices的数据类型*/ ); }
virtual const aabbox3df& getBoundingBox() const { return Box; } virtual u32 getMaterialCount() const { return 1; } virtual video::SMaterial& getMaterial(u32 i) { return Material; }
CSampleSceneNode *myNode = new CSampleSceneNode( smgr->getRootSceneNode(), smgr, 123 );
myNode->drop(); myNode = 0;
scene::ISceneManager::createRotationAnimation(core::vector3df(rx,ry,rz));
myNode->addAnimator(anim);
#include <irrlicht.h> using namespace irr; #ifdef _MSC_VER #pragma comment(lib,"Irrlicht.lib") #endif class CSampleSceneNode :public scene::ISceneNode { /*成员:包围盒,四个顶点,材质*/ core::aabbox3d<f32> Box; video::S3DVertex Vertices[4]; video::SMaterial Material; public: /*构造函数:父节点,场景管理器的指针,场景节点的id*/ CSampleSceneNode(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id) :scene::ISceneNode(parent, mgr, id) { //Material.Wireframe = false; Material.Lighting = false; Vertices[0] = video::S3DVertex(0, 0, 10, 1, 1, 0, video::SColor(255, 0, 255, 255), 0, 1); Vertices[1] = video::S3DVertex(10, 0, -10, 1, 0, 0, video::SColor(255, 255, 0, 255), 1, 1); Vertices[2] = video::S3DVertex(0, 20, 0, 0, 1, 1, video::SColor(255, 255, 255, 0), 1, 0); Vertices[3] = video::S3DVertex(-10, 0, -10, 0, 0, 1, video::SColor(255, 0, 255, 0), 0, 0); //包围盒初始化,这个是必须的,用于自动裁剪, //如果不想设置的话,需要在关闭包围盒; //irr::scene::ISceneNode::setAutomaticCulling(irr::scene::EAC_OFF); Box.reset(Vertices[0].Pos); for (s32 i = 1; i < 4; i++) Box.addInternalPoint(Vertices[i].Pos); } //注册场景节点, virtual void OnRegisterSceneNode() { if (IsVisible) SceneManager->registerNodeForRendering(this); ISceneNode::OnRegisterSceneNode(); } //重载渲染函数 virtual void render() { u16 indices[] = { 0, 2, 3, 2, 1, 3, 1, 0, 3, 2, 0, 1 }; video::IVideoDriver* driver = SceneManager->getVideoDriver(); driver->setMaterial(Material);//启用当前材质 driver->setTransform(video::ETS_WORLD, AbsoluteTransformation); driver->drawVertexPrimitiveList(&Vertices[0], 4, &indices[0], 4, video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT); } //添加一些额外的方法 virtual const core::aabbox3d<f32>& getBoundingBox() const { return Box; } virtual u32 getMaterialCount() const { return 1; } virtual video::SMaterial& getMaterial(u32 i) { return Material; } }; int main() { IrrlichtDevice *device = createDevice(video::EDT_OPENGL, core::dimension2d<u32>(720, 455), 16, false, false, false, 0); if (!device) return 1; device->setWindowCaption(L"CustomSceneNode "); video::IVideoDriver *driver = device->getVideoDriver(); scene::ISceneManager *smgr = device->getSceneManager(); CSampleSceneNode *myNode = new CSampleSceneNode(smgr->getRootSceneNode(), smgr, 666); scene::ISceneNodeAnimator* anim = smgr->createRotationAnimator(core::vector3df(0.8f, 0.5f, 0.1f)); if (anim) { myNode->addAnimator(anim); anim->drop(); anim = 0; } myNode->drop(); myNode = 0; smgr->addCameraSceneNode(0, core::vector3df(0, -40, 0), core::vector3df(0, 0, 0)); u32 frames = 0; while (device->run()) { driver->beginScene(true, true, video::SColor(0, 100, 100, 100)); smgr->drawAll(); driver->endScene(); if (++frames == 100) { core::stringw str = L"CustomeSceneNode ["; str += driver->getName(); str += L"] FPS: "; str += (s32)driver->getFPS(); device->setWindowCaption(str.c_str()); frames = 0; } } device->drop(); return 0; }