sarge
14-02-2006 13:31:44
Hi,
I am very new to Ogre so please forgive me if any of my questions seem very basic. I am an embedded systems programmer who has sone some PC applications by trade, but mostly work in the 8 and 16 bit world. I work with gaming as more of a hobby. But my schooling was in Computer Science so I am strong in object oriented design.
I guess this question is due more to the fact that I have not worked much with wrappers, or applications with dependencies between different languages.
I have been going through the different ogre tutorials and have been doing them in OgreDotNet as well. Is there a way to load DotScene files into OgreDotNet? Has anyone succesfully done this?
I came across this thread here
http://www.ogre3d.org/phpBB2addons/view ... t=dotscene
If anyone has more detail, it would be appreciated.
Thanks
rastaman
14-02-2006 16:40:39
rastaman
14-02-2006 18:12:40
it wasn't the wiki page just the demo source I converted.
ExampleAppliation has changed a bit since the last time I tried this code but I think it will work.
No wrapper code is needed for the Dotsceneoctree plugin, Ogre handles all that. Just compile the DSOT plugin in ogreaddons cvs, folow the tutorial. Then use this code in a new c# project. Make sure you edit your plugins.cfg add the dsot dll, and in resources.cfg add the tutorials media.
using System;
using System.Collections;
using System.Xml;
using System.Drawing;
using Math3D;
using OgreDotNet;
namespace DemoDSOT
{
class cDemoDSOT : ExampleApplication
{
protected OgreDotNet.Log mLog;
protected override void CreateScene()
{
mSceneManager.SetSkyBox(true, "Examples/CloudyNoonSkyBox" );
mLog = LogManager.Singleton.createLog("DemoDSOT.log", false, true );
mLog.LogMessage(string.Format("DemoDSOT log {0}" , System.DateTime.Now ) );
mLog.LogMessage("CreateScene point 1 ");
//create4LineDebugOverLay();
mLog.LogMessage("CreateScene point 2 ");
//Show4LineDebugOverLay();
mLog.LogMessage("CreateScene point 3 ");
mSceneManager.SetWorldGeometry( "MyScene.scene" );
ParseDotScene( "MyScene.scene", "General" );
mCamera.Move( new Vector3(0, 300, 600) );
mCamera.LookAt = new Vector3( 0, 0, 0 );
}
protected void ParseDotScene( string ResourceSceneName, string groupName )
{
XmlDocument doc = new XmlDocument();
string strSceneAsString;
try {
DataStreamPtr Data = ResourceGroupManager.getSingleton().openResource( ResourceSceneName );
strSceneAsString = Data.getAsString();
doc.LoadXml(strSceneAsString);
}
catch ( Exception e)
{
mLog.LogMessage(string.Format("### Exception {0}\n{1}\n{2}",
e.Message ,e.Source , e.StackTrace ) );
mDone=true;
return;
}
try
{
XmlNode root = doc.FirstChild;
if (root.Name != "scene")
throw new Exception("Error: Invalid .scene File. Missing <scene>");
XmlNode nodes = root.FirstChild;
if (nodes.Name != "nodes")
throw new Exception("Error: Invalid .scene File. <scene> first child should be <nodes>");
if (nodes.HasChildNodes) {
foreach( XmlNode xmln in nodes.ChildNodes) {
string sName;
XmlAttribute attr;
sName = ((XmlAttribute)xmln.Attributes.GetNamedItem("name")).Value;
SceneNode n = mSceneManager.GetRootSceneNode().CreateChildSceneNode(sName);
foreach( XmlNode xmlnc in xmln.ChildNodes) {
if (xmlnc.Name == "position") {
Math3D.Vector3 v = Math3D.Vector3.Zero;
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("x");
if (attr != null) v.x = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("y");
if (attr != null) v.y = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("z");
if (attr != null) v.z = Convert.ToSingle(attr.Value);
n.SetPosition( v );
}
else if (xmlnc.Name == "scale") {
Math3D.Vector3 v = Math3D.Vector3.Zero;
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("x");
if (attr != null) v.x = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("y");
if (attr != null) v.y = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("z");
if (attr != null) v.z = Convert.ToSingle(attr.Value);
n.SetScale( v );
}
else if (xmlnc.Name == "rotation") {
Math3D.Quaternion q = Math3D.Quaternion.Identity;
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("qx");
if (attr != null) q.x = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("qy");
if (attr != null) q.y = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("qz");
if (attr != null) q.z = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("qw");
if (attr != null) q.w = Convert.ToSingle(attr.Value);
n.SetOrientation( q );
}
else if (xmlnc.Name == "light") {
OgreDotNet.Light l = LoadLight(xmlnc);
if (l == null)
throw new Exception("Error: LoadLight returned error");
n.AttachObject(l);
}
else if (xmlnc.Name == "entity") {
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("static");
if (attr != null) {
if ( attr.Value.ToLower() == "false" ) {
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("name");
if ((attr == null) || (attr.Value.Trim().Length==0) )
throw new Exception("Error: Invalid .scene File. <node><entity> null attribute name");
string strEName = attr.Value;
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("meshFile");
if ((attr == null) || (attr.Value.Trim().Length==0) )
throw new Exception("Error: Invalid .scene File. <node><entity> null attribute meshFile");
string strEMeshFile = attr.Value;
Entity e = mSceneManager.CreateEntity( strEName, strEMeshFile );
//e.SetMaterialName("Examples/GrassFloor");
//e.SetCastShadows(false);
n.AttachObject(e);
}
}
}
else if (xmlnc.Name == "billboardSet") {
BillboardSet bs = mSceneManager.CreateBillboardSet(sName);
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("type");
if ( (attr ==null) || (attr.Value.Trim().Length==0) )
throw new Exception("Error: Invalid .scene File. <node><billboardSet> null attribute type");
BillboardType bbType;
BillboardOrigin bbOrigin;
if (attr.Value == "orientedCommon")
bbType = BillboardType.OrientedCommon;
else if (attr.Value == "orientedSelf")
bbType = BillboardType.OrientedSelf;
else
bbType = BillboardType.Point;
if (attr.Value == "bottom_left")
bbOrigin = BillboardOrigin.BottomLeft;
else if (attr.Value == "bottom_center")
bbOrigin = BillboardOrigin.BottomCenter;
else if (attr.Value == "bottomRight")
bbOrigin = BillboardOrigin.BottomRight;
else if (attr.Value == "left")
bbOrigin = BillboardOrigin.CenterLeft;
else if (attr.Value == "right")
bbOrigin = BillboardOrigin.CenterRight;
else if (attr.Value == "topLeft")
bbOrigin = BillboardOrigin.TopLeft;
else if (attr.Value == "topCenter")
bbOrigin = BillboardOrigin.TopCenter;
else if (attr.Value == "topRight")
bbOrigin = BillboardOrigin.TopRight;
else
bbOrigin = BillboardOrigin.Center;
bs.SetBillboardType( bbType );
bs.SetBillboardOrigin( bbOrigin );
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("name");
if ( (attr ==null) || (attr.Value.Trim().Length==0) )
throw new Exception("Error: Invalid .scene File. <node><billboardSet> null attribute name");
bs.SetMaterialName(attr.Value);
int width, height;
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("width");
if ( (attr ==null) || (attr.Value.Trim().Length==0) )
throw new Exception("Error: Invalid .scene File. <node><billboardSet> null attribute width");
width = Convert.ToInt32(attr.Value);
attr = (XmlAttribute)xmlnc.Attributes.GetNamedItem("height");
if ( (attr ==null) || (attr.Value.Trim().Length==0) )
throw new Exception("Error: Invalid .scene File. <node><billboardSet> null attribute height");
height = Convert.ToInt32(attr.Value);
bs.SetDefaultDimensions( width, height );
bs.SetVisible( true );
n.AttachObject(bs);
foreach( XmlNode BSChild in xmlnc.ChildNodes) {
if (BSChild.Name == "billboard") {
Billboard bb=null;
Math3D.Vector3 v = Math3D.Vector3.Zero;
float r=1.0f, g=1.0f, b=1.0f, a=1.0f;
foreach( XmlNode child in BSChild.ChildNodes) {
if (child.Name == "position") {
attr = (XmlAttribute)child.Attributes.GetNamedItem("x");
if (attr != null) v.x = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)child.Attributes.GetNamedItem("y");
if (attr != null) v.y = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)child.Attributes.GetNamedItem("z");
if (attr != null) v.z = Convert.ToSingle(attr.Value);
}
else if (child.Name == "colourDiffuse") {
attr = (XmlAttribute)child.Attributes.GetNamedItem("r");
if (attr != null) r = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)child.Attributes.GetNamedItem("g");
if (attr != null) g = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)child.Attributes.GetNamedItem("b");
if (attr != null) b = Convert.ToSingle(attr.Value);
}
}
bb = bs.CreateBillboard( v, Converter.GetColor(r, g, b, a) );
}
}
}
}
}
}
}
catch ( Exception e)
{
mLog.LogMessage(string.Format("### Exception {0}\n{1}\n{2}",
e.Message ,e.Source , e.StackTrace ) );
mDone=true;
return;
}
doc=null;
}
protected OgreDotNet.Light LoadLight( XmlNode xmlLNode )
{
OgreDotNet.Light l=null;
XmlAttribute attr;
string sName;
// Create a light (point | directional | spot | radPoint)
sName = ((XmlAttribute)xmlLNode.Attributes.GetNamedItem("name")).Value;
l = mSceneManager.CreateLight(sName);
attr = (XmlAttribute)xmlLNode.Attributes.GetNamedItem("type");
if ( (attr ==null) || (attr.Value.Trim().Length==0) )
throw new Exception("Error: Invalid .scene File. <node><light> null attribute name");
if (attr.Value == "point")
l.SetLightType(Light.LightTypes.LT_POINT);
else if (attr.Value == "directional")
l.SetLightType(Light.LightTypes.LT_DIRECTIONAL);
else if (attr.Value == "spot")
l.SetLightType(Light.LightTypes.LT_SPOTLIGHT);
else if (attr.Value == "radPoint")
l.SetLightType(Light.LightTypes.LT_POINT);
else
throw new Exception("Error: Invalid .scene File. <node><light> invalid attribute type [" + attr.Value + "]");
foreach( XmlNode child in xmlLNode.ChildNodes) {
if (child.Name == "colourDiffuse") {
float r=0.0f, g=0.0f, b=0.0f, a=1.0f;
attr = (XmlAttribute)child.Attributes.GetNamedItem("r");
if (attr != null) r = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)child.Attributes.GetNamedItem("g");
if (attr != null) g = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)child.Attributes.GetNamedItem("b");
if (attr != null) b = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)child.Attributes.GetNamedItem("a");
if (attr != null) a = Convert.ToSingle(attr.Value);
l.SetDiffuseColour( Converter.GetColor(r, g, b, a) );
}
else if (child.Name == "colourSpecular") {
float r=0.0f, g=0.0f, b=0.0f, a=1.0f;
attr = (XmlAttribute)child.Attributes.GetNamedItem("r");
if (attr != null) r = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)child.Attributes.GetNamedItem("g");
if (attr != null) g = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)child.Attributes.GetNamedItem("b");
if (attr != null) b = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)child.Attributes.GetNamedItem("a");
if (attr != null) a = Convert.ToSingle(attr.Value);
l.SetSpecularColour( Converter.GetColor(r, g, b, a) );
}
else if (child.Name == "lightAttenuation") {
float range, constant, linear, quadratic;
range = l.GetAttenuationRange();
constant = l.GetAttenuationConstant();
linear = l.GetAttenuationLinear();
quadratic = l.GetAttenuationQuadric();
attr = (XmlAttribute)child.Attributes.GetNamedItem("range");
if (attr != null) range = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)child.Attributes.GetNamedItem("constant");
if (attr != null) constant = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)child.Attributes.GetNamedItem("linear");
if (attr != null) linear = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)child.Attributes.GetNamedItem("quadratic");
if (attr != null) quadratic = Convert.ToSingle(attr.Value);
l.SetAttenuation( range, constant, linear, quadratic );
}
else if (child.Name == "position") {
Math3D.Vector3 v = Math3D.Vector3.Zero;
attr = (XmlAttribute)child.Attributes.GetNamedItem("x");
if (attr != null) v.x = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)child.Attributes.GetNamedItem("y");
if (attr != null) v.y = Convert.ToSingle(attr.Value);
attr = (XmlAttribute)child.Attributes.GetNamedItem("z");
if (attr != null) v.z = Convert.ToSingle(attr.Value);
l.SetPosition( v );
}
}
//castShadows (true | false) "true"
l.SetCastShadows( true );
attr = (XmlAttribute)xmlLNode.Attributes.GetNamedItem("castShadows");
if (attr != null)
if (attr.Value == "false" )
l.SetCastShadows( false );
//visible (true | false) "true"
l.SetVisible( true );
attr = (XmlAttribute)xmlLNode.Attributes.GetNamedItem("visible");
if (attr != null)
if (attr.Value == "false" )
l.SetVisible( false );
return l;
}
protected override bool FrameStarted( FrameEvent e )
{
if (!base.FrameStarted( e ))
return false;
return true;
}
protected override bool FrameEnded( FrameEvent e )
{
if (!base.FrameEnded( e ))
return false;
SetDebugCaption( 0, string.Format("Camera Location: ({0}, {1}, {2}) ",
mCamera.GetPosition().x, mCamera.GetPosition().y, mCamera.GetPosition().z ));
SetDebugCaption( 1, string.Format("Camera Orientation: ({0}, {1}, {2}, {3}) ",
mCamera.GetOrientation().x, mCamera.GetOrientation().y, mCamera.GetOrientation().z, mCamera.GetOrientation().w ));
return true;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
cDemoDSOT app = new cDemoDSOT();
try
{
app.Start();
}
catch ( Exception e)
{
Console.WriteLine( "### Exception {0}\n{1}\n{2}",
e.Message ,e.Source , e.StackTrace );
}
finally
{
try
{
app.Dispose();
}
catch
{
}
}
}
}
}
sarge
14-02-2006 18:52:36
Thanks a lot.
I will try it out tonight at home!