学习OGRE有一段时间了,领导为了检测学习效果,根据已有C++项目,弄一个类似的用c#语言编写的小项目。

配置:win7,DirectX2009,vs2010.

项目要求:

1.有Ogre窗口(尺寸1600*900),周围有导航按钮(分别进行旋转拉伸平移操作)
2.导入VIV手臂模型(左右手),默认只显示左手,通过按钮可以进行模型切换显示
3.模型上有完整贴图,点光源绑在照相机上
4.Ogre窗口中开小Viewport,从正上方俯视整条手臂
5.导入消毒动画,并实现手背上消毒效果(本来就是对齐的)
6.导入小垫枕模型,可以通过wasd按键控制其以后
7.通过点击按钮能实现手臂更换贴图

编写时遇到的问题:

1、在新建完项目工程winform后,需要对winform进行配置,此过程中本来是想走捷径, 使用以前拷贝的cfg文件直接添加到项目中(半个月前使用过此方法成功创建项目),导致有错误出现。

2、在设置手臂材料时,最好是新建一个material文件夹。 (设置左手臂时,在以前练习文件的基础上添加左臂材质,始终没有成功, 原因可能是后来的材质带有片段和顶点着色器,而本次没有设置)

实现过程:

1导航按钮

旋转:nodeRoot.Pitch(new Radian(0.5f)); 平移: 左移右移,nodeRoot.Position += new Vector3(-5, 0, 0); nodeRoot.Position += new Vector3(10, 0, 0);

2 导入模型:

将mesh导入到model文件夹中,在代码中引用即可,同时设置材质

entLArm = sceneMgr.CreateEntity("leftArm", "A_N_Y_F_LH_S.mesh");
            SceneNode nodeLArm = sceneMgr.CreateSceneNode("nodelarm");
            nodeRoot.AddChild(nodeLArm);
            nodeLArm.AttachObject(entLArm);
            nodeLArm.SetPosition(0, 0, 0);
            entLArm.SetMaterialName("LArmMaterial");
            entLArm.Visible = showLArm;

3 将点光源绑定到照相机上 定义一个照相机,同时创建一个视口。创建一个节点,将照相机和光源都绑定到此节点即可实现此功能。

SceneNode nodeCam1 = sceneMgr.CreateSceneNode("nodecam");
            nodeCam1.AttachObject(cam1);
            nodeCam1.AttachObject(light1);

4 开一小窗口 因为视角不同,所以新建一个照相机和视口,即可实现。

cam1 = sceneMgr.CreateCamera("Camera1");
            cam1.AutoAspectRatio = true;
            cam1.Position = new Vector3(0,0,300);
            cam1.LookAt(0, 0, 0);
            Viewport vp1 = rWindow.AddViewport(cam1,0,0,0,1,1);
            vp1.BackgroundColour = ColourValue.White;
            cam2 = sceneMgr.CreateCamera("Camera2");
            cam2.AutoAspectRatio = true;
            cam2.Position = new Vector3(-50, 300, 0);
            cam2.LookAt(0, 0, 0);
            Viewport vp2 = rWindow.AddViewport(cam2,1,0,0,0.2f,0.3f);
            vp2.BackgroundColour = ColourValue.Black;

5 导入消毒动画 将消毒动画文件skeleton放入项目文件中,并设置动画状态。

entDisinfection = sceneMgr.CreateEntity("disinfection", "Disinfection_big.mesh");
            animaStateDis = entDisinfection.GetAnimationState("Disinfection_big");
            animaStateDis.Enabled = false;
            animaStateDis.Loop = true;

并在帧监听中添加:

if (animaStateDis != null)
            {
                animaStateDis.AddTime(evt.timeSinceLastFrame);
            }

6 加入垫枕模型,并用wasd键移动它 写入键盘事件:

首先设置:

private void Form1_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
            this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);
        }

void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.KeyDown += new KeyEventHandler(Form1_KeyDown);
            this.KeyUp += new KeyEventHandler(Form1_KeyUp);
        }

void Form1_KeyUp(object sender, KeyEventArgs e)
        {
        }

void Form1_KeyDown(object sender, KeyEventArgs e)
        {
        }

7 更换贴图
实际就是更换材质。将其材质放在帧监听中
switch (stateMaterial)
            {
                case 1:
                    entLArm.SetMaterialName("LArmMaterial");
                    break;
                case 2:
                    entLArm.SetMaterialName("BLArmMaterial");
                    break;
                default :
                    entLArm.SetMaterialName("LArmMaterial");
                    break;
            }

完整的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; using Mogre;
using MogreFramework; namespace MOGREWinform
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); //release resource
Disposed += new EventHandler(Form1_Disposed);
} //dispose resource
void Form1_Disposed(object sender, EventArgs e)
{
this.dispose1();
} public void dispose1()
{
if (mRoot != null)
{
mRoot.Dispose();
mRoot = null;
}
} Root mRoot;
RenderWindow rWindow;
SceneManager sceneMgr;
Camera cam1;
Camera cam2;
SceneNode nodeRoot;
SceneNode nodePillow;
Light light1; Entity entLArm;
Entity entRArm;
Entity entDisinfection; bool showLArm = true;//default show left arm
bool showRArm = false;//show right arm
bool mRotating = false;// mouse rotate
Point mLastPosition;
float Rotate = 0.2f; int stateMaterial = ;//arm 1:left yellow,2:left black,3:right yellow,4:right black,5:disinfection
AnimationState animaStateDis;//disinfection animationstate //set up initialize
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);
}
} //set up 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(); //将panel1作为渲染容器
misc["externalWindowHandle"] = this.panel1.Handle.ToString(); rWindow = mRoot.CreateRenderWindow("Main RenderWindow", , , false, misc); //Init resources
TextureManager.Singleton.DefaultNumMipmaps = ;
ResourceGroupManager.Singleton.InitialiseAllResourceGroups(); //
sceneMgr = mRoot.CreateSceneManager(SceneType.ST_GENERIC); //set nodeRoot
nodeRoot = sceneMgr.CreateSceneNode("node0");
sceneMgr.RootSceneNode.AddChild(nodeRoot);//important
nodeRoot.Position = new Vector3(, , );
//nodeRoot.SetPosition(0, 0, 0); //set up camera,viewport
cam1 = sceneMgr.CreateCamera("Camera1");
cam1.AutoAspectRatio = true;
cam1.Position = new Vector3(,,);
cam1.LookAt(, , ); Viewport vp1 = rWindow.AddViewport(cam1,,,,,);
vp1.BackgroundColour = ColourValue.White; cam2 = sceneMgr.CreateCamera("Camera2");
cam2.AutoAspectRatio = true;
cam2.Position = new Vector3(-, , );
cam2.LookAt(, , ); Viewport vp2 = rWindow.AddViewport(cam2,,,,0.2f,0.3f);
vp2.BackgroundColour = ColourValue.Black; light1 = sceneMgr.CreateLight("light1");
light1.Type = Light.LightTypes.LT_POINT;
light1._setCameraRelative(cam1);
light1.Position = new Vector3(, , );
light1.SetDiffuseColour(, , ); SceneNode nodeCam1 = sceneMgr.CreateSceneNode("nodecam");
//nodeRoot.AddChild(nodeCam1);
nodeCam1.AttachObject(cam1);
nodeCam1.AttachObject(light1); //FrameListener
mRoot.FrameStarted += new FrameListener.FrameStartedHandler(mRoot_FrameStarted); this .panel1 .MouseDown +=new MouseEventHandler(panel1_MouseDown);
this.panel1.MouseUp += new MouseEventHandler(panel1_MouseUp);
this.panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);
} void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (mRotating)
{
float x = mLastPosition.X - Cursor.Position.X;
float y = mLastPosition.Y - Cursor.Position.Y; cam1.Yaw(new Degree(x * Rotate));
cam1.Pitch(new Degree(y * Rotate)); mLastPosition = Cursor.Position;
}
} void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Cursor.Show();
mRotating = false;
}
} void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Cursor.Hide();
mRotating = true;
mLastPosition = Cursor.Position;
}
} bool mRoot_FrameStarted(FrameEvent evt)
{
//change arm
if (showLArm == true)
{
entLArm.Visible = true;
entRArm.Visible = false;
}
else
{
entRArm.Visible = true;
entLArm.Visible = false;
} //
switch (stateMaterial)
{
case :
entLArm.SetMaterialName("LArmMaterial");
break;
case :
entLArm.SetMaterialName("BLArmMaterial");
break;
case :
entRArm.SetMaterialName("RArmMaterial");
break;
case :
entRArm.SetMaterialName("BRArmMaterial");
break;
case :
entLArm.SetMaterialName("DisinfectionMaterial");
break;
case :
entRArm.SetMaterialName("DisinfectionMaterial");
break;
default :
entLArm.SetMaterialName("LArmMaterial");
break;
} //disinfection animation
if (animaStateDis != null)
{
animaStateDis.AddTime(evt.timeSinceLastFrame);
} return true;
} public void run()
{
Show();
while (mRoot != null && mRoot.RenderOneFrame())
{
Application.DoEvents();
}
} //close the Form1
private void btClose_Click(object sender, EventArgs e)
{
this.Close();
} //import left arm
public void importLArm()
{
entLArm = sceneMgr.CreateEntity("leftArm", "A_N_Y_F_LH_S.mesh");
SceneNode nodeLArm = sceneMgr.CreateSceneNode("nodelarm");
nodeRoot.AddChild(nodeLArm);
nodeLArm.AttachObject(entLArm);
nodeLArm.SetPosition(, , );
//entLArm.SetMaterialName("LArmMaterial");
entLArm.Visible = showLArm;
} //import right arm
public void importRArm()
{
entRArm = sceneMgr.CreateEntity("rightArm", "A_N_Y_F_RH_S.mesh");
SceneNode nodeRArm = sceneMgr.CreateSceneNode("noderarm");
nodeRoot.AddChild(nodeRArm);
nodeRArm.AttachObject(entRArm);
//entRArm.SetMaterialName("RArmMaterial");
entRArm.Visible = showRArm;
} //change arm
private void btChangeArm_Click(object sender, EventArgs e)
{
if (showLArm == true)
{
showLArm = false;
showRArm = true;
stateMaterial = ;
}
else
{
showRArm = false;
showLArm = true;
stateMaterial = ;
}
} private void Form1_Load(object sender, EventArgs e)
{
importLArm();
importRArm();
this.KeyPreview = true;
this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);
} #region wasd control
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
this.KeyUp += new KeyEventHandler(Form1_KeyUp);
} void Form1_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.W:
case Keys.S:
case Keys.A:
case Keys.D:
nodePillow.Position += new Vector3(, , );
break;
}
} void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (nodePillow != null)
{
switch (e.KeyCode)
{
case Keys.W:
nodePillow.Position += new Vector3(, , -0.05f);
break;
case Keys.S:
nodePillow.Position += new Vector3(, , 0.02f);
break;
case Keys.A:
nodePillow.Position += new Vector3(-0.02f, , );
break;
case Keys.D:
nodePillow.Position += new Vector3(0.02f, , );
break;
}
}
}
#endregion //rotate
private void btRotate_Click(object sender, EventArgs e)
{
nodeRoot.Pitch(new Radian(0.5f));
} //stretch
private void btStretch_Click(object sender, EventArgs e)
{ } //Translation
private void btLTranslation_Click(object sender, EventArgs e)
{
nodeRoot.Position += new Vector3(-, , );
} private void btRTranslation_Click(object sender, EventArgs e)
{
nodeRoot.Position += new Vector3(, , );
} //add pillow
private void btAddPillow_Click(object sender, EventArgs e)
{
Entity entPillow = sceneMgr.CreateEntity("entpillow", "Pillow2.mesh");
nodePillow = sceneMgr.CreateSceneNode("nodepillow");
nodeRoot.AddChild(nodePillow);
nodePillow.AttachObject(entPillow);
entPillow.SetMaterialName("Pillow2");
this.btAddPillow.Enabled = false;
} //change the material
private void btChangeMap_Click(object sender, EventArgs e)
{
if (entLArm.Visible == true)
{
if ((stateMaterial == )||(stateMaterial == ))
{
stateMaterial = ;
}
else
{
stateMaterial = ;
}
}
else
{
if (stateMaterial == )
{
stateMaterial = ;
}
else
{
stateMaterial = ;
}
}
} //disinfection
private void cbDisinfection_CheckedChanged(object sender, EventArgs e)
{
if (animaStateDis != null)
{
if (cbDisinfection.Checked)
{
if (animaStateDis != null)
{
animaStateDis.Enabled = true;
}
}
else
{
animaStateDis.Enabled = false;
entDisinfection.Visible = false; switch (stateMaterial)
{
case :
case :
stateMaterial = ;
break;
case :
case :
default:
stateMaterial = ;
break;
}
}
}
else
{ MessageBox.Show("没有消毒棒!");
cbDisinfection.Checked = false;
}
} //add stick
private void btAddStick_Click(object sender, EventArgs e)
{
entDisinfection = sceneMgr.CreateEntity("disinfection", "Disinfection_big.mesh");
SceneNode nodeDisifection = sceneMgr.CreateSceneNode("nodedisifection");
nodeRoot.AddChild(nodeDisifection);
nodeDisifection.AttachObject(entDisinfection);
nodeDisifection.Position = new Vector3(, , );
this.btAddStick.Enabled = false;
animaStateDis = entDisinfection.GetAnimationState("Disinfection_big");
animaStateDis.Enabled = false;
animaStateDis.Loop = true;
} private void btTwodisinfection_Click(object sender, EventArgs e)
{
entDisinfection.Visible = true;
} //private void btAddBaby_Click(object sender, EventArgs e)
//{
// Entity entBaby = sceneMgr.CreateEntity("baby", "VIV1.1_head_baby.mesh");
// SceneNode nodeBaby = sceneMgr.CreateSceneNode("nodebaby");
// nodeRoot.AddChild(nodeBaby);
// nodeBaby.AttachObject(entBaby);
// entBaby.SetMaterialName("VIV1.1_head_baby");
// //nodeBaby.Scale(5, 5, 5);
// this.btAddBaby.Enabled = false;
//} }
}

总结:虽然是一个很小很小的项目,还是有一点体悟:

1、纸上得来终觉浅,绝知此事要躬行。事前感觉到很简单的事,做起来可能制造点麻烦。

2、理解需求很重要。深入、正确的理解需求,后来编写代码事半功倍。

3、思路。思路很重要,如果思路不对,问题永远是不可能解决的。

MOGRE学习笔记(3)--MOGRE小项目练习的更多相关文章

  1. 微信小程序开发:学习笔记[7]——理解小程序的宿主环境

    微信小程序开发:学习笔记[7]——理解小程序的宿主环境 渲染层与逻辑层 小程序的运行环境分成渲染层和逻辑层. 程序构造器

  2. MOGRE学习笔记(1) - OGRE简介及在vs2010下配置

    由于工作需要,花费了一段时间研究OGRE,但是研究的目的是要在vs2010平台下用c#进行MOGRE的开发,不得已才转到MGRE,步骤是首选熟悉MOGRE的一些基础知识,做到在winform下能用MO ...

  3. Maven学习笔记-03-Eclipse下maven项目在Tomcat7和Jetty6中部署调试

    现在最新的Eclipse Luna Release 已经内置了Maven插件,这让我们的工作简洁了不少,只要把项目直接导入就可以,不用考虑插件什么的问题,但是导入之后的项目既可以部署在Tomcat也可 ...

  4. springmvc学习笔记---idea创建springmvc项目

    前言: 真的是很久没搞java的web服务开发了, 最近一次搞还是读研的时候, 想来感慨万千. 英雄没落, Eclipse的盟主地位隐隐然有被IntelliJ IDEA超越的趋势. Spring从2. ...

  5. Android(java)学习笔记207:开源项目使用之gif view

    1. 由于android没有自带的gif动画,我在Android(java)学习笔记198:Android下的帧动画(Drawable Animation) 播客中提到可以使用AnimationVie ...

  6. Maven学习笔记-04-Eclipse下maven项目在Tomcat7和Jetty6中部署调试

    现在最新的Eclipse Luna Release 已经内置了Maven插件,这让我们的工作简洁了不少,只要把项目直接导入就可以,不用考虑插件什么的问题,但是导入之后的项目既可以部署在Tomcat也可 ...

  7. flink学习笔记-快速生成Flink项目

    说明:本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKh ...

  8. Android(java)学习笔记150:开源项目使用之gif view

    1. 由于android没有自带的gif动画,我在Android(java)学习笔记198:Android下的帧动画(Drawable Animation) 播客中提到可以使用AnimationVie ...

  9. Docker学习笔记之--.Net Core项目容器连接mssql容器(环境:centos7)

    前一节演示在docker中安装mssql,地址:Docker学习笔记之--安装mssql(Sql Server)并使用Navicat连接测试(环境:centos7) 本节演示 .Net Core项目容 ...

随机推荐

  1. opengl画圆

    通过这个例子可以更加深刻的了解割圆术的原理,明白如何的化曲为直,且看代码: #include <windows.h> //#include <GLUT/glut.h> #inc ...

  2. NBU7.0 Image Cleanup作业在没有配置hot catalog backup的情况下失败,Status=1

    Issue NBU7.0 Image Cleanup作业在没有配置hot catalog backup的情况下失败,Status=1 Error NBU7.0 Image Cleanup作业失败, D ...

  3. virt-XXX

    尽管 virt-manager 是 libvirt 虚拟化 API 的一个首要用户,但有一个越来越庞大的工具生态系统在使用此接口进行虚拟化管理.virt-manager 包提供了一个便捷的 GUI,用 ...

  4. POJ 1511 最短路spfa

    题很简单 就是有向图中求给出的源点到其余所有点的最短路的和与其余所有点到源点的最短路之和 一开始以为dij对于正权图的单源最短路是最快的 写了一发邻接表的dij 结果超时 把所有的cin改成scanf ...

  5. shell 中的引用

    1. 什么叫引用对 shell 脚本.程序.终端命令.变量.字符串等结果的反馈.2. 引用的类型 " " 双引号           ` 反引号' ' 单引号           ...

  6. svn local obstruction, incoming add upon merge

    http://little418.com/2009/05/svn-local-obstruction-incoming-add-upon-merge.html If you've found this ...

  7. 一个Delphi7的BUG

    combobox有个属性DropDownCount可以控制显示的下拉数量, 但是 在Delphi7中, TCombobox或者任何从TCustomComboBox继承下来的类, 在windows7环境 ...

  8. Link Management Protocol (LMP)

    1.1. Link Management Protocol (LMP)   1.1.1.   Introduction and Theory The Link Manager (LM) transla ...

  9. iOS简单排序--字母排序、NSDictionary排序

    // 数组用系统方法compare做字母的简单排序 NSArray *oldArray = @[@"bac",@"bzd",@"azc",@ ...

  10. Oracle一些基本操作

    查看表以及列: Select * From all_tables where owner = 'userName' ---注意,这里需要区分大小写! select * from user_tab_co ...