Mogre學習系列(5)如何使用Ogre建立遊戲程式

來源:互聯網
上載者:User

      前面我們已經學會了如何使用Mogre來建立一個3D遊戲,但是那是建立在一個叫做MogreFramework.dll的基礎上。如果我們想直接利用Ogre的封裝API來建立遊戲,又是如何做到呢?

 

1.首先需要遵循一定的建立順序。具體來說,包括一下步驟:

 CreateRoot();
 DefineResources();
 SetupRenderSystem();
 CreateRenderWindow();
 InitResources();
 CreateScene();
 StartRenderLoop();

 

下面我們分別解釋。

1)CreateRoot

還記得我們之前說的,在Ogre的世界裡面,對象的組織是按照node的方式來建立的,因此,一開始我們需要建立一個root node。例如:

mRoot = new Root();


2) DefineResources

建立完root node之後,我們需要載入遊戲中所用到的資源。我們一般將資源路徑資訊存放在config檔案中。因此需要先解析config檔案的內容,例如:

           ConfigFile cf = new ConfigFile();
           cf.Load("resources.cfg", "/t:=", true);
           ConfigFile.SectionIterator seci = cf.GetSectionIterator();
           String secName, typeName, archName;

           while (seci.MoveNext())
           {
               secName = seci.CurrentKey;
               ConfigFile.SettingsMultiMap settings = seci.Current;
               foreach (KeyValuePair<string, string> pair in settings)
               {
                   typeName = pair.Key;
                   archName = pair.Value;
                   ResourceGroupManager.Singleton.AddResourceLocation(archName, typeName, secName);
               }
           }

 

注意,上面的代碼只是解析了資源檔的路徑和類別,但是並沒有將資源載入到記憶體中。

 

3)SetupRenderSystem

之後我們需要設定Render System。例如究竟是使用DirectX3D還是OpenGL,是否全屏和顯示模式等。例如:

RenderSystem rs = mRoot.GetRenderSystemByName("Direct3D9 Rendering Subsystem");    // or use "OpenGL Rendering Subsystem"
mRoot.RenderSystem = rs;
rs.SetConfigOption("Full Screen", "No");
rs.SetConfigOption("Video Mode", "800 x 600 @ 32-bit colour");

 

4)CreateRenderWindow

設定完RenderSystem之後,我們需要建立RenderWindow,就是用來顯示我們以後建立遊戲對象的視窗。例如:

mRoot.Initialise(true, "Main Ogre Window");

 

如果我們想要將RenderWindow嵌在Windows Form裡面,代碼如下:

 

  NameValuePairList misc = new NameValuePairList();
           misc["externalWindowHandle"] = handle.ToString();
           RenderWindow win = mRoot.CreateRenderWindow("Main RenderWindow", 800, 600, false, misc);


 5)InitializeResourceGroups

在2)中我們制定了資源的路徑和group資訊,但是我們並沒有將資源載入到記憶體中。為了實現資源載入,我們需要調用:

ResourceGroupManager.Singleton.InitialiseAllResourceGroups();

這樣,我們在遊戲中需要的紋理和貼圖就可以使用了,否則就會看到一個沒有白色的空殼。(如果是一個3D物體的話)

 

6)CreateScene

準備工作已經就緒,我們可以開始建立遊戲情境了。包括SceneManager, Entity, SceneNode, Camera, Light, Viewport等。具體內容可以查看學習系列的(1)-(4)節。

 

7)StartRenderingLoop

遊戲情境已經建立完畢,之後我們可以啟動Rendering了。通過功能,遊戲情境會被渲染和顯示出來。

           mRoot.StartRendering();

當然了,如果我們希望每次渲染frame的時候都能夠添加自己的控制邏輯,可以使用

while (mRoot.RenderOneFrame())

{

  // Do other things here, such as sleep for a short period of time

}

 

注意,這裡面有一個重要的topic沒有涵蓋,就是Game Input System。通常我們可以使用DirectX Input System.

 
範例程式碼:

 using System; using System.Collections.Generic; using System.Windows.Forms; using Mogre; using System.Drawing;  namespace Tutorial05 {     static class Program     {         [STAThread]         static void Main()         {             OgreStartup ogre = new OgreStartup();             ogre.Go();         }     }      class OgreStartup     {         Root mRoot = null;         float ticks = 0;          public void Go()         {             CreateRoot();             DefineResources();             SetupRenderSystem();             CreateRenderWindow();             InitResources();             CreateScene();             StartRenderLoop();         }          void CreateRoot()         {             mRoot = new Root();         }          void DefineResources()         {             ConfigFile cf = new ConfigFile();             cf.Load("resources.cfg", "/t:=", true);             ConfigFile.SectionIterator seci = cf.GetSectionIterator();             String secName, typeName, archName;              while (seci.MoveNext())             {                 secName = seci.CurrentKey;                 ConfigFile.SettingsMultiMap settings = seci.Current;                 foreach (KeyValuePair<string, string> pair in settings)                 {                     typeName = pair.Key;                     archName = pair.Value;                     ResourceGroupManager.Singleton.AddResourceLocation(archName, typeName, secName);                 }             }         }          void SetupRenderSystem()         {             if (!mRoot.ShowConfigDialog())                 throw new Exception("The user canceled the configuration dialog.");              //// Setting up the RenderSystem manually.             //RenderSystem rs = mRoot.GetRenderSystemByName("Direct3D9 Rendering Subsystem");             //                                    // or use "OpenGL Rendering Subsystem"             //mRoot.RenderSystem = rs;             //rs.SetConfigOption("Full Screen", "No");             //rs.SetConfigOption("Video Mode", "800 x 600 @ 32-bit colour");         }          void CreateRenderWindow()         {             mRoot.Initialise(true, "Main Ogre Window");              //// Embedding ogre in a windows hWnd.  The "handle" variable holds the hWnd.             //NameValuePairList misc = new NameValuePairList();             //misc["externalWindowHandle"] = handle.ToString();             //mWindow = mRoot.CreateRenderWindow("Main RenderWindow", 800, 600, false, misc);         }          void InitResources()         {             TextureManager.Singleton.DefaultNumMipmaps = 5;             ResourceGroupManager.Singleton.InitialiseAllResourceGroups();         }          void CreateScene()         {             SceneManager mgr = mRoot.CreateSceneManager(SceneType.ST_GENERIC);             Camera cam = mgr.CreateCamera("Camera");             mRoot.AutoCreatedWindow.AddViewport(cam);              Entity ent = mgr.CreateEntity("ninja", "ninja.mesh");             mgr.RootSceneNode.CreateChildSceneNode().AttachObject(ent);              cam.Position = new Vector3(0, 200, -400);             cam.LookAt(ent.BoundingBox.Center);              mRoot.FrameEnded += new FrameListener.FrameEndedHandler(FrameEnded);             ticks = Environment.TickCount;         }          void StartRenderLoop()         {             mRoot.StartRendering();              //// Alternate Rendering Loop             //while (mRoot.RenderOneFrame())             //{             //    // Do other things here, such as sleep for a short period of time             //}         }          bool FrameEnded(FrameEvent evt)         {             if (Environment.TickCount - ticks > 5000)                 return false;              return true;         }     } }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.