Mogre学习笔记(五)

Section 5: Embed Ogre in a Windows.Forms window

很多时候,我们需要把Ogre放到一个Windows.Form中,以方便使用,下面给出代码先:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mogre;

namespace Tutorial06
{
public partial class OgreForm : Form
{
Root mRoot;
RenderWindow mWindow;

public OgreForm()
{
this.Size = new Size(800, 600);
//Disposed += new EventHandler(OgreForm_Disposed);
Resize += new EventHandler(OgreForm_Resize);

InitializeComponent();
}

void OgreForm_Resize(object sender, EventArgs e)
{
mWindow.WindowMovedOrResized();
}

public void Go()
{
Show();
while (mRoot != null && mRoot.RenderOneFrame())
Application.DoEvents();
}

public void Init()
{
// Create root object
mRoot = new Root();

// Define Resources
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);
}
}

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

// Create Render Window
mRoot.Initialise(false, "Main Ogre Window");
NameValuePairList misc = new NameValuePairList();
misc["externalWindowHandle"] = Handle.ToString();
mWindow = mRoot.CreateRenderWindow("Main RenderWindow", 800, 600, false, misc);

// Init resources
TextureManager.Singleton.DefaultNumMipmaps = 5;
ResourceGroupManager.Singleton.InitialiseAllResourceGroups();

// Create a Simple Scene
SceneManager mgr = mRoot.CreateSceneManager(SceneType.ST_GENERIC);
Camera cam = mgr.CreateCamera("Camera");
cam.AutoAspectRatio = true;
mWindow.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);

}

}
}

注意看红色部分代码,我们必须调用Root的CreateRenderWindow方法创建一个Render Window,传给其一个句柄,作为这个Render Window的容器。

最后我们在另一个cs文件中添加如下代码,运行即可:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Tutorial06
{
static class Program
{
[STAThread]
static void Main()
{
OgreForm form = new OgreForm();
form.Init();
form.Go();
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值