[英]How to use `GL.DrawArrays` to draw LineStrip using OpenTK?
[英]How to use GL.Ortho in OpenTK correctly?
我在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();
}
}
但是,如果我取消注釋GL.Ortho代碼,則會得到以下信息:
起初我以為,由於我僅使用2d對象,因此應該使用Ortho2D。 但是,我發現OpenTK中不存在Gl.Ortho2d。 使用官方文檔,我發現這2個之間沒有太大區別,除了使用Ortho2d時,near和far參數分別隱式設置為-1和1。
設置完這些參數后,我得到了白屏。 我想知道,我在做什么錯?
免責聲明:我不需要確切的坐標即可在屏幕截圖中獲得結果。 我只是用它來讓您了解我要做什么。 我想知道為什么當我使用Gl.Ortho2d時窗口完全空白。
問題在於GL.Ortho()將當前矩陣與正交矩陣相乘。 因此,對於每個渲染的幀,您都將矩陣與新的正交矩陣相乘,並且您的視圖會漂移到看不到任何東西的地方。
將其更改為類似的內容,您會看到一個漂亮的動畫:
GL.Ortho(-0.99, 1, -0.99, 1, -1, 1);
在GL.Ortho()調用之前添加這兩行,這樣,在將矩陣與正交矩陣相乘之前,矩陣就是一個單位矩陣。
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
這就是我在我的項目中所做的事情。 希望能有所幫助。 但是我不記得為什么我要這樣做。
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.