在winforms中显示OpenTk.GLControl



我一直在尝试用C# winforms显示OpenGl图形,我偶然发现了一个名为OpenTK的非常有用的NuGet包。我按照OpenTK的"学习"选项卡上的介绍性教程进行操作,并能够渲染一些简单的形状。本教程非常有帮助,但它是基于在游戏窗口(与主窗体分开(中显示OpengGL图形的。我发现通过使用OpenTK.GLControl控件可以直接在主窗体中显示OpenGL图形。为了完成工作,我安装了额外的NuGet包OpenTK.GLControl。 我在 Form1.Designer.cs 文件中添加了该控件,并在 Form1.cs 中定义了它的必要方法。但是,我无法看到GLControl。

我是否错过了代码中的关键步骤,或者我的包/dll 文件有问题?

表单1.设计器.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;

表格1.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();
}

着色器.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);
}

您错过了将控件添加到窗体:

this.Controls.Add(this.gLControl);

我对 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标注。请注意,我没有提到Form1glControl

在同一文件中,我还有其他关于glControl的代码。我假设有人知道如何将此代码添加到设计器中,因为必须对 Form1 进行相关的标注才能使代码正常工作。

// 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;

以上是使用语句。

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();
}

请注意,我已经注释掉了上面的一些代码。那是因为我对着色对象不感兴趣,而是专注于 2D 图形。

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 代码基本相同,除了"着色器。使用"被注释掉的原因与上面列出的相同(对 2D 图形感兴趣(。

我很肯定这里有更有经验的程序员可以对上面列出的代码进行推荐的更新。至少对于那些想要在WinForm上获得glControl的人来说,这是一个起点。

相关内容

  • 没有找到相关文章

最新更新