11 [actual application of particle system]

xiaoxiao2021-04-05  274

The actual application of the eleventh episode system

Realistic irregular objects To accurately simulate more particle properties and better process control, we just simulate some common objects.

The parameter initialization value and process control of the following particles are only an empirical model.

11.1 Snow

Beautiful "snow velvet flower" in the virtual world.

11.1.1 Parameter initialization value

Snowflakes are slow from the sky, and the wind is drifting into the land ....

(1). The initial position Y value is a relatively large positive value, indicating that the X and Z position values ​​can be random positive and negative values, indicating that there are snowflakes in all parts of the sky.

(2). Speed; the role of gravity acceleration in reality, where a simple y-axis negative value is used to simulate, X and Z speed values ​​randomly positive and negative values, simulation float in all directions.

(3). Life value; because the snow is slowly falling, the health is appropriate.

(4). Color; the texture of the snowflake is generally white, and the particle color has no personal hobby decision.

m_vpos.x = randf (-16.0, 16.0); // x The initial value is the random value of -16.0 to 16.0

m_vpos.y = 20.0;

m_vpos.z = randf (-16.0, 16.0);

m_vvel.x = randf (-0.1f, 0.1f);

m_vvel.y = randf (-0.2f, -0.1f);

m_vvel.z = randf (-0.1f, 0.1f);

m_flife = randf (16.0, 32.0);

11.1.2 Process Control

The process control of the snowflake is randomly reduced health; if the health is greater than zero, the location is updated according to the speed of each direction; if the health is less than zero, "delete" particles - actually reinitialize all values.

IF (m_ap [i] .m_flife> 0.0)

{

FRAND = RANDF (0.0F, 0.2F);

M_AP [i] .m_flife - = frand;

M_AP [i] .m_vpos.x = m_ap [i] .m_vvel.x;

M_AP [i] .m_vpos.y = m_ap [i] .m_vvel.y;

M_AP [i] .m_vpos.z = m_ap [i] .m_vvel.z;

}

Else

{

M_AP [i] .init (g_ntype);

}

11.1.3 Drawing Control

On the basis of the top set, the DEPTH-BUFFER is closed when the particles are rendered, preventing particles from being covered by the distant relationship of the departure point, and the far particles are covered with a close particle (there will be a black square border). Similarly After the rendering is completed, the logo is turned on to prevent it from affecting the rendy of rendering.

Void cpsystem :: Enterpsprite ()

{

Float fsize = float (g_fset * g_fsize);

FLOAT FMIN = float (g_fmin * g_fsize);

m_pd3ddev-> setrenderstate (D3DRS_ZWRIENABLE, FALSE);

m_pd3ddev-> setrenderstate (D3DRS_ALPHABLENDENABLE, TRUE);

m_pd3ddev-> setrenderstate (d3drs_destblend, d3dblend_one);

m_pd3ddev-> setrenderstate (D3DRS_PointSpriteEnable, true);

m_pd3ddev-> setrenderstate (D3DRS_PointScaleEnable, true);

m_pd3ddev-> setrenderstate (D3DRS_PointSize, Ftod (fsize));

m_pd3ddev-> setrenderstate (D3DRS_PointSize_min, Ftod (fmin));

m_pd3ddev-> setrenderstate (D3DRS_POINTSCALE_A, FTOD (0.0F)); m_pd3ddev-> setrenderstate (d3drs_pointscale_b, ftod (0.0f));

m_pd3ddev-> setrenderstate (D3DRS_PointScale_c, Ftod (1.0F));

}

Void cpsystem :: EXIXTPSPRITE ()

{

m_pd3ddev-> setrenderstate (D3DRS_ZWRIENABLE, TRUE);

m_pd3ddev-> setrenderstate (D3DRS_ALPHABLENDENABLE, FALSE);

m_pd3ddev-> setrenderstate (D3DRS_PointSpriteEnable, False);

m_pd3ddev-> setrenderstate (D3DRS_PointScaleEnable, False);

}

If the entire rendering process does not require depth-buffer and z-buffer, the enableautodepthstencil of the created parameter d3dpresent_Parameters is set directly to false when creating idirect3ddevice9. This does not have to be rendered to the D3DRS_ZWRITEENABLE flag.

D3DPresent_Parameters D3DPM;

Fillmemory (& D3DPM, SizeOf (D3DPresent_Parameters), 0);

// ...

D3DPM.Enableautodepthstencil = false;

// ...

The following example requires an on / off D3DRS_ZWRITEENABLE flag

11.2 Simple Fountain

11.2.1 Parameter initialization value

(1). The initial location is generally the world coordinate original.

(2). Speed; first there is an upward y-axis speed, then the Y-axis speed becomes a negative value under the action of the gravity acceleration, that is, the Y speed begins to be positive, and slowly becomes smaller than a negative value over time. Lazy, the Y-axis speed is only a random value per frame.

(3). Life value; personal hobby decision.

(4). Color; personal hobby decision.

m_vpos.x = 0.0; // The initial position is the origin

m_vpos.y = 0.0;

m_vpos.z = 0.0;

m_vvel.x = randf (-0.02f, 0.02f);

m_vvel.y = randf (0.1f, 0.2f); // The initial Y value is positive

m_vvel.z = randf (-0.02f, 0.02f);

m_flife = randf (8.0, 16.0);

11.2.2 Process Control

Each frame Y value subtracts a value to simulate the role of gravity acceleration.

M_AP [i] .m_flife - = frand;

FRAND = randf (-0.004f, 0.0f); // Decrease speed value randomly generated

M_AP [i] .m_vvel.y = frand;

M_AP [i] .m_vpos.x = m_ap [i] .m_vvel.x;

M_AP [i] .m_vpos.y = m_ap [i] .m_vvel.y;

M_AP [i] .m_vpos.z = m_ap [i] .m_vvel.z;

11.3 Simple Fireworks

11.3.1 Parameter initialization value

Fireworks are generally colorful, diffuse along a circular sphere, slowly decline under the action of gravity acceleration, so the initialization value of fireworks is special,

FLOAT FALPHA = RANDF ((- D3DX_PI / 2), (D3DX_PI / 2));

Float fbeta = randf (0.0, D3DX_PI * 2);

m_vpos.x = 0.0;

m_vpos.y = 10.0; // Initial location (0, 10, 0) m_vpos.z = 0.0;

m_vvel.x = cos (falpha) * sin (fbeta);

m_vvel.y = sin (falpha);

M_Vvel.z = cos (falpha) * cos (fbeta);

m_flife = 8.0;

For speed parameters, our initialized value is the longitude and latitude of the unit circle, the radius of fireworks and the radius of fireworks and the life value just opposite.

11.3.2 Process Control

The position of each frame of particles is in the initial position as a dot, the opposite of the health (here is 10.0 - life), and the Y-axis also subtracts the action of the simulated gravity acceleration, the Y-axis minus a value

Float fsize = 10.0F - m_ap [i] .m_flife;

M_AP [i] .m_flife - = 0.02F;

M_AP [i] .m_vpos.x = fsize * m_ap [i] .m_vvel.x;

M_AP [i] .m_vpos.y = 10.0f fsize * m_ap [i] .m_vvel.y - fsize * 0.6f;

M_AP [i] .m_vpos.z = fsize * m_ap [i] .m_vvel.z;

11.4 Simple Smoke

Similar to the fountain, only as a gaseous substance, ignore the role of gravity acceleration, and do not have to reduce the speed value in the Y direction per frame.

11.5 Thunder Light

The particles are arranged on a straight line, and the points on the straight line are added to each unit x value, the increment of the Y and Z axes is a constant. Here, the line of X: Y: Z = 1: 1: 1 is selected.

11.5.1 Parameter initialization value

m_vpos.x = 0.0; // Non-location and speed

m_vpos.y = 0.0;

m_vpos.z = 0.0;

m_vvel.x = 0.0;

m_vvel.y = 0.0;

m_vvel.z = 0.0;

m_flife = randf (16.0, 32.0);

11.5.2 Process Control

FRAND = RANDF (-0.4f, 0.4f); // x, y, z increment

m_ap [i] .m_vpos.x = frand;

M_AP [i] .m_vpos.y = frand;

M_AP [i] .m_vpos.z = frand;

11.6 Examples of the actual application of particle systems

11.6.1 Example

Example Only the position, speed, life, and color properties of the particles are used. The particles in the actual particle system may contain more attributes, which can achieve a more beautiful effect.

The number of particles also affects the effect, and our example uses up to 128 particles, and the upper limit of the number of particles rendered per frame is dependent and hardware.

Game10 Project - Game11 Project, simple, and a small amount of Hard Code is implemented for the code.

Use the mouse to take different text to view the effect of different particle systems, click on the particle size and the arrow in the speed box to change the size and speed of the particles. Use the direction key to control the left and right and up and down movements of the viewpoint, the roller control of the mouse The distance of the object, the ESC button exits.

The space bar switches automatically rotates because there is no delay time of the button, so the automatic rotation switching speed of the space bar is very fast, and it is necessary to quickly press. If there is no switching, it is successful.

Endose episode

The knowledge of some objects will make the particle system more real.

转载请注明原文地址:https://www.9cbs.com/read-132184.html

New Post(0)