I have to show frames from two cameras in two openGl control separately at same time. For that I have put two opengl control in my form and pass those frames to opengl accordingly. But when running the app, only second opengl control is working. First one is remain with black screen. When i remove the second opengl control first one is working. Means only one is working at a time. Can I solve this? Please check my code. I have created a common class (openTK.cs) for define all function related to opengl and calls it for each glcontrol. Also I created a new GraphicsContext on the thread I want to render my stuff.
Bitmap videoFrame = null;//should show this frames in glcontrol1
Bitmap videoFrame2 = null;//should show this frames in glcontrol2
IGraphicsContext control2Context;
openTK obj_openTK = new openTK();//common class for opengl functions
private void Show_Click(object sender, EventArgs e)
{
control2Context = new GraphicsContext(GraphicsMode.Default,glControl2.WindowInfo);
GL.ClearColor(Color.MidnightBlue);
GL.Enable(EnableCap.DepthTest);
TexUtil.InitTexturing();
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
GL.DepthFunc(DepthFunction.Lequal);
GL.ColorMaterial.ColorMaterial
(MaterialFace.FrontAndBack,ColorMaterialParameter.AmbientAndDiffuse);
GL.Enable(EnableCap.ColorMaterial);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); // render per default onto screen, not some FBO
glControl1.Resize += new EventHandler(glControl1_Resize);
glControl1.Paint += new PaintEventHandler(glControl1_Paint);
glControl2.Resize += new EventHandler(glControl2_Resize);
glControl2.Paint += new PaintEventHandler(glControl2_Paint);
Application.Idle += Application_Idle;
glControl1_Resize(glControl1, EventArgs.Empty);
glControl2_Resize(glControl2, EventArgs.Empty);
}
private void Application_Idle(object sender, EventArgs e)
{
while (glControl1.IsIdle)
{
glControl1.MakeCurrent();
Render();
GL.Flush();
glControl1.SwapBuffers();
control2Context.MakeCurrent(glControl2.WindowInfo);
Render2();
GL.Flush();
glControl2.SwapBuffers();
}
}
public void Render ()
{
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); // use the visible framebuffer
if (videoFrame != null)
lock (videoFrame)//frames from video 1
{
if (videoTexture != -1)
GL.DeleteTextures(1, ref videoTexture);
videoTexture = LoadTexture(videoFrame);
videoFrame.Dispose();
videoFrame = null;
}
GC.Collect();
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
obj_openTK.DrawImage(videoTexture, glControl1);
}
public void Render2()
{
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); // use the visible framebuffer
if (videoFrame2 != null)
lock (videoFrame2) // frames from video 2
{
if (videoTexture2 != -1)
GL.DeleteTextures(1, ref videoTexture2);
videoTexture2 = LoadTexture(videoFrame2);
videoFrame2.Dispose();
videoFrame2 = null;
}
GC.Collect();
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
obj_openTK.DrawImage(videoTexture2,glControl2);
}
private void glControl1_Paint(object sender, PaintEventArgs e)
{
obj_openTK .Render();
}
private void glControl1_Resize(object sender, EventArgs e)
{
obj_openTK.Init();
}
private void glControl2_Paint(object sender, PaintEventArgs e)
{
obj_openTK.Render2();
}
private void glControl2_Resize(object sender, EventArgs e)
{
obj_openTK.Init();
}
========================
class openTK
{
int positionLocation1;
int program;
int positionLocation;
int vertShader;
int fragShader;
int buffer;
float[] vertices = {
// Left bottom triangle
-1f, -1f, 0f,
1f, -1f, 0f,
1f, 1f, 0f,
// Right top triangle
1f, 1f, 0f,
-1f, 1f, 0f,
-1f, -1f, 0f
};
public int LoadTexture(Bitmap bitmap)
{
int tex = -1;
if (bitmap != null)
{
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
GL.GenTextures(1, out tex);
GL.BindTexture(TextureTarget.Texture2D, tex);
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
bitmap.UnlockBits(data);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
}
return tex;
}
public void DrawImage(int image, GLControl glControl)
{
GL.MatrixMode(MatrixMode.Projection);
GL.PushMatrix();
GL.LoadIdentity();
GL.MatrixMode(MatrixMode.Modelview);
GL.PushMatrix();
GL.LoadIdentity();
GL.Disable(EnableCap.Lighting);
GL.Enable(EnableCap.Texture2D);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, image);
GL.Uniform1(positionLocation1, 0);
RunShaders();
GL.Disable(EnableCap.Texture2D);
GL.PopMatrix();
GL.MatrixMode(MatrixMode.Projection);
GL.PopMatrix();
GL.MatrixMode(MatrixMode.Modelview);
ErrorCode ec = GL.GetError();
if (ec != 0)
System.Console.WriteLine(ec.ToString());
Console.Read();
}
private void RunShaders()
{
GL.UseProgram(program);
GL.DrawArrays(PrimitiveType.Triangles, 0, vertices.Length / 3);
ErrorCode ec = GL.GetError();
if (ec != 0)
System.Console.WriteLine(ec.ToString());
Console.Read();
}
private void Init()
{
CreateShaders();
CreateProgram();
InitBuffers();
}
private void CreateProgram()
{
program = GL.CreateProgram();
GL.AttachShader(program, vertShader);
GL.AttachShader(program, fragShader);
GL.LinkProgram(program);
}
private void InitBuffers()
{
buffer = GL.GenBuffer();
positionLocation = GL.GetAttribLocation(program, "a_position");
positionLocation1 = GL.GetUniformLocation(program, "sTexture");
GL.EnableVertexAttribArray(positionLocation);
GL.BindBuffer(BufferTarget.ArrayBuffer, buffer);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(positionLocation, 3, VertexAttribPointerType.Float, false, 0, 0);
}
private void CreateShaders()
{
/***********Vert Shader********************/
vertShader = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(vertShader, @"attribute vec3 a_position;
varying vec2 vTexCoord;
void main() {
vTexCoord = (a_position.xy+1)/2 ;
gl_Position = vec4(a_position, 1);
}");
GL.CompileShader(vertShader);
/***********Frag Shader ****************/
fragShader = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(fragShader, @"precision highp float;
uniform sampler2D sTexture;varying vec2 vTexCoord;
void main ()
{
vec4 color = texture2D (sTexture, vTexCoord);
gl_FragColor = color;
}");
GL.CompileShader(fragShader);
}
}