我必须同时在两个openGl控件中显示来自两个摄像机的帧。为此,我在表单中放置了两个opengl控件,并相应地将这些帧传递给opengl。但在运行该应用程序时,只有第二个opengl控件可以工作。第一个是保留黑色屏幕。当我移除第二个opengl控件时,第一个是工作的。意味着一次只有一个在工作。我能解决这个问题吗?请检查我的密码。我创建了一个公共类(openTK.cs),用于定义与opengl相关的所有函数,并为每个glcontrol调用它。另外,我在我想要呈现我的东西的线程上创建了一个新的GraphicsContext。
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);
}
}
发布于 2019-05-24 12:24:27
我可能会认为,这是因为OpenTK在创建OpenGL上下文时打开了它。这里清楚地说明:https://www.khronos.org/opengl/wiki/Creating_一个_OpenGL_上下文_(WGL)#Create_这个_上下文,目前一个窗口只能在一个上下文中运行。理论上,您可以切换上下文,如下所示:https://stackoverflow.com/questions/40578910/opentk-multiple-glcontrol-with-a-single-context
这里还有:https://opentk.net/api/OpenTK.GLControl.html是关于它的一个注释:
在使用多个GLControls时,在执行任何OpenGL操作之前,请确保上下文是当前的。MakeCurrent()
当事情真的很奇怪的时候,你可能会在OpenTK上提出问题:https://discord.gg/6HqD48s。在那里经常可以看到OpenTK的开发人员。
https://gamedev.stackexchange.com/questions/172170
复制相似问题