Lesson 3rd: OpenGL-line

Source: Internet
Author: User
Let's look at a basic program first:
(I believe this is not difficult to achieve with the foundation of the first two courses) # include <windows. h>
# Include <Gl/glut. h>

Void mydisplay (void)
{
Glclear (gl_color_buffer_bit );
Glenable (gl_line_stipple); // start the dotted line mode (gldisable (gl_line_stipple) can be disabled)
Gllinestipple (2, 0x0f0f );
Gllinewidth (10.0f );
Glbegin (gl_lines );
Glvertex2f (0.0f, 0.0f );
Glvertex2f (0.5f, 0.5f );
Glend ();
Glflush ();
}

Int main (INT argc, char * argv [])
{
Gluinit (& argc, argv );
Fig );
Gluinitwindowposition (100,100 );
Gluinitwindowsize (400,400 );
Valley createwindow ("dotted line ");
Gludisplayfunc (& mydisplay );
Glumainloop ();
Return 0;
}

As follows:

For example, replace gllinestipple (2, 0x0f0f) in the program with gllinestipple (2, 0 xaaaa), the dotted line width gllinewidth (10.0f), and the line width gllinewidth (2.0f); 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 (from the Internet ):

Some necessary instructions:

1. Void glable (gl_line_stipple); it is used to start the dotted line mode. In the future, gldisable (gl_line_stipple) will be used frequently, you can find a lot through the http://www.google.cn.

2. Void gllinestipple (glint factor, glushort pattern); this function sets the dashes of the current vertex. Factor indicates the number of consecutive draw lines, ranging from 1 ~ 255, pattern is a hexadecimal number composed of 0 and 1. When the bit value is 1, draw a straight line. If it is 0, do not draw a straight line. For example: the hexadecimal value of 0000111100001111 is 0x0f0f, indicating that a short segment is drawn, that is, the broken line.

3. Void gllinewidth (glfloat width); indicates the function of the line width.

For the dotted line mode, an intuitive program is provided:

# Include <windows. h>
# Include <Gl/glut. h>
# Include <stdlib. h>

Void drawaline (glfloat X1, glfloat Y1, glfloat X2, glfloat Y2)
{
Glbegin (gl_lines );
Glvertex2f (X1), (Y1 ));
Glvertex2f (X2), (Y2 ));
Glend ();
}

Void Init (void)
{
Glclearcolor (0.0, 0.0, 0.0, 0.0); // clear the screen color to white
Glshademodel (gl_flat );
}

Void display (void)
{
Int I;
Glclear (gl_color_buffer_bit );
Glcolor3f (1.0, 1.0, 1.0 );

// Draw the first line
Glenable (gl_line_stipple );
Gllinewidth (3.0 );
Gllinestipple (0101 X );
Drawaline (50.0, 125.0, 150.0, 125.0); // call the drawaline () function
Gllinestipple (1, 0x00ff );
Drawaline (150.0, 125.0, 250.0, 125.0 );
Gllinestipple (1, 0x1c47 );
Drawaline (250.0, 125.0, 350.0, 125.0 );

// Draw the second line
Gllinewidth (6.0 );
Gllinestipple (0101 X );
Drawaline (50.0, 100.0, 150.0, 100.0 );
Gllinestipple (1, 0x00ff );
Drawaline (150.0, 100.0, 250.0, 100.0 );
Gllinestipple (1, 0x1c47 );
Drawaline (250.0, 100.0, 350.0, 100.0 );

// Draw the third line
Gllinewidth (3.0 );
Gllinestipple (1, 0x1c47 );
Glbegin (gl_line_strip );
For (I = 0; I <7; I ++)
Glvertex2f (50.0 + (glfloat) I * 50.0), 75.0 );
Glend ();

// Draw the fourth line
For (I = 0; I <6; I ++)
{
Drawaline (50.0 + (glfloat) I * 50.0), 50.0, 50.0 + (glfloat) (I + 1) * 50.0), 50.0 );
}

// Draw the fifth line
Gllinestipple (5, 0x1c47 );
Drawaline (50.0, 25.0, 350.0, 25.0 );
Glflush ();
}

Void reshape (int w, int H)
{
Glviewport (0, 0, (glsizei) W, (glsizei) H );
Glmatrixmode (gl_projection );
Glloadidentity ();
Gluortho2d (0.0, (gldouble) W, 0.0, (gldouble) H );
}

Int main (INT argc, char * argv [])
{
Gluinit (& argc, argv );
Fig );
Gluinitwindowposition (100,100 );
Gluinitwindowsize (400,400 );
Glucreatewindow ("OpenGL stipple ");
Init ();
Glureshapefunc (& Reshape );
Gludisplayfunc (& Display );
Glumainloop ();
Return 0;
}

Running effect:

The above procedures are described as follows:

1. Void drawaline (glfloat X1, glfloat Y1, glfloat X2, glfloat Y2) is a function compiled by the user to implement the draw line function: draw a line with (x1, Y1) and (X2, y2) is the line segment of the endpoint. Because the void display (void) function must call this function repeatedly.

2. For functions:
Void reshape (int w, int H)
{
Glviewport (0, 0, (glsizei) W, (glsizei) H );
Glmatrixmode (gl_projection );
Glloadidentity ();
Gluortho2d (0.0, (gldouble) W, 0.0, (gldouble) H );
}

As follows:
(1) the prototype of the function is void reshape (glsizei W, glsizei H), where W represents the width of the viewport (the viewport: Is a rectangular area drawn on the screen, h indicates the height of the view. By coordinating W and H, you can change the size of the view. Of course, you can also keep the value unchanged. You can restore the size of the view, especially when called.
(2) In the main function main (), the glureshapefunc (& Reshape); Calls void reshape (int w, int h) to change the window size at any time, can make the square look still a square, and when the window is reduced, the square will also become smaller.
(3) glviewport (, (glsizei) W, (glsizei) H); the function prototype is:
Void glviewport (glint X, glint y, glsizei width, glsizei height );
(X, y) defines the coordinates in the lower left corner of the view. width and height define the width and height of the view. By default, the initial values of the view area are (0, 0, winwidth, winheight ), here, winwidth and winheight are respectively the width and height of the window.
(4) The function prototype of glmatrixmode (gl_projection); is:
Void glmatrixmode (glenum mode );
This function can be used to implement the desired matrix mode. moded can be set to gl_modeview, gl_projection, and gl_texture, respectively specifying the currently manipulated matrix model visual matrix, projection matrix, and texture matrix, subsequent conversion commands only affect the specified matrix.
(5) glloadidentity (); indicates clearing the current matrix as the unit matrix. If matrix conversion is not performed, the current matrix is the unit matrix. Glloadidentity () is called after matrix transformation in each step. It is because the next matrix conversion is multiplied by the current matrix. If glloadidentity () is not called (); multiplied by the last transformed matrix (not the unit matrix), it deviates from the user's wishes, and the expected results are not met.
(6) gluortho2d (0.0, (gldouble) W, 0.0, (gldouble) H); function prototype:
Void gluortho2d (gldouble left, gldouble right, gldouble bottom, gldouble top, gldouble near, gldouble far );
This function creates a parallel visual object. (Left, bottom, near) and (right, top, near) define the coordinates of the lower left corner and the upper right corner, (left, bottom, FAR) and (right, top, far) defines the coordinates of the lower left corner and the upper right corner of the plane. For example, the visual projection defined by gluortho2d () is intuitively shown in (from the network:

 

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.