Nak
Nak

Reputation: 25

C# + GL Control blend background with form

i'm working with C# and OpenTK libraries for OpenGL, i want to blend a GLControl with the picturebox placed as background.

* EDIT *

i solved the problem loading source image by changing method, so now i use bitmap and bitmapdata structures:

Bitmap bitmap = new Bitmap("image.png");
BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
         ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

i'm sure that my png source has 4 channels and alpha value is 0 on black background

this is the original image: http://imageshack.com/a/img801/517/u2z8.png

And this is the problem: http://imageshack.com/a/img40/1729/zezj.jpg

top is the texture using GLControl and bottom is a Picturebox of Windows Form, what i'm trying to do is that black background of GLControl becomes transparent.

I put Blend cap enable and use GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); so i think that's not the problem.

Maybe the problem is on texture environment?

GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode,GL_MODULATE);

Upvotes: 2

Views: 2037

Answers (1)

The Fiddler
The Fiddler

Reputation: 2794

Due to airspace restrictions, blending OpenGL and WPF directly may be impossible. However, it is possible to achieve the effect you are describing using an indirect approach:

  1. Create the GLControl, but keep it invisible (i.e. don't attach it to a WindowsFormsHost)
  2. Create a framebuffer object (documentation and example code)
  3. Render your OpenGL scene to the framebuffer object
  4. Use GL.GetTexImage2D() to retrieve the rendered scene
  5. Display the result in a regular WPF PictureBox

This way, you can use any and all WPF effects on the final PictureBox, including transparency.

(Nitpicking: it appears that you are using OpenTK.GLControl, not Tao.SimpleGlControl. They share a similar function, but their APIs and capabilities are quite different.)

Upvotes: 5

Related Questions