Many materials in this article come from the Internet. After my organization and processing, I try to clarify the basic concepts required by a small part of the program in an article. The following is a reference list:
<OpenGL tutorial>
Http://old.blog.edu.cn/user1/20928/archives/2007/1863585.shtml
Http://blog.163.com/zhaoxin851055@126/blog/static/8112929820092116249704/
Http://blog.csdn.net/firefly_liu/archive/2009/05/08/4160296.aspx
Gluinitwindowposition (0, 0 );
Set the start position of the window. The default value is-1.
Gluinitwindowsize (300,300 );
Set the initial size in pixels. The default value is 300. This parameter must be greater than 0.
Note that the above two functions are recommended instead of forcing the system to create a window at the specified position and with the specified size. Therefore, we need a way to determine the real window position and size. The following section provides the answer:
The intent ofInitial window positionAndSizeValues is to provide a suggestion to the window system for a window 'sinitial size and position. the window system is not obligated to usethis information. therefore, Glut programs shocould not assume the specified wwas created at the specified size or position. A glut program shoulduse the window's reshape callback to determine the true size of thewindow.
Valley createwindow ("OpenGL 3D view ");
Create a top-level window. The parameter is the window name. This window will become the current window of the application. The displayed status of the created window is valid, but you still need to display it using the gludisplayfunc function.
Returns an integer greater than or equal to 1, representing the window.
Gludisplayfunc (Display );
Set the callback function of the drawing. glut automatically determines whether to call this function to re-draw the drawing. You can also force a notification to the glut re-painting through the Function Map.
Glclearcolor (0.0, 0.0, 0.0, 0.0); sets the clear color (that is, the background color)
The four parameters are actually float type. The value range is between 0 and 1, and the default value is 0. This function cannot be executed between glbegin and glend.
The fourth parameter is Alpha, which indicates transparency. 0 indicates transparency, and 1 indicates opacity.
Glmatrixmode (gl_projection );
Glortho (-5, 5,-5, 5, 5, 15 );
Glmatrixmode (gl_modelview );
Glulookat (0, 0, 10, 0, 0, 0, 0, 1, 0 );
These functions involve matrix, 3D to 2D transformation knowledge. First, let's introduce the background knowledge and then look at the usage of these functions.
In the real world, all objects are 3D. However, these 3D objects must be displayed in the form of 2D flat objects in the computer world. So how are these objects transformed from 3d to 2D? The following describes this concept using a camera (CAMERA) simulation, as shown in Figure 8-1.
In fact, from a three-dimensional space to a two-dimensional plane, just like taking a photo with a camera, we usually have to go through the following steps (the corresponding graphics concept is represented in parentheses ):
Step 1: place the camera on a tripod to align it with a 3D scene (view point transformation, viewing transformation ).
Step 2: place the 3D object in the appropriate position (model transformation, modeling transformation ).
Step 3: select the camera lens and focus on the camera, so that the three-dimensional object is projected on the two-dimensional film (projection transformation, projection transformation ).
Step 4: determine the size of a two-dimensional image (view transform, viewport transformation ).
In this way, an object in a three-dimensional space can be represented by a corresponding two-dimensional plane object, and it can be correctly displayed on a two-dimensional computer screen. Note that projection in is perspective projection.
Projection TransformationSimilar to camera focusing, including orthogonal projection and perspective projection, the difference is:
Orthogonal projection, the camera can be located in the cropping body, or outside. Therefore, near and far can take two positive values or one positive and one negative value. Use the glortho function.
Perspective Projection, the camera cannot be located in the cropping body, so both near and far must go to the positive value. Use the glfrustum function. Its Parameter Matrix defines the cropping and projection types, but does not define the camera orientation. The camera is located at the origin and faces the negative direction of the Z axis. The function glfrustum only modifies the camera lens (focal length), not the position. Glfrustum is very sensitive to the far and near parameters. Modifying near and far may greatly affect the vision, so it is often used to observe the close scene.
Objects in the real world all have three-dimensional coordinates, which are called the world coordinate system. The world coordinate system uses the screen center as the origin (0, 0, 0 ). When you face the screen, the right side is the X axis, and the Y axis is on the top. The screen points to you as the Z axis. The computer screen uses a two-dimensional coordinate system. Drawing objects in the world coordinate system to the screen coordinate system requires a series of steps.
Based on the current knowledge, let's look at the following code:
Glmatrixmode (gl_projection); // glmatrixmode specifies the matrix stack used for subsequent matrix operations.
Glortho (-5, 5,-5, 5, 5, 15 );
The first line specifies the subsequent operations of the matrix operation as projection ). On the second line, the camera focal length ranges from left to right and from bottom to bottom, and the nearest position is 5 units. The farthest position is 15 units.
Now let's take a look at another matrix operation-Model View)
Modelview is performed in the world coordinate system. In this coordinate system, you can translate gltranslatef (), rotate glrotatef (), and zoom in and out glscalef () to the observed target (). If the observed target is a cube, the y-axis ratio is 2.0, and the rest is 1.0, that is, the cube is changed to a rectangular cube.
The following code uses modelview
Glmatrixmode (gl_modelview );
Glulookat (0, 0, 10, 0, 0, 0, 0, 1, 0 );
Glulookat is an auxiliary function provided by the Glu database to set the visual coordinate system. In practical programming applications, after modeling a scenario, you often need to select a proper perspective or constantly change the perspective to observe the scenario.
Glulookat (
Camera X, camera y, camera Z,
Target point X, target point y, target point Z,
Camera top orientation X, camera top orientation y, camera top orientation Z
)
Therefore, glulookat (0, 0, 10, 0, 0, 0, 0, 1, 0) allows the camera to be located at a distance of 10 units in front of the 3D teapot. If you change 10 to-10, you can see from the front of the 3D teapot. Below are two:
The following function draws a red teapot. First, clear the color buffer with the clear color black (glclearcolor (0.0, 0.0, 0.0, 0.0) set earlier, and then specify the drawing color as red, draw a 3-dimensional teapot. Finally, force rendering engine plotting.
Void display (){
Glclear (gl_color_buffer_bit );
Glcolor3f (1.0, 0, 0 );
Gluwireteapot (3 );
Glflush ();
}
Note that the return of the glflush function does not indicate that the rendering engine has completed rendering (the screen drawing is complete ).
In short, there is a 3-dimensional red teapot (the teapot is located at the center of the coordinate axis) 10 units far ahead of me. I set the focal length of the camera, this allows you to see the scenes in the space covered by a cube in front of the cube: 5 to 5 from left to 5 from right, 5 to 5 from bottom to top, 5 to 15 in front of me. The teapot is in this range, so I can see it.
This chapter involves some theoretical knowledge of OpenGL, which is still relatively superficial. The next chapter of the plan begins to discuss the matrix.
You are welcome to give your comments, comments, and suggestions. I am also exploring this series of articles. If any errors occur, I can modify them at any time. Currently, we only plan to study OpenGL applications on Linux. We will test windows later.