我在 Windows 窗体中使用 GLControl (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();
}
}
起初我想,因为我只使用 2d 对象,所以我应该使用 Ortho2D。但是,我发现 OpenTK 中不存在 Gl.Ortho2d。使用官方文档我发现,这两者之间没有太大区别,除了当使用 Ortho2d 时,near 和 far 参数分别隐式设置为 -1 和 1。
设置这些参数后,我得到一个白屏。我想知道,我做错了什么?
免责声明:我不需要确切的坐标来获得屏幕截图中的结果。我只是用它来让你看到我想要做什么。我想知道为什么当我使用 Gl.Ortho2d 时我的窗口是完全空白的。