简体   繁体   English

如何使用 OpenTK 正确绘制多边形

[英]How correctly draw a polygon with OpenTK

I'm new to OpenTK and I'm using the following code to draw large number of polygons using 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();
}

And this is the configuration code which executes before calling the DrawPolygon这是在调用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);

Using this code when I save the rendered image to disk as png, the result will be like this当我将渲染图像以 png 格式保存到磁盘时使用此代码,结果将是这样的

在此处输入图片说明

result for: GL.Begin(BeginMode.Polygon);结果为: GL.Begin(BeginMode.Polygon);

However If I change the first line of the DrawPolygon to GL.Begin(BeginMode.LineStrip);但是,如果我将DrawPolygon的第一行更改为GL.Begin(BeginMode.LineStrip); the polygon would be rendered as expected like this:多边形将按预期呈现,如下所示:

在此处输入图片说明

result for : GL.Begin(BeginMode.LineStrip);结果为: GL.Begin(BeginMode.LineStrip);

Anyone knows why those two extra lines appears when using BeginMode.Polygon ?任何人都知道为什么在使用BeginMode.Polygon时会出现那两条额外的行?

I believe it's because GL_POLYGON / BeginMode.Polygon only renders convex polygons .我相信这是因为GL_POLYGON / BeginMode.Polygon只呈现凸多边形 I think that if you attempt render a concave polygon, as you're doing, the driver will try split up the geometry that you give it to render it as convex polygons, hence the unexpected lines in your example.我认为,如果您尝试渲染凹多边形,正如您所做的那样,驱动程序将尝试拆分您提供的几何图形以将其渲染为凸多边形,因此在您的示例中出现了意外的线条。

It's inadvisable to use the polygon rendering mode in OpenGL.在OpenGL中使用多边形渲染模式是不可取的。 The rendering of line strips and triangles is much more highly optimized, and therefore much, much quicker.线条和三角形的渲染得到了更高程度的优化,因此速度要快得多。 I'd advise staying with line strips.我建议留在线带。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM