Draw lines with OpenGL

. Call connection line between two points on the line segment display now is not a surprising thing on the screen, most advanced graphics API can be easily achieved, I try to use OpenGL drawing a line where the record about the harvest.

. This level OpenGL graphics API, will usually provide some basic graphics rendering interface, called these basic graphics primitives , OpenGL and provides a point, line, triangle three kinds of primitive drawing interface that you may not believe it, it OpenGL can only draw three graphics, this post is one of the theme line drawing primitives, OpenGL direct rendering.


FIG line drawing in OpenGL element

Given the coordinates of two points, you can directly call the API to draw the line.

//  伪代码
glVertex(x0, y0);           //  点A
glVertex(x1, y1);           //  点B
glDrawPrimitive(GL_LINES);  //  绘制线段图元

If executed properly, you can see a line.

opengl_ line

OpenGL is also provided to adjust the line width and color interface.

//  伪代码
glLineWidth(width)          //  宽度
glLineColor(r, g, b, a)     //  颜色
glVertex(x0, y0);           //  点A
glVertex(x1, y1);           //  点B
glDrawPrimitive(GL_LINES);  //  绘制线段图元

opengl_ crude line

If there are readers of this blog, if you are just readers of this blog, see here, you probably have been overawed by the power of OpenGL, calm down, read on.

Many times, we want to draw a continuous line, OpenGL can easily get this problem, you only need to repeat the above code on it.

//  伪代码
glLineWidth(width)          //  宽度
glLineColor(r, g, b, a)     //  颜色
glVertex(x0, y0);           //  第一条线段点A
glVertex(x1, y1);           //  第一条线段点B
glVertex(x2, y2);           //  第二条线段点A
glVertex(x3, y3);           //  第二条线段点B
glDrawPrimitive(GL_LINES);  //  绘制线段图元

opengl_ segment 2

Just a few lines of code, you can draw two segments, in more than marveling, you may not find in the junction of two segments, obviously missing a piece, because the width of the line is drawn 3 pixels and drawing a line at right angles to OpenGL, two rectangular figure is actually connected end to end, the following can be clearly seen in FIG problem.

opengl_ line 22

It is easy to imagine, the wider the line, the greater the gap junction of the line, most people can not accept such a defect, but most people think it is not a problem, if this blog audience, and exactly what you, you certainly feel the same, but in reality, you guess right, because OpenGL is designed to draw the line primitives even considered this issue, so the above ideas to draw lines, this problem has no solution, no solution of the problem is not problem, so this really is not a problem.


Triangle primitives to draw a line segment

. Many newcomers are often confused by the line primitives, OpenGL drawing lines on the misconception that use it, in fact, the interface OpenGL primitives provided on line is very limited, even so, it is still able to play to the strengths of usage scenarios, but described herein it does not fit the scene.

. To solve the "gap" issue was put it another thought, Mr. segments into a grid, and then populate the grid, so that more flexibility is high.

Grid line _

_ _ Filling line grid

As can be seen from the figure, the line of the junction appears less obtrusive.

Further, since the line is filled with a grid, so we can modify the arbitrary grid and adjusting it by programmable pipeline.


Based on the above line segments drawn

lightning

Lightning segments

lightning

lightning

curve

Curve _ Grid

curve

curve

Guess you like

Origin www.cnblogs.com/mmc1206x/p/11846238.html