gpt4 book ai didi

c# - 如何在 Monodevelop 中使用 OpenTK GLWidget 进行渲染?

转载 作者:太空宇宙 更新时间:2023-11-03 14:04:32 25 4
gpt4 key购买 nike

我正在尝试使用这个 GLWidget 来使用 OpenTK 和 GTK# 进行开发,这似乎是一件好事,但遗憾的是几乎没有关于它的文档。我试图了解如何在该小部件中呈现任何内容。到目前为止,我创建了一个 modevelop 解决方案,添加了对 OpenTK 和 GLWidget 的引用,现在我在 Stetic 的工具 Pane 中看到了 GLWidget,我添加了一个带有两个插槽的 Vbox,在上面的那个我添加了一个菜单栏,在下面的那个著名的 GLWidget。我还为 OnRender 事件和 Initialized 事件创建了事件处理程序,但我什至无法绘制三角形。有没有人曾经使用过 GLWidget 并且可以给我一些建议?这是我的 MainWindow.cs 代码:

   using System;
using Gtk;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using OpenTK.Input;

public partial class MainWindow : Gtk.Window{

public MainWindow () : base(Gtk.WindowType.Toplevel)
{
Build ();
}

protected void GLWidgetInitialize (object sender, System.EventArgs e)
{
int width = 0, height = 0;
//glwidget7.GdkWindow.GetSize(out width, out height);
this.vbox3.GetSizeRequest(out width, out height);
GL.Viewport(0, 0, width, height);
GL.ClearColor(1.0f, 1.0f,1.0f,1.0f);
GL.Clear(ClearBufferMask.ColorBufferBit);
}

protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}


protected void OnRenderFrameWidget (object sender, System.EventArgs e)
{

GL.ClearColor(1.0f, 1.0f,1.0f,1.0f);
GL.Begin(BeginMode.Triangles);

GL.Color3(1.0f, 1.0f, 0.0f); GL.Vertex3(-1.0f, -1.0f, 4.0f);
GL.Color3(1.0f, 0.0f, 0.0f); GL.Vertex3(1.0f, -1.0f, 4.0f);
GL.Color3(0.2f, 0.9f, 1.0f); GL.Vertex3(0.0f, 1.0f, 4.0f);

GL.End();
}

顺便说一句,更改 GLClearColor 值确实会使我的 GLWidget 更改背景颜色。

最佳答案

好吧,我终于让它工作了,MainWindow 代码应该是这样的:

  public partial class MainWindow : Gtk.Window
{

public bool GLinit;

public MainWindow () : base(Gtk.WindowType.Toplevel)
{
Build ();
GLinit = false;
}

protected virtual void GLWidgetInitialize (object sender, System.EventArgs e)
{
//this might be overkill to some people, but worked for me

int width = 0, height = 0;
this.vbox3.GetSizeRequest(out width, out height);
float aspectRatio = width/ height;
GL.Viewport(0, 0, width, height);
GL.ClearColor(1.0f, 1.0f,1.0f,1.0f);
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.ShadeModel(ShadingModel.Smooth);
Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, aspectRatio, 1.0f, 64.0f);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref projection);
GL.ClearDepth(1);
GL.Disable(EnableCap.DepthTest);
GL.Enable(EnableCap.Texture2D);
GL.Enable(EnableCap.Blend);
GL.DepthFunc(DepthFunction.Always);
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
//add idle event handler to process rendering whenever and as long as time is availble.
GLinit = true;
GLib.Idle.Add(new GLib.IdleHandler(OnIdleProcessMain));

}

protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}


protected void RenderFrame(){

//Here's where you write your OpenGL code to draw whatever you want
//Don't forget to swap your buffers

OpenTK.Graphics.GraphicsContext.CurrentContext.SwapBuffers();

}

protected bool OnIdleProcessMain ()
{
if (!GLinit) return false;
else{
RenderFrame();
return true;
}
}
}

有关详细信息,请参见此处:http://www.opentk.com/node/2910 (已存档)

关于c# - 如何在 Monodevelop 中使用 OpenTK GLWidget 进行渲染?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9778875/

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