cnamethyst's picture

Help: C# frameless (non client) drawing

Hi Guys,

is it possible to render images using OpenGL with C# without a physic device? I mean draw a image without assigning a physic window/frame to host it?

Many thanks,
cnamethyst


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Stefan Monov's picture

You can hide the window border by doing:

yourWindow.WindowBorder = WindowBorder.Hidden;

Is that what you want?

Edit: Wait, I get it. You want render directly to an image without creating a window at all. I don't know if that's possible.

I think you can create a window, then set its Visible property to false (or, best, never set it to true, and don't call Run which sets it to true), and then render to a texture via FBO. Then you can download the pixels from the texture via glReadPixels. Haven't tried it though.

the Fiddler's picture

That's the best approach: create an invisible GameWindow (i.e. don't call its Run() method or set Visible = false), create a framebuffer object and use GL.ReadPixels to download the rendering to system memory. (The FBO is necessary because the contents of an invisible window are undefined).

You will still need a video card for OpenGL, though.

Stefan Monov's picture

AFAIK if he lacks a video card, it can still work if his drivers implement software rendering (like Mesa3D does), Fiddler. Really slow though.

cnamethyst's picture

Thanks for the comments. Do you guys have some examples for using the suggested approach? I do have a video card. I need to integrate an OpenGL implementation to an existent C# application which does not work with OpenGL directly and it is not easy to obtain an physic window device for drawing.

Stefan Monov's picture

I don't have an example for the FBO approach, but if your C# app uses WinForms, GTK# or WPF, you can directly integrate the OpenGL code with it - OpenTK supports it. If your C# app uses SDL or Qt or some other lib that is capable of creating a GL context, it should also be possible to directly integrate it (using a dummy GraphicsContext, it's been discussed on the forum).

Edit: If you decide to go for the FBO approach, the code for creating the window is just "new GameWindow()", then you set up your viewport, projection, etc. Then you can create a FBO using the FrameBuffer class from OOGL (it's probably easier than doing it manually). Then do your drawing and finally call GL.ReadPixels.

the Fiddler's picture
cnamethyst wrote:

Thanks for the comments. Do you guys have some examples for using the suggested approach? I do have a video card. I need to integrate an OpenGL implementation to an existent C# application which does not work with OpenGL directly and it is not easy to obtain an physic window device for drawing.

As Stefan said above, the best approach depends on the toolkit used by the existing application. If it is using WinForms or WPF all you need to do is create a GLControl (without attaching it to any form). After that, you will be able to call GL functions at will (create a FBO, render, read with GL.ReadPixels).

@Stefan: headless rendering via software GL may be possible but this is not officially supported. Can you even get a GDI device context without a video card?

Shocker's picture

In the Windows, you can draw without window frame to a screen by using "layered window" .

Stefan Monov's picture

Just a note. When I mentioned toolkits like WinForms, I didn't mean "use those to headlessly render more easily". What I meant was: "if you're just going to blit your headlessly-rendered image onto a non-OpenGL window, you can skip the 'headless' part and just render directly to your non-OpenGL window by putting an appropriate OpenGL control inside it."

In case he really doesn't have a window (so he isn't going to blit his headlessly-rendered image onto it), then Fiddler's advice is correct.

@Shocker: that's not what he wants, read my first post.

cnamethyst's picture

Thank you all for the kind and comprehensive response. I am trying to use Opengl to render images on the background (memory), and then pass some image processing results. I am not sure whether the layer windows or windows form solution would work for me, but I will give it a shot.

Many thanks,
cnamethyst