Particle Engine, Creating alot of Rain/Snow

Started by
11 comments, last by jefferytitan 11 years, 10 months ago
I have created a particle engine, basically, it create ONE single vertex using CreateVertexBuffer() and rendering it as much as particles we need.

Using the class I assign the number of particles that I want to generate at once, I am using "for() { }" statement to generate that number each time like the following:


void RenderParticles(...)
{
// Some other codes goes here...

for(DWORD i = 0; i < maxNumOfParticles; i++)
CreateNewParticle();

}


When I create a rain usually I set a high number for maxNumOfParticles number like 150-250, so I can have alot of rains rendering (if the weather is so rainy).

However, the rendering SLOW down when 'maxNumOfParticles' is high, making the game slow even on a modern graphic card.
Advertisement
It's hard to tell from this sample how you're using it. Are you instantiating objects every frame? Are you drawing each particle with a separate call instead of batching? Is each particle literally one raindrop? I would think that if you had a few different rain textures (each containing a few randomly scattered raindrops with alpha), players wouldn't notice that each raindrop wasn't an individual particle.

Each time DirectX render, I create particles according to [color=#000000]

maxNumOfParticles, here is what I use on each frame:



for(DWORD i = 0; i < maxNumOfParticles; i++)
CreateNewParticle();

Hmm, I don't think we're quite on the same page. Here are my questions:

  1. Does one particle = one raindrop?
  2. How many textures are you using for your particles?
  3. What does CreateNewParticle do?

    1. Are you allocating new vertices or textures in CreateNewParticle?
    2. Is each particle rendered as a separate batch? One thread on here concerned a developer who was drawing a circle using approximately 100 line segments. With begin and end after each line segment the speed was terrible. As one batch it was very fast.
    3. Does CreateNewParticle just perform rendering of an existing particle, or create a particle?
    4. Where is the code for moving a particle?

1. Yes, one particle = one raindrop.
2. I am using only one texture for the rain, each particle (raindrop) set the same texture.
3.
1. Only ONE single vertex created at loading time, and used in rendering to create different raindrops.
3. [color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]CreateNewParticle() ONLY create new particles and it does that only during rendering process.[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)] 4. The code for moving particles is inside RenderParticles() method[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]Basically, I ONLY create ONE single vertex for ALL the particles, this vertex is rendered as many as the particles with different matrix world so I can get the particles to move in different positions, I am using point sprite.[/background]

[/font]

The key question being asked here is how many draw calls do you have? Do you have one draw call that handles all of your particles, or do you have one draw call per-particle? The fact that you have a different matrix for each particle suggests the latter, in which case - yes - it's going to perform terribly. You really need to post the code you have for CreateNewParticle and RenderParticle here - and the full code, not just an excerpt. It's not enough to describe what they do as it's not possible to correctly analyse why you're running slow based on a mere description - hence all the questions.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

As pointed out above, it seems as though you are rendering and updating for each particle! Think about that and you will see why it will slow down. If you have 10,000 particles, each one has to create a new transformation matrix (10,000+ operations) and each one will render itself separately(10,000+ draw calls).

Most modern particle systems use the GPU for both rendering and calculating positions. And then some sort of particle collision system on the CPU. Using the GPU , you will then batch alot of particles together, in a DynamicVertexBuffer which will be sent to the graphics card at once! Turning 10,000 draw calls into maybe 2 or 3!
You should post both the CreateNewParticle and RenderParticle methods.
I'm confused because in your other post (that I just repied to) you stated you were using Point Sprites. In this post your refering to vertices, implying quads. In any event, like the above posts, we'll need to see your render function at the very least. Performing a transformation and a draw call for each particle is highly inefficient.
So I'm reading this and wondering. Would the best way to do this be to have 1 particle and instance it with separate transformation matrices? Sorry if I just hijacked your thread.

So I'm reading this and wondering. Would the best way to do this be to have 1 particle and instance it with separate transformation matrices? Sorry if I just hijacked your thread.


I'd use a list of particles, updating their positions in a dynamic vertex buffer, calculate the world matrix once and then draw with the D3DDevice->DrawIndexedPrimitive (triangle list) method. But depending on your target hardware and version of DX, you can do almost all of it on the GPU these days.

This topic is closed to new replies.

Advertisement