# Betajaen的NxOgre简短教程——宝爷译

## Alpha and Omega — A bit of everything

World就像我们的宇宙，漫无边际而且只有唯一的一个。我们在world里面的东西叫Scene，它们是有大小的。SceneScene之间不会有交集

The World is like our universe, it’s infinite in size and there can be only one of them.Inside the world we have things called a Scene. They could be considered like dimensions in our world, we inhabit one dimension and ghosts and monsters inhabit another. Things in one scene cannot interact with things with another.

Inside the scenes is the matter itself, it is divided into units or RigidBodies such as a pencil, a corpse, a house or a oil canister.

RigidBodies有四种类型，ActorKinematicActorSceneGeometryVolume这四种类型。每一种类型都是基于一些函数来进行与其他各种对象的动态碰撞检测，在这一点上，我们可以把他们当作同一个东西。每一个RigidBody 都是由一个或者多个形状Shapes组成的。当一个RigidBody Shapes和另一个RigidBody Shapes碰撞的时候，我们需要通过它的Material属性来知道它们碰撞之后会产生怎样的效果。

There are four types of RigidBodies; An Actor, a KinematicActor,SceneGeometry and a Volume, each different type is based on the method of movement it has and how it interacts with each other. But for now; let’s just call treat them as one.

Each RigidBody is made from one or more Shapes. When a shape collides with another shape from another RigidBody, it’ll need to know how to react properly this defined by a few properties called a Material.

## The World and a fitting Description of it — The infamous ’Verse

World和对它的恰当描述——无名的诗

The World is the top-most class of NxOgre; it is responsible for handling of all the Scenes and indirectly working with it’s friends; the Singletons. World is normally the first to be created, and most always the last destroyed.

When we create the World, we normally give it a Description. A bit like a mug shot; What kind of  ice-cream it likes?, What’s it’s favourite colour?, Where it get’s hair done?, things like that. We give the description to the World or any other class that likes descriptions for it to read and pretend to be that class you stole the description from.

WorldDescription description; description.mNoHardware = true; World* mWorld = World::createWorld(description);

At this point, World seems very boring. Because it is, it’s always out there but there are more important things in life. So lets move on, to time!

## Father Time — Controlling time with your time machine

TimeController 负责时间的控制，它控制事件的发生，它控制事件的起因和结果，他就是你！因为时间是一个非常奇怪的东西(如果我们没有它可能会更好，个人观点)，所有东西都需要在一个时间维度之中，他们需要继承TimeListener对象

TimeController is the second one in charge, he makes things happen, he makes the whole cause and event thing go, he’s the man.

TimeController* mTimeController = mWorld->getTimeController();

Because Time is a strange thing (and personally I think we would do better without it), anything that needs to be in the time dimension must be a TimeListener. We can do that by inheriting from the TimeListener class;

class myClock : public TimeListener {  bool advance(const TimeStep& step, const Enums::Priority&) {   std::cout << "My word! " << step.mActual << " seconds have passed.\n";  } };

在BloodyMess中，很多对象都是TimeListener，例如Scene，一些单例，Renderables，甚至World！通过TimeController的advance函数来施展我们的时间魔法，让时间前进。

Lot’s of things in Bloody Mess are TimeListeners; Scenes, some Singletons, Renderables, even the World – although you ask him about it, he’ll deny everything.

With our now time controlling superpowers we can exercise our will to advance time forward as much as we want, as many times as we like!

TimeController* mr_timey_wimey = TimeController::getSingleton(); mr_timey_wimey->advance(100.0f);

With the universe advanced 100 seconds into the future and we did not age a day, our quest for being an evil mastermind is coming to an end.

## The Singletons — Download all six seasons for \$29.95!

Singletons NxOgre的手下，是好人，不是坏人。这里总共有5Singletons，尽管第五个家伙没有在下面这张图片中显示出来。他们是ErrorStreamTimeControllerResourceSystemMeshManager以及HeightFieldManager。虽然他们是World的手下，但他们可以在World创建之前被创建。以及在World销毁之后再销毁。单例对象可以在任何地方被调用。

The Singletons are the underlings of NxOgre, not the bad underlings kind, the good ones really. Well, almost. There are five singletons; although the isn’t fifth not shown (He joined us in season 3, and I couldn’t find a more recent screengrab).

They are; ErrorStream, TimeController, ResourceSystem, MeshManager and HeightFieldManager. I’ll let you guess who’s the one in pink.

Although they are the underlings of the World, they can be created before the world, and after the world has perished – If your evil bidding desires it.

World::precreateSingletons();        //预创建单例World* mWorld* = World::createWorld();      //创建世界World::destroyWorld(false);         //销毁世界World::destroySingletons();           //销毁单例

Singletons can be accessed any where, and there is only one of their kind – sadly.

ErrorStream::getSingleton()->throwAssertion("I've broken it!!!", __FILE__, __LINE__);

## The ResourceSystem — Gather ye resources

The greatest achievement of the human spirit is to live up to one’s opportunities and make the most of one’s resources. — Vauvenargues

We start our lesson with the ResourceSystem. A singleton designed to handle any external resources to our world. Meshes or Heightfields are commonly used with the ResourceSystem, but other less complicated files such as text files can be used – if you so wish.

ResourceSystem* sparky = ResourceSystem::getSingleton();

The ResourceSystem is typically a very lazy creature, it never went to school and read about different file types, operating system structures or the exact placement of a block header should be. Instead the ResourceSystem has lots of friends calledResourceProtocols which the ResourceSystem has bribed them to handle any resources for him.

Two examples are the FileResourceProtocol and MemoryResourceProtocol which you’ll never meet because they are invisible and really shy. We talk to these ResourceProtocols using an ancient tribal dance called a UniformResourceIdentifier to quickly reveal themselves, open their magic secret bags full of goodies or if your a traditionalist Archives.

We start our dance by writing down the name of the archive we want to call it to a green ribbon, then shouting out aloud theprotocol we want, then write an ancient symbol the colon in the air then finally we write down the location of the Archive and bury it in the ground.

sparky->openArchive("myFishesArchive", "file:C:/Program Files/Fishes/");

Some Protocols don’t use Archives at all. When we want something – it will be there regardless of where it came from; MemoryResourceProtocol subscribes to this new age thinking. Of course MemoryResourceProtocol doesn’t even name it’s resources – it’s that new age.

After we’ve gotten hold of our magic bag or archive if you prefer. We can now open it and play with the things inside. We do that by another dance called the ArchiveResourceIdentifier. It’s similar to the UniformResourceIdentifier dance, but now we whisper the name of the archive, trace another colon in the air, and start singing out the name of the resource we want, and finish of with a long burp indicating how much access we want to it.

Resource* mr_trout = sparky->open("myFishesArchive:fish.nxs", Enums::ResourceAccess_ReadOnly);

With our Resource now opened we can now play with it, with the assurance that our new friends will handle the nasty bits with Operating Systems, Memory, or even really knowing what type of file or archive we are using.

int x = mr_trout->readInt(); if (size_t size = mr_trout->getSize() > 140) {    mr_trout->seek(140);   short y = mr_trout->readUShort(); } unsigned int readOps = mr_trout->getNbReadOperations(); mr_trout->seekBeginning();

Next time on the Singletons, we explore the easier side of Resources; Meshes and Heightfields.

## Meshes & Heightfields — Behold the Starfish King of North London!

It is clearly said that Meshes and HeightFields have their differences, but what are they and how are the different?

### Heightfields

HeightFields are pieces of terrain; much like a hill, huge tracks of land, or a underwater mini-golf course. HeightFields are like spreadsheets organised into rows and columns – with each cell representing the height of the terrain at that particular point. A value of 32768 may feel like being on top of the world, where as -32678 may feel like everyone is all above at you – laughing.

HeightFields in NxOgre use the HeightField class and are organised by the HeightFieldManager singleton (you’ve met them already). HeightFields may be created within code via the ManualHeightField class or if your feeling adventurous be loaded from a NxOgre HeightField xhf file, which is loaded through the ResourceSystem. You can save xhf files from the ManualHeightField – so it’s win win.

HeightField* northLondon = HeightFieldManager::getSingleton()->load("myFishesArchive:northLondon.xhf");

But for now my petite starfish, we will do it the old fashioned way and make our HeightFields by hand, like our forefathers did.

ManualHeightField southLondon; southLondon.begin(8,8); for (unsigned int i=0; i < 8;i++)  for (unsigned int j=0; j < 8;j++)   southLondon.sample((i * 10) + j * 100); HeightField* theRealSouthLondon = mhf.end("southLondon");

Heightfields are used with StaticGeometries which you’ll encounter later in Level 2. Meshes are also used with StaticGeometries which I will have a chin-wag with you now.

### Meshes

Meshes unlike Heightfields are superior (or not – depending on your point of view). Unlike Heightfields they can take any upon any shape you wish. You can picture of them as an unmovable, loud mouth, shapeshift once sort of…shape.

Like Heightfields Meshes can be loaded from disk via a saved PhysX Mesh nxs file using the MeshManager singleton.

Mesh* the_starfish_king = MeshManager::getSingleton()->load("myFishesArchive:donald.nxs");

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

04-22
10-24 279

12-20
12-08
04-02 1139
02-20 466
04-03 1472
04-02 1289
04-02 1297
03-05 2314
10-13 48
03-13 31
04-02 1149
04-08 479
03-06 1441
04-02 1170
©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。