Sign in to follow this  

[.net] C#-based OpenGL Engine?

This topic is 4306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I just have a quick question. But first, a couple of friends and I are wanting to start on a 3D engine. I mean, it's not going to be perfect. And I already know how to create a basic engine. That's all we're looking for. But I was wondering if C# would be a good language to create a 3D OpenGL engine. I mean, would it be more effective? Or should we stick with C++ on this one? I'm leaning more toward C++, because I've used it the longest. But I am working on C#, and we're willing to learn as much as we can if C# is a better choice. I just wanted everyone's opinion, as far as performance, etc. goes between C++ and C#. I hope I gave enough detail. And if C#, then what will I need to create an OpenGL application? Thanks in advance, Matt U.

Share this post


Link to post
Share on other sites
C++ will definantly give you better performance, but C# will allow you to develop your engine faster because there is less base that you have to put into C#.

Share this post


Link to post
Share on other sites
Quote:
Original post by gonzo
C++ will definantly give you better performance, but C# will allow you to develop your engine faster because there is less base that you have to put into C#.
That's partly incorrect. There have been many P/Invoke tests done that prove that the performance boost from using C++ is very insignificant. I'm currently working on a 2D OpenGL engine using a large chunk of the Tao Framework and I'm getting halariously high framerates:



Of course the 870 FPS is with only one sprite and a written string showing the FPS. But even with a tile map being rendered I'm still getting ~600 FPS, and that's in Debug mode too. Even if the hit becomes noticable, I think the benefits of using the .NET Framework outweigh that minor framerate hit. You're still going to get over 60 frames per second, anything over that surely isn't noticable.

Share this post


Link to post
Share on other sites
I dunno about 870, but with DirectX, physics and collisions I get around 5000fps in debug mode... So I think C++ is just silly for a single developer. :)

Share this post


Link to post
Share on other sites
C# is fine. It might be a little trickier to get updated opengl bindings than it would if you were using c++. I've used the Tao bindings, and they worked fine, but I never got the vertex/pixel shader extensions working. Of course that was 2 years ago, so they're probably in there now.

As far as performance, c# will be fast enough. In terms of just calling into DX/OpenGL, there is a very minimal overhead(as Rob pointed out). It's a little harder to do things like use SSE intrinsics in c#, though, so if you're planning in doing hardcore physics calculations I'd recommend c++. Of course, you can always put that in a separate library, and then have a managed wrapper which can be called by your c# code - it's very slick.

Share this post


Link to post
Share on other sites
Well, I'm not looking for TOP performance. I am just looking for something that will help me gain more "experience" (I guess you'd call it =P). I mean, I just want to do this to learn C# altogether.

But would you all recommend C++ or C#? I haven't used C# (or any .NET) for very long. But I need to get at least a little more up-to-date. I have been using VS 6.0 (VC++ 6) for about 3 years now.

And I don't have .NET 2005, but I do have .NET 2003. They're close enough, right? LoL.

And I currently have an older video card that doesn't support vertex/pixel shaders. But soon, in about a month, I should have at least a GeForce FX 5500 or 6800 for my machine. I have been wanting to get into vertex/pixel shader programming for a while, once I get a better grip on DX.

I just want to know, if I'm looking to learn something new, I should go with C#, right? I am about intermediately skilled in OpenGL in C++. I'm not the greatest, but I know enough to get by with a simple game, 2D or 3D.

I was just looking for opinions. That's all. Sorry if I repeated myself at all. LoL. =)

Thanks in advance,
Matt U.

Share this post


Link to post
Share on other sites
Well I can tell you through personal experience of making my 2D engine that I am exploiting and learning all of C# 2.0 to make my job easier. I really have learned a lot about generics, singletons, large software testing and maintenence, etc. C# is very effective in making my life easier, afterall I do not have to even begin to deal with pointers. But that is a small point for some... I will just say that with C# life seems more structured, error messages make more sense and blah blah blah... sorry, bit tired right now. My vote is for C#. But make sure you know C++ since it is the industry standard.

Share this post


Link to post
Share on other sites
TFS_Waldo,

As Rob said earlier, performance difference is minor - mostly at startup and then some residual stuff later on due to the CLR's memory management, etc...So dont let performance problems be the reason you dont use C#.

Additionally, C# will definately speed up your development time with advanced classes for containers, arrays, windows development, debugging, etc...

Finally, you can download the Visual Studio .NET 2005 Express edition free of charge from microsoft's site. If you'll be using the .NET framework, save yourself some headaches and make sure to use VS.NET 2005 with .NET 2.0

Cheers!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Although C# will not introduce much overhead, my reasons for not using it is that everyone does not have the .NET framework installed. Plus there are a couple of versions of this framework that also might not be installed. I will say that when I2 used C# that I was very productive. However, I used it for forms only so it was quite the upgrade over MFC and such, more like VB style double click ont hings and magic happens. The few demos that used .NET for me simply dont work, because the framework on every computer I have ever randomly been on never had it (or not the correct version), so I never played with any of those demos. That is my main reason for not using it currently. Its a back compatability issue that I feel still needs attention. You should however learn it, either way - as this industry changed rapidly and everyone seems to be on the C# bandwagon. To me it seems like a great tool for editors and such, since you can make a GUI quickly and easily.

Share this post


Link to post
Share on other sites
Well im using C# now for my engine and things run fine ( and its not fully optimized yet! )

So far as quotes saying C++ has improved performance, this is fine if you are comparing like for like (but may not always be true!) However I remember reading an article once on the performance advantages of using assembly over c/c++ ( I think it was either by Micheal Abrash or John Carmack ) and the basic gist of it was well written c++ is better than badly written assembler!

So if you are more comfortable writing software using c# go with this
else use c++!

You may also be swayed by what you use at work, when I was programming in Delphi at work I wrote my engine in Delphi since moving to c# I converted it to C# I have found this makes life considerably easier!

[Edit MButchers Fixed Link]

Share this post


Link to post
Share on other sites
Those are some nice screenshots of that engine MButchers, looks rather much like the one I'm working on. It's striking that most C# engines I've seen so far support an extensive world-editor or even a complete development environment. I may be biased, but I think that is a testament to the productivity provided by .NET and the Windows Forms API.

IIRC the Doom3 engine supports something similar and folks went all 'wow, nice!' about that, so it's quite an accomplishment if a platform allows a programmer to easily code this singlehandedly [wink]

Share this post


Link to post
Share on other sites
Quote:
Those are some nice screenshots of that engine MButchers


Many thanks :) may get around to releasing a demo sometime soon

Quote:
It's striking that most C# engines I've seen so far support an extensive world-editor or even a complete development environment. I may be biased, but I think that is a testament to the productivity provided by .NET and the Windows Forms API.


This is very true, personally coming from a Delphi/C++ Builder background I have found it very easy to pickup the Windows Forms side of the .NET framework. The .NET framework also simplifies many tasks which leaves you to concentrate on the important side of things.

Share this post


Link to post
Share on other sites
@Rob Loach: How did you get the Tao framework to render OpenGL? I tried using the SimpleOpenGLControl and could never get it to work right. I'm using VS.NET 2005 and the lastest version of the framework. I can't just render to the full window in my particular project (needs to be a Control).

@TFS_Waldo: I wrote a small FPS about a year ago in C# that used Quake 3 BSP files. I had great performance even in a windowed mode on my school's lab computers (which are always out of date). I used the C++ examples as guidelines, but found that I could get so much more done in C# (especially when doing the GUI). I still have the source code, if you'd like it.

Share this post


Link to post
Share on other sites
Quote:
Original post by DaWanderer
@Rob Loach: How did you get the Tao framework to render OpenGL? I tried using the SimpleOpenGLControl and could never get it to work right. I'm using VS.NET 2005 and the lastest version of the framework. I can't just render to the full window in my particular project (needs to be a Control).
I'm actually not using the SimpleOpenGLControl. I'm using Tao.FreeGLUT for window and input management [smile].

As an alternate to FreeGLUT, you could use Tao.GLFW, or as you mentioned before, the SimpleOpenGLControl. I should try the SimpleOpenGLControl one day though, it definately sounds interesing and could provide some excellent functionality. You could contact the Tao Mailing List with your problem...

Quote:
I wrote a small FPS about a year ago in C# that used Quake 3 BSP files.
How did you load the BSP files in C#? Axiom is going over some major management furbishings and I'm sure they'd love being able to load BSP files [wink].

Share this post


Link to post
Share on other sites
Im using Tao openGL and rather than using the control use the usual initialisation code as below

_hdc & _hrc are of type IntPTR
_graphics is of type Graphics :)

private void InitializeOpenGL()
{
_graphics = Owner.CreateGraphics();
_hdc = _graphics.GetHdc();

Tao.Platform.Windows.Gdi.PIXELFORMATDESCRIPTOR pfd = new Tao.Platform.Windows.Gdi.PIXELFORMATDESCRIPTOR();
//pfd.Size = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = Gdi.PFD_DRAW_TO_WINDOW | Gdi.PFD_SUPPORT_OPENGL | Gdi.PFD_DOUBLEBUFFER | Gdi.PFD_GENERIC_ACCELERATED | Gdi.PFD_SWAP_EXCHANGE;
pfd.iPixelType = Gdi.PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.cStencilBits = 8;

int ipf = Gdi.ChoosePixelFormat(_hdc,ref pfd);
Gdi.SetPixelFormat(_hdc,ipf,ref pfd);

_hrc = Wgl.wglCreateContext(_hdc);
Wgl.wglMakeCurrent(_hdc,_hrc);

Tao.OpenGl.GlExtensionLoader.LoadAllExtensions();
}

Hope it helps

Mark


Share this post


Link to post
Share on other sites
MButchers,

Those pictures really look very promising. The 'development enviroment' style design looks really, really top notch.

Needless to say, like everyone else, I'm doing something similar myself. A bit of a different direction however. I don't have too much that I can show off however, as I've been very focused on the internals, things like multithreading, etc.

What might be of interest, is that I started my engine as a C++ project. Using my *existing* code base for my previous project (which I scrapped about 99% of). The point I switched to C# was when I came to fixing up a bug in my array template. Porting the code over from C++ took all of a couple of hours. In the process, I culled off about 50+% of it, as it was already implemented (better) in the framework.

I have a short video of showing a couple of things I've done here... It's a wee bit old, but still should give an idea of the direction I'm heading in.


Performance wise, I've set the system up to reduce it's cpu usage if it gets over 60fps. So it sits at 500fps, drawing the high detail standford bunny with no measurable cpu usage in task manager (0%). With 10,000 objects (all independant objects, scene graphed) I get 75fps setup, ending up as 43fps rendering. I think this is more than adequate.

Share this post


Link to post
Share on other sites
Quote:
Original post by remigius
Pieter made a C# BSP loader some time ago. I'm not aware of its status, but you can find it here: http://dotnet.org.za/pieterg/archive/2005/08/21/42492.aspx. I believe he also made a more elaborate forum post on the loader here on GameDev, but I can't seem to find it right now. Enjoy [smile]


YES! I remember that!

And for the hell of it, I retrofitted it to do HDR lighting with full stencil shadows for every light :-) I still chat to him every few days, he's a cool guy.

The thread was here:

http://www.gamedev.net/community/forums/topic.asp?topic_id=340488

got it looking like this before I stopped working on it picy

Quote:

I'd love to post the complete source code to my game, but it's about 20mb for everything. Anyone have a good idea of where to post it?


I can probably host it for you, just as long as 5,000 people don't download it, things should be sweet.

Share this post


Link to post
Share on other sites
i have also been playing with Tao Framework and have been using SDL for window creationa and input managment as well as timing. as i have to work on both Window and Linux systems some times. granted i am fairly new to this as i stopped doing any programming a few years ago really. this code contains some of my other classes as well just ignore gameconsole,gameengine and defaultfont


static void Main(string[] args)
{
bool bRunning = true;
bool bActive = true;
bool bCapMouse = true;
bool bFullScreen = false;
IntPtr sdlSurface = IntPtr.Zero;
int SDLVideoFlags = 0;
int SDLVideoFlagsFull = 0;
bool[] Keys = new bool[500];
bool[] MouseKeys = new bool[8];
int X = 0, Y = 0;
int width = 800, height = 600;

Console.WriteLine("Setting up SDL and Devices Surface!");

if (Sdl.SDL_Init(Sdl.SDL_INIT_VIDEO) < 0)
{
Console.WriteLine("Unable to start SDL, SDL gave the following error!");
Console.WriteLine("{0}", Sdl.SDL_GetError());
Sdl.SDL_Quit();
}

try
{
Sdl.SDL_VideoInfo info = (Sdl.SDL_VideoInfo)Marshal.PtrToStructure(Sdl.SDL_GetVideoInfo(),
typeof(Sdl.SDL_VideoInfo));

SDLVideoFlags = Sdl.SDL_OPENGL |
Sdl.SDL_GL_DOUBLEBUFFER |
Sdl.SDL_HWPALETTE |
Sdl.SDL_RESIZABLE;

SDLVideoFlags |= (info.hw_available > 0 ? Sdl.SDL_HWSURFACE : Sdl.SDL_SWSURFACE);

if (info.blit_hw > 0)
SDLVideoFlags |= Sdl.SDL_HWACCEL;


SDLVideoFlagsFull = SDLVideoFlags | Sdl.SDL_FULLSCREEN;

Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_DOUBLEBUFFER, 1);

sdlSurface = Sdl.SDL_SetVideoMode(width, height, 0, SDLVideoFlags);

Sdl.SDL_WM_SetCaption("Untitled",null);

if (sdlSurface == IntPtr.Zero)
{
Console.WriteLine("Unable to save surface pointer");
}

}
catch (NullReferenceException)
{
Console.WriteLine("Unable to get video info");
Console.WriteLine("{0}", Sdl.SDL_GetError());
Sdl.SDL_Quit();
}
catch (Exception exc)
{
Console.WriteLine("An unknown error has occured while initializing SDL");
Console.WriteLine("{0}", Sdl.SDL_GetError());
Console.WriteLine(exc);
Sdl.SDL_Quit();
}

Sdl.SDL_Event sdlEvent;

InitGL();

CGameEngine engine = new CGameEngine();
engine.InitEngine();
GameConsole console = new GameConsole("console.tga");
engine.Camera.AspectRatio = width / height;
engine.Camera.ResizeCameraViewPort(width,height);
int halfWidth = width >> 1;
int halfHeight = height >> 1;
GameConsole.width = width;
GameConsole.height = height;

Sdl.SDL_WM_GrabInput(Sdl.SDL_GRAB_ON);
Sdl.SDL_ShowCursor(0);

while (bRunning)
{
while(Sdl.SDL_PollEvent(out sdlEvent) == 1)
{
switch (sdlEvent.type)
{
case Sdl.SDL_ACTIVEEVENT:

if (sdlEvent.active.gain == 0)
{
Sdl.SDL_ShowCursor(1);
Sdl.SDL_WM_GrabInput(Sdl.SDL_GRAB_OFF);
bCapMouse = false;
}
else
{
Sdl.SDL_ShowCursor(0);
Sdl.SDL_WM_GrabInput(Sdl.SDL_GRAB_ON);
bCapMouse = true;
}
break;
case Sdl.SDL_VIDEORESIZE:
sdlSurface = Sdl.SDL_SetVideoMode(sdlEvent.resize.w, sdlEvent.resize.h, 0, SDLVideoFlags);
engine.Camera.AspectRatio = (float)sdlEvent.resize.w / (float)sdlEvent.resize.h;
engine.Camera.ResizeCameraViewPort(sdlEvent.resize.w, sdlEvent.resize.h);
halfWidth = width >> 1;
halfHeight = height >> 1;
break;
case Sdl.SDL_KEYDOWN:
if (!GameConsole.IsVisible)
{
Keys[sdlEvent.key.keysym.sym] = true;
}
else
{
int key = sdlEvent.key.keysym.sym;
if (key == (int)Engine.Keys.K_BACKSPACE)
{
GameConsole.Backspace();
}
else if (key == (int)Engine.Keys.K_RETURN)
{
GameConsole.AddLine();
}
else if (key == (int)Engine.Keys.K_TAB)
GameConsole.ToggleState();
else if (key < 256)
{
GameConsole.AddCharacterToEntryLine((char)sdlEvent.key.keysym.sym);
}
}

if (sdlEvent.key.keysym.sym == Sdl.SDLK_ESCAPE)
{
bActive = bRunning = false;
Sdl.SDL_Quit();
}
if (sdlEvent.key.keysym.sym == Sdl.SDLK_F1)
{
bFullScreen = !bFullScreen;
Sdl.SDL_FreeSurface(sdlSurface);
sdlSurface = Sdl.SDL_SetVideoMode(width, height, 0, (bFullScreen ? SDLVideoFlagsFull : SDLVideoFlags));
engine.Camera.AspectRatio = width / height;
engine.Camera.ResizeCameraViewPort(width, height);
InitGL();
}
if (Keys[(int)Engine.Keys.K_TAB])
GameConsole.ToggleState();

break;
case Sdl.SDL_KEYUP:
Keys[sdlEvent.key.keysym.sym] = false;
break;
case Sdl.SDL_MOUSEBUTTONDOWN:
MouseKeys[sdlEvent.button.button] = true;
break;
case Sdl.SDL_MOUSEBUTTONUP:
MouseKeys[sdlEvent.button.button] = false;
break;
case Sdl.SDL_QUIT:
bActive = bRunning = false;
Sdl.SDL_Quit();
break;
}
}

if (bCapMouse)
{
Sdl.SDL_GetMouseState(out X, out Y);

X = X - halfWidth;
Y = Y - halfHeight;

Sdl.SDL_WarpMouse((short)halfWidth, (short)halfHeight);
}

Sdl.SDL_Delay(1); // sleep and allow other threads to execute

engine.Handle_Input(Keys, MouseKeys, -X, -Y); // handle Key and mouse events in Engine

engine.GameMain(); // game main

if (bActive)
{
engine.GameRender();
console.Render();
DefaultFont.Print(string.Format("{0} Frames in {1} Seconds = {2} AVG FPS : {3} Actual FPS",
EngineTime.g_TotalFrames,
(int)EngineTime.CurrentTime,
(int)(EngineTime.g_TotalFrames/EngineTime.CurrentTime),(int)EngineTime.g_FramePerSecond),1,1.0f,10,height -15) ;
Sdl.SDL_GL_SwapBuffers();
}

}

Sdl.SDL_Quit();
}



[Edited by - Rob Loach on March 3, 2006 10:54:44 AM]

Share this post


Link to post
Share on other sites
Looks great, RenZimE. Speaking of SDL and the .NET Framework, SDL.NET 4.0.3-1 just came out. You can see details on the new version here.

Difference between Tao.Sdl and SDL.NET? SDL.NET puts an object oriented layer onto Tao.Sdl. This means instead of:
IntPtr surface = Tao.Sdl.Sdl_Image.IMG_Load("image.bmp");
... you'd be using ...
Surface surface = new Surface("image.bmp");

Share this post


Link to post
Share on other sites
Quote:
Original post by Rob Loach
Looks great, RenZimE. Speaking of SDL and the .NET Framework, SDL.NET 4.0.3-1 just came out. You can see details on the new version here.

Difference between Tao.Sdl and SDL.NET? SDL.NET puts an object oriented layer onto Tao.Sdl. This means instead of:
IntPtr surface = Tao.Sdl.Sdl_Image.IMG_Load("image.bmp");
... you'd be using ...
Surface surface = new Surface("image.bmp");


hmm ty for the info on SDL.net i had no idea it even existed.

Share this post


Link to post
Share on other sites
I recently wrote a 3d engine using C# and OpenGL.
I liked it.

Here are a couple of tips though.

Use some OpenGL pinvoke signatures that closely match the normal signatures and create a wrapper around the whole thing again. Find the simplest example that you can on the internet.

Use a wrapper for your code. Later, when you decide to use directx or something else instead, you will need to change much less code and it will be optimized across platforms.

Use directx for sound and joystick work at first. It will be easier that way.

Remember that most examples that you find for OpenGL will be in C++. To be honest though, this didn't pose too much of a problem for me. Just drop the C++ code into the project and port it right there.

Share this post


Link to post
Share on other sites
Quote:
Original post by RipTorn
I can probably host it for you, just as long as 5,000 people don't download it, things should be sweet.


OK, how should I send it to you?

I've made some modifications to the Tao SimpleOpenGLControl to make it:

1. Actually work
2. Behave nicely in design mode or when embedded in another control in design mode
3. Work with .NET 2.0 and VS.NET 2005

Do you think the Tao people would want it or are they sticking with .NET 1.1?

Share this post


Link to post
Share on other sites

This topic is 4306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this