如何在MonoDevelopment中使用OpenTK GLWidget进行渲染?

jc3wubiy  于 2022-09-26  发布在  其他
关注(0)|答案(2)|浏览(131)

我正在尝试使用GLWidget来使用OpenTK和GTK#进行开发,这似乎是一件好事,但遗憾的是几乎没有相关的文档。我正在尝试理解如何呈现该Widget中的任何内容。到目前为止,我创建了一个单一开发解决方案,添加了对OpenTK和GLWidget的引用,现在我在Stetic的工具窗格中看到了GLWidget,我添加了一个带有两个插槽的Vbox,在上面的一个中添加了一个菜单栏,在下面的一个中添加了著名的GLWidget。我还为OnRender事件和初始化事件创建了一个事件处理程序,但我甚至不能画一个三角形。有没有人使用过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更改背景颜色。

s4n0splo

s4n0splo1#

好吧,我终于能够让它工作了,主窗口代码应该是这样的:

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(已存档)

rslzwgfq

rslzwgfq2#

本页应该会给出缺失的部分http://www.opentk.com/doc/chapter/2/glcontrol-参见SetupViewport方法(如下所示)-执行类似的GL正射操作以在您的设置中设置投影矩阵(也称为‘Camera’)。

private void SetupViewport()
{
  int w = glControl1.Width;
  int h = glControl1.Height;
  GL.MatrixMode(MatrixMode.Projection);
  GL.LoadIdentity();
  GL.Ortho(0, w, 0, h, -1, 1); // Bottom-left corner pixel has coordinate (0, 0)
  GL.Viewport(0, 0, w, h); // Use all of the glControl painting area
}

相关问题