2012-03-19 48 views
2

我想使用這個GLWidget的事情來開發使用OpenTK和GTK#,這似乎是一件好事,但遺憾的是,它旁邊沒有任何文檔。我試圖瞭解如何在該Widget中渲染任何東西。到目前爲止,我創建了monodevelop解決方案,添加了對OpenTK和GLWidget的引用,現在我在Stetic的工具窗格中看到了GLWidget,我添加了一個帶有兩個插槽的Vbox,在上面一箇中添加了一個菜單欄,着名的GLWidget。我還爲OnRender事件和初始化事件創建了一個事件處理程序,但我甚至無法繪製三角形。有沒有人曾與GLWidget合作過,可以給我一些建議?這裏是我的MainWindow.cs代碼:如何在Monodevelop中使用OpenTK GLWidget進行渲染?

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改變背景顏色。

+0

你需要設置投影機(相機) - 另外,請瀏覽www.opentk.com網站上的介紹文檔。在渲染的第一部分中有簡單的「繪製三角形」樣本。 – holtavolt 2012-03-20 02:49:18

+0

感謝您的評論,我看到了這些示例,但它們僅適用於GameWindow,不適用於GLWidget。 – Samssonart 2012-03-20 17:22:10

回答

1

好了,我終於能夠得到它的工作,在主窗口的代碼看起來應該是這樣的:

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

0

這個頁面應該給你缺少的部分http://www.opentk.com/doc/chapter/2/glcontrol - 請參閱SetupViewport方法(下面) - 執行類似的GL.Ortho操作來設置投影矩陣(又名'相機')在您的設置。

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 
} 
+0

感謝您的評論,我已經看過該教程了,但那是GLControl,它只適用於Windows窗體,不適用於GTK#,GLWidget是一個GTK#工具,http://www.opentk.com/project/glwidget – Samssonart 2012-03-20 17:24:18