OpenGL basic graphics programming-OpenGL Modeling

Source: Internet
Author: User
The OpenGL Basic Library provides a large number of methods for drawing various types of elements, and the auxiliary Library also provides many functions to describe complex 3D images. This chapter mainly introduces basic elements, such as points, lines, and polygon. With these elements, you can create a complex model.

7.1. description elements
OpenGL is a function library for 3D graphics. Its elements such as points, lines, and polygon are not the same as those defined in general. It is important for programmers to understand the differences between the two. One difference stems from the limitations of computer-based computing. All floating point calculation accuracy in OpenGL is limited, so there is a certain error in the coordinate values of points, lines, and polygon. Another difference stems from the limitations of bitmap display. In this way, the smallest display element is a pixel. Although the width of each pixel is small, they are much larger than the point or line width defined in mathematics. When OpenGL is used for computation, although a series of floating point values are used to define the vertex string, each vertex is still displayed in a single pixel, which is just an approximate fit.
OpenGL elements are abstract geometric concepts rather than objects in the real world. Therefore, they must be described using mathematical models.

  7.1.1 homogeneous coordinates(Homogeneous coordinate)
In the space Cartesian coordinate system, any point can be represented by a three-dimensional coordinate matrix [x y z. If the point is represented by a four-dimensional coordinate matrix [HX hy Hz H], it is called the homogeneous coordinate representation method. In homogeneous coordinates, the last one-dimensional coordinate H is called a proportional factor.
In OpenGL, two-dimensional coordinate points are regarded as three-dimensional coordinate points. All points are described by homogeneous coordinates and processed as three-dimensional homogeneous points. Each homogeneous point is represented by a vector (X, Y, Z, W). All four elements are not zero. Homogeneous points have the following properties:
1) if the real number A is non-zero, (x, y, X, W) and (ax, ay, AZ, AW) represent the same point, similar to x/y = (ax) /(AY ).
2) the homogeneous coordinate of three-dimensional spatial points (x, y, z) is (X, Y, Z, 1.0), two-dimensional plane points (x, y) is (X, Y, 0.0, 1.0 ).
3) When W is not zero, X, Y, Z, and W are the coordinate points of the three-dimensional space (x/W, Y/W, Z/W ); when W is zero, the point (X, Y, Z, 0.0) indicates that this point is located in an infinite distance in a certain direction.
  Note:: In OpenGL, W is greater than or equal to 0.0.

  7.1.2 point (Point)
Vertex is a vertex represented by a floating point value ). All vertices are processed as three-dimensional points during OpenGL internal computation. The default Z value of vertices defined by two-dimensional coordinates (x, y) in OpenGL is 0. All vertex coordinates are represented by homogeneous coordinates (x, y, z, W). If W is not 0.0, the vertices in these homogeneous coordinates are three-dimensional spatial points (x/W, Y/W, Z/W ). Programmers can specify the W value, but few do so. Generally, the default value of W is 1.0.

  7.1.3 line (Line)
In OpenGL, a line represents a line segment. It is not a mathematical line extending infinitely along the two directions of the axis. The line here is connected by a series of vertices, which are closed and not closed. See Figure 7-1.

Figure 7-1 two link modes of a line segment

  7.1.4 polygon (Polygon)
The polygon defined in OpenGL is a closed area that is sequentially linked by a series of line segments. These line segments cannot be crossed, and there cannot be holes in the area. Polygon must be convex polygon; otherwise, they cannot be accepted by OpenGL functions. The valid and invalid polygon icons are shown in figure 7-2.

Figure 7-2 valid and invalid Polygon

OpenGL polygon can be a plane polygon, that is, all vertices are on a plane or a space polygon. More complex polygon will be introduced in the article on improvement.

7.2 plot Elements

  7.2.1 define Vertex
In OpenGL, all geometric objects are finally described by vertex sets with certain sequence.
The function glvertex {234} {sifd} [v] (type coords) can be used to define vertices in 2D, 3D, or homogeneous coordinates. Example:

Glvertex2s (2, 3 );
Glvertex3d (0.0, 1.0, 3.1414926535 );
Glvertex4f (2.4, 1.0,-2.2, 2.0 );
Glfloat PP [3] = {5.0, 2.0, 10.2 };
Glvertex3fv (PP );

The first example represents a space vertex (2, 3, 0), the second example defines a vertex with a double-precision floating point number, and the third example defines a vertex with a homogeneous coordinate, the actual coordinates are (1.2, 0.5,-1.1). In the last example, a pointer (OR array) is used to define the vertex.

  7.2.2 construct geometric elements
In practice, a set of related vertex sequences are usually organized to define a geometric element in a certain way, instead of defining multiple vertices separately to construct geometric elements. In OpenGL, all defined vertices must be placed between glbegain () and glend () functions to correctly express a geometric element or object. Otherwise, glvertex *() no operation is completed. For example:

Glbegin (gl_polygon );
Glvertex2f (0.0, 0.0 );
Glvertex2f (0.0, 3.0 );
Glvertex2f (3.0, 3.0 );
Glvertex2f (4.0, 1.5 );
Glvertex2f (3.0, 0.0 );
Glend ();

The above section defines a polygon. If you change the gl_polygon parameter in glbegin () to gl_points, the graph is changed to a set of vertices (5), as shown in figure 7-3.
  

Figure 7-3 draw a polygon or a set of vertices

The glbegin (glenum mode) sign describes the beginning of the vertex list of a geometric element. The mode parameter indicates the description type of the geometric element. All types and descriptions are shown in Table 7-1. See figure 7-4.

Type Description
Gl_points Single vertex set
Gl_lines Multiple groups of Dual-vertex Line Segments
Gl_polygon Single simple fill Convex Polygon
Gl_traingles Multiple groups of independent filled triangles
Gl_quads Multiple groups of independently filled Quadrilateral
Gl_line_strip Do not close the line
Gl_line_loop Closed line
Gl_traingle_strip Linear Continuous filling triangle string
Gl_traingle_fan Slice continuous filling triangle string
Gl_quad_strip Continuous filling of quadrilateral strings
Table 7-1 geometric element types and descriptions

Figure 7-4 geometric element type

The function glend () indicates the end of the vertex list.
From Figure 7-4, we can see that many methods can be used to construct geometric elements. These methods only depend on the given vertex data.
The most important information between glbegin () and glend () is the vertex defined by the glvertex * () function, if necessary, you can also specify the color, direction, texture coordinate, or other parameters for each vertex. Call related functions, as shown in table 7-2.

Function Function meaning
Glvertex *() Set vertex coordinates
Glcolor *() Set current color
Glindex *() Set the current color table
Glnormal *() Set normal coordinates
Glevalcoord *() Generate coordinates
Glcalllist (), glcalllists () Execution display list
Gltexcoord *() Set texture coordinates
Gledgeflag *() Control boundary plotting
Glmaterial *() Set Material
Table 7-2 callable functions between glbegin () and glend ()

See the following sentence:

Glbegin (gl_points );
Glcolor3f (1.0, 0.0, 0.0);/* red color */
Glvertex (...);
Glcolor3f (0.0, 1.0, 0.0);/* green color */
Glcolor3f (0.0, 0.0, 1.0);/* blue color */
Glvertex (...);
Glvertex (...);
Glend ();

The color setting is only valid for the current or subsequent vertex. In the previous example, the first vertex is red, and the second vertex and the third vertex are blue. When green is set, no vertex operation is performed afterwards, but blue is set. Therefore, only the current Blue is effective for the two vertices that follow it.
To better understand the usage of geometric element functions, the following is a simple example:

  Example 7-3 geometric element construction routine(Drawgeom. c)

# Include "Glos. H"
# Include <Gl/Gl. h>
# Include <Gl/Glaux. h>

Void myinit (void );
Void drawmyobjects (void );
Void callback myreshape (glsizei W, glsizei H );
Void callback display (void );

Void myinit (void)
{
Glclearcolor (0.0, 0.0, 0.0, 0.0 );
Glclear (gl_color_buffer_bit );
Glshademodel (gl_flat );
}

Void callback myreshape (glsizei W, glsizei H)
{
Glviewport (0, 0, W, H );
Glmatrixmode (gl_projection );
Glloadidentity ();

If (W <= H)
Glortho (-20.0, 20.0,-20.0 * (glfloat) h/(glfloat) W, 20.0 * (glfloat) h/(glfloat) W,-50.0, 50.0 );
Else
Glortho (-20.0 * (glfloat) h/(glfloat) W, 20.0 * (glfloat) h/(glfloat) W,-20.0, 20.0,-50.0, 50.0 );
Glmatrixmode (gl_modelview );
Glloadidentity ();
}

Void callback display (void)
{
Glcolor3f (1.0, 1.0, 0.0 );
Drawmyobjects ();
Glflush ();
}

Void drawmyobjects (void)
{
/* Draw some points */
Glbegin (gl_points );
Glcolor3f (1.0, 0.0, 0.0 );
Glvertex2f (-10.0, 11.0 );
Glcolor3f (1.0, 1.0, 0.0 );
Glvertex2f (-9.0, 10.0 );
Glcolor3f (0.0, 1.0, 1.0 );
Glvertex2f (-8.0, 12.0 );
Glend ();

/* Draw some line_segments */
Glbegin (gl_lines );
Glcolor3f (1.0, 1.0, 0.0 );
Glvertex2f (-11.0, 8.0 );
Glvertex2f (-7.0, 7.0 );
Glcolor3f (1.0, 0.0, 1.0 );
Glvertex2f (-11.0, 9.0 );
Glvertex2f (-8.0, 6.0 );
Glend ();

/* Draw one opened_line */
Glbegin (gl_line_strip );
Glcolor3f (0.0, 1.0, 0.0 );
Glvertex2f (-3.0, 9.0 );
Glvertex2f (2.0, 6.0 );
Glvertex2f (3.0, 8.0 );
Glvertex2f (-2.5, 6.5 );
Glend ();

/* Draw one closed_line */
Glbegin (gl_line_loop );
Glcolor3f (0.0, 1.0, 1.0 );
Glvertex2f (7.0, 7.0 );
Glvertex2f (8.0, 8.0 );
Glvertex2f (9.0, 6.5 );
Glvertex2f (10.3, 7.5 );
Glvertex2f (11.5, 6.0 );
Glvertex2f (7.5, 6.0 );
Glend ();

/* Draw one filled_polygon */
Glbegin (gl_polygon );
Glcolor3f (0.5, 0.3, 0.7 );
Glvertex2f (-7.0, 2.0 );
Glvertex2f (-8.0, 3.0 );
Glvertex2f (-10.3, 0.5 );
Glvertex2f (-7.5,-2.0 );
Glvertex2f (-6.0,-1.0 );
Glend ();

/* Draw some filled_quandrangles */
Glbegin (gl_quads );
Glcolor3f (0.7, 0.5, 0.2 );
Glvertex2f (0.0, 2.0 );
Glvertex2f (-1.0, 3.0 );
Glvertex2f (-3.3, 0.5 );
Glvertex2f (-0.5,-1.0 );
Glcolor3f (0.5, 0.7, 0.2 );
Glvertex2f (3.0, 2.0 );
Glvertex2f (2.0, 3.0 );
Glvertex2f (0.0, 0.5 );
Glvertex2f (2.5,-1.0 );
Glend ();

/* Draw some filled_strip_quandrangles */
Glbegin (gl_quad_strip );
Glvertex2f (6.0,-2.0 );
Glvertex2f (5.5, 1.0 );
Glvertex2f (8.0,-1.0 );
Glcolor3f (0.8, 0.0, 0.0 );
Glvertex2f (9.0, 2.0 );
Glvertex2f (11.0,-2.0 );
Glcolor3f (0.0, 0.0, 0.8 );
Glvertex2f (11.0, 2.0 );
Glvertex2f (13.0,-1.0 );
Glcolor3f (0.0, 0.8, 0.0 );
Glvertex2f (14.0, 1.0 );
Glend ();

/* Draw some filled_triangles */
Glbegin (gl_triangles );
Glcolor3f (0.2, 0.5, 0.7 );
Glvertex2f (-10.0,-5.0 );
Glvertex2f (-12.3,-7.5 );
Glvertex2f (-8.5,-6.0 );
Glcolor3f (0.2, 0.7, 0.5 );
Glvertex2f (-8.0,-7.0 );
Glvertex2f (-7.0,-4.5 );
Glvertex2f (-5.5,-9.0 );
Glend ();

/* Draw some filled_strip_triangles */
Glbegin (gl_triangle_strip );
Glvertex2f (-1.0,-8.0 );
Glvertex2f (-2.5,-5.0 );
Glcolor3f (0.8, 0.8, 0.0 );
Glvertex2f (1.0,-7.0 );
Glcolor3f (0.0, 0.8, 0.8 );
Glvertex2f (2.0,-4.0 );
Glcolor3f (0.8, 0.0, 0.8 );
Glvertex2f (4.0,-6.0 );
Glend ();

/* Draw some filled_fan_triangles */
Glbegin (gl_triangle_fan );
Glvertex2f (8.0,-6.0 );
Glvertex2f (10.0,-3.0 );
Glcolor3f (0.8, 0.2, 0.5 );
Glvertex2f (12.5,-4.5 );
Glcolor3f (0.2, 0.5, 0.8 );
Glvertex2f (13.0,-7.5 );
Glcolor3f (0.8, 0.5, 0.2 );
Glvertex2f (10.5,-9.0 );
Glend ();
}

Void main (void)
{
Auxinitdisplaymode (aux_single | aux_rgba );
Auxinitposition (0, 0, 500,500 );
Auxinitwindow ("geometric primitive types ");
Myinit ();
Auxreshapefunc (myreshape );
Auxmainloop (Display );
}

The above program running result is shown in Figure 7-4. This example demonstrates the usage of functions such as the type and color of geometric elements. It is hoped that the reader will carefully analyze the rendering method of each object and understand the key points to achieve the opposite effect. Of course, you can also use the basic 3D elements provided in the auxiliary library in the previous chapter to construct complex objects. You may also try it.

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.