Anti-aliased line drawing in OpenGL (below)

0. Preface

        The link describes the aliasing problem and the treatment method when drawing line segments in OpenGL, but the drawing of line segments with continuously changing width has not been realized. Please take out this picture again. I want to realize the anti-aliasing drawing of the line segment with continuously changing width in the picture below. After two days of short and arbitrary thinking, I confirmed that it is impossible to use the native interface in OpenGL, um. . .

1. The principle of anti-aliasing drawing line segments

        Briefly describe the principle of OpenGL anti-aliasing to draw a line segment. Since the line segment has a width, the line segment we draw is actually a rectangle, which is realized by drawing two triangles in OpenGL; if you only do the above operations, it will inevitably produce a sense of jaggedness. ; Then we can add a rectangle of equal length on both sides of the above rectangle, but the inside of the rectangle has a gradient color, which has anti-aliasing effect visually, as shown in the figure below. So the drawing of a line segment is actually achieved by drawing 6 triangles. For the detailed introduction of this part, please refer to http://ju.outofmemory.cn/entry/142921

tessellate

        But the above is just the basic principle of anti-aliasing to draw line segments. There are many details in the actual implementation, such as how to set the height of the two newly added triangles? How to calculate the connecting part between line segments when drawing polygonal line segments? What is the shape (rectangular or rounded) of the start/end portion of the segment? and so on.

 

        Due to time and ability constraints, the poster did not do more in-depth research, but some predecessors have done relevant research and generously shared it on the Internet, which is the vaserenderer library.

2.vaserenderer library

2.1 Introduction

        Project github homepage: https://github.com/tyt2y3/vaserenderer , which is a line segment based on OpenGL1.1 that can draw color, transparency, and width gradient controllable. The line segment types include straight line, polygonal line segment, and Bezier line segment . When calling the interface function, pass in the color, transparency, and width of each control point, and set anti-aliasing related options. The library internally generates triangle combinations with anti-aliasing effects, and calls the relevant drawing interface in OpenGL1.1 to complete the final The drawing of line segments is outstanding. Post some pictures of the effect

2.2 use

        I tried to introduce the library test in the VS project by myself, and the basic process is summarized as follows:

1) Put the source code file into the project directory

2) Add the definition of Vec2 and Color, and then include the vaser.cpp file

namespace VASEr
{
	struct Vec2 { double x, y; };
	struct Color { float r, g, b, a; };
}
#include "vaser\vaser.cpp"

3) Open the vaser.h file and add glew.h to it; this step can be omitted, because VS supports OpenGL1.1 by default

       Note: Do not add the source code files in vaserenderer to the solution manager of the project . This will cause project compilation errors and a bunch of problems that cannot find definitions; the reason is that the source code has a special structural organization and compilation order. . .

2.3 Improvements

        The specific call can refer to the sample in the file, so I won't go into details here. To give the author a thumbs up, the library is very powerful and can fully meet the general line segment drawing needs. But there is one thing that is not very friendly, it is based on OpenGL1.1 implementation, that is, fixed pipeline rendering; but since version 2.0, OpenGL ES on the embedded platform no longer supports fixed pipeline rendering, only supports programmable pipeline rendering. It just so happens that the device in my project uses the OpenGL ES2.0 version, embarrassing.

        In order to complete the project requirements, I roughly sorted out the code structure, skipped the principle part of the code (because I can’t understand it), and made changes to the rendering part of the final primitive to make it compatible with OpenGL2.0 and above ( It is distinguished by the macro definition in config.h), and compiled and verified on the Android platform. In addition, there are some other small changes (non-principle parts) to the code. It is recommended to compare it with the original code.

        Here is the modified library code, and the GL project (VS2015 version) that calls it, for reference, the download link is https://download.csdn.net/download/lwx309025167/11388979

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/lwx309025167/article/details/96352756