[英]How correctly draw a polygon with OpenTK
我是OpenTK 的新手,我正在使用以下代碼使用OpenTK繪制大量多邊形
public static void DrawPolygon(Point[] points)
{
GL.Begin(BeginMode.Polygon); //IF I Change this to LineStrip every things will be OK
int numberOfPoints = points.Length;
for (int i = 0; i < numberOfPoints; i++)
{
GL.Vertex2(points[i].X, points[i].Y);
}
GL.End();
}
這是在調用DrawPolygon
之前執行的配置代碼
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(0, width, 0, height, -1, 1); // Bottom-left corner pixel has coordinate (0, 0)
GL.Viewport(0, 0, (int)width, (int)height);
GL.ClearColor(drawing.Color.Transparent);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.Color3(pen.Color);
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
GL.PointSize(5f);
GL.LineWidth(2f);
當我將渲染圖像以 png 格式保存到磁盤時使用此代碼,結果將是這樣的
結果為: GL.Begin(BeginMode.Polygon);
但是,如果我將DrawPolygon
的第一行更改為GL.Begin(BeginMode.LineStrip);
多邊形將按預期呈現,如下所示:
結果為: GL.Begin(BeginMode.LineStrip);
任何人都知道為什么在使用BeginMode.Polygon
時會出現那兩條額外的行?
我相信這是因為GL_POLYGON
/ BeginMode.Polygon
只呈現凸多邊形。 我認為,如果您嘗試渲染凹多邊形,正如您所做的那樣,驅動程序將嘗試拆分您提供的幾何圖形以將其渲染為凸多邊形,因此在您的示例中出現了意外的線條。
在OpenGL中使用多邊形渲染模式是不可取的。 線條和三角形的渲染得到了更高程度的優化,因此速度要快得多。 我建議留在線帶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.