---
title: "Mogre学习笔记（二）"
author: "zzkathy"
date: "2008-07-31T15:47:00Z"
tags: []
slug: "mogre"
draft: false
lastmod: "2008-07-31T15:47:00Z"
id: 391738
url: "https://zzkathy.iteye.com/blog/391738"
---

**Section 2: Terrain, Sky, Fog, and the Root object**

还是先给出代码：

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);

}  
}  
}

运行结果截图如下：

![image](http://v1zduq.bay.livefilestore.com/y1pIFAllAfznRbKIDD2gOGPVZGXSPXzbPbb4GAH6H5AB9tEzax8kguOKTHh7-dtIR1VpLRIfwRFwcI?PARTNER=WRITER)

接下来，我们就对其做相应的解释：

##### Root

Root对象是OGRE的核心对象，在我的OGRE学习笔记中可以看到Root和其他对象的UML关系图。在本例中，我们需要用Root新建一个SceneManager，代码如下：

```
protected override void CreateSceneManager()
{
    SceneManager = Root.CreateSceneManager(SceneType.ST_EXTERIOR_CLOSE);
}
```

[]()

#### Terrain

在本例中，我们要改变MogreFramework原来提供的Terrain，SceneManager提供了SetWorldGeometry：

```
win.SceneManager.SetWorldGeometry("terrain.cfg");
```

**Sky**

1. SkyBox： 一个巨大的立方体形的天空，它环绕在场景中所有的对象周围；
2. SkyDome：类似于SkyBox，但是它是圆屋顶形，不会盖住地面；
3. SkyPlane：它与SkyBox和SkyDome有很大的不同，是一个平面的天空。

本例中使用了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);

#### Fog

Ogre中，Fog的使用非常简单， 主要有两种基本类型的Fog：

1. linear：雾线性的变浓，
2. exponential：雾指数级的变浓。

在本例中，使用了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运行，可以看到以上结果了。

&lt;nobr id="blogDT"&gt;15:14:54&lt;/nobr&gt;| &lt;nobr dir="ltr"&gt;&lt;a id="blogAddComment0" href="http://zengzheng2001.spaces.live.com/blog/cns!189D196A5ED03394!992.entry#post" bvitemtype="post"&gt;&lt;font color="#4389cf"&gt;添加评论&lt;/font&gt;&lt;/a&gt;&lt;/nobr&gt;| &lt;nobr dir="ltr"&gt;&lt;a id="blogSendMessage0" title="给作者发送消息" href="http://spaces.live.com/api.aspx?wx\_action=sendMessage&amp;wxp\_toCid=1773601772901774228&amp;wx\_ru=http%3a%2f%2fzengzheng2001.spaces.live.com%2fdefault.aspx%3f\_c11\_BlogPart\_pagedir%3dNext%26\_c11\_BlogPart\_handle%3dcns!189D196A5ED03394!987%26\_c11\_BlogPart\_BlogPart%3dblogview%26\_c%3dBlogPart&amp;wxp\_subject=%e7%ad%94%e5%a4%8d%ef%bc%9a%e6%82%a8%e7%9a%84%e6%97%a5%e5%bf%97%e2%80%9cMogre%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%ef%bc%88%e4%ba%8c%ef%bc%89%e2%80%9d&amp;wxp\_body=%3cdiv%3e%3cbr+%2f%3e%3cbr+%2f%3e%3chr+%2f%3e%e6%9f%a5%e7%9c%8b%e6%97%a5%e5%bf%97%ef%bc%9a%3ca+href%3d%22http%26%2358%3b%26%2347%3b%26%2347%3bzengzheng2001.spaces.live.com%26%2347%3bblog%26%2347%3bcns%26%2333%3b189D196A5ED03394%26%2333%3b992.entry%22%3ehttp%26%2358%3b%26%2347%3b%26%2347%3bzengzheng2001.spaces.live.com%26%2347%3bblog%26%2347%3bcns%26%2333%3b189D196A5ED03394%26%2333%3b992.entry%3c%2fa%3e%3c%2fdiv%3e"&gt;&lt;font color="#4389cf"&gt;发送消息&lt;/font&gt;&lt;/a&gt;&lt;/nobr&gt;| &lt;nobr dir="ltr"&gt;&lt;a id="blogPermalink0" title="此日志的固定链接" href="http://zengzheng2001.spaces.live.com/blog/cns!189D196A5ED03394!992.entry"&gt;&lt;font color="#4389cf"&gt;固定链接&lt;/font&gt;&lt;/a&gt;&lt;/nobr&gt;| &lt;nobr dir="ltr"&gt;&lt;a id="blogTrackbacks0" href="http://zengzheng2001.spaces.live.com/blog/cns!189D196A5ED03394!992.entry#trackback" bvitemtype="trackback"&gt;&lt;font color="#4389cf"&gt;查看引用通告 (0)&lt;/font&gt;&lt;/a&gt;&lt;/nobr&gt;| &lt;nobr dir="ltr"&gt;&lt;a id="blogThis0" title="写入您的共享空间日志" href="javascript:BlogIt('cns!189D196A5ED03394!992');"&gt;&lt;font color="#4389cf"&gt;写入日志&lt;/font&gt;&lt;/a&gt;&lt;/nobr&gt;| &lt;nobr dir="ltr"&gt;&lt;a id="blogCategory0" title="显示此类别的所有日志" href="http://zengzheng2001.spaces.live.com/?\_c11\_BlogPart\_BlogPart=blogview&amp;\_c=BlogPart&amp;partqs=cat%3d%25e8%25ae%25a1%25e7%25ae%2597%25e6%259c%25ba%25e4%25b8%258e%2bInternet"&gt;&lt;font color="#4389cf"&gt;计算机与 Internet&lt;/font&gt;&lt;/a&gt;&lt;/nobr&gt;