Google

An alternative method to use Ogre from managed code

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.

Moderators: Moderators, OGRE Team

An alternative method to use Ogre from managed code

Postby Maleficus » Sun Aug 07, 2005 10:29 pm

Sorry if I'm rambling a bit, but I hope other people in my position can learn something from my experiences.

For a long time I've been in the awkward position of being a C# lover, yet also loving graphics programming.

I've tried doing my own engines, and have done some things that are pretty decent (imho), but well, making your own engine is time consuming. I know I can do it, but my free time isn't limitless. So not long ago I decided to use existing engines.

To my knowledge the only native C# choice is Axiom. It's a good engine, it's only problem is that the C# port of CEGUI seems to be broken/bugged. And since I know from previous experience how much coding a gui sucks, this was enough to make me lose interest. Axiom's development seems far less active than Ogre's anyways.

So I tried using Ogre via managed c++. This worked well, up to a point. Namely, RenderWindow.isClosed always returns true in managed c++. I've seen it mentioned before, and the dev who replied was uncertain as to what caused this. I've looked at the source myself, and see no reason for it. I was able to run Ogre in a .NET form, but I thought, if something that trivial doesn't work, what else will I find doesn't work?

Enter an unmanaged wrapper. Ironically, it consists of making a simple c front end for ogre and sticking it in a DLL. Then you can call those c functions from c# (or any other language capable of external declarations). It's not hard to do with visual studio. Create an unmanaged DLL project. Configure the project like you would any Ogre application, and you'll be presented with a DLL main function.

The tricky part is making functions external. Declare this in an include file somewhere:

Code: Select all
#define export extern "C" __declspec(dllexport)


Then create a wrapper (Camera in this case):

Code: Select all
#pragma once

#include "Ogre.h"

#include "export.h"

using namespace Ogre;

export void CameraLookAt( Camera * camera, float x, float y, float z )
{
   camera->lookAt( x, y, z );
}


Then from C# you can access it as so:

Code: Select all
using System;
using System.Security;
using System.Runtime.InteropServices;

namespace OgreNet
{
   public class Camera
   {
      protected IntPtr Unmanaged;

      public Camera( IntPtr ptr )
      {
         this.Unmanaged = ptr;
      }

      [DllImport("Ogre.Glue.dll", EntryPoint="CameraLookAt"), SuppressUnmanagedCodeSecurity]
      private static extern void _LookAt( IntPtr camera, float x, float y, float z );
      public void LookAt( float x, float y, float z )
      {
         _LookAt( this.Unmanaged, x, y, z );
      }
   }
}


Naturally you'd also need to wrap a SceneManager to obtain a pointer to a camera object.

I know this is ugly, and those of you who hate wrappers are probably laughing, but there are a few good things to say about it.

1) The performance loss from wrappers on a modern computer is negligable anyways.
2) Most of the cpu intensive stuff is going on internally in the Ogre library anyways, the wrapper is just needed to set things up and make changes each frame.
3) Development time, development time, development time. Coding is fast in c#s simpler syntax, and debugging a .NET application is WAY easier.
4) Using an unmanaged wrapper would allow for crossplatform goodness (mono doesn't have a managed c++ compiler yet and probably won't for some time).
5) It works, perfectly. No annoying inexplicable bugs like in managed c++.

This is just a test case for now. At some point I'm going to try whipping up a program that parses through Ogre's source and generates the source for the wrapper from it. That would be a lot of work, but less than manually wrapping it function by function, class by class.

Thoughts? Naturally if you don't use .NET this won't interest you, but I've seen lots of posts by .NET users inquiring about this sort of thing.
Maleficus
Regular
 
Posts: 116
Joined: Sat Jul 30, 2005 11:11 am
Location: Vancouver, B.C. Canada

Postby pjcast » Mon Aug 08, 2005 12:10 am

Well, Axiom appears to be dead (for now), and from my understanding, RealmForge is picking it back up (http://realmforge.com/). Its a few versions behind Ogre I believe, but is your best choice (if you do not want to use MC++).

Making a wrapper for each possible method you could call on every Ogre object is tedious and not really worth it IMO (at that point you might as well just port Ogre to c# - where Axiom & RealmForge come in).
Have a question about Input? Video? WGE? Come on over... http://www.wreckedgames.com/forum/
User avatar
pjcast
OGRE Team Member
OGRE Team Member
 
Posts: 2942
Joined: Fri Oct 24, 2003 2:53 am
Location: San Diego, Ca

Postby Maleficus » Mon Aug 08, 2005 5:30 am

pjcast wrote:Well, Axiom appears to be dead (for now), and from my understanding, RealmForge is picking it back up (http://realmforge.com/). Its a few versions behind Ogre I believe, but is your best choice (if you do not want to use MC++).


It being a few versions behind is another reason I'm not too interested in Axiom. But to each their own :) .

pjcast wrote:Making a wrapper for each possible method you could call on every Ogre object is tedious and not really worth it IMO (at that point you might as well just port Ogre to c# - where Axiom & RealmForge come in).


That's why I intend to create an application to parse through the ogre source and generate wrapper source. The .NET capabilities for analysing text are very powerfull, so I know it's doable, and will avoid a lot of the tediousness of coding a wrapper.

An example of such an endeavor is the Tao framework, which provides opengl bindings for .NET. It's source files are entirely generated by a script that analyses the c opengl headers.
Maleficus
Regular
 
Posts: 116
Joined: Sat Jul 30, 2005 11:11 am
Location: Vancouver, B.C. Canada

Postby jacmoe » Mon Aug 08, 2005 6:07 am

I don't get it: if you are willing to put enormous amounts of work into it, why not help bring Axiom up-to-date? :)
/* when invited to a free lunch, you should not complain about the quality of the caviar */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, Fueled by Passion.
Ogre AppWizards - project wizards for Visual Studio and Code::Blocks.
User avatar
jacmoe
OGRE Moderator
OGRE Moderator
 
Posts: 17493
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark

Postby Lorenzo » Mon Aug 08, 2005 7:20 am

That's why I intend to create an application to parse through the ogre source and generate wrapper source. The .NET capabilities for analysing text are very powerfull, so I know it's doable, and will avoid a lot of the tediousness of coding a wrapper.


Hi, I don't know if it suits your needs but give a look at swig: swig.sf.net , it is a program that generates wrapper classes automatically for a lot of languages (including C#), I've used it with Python and C# and it works really well, it doesn't require any modification to source code, it supports C++ syntax and it supports STL (although there is a limited template support in swig, you'll have to instantiate them manually, anyway maybe with C# 2.0 and generics a more mature template support will be added)

to build a wrapper you need to create an interface definition file (a text file with a syntax similar to C++), and then run swig on it, it will generate a dll (or a .so file on unix system) which will wrap the specified class and will allow you to use it in C# (so you won't get mad with all the p/invoke stuff :))

give it a try, it will save you a lot of time :)

Bye!

Lorenzo
Lorenzo
Newcomer
 
Posts: 18
Joined: Sun Aug 07, 2005 5:46 am

Postby Maleficus » Mon Aug 08, 2005 9:15 am

Lorenzo, thank you very much for the link. I was aware of swig, but didn't realise it worked with c# :) . Looking at it now.

Edit: man, swig is perfect. It's does exactly what I was doing, but for me. Thanks again.

jacmoe wrote:I don't get it: if you are willing to put enormous amounts of work into it, why not help bring Axiom up-to-date? :)


I honestly don't see why you're so baffled :) . I'm not interested in diving into the internal workings of a complex engine, for the very reasons I stopped doing my own engine coding: the desire to do a concrete project, combined with a lack of spare time.

I'd rather use a stable, actively developed engine through a wrapper, than a young and in some ways incomplete, unwrapped engine (although like, I said, Axiom is a good engine, just not as far along as Ogre is).

Look at it this way, how is using Ogre from a managed language any different from using it via python bindings? Both techniques allow one to use this excellent library in a simpler, easier to use language.

And personally, while my c++ skills are decent, after using c# for a long time, I couldn't imagine using c++ for a major project. That's just me though :) .
Maleficus
Regular
 
Posts: 116
Joined: Sat Jul 30, 2005 11:11 am
Location: Vancouver, B.C. Canada

Postby jacmoe » Mon Aug 08, 2005 5:28 pm

Maleficus wrote:I honestly don't see why you're so baffled :) . I'm not interested in diving into the internal workings of a complex engine, for the very reasons I stopped doing my own engine coding: the desire to do a concrete project, combined with a lack of spare time.

I am not baffled any longer! :wink:
Just checking. Your idea sounds great! :)
Could the pyOgre swig pkg's be reused?
That would be very cool. 8)

Maleficus wrote:And personally, while my c++ skills are decent, after using c# for a long time, I couldn't imagine using c++ for a major project. That's just me though :) .

Good point! :)
/* when invited to a free lunch, you should not complain about the quality of the caviar */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, Fueled by Passion.
Ogre AppWizards - project wizards for Visual Studio and Code::Blocks.
User avatar
jacmoe
OGRE Moderator
OGRE Moderator
 
Posts: 17493
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark

Postby Maleficus » Mon Aug 08, 2005 7:26 pm

jacmoe wrote:Could the pyOgre swig pkg's be reused?


I've looked at it, and can probably borrow a lot of code from it, but I think I'm making too many changes to make it practical for python and c# to use the same interface file. There'd be a #define for pretty much every member.

Those changes consist of .NETifying things. For example, eliminating the annoying c++ style lower case function names, and hiding singletons behind sealed classes with static members. Singletons have no business existing in a managed language :) . I was surprised Axiom uses singletons, that's conformity running amok.

So for example in pyogre you'd do:

Code: Select all
ogre.TextureManager.getSingleton().defaultNumMipmaps = 5


in c# I'd like it to be simply:

Code: Select all
TextureManager.DefaultNumMipMaps = 5;


Regardless, the interface file doesn't appear to come with pyogre. How would I go about obtaining it?
Maleficus
Regular
 
Posts: 116
Joined: Sat Jul 30, 2005 11:11 am
Location: Vancouver, B.C. Canada

Postby jacmoe » Mon Aug 08, 2005 7:33 pm

http://svn.berlios.de/viewcvs/pyogre/trunk/pyogre/ogre/?rev=186
Notice the .i files there? :)

The pyOgre in ogreaddons had a new home - and a switch from boost::python to SWIG - as announced here:
http://www.ogre3d.org/phpBB2/viewtopic.php?t=9375&start=0&postdays=0&postorder=asc&highlight=

:wink:
/* when invited to a free lunch, you should not complain about the quality of the caviar */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, Fueled by Passion.
Ogre AppWizards - project wizards for Visual Studio and Code::Blocks.
User avatar
jacmoe
OGRE Moderator
OGRE Moderator
 
Posts: 17493
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark

Postby Maleficus » Wed Aug 10, 2005 9:18 am

Wow, what can I say, but swig rocks. And there doesn't seem to be a performance hit either, same as pyOgre.

I'd like to make a point to thank the pyOgre guys, because looking through your interface files made the learning curve significantly less steep.

Image

Code: Select all
using System;
using System.Drawing;

using OgreNet;

namespace Test
{
   public sealed class OgreApplication
   {
      private static Root mRoot = null;
      private static RenderWindow mRenderWindow = null;
      private static SceneManager mSceneManager = null;
      private static Camera mCamera = null;
      private static Viewport mViewport = null;
      private static ListenerHack mListenerHack = null;
      private static SceneNode mRazorNode = null;
      private static Entity mRazor = null;
      private static Light mLight = null;
      private static ParticleSystem mParticleSystem = null;
      private static ParticleEmitter mEmitter1 = null;
      private static ParticleEmitter mEmitter2 = null;

      public static void Start()
      {
         if( !Setup() ) return;

         mRoot.startRendering();

         mRoot.Dispose();
      }

      public static bool Setup()
      {
         mRoot = new Root();

         Initialiser.SetupResources("resources.cfg");

         mRoot.showConfigDialog();

         mRenderWindow = mRoot.initialise(true, "This is a test");

         mSceneManager = mRoot.getSceneManager( SceneType.Generic );

         mCamera = mSceneManager.createCamera("playercam");
         mCamera.setPosition( new Vector3(100,100,100));
         mCamera.lookAt( new Vector3(0,0,0));
         mCamera.setNearClipDistance( 5 );
         mViewport = mRenderWindow.addViewport( mCamera );
         mViewport.setBackgroundColour( ColourValue.Red );
         mCamera.setAspectRatio((float)mViewport.getActualWidth()/(float)mViewport.getActualHeight());

         TextureManager.getSingleton().setDefaultNumMipmaps(5);

         mListenerHack = new ListenerHack( mRoot );
         mListenerHack.FrameStarted = new ListenerHack.FrameDelegate( FrameStarted );

         mSceneManager.setSkyBox(true, "Skybox/Space", 50);
         mSceneManager.setAmbientLight( new ColourValue( 0.25f, 0.25f, 0.25f ));

         mLight = mSceneManager.createLight("RazorLight");
         mLight.setType( Light.LightTypes.LT_DIRECTIONAL );
         mLight.setDirection(1.0f, 0.0f, 0.0f);
         mLight.setDiffuseColour(new ColourValue(0.5f, 0.5f, 1.0f));

         mRazorNode = mSceneManager.getRootSceneNode().createChildSceneNode("RazorNode");
         mRazor = mSceneManager.createEntity("Razor", "razor.mesh");
         mRazorNode.attachObject(mRazor);

         mParticleSystem = ParticleSystemManager.getSingleton().createSystem("particlesystem", 200);
         mParticleSystem.setMaterialName( "Particles/Flare" );
         mParticleSystem.setDefaultDimensions( 25, 25 );

         mEmitter1 = mParticleSystem.addEmitter( "Point" );
         mEmitter1.setTimeToLive( 0.2f );
         mEmitter1.setEmissionRate( 70 );
         mEmitter1.setParticleVelocity( 100 );
         mEmitter1.setDirection( new Vector3( 0.0f, 0.0f, -1.0f ));
         mEmitter1.setColour( ColourValue.White, ColourValue.Red);     
         mEmitter1.setPosition( new Vector3( 5.7f, 0.0f, 0.0f ) );

         mEmitter2 = mParticleSystem.addEmitter( "Point" );
         mEmitter2.setTimeToLive( 0.2f );
         mEmitter2.setEmissionRate( 70 );
         mEmitter2.setParticleVelocity( 100 );
         mEmitter2.setDirection( new Vector3( 0.0f, 0.0f, -1.0f ));
         mEmitter2.setColour( ColourValue.White, ColourValue.Red);
         mEmitter2.setPosition( new Vector3( -18.0f, 0.0f, 0.0f ) );

         mSceneManager.getRootSceneNode().
            createChildSceneNode( new Vector3( 0.0f, 6.5f, -67.0f ) ).attachObject(mParticleSystem);

         return true;
      }

      private static bool FrameStarted()
      {
         if( mRenderWindow.isClosed() ) return false;
         
         return true;
      }
   }
}


A few miscellaneous things to mention:

- That Initialiser class is a temporary hack put in because I haven't gotten around to wrapping the ConfigFile class and the associated iterators.

- I'm not going to merge with pyOgre. Although the code as you see it is basically the default swig wrapping, I'm going to spend a lot of time .NETing things up, and it will probably be impractical to have the python and c# interfaces in the same file. For example, I want to hide singletons, replace the ColourValue class with .Net's System.Drawing.Color, and make as many things as possible properties. I'd also like to recreate the vector, matrix, and quaternion classes in c#, so that data isn't being passed through the wrapper every time the user performs an operation with them.

- I'll live with the lower case function names, simply because I can't be bothered to do a million %renames.

- Swig doesn't have director support with c# yet, so I can't implement listeners the way they're supposed to be (yet). So I slapped together a class (ListenerHack) that takes function pointers and calls back whenever an event occures. I may make this permanent, since it's a more .NET way of handling events (though I'll give the class a more elegant name of course).

- I have CEGUI working through a swig wrapper too, I'm just too tired to post a screenshot :P .
Maleficus
Regular
 
Posts: 116
Joined: Sat Jul 30, 2005 11:11 am
Location: Vancouver, B.C. Canada

Postby jacmoe » Thu Aug 11, 2005 8:35 pm

This is beautiful! :)
Thanks to SWIG, and your clever use of it.
And you work very fast, too. 8)
/* when invited to a free lunch, you should not complain about the quality of the caviar */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, Fueled by Passion.
Ogre AppWizards - project wizards for Visual Studio and Code::Blocks.
User avatar
jacmoe
OGRE Moderator
OGRE Moderator
 
Posts: 17493
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark

Postby Chris Jones » Thu Aug 11, 2005 9:39 pm

im not sure, maybe im missing a major point here, but y isnt it possible just to use ogre in a managed application?

i wrote about the renderwindow.isClosed() always returns true, but actually, that doesnt matter, if u have a .net window, you can check if that closes using an event, you dont have to use ogre to tell you if it is

im pretty sure ive managed to run a basic tutorial in a .net window (cant remember what i exactly did).

i dont get y we need a wrapper, especially that if you have to call a wrapper function, that only calls an ogre function, thats a waste, and will (only slightly on modern computers) slow the program down
User avatar
Chris Jones
Veteran
 
Posts: 1714
Joined: Tue Apr 05, 2005 1:11 pm
Location: Gosport, South England

Postby skullfire » Thu Aug 11, 2005 11:16 pm

OMG this is just what i need!! Dont u see people! This can help the community in sooo many ways... first off: EDITORS. Character editors, Scene editors, whatever you want on managed .NET.. looking awesome!!!!!
using OgreNet;
*drools*
@Maleficus: you're my new hero. *spasm*
Please make a CVS to show off the wrappers or make a download or something.. this CANNOT get lost in the forums! please let it make it to the wiki. Axiom and Realmforge have nothing to do against a wrapper that can be automatically updated using SWIG.
I may have alzheimer, but at least I dont have alzheimer.
User avatar
skullfire
Regular
 
Posts: 150
Joined: Sat Mar 19, 2005 7:51 pm
Location: San Jose, Costa Rica

Postby Maleficus » Fri Aug 12, 2005 4:28 am

jacmoe wrote:This is beautiful! :)
Thanks to SWIG, and your clever use of it.
And you work very fast, too. 8)


Thanks :) . I actually have a lot of experience interoping managed and unmanaged code, so the only really tough part was learning swig. Swig itself it easy to use, but the c# parts aren't too well documented yet.

Chris Jones wrote:i wrote about the renderwindow.isClosed() always returns true, but actually, that doesnt matter, if u have a .net window, you can check if that closes using an event, you dont have to use ogre to tell you if it is

im pretty sure ive managed to run a basic tutorial in a .net window (cant remember what i exactly did).


This is covered in my first post (does nobody read an entire thread before posting? :P ). I too got ogre running nicely in a .NET form, but I figured if something as trivial as the isClosed function doesn't work in managed c++, what else will I find later doesn't work? I'm not going to spend a lot of time on the managed c++ route if I'm going to run into an insurmountable problem months down the road.

Chris Jones wrote:i dont get y we need a wrapper, especially that if you have to call a wrapper function, that only calls an ogre function, thats a waste, and will (only slightly on modern computers) slow the program down


It really isn't that much more overhead than using Ogre via managed c++. A wrapper is a wrapper. Regardless the performance loss appears to be minimal so far. And frankly even if it does turn out that ogre runs a few percent slower in managed code, I'm fine with that since faster development time comes along with it.

skullfire wrote:OMG this is just what i need!! Dont u see people! This can help the community in sooo many ways... first off: EDITORS. Character editors, Scene editors, whatever you want on managed .NET.. looking awesome!!!!!


Thanks again. Indeed, creating things like editors is one area where c# blows c++ out of the water. (Not trying to start a flamewar here, c++ is indeniably better in other areas. Raw performance for example.)

skullfire wrote:Please make a CVS to show off the wrappers or make a download or something.. this CANNOT get lost in the forums! please let it make it to the wiki. Axiom and Realmforge have nothing to do against a wrapper that can be automatically updated using SWIG.


Don't worry, I'm not going anywhere, now that I've found the best of both worlds. ie the ability to use a good, actively developed engine in a language I love.
Maleficus
Regular
 
Posts: 116
Joined: Sat Jul 30, 2005 11:11 am
Location: Vancouver, B.C. Canada

Postby jwace81 » Fri Aug 12, 2005 11:01 am

Maleficus wrote:It really isn't that much more overhead than using Ogre via managed c++. A wrapper is a wrapper. Regardless the performance loss appears to be minimal so far. And frankly even if it does turn out that ogre runs a few percent slower in managed code, I'm fine with that since faster development time comes along with it.

Another possible benefit of creating a C# wrapper rather than using managed C++ is that it has the potential to be more portable. Managed C++ isn't supported in Mono, and IIRC they don't have plans to support it. A C# wrapper on the other hand should be running on any platform that supports mono or the .Net framework.

This makes me want to go out and learn how to use SWIG, since I do like working with C# quite a lot. I'd be very interested in a C# wrapper for Ogre.

J.W.
jwace81
Familiar face
 
Posts: 42
Joined: Sat Jul 31, 2004 8:46 pm

Postby Chris Jones » Fri Aug 12, 2005 11:22 am

oh right, i get the point of it now :p

i will probably use this wrapper, ive decided to learn C# as im going to make an editor. i want to make the editor so that its an all in 1 editor, and plugins are used to change what each of the buttons etc do

ive downloaded SharpDevelop, gona start learning now!
User avatar
Chris Jones
Veteran
 
Posts: 1714
Joined: Tue Apr 05, 2005 1:11 pm
Location: Gosport, South England

Postby jacmoe » Fri Aug 12, 2005 5:30 pm

It would be very, very cool if someone could make this work in mono on Linux! :D
/* when invited to a free lunch, you should not complain about the quality of the caviar */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, Fueled by Passion.
Ogre AppWizards - project wizards for Visual Studio and Code::Blocks.
User avatar
jacmoe
OGRE Moderator
OGRE Moderator
 
Posts: 17493
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark

Postby Maleficus » Fri Aug 12, 2005 7:24 pm

jacmoe wrote:It would be very, very cool if someone could make this work in mono on Linux! :D


I've used mono before, so I'm sure I can do that. I'm pretty sure all that would be required would be an #ifdef in the unmanaged wrapper to replace DllMain with a basic main function, and rebuilding as a .so. I haven't used linux in a good six months though, so I'm probably rusty.

And lets not forget mac os x :) . I have a crappy old mac I can test on. I won't be able to run it, but can test if it builds properly.

The only concern I have is performance: mono isn't as heavily optimized as .NET. Some parts run just as fast as .NET, some are significantly slower. Back when I used OpenGL in c# through mono, applications always ran about 30% slower in linux than windows. This was on the same machine: I have Fedora and XP dual booted.

However, I never determined if that was due to mono, or ATI's crappy linux drivers, or both.
Maleficus
Regular
 
Posts: 116
Joined: Sat Jul 30, 2005 11:11 am
Location: Vancouver, B.C. Canada

Postby skullfire » Fri Aug 12, 2005 7:43 pm

Yeah, would be great to make it just as portable as Ogre. One thing though, when are you planning on making a release? im kinda anxious :p
I may have alzheimer, but at least I dont have alzheimer.
User avatar
skullfire
Regular
 
Posts: 150
Joined: Sat Mar 19, 2005 7:51 pm
Location: San Jose, Costa Rica

Postby Maleficus » Fri Aug 12, 2005 10:10 pm

skullfire wrote:One thing though, when are you planning on making a release?


So far I've wrapped a bit over half the classes that pyOgre has wrapped, so soon. Let's say a day or two, perhaps longer if life intervenes or I run into problems.

I'll release an initial version that's just the basic swig wrapper, not prettied up at all. Then I'll patch it up over time, basically as I use the code in my own project(s).

I've encountered a couple pitfalls so far, though I'm sure they're nothing that can't be fixed eventually. For example:

The Frustum class derives from both Renderable and MovableObject. C# however doesn't support c++ style multiple inheritance. It does however have multiple inheritance of interfaces, which are basically abstract classes. So until I figure out how to get swig to convert Renderable (an abstract class) into a c# interface, Renderable is not included in the wrapper.

I'm not confident swig can do that, so I may wind up creating a Renderable interface myself.

edit: just confirmed that swig does not support c# interfaces.
Maleficus
Regular
 
Posts: 116
Joined: Sat Jul 30, 2005 11:11 am
Location: Vancouver, B.C. Canada

Postby Kentamanos » Sat Aug 13, 2005 3:43 am

Very cool work. I'm curious if interop overhead would at some point potentially make this slower than Axiom (assuming Axiom was up-to-date enough to be a candidate for using it).

In any event, I love C# and look forward to seeing more of your work.
User avatar
Kentamanos
Veteran
 
Posts: 1026
Joined: Sat Aug 07, 2004 12:08 am
Location: Dallas, TX

Postby Maleficus » Sun Aug 14, 2005 9:37 am

Just an update: OgreNet in vb.net using an ExampleApplication class I whipped up. I prefer C#, myself, but enough people like vb.net I figured it couldn't hurt to toss this out there.

This is what I want the prettied up version of the code to look like, though right now I'd say at least 90% of the wrapper is still the default swig generated code.

I need to finish implementing event handling, so in a day or two I'll release an early alpha version. ie, just about everything that has been wrapped should work, but I won't guarentee that everything will.

The next major task after that will be replacing the vector, quaternion and matrix classes with c# equivalents. Before someone asks, there's a number of reasons for that:

1) Swig wraps these as classes, understandably. However, in .NET instancing classes is significantly slower than instancing structs. Using classes for something that can potentially be instanced thousands of times a frame is a BAD idea. So I want these to be structs, just like in managed directx. Thankfully the math syntax of c# and c/c++ is identical, so porting the code will be trivial.

2) Swig does not support operator overloading with C# yet. So as long as I have to code a bunch of new operator overloads anyways...

3) I don't want data being transfered across the wrapper a ton of times if the user does a large number of his own calculations.

Code: Select all
Imports System.Drawing

Imports OgreNet

Public Class SkyBoxApplication
    Inherits ExampleApplication

    Protected mLight As Light = Nothing
    Protected mEntity As Entity = Nothing
    Protected mParticleSystem As ParticleSystem = Nothing
    Protected mParticleEmitter1 As ParticleEmitter = Nothing
    Protected mParticleEmitter2 As ParticleEmitter = Nothing

    Protected Overrides Function FrameStarted(ByVal TimeSinceLastFrame As Single, ByVal TimeSinceLastEvent As Single) As Boolean
        Dim ret As Boolean = MyBase.FrameStarted(TimeSinceLastFrame, TimeSinceLastEvent)

        Return ret
    End Function

    Protected Overrides Sub CreateScene()
        mSceneManager.AmbientLightColor = Color.FromArgb(125, 125, 125, 125)
        mSceneManager.SetSkyBox(True, "Skybox/Space", 50)

        mLight = mSceneManager.CreateLight("MainLight")
        mLight.Position = New Vector3(20, 80, 50)

        mEntity = mSceneManager.CreateEntity("razor", "razor.mesh")
        mSceneManager.RootSceneNode.AttachObject(mEntity)

        mParticleSystem = ParticleSystemManager.createSystem("ParticleSys1", 200)
        mParticleSystem.MaterialName = "Particles/Flare"
        mParticleSystem.SetDefaultDimensions(25, 25)

        mParticleEmitter1 = mParticleSystem.AddEmitter("Point")
        With mParticleEmitter1
            .TimeToLive = 0.2
            .EmissionRate = 70.0
            .ParticleVelocity = 100.0
            .Direction = New Vector3(0.0, 0.0, -1.0)
            .SetColors(Color.White, Color.Red)
            .Position = New Vector3(5.7, 0.0, 0.0)
        End With

        mParticleEmitter2 = mParticleSystem.AddEmitter("Point")
        With mParticleEmitter2
            .TimeToLive = 0.2
            .EmissionRate = 70.0
            .ParticleVelocity = 100.0
            .Direction = New Vector3(0.0, 0.0, -1.0)
            .SetColors(Color.White, Color.Red)
            .Position = New Vector3(-18.0, 0.0, 0.0)
        End With

        mSceneManager.RootSceneNode.CreateChildSceneNode(New Vector3(0.0, 6.5, -67.0)).AttachObject(mParticleSystem)

    End Sub

End Class

Module VB_SkyBox

    Sub Main()
        Dim app As New SkyBoxApplication
        app.Start()
        app.Dispose()
    End Sub

End Module
Maleficus
Regular
 
Posts: 116
Joined: Sat Jul 30, 2005 11:11 am
Location: Vancouver, B.C. Canada

Postby jwace81 » Sun Aug 14, 2005 5:47 pm

Instead of doing all the work of porting the Vector, Matrix, and Quaternion classes to C#, you could use the versions of the classes that have already been ported in Axiom. That would save you some work.

J.W.
jwace81
Familiar face
 
Posts: 42
Joined: Sat Jul 31, 2004 8:46 pm

Postby Maleficus » Sun Aug 14, 2005 6:57 pm

jwace81 wrote:Instead of doing all the work of porting the Vector, Matrix, and Quaternion classes to C#, you could use the versions of the classes that have already been ported in Axiom. That would save you some work.

J.W.


I already thought of that, but thanks :) . That does save me a lot of work. All I have to do is add functions to convert between the managed and unmanaged versions.
Maleficus
Regular
 
Posts: 116
Joined: Sat Jul 30, 2005 11:11 am
Location: Vancouver, B.C. Canada

Postby mjsimpson » Mon Aug 15, 2005 2:46 pm

I've been trying to develop a small game using C# and have been sticking with Axiom/RealmForge because I really wanted to avoid using C++. However, I've also been frustrated by the lack of a useable product. I've been off the net for a couple of weeks and just this weekend I was thinking I'd bite the bullet and switch to C++ just so I could use Ogre. But then I find this thread! So thank you! I'm anxiously waiting for a release!
mjsimpson
Newcomer
 
Posts: 13
Joined: Mon Aug 15, 2005 2:42 pm

Next

Return to Using OGRE in practice

Who is online

Users browsing this forum: NQ and 2 guests