In this recipe we will render particles introduced in the previous chapter using texture loaded from the PNG file.
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.
We will render particles using the previously created texture.
#include "cinder/gl/Texture.h" #include "cinder/ImageIo.h"
gl::Texture particleTexture;
setup
method load particleTexture
:particleTexture=gl::Texture(loadImage(loadAsset("particle.png")));
float radius = Rand::randFloat(2.f, 10.f);
draw
method we will draw our particles as follows:gl::enableAlphaBlending(); particleTexture.enableAndBind(); gl::color(ColorA::white()); mParticleSystem.draw();
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)); }
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
.
18.223.206.225