Hi all,
I am trying to make a simple particle system with basic physics computed on the GPU. Each particle has 2 forces: a spring force to an anchor position and a repulsive force from the mouse position.
I managed to code that using the ofxMSAOpenCL add-on and it works just fine up to 2 million particles on my iMac. For this project, however, I can’t use openCL as the app will be run on a MacMini.
I looked at the GPUparticleSystemExample which seems to do what I need. It uses FBO to store position data, send them to the fragment shader, and write them back to another FBO. Then the FBO is used in a vertex shader to set the new particles position.
I tried to modify the example to match my needs, but weird things happened. Particles are not where there are supposed to be. Forces are applied correctly on some particles, but not on others. Let me explain with a simple example, starting from unmodified GPUparticleSystemExample example:
I set the velocity to 0.0 to have a static system.
// 2. Making arrays of float pixels with velocity information and the load it to a texture
float * vel = new float[numParticles*3];
for (int i = 0; i < numParticles; i++){
//vel[i*3 + 0] = ofRandom(-1.0,1.0);
//vel[i*3 + 1] = ofRandom(-1.0,1.0);
// set velocity to 0.0
vel[i*3 + 0] = 0.0;
vel[i*3 + 1] = 0.0;
vel[i*3 + 2] = 1.0;
}
Then I try to put the points on a grid (reducing particle number to 200 for visibility): The particles don’t occupy the whole space!
numParticles = 200;
// 1. Making arrays of float pixels with position information
float * pos = new float[numParticles*3];
// add on a grid
float pX = 0.0, pY = 0.0;
float delta = 1.0/(float)textureRes;
for (int x = 0; x < textureRes; x++){
for (int y = 0; y < textureRes; y++){
int i = textureRes * y + x;
//pos[i*3 + 0] = ofRandom(1.0); //x*offset;
//pos[i*3 + 1] = ofRandom(1.0); //y*offset;
pos[i*3 + 0] = pX;
pos[i*3 + 1] = pY;
pos[i*3 + 2] = 0.0;
pY += delta;
}
pY = 0.0;
pX += delta;
}
If I manually set the position of one point afterwards, other points have moved!!?!
pos[0] = 0.9;
pos[1] = 0.9;
I really don’t get what is going wrong. I am fairly new to openGL and shaders. Any help would be appreciated!
Thanks!