2013-03-18 27 views
0

我在Windows窗體中使用GLControl(OpenTK)繪製一些圖形。但是,問題在於我無法弄清楚,如何使用GL.Ortho()方法。如何在OpenTK中正確使用GL.Ortho?

這是我寫的代碼:我想

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void GlControlPaint(object sender, PaintEventArgs e) 
    { 
     GlControl.MakeCurrent(); 
     GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); 


     GL.Viewport(150, 150, 300, 300); 
     //GL.Ortho(0, 1, 0, 1, -1, 1); 
     GL.ClearColor(Color.White); 

     PaintSquareOrBorder(BeginMode.Quads, Color.Cyan, 0.2, 0.2, 0.45, 0.2, 0.45, -0.2, 0.2, -0.2); 
     PaintSquareOrBorder(BeginMode.Quads, Color.Cyan, 0.1, -0.1, 0.1, 0.1, 0.2, 0.2, 0.2, -0.2); 
     PaintSquareOrBorder(BeginMode.Quads, Color.Cyan, -0.2, -0.2, -0.45, -0.2, -0.45, 0.2, -0.2, 0.2); 
     PaintSquareOrBorder(BeginMode.Quads, Color.Cyan, -0.1, 0.1, -0.1, -0.1, -0.2, -0.2, -0.2, 0.2); 
     PaintSquareOrBorder(BeginMode.Quads, Color.Cyan, -0.1, 0.1, -0.2, 0.2, 0.2, 0.2, 0.1, 0.1); 
     PaintSquareOrBorder(BeginMode.Quads, Color.Cyan, 0.1, -0.1, 0.2, -0.2, -0.2, -0.2, -0.1, -0.1); 
     PaintSquareOrBorder(BeginMode.Quads, Color.Cyan, -0.2, 0.2, -0.2, 0.45, 0.2, 0.45, 0.2, 0.2); 
     PaintSquareOrBorder(BeginMode.Quads, Color.Cyan, 0.2, -0.2, 0.2, -0.45, -0.2, -0.45, -0.2, -0.2); 
     PaintSquareOrBorder(BeginMode.LineLoop, Color.Black, -0.1, -0.1, 0.1, -0.1, 0.1, 0.1, -0.1, 0.1); 

     PaintBordersForMainFigure(); 



     GlControl.SwapBuffers(); 
     GlControl.Refresh(); 

    } 

    private void PaintBordersForMainFigure() 
    { 
     PaintLine(Color.Black, 0.2, 0.2, 0.45, 0.2); 
     PaintLine(Color.Black, 0.45, 0.2, 0.45, -0.2); 
     PaintLine(Color.Black, 0.45, -0.2, 0.2, -0.2); 
     PaintLine(Color.Black, 0.2, -0.2, 0.2, -0.45); 
     PaintLine(Color.Black, 0.2, -0.45, -0.2, -0.45); 
     PaintLine(Color.Black, -0.2, -0.45, -0.2, -0.2); 
     PaintLine(Color.Black, -0.2, -0.2, -0.45, -0.2); 
     PaintLine(Color.Black, -0.45, -0.2, -0.45, 0.2); 
     PaintLine(Color.Black, -0.45, 0.2, -0.2, 0.2); 
     PaintLine(Color.Black, -0.2, 0.2, -0.2, 0.45); 
     PaintLine(Color.Black, -0.2, 0.45, 0.2, 0.45); 
     PaintLine(Color.Black, 0.2, 0.45, 0.2, 0.2); 
    } 

    private static void PaintLine(Color color, double x1, double y1, double x2, double y2) 
    { 
     GL.Color3(color); 

     GL.Begin(BeginMode.Lines); 

     GL.Vertex2(x1, y1); 
     GL.Vertex2(x2, y2); 

     GL.End(); 
    } 

    private static void PaintSquareOrBorder(BeginMode beginMode, Color color, double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) 
    { 
     GL.Color3(color); 

     GL.Begin(beginMode); 

     GL.Vertex2(x1, y1); 
     GL.Vertex2(x2, y2); 
     GL.Vertex2(x3, y3); 
     GL.Vertex2(x4, y4); 

     GL.End(); 
    } 
} 

This is the result I get without GL.Ortho:

This is the result I would like to get with GL.Ortho

But if I uncomment the GL.Ortho code, I get this:

在第一,因爲我只用2D對象,我應該使用Ortho2D。但是,我發現Gl.Ortho2d在OpenTK中不存在。使用官方文檔我發現,除了使用Ortho2d near和far參數分別隱式設置爲-1和1這一事實之外,這兩者之間沒有太大區別。

設置了這些參數後,我得到一個白色屏幕。我想知道,我做錯了什麼?

免責聲明:我不需要確切的座標來實現截圖中的結果。我只是爲了讓你看到我想要做的事情而使用它。當我使用Gl.Ortho2d時,我想知道爲什麼我的窗口完全空白。

回答

1

問題是GL.Ortho()將當前矩陣與正交矩陣相乘。因此,對於每個渲染框架,您不斷將矩陣與新的拼寫矩陣相乘,並且您的視圖會在您無法看到任何內容的地方漂移。

將其更改爲類似的東西,和你看到一個漂亮的動畫:

GL.Ortho(-0.99, 1, -0.99, 1, -1, 1); 

添加這兩條線在你GL.Ortho()調用的前面,這樣的矩陣是你之前的身份矩陣把它與你的正交矩陣相乘。

GL.MatrixMode(MatrixMode.Projection); 
GL.LoadIdentity(); 
+0

Yep,GL.MatrixMode和Gl.LoadIdentity是這裏的關鍵線。謝謝。 :) – zmockus 2013-06-19 09:19:38

0

這是我如何在我的項目。希望有所幫助。但我不記得我爲什麼這樣做。

GL.MatrixMode(MatrixMode.Projection); 
Matrix4 ortho = Matrix4.CreateOrthographic(glControl1.Width, glControl1.Height, 10, 3000); 
GL.LoadMatrix(ref ortho); 
v = Matrix4.LookAt(0, 0, 100, 0, 0, 0, 0, 1, 0); 
GL.MatrixMode(MatrixMode.Modelview); 
GL.LoadMatrix(ref v); 

我的視口是GL.Viewport(0,0,glControl1.Width,glControl1.Height);