Texturing particles

In this recipe we will render particles introduced in the previous chapter using texture loaded from the PNG file.

Getting started

This recipe code base is an example of the recipe Simulating particles flying on the wind from Chapter 5, Building Particle Systems. We also need a texture for a single particle. You can prepare one easily with probably any graphical program. For this example, we are going to use a PNG file with transparency stored inside the assets folder with a name, particle.png. In this case it is just a radial gradient with transparency.

Getting started

How to do it…

We will render particles using the previously created texture.

  1. Include the necessary header files:
    #include "cinder/gl/Texture.h"
    #include "cinder/ImageIo.h"
  2. Add a member to the application main class:
    gl::Texture particleTexture;
  3. Inside the setup method load particleTexture:
    particleTexture=gl::Texture(loadImage(loadAsset("particle.png")));
  4. We also have to change the particle size for this example:
    float radius = Rand::randFloat(2.f, 10.f);
  5. At the end of the draw method we will draw our particles as follows:
    gl::enableAlphaBlending();
    particleTexture.enableAndBind();
    gl::color(ColorA::white());
    mParticleSystem.draw();
  6. Replace the draw method inside the Particle.cpp source file with the following code:
    void Particle::draw(){
    ci::gl::drawSolidRect(ci::Rectf(position.x-radius, position.y-radius,
    position.x+radius, position.y+radius));
    }

How it works…

In step 5, we saw two important lines. One enables alpha blending and the other binds our texture stored in the particleTexture property. If you look at step 6, you can see we drew each particle as a rectangle and each rectangle had texture applied. It is a simple way of texturing particles and not very performance effective, but in this case, it works quite well. It is possible to change the color of drawing particles by changing the color just before invoking the draw method on ParticleSystem.

How it works…

See also

Look into the recipe Texturing the particle system using Point sprites and shaders

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.206.225