还是先给出代码:
using System;
using System.Windows.Forms;
using MogreFramework;
using Mogre;
namespace Tutorial03
{
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 CreateSceneManager()
{
SceneManager = Root.CreateSceneManager(SceneType.ST_EXTERIOR_CLOSE);
}
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)
{
ColourValue fadeColour = new ColourValue(0.1f, 0.1f, 0.1f);
win.Viewport.BackgroundColour = fadeColour;
win.SceneManager.SetFog(FogMode.FOG_LINEAR, fadeColour, 0, 10, 150);
win.SceneManager.SetWorldGeometry("terrain.cfg");
Plane plane;
plane.d = 10;
plane.normal = Vector3.NEGATIVE_UNIT_Y;
win.SceneManager.SetSkyPlane(true, plane, "Examples/SpaceSkyPlane", 100, 45, true, 0.5f, 150, 150);
}
}
}
运行结果截图如下:
接下来,我们就对其做相应的解释:
Root对象是OGRE的核心对象,在我的OGRE学习笔记中可以看到Root和其他对象的UML关系图。在本例中,我们需要用Root新建一个SceneManager,代码如下:
protected override void CreateSceneManager() { SceneManager = Root.CreateSceneManager(SceneType.ST_EXTERIOR_CLOSE); }
在本例中,我们要改变MogreFramework原来提供的Terrain,SceneManager提供了SetWorldGeometry:
win.SceneManager.SetWorldGeometry("terrain.cfg");
Sky
本例中使用了SkyPlane,代码如下:
Plane plane;
plane.d = 10;
plane.normal = Vector3.NEGATIVE_UNIT_Y;
win.SceneManager.SetSkyPlane(true, plane, "Examples/SpaceSkyPlane", 100, 45, true, 0.5f, 150, 150);
Ogre中,Fog的使用非常简单, 主要有两种基本类型的Fog:
在本例中,使用了linear的Fog,代码如下:
ColourValue fadeColour = new ColourValue(0.1f, 0.1f, 0.1f);
win.Viewport.BackgroundColour = fadeColour;
win.SceneManager.SetFog(FogMode.FOG_LINEAR, fadeColour, 0, 10, 150);
按F5运行,可以看到以上结果了。