OpenGL study NOTE 4: color, size, dotted line, polygon invert, hollow out, use bitmap, opengl Study Notes

Source: Internet
Author: User

OpenGL study NOTE 4: color, size, dotted line, polygon invert, hollow out, use bitmap, opengl Study Notes

Reprinted, Please retain the source, hushuai1992http: // blog.csdn.net/u013642494/article/category/2675731

Well, I don't know how to start this title. If there is no title, please don't care about these details .....


Let's look at the points and lines we drew last time. We seem to have forgotten how to set the point size (OH, NO, I seem to have mentioned it later ....), Now let's take a look


1. Set the vertex size and line width.

Void glPointSize (GLfloat size); // sets the vertex size. The default value is a pixel.

Void glLineWidth (GLfloat width); // you can specify the line width.


Note: These two functions must be used before glBegin (). They are invalid after glBegin ().

You must enable the reverse sample (GL_LINE_SMOOTH);) before using decimal places.

Look at the Code:

void display(){glClear( GL_COLOR_BUFFER_BIT);glPointSize( 20.0f);glBegin( GL_POINTS);glVertex2f( 0.0f, 0.0f);glEnd();glLineWidth( 5.0f);glBegin( GL_LINES);glVertex2f( -0.5f, -0.5f);glVertex2f( 1.0f, -0.5f);glEnd();glFinish();}



2. Set the color

I guess all my friends have guessed it.

Void glColor3f (GLfloat red, GLfloat green, GLfloat blue); // set the color

Void glColor4f (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha); // sets the color with transparent channels.


However, the parameter here is not [0,255], but [0, 1]. The maximum value represented by floating point values such as linear ing is mapped to 1.0 (full intensity), and zero is mapped to 0.0 (zero intensity ).


Void display () {glClear (GL_COLOR_BUFFER_BIT); glable ); // The glColor3f (0.0f, 1.0f, 0.0f), glPointSize (20366f), glBegin (GL_POINTS), and glVertex2f (0.0f, 0.0f) based on the alpha Channel value of the source pixel ); glEnd (); glColor4f (1.0f, 0.0f, 0.0f, 0.5f); glLineWidth (5.0f); glBegin (GL_LINES); glVertex2f (-0.5f,-0.5f); glVertex2f (0.5f, 0.5f); glEnd (); glFinish ();}


If you need to use a transparent channel, you must enable the ALPHA mixer and specify the blending method between the source and target. Read the code

Glable (GL_BLEND); // enable the hybrid glDisable (GL_DEPTH_TEST); // disable the glBlendFunc (GL_SRC_ALPHA, GL_ONE) for the deep test; // a semi-transparent hybrid function based on the alpha Channel value of the source Pixel

Void glable (GLenum cap); // used to enable various functions

Of course, the corresponding function glDisable () must be disabled ()

Let's take a look at the following parameters.

Type Value Description
GL_ALPHA_TEST 4864 Determines whether the transparent layer level of the image is displayed based on the condition requirements of the glAlphaFunc function. For details, see glAlphaFunc.
GL_AUTO_NORMAL 3456 After the image is executed, it can reflect the light in all directions.
GL_BLEND 3042 Enable color mixing. For example, achieve translucent Effect
GL_CLIP_PLANE0 ~ GL_CLIP_PLANE5 12288 ~ 12283 Enable the graphic cutting pipe as required by the glClipPlane function. Six cache Pipelines
GL_COLOR_LOGIC_OP 3058 Enable logical operation of color of each pixel
GL_COLOR_MATERIAL 2930 After the image is executed, the image (material) is reflected based on the light. Reflection must be set by the glColorMaterial function.
GL_CULL_FACE 2884 Enable the surface for hiding the graphic material as required by the glCullFace function.
GL_DEPTH_TEST 2929 Enable deep test. Automatically hide the hidden image (material) based on the distance of coordinates)
GL_DITHER 3024 Enable Jitter
GL_FOG 2912 Atomization effect, for example, the farther the distance is, the blurrier it.
GL_INDEX_LOGIC_OP 3057 Logical operation
GL_LIGHT0 ~ GL_LIGHT7 16384 ~ 16391 The function glLight function is required to enable the light source from lamp 0 to lamp 7.
GL_LIGHTING 2896 Enable Light Source
GL_LINE_SMOOTH 2848 After the code is executed, the line segment's sawtooth is ignored.
GL_LINE_STIPPLE 2852 Draw a dotted line
GL_LOGIC_OP 3057 Logical operation
GL_MAP1_COLOR_4 3472 Enable glEvalCoord1, glEvalMesh1, and glEvalPoint1 to generate RGBA curves based on the besale curve settings of the Map1 function.
GL_MAP1_INDEX 3473 Enable glEvalCoord1, glEvalMesh1, and glEvalPoint1 to generate color index curves based on the besale curve settings of the Map1 function.
GL_MAP1_NORMAL 3474 Enable glEvalCoord1, glEvalMesh1, and glEvalPoint1 to generate the normal line based on the besale curve settings of the Map1 function.
GL_MAP1_TEXTURE_COORD_1 3475 Enable glEvalCoord1, glEvalMesh1, and glEvalPoint1 to generate the coordinates of arts and sciences based on the besale curve settings of the Map1 function.
GL_MAP1_TEXTURE_COORD_2 3476 Enable glEvalCoord1, glEvalMesh1, and glEvalPoint1 to generate the coordinates of arts and sciences based on the besale curve settings of the Map1 function.
GL_MAP1_TEXTURE_COORD_3 3477 Enable glEvalCoord1, glEvalMesh1, and glEvalPoint1 to generate the coordinates of arts and sciences based on the besale curve settings of the Map1 function.
GL_MAP1_TEXTURE_COORD_4 3478 Enable glEvalCoord1, glEvalMesh1, and glEvalPoint1 to generate the coordinates of arts and sciences based on the besale curve settings of the Map1 function.
GL_MAP1_VERTEX_3 3479 Enable glEvalCoord1, glEvalMesh1, and glEvalPoint1 to generate a curve in three-dimensional space based on the besale curve settings of the Map1 function.
GL_MAP1_VERTEX_4 3480 Enable glEvalCoord1, glEvalMesh1, and glEvalPoint1 to generate a normal in a four-dimensional space based on the besale curve settings of the Map1 function.
GL_MAP2_COLOR_4 3504 Enable glEvalCoord2, glEvalMesh2, and glEvalPoint2 to generate RGBA curves based on the besale curve settings of the function Map2.
GL_MAP2_INDEX 3505 Enable glEvalCoord2, glEvalMesh2, and glEvalPoint2 to generate color indexes based on the besale curve settings of the Map2 function.
GL_MAP2_NORMAL 3506 Enable glEvalCoord2, glEvalMesh2, and glEvalPoint2 to generate the normal line based on the besale curve settings of the Map2 function.
GL_MAP2_TEXTURE_COORD_1 3507 Enable glEvalCoord2, glEvalMesh2, and glEvalPoint2 to generate texture coordinates based on the besale curve settings of the Map2 function.
GL_MAP2_TEXTURE_COORD_2 3508 Enable glEvalCoord2, glEvalMesh2, and glEvalPoint2 to generate texture coordinates based on the besale curve settings of the Map2 function.
GL_MAP2_TEXTURE_COORD_3 3509 Enable glEvalCoord2, glEvalMesh2, and glEvalPoint2 to generate texture coordinates based on the besale curve settings of the Map2 function.
GL_MAP2_TEXTURE_COORD_4 3510 Enable glEvalCoord2, glEvalMesh2, and glEvalPoint2 to generate texture coordinates based on the besale curve settings of the Map2 function.
GL_MAP2_VERTEX_3 3511 Enable glEvalCoord2, glEvalMesh2, and glEvalPoint2 to generate curves in 3D space based on the besale curve settings of the Map2 function.
GL_MAP2_VERTEX_4 3512 Enable glEvalCoord2, glEvalMesh2, and glEvalPoint2 to generate curves in 3D space based on the besale curve settings of the Map2 function.
GL_NORMALIZE 2977 Enable normal vectors based on the setting conditions of the function glNormal
GL_POINT_SMOOTH 2832 After the command is executed, the line point's sawtooth is ignored.
GL_POLYGON_OFFSET_FILL 32823 Enable depth offset based on the glPolygonOffset setting of the function.
GL_POLYGON_OFFSET_LINE 10754 Enable line depth offset based on the glPolygonOffset setting of the function.
GL_POLYGON_OFFSET_POINT 10753 Enable the point depth offset based on the glPolygonOffset setting of the function.
GL_POLYGON_SMOOTH 2881 Excessive image (polygon) sawtooth
GL_POLYGON_STIPPLE 2882 After execution, the polygon is a vector drawing.
GL_SCISSOR_TEST 3089 Enable image cutting based on the glScissor Function
GL_STENCIL_TEST 2960 Enable template testing and update template cache. SeeGlStencilFuncAndGlStencilOp.
GL_TEXTURE_1D 3552 Enable one-dimensional Arts and Sciences
GL_TEXTURE_2D 3553 Enable 2D Arts and Sciences
GL_TEXTURE_GEN_Q 3171 Enable texture Processing Based on the function glTexGen
GL_TEXTURE_GEN_R 3170 Enable texture Processing Based on the function glTexGen
GL_TEXTURE_GEN_S 3168 Enable texture Processing Based on the function glTexGen
GL_TEXTURE_GEN_T 3169 Enable texture Processing Based on the function glTexGen

These things are frequently used ........

Void glBlendFunc (GLenum sfactor, GLenum dfactor); // defines the pixel algorithm.



3. The front and back sides of a polygon and the drawing method

Well, let's say it's swollen. Let's take a look at the code first. It's very empty to directly talk about it...

void display(){glClear( GL_COLOR_BUFFER_BIT);glPolygonMode( GL_FRONT, GL_FILL);glPolygonMode( GL_BACK, GL_LINE);glFrontFace( GL_CCW);glBegin( GL_POLYGON);glVertex2f( -0.5f, -0.5f);glVertex2f( 0.0f, -0.5f);glVertex2f( 0.0f, 0.0f);glVertex2f( -0.5f, 0.0f);glEnd();glBegin( GL_POLYGON);glVertex2f( 0.0f, 0.0f);glVertex2f( 0.0f, 0.5f);glVertex2f( 0.5f, 0.5f);glVertex2f( 0.5f, 0.0f);glEnd();glFinish();}


Let's take a look,

Void glPolygonMode (GLenum face, GLenum mode );

Controls the display mode of polygon.

Parameter 1: determines which part of the display mode will apply to the object and controls the drawing mode on the front and back of the polygon.

GL_FRONT indicates that the display mode will apply to the forward surface of an object (that is, the surface that the object can see) GL_BACK indicates that the display mode will apply to the backward surface of an object (that is, the surface invisible to the object) GL_FRONT_AND_BACK indicates that the display mode will apply to all faces of an object. Generally, we use the vertex to appear on the screen in a counterclockwise order "as the front", and the other is the opposite"

Parameter 2: determine how the selected object surface is displayed (Display Mode)

GL_LINE indicates the display line segment, GL_FILL indicates the display surface with the contour displayed on the polygon, and fill the polygon with the fill form.
See the two squares above
Of course, there is also a GL_POINT that only displays the specified point (it is estimated that it is useless .....)


VoidGlFrontFace(GLenumMode);

Well, this function is used to determine how to set the front and back sides of a square,

GL_CCW indicates that the vertex sequence of the projection Polygon on the window coordinate is clockwise and the surface is positive. GL_CW indicates that the face in clockwise direction of the vertex is positive. The default value is GL_CCW. Let's take a look at the effect of GL_CW.

Right, let's reverse it. Oh, right, these functions should also be written before glBegin ....


4. Remove the polygon surface

Most of the time, our stuff will have the effect of front and back occlusion. If we still draw all of them, it would be a waste of resources, so we can remove the blocked polygon, this can greatly improve the running efficiency of our programs ,,,

Do you still remember what we just said about glEnable ()? Yes, we have to use it to enable the removal function first, and then use glCullFace () to remove it (the parameter is both front and back, all ),,,

Removing a polygon only affects vertices and straight lines.

Start code

glEnable(GL_CULL_FACE);glCullFace( GL_FRONT_AND_BACK);


Dotted Line and polygon

Well, the old rule is that the dotted line is enabled for glEnable (), and glLineStipple () sets the dotted line display mode.

void display( void){glClear( GL_COLOR_BUFFER_BIT);glEnable( GL_LINE_STIPPLE);glLineStipple( 2.0f, 0x0F0F);glLineWidth( 5.0f);glBegin( GL_LINES);glVertex2f( -0.5f, -0.5f);glVertex2f( 0.5f, 0.5f);glEnd();glFinish();}

Void glLineStipple (GLint factor, GLushort pattern );
In OpenGL, set the current dot-draw mode of the straight line. The pattern parameter is a 16-bit sequence consisting of 1 or 0. It repeats as needed and points a specific line. Starting from the low position of this mode, one pixel and one pixel are processed. If the corresponding bit in the mode is 1, this pixel is drawn; otherwise, this pixel is not drawn. The mode can be extended using the factor parameter (indicating the repeated factor), which is multiplied by the continuous subsequences of 1 and 0. Therefore, if three 1 s appear in the mode and the factor is 2, they are extended to six consecutive 1 s. You must use GL_LINE_STIPPLE as the parameter to call glable () to enable the linear dot painting function. To disable the linear dot painting function, you can pass the same parameter to the glDisable () function.

Let's look at the 0x0F0F (0000111100001111) We just wrote. If we wear 1 as the first parameter, we will draw from low to high (from right to left). 1 indicates drawing, 0 indicates no painting, and the drawing result is [draw four first, then empty four, then draw four, and then empty four] If we wear 2 parameters, it is [Draw eight first, eight more blank, eight more blank, and so on. View chart



Let's talk about the hollow out of polygon.

You can also enable the hollow-out mode by using glEnable () and glPolygonStipple (). First read the code and then explain it.

This array is applied to the redbooks. If I want to write it myself, I guess I will go crazy.

GLubyte data[128] = {0x00, 0x00, 0x00, 0x00,   0x00, 0x00, 0x00, 0x00,  0x03, 0x80, 0x01, 0xC0,  0x06, 0xC0, 0x03, 0x60,  0x04, 0x60, 0x06, 0x20,   0x04, 0x30, 0x0C, 0x20,  0x04, 0x18, 0x18, 0x20,   0x04, 0x0C, 0x30, 0x20,  0x04, 0x06, 0x60, 0x20,  0x44, 0x03, 0xC0, 0x22,  0x44, 0x01, 0x80, 0x22,   0x44, 0x01, 0x80, 0x22,  0x44, 0x01, 0x80, 0x22,   0x44, 0x01, 0x80, 0x22,  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, 0x33, 0xcc, 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,  };  void display( void){glClear( GL_COLOR_BUFFER_BIT);glEnable( GL_POLYGON_STIPPLE);glPolygonStipple( data);glRectf( -0.5f, -0.5f, 0.0f, 0.0f);glFinish();}


Haha, you may find it amazing. We actually drew a bunch of flies on the screen. Our array is actually a fly image. The array contains 128 bytes, indicating a 32x32 matrix hollow-out data. The first byte in the array indicates whether the 8 pixels from left to right in the lower left are hollowed out...

This graph can be used to represent


Let's take a look at the effect of no hollows

It's like this .....



If we ask our program to make such an array every time, I guess the programmer will go crazy. Let's look at the simple method. First, open your canvas tool (the one that comes with your computer) save the bmp of a monochrome bitmap.




Now, let's use this bitmap.

void display( void){GLubyte data[128];FILE* fp;fp = fopen( "openGLTest.bmp", "rb");if ( !fp){exit( 0);}if ( fseek( fp, -(int)sizeof( data), SEEK_END)){exit( 0);}if ( !fread( data, sizeof( data), 1, fp)){exit( 0);}fclose( fp);glClear( GL_COLOR_BUFFER_BIT);glEnable( GL_POLYGON_STIPPLE);glPolygonStipple( data);glRectf( -0.5f, -0.5f, 0.0f, 0.0f);glFinish();}

Isn't the picture we drew loaded? Well, it's worth noting that the image must be placed with the execution file. After all, people are good friends, today we are here...







Accumulate a little bit every day, and one day you will become a great god

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.