Mogre,也就是Managed OGRE,它对OGRE(在上一篇ORGE学习笔记中有简单介绍)做了一个封装,使得其可以使用C#进行开发。对于我这种写惯Java,见过C#,对C++有强烈恐惧心理的人来说,不得不说是一个很好的选择。
因为它只是对OGRE做了一个封装,所以,除了在coding时,让我觉得更好用以外,结构、接口之类是完全一致的,在此不再多说。
Tutotial的地址:http://www.ogre3d.org/wiki/index.php/MOGRE
下面对Mogre的一些简单使用,分篇进行一些记录和介绍
Section 1: The SceneNode, Entity, SceneManager constructs, Cameras, Lights, and Shadows
开发环境为Visual Studio 2005,先给出代码如下:
using System;
using System.Windows.Forms;
using MogreFramework;
using Mogre;
namespace Tutorial02
{
static class Program
{
[STAThread]
static void Main()
{
try
{
MyOgreWindow win = new MyOgreWindow();
new SceneCreator(win);
win.Go();
}
catch (System.Runtime.InteropServices.SEHException)
{
if (OgreException.IsThrown)
MessageBox.Show(OgreException.LastException.FullDescription, "An Ogre exception has occurred!");
else
throw;
}
}
}
class MyOgreWindow : OgreWindow
{
protected override void CreateCamera()
{
Camera = this.SceneManager.CreateCamera("PlayerCam");
Camera.Position = new Vector3(0, 10, 500);
Camera.LookAt(Vector3.ZERO);
Camera.NearClipDistance = 5;
}
protected override void CreateViewport()
{
Viewport = this.RenderWindow.AddViewport(Camera);
Camera.AspectRatio = Viewport.ActualWidth / Viewport.ActualHeight;
}
}
class SceneCreator
{
public SceneCreator(OgreWindow win)
{
win.SceneCreating += new OgreWindow.SceneEventHandler(SceneCreating);
}
void SceneCreating(OgreWindow win)
{
SceneManager mgr = win.SceneManager;
mgr.AmbientLight = ColourValue.White;
mgr.ShadowTechnique = ShadowTechnique.SHADOWTYPE_STENCIL_ADDITIVE;
Entity ent = mgr.CreateEntity("ninja", "ninja.mesh");
ent.CastShadows = true;
mgr.RootSceneNode.CreateChildSceneNode().AttachObject(ent);
Plane plane = new Plane(Vector3.UNIT_Y, 0);
MeshManager.Singleton.CreatePlane("ground", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME,
plane, 1500, 1500, 20, 20, true, 1, 5, 5, Vector3.UNIT_Z);
ent = mgr.CreateEntity("GroundEntity", "ground");
mgr.RootSceneNode.CreateChildSceneNode().AttachObject(ent);
ent.SetMaterialName("Examples/Rockwall");
ent.CastShadows = false;
Light light = mgr.CreateLight("Light");
light.Type = Light.LightTypes.LT_DIRECTIONAL;
light.DiffuseColour = new ColourValue(.25f, .25f, 0);
light.SpecularColour = new ColourValue(.25f, .25f, 0);
light.Direction = new Vector3(0, -1, -1);
}
}
}
按F5运行,截图如下:
下面对代码和其中的元素做一定的解释:
SceneManager
SceneManager有很多种,所有在场景中显示的物体都由SceneManager管理, 包括Cameras,planes, billboards,lights等。
我们这个程序中,MyOgreWindow继承自OgreWindow,在OgreWindow已经有创建SceneManager的代码。因此只需要使用如下代码直接使用:
SceneManager mgr = win.SceneManager;
Entity
一个实体是一种可以在场景中被呈现的物体对象,它的所有信息由mesh文件中定义。一个robot,fish和terrain都是一个Entity;Lights, Billboards, Particles, Cameras等不是Entity。
在Ogre中,必须注意到物体和它们的位置和方向信息是分开呈现的,因此我们不能直接在场景中放置Entity,而必须将Entity attach 到SceneNode上,这个SceneNode包含了位置和方向信息。添加一个Entity到场景的代码如下:
Entity ent = mgr.CreateEntity("ninja", "ninja.mesh");
mgr.RootSceneNode.CreateChildSceneNode().AttachObject(ent);
SceneNode
就像在上面提到的,SceneNode包含了attach到自身的所有物体的位置和方向信息。Entity被创建后不会被呈现,除非它被attach到一个SceneNode;同样,一个 SceneNode不会被现实在屏幕上,除非它attach了一个Entity或者别的object。一个SceneNode可以attach任意个objects。
一个SceneNode的位置和他的父SceneNode相关。
Cameras
Camera是我们用来观察我们建立的场景的,它是一个特殊的对象类似于一个sceneNode。Camera对象有他的Position和Yaw,Roll,Pitch等方法,我们可以将它attach到任何一个SceneNode,而且和SceneNode一样,一个Camera的position和它的父节点相关。
必须注意的是,在同一时间,只能有一个Camera被使用。
Camera由SceneManager创建,代码如下:
protected override void CreateCamera()
{
Camera = this.SceneManager.CreateCamera("PlayerCam");
}
调整Camera 的位置,拍摄方向和最近的拍摄距离:
Camera.Position = new Vector3(0, 10, 500); Camera.LookAt(Vector3.ZERO); Camera.NearClipDistance = 5;
我们也可以通过FarClipDistance设置最远拍摄距离。
Viewports
当我们开始处理多Cameras问题的时候,Viewport就变得非常有用了。 SceneManager创建Cameras以观察场景,我们必须告诉RenderWindow使用哪个Camera来显示在屏幕的那个部分上。 这个用来显示Camera的区域(The area in which you tell the RenderWindow to display the Camera)就是Viewport。
创建Viewport的代码如下:
protected override void CreateViewport()
{
Viewport = this.RenderWindow.AddViewport(Camera);
Viewport.BackgroundColour = ColourValue.Black;
Camera.AspectRatio = Viewport.ActualWidth / Viewport.ActualHeight;
}
Lights and Shadows
Ogre目前支持以下三种Shadows :
- Modulative Texture Shadows
- Modulative Stencil Shadows
- Additive Stencil Shadows
它的使用非常简单, 代码如下:
mgr.ShadowTechnique = ShadowTechnique.SHADOWTYPE_STENCIL_ADDITIVE;
现在SceneManager已经使用additive stencil shadows了,我们可以创建一个对象,并显示他的Shadow:
Entity ent = mgr.CreateEntity("ninja", "ninja.mesh"); ent.CastShadows = true; mgr.RootSceneNode.CreateChildSceneNode().AttachObject(ent);
Ogre提供了三种Light
- Point
- Spotlight
- Directional
使用SceneManager的CreateLight方法来创建一个光源:
light = mgr.CreateLight("Light");
light.Type = Light.LightTypes.LT_DIRECTIONAL;
light.DiffuseColour = new ColourValue(.25f, .25f, 0);
light.SpecularColour = new ColourValue(.25f, .25f, 0);
light.Direction = new Vector3(0, -1, -1);
至此,我们的这段代码就完成了,编译运行可以看到如上图示。