Particle Systems - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Particle Systems

Description:

Refresher on Vertex Buffers. A chunk of contiguous memory that ... Offset Offset in bytes from the start of the buffer. Size Number of bytes to lock ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 24
Provided by: bdug1
Category:

less

Transcript and Presenter's Notes

Title: Particle Systems


1
Particle Systems
  • Bryan Duggan

2
Introduction
  • Sometimes instead of modelling a complex object
    with many vertices, we need to model
  • Lots of small objects each with a single vertex
  • This is called a particle system
  • There are lots of applications for particle
    systems

3
(No Transcript)
4
Example uses
  • Dust from a moving vehicle
  • Snow
  • Exhaust jets
  • Blood gushing out
  • Fire
  • Sparks

5
Requirements
  • Keep track of lots of particles
  • Create new particles (sometimes using random
    attributes)
  • Update each particle
  • Using the laws of physics
  • Render each one
  • When particles die, create new ones

6
Refresher on Vertex Buffers
  • A chunk of contiguous memory that contains vertex
    data
  • A place to hold the vertices we want to draw.
  • Can be placed in VRAM for faster rendering
  • Represented by a IDirect3DVertexBuffer9 object

7
You decide the format of the vertex
  • Depending on whether you want colour, texturing
    etc.
  • E.g.
  • struct Vertex
  • Vertex()
  • Vertex(float x, float y, float z)
  • _x x _y y _z z
  • float _x, _y, _z
  • static const DWORD FVF
  • const DWORD VertexFVF D3DFVF_XYZ

8
Vertices with colours
  • struct ColorVertex
  • ColorVertex()
  • ColorVertex(float x, float y, float z, D3DCOLOR
    c)
  • _x x _y y _z z _color c
  • float _x, _y, _z
  • D3DCOLOR _color
  • static const DWORD FVF
  • const DWORD ColorVertexFVF D3DFVF_XYZ
    D3DFVF_DIFFUSE

9
Vertices with textures
  • struct TexelVertex
  • TexelVertex()
  • TexelVertex(float x, float y, float z, float u,
    float v)
  • _x x _y y _z z
  • _u u _v v
  • float _x, _y, _z
  • float _u, _v
  • static const DWORD FVF
  • const DWORD TexelVertexFVF D3DFVF_XYZ
    D3DFVF_TEX1

10
To create one
  • HRESULT CreateVertexBuffer(      
  • UINT Length,     DWORD Usage,DWORD FVF,
    D3DPOOL Pool,     IDirect3DVertexBuffer9 ppVer
    texBuffer, HANDLE pSharedHandle
  • )
  • Length Number of bytes (number_of_vertices
    sizeof(Vertex)
  • Usage Well usually use D3DUSAGE_WRITEONLY
  • FVF Flexible vertex format The static member
    of the struct
  • Pool - D3DPOOL_MANAGED Let DirectX manage the
    pool
  • pSharedHandle Usually 0

11
Example
  • device-gtCreateVertexBuffer(
  • 36 sizeof(TexelVertex), // size in bytes
  • D3DUSAGE_WRITEONLY, // flags
  • TexelVertexFVF, // vertex format
  • D3DPOOL_MANAGED, // managed memory pool
  • _landscapeVertices,// return create vertex
    buffer
  • 0
  • )

12
To write to the buffer
  • Obtain a pointer to the contents by
  • Vertex vertices
  • IDirect3DVertexBuffer9Lock(
  • UINT Offset,
  • UNIT size,
  • BYTE vertices,
  • DWORD flags
  • )
  • Offset Offset in bytes from the start of the
    buffer
  • Size Number of bytes to lock
  • vertices Pointer to the start of the locked
    data. We can write to this pointer after we lock
    the buffer
  • Flags Usually 0.
  • E.g.
  • landscapeVertices-gtLock(0, 0, (void)vertices,
    0)
  • vertices0 Vertex(1,2,3)
  • vertices1 Vertex(1,2,3)

13
To draw the buffer
  • Set the vertex stream source
  • device-gtSetStreamSource(0, _landscapeVertices,
    0, sizeof(Vertex))
  • Set the format of the stream
  • device-gtSetFVF(VertexFVF)
  • Perform any transformations necessary
  • D3DXMATRIX landscapeScale
  • D3DXMatrixScaling(landscapeScale, 1,1,1)
  • device-gtSetTransform(D3DTS_WORLD,
    landscapeScale)
  • Draw the vertices
  • device-gtDrawPrimitive(D3DPT_TRIANGLELIST, 0, 2)

14
The Particle Class
  • class Particlepublic MoveableEntity
  • protected
  • int _size
  • D3DCOLOR _colour
  • float _duration
  • float _age
  • public
  • Particle()
  • void update(float timeDelta)
  • int getSize()
  • void setSize(int size)
  • D3DCOLOR getColour()
  • void setColour(D3DCOLOR colour)
  • float getAge()
  • void setAge(float age)

15
The ParticleSystem class
  • class ParticleSystempublic MoveableEntity,
    public DrawableEntity
  • private
  • Particle _particles
  • long _numParticles
  • IDirect3DVertexBuffer9 _vertices
  • IDirect3DTexture9 _texture
  • float _size
  • stdstring _textureName
  • public
  • ParticleSystem()
  • ParticleSystem(int numParticles)
  • ParticleSystem()
  • bool setup(IDirect3DDevice9 device)
  • void update(float timeDelta)
  • void draw(IDirect3DDevice9 device)
  • void cleanup(IDirect3DDevice9 device)
  • void initParticle(Particle particle)

16
New FVF
  • struct ParticleVertex
  • ParticleVertex(float x, float y, float z,
    D3DCOLOR c)
  • _x x _y y _z z _color c
  • ParticleVertex()
  • _x _y _z 0
  • _color 0
  • float _x, _y, _z
  • D3DCOLOR _color
  • static const DWORD FVF

17
Point Sprites
  • A vertex
  • But rendered with a texture in place of every
    vertex
  • Create a vertex buffer of ParticleVertexs

18
To render
  • device-gtSetRenderState(D3DRS_LIGHTING, false)
  • device-gtSetRenderState(D3DRS_POINTSPRITEENABLE,
    true)
  • device-gtSetRenderState(D3DRS_POINTSCALEENABLE,
    true)
  • device-gtSetRenderState(D3DRS_POINTSIZE,
    FtoDw(_size))
  • device-gtSetRenderState(D3DRS_POINTSIZE_MIN,
    FtoDw(0.0f))
  • // control the size of the particle relative to
    distance
  • device-gtSetRenderState(D3DRS_POINTSCALE_A,
    FtoDw(0.0f))
  • device-gtSetRenderState(D3DRS_POINTSCALE_B,
    FtoDw(0.0f))
  • device-gtSetRenderState(D3DRS_POINTSCALE_C,
    FtoDw(1.0f))
  • // use alpha from texture
  • device-gtSetTextureStageState(0,
    D3DTSS_ALPHAARG1, D3DTA_TEXTURE)
  • device-gtSetTextureStageState(0, D3DTSS_ALPHAOP,
    D3DTOP_SELECTARG1)
  • device-gtSetRenderState(D3DRS_ALPHABLENDENABLE,
    true)
  • device-gtSetRenderState(D3DRS_SRCBLEND,
    D3DBLEND_SRCALPHA)
  • device-gtSetRenderState(D3DRS_DESTBLEND,
    D3DBLEND_INVSRCALPHA)

19
Setting up the particle system
  • _numParticles numParticles
  • _particles new Particle_numParticles
  • _vertices NULL
  • _texture NULL
  • _size 0.25f
  • for (int i 0 i lt _numParticles i )
  • initParticle( _particlesi)

20
  • bool ParticleSystemsetup(IDirect3DDevice9
    device)
  • _device device
  • device-gtCreateVertexBuffer(_numParticles
    sizeof(ParticleVertex), 0, ParticleVertexFVF,
    D3DPOOL_DEFAULT, _vertices, NULL)
  • _texture AssetLoaderinstance()-gtgetTexture(_t
    extureName.c_str(), device)
  • return true

21
Initialise a new particle
  • void ParticleSysteminitParticle(Particle
    particle)
  • // printf("New particle!\n")
  • // Reset the particle
  • particle-gtsetPosition( D3DXVECTOR3(10, 0, 10))
  • D3DXVECTOR3 newLook
  • newLook.x getRandomFloat(-1.0f, 1.0f)
  • newLook.y getRandomFloat(-1.0f, 1.0f)
  • newLook.z getRandomFloat(-1.0f, 1.0f)
  • particle-gtsetLook( newLook)
  • particle-gtsetColour(D3DXCOLOR(
  • getRandomFloat(0.0f, 1.0f),
  • getRandomFloat(0.0f, 1.0f),
  • getRandomFloat(0.0f, 1.0f),
  • 1.0f))
  • particle-gtsetSpeed(getRandomFloat(1.0f, 5.0f))
  • particle-gtsetDuration(getRandomFloat(1.0f,
    5.0f))

22
Update
  • for(int i 0 i lt _numParticles i)
  • if(_particlesi.getAge() gt _particlesi.getDu
    ration())
  • initParticle( _particlesi)
  • _particlesi.update(timeDelta)

23
Things to try
  • Change the particle system to use
  • Random Acceleration instead of just random
    velocity
  • Change the particle system so that they all die
    at the same time (to make an explosion)
  • Change the particle system to make an explosion
    up from the ground (the particles only shoot
    upwards
  • Change the particle system to simulate snow
Write a Comment
User Comments (0)
About PowerShow.com