In three, OpenGL entry – pyramid, hexagon, ring drawing of the article, for the frame drawing, and did not make a detailed description, this part will be supplemented and improved below.

First, let’s think about what it would look like to draw a pyramid without the problem of drawing a black border. The effect is as follows:

As can be seen from the figure, there are problems similar to ZFighting in the process of pyramid drawing. The main problem is that OpenGL cannot distinguish layer relations through layers with the same color, so it is necessary to draw black borders for the graph to distinguish different layers.

The drawing process of the black layer is shown below

There are two main parts to a function

  • The drawing of graph is pyramid
  • Drawing a border is a black border

Graph drawing here is more than explained, mainly about the process of border drawing

Borders drawn

  • Turn on polygon offset and set the offset

Drawing padding and edges in the same position at the same time will cause ZFighting problem, which can be solved by polygon offset, so you need to set an offset, usually -1 and -1 by default

GlPolygonOffset (1.0 f, 1.0 f); // Enable line depth offset glEnable(GL_POLYGON_OFFSET_LINE);Copy the code
  • Turn on the anti-aliasing function: Mainly to make the lines smoother
glEnable(GL_LINE_SMOOTH);

Copy the code
  • Color mixing is mainly applied to the mixing of border color and fill color, but personally, there is no need to turn on color mixing, as there is no translucent color in the upper layer of the figure that needs to be mixed with other colors (it has been verified in the code, there is no difference between turning on and off color).
glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Copy the code
  • In fact, in the whole drawing process, the graph is drawn on both sides, the first time to fill the pyramid with colors and faces, the second time to fill the line frame with colors and lines
// Set the front or back of the polygon to wireframe mode by calling glPolygonMode. GlPolygonMode (GL_FRONT_AND_BACK, GL_LINE); // Set line width glLineWidth(2.5f); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vBlack); pBatch->Draw();Copy the code
  • Restore Settings properties
// Set the front or back of the polygon to full fill mode by calling glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glDisable(GL_POLYGON_OFFSET_LINE); GlLineWidth (1.0 f); glDisable(GL_BLEND); glDisable(GL_LINE_SMOOTH);Copy the code

The final result is shown below

Author: Style_ month links: www.jianshu.com/p/58b973d0d… The copyright of the book belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.