OpenGL Super Treasure Note-Draw triangle

Source: Internet
Author: User

http://my.oschina.net/sweetdark/blog/161002

Learning the knowledge of drawing lines, we can use Gl_line_loop to draw closed polygons. But in this way, only the wireframe is drawn, and the polygon has no fill color. OpenGL supports drawing a solid polygon and fills it with the current color.

Triangle

For a simple triangle, you need to specify three vertices.

   1:glbegin (Gl_triangles);
   2:     glvertex2f (0.0f, 0.0f);            V0
   3:     glvertex2f (25.0f, 25.0f);          V1
   4:     glvertex2f (50.0f, 0.0f);           V2
   5:     glvertex2f ( -50.0f, 0.0f);          V3
   6:     glvertex2f ( -75.0f, 50.0f);         V4
   7:     glvertex2f ( -25.0f, 0.0f);          V5
   8:glend ();
   9:

The two triangles are populated with the current color.

Wrapping of polygons

The combination of order and direction specified by vertices is called wrapping. The two polygons are circled clockwise. We can change the wrapping direction by changing the order of the vertices. We swapped the order of V4 and V5, and the triangle was counterclockwise in the direction of its orbit.

OpenGL defaults to a counterclockwise wrapping direction in the positive direction, that is, the triangle around the counter-clockwise is positive. The triangle on the left shows the front, and the right side shows the opposite. We can also use the function Glfrontface (GL_CW) to tell OpenGL that the polygon around clockwise is positive. The wrapping direction is a very useful property of polygons that can be used to eliminate unnecessary polygons.

Triangle Band

We can draw multiple triangles together to form multiple polygons or polygons. With Gl_triangle_strip primitives, you can draw a bunch of connected polygons to save a lot of time.

As you can see, the drawing order of the sides of the polygon is not exactly in the order of the vertices we specify. Instead, it is drawn in a counterclockwise direction in the order specified by OpenGL.

There are two advantages to using a triangle band instead of specifying each triangle's vertices individually:

    1. Once you specify a triangle with the previous three vertices, you can draw a second triangle by specifying a vertex. This method saves a lot of code and data storage space when you are drawing a large number of triangles
    2. Increased computational performance and bandwidth savings. Fewer vertices, a faster time to transfer graphics from memory, and fewer vertices to participate in transforms.
Triangular fan

In addition to the triangle bands, we can also create a set of triangular fans around a central point through the Gl_triangle_fan entity. By making 5 vertices, we can draw 3 triangles to form a triangular fan. After specifying a triangle with 3 vertices, the subsequent vertex vn is formed by a triangle with the Origin V0 and a previous vertex Vn-1.

Create a solid object


   1:static void Renderscene ()
   2: {
   3:   //Clear color buffer and depth buffer
   4:     glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit);
   5:
   6:     glfloat x, y, z, Angle;
   7:   //For color judging
   8:   bool color;
   9:     glcolor3f (1.0f, 1.0f, 0.0f);
  :   //whether to turn on depth buffer
  One:   if (bdepth)
  :   {
  :     glenable (gl_depth_test);
  :   }
  :   Else
  :   {
  :     gldisable (gl_depth_test);
  :   }
  :   //whether to turn on hidden surface removal
  :   if (bcull)
  :   {
  :     glenable (Gl_cull_face);
  :   }
  :   Else
  :   {
  :     gldisable (Gl_cull_face);
  :   }
  28:   
  :   if (boutline)
  :   {
  To:     Glpolygonmode (Gl_back, gl_line);
  :   }
  :   Else
  :   {
  :     Glpolygonmode (Gl_back, Gl_fill);
  :   }
  37:
  38:
  :   Glpushmatrix ();
  Max:   Glrotatef (Xrot, 1.0f, 0.0f, 0.0f);
  :   Glrotatef (Yrot, 0.0f, 1.0f, 0.0f);
  :   //Draw the vertebral body
  :   Glbegin (Gl_triangle_fan);
  :     glvertex3f (0.0f, 0.0f, 70.0f);
  45:
  : for     (angle = 0.0f; angle <= (2 * gl_pi); angle + = (gl_pi  /8))
  :     {
  :           x = 50.0f * sin (angle);
  A:           y = 50.0f * cos (angle);
  :           color =!color;
  51:
  :       glvertex3f (x, y, 0.0f);
  :           if (color)
  Si:               glcolor3f (1.0f, 0.0f, 0.0f);
  :           Else
  :               glcolor3f (0.0f, 1.0f, 1.0f);
  £ º     }
  :   glend ();
  :   //Draw the underside of the vertebral body
  :     Glbegin (Gl_triangle_fan);
  :       glvertex2f (0.0f, 0.0f);
  62:
  : for       (angle = 0.0f; angle <= (2 * gl_pi); angle + = (gl_pi  /8))
  :       {
  N:           x = 50.0f * sin (angle);
  :           y = 50.0f * cos (angle);
  :           color =!color;
  68:
  :           glvertex2f (x, y);
  70:
  :           if (color)
  A:               glcolor3f (1.0f, 0.0f, 0.0f);
  I:           Else
  :               glcolor3f (0.0f, 1.0f, 1.0f);
  :       }
  :     glend ();
  77:
  :   Glpopmatrix ();
  :     glutswapbuffers ();
  80:}
  81:
  82:static void SETUPRC ()
  83: {
  :     Glclearcolor (0.0f, 0.0f, 0.0f, 1.0f);
  :   //Set the surround direction
  :     glenable (GL_CW);
  :   //Set fill mode
  :     Glshademodel (Gl_flat);
  :   //Set the hidden face to be removed, set to reverse
  :   glcullface (Gl_back);
  91:}

Use a red and green way to draw a cone.

Set the color of a polygon

There are two types of polygon fill modes: Monotone coloring Gl_flat, a smoothing shading gl_smooth.

Set by Glshademode (Gl_flat), and Glshademode (Gl_smooth); Monotone coloring fills a triangle with the color of the last vertex of the triangle. Smoothing shading is the interpolation of the color values of three vertices to fill the color of the triangle.

The smoothing shading effect is as follows:

Hide Polygon Elimination

If the depth buffer is not used, the depth test is performed. The top of the vertebral body rotation (through the arrow keys can be rotated), is the bottom of the display. Because the bottom side is drawn at the back, the last drawn object appears in front of the previously drawn object. If you turn on deep testing to fix the problem.

Depth test: When a pixel is drawn, it is set to a value (z-value) to indicate its distance from the Observer. Later, when the new pixel is to be drawn in this position, the z-value of the new pixel is compared to the z-value of the old pixel. If the new pixel Z value is higher, then closer to the observer, it should be drawn, otherwise ignored.

The depth test should only be called glenable (Gl_depth_test), and the depth buffer should be clear before each scene is rendered, similar to a clear color buffer.

Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit);

In the program use right click, pop-up menu, depth test to switch depth test. Depth testing is often required when drawing 3D solids.

Eliminate

Although deep testing is enabled, you can eliminate hidden polygons. But most of the time, we know that those faces do not need to be drawn and should be removed. For example, the back of a polygon should not be displayed. If we tell OpenGL that the graphics surface does not need to be drawn, it will not send this graphic to OpenGL drivers and hardware, which can improve performance.

Enables the culling polygon feature.

Specifies that faces that are surrounded clockwise are positive

Glfrontface (GL_CW);

Open the Reject face and remove the back.

Glenable (Gl_cull_face);

The underside of the cone faces the inside of the cone is the front. To change the face of the bottom surface, you can set Glfrontface (GL_CCW) When you draw the bottom surface again;

Polygon Mode

Polygon we can specify its pattern, fill or draw only the wireframe, or just draw vertices.

by Glpolygonmode (Gl_back, Gl_line), set the back of the polygon to draw only lines.

by Glpolygonmode (Gl_back, Gl_fill), set the back of the polygon as an entity.

by Glpolygonmode (Gl_back, Gl_point), set the back of the polygon to draw only points.

Source code: Https://github.com/sweetdark/openglex/tree/master/triangle

OpenGL Super Treasure Note-Draw triangle (turn)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.