Hello all,
I'm trying to get back into C++ programming, after a few years of Java (damned uni). I am particularly trying to use STL and classes, which i hardly used before (I guess java showed me how useful 'vectors' and more OO can be).
I'm trying to make a basic particle engine in OpenGL. It works by holding a vector of 'emitters', that then emit particles at a known rate (10/sec in my early tests). The particles are then copied into a vector (the emitter holds the values of the standard particle it emits). Both the emitter and particle is a struct.
My problem is that the particles don't seem to be created correctly. As soon as they are used (moved or drawn for example) the program crashes. I suspect it is a problem with using structs in a vector, or somehow a problem with my use of vectors.
Here are the structs, declared in a header file:
Code:struct particle { GLfloat col[4]; //rgba GLfloat colfade[4]; //rgba GLfloat pos[3]; //x,y,z GLfloat vel[3]; //x,y,z GLfloat acc[3]; //x,y,z: mainly for gravity GLfloat partsize; //the size of the particles: height and width of each }; struct emitter { //the default particle that will be created GLfloat col[4]; //rgba GLfloat colfade[4]; //rgba GLfloat pos[3]; //x,y,z GLfloat vel[3]; //x,y,z GLfloat acc[3]; //x,y,z: mainly for gravity GLfloat partsize; //the size of particles //add some 'variation', especially to velocity / colour double createrate, lastcreated; //the rate that particles are created per second double ttl; //time the emitter will 'die' };
the vectors are then created:
And here is the function that actually makes the particlesCode:std::vector <particle> parts; std::vector <emitter> emitters;
The for loops etc. are probably irrelevant, but shown to make sure I am iterating correctly. Basically, it works out how many particles should have been created since the method was last called, then creates that many by copying the 'default' values from the emitter into a 'temp' particle that is then copied into the vector. Once it is working I will add some randomness to the particles created, but for now I'd be happy with a single particle being drawn successfully :SCode:void ParticleSystem::addparticles(double time){ for (std::vector<emitter>::iterator iter = emitters.begin(); iter!=emitters.end(); ++iter) { emitter it = *iter; int numcreated = (int)((time-it.lastcreated)*it.createrate); if (numcreated>0){ //this ensures not creating excessive particles due to extra calls for (int i=0; i<numcreated; i++){ //create a particle at the given rate //create the particle std::cout << "creating a particle\n"; particle temp; for (int c=0; c<4; c++) { temp.col[c]=it.col[c]; temp.colfade[c]=it.colfade[c]; } for (int c=0; c<3; c++){ temp.pos[c]=it.pos[c]; temp.vel[c]=it.vel[c]; temp.acc[c]=it.acc[c]; } temp.partsize = it.partsize; //add it to the vector parts.push_back(temp); } it.lastcreated = time; } } }
Thanks for reading, hopefully you can help me![]()



LinkBack URL
About LinkBacks




This may not be the best way, but I'd probably use a size_type index into the vector.
