gpt4 book ai didi

c# - 在 winforms 中显示 OpenTk.GLControl

转载 作者:行者123 更新时间:2023-12-04 10:39:55 36 4
gpt4 key购买 nike

我一直在尝试在 C# winforms 中显示 OpenGl 图形,我偶然发现了一个非常有用的 NuGet 包,称为 OpenTK。我遵循了 OpenTK's Learn tab 上的介绍性教程,并能够渲染一些简单的形状。该教程非常有用,但它的前提是在 GameWindow 中显示 OpengGL 图形(与主窗体分开)。我发现可以通过使用 OpenTK.GLControl 控件直接在主窗体中显示 OpenGL 图形。为了工作,我安装了额外的 NuGet 包 OpenTK.GLControl。我在 Form1.Designer.cs 文件中添加了控件,并在 Form1.cs 中定义了其必要的方法。但是,我看不到 GLControl。

我是否遗漏了代码中的关键步骤,或者我的包/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();
}

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

着色器片段:

#version 330 core
out vec4 FragColor;

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

最佳答案

您没有将控件添加到表单:

this.Controls.Add(this.gLControl);

关于c# - 在 winforms 中显示 OpenTk.GLControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59977236/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com