OpenGL note 2.2 hollow out

Source: Internet
Author: User
Tags 0xc0 fread

In the second lesson, we learned how to draw Ry, but if you write a few more programs, you will find that there are still some depressing points. For example, the dot is too small to be clearly visible; the straight line is too thin and uncomfortable; or you want to draw a dotted line, but you do not know how to use a lot of short lines, or even a combination of dots.

These problems will be solved in this lesson.

The following describes vertices, straight lines, and polygon.

1. Points

The default vertex size is 1 pixel, but it can also be changed. The changed command is glPointSize. Its function prototype is as follows:

Void glPointSize (GLfloat size );

The size must be greater than 0.0f. The default value is 1.0f, and the unit is pixel ".

Note: for specific OpenGL implementations, the vertex size has a limit. If the set size exceeds the maximum value, the setting may be faulty.

Example:

Void myDisplay (void)

{

GlClear (GL_COLOR_BUFFER_BIT );

GlPointSize (5.0f );

GlBegin (GL_POINTS );

GlVertex2f (0.0f, 0.0f );

GlVertex2f (0.5f, 0.5f );

GlEnd ();

GlFlush ();

}

2. About straight lines

(1) the width of a line can be specified:

Void glLineWidth (GLfloat width );

Its usage is similar to glPointSize.

(2) draw a dotted line.

First, use GL_LINE_STIPPLE; to start the dotted line mode (glDisable (GL_LINE_STIPPLE ).

Then, use glLineStipple to set the dotted line style.

Void glLineStipple (GLint factor, GLushort pattern );

Pattern is a 16-length sequence composed of 1 and 0. Starting from the percentile, if it is 1, the factor points that should be drawn next on the straight line will be painted as solid; if it is 0, the factor points that should be drawn next in the straight line will be painted as virtual points.

Here are some examples:

Sample code:

Void myDisplay (void)

{

GlClear (GL_COLOR_BUFFER_BIT );

GlEnable (GL_LINE_STIPPLE );

GlLineStipple (2, 0x0F0F );

GlLineWidth (10.0f );

GlBegin (GL_LINES );

GlVertex2f (0.0f, 0.0f );

GlVertex2f (0.5f, 0.5f );

GlEnd ();

GlFlush ();

}

3. Polygon

Polygon has many contents. We will introduce the following four aspects.

(1) two sides of a polygon and the method of drawing the polygon.

Although we have not yet used 3D coordinates for drawing, it is necessary to establish some 3D concepts.

From a three-dimensional perspective, a polygon has two sides. You can set different painting methods for each surface: Fill, draw only edge contour lines, and draw only vertices. "fill" is the default method. You can set different methods for the two sides.

GlPolygonMode (GL_FRONT, GL_FILL); // you can specify the filling mode as the frontend.

GlPolygonMode (GL_BACK, GL_LINE); // you can specify the reverse side as the edges.

GlPolygonMode (GL_FRONT_AND_BACK, GL_POINT); // you can specify the vertex rendering mode for both sides.

(2) reverse

It is generally agreed that "the vertex appears on the screen in a counter-clockwise order" is "front", and the other side is "opposite ". The surface of common objects in life can usually be expressed with such "front" and "back" as "reasonable" (please find a more transparent mineral water bottle, draw a circle clockwise toward your side and indicate the direction of the painting. Then, convert the back to the front and draw a circle similar to that of the front and back ". You will find that it is in your direction, the outside of the bottle is the front, and the back is in your direction, the inside of the bottle is the front. It is opposite to your inner and back to your outer. In this way, it also belongs to the "outside of the bottle" surface, but some places are positive and some places are opposite ).

But there are some special surfaces. For example, you can use the "front" or "back" to represent all the "mbius Belt" (please Google it yourself.

The glFrontFace function can be used to exchange the concepts of "front" and "back.

GlFrontFace (GL_CCW); // sets the CCW direction to "front", and the CCW is CounterClockWise, which is counter-clockwise.

GlFrontFace (GL_CW); // Set The CW direction to "front", CW is ClockWise, ClockWise

The following is an example program. Use it to replace the myDisplay function in lesson 1, change glFrontFace (GL_CCW) to glFrontFace (GL_CW), and observe the changes in the results.

Void myDisplay (void)

{

GlClear (GL_COLOR_BUFFER_BIT );

GlPolygonMode (GL_FRONT, GL_FILL); // you can specify filling mode for the front.

GlPolygonMode (GL_BACK, GL_LINE); // Set the opposite side to the linear mode.

GlFrontFace (GL_CCW); // you can specify a counter-clockwise direction to the front.

GlBegin (GL_POLYGON); // draw a square counter-clockwise, in the lower left

GlVertex2f (-0.5f,-0.5f );

GlVertex2f (0.0f,-0.5f );

GlVertex2f (0.0f, 0.0f );

GlVertex2f (-0.5f, 0.0f );

GlEnd ();

GlBegin (GL_POLYGON); // draw a square clockwise in the upper right corner

GlVertex2f (0.0f, 0.0f );

GlVertex2f (0.0f, 0.5f );

GlVertex2f (0.5f, 0.5f );

GlVertex2f (0.5f, 0.0f );

GlEnd ();

GlFlush ();

}

(3) remove the Polygon surface

In 3D space, although a polygon has two sides, we cannot see those polygon on the back. While some polygon are positive, they are blocked by other polygon. If you treat invisible polygon as visible polygon, it will undoubtedly reduce the processing efficiency of the image. In this case, unnecessary surfaces can be removed.

First, use GL_CULL_FACE; to enable the removal function (glDisable (GL_CULL_FACE)

Then, use glCullFace for removal.

The glCullFace parameter can be GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK, indicating that the front, back, and front sides of the polygon are removed respectively.

Note: the remove function only affects polygon, but does not affect vertices and straight lines. For example, after glCullFace (GL_FRONT_AND_BACK) is used, all polygon will be removed, so only vertices and straight lines are seen.

(4) hollow polygon

A straight line can be painted as a dotted line, while a polygon can be hollowed out.

First, use GL_POLYGON_STIPPLE; to start the hollow-out mode (glDisable (GL_POLYGON_STIPPLE ).

Then, use glPolygonStipple to set the hollow-out style.

Void glPolygonStipple (const GLubyte * mask );

The parameter mask points to a 128-byte space, which indicates how to empty a 32*32 rectangle. Where: the first byte indicates the left-to-right (or right-to-left, which can be changed) whether eight pixels are hollowed out (1 indicates not hollowed out, show this pixel; 0 indicates hollow out, and the color behind it is displayed). The last byte indicates whether the eight pixels at the top right are hollow out.

However, if we directly define this mask array, like this:

Static GLubyte Mask [128] =

{

0x00, 0x00, 0x00, 0x00, // This is the bottom line

0x00, 0x00, 0x00, 0x00,

0x03, 0x80, 0x01, 0xC0, // hemp

0x06, 0xC0, 0x03, 0x60, // annoying

0x04, 0x60, 0x06, 0x20, //

0x04, 0x30, 0x0C, 0x20, // initial

0x04, 0x18, 0x18, 0x20, // start

0x04, 0x0C, 0x30, 0x20, //

0x04, 0x06, 0x60, 0x20 ,//,

0x44, 0x03, 0xC0, 0x22, // No

0x44, 0x01, 0x80, 0x22, // Create

0x44, 0x01, 0x80, 0x22, // discussion

0x44, 0x01, 0x80, 0x22, //

0x44, 0x01, 0x80, 0x22, // use

0x44, 0x01, 0x80, 0x22,

0x44, 0x01, 0x80, 0x22,

0x66, 0x01, 0x80, 0x66,

0x33, 0x01, 0x80, 0xCC,

0x19, 0x81, 0x81, 0x98,

0x0C, 0xC1, 0x83, 0x30,

0x07, 0xE1, 0x87, 0xE0,

0x03, 0x3F, 0xFC, 0xC0,

0x03, 0x31, 0x8C, 0xC0,

0x03, 0x3F, 0xFC, 0xC0,

0x06, 0x64, 0x26, 0x60,

0x0C, 0xCC, 0x33, 0x30,

0x18, 0xCC, 0x33, 0x18,

0x10, 0xC4, 0x23, 0x08,

0x10, 0x63, 0xC6, 0x08,

0x10, 0x30, 0x0C, 0x08,

0x10, 0x18, 0x18, 0x08,

0x10, 0x00, 0x00, 0x08 // This is the top line

};

Such a pile of data is very unintuitive. We need to analyze it very hard to find that it represents a fly.

It is much easier to save the data as an image and edit it with a dedicated tool. The following describes how to do this.

First, create a new image using the image recording program provided by Windows and name it mask.bmp. When saving the image, select "monochrome bitmap ". In the "image"-> "properties" dialog box, set the image height and width to 32.

Use a magnifier to observe and edit the image. Black corresponds to binary zero (hollow out), white corresponds to binary one (not hollow out), and is saved after editing.

Then, you can use the following code to obtain the Mask array.

Static GLubyte Mask [128];

FILE * fp;

Fp = fopen ("mask.bmp", "rb ");

If (! Fp)

Exit (0 );

// Move the file pointer to this position so that the end of the file will occur when the sizeof (Mask) byte is read again.

// Note-(int) sizeof (Mask) is not a good method, but it is correct and valid here.

// If you write-sizeof (Mask) directly, because sizeof gets an unsigned number, it may cause a negative number.

If (fseek (fp,-(int) sizeof (Mask), SEEK_END ))

Exit (0 );

// Read the sizeof (Mask) bytes to the Mask

If (! Fread (Mask, sizeof (Mask), 1, fp ))

Exit (0 );

Fclose (fp );

Now, edit an image as a mask and use the above method to obtain the Mask array. After running the image, observe the effect.

Note: The factor can be set when the dotted line is drawn, but the factor cannot be set when the polygon is hollow out. Use the mouse to change the window size and observe the changes in the hollow-out effect.

# Include <stdio. h>

# Include <stdlib. h>

Void myDisplay (void)

{

Static GLubyte Mask [128];

FILE * fp;

Fp = fopen ("mask.bmp", "rb ");

If (! Fp)

Exit (0 );

If (fseek (fp,-(int) sizeof (Mask), SEEK_END ))

Exit (0 );

If (! Fread (Mask, sizeof (Mask), 1, fp ))

Exit (0 );

Fclose (fp );

GlClear (GL_COLOR_BUFFER_BIT );

GlEnable (GL_POLYGON_STIPPLE );

GlPolygonStipple (Mask );

GlRectf (-0.5f,-0.5f, 0.0f, 0.0f); // draw a square with a hollow-out effect in the lower left corner.

GlDisable (GL_POLYGON_STIPPLE );

GlRectf (0.0f, 0.0f, 0.5f, 0.5f); // draw a square with no hollow-out effect in the upper right corner.

GlFlush ();

}

Summary

This lesson describes how to draw geometric figures.

Point to set the size.

You can set the width of a straight line. You can draw a straight line as a dotted line.

You can set the two sides of a polygon. In a 3D space, invisible polygon can be removed. You can draw a filled polygon into a hollow style.

Understanding these details will make us more comfortable in some image rendering.

In addition, writing some data to files outside the program and editing it with special tools can sometimes be more convenient.

=================================== Third lesson completion ====================== =====

================================= To be continued ======================== =====

OpenGL note 2.2 hollow out

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.