opengl knowledge point 2

glViewport(0, 0, width, height); // Reset the current viewport The


next line enables smooth shading. Shadow smoothing Finely blends colors through polygons and smoothes external light.
glShadeModel(GL_SMOOTH); // Enable shadow smoothing The

next line sets the color to use when clearing the screen. If you're not sure how color works, I'll quickly explain. Color values ​​range from 0.0f to 1.0f. 0.0f is the darkest case and 1.0f is the brightest case. The first parameter after glClearColor is Red Intensity, the second is green, and the third is blue. The maximum value is also 1.0f, representing the brightest case for a particular color component. The last parameter is the alpha value. We don't care about the fourth number when it's used to clear the screen. Now let it be 0.0f.

glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // black background

The next three lines have to do with the depth buffer. Think of the depth buffer as a layer behind the screen. The depth buffer keeps track of how far objects go inside the screen. Our program in this section doesn't actually use the depth buffer, but almost all OpenGL programs that display 3D scenes on the screen do. Its ordering determines which object is drawn first. This way you don't draw a square behind a circle onto the circle. The depth buffer is a very important part of OpenGL.

glClearDepth(1.0f); // set depth buffer
glEnable(GL_DEPTH_TEST); // enable depth test
glDepthFunc(GL_LEQUAL); // Type of depth test done

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Tell system to correct perspective

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen and depth buffer
glLoadIdentity(); // Reset current model view matrix

We Use ChangeDisplaySettings(NULL,0) to return to the original desktop. Passing NULL as the first parameter and 0 as the second parameter forces Windows to use the values ​​currently stored in the registry (default resolution, color depth, refresh rate, etc.) to effectively restore our original desktop . After switching back to the desktop, we also need to make the mouse pointer visible again.


ChangeDisplaySettings(NULL,0); //If yes, switch back to the desktop
ShowCursor(TRUE); //Show the mouse pointer

glColor3f(r,g,b)//The three parameters in parentheses are red, green, blue in turn weight. The value can range from 0,0f to 1.0f

The first parameter of glTexCoord2f is the X coordinate. 0.0f is the left side of the texture. 0.5f is the midpoint of the texture and 1.0f is the right side of the texture. The second argument to glTexCoord2f is the Y coordinate. 0.0f is the bottom of the texture. 0.5f is the midpoint of the texture and 1.0f is the top of the texture.

//Lines can specify width
void glLineWidth(GLfloat width);

//Draw a dashed line
Use glEnable(GL_LINE_STIPPLE); to enable dashed line mode (use glDisable(GL_LINE_STIPPLE) to turn it off) and
use glLineStipple to set the style of the dashed line.
void glLineStipple(GLint factor, GLushort pattern);

//======================================== =
Shading Description Table
Device Description Table

//========================================
Model Transformation
The purpose of model transformation is to set the position and orientation of the model. For example, the model can be rotated, moved and scaled, or a combination of these operations.


In fact, the essence of commands such as rotation and translation transformation is to multiply the coordinates by a transformation matrix. The order in which the matrix multiplication occurs is exactly the opposite of the order in which the commands appear in the code (matrix multiplication is sequential).

These functions are the functions that come with the OpenGL graphics development kit.
glTranslatef(0.0f,-20.0f,-40.0f) means to translate the current graph to the x-axis by 0, to the y-axis by -20, and to the z-axis by -40
. glScaled(10.0f,10.0f,10.0f) means to translate the current graphics The graph is enlarged to 10 times the original along the x, y, and z axes respectively.
glRotatef(-80.0f, 10.0f, 1.0f, 0.0f) means to rotate the current graph 80 degrees clockwise along the direction vector (-10, 1, 0). .

glRotatef(GLfloat angle,GLfloat x,GLfloat y,GLfloat z)

glRotatef(45,1,0,0)
How does the object rotate? Imagine: from the coordinates (0, 0, 0) that is the origin, draw a line to (1, 0, 0), and hold the line with your right hand. At this time, you will ask, how to hold it? Hold with the right thumb pointing in the direction of (0, 0, 0) to (1, 0, 0). The bending direction of the other four fingers is the direction of the object's rotation.

1.glScalef() - Model transformation function scaling
void glScalef(GLfloat x, GLfloat y, GLfloat z);
this function indicates how the model is scaled on each axis. For example:
glScalef (1.0, 2.0, 1.0);//Indicates that the y coordinate value is doubled, so that the original square object becomes long.


2.glTranslatef() - Model transformation function to move
void glTranslatef(GLfloat x, GLfloat y, GLfloat z);
this function represents how the model is moved. For example:
glTranslatef(-1.0,0.0,-2.0);//Indicates that the object moves 1.0 in the negative x direction and 2.0 in the negative z direction. So it's as if you can see the side
// Note that in glTranslatef(x, y, z), when you move, you are not moving relative to the center of the screen, but relative to the current screen position. Its function is to translate the origin of your drawn point coordinates by a (x, y, z) vector based on the current origin

3. glRotatef() - model transformation function Rotate
void glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
angle represents the angle of rotation (note that the unit is not radians), and (x, y, z) represents the axis of rotation. For example:
glRotatef(45.0, 0.0, 0.0, 1.0);//Indicates that the model is rotated 45° along the (0,0,1) axis.
//Similar to glTranslatef(x, y, z), glRotatef(angle, x, y, z) also operates on the coordinate system.
The rotation axis passes through the origin, the direction is (x, y, z), the rotation angle is angle, and the direction satisfies the right-hand rule.


4.glFrustum() - Projection transformation function perspective projection

The first four parameters represent the clipping range, and the last two parameters (required to be positive numbers) represent the distance between the near and far surfaces from the eye. Kind of like focusing. After cropping, stretch to cover the entire screen.

5.glPerspective() - Projection transformation function perspective projection
void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);
The first parameter indicates the number of viewing angles in the y direction. Personal understanding: For example, 45, which represents the angle between the line of the eye and the center of the model and the y-axis.
The second parameter represents the aspect ratio. x/y. For example, 2 means that x and y were originally one length, and now x can be used as two lengths, so that the model seems to be compressed in the x direction.
The last two parameters of glFrustum() are similar. The requirements are all positive numbers.


6.glLoadIdentity() //Move the origin of the current user coordinate system to the center of the screen: similar to a reset operation

This space can be "orthoprojected" (using glOrtho or gluOrtho2D) or "perspectively projected" (using glFrustum or gluPerspective)

The matrix has its own "stack" for easy saving and restoration. This is helpful when drawing complex graphics. The functions used are glPushMatrix and glPopMatrix.















































Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324415844&siteId=291194637