我在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時,我想知道爲什麼我的窗口完全空白。
Yep,GL.MatrixMode和Gl.LoadIdentity是這裏的關鍵線。謝謝。 :) – zmockus 2013-06-19 09:19:38