您的位置:首页 > 移动开发 > Objective-C

第六章 OGRE中场景管理器、资源加载、manualObject的使用

2013-04-03 23:22 447 查看
#include "ExampleApplication.h"
class Example41:public ExampleApplication
{
public:
void createScene()
{
//1 begin
//std::cout<<mSceneMgr->getTypeName()<<"::"<<mSceneMgr->getName()<<std::endl;
//1 end
//One important task the scene manager fulfills is the management of objects.This can be scene node,entities,lights,or a lot of other object types
//that Ogre 3D has.Ogre 3D works with the principle - he who creates an object,also destroys it.Every time we want an entity or scene node deleted,we
//must use the scene manager.When the scene is rendered,only the position of nodes that have been moved and all their children are calculated.For the
//rest,we use the positions from the last frame.This saves a lot of computation time and is an important task of the scene manager.
//3|all begin
Ogre::Plane plane(Vector3::UNIT_Y,-10);
Ogre::MeshManager::getSingleton().createPlane("planemesh",ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,plane,1500,1500,200,200,true,1,5,5,Vector3::UNIT_Z);
Ogre::Entity* ent=mSceneMgr->createEntity("GrassFloorPlane","planemesh");
mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent);
ent->setMaterialName("Examples/GrassFloor");
Ogre::Light* light=mSceneMgr->createLight("Light1");
light->setType(Ogre::Light::LT_DIRECTIONAL);
light->setDirection(Ogre::Vector3(1,-1,0));
Ogre::ManualObject* manual=mSceneMgr->createManualObject("grass");

manual->begin("Examples/GrassBlades",RenderOperation::OT_TRIANGLE_LIST);
//8|3.2 begin
manual->position(5.0,0.0,0.0);
manual->textureCoord(1,1);
manual->position(-5.0,10.0,0.0);
manual->textureCoord(0,0);
manual->position(-5.0,0.0,0.0);
manual->textureCoord(0,1);
manual->position(5.0,10.0,0.0);
manual->textureCoord(1,0);

manual->position(2.5,0.0,4.3);
manual->textureCoord(1,1);
manual->position(-2.5,10.0,-4.3);
manual->textureCoord(0,0);
manual->position(-2.0,0.0,-4.3);
manual->textureCoord(0,1);
manual->position(2.5,10.0,4.3);
manual->textureCoord(1,0);

manual->position(2.5,0.0,-4.3);
manual->textureCoord(1,1);
manual->position(-2.5,10.0,4.3);
manual->textureCoord(0,0);
manual->position(-2.0,0.0,4.3);
manual->textureCoord(0,1);
manual->position(2.5,10.0,-4.3);
manual->textureCoord(1,0);

manual->index(0);
manual->index(1);
manual->index(2);
manual->index(0);
manual->index(3);
manual->index(1);

manual->index(4);
manual->index(5);
manual->index(6);
manual->index(4);
manual->index(7);
manual->index(5);

manual->index(8);
manual->index(9);
manual->index(10);
manual->index(8);
manual->index(11);
manual->index(9);
manual->end();

Ogre::StaticGeometry* field=mSceneMgr->createStaticGeometry("FieldOfGlass");
//8|3.2 end
//We called the begin method,which prepared the manual object to receive its vertex information.The begin method needs a material name that
//the vertex will be using,the way we are going to input the vertex information,and what we want to create.There are six different ways for
//how and what we can put into a manual object.OT_POINT_LIST,OT_LINT_LIST,OT_LINE_STRIP,OT_TRIANGLE_LIST,OT_TRIANGLE_STRIP,OT_TRIANGLE_FAN
/*
//3.2 begin
manual->position(5.0,0.0,0.0);
manual->textureCoord(1,1);
//(0,0) means the upper-left corner of the image and (1,1) meansthe bottom-right corner.
manual->position(-5.0,10.0,0.0);
manual->textureCoord(0,0);
manual->position(-5.0,0.0,0.0);
manual->textureCoord(0,1);
manual->position(5.0,0.0,0.0);
manual->textureCoord(1,1);
manual->position(5.0,10.0,0.0);
manual->textureCoord(1,0);
manual->position(-5.0,10.0,0.0);
manual->textureCoord(0,0);
//5_4 begin
manual->position(2.5,0.0,4.3);
manual->textureCoord(1,1);
manual->position(-2.5,10.0,-4.3);
manual->textureCoord(0,0);
manual->position(-2.0,0.0,-4.3);
manual->textureCoord(0,1);

manual->position(2.5,0.0,4.3);
manual->textureCoord(1,1);
manual->position(2.5,10.0,4.3);
manual->textureCoord(1,0);
manual->position(-2.5,10.0,-4.3);
manual->textureCoord(0,0);

manual->position(2.5,0.0,-4.3);
manual->textureCoord(1,1);
manual->position(-2.5,10.0,4.3);
manual->textureCoord(0,0);
manual->position(-2.0,0.0,4.3);
manual->textureCoord(0,1);

manual->position(2.5,0.0,-4.3);
manual->textureCoord(1,1);
manual->position(2.5,10.0,-4.3);
manual->textureCoord(1,0);
manual->position(-2.5,10.0,4.3);
manual->textureCoord(0,0);
//5_4 end
manual->end();
//3.2 end*/

//3.1 begin
Ogre::SceneNode* grassNode=mSceneMgr->getRootSceneNode()->createChildSceneNode("GrassNode2");
grassNode->translate(0,-10,0);
grassNode->attachObject(manual);
//3.1 end
//6_5|3.1 begin
manual->convertToMesh("BladesOfGrass");
for(int i=0;i<50;i++)
{
for(int j=0;j<50;j++)
{
Ogre::Entity* bladesOfGlass=mSceneMgr->createEntity("BladesOfGrass");
//6.1 begin
//Ogre::SceneNode* node=mSceneMgr->getRootSceneNode()->createChildSceneNode(Ogre::Vector3(i*3,-10,j*3));
//node->attachObject(bladesOfGlass);//It's very slow,because Ogre 3D has to calculate the world position of every grass entity for each frame
//6.1 end
//8|6.1&7_6 begin
field->addEntity(bladesOfGlass,Ogre::Vector3(i*3,-10,j*3));
//8|6.1&7_6 end
//7_6 begin
//std::cout<<node->getName()<<"::"<<bladesOfGlass->getName()<<std::endl;
//We see that scene node names use the following scheme:Unnamed_Nr,where Nr is a counter that get increased each time we create a new
//unnamed scene node.Entities use a similar scheme,but they use MO instead of Unnamed_;MO is the short for movable object.A movable object
//is a class used as the base class for many different classes in Ogre 3D.Everything that can be moved using scene nodes inherits from a movable object.
//7_6 end
}
}
//6_5|3.1 end
//3|all end
//8|7 begin
field->build();
//8|7 end
//4_3 begin
Ogre::ManualObject* manual2=mSceneMgr->createManualObject("MyObject");
manual2->begin("Examples/BaseWhiteNoLighting",RenderOperation::OT_LINE_LIST);
manual2->position(10.0,0.0,0.0);
manual2->position(-10.0,0.0,0.0);
manual2->end();
Ogre::SceneNode* lineNode=mSceneMgr->getRootSceneNode()->createChildSceneNode("LineNode");
lineNode->attachObject(manual2);
//4_3 end
}
//2|1 begin
/*
virtual void chooseSceneManager()
{
ResourceGroupManager::getSingleton().addResourceLocation("chiropteraDM.pk3","Zip",ResourceGroupManager::getSingleton().getWorldResourceGroupName());
//During startup,the manager gets a list with directories and ZIP archives we want to load.This list can be read from a file,like resources.cfg,
//or it can be written into the application code.After this,we can create an entity using only the filename to create it.就像加载了chiropteraDM.pk3的Zip包一样,SetworldGeometry只需要给出maps/chiropteradm.bsp
//We don't need the full path of the file because the manager has already indexed it;only when we create an instance of the index file will it really get loaded.
//Indexing saves us the trouble of checking that we don't load the same model twice.When we use a model twice,the manager loads each model exactly once,and when we need two instances of the same
//model the manager uses the already loaded model and doesn't load it again.
//第二个参数定义它的类型,通常它可以是zip压缩包或者一个文件夹。如果需要我们可以添加自己的资源的类型。当我们想要加载自己定义的数据包类型时,这会变得非常有用
//第三个参数是我们想要加载文件到所在资源组的名称。资源组就好像C++的命名空间一样。
//最后一个参数告诉Ogre 3D,我们加载的路径是否被递归调用。如果设置为false,只有在目录中的文件被加载,在子文件夹的文件不被加载。如果设置为true,Ogre 3D也会加载子文件的文件。默认的设置为false
ResourceGroupManager::getSingleton().initialiseResourceGroup(ResourceGroupManager::getSingleton().getWorldResourceGroupName());
//With the function call initialiseResourceGroup(),we tell Ogre 3D to index all files in the ResourceGroup which aren't already indexed.
//Of course,we have to give the name of the resource group we want to index.After this call,we can us all files that are associated to this ResourceGroup.
mSceneMgr=mRoot->createSceneManager("BspSceneManager");
mSceneMgr->setWorldGeometry("maps/chiropteradm.bsp");
}*/

//2|1 end
};
int main()
{
Example41 app;
app.go();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: