现在的位置: 首页 > 综合 > 正文

Mogre学习系列(3)基本概念Cameras, Lights, and Shadows

2013年03月01日 ⁄ 综合 ⁄ 共 3883字 ⁄ 字号 评论关闭

1.Camera用来观察我们创建的对象,它的使用方法和组织方式有点类似SceneNode。基本上我们可以将其视为特殊的SceneNode。同样需要设置Position属性来确定方位。

 

2.如果我们想要设置Camera的渲染范围,也就是说某个物体在多近(远)的距离就看不见了,可以设置Camera.NearClipDistance或者Camera.FarClipDistance。

 

3.当有了Camera对象,我们如何告诉窗体使用哪个Camera?(因为我们可以在一个窗体中创建多个Camera对象)这个时侯我们需要Viewport对象。Viewport = this.RenderWindow.AddViewport(Camera)

 

4.当我们在屏幕上绘制了物体,也设定了观察Camera,此时我们还是不能看到物体,原因在于没有光。因此我们需要设定一些光源Light,来照亮物体。  

 

5.有了光源,当光线照在物体上的时候,就会差生阴影,我们也可以设定阴影的显示。例如:mgr.ShadowTechnique = ShadowTechnique.SHADOWTYPE_STENCIL_ADDITIVE;

 

 参考代码:

 

using System;
using System.Collections.Generic;
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);

             Viewport.BackgroundColour = ColourValue.Black;
             Camera.AspectRatio = (float)Viewport.ActualWidth / Viewport.ActualHeight;

       }
     }

     class SceneCreator
     {
         public SceneCreator(OgreWindow win)
         {
             win.SceneCreating += new OgreWindow.SceneEventHandler(SceneCreating);
         }

         void SceneCreating(OgreWindow win)
         {
             // Set the ambient light and shadow technique
             SceneManager mgr = win.SceneManager;
             mgr.SetShadowUseInfiniteFarPlane(false);
             mgr.AmbientLight = ColourValue.Black;
             mgr.ShadowTechnique = ShadowTechnique.SHADOWTYPE_STENCIL_ADDITIVE;

             // Create a ninja
             Entity ent = mgr.CreateEntity("ninja", "ninja.mesh");
             ent.CastShadows = true;
             mgr.RootSceneNode.CreateChildSceneNode().AttachObject(ent);

             // Define a ground plane
             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);

             // Create a ground plane
             ent = mgr.CreateEntity("GroundEntity", "ground");
             mgr.RootSceneNode.CreateChildSceneNode().AttachObject(ent);

             ent.SetMaterialName("Examples/Rockwall");
             ent.CastShadows = false;

            // Create the first light
             Light light;
             light = mgr.CreateLight("Light1");
             light.Type = Light.LightTypes.LT_POINT;
             light.Position = new Vector3(0, 150, 250);
             light.DiffuseColour = ColourValue.Red;
             light.SpecularColour = ColourValue.Red;

             // Create the second light
             light = mgr.CreateLight("Light2");
             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);

             // Create the third light
             light = mgr.CreateLight("Light3");
             light.Type = Light.LightTypes.LT_SPOTLIGHT;
             light.DiffuseColour = ColourValue.Blue;
             light.SpecularColour = ColourValue.Blue;
             light.Direction = new Vector3(-1, -1, 0);
             light.Position = new Vector3(300, 300, 0);
             light.SetSpotlightRange(new Degree(35), new Degree(50));
         }
     }
}

抱歉!评论已关闭.