OpenGL-based fireworks Particle System voyage blog)

Source: Internet
Author: User
Animation contentThis animation simulates the fireworks releasing process. The fireworks rose from the ground and exploded in the air, generating numerous small fireworks in circles and falling down. The energy degrades until it disappears. The animation's angle of view is roughly 30 °. GlossaryVC ++ 6.0: A Development Environment; OpenGL: A professional open 3D program interface, is a powerful and convenient underlying 3D graphics library; glut: OpenGL utility toolkit; particle System: a collection of many particles. In the field of image science, people use particle systems to simulate various phenomena, such as sparks, flying birds, and waves. In these applications, the dynamics of the particle system determine the position of the particle. At each position, we place a graphical object instead of a point. [1] Running Environment
Hardware requirements Pentium 4 GHz or above, graphics card x550 (Bit Width, MB memory) or above.
Software requirements Windows 2 k/XP operating system, VC ++ 6.0, and support for OpenGL glut toolkit.
Detailed Design Data structure definition of ParticlesTypedef struct {float x, y, z; // particle position float xspeed, yspeed, zspeed; // particle speed float XG, YG, ZG; // Particle Motion Acceleration float R, G, B; // the color of the particle int style; // whether the particle is rising or falling} participant; Data structure definition of fireworks # Define max_participant 24 // Number of small fireworks # Define max_tail 30 // Tail length of fireworks # Define max_fire 5 // Number of fireworks Struct { Particle [max_participant] [max_tail]; // Fireworks system Array Glfloat life, fade, Rad ;// Fireworks life, attenuation speed, and X - Z Movement speed on the plane } Fire [max_fire]; Modeling and Implementation of fireworks Fireworks are generated and raised from a certain point on the ground ( Y Axis direction) for deceleration, in X , Z The Axis speed is 0 . When Y The Direction Speed is 0 When the fireworks exploded, a small fireworks were generated. The fireworks X - Z Speed on the plane Rad Assign an initial value and break it down X , Z Direction (the angle with the coordinate axis is determined by subscript ), Y The acceleration of the axis remains unchanged, so the trajectory of the small fireworks is parabolic. The mathematical model of Location Coordinate change after explosion is as follows: X = rad * Cos ();   Y = yspeed + Gt; Z = rad * sin (); Below are the initial values assigned to the fireworks System ( Init () Main source code of the Function ) For (loop2 = 0; loop2 <max_fire; loop2 ++) { // Initial Location Xtemp = rand () % 30-15; Ytemp = (-1) * rand () % 5-8; Ztemp = (-1) * rand () % 5-100; Speed = rand () % 5 + 10 ;// X - Z Movement speed on the plane Fire [loop2]. Life = 1.0f; Fire [loop2]. Fade = (float) (RAND () % 100)/20000 + 0.002f; Fire [loop2]. Rad = rand () % 3 + 4; For (loop = 0; loop <max_participant; loop ++) { Fire [loop2]. Particle [loop] [0]. Style = 0; // Initial Location Fire [loop2]. Particle [loop] [0]. x = xtemp; Fire [loop2]. Particle [loop] [0]. Y = ytemp; Fire [loop2]. Particle [loop] [0]. z = ztemp; // Initial speed Fire [loop2]. Particle [loop] [0]. xspeed = 0.0f; Fire [loop2]. Particle [loop] [0]. yspeed = speed; Fire [loop2]. Particle [loop] [0]. zspeed = 0.0f; // Initial Acceleration Fire [loop2]. Particle [loop] [0]. XG = 0.0f; Fire [loop2]. Particle [loop] [0]. YG =-2.0f; Fire [loop2]. Particle [loop] [0]. zg = 0.0f; // Initial color Fire [loop2]. Particle [loop] [0]. r = 1.0f; Fire [loop2]. Particle [loop] [0]. G = 1.0f; Fire [loop2]. Particle [loop] [0]. B = 1.0f; // Tail Initialization For (loop1 = 1; loop1 <max_tail; loop1 ++) { Fire [loop2]. Particle [loop] [loop1]. x = fire [loop2]. Particle [loop] [0]. X; Fire [loop2]. Particle [loop] [loop1]. Y = fire [loop2]. Particle [loop] [0]. Y; Fire [loop2]. Particle [loop] [loop1]. z = fire [loop2]. Particle [loop] [0]. Z; } // For1 end } // For2 end } // For3 end Changes in the moving position of a particle ( Idle () Main source code of the Function ) For (loop = 0; loop <max_participant; loop ++) { // Particle position update Fire [loop2]. Particle [loop] [0]. x + = fire [loop2]. Particle [loop] [0]. xspeed/(speedfator * 1000.0f ); Fire [loop2]. Particle [loop] [0]. Y + = fire [loop2]. Particle [loop] [0]. yspeed/(speedfator * 1000.0f ); Fire [loop2]. Particle [loop] [0]. Z + = fire [loop2]. Particle [loop] [0]. zspeed/(speedfator * 1000.0f ); // Particle speed update Fire [loop2]. Particle [loop] [0]. yspeed + = fire [loop2]. Particle [loop] [0]. YG/(speedfator * 1000.0f ); If (fire [loop2]. Particle [loop] [0]. Style = 0 & fire [loop2]. Particle [loop] [0]. yspeed <2.0) { Fire [loop2]. Particle [loop] [0]. Style = 1; Fire [loop2]. Particle [loop] [0]. r = colors [color] [0]; Fire [loop2]. Particle [loop] [0]. G = colors [color] [1]; Fire [loop2]. Particle [loop] [0]. B = colors [color] [2]; // Speed Decomposition Fire [loop2]. Particle [loop] [0]. xspeed = fire [loop2]. rad * sin (loop * 15.0/(2*3.14159 )); Fire [loop2]. Particle [loop] [0]. zspeed = fire [loop2]. rad * Cos (loop * 15.0/(2*3.14159 )); } } Fire [loop2]. Life-= fire [loop2]. Fade ;// Life decay Death and regeneration of Particles When the particle lifecycle is reduced to less 0 Particle death (the life value is used as the fourth parameter of the display color during the display process, that is Alpha Value ). Then regenerate the particle. The code is similar to the initialization code. Post-Production Add a background image to the animation, and use the Oriental Pearl night scene as the background. In order to reflect the authenticity of the fireworks, the mix is enabled after the background image is painted, so that the background color changes with the fireworks color. Glcalllist (1 );// Draw background Glable (gl_blend );// Enable Hybrid Change the angle of view to make the camera look up 30 °, Giving people an immersive feeling. Glulookat (0.0,-10.-30, 0.0, 0.0, 10.0,-100.0, 0.0, 1.0, 0.0 );

 

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.