OpenGL Getting Started Tutorial 3

Source: Internet
Author: User
Tags 0xc0 dashed line fread

The size of the point defaults to 1 pixels, but it can also be changed. The changed command is glpointsize, and its function prototype is as follows:
void Glpointsize (glfloat size);
Size must be greater than 0.0f and the default value is 1.0f, in pixels.
Note: For specific OpenGL implementations, there is a limit to the size of the point, and if you set a size that exceeds the maximum value, the setting may have
Problem.

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 the line
(1) The line can specify the width:
void Gllinewidth (glfloat width);
The usage is similar to glpointsize.
(2) Draw a dashed line.
First, use Glenable (gl_line_stipple) to start the dash mode (which can be closed using gldisable (Gl_line_stipple)).
Then, use Gllinestipple to set the style of the dashed line.
void Gllinestipple (glint factor, glushort pattern);
Pattern is a sequence of length 16 consisting of 1 and 0, starting from the lowest bit and, if 1, the next factor to be drawn on a straight line
A point is painted as true, and if 0, the factor points that should be drawn on the line will be painted as virtual.
Here are some examples:


Disclaimer: The picture is from www.opengl.org, which is the drawing of the book "OpenGL Programming Guide", due to the old version of the book (first edition,
1994) is already circulating in the network, I hope not to touch the copyright issue.
Example 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. About polygons
Polygon content is more, we will tell the following four aspects.
(1) Both sides of the polygon and how to draw.
Although we have not really used the three-dimensional coordinates to draw, but the establishment of some three-dimensional concept is still necessary.
From a three-dimensional point of view, a polygon has two faces. Each polygon can be drawn in a different way: fill, draw only the edge wheel
The contour, drawing only the vertices, where fill is the default way. You can set a different way for two faces, respectively.
Glpolygonmode (Gl_front, Gl_fill); Set the front for fill mode
Glpolygonmode (Gl_back, gl_line); Set the opposite side for the edge drawing method
Glpolygonmode (Gl_front_and_back, gl_point); Set both sides to be the vertex drawing mode
(2) Reversal
The general convention is that "vertices appear in a counterclockwise order on the screen" as "front" and the other face as "negative". A common object in life
Surface, usually can be used such "front" and "negative", "reasonable" is displayed (please find a more transparent mineral water bottle, in
Draw a circle counterclockwise on your side and mark the direction of the painting, then turn the back to the front, draw a similar circle, and experience the "positive
Face "and" reverse ". You will find your direction, the outside of the bottle is positive, and back to your direction, the inside of the bottle is the front side. is right for you.
The inside and the back of your back are the opposite side. This also belongs to the surface of the "outer side of the bottle", but some areas are positive and some
Side is the opposite).
But there are some surfaces that are more special. For example, "Mobius Belt" (please Google yourself), you can use the "front" or all use "back
Face "to indicate.
The concept of "front" and "reverse" can be exchanged through the Glfrontface function.
Glfrontface (GL_CCW); Set CCW direction to "front", CCW is counterclockwise, counterclockwise
Glfrontface (GL_CW); Set the CW direction to "front", CW is clockwise, clockwise
Here is an example program, replace the Mydisplay function in the first lesson with it and 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); Set the front to fill mode
Glpolygonmode (Gl_back, gl_line); Set the reverse side to linear mode
Glfrontface (GL_CCW); Set the counterclockwise direction to the front
Glbegin (Gl_polygon); Draw a square counterclockwise, at the bottom 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
GLVERTEX2F (0.0f, 0.0f);
GLVERTEX2F (0.0f, 0.5f);
GLVERTEX2F (0.5f, 0.5f);
GLVERTEX2F (0.5f, 0.0f);
Glend ();
Glflush ();
}
(3) Reject polygon surface
In three-dimensional space, a polygon has two faces, but we cannot see those polygons on the back, while some polygons are positive
, but is obscured by other polygons. If you treat invisible polygons as if they were visible polygons, you will no doubt reduce our processing graph
The efficiency of the shape. At such times, unnecessary faces can be removed.
First, use Glenable (Gl_cull_face) to start the culling function (use gldisable (gl_cull_face) to turn it off)
Then, use the Glcullface to reject.
The parameters of the glcullface can be gl_front,gl_back or gl_front_and_back, respectively, to reject the front, tick
Remove both sides of the polygon except the opposite side.
Note: The culling function only affects polygons, and there is no effect on points and lines. For example, using Glcullface (Gl_front_and_back)
, all the polygons are removed, so only the dots and lines are visible.
(4) Hollow polygon
Lines can be drawn as dashed lines, while polygons can be hollowed out.
First, use Glenable (gl_polygon_stipple) to start the cutout mode (using Gldisable (gl_polygon_stipple)
can be closed).
Then, use Glpolygonstipple to set the cutout style.
void Glpolygonstipple (const glubyte *mask);
The parameter mask points to a space of 128 bytes, which represents how a 32*32 rectangle should be hollowed out. Among them: the first
A byte indicates the leftmost left-to-right (or right-to-left, this can be modified) 8 pixels are hollow (1 means no cutouts,
The pixel is displayed, 0 is the cutout, the color behind it is displayed, and the last byte indicates whether the 8 pixels above the top right are hollowed out.
However, if we define this mask array directly, 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,//early
0x04, 0x18, 0x18, 0x20,//Start
0x04, 0x0C, 0x30, 0x20,//
0x04, 0x06, 0x60, 0x20,//,
0x44, 0x03, 0xC0, 0x22,//No
0x44, 0x01, 0x80, 0x22,//Building
0x44, 0x01, 0x80, 0x22,//discussion
0x44, 0x01, 0x80, 0x22,//Make
0x44, 0x01, 0x80, 0x22,//With
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 row.
};
Such a lot of data is very lack of intuition, we need to be very laborious to analyze, we will find that it is actually a fly.
It is obviously much easier to save such data as a picture and edit it with a specialized tool. Here's how to do this.
First, create a new picture with the brush program from Windows, named Mask.bmp, and note that when you save, you should select Monochrome bitmap.
In the Properties dialog box for image, set the height and width of the picture to 32.
Look at the picture with a magnifying glass and edit it. Black corresponds to binary 0 (skeleton), white corresponding to the binary one (not hollow), after the completion of the edit
Save.
You can then use the following code to get 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 reread sizeof (Mask) byte will encounter the end of the file
Note-(int) sizeof is not a good way of writing, but here it does work correctly.
If you write-sizeof (Mask) directly, because sizeof obtains an unsigned number, the minus sign will be problematic.
if (fseek (FP,-(int) sizeof (Mask), seek_end))
Exit (0);
Read sizeof (mask) bytes to mask
if (!fread (Mask, sizeof (mask), 1, FP))
Exit (0);
Fclose (FP);
OK, now please edit a picture as mask, and use the above method to get the mask array, after running the observation effect.
Description: You can set the factor factor when you draw a dashed line, but the polygon cutout cannot set the factor factor. Please use the mouse to change the size of the window,
Observe the change of the hollow 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 effect at the bottom left
Gldisable (gl_polygon_stipple);
GLRECTF (0.0f, 0.0f, 0.5f, 0.5f); Draw a square with no hollow effect on the top right
Glflush ();
}
Summary
This lesson learns some of the details of drawing geometry.
Click to set the size.
The line can be set to a width, and a line can be drawn dashed.
The two facets of a polygon can be plotted separately, and in three-dimensional space, invisible polygons can be removed;
The shape is drawn into a cutout style.
Knowing these details will make us more comfortable in some of the image drawing.
In addition, it is sometimes more convenient to write some data to a file outside of the program and edit it with specialized tools.

OpenGL Getting Started Tutorial 3

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.