First talk about the big idea: water waves, we take out his cross-section is actually a curve, so in the drawing we can use multiple trigonometric functions and other superposition to achieve this curve, we from the top to see this water wave, in fact, is a circle, it can be understood that n cross-sectional synthesis. To sum up, so we can directly construct a number of curves to synthesize our water waves, as for the ripple out of the outside, we are through the displacement of each plane to achieve.
Say back to the code implementation, first, through Glutinit (&ARGC, argv) initialization, and then set the window position and size, and the title, and then call a Glutreshapefunc call their own write myreshape to specify the view:
1 voidMyreshape (Glsizei W, Glsizei h) {2 //If the height is large than W, we should use the Glviewport to adjust it3 if(W <h) {4Glviewport (0,0, W, W);5}Else {6Glviewport (0,0, W, h);7 }8}
Then use Glutdisplayfunc to call your display function to draw the graph:
1 voidMydisplay (void) {2 glfloat x;3 //set the Backgroud color4Glclearcolor (0.7f,0.7f,0.7f,1.0f);5Glclear (Gl_color_buffer_bit |gl_depth_buffer_bit);6 //set The point we look it7 Glmatrixmode (gl_modelview);8 //Replaces the current matrix with the identity matrix9 glloadidentity ();TenGlulookat (0, -0.5,0.4,0,0,0,0,0,0.5); One A Glbegin (gl_lines); - //set the color of the line -GLCOLOR3F (0.5f,1.0f,1.0f); the //Draw the line - for(floati =0.0f; I <360.0; i + =3.0) { - for(x =0; X <1.0f/factor; X + =0.01f) { -glvertex3f (X*factor*cos (i), cos (2* x-num) *factor, x*factor*sin (i)); + } - } + glend (); A Glflush (); at glutswapbuffers (); -}
One of the Glclearcolor (0.7f, 0.7f, 0.7f, 1.0f) is defined as the background color. Glulookat (0,-0.5, 0.4, 0, 0, 0, 0, 0, 0.5) Set your point of view, which is where you look from. Note that the Glmatrixmode (Gl_modelview) and glloadidentity () are added here, the previous one specifies the processing mode, the second line restores the initial coordinate system, and the image that is drawn without adding is strange (see Http://blog.csdn.net /shuaihj/article/details/7228265).
When drawing, I set the line color to 0.5f, 1.0f, 1.0f, and then through a for loop draw 120 lines, for each line, is the origin of the coordinates as the starting point, X*factor*cos (i) for the x-axis coordinates, x*factor*sin (i) for the z-axis, cos ( 2 * x-num) *factor is the y-axis coordinate, and the ripple effect is achieved by changing the value of num to achieve constant displacement. glutswapbuffers () is used to achieve double buffering, which is better than animation, not Kaka.
To implement the animation, call your animation function Spincube with Glutidlefunc:
1 void Spincube () {2 0.1 ; 3 Glutpostredisplay (); 4 }
Glutpostredisplay mark the current window needs to be redrawn and num is used to change the position of the displacement at each cycle.
The last Call to Glutmainloop () loops is done.
The effect is as follows (no ripple effect is visible):
The detailed code is as follows:
1#include <GL/glut.h>2#include <math.h>3 4 ConstGlfloat factor =0.1f;5Glfloat num =0.0f;6 voidMydisplay (void) {7 glfloat x;8 //set the Backgroud color9Glclearcolor (0.7f,0.7f,0.7f,1.0f);TenGlclear (Gl_color_buffer_bit |gl_depth_buffer_bit); One //set The point we look it A Glmatrixmode (gl_modelview); - //Replaces the current matrix with the identity matrix - glloadidentity (); theGlulookat (0, -0.5,0.4,0,0,0,0,0,0.5); - - Glbegin (gl_lines); - //set the color of the line +GLCOLOR3F (0.5f,1.0f,1.0f); - //Draw the line + for(floati =0.0f; I <360.0; i + =3.0) { A for(x =0; X <1.0f/factor; X + =0.01f) { atglvertex3f (X*factor*cos (i), cos (2* x-num) *factor, x*factor*sin (i)); - } - } - glend (); - Glflush (); - glutswapbuffers (); in } - to voidSpincube () { +num = num +0.1; - Glutpostredisplay (); the } * $ voidMyreshape (Glsizei W, Glsizei h) {Panax Notoginseng //If the height is large than W, we should use the Glviewport to adjust it - if(W <h) { theGlviewport (0,0, W, W); +}Else { AGlviewport (0,0, W, h); the } + } - $ intMainintargcChar*argv[]) { $ //Init -Glutinit (&argc, argv); - //Set Mode theGlutinitdisplaymode (glut_double | Glut_rgb |glut_depth); - //set the positionWuyiGlutinitwindowposition ( -, -); the //set the width and weigth -Glutinitwindowsize ( -, the); WuGlutcreatewindow ("It's a rippling wave."); - Glutreshapefunc (myreshape); AboutGlutdisplayfunc (&mydisplay); $ //To set the action - Glutidlefunc (spincube); - //Loop - Glutmainloop (); A return 0; +}
Use OpenGL to make a simple rippling wave of lines