none
Multiple glControl on same winform is not working - OpenTK C# RRS feed

  • Question

  • 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? Full code is here. Please check

     Bitmap videoFrame = null;//should show this frames in glcontrol1
        Bitmap videoFrame2 = null;//should show this frames in glcontrol2
        int videoTexture = -1;
        int videoTexture2 = -1;
        int positionLocation;
        int positionLocation1;
        int program;
        int vertShader;
        int fragShader;
        int buffer;
        private void Show_Click(object sender, EventArgs e)
        {
            GL.ClearColor(Color.MidnightBlue);
            GL.Enable(EnableCap.DepthTest);
            TexUtil.InitTexturing();
            GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
            GL.DepthFunc(DepthFunction.Lequal);
            GL.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)
            {
                Render();
                Render2();
            }
        }
         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);
            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);
            DrawImage(videoTexture2,glControl2);
        }
       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();
            glControl.SwapBuffers();
        }
        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 glControl1_Paint(object sender, PaintEventArgs e)
        {
            Render();
        }
        private void glControl1_Resize(object sender, EventArgs e)
        {
            Init();
        }
        private void glControl2_Paint(object sender, PaintEventArgs e)
        {
    
            Render2();
        }
        private void glControl2_Resize(object sender, EventArgs e)
        {
            Init();
        }
        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);
        }

    Wednesday, May 22, 2019 6:05 AM

All replies

  • Hi,

    This forum is discussing and asking questions about the Windows Forms and ClickOnce. But your problem is obviously related to the third-party library. So, it will be more appropriate to submit your question at https://github.com/opentk/opentk/issues.

    Thank you for your understanding.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 22, 2019 6:39 AM