oFusion and Mogre

pepote

17-06-2007 12:29:44

Where do I put the code in my MOGRE app to run the oFusion Manager?

OSMLoader loader = new OSMLoader(sceneMgr, window);
loader.Initialize("test.osm");
loader.CreateScene(sceneMgr.RootSceneNode);



using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Mogre;
using System.Drawing;
using MOIS;

namespace MogreWindow
{
static class Program
{
[MTAThread]
static void Main()
{
OgreStartup ogre = new OgreStartup();
ogre.Go();
}
}

class OgreStartup
{
protected RenderWindow mWindow;
protected InputManager mInputManager;
protected Keyboard mKeyBoard;
protected Mouse mMouse;
protected Camera cam;

const float TRANSLATE = 200;
const float ROTATE = 0.2f;
bool mRotating = false;
Mogre.Vector3 mTranslation = Mogre.Vector3.ZERO;
Point mLastPosition;

Root mRoot = null;

public void Go()
{
CreateRoot();
DefineResources();
SetupRenderSystem();
CreateRenderWindow();
InitializeResourceGroups();
CreateScene();
StartRenderLoop();
}

void CreateRoot()
{
mRoot = new Root();
//mWindow = mRoot.Initialise(true);
}

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.");
}

void CreateRenderWindow()
{
mWindow = mRoot.Initialise(true, "Mogre Example");
mRoot.FrameEnded += new FrameListener.FrameEndedHandler(FrameEnded);
//mRoot.FrameStarted += new FrameListener.FrameStartedHandler(FrameStarted);

//Create Input Manager
ParamList pl = new ParamList();
IntPtr windowHnd = new IntPtr(0);
mWindow.GetCustomAttribute("WINDOW", out windowHnd);
pl.Insert("WINDOW", windowHnd.ToString());


mInputManager = MOIS.InputManager.CreateInputSystem(pl);

mKeyBoard = (MOIS.Keyboard)mInputManager.CreateInputObject(MOIS.Type.OISKeyboard, true);
//mMouse = (MOIS.Mouse)mInputManager.CreateInputObject(MOIS.Type.OISMouse, true);

// Register input listeners
mKeyBoard.KeyPressed += new KeyListener.KeyPressedHandler(KeyPressed);
mKeyBoard.KeyReleased += new KeyListener.KeyReleasedHandler(KeyReleased);
//mMouse.MouseMoved += new MouseListener.MouseMovedHandler(MouseMoved);

}

void InitializeResourceGroups()
{
TextureManager.Singleton.DefaultNumMipmaps = 5;
ResourceGroupManager.Singleton.InitialiseAllResourceGroups();
}

private void CreateScene()
{
SceneManager mgr = mRoot.CreateSceneManager(SceneType.ST_GENERIC);
cam = mgr.CreateCamera("Camera");
mgr.AmbientLight = ColourValue.Black;
mgr.ShadowTechnique = ShadowTechnique.SHADOWTYPE_STENCIL_ADDITIVE;
CreateViewPort(cam);

//Creamos un personaje
Entity ent = mgr.CreateEntity("ninja", "ninja.mesh");
ent.CastShadows = true;
mgr.RootSceneNode.CreateChildSceneNode().AttachObject(ent);

//Creamos el escenario a partir de un plano
Plane plane = new Plane(Mogre.Vector3.UNIT_Y, 0);

MeshManager.Singleton.CreatePlane("ground",
ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, plane,
1500, 1500, 20, 20, true, 1, 5, 5, Mogre.Vector3.UNIT_Z);

//Creamos el terreno
ent = mgr.CreateEntity("GroundEntity", "ground");
mgr.RootSceneNode.CreateChildSceneNode().AttachObject(ent);
ent.SetMaterialName("Examples/Rockwall");
ent.CastShadows = false;
//mgr.SetWorldGeometry("terrain.cfg");

//Creamos las luces
//luz 1 de punto fijo
Light light;
light = mgr.CreateLight("Light1");
light.Type = Light.LightTypes.LT_POINT;
light.Position = new Mogre.Vector3(0, 150, 250);
light.DiffuseColour = ColourValue.Red;
light.SpecularColour = ColourValue.Red;

/*//luz 2 direccional
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 Mogre.Vector3(0, -1, -1);


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


//luz 4 foco cenital
light = mgr.CreateLight("Light4");
light.Type = Light.LightTypes.LT_SPOTLIGHT;
light.DiffuseColour = ColourValue.White;
light.SpecularColour = ColourValue.White;
light.Direction = new Mogre.Vector3(0, 0, 20);
light.Position = new Mogre.Vector3(0, 120, -400);*/
//light.SetAttenuation(20.20f, 200f, 30f, 30f);


cam.Position = new Mogre.Vector3(0, 200, -600);
//cam.FarClipDistance = 500;
cam.LookAt(ent.BoundingBox.Center);
//mgr.SetShadowUseInfiniteFarPlane(false);


}

bool FrameEnded(FrameEvent evt)
{
// Quit if window is closed
if (mWindow.IsClosed)
return false;

if (mKeyBoard != null)
{
mKeyBoard.Capture();
if (mKeyBoard.IsKeyDown(KeyCode.KC_ESCAPE))
{
return false;
}
}
/*if (mMouse != null)
mMouse.Capture();*/

// cam.Position += cam.Orientation * mTranslation * evt.timeSinceLastFrame;
return true;
}




// KeyListener
public virtual bool KeyPressed(KeyEvent e)
{
// Start repeating key
if (e.key == KeyCode.KC_ESCAPE)
{
return false;
}

switch (e.key)
{
case KeyCode.KC_UP:
case KeyCode.KC_W:
mTranslation.z = -TRANSLATE;
break;

case KeyCode.KC_DOWN:
case KeyCode.KC_S:
mTranslation.z = TRANSLATE;
break;

case KeyCode.KC_LEFT:
case KeyCode.KC_A:
mTranslation.x = -TRANSLATE;
break;

case KeyCode.KC_RIGHT:
case KeyCode.KC_D:
mTranslation.x = TRANSLATE;
break;

case KeyCode.KC_PGUP:
case KeyCode.KC_Q:
mTranslation.y = TRANSLATE;
break;

case KeyCode.KC_PGDOWN:
case KeyCode.KC_E:
mTranslation.y = -TRANSLATE;
break;
}

return true;
}

public virtual bool KeyReleased(KeyEvent e)
{
switch (e.key)
{
case KeyCode.KC_UP:
case KeyCode.KC_W:
case KeyCode.KC_DOWN:
case KeyCode.KC_S:
mTranslation.z = 0;
break;

case KeyCode.KC_LEFT:
case KeyCode.KC_A:
case KeyCode.KC_RIGHT:
case KeyCode.KC_D:
mTranslation.x = 0;
break;

case KeyCode.KC_PGUP:
case KeyCode.KC_Q:
case KeyCode.KC_PGDOWN:
case KeyCode.KC_E:
mTranslation.y = 0;
break;
}

return true;
}

void StartRenderLoop()
{
mRoot.StartRendering();
}

void CreateInputManager()
{
// mInputKeyboard = (MOIS.Keyboard)mInputManager.CreateInputObject(MOIS.Type.OISKeyboard, BufferedInput);
}

void CreateViewPort(Camera cam)
{
Viewport vport = mWindow.AddViewport(cam);
vport.BackgroundColour = ColourValue.Black;
//cam.AspectRatio = vport.ActualWidth / vport.ActualHeight;
cam.NearClipDistance = 20;

}
}
}

smernesto

17-06-2007 17:34:42

You can write the code for OSM loading after you initialize ogre.
The OSM loader creates a new scenemanager, cameras if you have in the osm file, lights and entities. Next you can add more entities manually.

wingnet

18-06-2007 07:23:12

I'm curious whether there is a .net version OSM loader?

thank u for the tip.

smernesto

18-06-2007 07:36:45

Yes there are 2 loaders, one written by me and another one written before my port.

I rewrote the .net osm loader because the another seems to have some bugs .
Loader 1 http://www.ogre3d.org/wiki/index.php/MOGRE_OSMSceneLoader

My Loader http://www.ogre3d.org/wiki/index.php/MOGRE_OSMSceneLoader_2

Please try both and post comments if you find bugs.

Thanks

Ernesto

pepote

18-06-2007 20:38:30

I tried both and your OSMloader is best in my point of view. The first OSMloader has a bug on Ambient Light your OSMloader seem like the oFusion Viewport on 3dMax.

See this pictures:

OSMLoader




Ernesto's OSMLoader

wingnet

20-06-2007 09:48:14

thank u smernesto
I will try it.