1、OgreMadMard教程4翻译版MadMarx Tutorial 4 ManualObject to Mesh 手动创建meshForeword.If you prefer tutorials that come with a framework = check the other wiki tutorial series.If you prefer tutorials that go step by step without a framework = this page should be ok.加入你希望教程中是有framework的版本=请在主页中寻找其他教程系列。如果你希望教程中是
2、没有framework,一步一步来实现代码的=这一页就可以。I assume you know C+. If not, this tutorial will probably be hard to understand !咱假设你懂C+语言,否则你很难看得懂这页教程。This tutorial presents only a few elements of Ogre3D.这个教程只适合有一定OGRE3D基础的开发者。You can download the code and media for this tutorial at the bottom of this wiki page.This
3、 little tutorial is an extract of a bigger project which contains more tutorials & helper classes.你可以下载本教程需要的媒体文件和代码在本页中,这是一个小小的教程,但是是从一个巨大的项目里面提取出来的,而那个巨大的项目里面则有更多的教程和帮助类。This bigger project is avaible there :这个逼格(括弧笑)项目在这里:Also, make sure you read these tutorials in order!同时,确认你能良好的完成本教程。Table of
4、contents Foreword.o Tutorial Descriptiono After viewport creation main.cppTutorial DescriptionIn this program, I create aManualObject(like in the previous tutorial).在这个程序中,咱创建了一个手动创建对象(就像之前的教程一样)。This time it will be a little more complicated than just a quad.这此将会比一个正方形更加复杂一点。To be able to make seve
5、ral copies of it, and at the same time re-use the already created3D data, I will convert the Ogre:ManualObjectto an Ogre:Mesh.为了能多做几份拷贝,同时重用已经创建的数据,咱将用Ogre:Mesh代替掉Ogre:ManualObject。Such mesh can be used to create several Entities.这样的mesh可以用来创建多个实体。AnEntityis an instance of a mesh, and allow differen
6、tmaterialinformation for eachentity.Entities are often the most common object in a Ogre application.一个实体是一个mesh的实例,并且允许不同的实体附加材质信息。实体通常是ogre程序里最常见的单位。Different Entities can have different materials, while sharing the same mesh.It is possible to create Entities with different meshes too.不同的实体可以拥有不同的材
7、质,以其共享相同的mesh,还可以创建不同的mesh来创建实体。In order to move anEntity, attach it to aSceneNode, and move thisSceneNode.为了移动实体,我们需要将实体绑定到节点上,然后移动节点。Note : It was reported that on some video drivers (mesa/intel),ManualObject:convertToMesh()producessome kind of degenerated (invisible) mesh. If that is the case for
8、 you, please update your drivers.注意:在一些显卡驱动中会回报这种(mesa/intel),ManualObject:convertToMesh()producessome kind of degenerated (invisible) mesh。如果你遇见这种情况,请更新你的显卡驱动。After viewport creation在视口创建后 Ogre:ManualObject* lManualObject = NULL; The manualObject creation requires a name.ManualObject构造器需要一个名字: Ogre
9、:String lManualObjectName = CubeWithAxes; lManualObject = lScene-createManualObject(lManualObjectName);Always tell if you want to update the 3D (vertex/index) later or not.别忘记要告诉你的程序在稍后是否要更新3D(顶点/索引): bool lDoIWantToUpdateItLater = false; lManualObject-setDynamic(lDoIWantToUpdateItLater);Here I crea
10、te a cube in a first part with triangles, and then axes (in red/green/blue).这里咱要创建一个用三角形构成的立方体,and the axes(in red/green/blue)【这句话咱猜是赋予其轴向颜色】BaseWhiteNoLighting is the name of amaterialthat already exist inside Ogre.BaseWhiteNoLighting是一个已经在ogre里面的材质的名字。Ogre:RenderOperation:OT_TRIANGLE_LIST is a kin
11、d of primitive.Ogre:RenderOperation:OT_TRIANGLE_LIST是一种原语。 float lSize = 0.7f; lManualObject-begin(BaseWhiteNoLighting, Ogre:RenderOperation:OT_TRIANGLE_LIST); float cp = 1.0f * lSize ; float cm = -1.0f * lSize; lManualObject-position(cm, cp, cm);/ a vertex lManualObject-colour(Ogre:ColourValue(0.0f
12、,1.0f,0.0f,1.0f); lManualObject-position(cp, cp, cm);/ a vertex lManualObject-colour(Ogre:ColourValue(1.0f,1.0f,0.0f,1.0f); lManualObject-position(cp, cm, cm);/ a vertex lManualObject-colour(Ogre:ColourValue(1.0f,0.0f,0.0f,1.0f); lManualObject-position(cm, cm, cm);/ a vertex lManualObject-colour(Ogr
13、e:ColourValue(0.0f,0.0f,0.0f,1.0f); lManualObject-position(cm, cp, cp);/ a vertex lManualObject-colour(Ogre:ColourValue(0.0f,1.0f,1.0f,1.0f); lManualObject-position(cp, cp, cp);/ a vertex lManualObject-colour(Ogre:ColourValue(1.0f,1.0f,1.0f,1.0f); lManualObject-position(cp, cm, cp);/ a vertex lManua
14、lObject-colour(Ogre:ColourValue(1.0f,0.0f,1.0f,1.0f); lManualObject-position(cm, cm, cp);/ a vertex lManualObject-colour(Ogre:ColourValue(0.0f,0.0f,1.0f,1.0f);face behind / front朝向 背面/前面;(其实这里指的是咱创建的立方体的正面和背面) lManualObject-triangle(0,1,2); lManualObject-triangle(2,3,0); lManualObject-triangle(4,6,5
15、); lManualObject-triangle(6,4,7);face top / down朝向 上/下 lManualObject-triangle(0,4,5); lManualObject-triangle(5,1,0); lManualObject-triangle(2,6,7); lManualObject-triangle(7,3,2);face left / right朝向 左/右 lManualObject-triangle(0,7,4); lManualObject-triangle(7,0,3); lManualObject-triangle(1,5,6); lManu
16、alObject-triangle(6,2,1); lManualObject-end();Here I have finished myManualObjectconstruction.这里咱完成了咱的手动创建对象的构造。It is possible to add more begin()-end() thing, in order to usedifferent rendering operation types, or different materials in 1ManualObject.很可能需要添加更多的begin()和end()来应对不同的渲染操作类型或是1个手动创建对象的不同
17、材质。 lManualObject-begin(BaseWhiteNoLighting,Ogre:RenderOperation:OT_LINE_LIST); float lAxeSize = 2.0f * lSize; lManualObject-position(0.0f, 0.0f, 0.0f); lManualObject-colour(Ogre:ColourValue:Red); lManualObject-position(lAxeSize, 0.0f, 0.0f); lManualObject-colour(Ogre:ColourValue:Red); lManualObject
18、-position(0.0f, 0.0f, 0.0f); lManualObject-colour(Ogre:ColourValue:Green); lManualObject-position(0.0, lAxeSize, 0.0); lManualObject-colour(Ogre:ColourValue:Green); lManualObject-position(0.0f, 0.0f, 0.0f); lManualObject-colour(Ogre:ColourValue:Blue); lManualObject-position(0.0, 0.0, lAxeSize); lMan
19、ualObject-colour(Ogre:ColourValue:Blue); lManualObject-index(0); lManualObject-index(1); lManualObject-index(2); lManualObject-index(3); lManualObject-index(4); lManualObject-index(5); lManualObject-end(); Ogre:String lNameOfTheMesh = MeshCubeAndAxe; Ogre:String lResourceGroup = Ogre:ResourceGroupMa
20、nager:DEFAULT_RESOURCE_GROUP_NAME; lManualObject-convertToMesh(lNameOfTheMesh);Now I can create several entities using that mesh现在咱创建了几个使用mesh的实体。 int lNumberOfEntities = 5; for(int iter = 0; iter createEntity(lNameOfTheMesh);Now I attach it to a scenenode, so that it becomes present in the scene.现在
21、咱将它绑定在节点上, Ogre:SceneNode* lNode = lRootSceneNode-createChildSceneNode(); lNode-attachObject(lEntity);I move theSceneNodeso that it is visible to thecamera.咱移动了SceneNode,以便于摄像机的访问 float lPositionOffset = float(1+ iter * 2) - (float(lNumberOfEntities); lNode-translate(lPositionOffset, lPositionOffset
22、, -10.0f); (PS:完成上面的代码,你应该可以看到几个彩色的正方体在屏幕中,还有几个彩色的坐标轴线)main.cpp/ In this program, I create a ManualObject (like in the previous tutorial)./ This time it will be a little more complicated than just a quad./ To be able to make several copies of it, and at the same time re-use the already created/ 3D d
23、ata, I will convert the Ogre:ManualObject to an Ogre:Mesh./ Such mesh can be used to create several Entities. / An Entity is an instance of a mesh, and allow different material information for each entity./ Entities are often the most common object in a Ogre application. / Different Entities can hav
24、e different materials, while sharing the same mesh./ It is possible to create Entities with different meshes too./ In order to move an Entity, attach it to a SceneNode, and move this SceneNode./ Note : It was reported that on some video drivers (mesa/intel), convertToMesh produces/ some kind of dege
25、nerated (invisible) mesh. If that is the case for you, please update your drivers./ I will use std:auto_ptr so I need to include memory. / If you dont know std:auto_ptr, you should check some C+ tutorials/lesson on this matter.#include / I will check for std:exception. If you dont know what exceptio
26、n/try/catch means, you should learn C+ first.#include / These are some files that we need to include to use Ogre3D. Note that you can at the beginnings use directly Ogre.h, to include lots of commonly used classes.#include OGRE/OgreRoot.h#include OGRE/OgreRenderSystem.h#include OGRE/OgreRenderWindow
27、.h#include OGRE/OgreWindowEventUtilities.h#include OGRE/OgreManualObject.h#include OGRE/OgreEntity.h/Here I include my other files, like the one for SimpleOgreInit.#include SimpleOgreInit.h#include EasyDefines.h/ I declare a function in which I will make my whole application./ This is easy then to a
28、dd more things later in that function./ The main will call this function and take care of the global try/catch.void AnOgreApplication() / I construct my object that will allow me to initialise Ogre easily. OgreEasy:SimpleOgreInit lOgreInit; if(!lOgreInit.initOgre() std:coutImpossible to init Ogre co
29、rrectly.std:endl; return; /I prefer to be able to access my variables directly. Ogre:Root* lRoot = lOgreInit.mRoot.get(); Ogre:RenderWindow* lWindow = lOgreInit.mWindow; / I create a scenemanager. This is like a Scene, in which I can put lights, 3d objects, etc. / The scenemanager contains an arborescent graph of SceneNodes. To manage elements of the scene, / I will create SceneNodes in the SceneManager, and attach the elements to the scenenodes. / First parameter : I select a kind of SceneManager. This may have a huge impact on perfo
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1