OpenGL (1) 2D drawing: saving elements, polygon, color interpolation, text, query and error, and status

Source: Internet
Author: User

Three basic types of elements: point, line segment, and polygon. Other complex objects are built by these three elements.


Point

Void glpointsize (glfloat size) // set the point size state variable in pixels. The default value is 1.0.

Note: glpointsize () cannot be placed between glbegin () and glend () functions.


Straight Line

When glbegin (mode) draws a straight line, the mode value can be:

Gl_lines: draw a line segment between each two consecutive vertices of the glbegin () and glend () functions.

Gl_line_strip: line segment that is connected at the beginning and end.

Gl_line_loop: connects the first vertex and the last vertex to the first vertex.


The attributes of a straight line include color, line width, and mode (create a dotted line or dot line)


Void gllinewidth (glfloat width) // sets the line width. The default value is 1.0.

Void gllinestipple (glint factor, glushort pattern) // pattern is a hexadecimal number, which is read from the low bit. If the number of digits is 1, the pixel is drawn, if the value is 0, no painting is performed. factor indicates the number of times that the painting is repeated.


Enable OpenGL

OpenGL provides many rendering functions, such as illumination, blanking, and texture ing. Each feature affects the rendering processing speed. Therefore, many features are disabled by default. If needed, they are enabled. If not, they are disabled.

Void glable (glenum feature)

Void gldisable (glenum feature)

// Enable or disable a feature in OpenGL


For example, to draw a dot line in the preceding example, you must enable the dot line mode)


Note: do not enable a feature before using it. Otherwise, it is futile to set relevant parameters.


Filled Element

Elements with internal areas can be filled in a certain color or mode during display. The glbegin () parameter can be:

Gl_polygon: draws a convex polygon.

Gl_triangle :( P0, P1, P2), (P3, P4, P5), (P6, P7, P8), excessive vertices will be ignored.

Gl_triangle_strip :( P0, P1, P2), (P1, P2, P3), (P2, P3, P4 ).....

Gl_triangle_fan :( Po, P1, P2), (P0, P2, P3), (P0, P3, P4 ).......

Gl_quads :( P0, P1, P2, P3), (P4, P5, P6, P7 )..........

Gl_quan_strip :( P0, P1, P2, P3), (P2, P3, P4, P5 )..........


Rectangle:

Void glrect <sidf> (type X1, type Y1, type X2, type Y2)

Void glrect <sidf> V (type * V1, ytpe * V2)


Polygon Point Mode:

Glenable (gl_polygon_stipple)

Void glpolygonstipple (const glubyte * mask) mask is 32*32-bit


Polygon type

In OpenGL, a polygon can be displayed in three different ways: Fill mode, edge mode, or point set mode corresponding to the vertex. A two-dimensional polygon actually has two sides: the front and the back. OpenGL can be used to draw some or all of these faces.

By default, if the order of the polygon is clockwise, the face is the front, otherwise it is the back.


Void glpolymode (glenum face, glenum Mode)

Specify how the surface (gl_front, gl_back, gl_front_and_back) is drawn (gl_point, gl_line, and gl_fill ). By default, both the front and back are filled.


We can also not draw one or all of them on the surface.

Void glcullface (glenum Mode)

Ignore when drawing (gl_front, gl_back, gl_front_and_back)


Void glfrontface (glenum Mode)

Clockwise (gl_ccw) or clockwise (gl_cw) is defined as positive orientation


Void glpolygonoffset (glfloat factor, glfloat units)

Set the offset of the polygon edge. The offset can be positive or negative and can be enabled for Polygon in any mode.

Glenable (gl_polygon_offset_line)


Color Interpolation

First look at the code segment:

    glBegin(GL_LINES);        glColor3f(1.0,0.0,0.0);        glvertex2f(1.0,1.0);        glColor3f(0.0,0.0,1.0);        glvertex2f(0.0,1.0);     glEnd();

In the above Code, two vertices are defined as red and blue. But what color will OpenGL use to draw points between these vertices?

By default, OpenGL uses smooth coloring and interpolation through vertices at both ends. Another mode is the plane mode, where the color of the last vertex is colored.

Void glshademodel (glenum Mode)

Set the model to gl_smooth or flat ). The default mode is smooth.


Discrete processing and edge labeling

If you want to generate a non-convex polygon, you can say that the non-convex polygon is discrete into multiple triangles for processing. If you want to fill in the display, there is no problem, but if you only want to display edges, several more edges are drawn inside a non-convex polygon. The solution is to use gledgeflag *(). If this flag is set to gl_true. Each vertex is considered as the starting point of the line segment to be displayed. If gl_false is marked, the vertex that forms a false edge is not displayed.

Void gledgeflag (glboolean flag)

Void gledgeflagv (glboolean * flag)


Text:

Void glubitmapcharacter (void * font, int char)

Font specifies the font to draw the character char, where the character char is given by ASCII


For example:

Glubitmapcharacter (glu_bitmap_times_roman_10, 'A') // obtain the 10-pound bitmap character a of times-Romans.

Glubitmapcharacter (glu_bitmap_8_by_13, 'A') // A of 8*13 characters


The position of a character is determined by the position of the grating. This position determines the position of the next bitmap in the lower left corner of the screen.

Void glrasterpos <234> <sidf> (X, Y, Z, W );

Void glrasterpos <234> <sidf> V (type * array );

After a bitmap is drawn, the grating will automatically have an offset and will not be drawn to the previous position.


Int glubitmapwidth (void * font, int char)

Returns the pixel width of the char character in the glut font.




OpenGL (1) 2D drawing: saving elements, polygon, color interpolation, text, query and error, and status

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.