2

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);
Nak
  • 25
  • 7
  • You don't need to flip the pixels if you use `PixelFormat.Bgra` for the texture. – The Fiddler Mar 13 '14 at 15:06
  • Can you please explain (screenshot?) what you mean by "But the big problem is when i try to blend the GLControl whith the picturebox, it seems like blending is done with the other information inside the picture."? – The Fiddler Mar 13 '14 at 15:12

1 Answers1

5

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.)

Ryan Emerle
  • 15,461
  • 8
  • 52
  • 69
The Fiddler
  • 2,726
  • 22
  • 28
  • Yes, my bad, it is OpenTK. Thank you for your help, i will try this. I didn't know anything about that restrictions. – Nak Mar 13 '14 at 15:33