开源3d游戏引擎irrlicht(鬼火)example讲解——customscenenode

上传人:j****9 文档编号:45087650 上传时间:2018-06-15 格式:DOC 页数:7 大小:232KB
返回 下载 相关 举报
开源3d游戏引擎irrlicht(鬼火)example讲解——customscenenode_第1页
第1页 / 共7页
开源3d游戏引擎irrlicht(鬼火)example讲解——customscenenode_第2页
第2页 / 共7页
开源3d游戏引擎irrlicht(鬼火)example讲解——customscenenode_第3页
第3页 / 共7页
开源3d游戏引擎irrlicht(鬼火)example讲解——customscenenode_第4页
第4页 / 共7页
开源3d游戏引擎irrlicht(鬼火)example讲解——customscenenode_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《开源3d游戏引擎irrlicht(鬼火)example讲解——customscenenode》由会员分享,可在线阅读,更多相关《开源3d游戏引擎irrlicht(鬼火)example讲解——customscenenode(7页珍藏版)》请在金锄头文库上搜索。

1、开源 3D 游戏引擎 Irrlicht(鬼火)Example 讲解CustomSceneNode赵刚Irrlicht 引擎自带的第 3 个 Example 叫做 CustomSceneNode,她演示了用 户如何自己创建新的场景节点,如果用户对 irrlicht 里面已有的 SceneNode 都 不满意,希望自己创建一个有特定功能的 SceneNode 可以参照这个例子做,这 个例子创建的 SceneNode 很简单,只不过是一个四棱锥,但方法是可以参考的, 用户理解后完全可以创建出复杂的 SceneNode。程序运行起来如下图:运行 03.CustomSceneNode.exe 的时候会先

2、出来一个控制台窗口,按 a,再 按回车即可,表示选择 OpenGL 作为渲染 API。进入 bin 目录下的 examples 目录可以看到 03.CustomSceneNode 目录,双 击 CustomSceneNode_vc9.vcproj 文件打开工程文件,工程文件中只有一个 main.cpp 文件。内容如下(去除了英文注释):#include #include “driverChoice.h“using namespace irr;#ifdef _MSC_VER #pragma comment(lib, “Irrlicht.lib“) #endifclass CSampleScene

3、Node : public scene:ISceneNode core:aabbox3d Box; video:S3DVertex Vertices4; video:SMaterial Material;public:CSampleSceneNode(scene:ISceneNode* parent, scene:ISceneManager* mgr, s32 id) : scene:ISceneNode(parent, mgr, id) Material.Wireframe = false; Material.Lighting = false;Vertices0 = video:S3DVer

4、tex(0,0,10, 1,1,0, video:SColor(255,0,255,255), 0, 1); Vertices1 = video:S3DVertex(10,0,-10, 1,0,0, video:SColor(255,255,0,255), 1, 1); Vertices2 = video:S3DVertex(0,20,0, 0,1,1, video:SColor(255,255,255,0), 1, 0); Vertices3 = video:S3DVertex(-10,0,-10, 0,0,1, video:SColor(255,0,255,0), 0, 0);Box.re

5、set(Vertices0.Pos); for (s32 i=1; iregisterNodeForRendering(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, AbsoluteTr

6、ansformation); driver-drawVertexPrimitiveList( virtual const core:aabbox3d virtual u32 getMaterialCount() const return 1; virtual video:SMaterial ;int main() video:E_DRIVER_TYPE driverType=driverChoiceConsole(); if (driverType=video:EDT_COUNT) return 1;IrrlichtDevice *device = createDevice(driverTyp

7、e, core:dimension2d(640, 480), 16, false);if (device = 0) return 1; / could not create selected driver.device-setWindowCaption(L“Custom Scene Node - Irrlicht Engine Demo“);video:IVideoDriver* driver = device-getVideoDriver(); scene:ISceneManager* smgr = device-getSceneManager();smgr-addCameraSceneNo

8、de(0, core:vector3df(0,-40,0), core:vector3df(0,0,0);CSampleSceneNode *myNode = new CSampleSceneNode(smgr-getRootSceneNode(), smgr, 666);scene:ISceneNodeAnimator* anim = smgr-createRotationAnimator(core:vector3df(0.8f, 0, 0.8f);if(anim) myNode-addAnimator(anim);anim-drop(); anim = 0; myNode-drop();

9、myNode = 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“Irrlicht Engine “; str += driver-getName(); str += L“ FPS: “; str += (s32)driver-getFPS();device-setWindowCaption(str.c_str();

10、 frames=0; device-drop(); return 0; 程序很简洁,条理也很清晰,下面开始讲解代码。首先每个使用 irrlicht 的程序都必须包含:#include #pragma comment(lib, “Irrlicht.lib“)表示要使用 irrlicht SDK程序开头就定义了一个名为 CSampleSceneNode 的类,这个类从 ISceneNode 接口(抽象类)派生,在 irrlicht 中,用户创建的 SceneNode 类 都必须从 ISceneNode 接口派生。CSampleSceneNode 类包含下面 3 个成员变量:core:aabbox3

11、d Box; video:S3DVertex Vertices4; video:SMaterial Material;其中 Box 和 Material 所有的 SceneNode 类都会包含,Vertices 用于保存 网格顶点,数量和格式和用户创建的网格复杂度有关,这个例子中的网格是 4 个顶点的四棱锥,而且是很基本的网格类型(没有多层纹理,凹凸纹理等复杂 特性) ,所以使用 S3DVertex 这种简单类型。CSampleSceneNode 在构造函数中建立了四棱锥。Material.Wireframe = false; Material.Lighting = false;Vertice

12、s0 = video:S3DVertex(0,0,10, 1,1,0, video:SColor(255,0,255,255), 0, 1); Vertices1 = video:S3DVertex(10,0,-10, 1,0,0, video:SColor(255,255,0,255), 1, 1); Vertices2 = video:S3DVertex(0,20,0, 0,1,1, video:SColor(255,255,255,0), 1, 0); Vertices3 = video:S3DVertex(-10,0,-10, 0,0,1, video:SColor(255,0,255

13、,0), 0, 0);Box.reset(Vertices0.Pos); for (s32 i=1; iregisterNodeForRendering(this),表示将自己加入渲染 队列。然后 CSampleSceneNode 类重载了 virtual void render()函数,该函数在 每帧循环的绘图阶段会被 irrlicht SDK 调用,顾名思义,这个函数是执行绘图 功能的。 在 render()函数里,包含了一下代码:u16 indices = 0,2,3, 2,1,3, 1,0,3, 2,0,1; video:IVideoDriver* driver = SceneMana

14、ger-getVideoDriver();driver-setMaterial(Material); driver-setTransform(video:ETS_WORLD, AbsoluteTransformation); driver-drawVertexPrimitiveList(indices 是顶点索引表,表明了四棱锥的 4 个顶点按什么样顺序组成 4 个 三角形,绘图时需要通过 setMaterial 函数告知 irrlicht 使用什么材质,通过 setTransform 函数告知 irrlicht 使用什么变换矩阵,然后可以调用 driver 对 象中的绘图方法实现绘制,这里调

15、用的是 drawVertexPrimitiveList 函数,表 示以绘制三角形列表的方式绘制四棱锥。接下来的几个成员函数内容很简单,几乎所有的 SceneNode 都可以写成这 样,这里不再详述。这样 CSampleSceneNode 就代表了用户创建的场景节点,用户创建自己的场 景节点都可以参照这样的过程: 1. 从 ISceneNode 派生一个新类 2. 在构造函数里调用建立网格的函数(或者直接写)。 3. 重载 OnRegisterSceneNode(),告知 irrlicht 如何将场景节点加入到渲染队列中。 4. 重载 render(),实现绘制过程。 5. 实现 getBoun

16、dingBox(),getMaterialCount(),getMaterial()这几个 函数。接下来就可以在主程序中声明 CSampleSceneNode 的对象,并且像使用 irrlicht 自己的 SceneNode 一样使用了。主程序的开头和 02.Quake3Map 差不多,这里不再重复解释,关键看下面几 行代码:CSampleSceneNode *myNode = new CSampleSceneNode(smgr-getRootSceneNode(), smgr, 666);这行代码建立了 CSampleSceneNode 的实例对象,并且加到了场景中。scene:ISceneNodeAnimator* anim = smgr-createRotationAnimat

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号