Projection in OpenGL
In OpenGL, the projection matrix specifies the size and shape of the visible area. For normal projection and Perspective Projection, they have their respective purposes.
Forward projection
It is applicable to 2D graphics, such as text and architectural drawing. In its application, we hope to display accurate object sizes and measurements on the screen.
Perspective Projection
It uses the perspective division to shorten and contract objects that are far away from the observer. Because the Width Measurement Methods of the front-end and back-end of the visible area are different, the two objects with the same logical size are located at the front and back of the visible area, the former seems bigger than the latter.
The Perspective Projection defined by frustum is displayed, and its observation direction is from narrow to wide. The tool function gluperspective allows you to easily define an intercept:
Void gluperspective (gldouble fovy, gldouble aspect, gldouble near, gldouble far );
Fovy indicates the visual angle in the vertical direction, aspect indicates the aspect ratio of width to height, and near and far indicate the distance between the near and remote cropping planes.
The following code uses perspective projection to demonstrate a motion system consisting of the Sun (yellow), Earth (red), and Moon (Gray. This is a typical example of nested transformation. We use the matrix stack to transform an object based on another object. The execution result shows:
/* Program list 4-3
*
*/
# Include <glut. h>
# Include <math. h>
// Rotating step value
Static float fmoonrot = 0.0f;
Static float fearthrot = 0.0f;
// Set the rendering status
Void setuprc ()
{
// Set the clear form color (black background)
Glclearcolor (0.0f, 0.0f, 0.0f, 1.0f );
// Set the drawing color to green
Glcolor3f (0.0f, 1.0f, 0.0f );
// Open the deep dive test
Glenable (gl_depth_test );
}
// Draw the scenario (display callback function)
Void renderscene ()
{
// Clear the form with the current clear color
Glclear (gl_color_buffer_bit | gl_depth_buffer_bit );
// Save the matrix state (Model View matrix)
Glmatrixmode (gl_modelview );
Glpushmatrix ();
// Translate the coordinate system. Note that it is relative to the position of the visual coordinate.
Gltranslatef (0.0f, 0.0f,-300366f );
// Draw the sun
Glcolor3ub (255,255, 0 );
Glusolidsphere (15.0f, 15, 15 );
// Rotate the coordinate system to accumulate the effect
Glrotatef (fearthrot, 0.0f, 1.0f, 0.0f );
// Draw the earth
Glcolor3ub (255, 0, 0 );
// Translate the coordinate system to accumulate the effect
Gltranslatef (105.0f, 0.0f, 0.0f );
// Set the rotation step of the earth
Fearthrot + = 5.0f;
If (fearthrot> 360.0f ){
Fearthrot = 0.0f;
}
Glusolidsphere (15.0f, 15, 15 );
// Draw the moon
Glcolor3ub (200,200,200 );
// Rotate the coordinate system to accumulate the effect
Glrotatef (fmoonrot, 0.0f, 1.0f, 0.0f );
// Translate the coordinate system to accumulate the effect
Gltranslatef (30366f, 0.0f, 0.0f );
// Set the rotation step of the moon
Fmoonrot + = 15.0f;
If (fmoonrot> 360.0f ){
Fmoonrot = 0.0f;
}
Glusolidsphere (6.0f, 15, 15 );
// Restore the matrix state (the current coordinate system and visual coordinates coincide)
Glpopmatrix ();
// Swap buffer, Display Screen
Gluswapbuffers ();
}
// Called by the glut function library when the form size changes
Void changesize (glsizei W, glsizei H)
{
// Form Aspect Ratio
Glfloat faspect;
// Prevent division by 0
If (0 = h ){
H = 1;
}
// Set the window size to the form size
Glviewport (0, 0, W, H );
// Calculate the aspect ratio of the form
Faspect = (glfloat) W/(glfloat) h;
// Set the current operation matrix to a projection matrix.
Glmatrixmode (gl_projection );
Glloadidentity ();
// Define the intercept, with a 45-degree field of view. The near and far planes are 1.0 and 425.0.
Gluperspective (45.0f, faspect, 1.0, 425.0 );
// Set the matrix of the current operation as the model view Matrix
Glmatrixmode (gl_modelview );
Glloadidentity ();
}
// Timer function, triggering 10 screen weight painting events per second
Void timerfunc (intvalue)
{
Glupostredisplay ();
Glutimerfunc (100, timerfunc, 1 );
}
Int main (INT argc, char * argv [])
{
// Pass the number of arguments in the command line and initialize the glut function library
Gluinit (& argc, argv );
// Set the display mode when creating the form (dual-buffer, RGB color mode)
Fig );
// Set the initial size of the form
Gluinitwindowsize (480,320 );
// Create a form
Valley createwindow ("Bounce ");
// Set the display callback function
Gludisplayfunc (renderscene );
// Set the callback function when the size of the form changes
Glureshapefunc (changesize );
// Sets the timer function.
Glutimerfunc (100, timerfunc, 1 );
// Set the rendering status
Setuprc ();
// Start the execution of the glut framework. Once called, it will not be returned until the program is terminated.
Glumainloop ();
Return0;
}