opengl 在winforms中显示OpenTk.GLControl

nwsw7zdq  于 2022-11-04  发布在  其他
关注(0)|答案(2)|浏览(349)

我一直在尝试用C# winforms显示OpenGl图形,我偶然发现了一个非常有用的NuGet包OpenTK。我学习了OpenTK's Learn tab的入门教程,并能够渲染一些简单的形状。这个教程非常有帮助,但它是基于在GameWindow中显示OpenGGL图形(与主窗体分开)。我发现通过使用OpenTK.GLControl控件可以直接在主窗体中显示OpenGL图形。为了使with工作,我安装了附加的NuGet包OpenTK. GLControl。我在Form1.Designer.cs文件中添加了该控件,并在Form1.cs中定义了其必要的方法。但是,我无法看到GLControl。
我是否遗漏了代码中的一个关键步骤,或者packages/dll文件有问题?
Form1.Designer.cs:

private void InitializeComponent()
    {
        /* Generic Form1 code */

        this.gLControl.Location = new System.Drawing.Point(-2, 0);
        this.gLControl.Name = "gLControl";
        this.gLControl.Size = new System.Drawing.Size(500, 300);
        this.gLControl.TabIndex = 0;
        this.gLControl.VSync = false;
        this.gLControl.Load += new System.EventHandler(this.GLControl_Load);
        this.gLControl.Resize += new System.EventHandler(this.GLControl_Resize);
        this.gLControl.Paint += new System.Windows.Forms.PaintEventHandler(this.GLControl_Paint);
    }

    private OpenTK.GLControl gLControl;

Form1.cs:

private bool _loaded;
    private Shader shader;

    public Form1()
    {
        InitializeComponent();

    }

    private void GLControl_Load(object sender, EventArgs e)
    {
        GL.ClearColor(0.3f, 0.2f, 0.3f, 1.0f);
        GL.Enable(EnableCap.DepthTest);
        GL.Viewport(0, 0, gLControl.Width, gLControl.Height);
        shader = new Shader("shader.vert", "shader.frag");
        shader.Use();
        _loaded = true;
        gLControl.Invalidate();
    }

    private void GLControl_Resize(object sender, EventArgs e)
    {
        if (!_loaded)
            return;
        GL.Viewport(0, 0, gLControl.Width, gLControl.Height);
        gLControl.Invalidate();
    }

    private void GLControl_Paint(object sender, PaintEventArgs e)
    {
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        shader.Use();
        gLControl.SwapBuffers();
    }

Shader.cs:

class Shader
{
    int Handle;

    public Shader(string vertexPath, string fragmentPath)
    {
        int VertexShader;
        int FragmentShader;

        string VertexShaderSource;

        using (StreamReader reader = new StreamReader(vertexPath, Encoding.UTF8))
        {
            VertexShaderSource = reader.ReadToEnd();
        }

        string FragmentShaderSource;

        using (StreamReader reader = new StreamReader(fragmentPath, Encoding.UTF8))
        {
            FragmentShaderSource = reader.ReadToEnd();
        }

        VertexShader = GL.CreateShader(ShaderType.VertexShader);
        GL.ShaderSource(VertexShader, VertexShaderSource);

        FragmentShader = GL.CreateShader(ShaderType.FragmentShader);
        GL.ShaderSource(FragmentShader, FragmentShaderSource);

        GL.CompileShader(VertexShader);

        string infoLogVert = GL.GetShaderInfoLog(VertexShader);
        if (infoLogVert != System.String.Empty)
            Console.WriteLine(infoLogVert);

        GL.CompileShader(FragmentShader);

        string infoLogFrag = GL.GetShaderInfoLog(FragmentShader);

        if (infoLogFrag != System.String.Empty)
            Console.WriteLine(infoLogFrag);

        Handle = GL.CreateProgram();

        GL.AttachShader(Handle, VertexShader);
        GL.AttachShader(Handle, FragmentShader);

        GL.LinkProgram(Handle);

        GL.DetachShader(Handle, VertexShader);
        GL.DetachShader(Handle, FragmentShader);
        GL.DeleteShader(FragmentShader);
        GL.DeleteShader(VertexShader);
    }

    public void Use()
    {
        GL.UseProgram(Handle);
    }

    private bool disposedValue = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!disposedValue)
        {
            GL.DeleteProgram(Handle);

            disposedValue = true;
        }
    }

    ~Shader()
    {
        GL.DeleteProgram(Handle);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

shader.vert:


# version 330 core

layout (location = 0) in vec3 aPosition;

void main()
{
    gl_Position = vec4(aPosition, 1.0);
}

shader.frag:


# version 330 core

out vec4 FragColor;

void main()
{
    FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}
ejk8hzay

ejk8hzay1#

您错过了将控件添加到表单的操作:

this.Controls.Add(this.gLControl);
twh00eeo

twh00eeo2#

我对c#比较陌生,对OpenTK也是个菜鸟。我花了几个星期的时间在网上搜索,试图找到能让我在WinForm中使用OpenTK图形控件的代码。下面是目前实际工作的代码,主要基于上面列出的代码。这是我试图回报这个网站上许多发帖者的方式。
发布这段代码的另一个令人信服的原因是,我遇到了许多其他的帖子,人们说WinForms和OpenTK的代码示例非常少,和/或发布的示例已经有很多年了(截至2022年)。
Form1.Designer.cs

private void InitializeComponent()
  {
     this.LblStatus = new System.Windows.Forms.Label();
     this.BtnStart = new System.Windows.Forms.Button();
     this.glControl1 = new OpenTK.GLControl();
     this.SuspendLayout();

请注意上面的glControl标注。注意,我没有在Form1上引用glControl
在同一个文件中,我有关于glControl的其他代码。我假设您知道如何将此代码添加到设计器中,因为必须对Form 1进行相关的标注,才能使代码正常工作。

// glControl1
     // 
     this.glControl1.BackColor = System.Drawing.Color.Silver;
     this.glControl1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
     this.glControl1.Location = new System.Drawing.Point(133, 76);
     this.glControl1.Name = "glControl1";
     this.glControl1.Size = new System.Drawing.Size(638, 354);
     this.glControl1.TabIndex = 0;
     this.glControl1.VSync = false;
     this.glControl1.Load += new System.EventHandler(this.glControl1_Load);
     this.glControl1.Paint += new System.Windows.Forms.PaintEventHandler(this.glControl1_Paint);
     this.glControl1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.GLControl_MouseDown);
     this.glControl1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.GLControl_MouseMove);
     this.glControl1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.GLControl_MouseUp);
     this.glControl1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.GLControl_MouseWheel);
     this.glControl1.Resize += new System.EventHandler(this.glControl1_Resize);

Form1代码:

using System;
  using System.Drawing;
  using System.Windows.Forms;
  using OpenTK.Graphics.OpenGL;

以上是using语句。

private void glControl1_Load(object sender, EventArgs e)
  {
     //GL.ClearColor(0.3f, 0.2f, 0.3f, 1.0f);
     //GL.Enable(EnableCap.DepthTest);
     GL.Viewport(0, 0, glControl1.Width, glControl1.Height);
     //shader = new Shader("shader.vert", "shader.frag");
     //shader.Use();
     bLoaded = true;
     GL.ClearColor(Color.SkyBlue);
     glControl1.Invalidate();
  }

请注意,我注解掉了上面的一些代码,这是因为我对阴影对象不感兴趣,而是专注于二维图形。

private void glControl1_Resize(object sender, EventArgs e)
  {
     if (bLoaded != true)
     {
        return;
     }
     GL.Viewport(0, 0, glControl1.Width, glControl1.Height);
     glControl1.Invalidate();
  }

“调整大小”代码基本上与以前相同。

private void glControl1_Paint(object sender, PaintEventArgs e)
  {
     GL.Clear(ClearBufferMask.ColorBufferBit | 
     ClearBufferMask.DepthBufferBit);
     //shader.Use();
     glControl1.SwapBuffers();
  }

Paint代码基本上是相同的,除了“shader.Use”被注解掉,原因与上面列出的相同(对2D图形感兴趣)。
我确信这里有更有经验的程序员可以对上面列出的代码进行推荐的更新。至少对于那些想要在WinForm上获得glControl的人来说,这是一个起点。

相关问题