
Render To Texture Help
Posted Wednesday, 19 May, 2010 - 08:53 by thelolmaster inI've been using C# and Windows Forms to create a game, and I've recently decided to remake the game using OpenTK.
However, I've come across a problem that I'm not sure how to solve. In my game there are a lot of blood particles on screen at once, and once they have landed on the ground they get drawn to a bitmap "decal" layer (see attached image) so that I don't have to track as many particles.
In my Windows Forms game I've been using Graphics.FromImage() and Graphics.FillEllipse() etc to draw directly to the decal bitmap, but now that I've switched to OpenTK I don't know how to do something like this.
I've read about rendering the particles to a "decal" texture and then drawing that, but I've tried that using GL.CopyTexSubImage2D() and it ends up drawing not only the particles but all of the other layers as well (grass, characters etc) to the "decal" texture.
So if there is any way to go about doing something like this (preferably without Frame Buffer Objects or any recent extensions because I want this to run on as many computers as possible) in OpenTK I would love to hear it.
Alternatively, if there is a way to create a System.Drawing.Graphics object through the GameWindow class or something like that, then that might work just as well since I can then do what I've been doing in my Windows Forms game.
Any help is appreciated.


Comments
Re: Render To Texture Help
One thing you could try is adding a separate rendering pass just for the particles: clear the backbuffer, render the particles, call GL.Finish() and use GL.CopyTexSubImage2D() to copy the particles from the backbuffer to a texture. Afterwards, clear the backbuffer again and continue rendering as normal.
It is not possible to use System.Drawing.Graphics on a hardware-accelerated surface but it is possible to create a System.Drawing.Bitmap that contains the contents of the window using Bitmap.LockData() and GL.ReadPixels(). However, this approach is subject to the same limitations as GL.CopyTexSubImage2D() and it isn't hardware-accelerated to boot.
Re: Render To Texture Help
EDIT: Thanks for the suggestions theFiddler, I decided to go with Framebuffer Objects in the end when I found out one of my 5 year old laptops with an integrated graphics card could use them :).
Using the framebufferobject example in the OpenTK examples source code I was able to create a simple class wrapping the needed functionality.
I've attached it here in case anyone wants to use it.
Basic usage if anyone needs it:
Re: Render To Texture Help
There's a slight problem with your FboRenderTexture(int, int) constructor. It doesn't call Init(). I changed my copy to this:
public FboRenderTexture(int width, int height)
: this(new Size(width, height))
{
}