OpenGL Study Notes 4 (animation)

Source: Internet
Author: User

The following is an example of how to get started with OpenGL ~~~ Copyright or author's. I just cannot find the source.

OpenGL is similar to traditional animations. It presents a picture in front of the audience. Once the screen changes quickly, the audience will think that the screen is continuous.
Double buffering technology is a widely used technology in computer graphics. Most OpenGL implementations support double buffering technology.
It is usually used to draw an animation when the CPU is idle, but there can be other options.

Dual-buffer technology
The animation on the computer is somewhat different from the actual Animation: the actual animation is painted first, and it can be displayed directly during playback.

Computer Animation is to draw one, take one, then draw another, and then take it out.

If the image to be drawn is very simple, it is no problem. However, once the drawing is complex and takes a long time to draw, the problem becomes more prominent.

Let's think of a computer as a person who draws pictures quickly. If he draws pictures directly on the screen, the pictures are complicated, it is possible that he will be seen by the audience when he only draws half of a certain image. Although he completed the painting later, the audience's eyes did not respond, and they stayed on the incomplete picture. That is to say, sometimes the audience sees the complete image, but sometimes only the incomplete image, which causes the screen to flash.

How can this problem be solved? We imagine that there were two artboards. The drawing was painted by the person next to them. After the painting was finished, the drawing board in his hand was exchanged with the drawing board hanging on the screen. In this way, the audience will not see incomplete pictures.

This technology is applied to computer graphics and is called the dual-buffer technology. That is, two areas are opened in the memory (probably Display memory), one is used as the data sent to the display, the other is used as the painting area, and they are exchanged as appropriate. Because only two pointers are required to swap two memory areas, this method is very efficient and widely used.

Note: although most platforms support double buffering technology, this technology is not included in the OpenGL standard. To ensure better portability, OpenGL allows you not to use double buffering technology during implementation. Of course, our commonly used PCs support dual-buffer technology.

The simplest way to enable the dual-buffer function is to use the glut toolkit. We previously wrote in the main function:

Fig );

In this example, the value of "glu_single" indicates a single buffer. If it is changed to "glu_double", the double buffer is used.

Of course, there is something to change-every time the painting is complete, we need to swap two buffers and use the drawn information for Screen Display (otherwise, no matter how it is drawn or nothing ). If you use the glut toolkit, you can easily complete this job, as long as you simply call the gluswapbuffers function when the painting is complete.

Glmatrixmode

This function is actually a declaration of what to do next, that is, to tell the computer that I want to operate on "what", and what "in glmatrixmode" () "Options (parameters) include gl_projection, gl_modelview, and gl_texture;

If the parameter is gl_projection, it means to perform projection operations, that is, to project an object to a plane, just like taking a photo, place a three-dimensional object on a two-dimensional plane. In this way, the following statement can be a function related to perspective, such as glfrustum () or gluperspective ();


If the parameter is gl_modelview, this is an operation on the model Visual View. The following statement depicts a model-based adaptation. In this way, you can set the parameters. Next, you will use the parameters such as glulookat () such a function;

For gl_texture, texture-related operations are performed;


Operations in OpenGL are mostly based on operations on matrices, such as displacement, rotation, and scaling.

Therefore, the standard here is that glmatrixmode is used to specify which matrix is the current matrix, and its parameters represent the target to be operated. gl_projection is used to operate the projection matrix, gl_modelview is used to operate the model visual matrix, while gl_texture is used to perform subsequent operations on the texture matrix.


========================================================== ============================

The following function is explained by: http://www.cppblog.com/COOOOOOOOL/archive/2009/12/28/104255.html
The explanation is really good ~~ Haha

Gluperspective (gldouble
Fovy, gldouble aspect, gldouble znear, gldouble zfar)
Set Perspective Projection Matrix

First, set gluperspective to see what its parameters mean.

Fovy, which is the most difficult to understand. My understanding is that the angle of open eyes, that is, the size of the angle of view. If it is set to 0, you close your eyes, so you cannot see anything, if it is 180, you can think that your vision is very broad,

Aspect. This is the aspect ratio of the actual window, that is, x/y.

Znear. What about this? It indicates your neighborhood,

Zfar indicates the cutting surface in the distance,


If you do not understand it, continue reading,

We know that something in the distance looks smaller, and something in the near area looks larger, which is the principle of perspective.

As shown in


========================================================== ========================================================== ======================

Below this function comes from Baidu Encyclopedia: http://baike.baidu.com/view/2341537.htm

Viewpoint Conversion

Function prototype:

Void glulookat (gldouble eyex, gldouble eyey, gldouble Eyez, gldouble centerx, gldouble centery, gldouble centerz, gldouble UPX, gldouble upy, gldouble upz );

Parameters:

Glulookat () has nine parameters: eye position, eye orientation, and photo orientation.

This function defines the viewpoint matrix and times it by the current matrix. Eyex, eyey, and Eyez define the point of view. The centerx, centery, and centerz variables specify the point of reference, which is usually the point on the central axis of the scene targeted by the camera; the UPX, upy, and upz variables specify the direction of the UP vector.

Note:

Generally, the viewpoint conversion operation is triggered after the model conversion operation, so that the model conversion takes effect on the object first. In a scenario, the vertex of an object moves to the desired position after model conversion, and then positions the viewpoint in the scenario. Model conversion and viewpoint conversion form a visual matrix of the model.

This function is used to transform the model matrix (gl_modelview), while the gluperspective function is used to transform the projection matrix (gl_projection). This 1.1 must be clarified.

You can use the gluperspective function to set the near and far planes.

This section describes the knowledge about vertical synchronization.

This article introduces a simple method for calculating Frame Rate (FPS.
Finally, we listed a complete list of celestial Animation programs.

Code:

# Include <Gl/glut. h> # include <stdio. h> # include <time. h> // The Sun, Earth, and moon are assumed to be 12 days a month. // 12 months a year, a total of 360 days of static int day = 100; // the change of day: from 0 to 359 double calfrequency () {static int count; static double save; static clock_t last, current; double timegap; ++ count; If (count <= 50) return save; count = 0; last = current; current = clock (); timegap = (current-last)/(double) clk_tck; save = 50.0/timegap; return save ;} void mydisplay (void) {double FPS = calfrequency (); printf ("FPS = % F \ n", FPS); glable (gl_depth_test); glclear (gl_color_buffer_bit | gl_depth_buffer_bit ); glmatrixmode (gl_projection); // operate glloadidentity () on the projection matrix; // move coordinates to the center location gluperspective (75, 1, 1, 400000000 ); // set the Perspective Projection Matrix glmatrixmode (gl_modelview); // operate glloadidentity () on the model visual matrix; // move the origin of the current user coordinate system to the center of the screen: similar to a reset operation glulookat (0,-200000000,200 running 00, 0, 0, 0, 0, 0, 1 ); // viewpoint conversion // draw the red "sun" glcolor3f (1.0f, 0.0f, 0.0f ); // draw the blue "Earth" glcolor3f (0.0f, 0.0f, 1.0f); glrotatef (day/360.0*360.0, 0.0f, 0.0f,-1.0f); gltranslatef (150000000, 0.0f, 0.0f); maid (15945000, 20, 20); // draw the yellow "moon" glcolor3f (1.0f, 1.0f, 0.0f ); glrotatef (day/30.0*360.0-day/360.0*360.0, 0.0f, 0.0f,-1.0f); gltranslatef (38000000, 0.0f, 0.0f); glrotatsphere (4345000, 20, 20 ); glflush (); glswapbuffers (); // swap two buffers}/* new function, called when idle. The function is to move the date one day later and re-draw it, achieve the animation effect */void myidle (void) {++ day; if (day> = 360) Day = 0; mydisplay () ;}int main (INT argc, char * argv []) {gluinit (& argc, argv); gluinitdisplaymode (glu_rgb | glu_double); gluinitwindowposition (100,100); gluinitwindowsize (400,400); glucreatewindow ("Sun, earth and moon "); fig (& mydisplay); glutidlefunc (& myidle); fig (); Return 0 ;}

Result:

Di

The earth turns around the sun. The moon rotates around the Earth and the Sun.

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.