[OpenGL Study Notes 02] draw a line at a wide point

Source: Internet
Author: User
We need to know that there are three basic drawing operations: Clear the window, draw geometric figures, and draw rasterized objects.
The raster object will be explained later.
1. Clear the window

For example, we can clear both the color buffer and the depth buffer.

Glclearcolor (0.0, 0.0, 0.0, 0.0); // specify the color buffer to clear as black glcleardepth (1.0 ); // specify the depth buffer clearing value as 1.0 glclear (gl_color_buffer_bit | gl_depth_buffer_bit); // specify the buffer to be cleared and clear

2. To draw a geometric image, you must first set the painting color, such

glColor3f(1.0,1.0,1.0);
Describes the vertex of a geometric object.
glVertex2s(2,3);glVertex3d(0.0,0.0,3.14);glVertex4f(2.3,1.0,-2.2,112.3);
It is best to force the painting to complete after the painting ends.
Void glflush (); // force the previous OpenGL commands to start execution, so that they can be completed within a limited time. Void glfinish (); // force the previous OpenGL command to complete the execution. This function will not be returned before the command execution is complete.
Point and line width

Void glpointsize (glfloat size); // sets the width of the rendered point, in pixel. The default value is 1.0 void gllinewidth (glfloat width). // sets the width in pixels, used for linear Rendering
Set the dot-draw mode of a straight line:

Factor is the repetition factor, and pattern is the binary number.(1 draw 0 do not draw) before use, you must call glable (gl_line_stipple)

void glLineStipple(GLint factor,GLushort pattern);

3. Wide-point line drawing example

# Include <Gl/glut. h> # include <stdlib. h> # define drawoneline (x1, Y1, X2, Y2) glbegin (gl_lines); glvertex2f (X1), (Y1); glvertex2f (X2 ), (Y2); glend (); void Init (void) {glclearcolor (0.0, 0.0, 0.0, 0.0); glshademodel (gl_flat); // uses the constant coloring mode, use the color of a vertex in the element to render the entire element .} Void display (void) {int I; glclear (gl_color_buffer_bit); glcolor3f (1.0, 1.0, 1.0); // white glable (gl_line_stipple ); // enable the linear dot painting function // effect 1 gllinestipple (1, 0x0101); // set the dot painting mode drawoneline (50.0, 125.0, 150.0, 125.0) of the current line ); gllinestipple (1, 0x00ff); // you can specify the drawoneline (150.0, 125.0, 250.0, 125.0,) of the current line. gllinestipple (1, 0x1c47 ); // set the dot-draw mode of the current line drawoneline (250.0, 125.0, 350.0, 125.0); // effect 2 gllinewidth (5.0); // set the line-width gllinestipple (1, 0x0101); drawoneline (50.0, 100.0, 150.0, 100.0); gllinestipple (1, 0x00ff); drawoneline (150.0, 100.0, 250.0, 100.0); gllinestipple (1, 0x1c47); drawoneline (250.0, 100.0, 350.0, 100.0); gllinewidth (1.0); // effect 3 gllinestipple (1, 0x1c47); glbegin (gl_line_strip ); for (I = 0; I <7; I ++) glvertex2f (50.0 + (glfloat) I * 50.0), 75.0); glend (); // effect 4for (I = 0; I <6; I ++) {drawoneline (50.0 + (glfloat) I * 50.0), 50.0, 50.0 + (glfloat) (I + 1) * 50.0), 50.0);} // effect 5 gllinestipple (5, 0x1c47); // The repetition factor is 5 drawoneline (50.0, 25.0, 350.0, 25.0); gldisable (gl_line_stipple); glflush ();} void reshape (int w, int h) {glviewport (0, 0, (glsizei) W, (glsizei) H ); // adjust the pixel matrix size of the drawing. glmatrixmode (gl_projection); // specify the current matrix as the projection matrix glloadidentity (); // set the current matrix to the unit matrix gluortho2d (0.0, (gldouble) W, 0.0, (gldouble) H);} void keyboard (unsigned char key, int X, int y) {Switch (key) {Case 27: exit (0); break;} int main (INT argc, char ** argv) {maid (& argc, argv ); fig (400,150); fig (100,100); fig (argv [0]); Init (); fig (Display); fig (reshape ); // call the function of moving the window or changing the window size to call the function of map keyboardfunc (keyboard), map loop (), return 0 ;}

4. Results

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.