Пример #1
0
	void RandomForce::createData(DataSet& dataSet,const Group& group) const
	{
		dataSet.init(NB_DATA);
		dataSet.setData(FORCE_VECTOR_INDEX,SPK_NEW(Vector3DArrayData,group.getCapacity(),1));
		dataSet.setData(REMAINING_TIME_INDEX,SPK_NEW(FloatArrayData,group.getCapacity(),1));

		// Inits the data
		for (ConstGroupIterator particleIt(group); !particleIt.end(); ++particleIt)
			initParticle(*particleIt,&dataSet);
	}
Пример #2
0
void initParticles(PARTICLE * ps) {
  int i;
   
  srand(clock());

  for(i = 0; i != PARTICLE_NUMBER; i++) {
    initParticle(ps + i, i);
	/*if (ps->position.x == (ps + i)->position.x || 
		ps->position.y == (ps + i)->position.y ||
		ps->speed.x == (ps + i)->speed.x ||
		ps->speed.y == (ps + i)->speed.y)*/
  }
}
Пример #3
0
void PSystem::addParticle()
{
	if( mDeadParticles.size() > 0)
	{
		// Reinitialize a particle.
		Particle* p = mDeadParticles.back();
		initParticle(*p);

		// No longer dead.
		mDeadParticles.pop_back();
		mAliveParticles.push_back(p);
	}
}
Пример #4
0
bool ofxParticleEmitter::addParticle()
{
	// If we have already reached the maximum number of particles then do nothing
	if(particleCount == maxParticles)
		return false;
	
	// Take the next particle out of the particle pool we have created and initialize it
	Particle *particle = &particles[particleCount];
	initParticle( particle );
	
	// Increment the particle count
	particleCount++;
	
	// Return true to show that a particle has been created
	return true;
}
Пример #5
0
// Returns true in case of allocation failure
bool ParticleEngine::initParticles() {
	srandom(time(0));
	unsigned int size = (m_max_part_nbr * sizeof(int)) + // m_active
			(m_max_part_nbr * sizeof(float) * 3) + // m_positions
			(m_max_part_nbr * sizeof(float) * 3) + // m_speed
			(EIGHT_SIZE * sizeof(float) * 2); // m_eightPos
	LOGI("Allocating particles memory : %d kbytes\n", size / 1000);
	m_active = 0;
	m_positions = m_speed = m_eightPos = 0;

	m_active = new (std::nothrow) int[m_max_part_nbr];
	if (!m_active) {
		allocFailed(size);
		return true;
	}
	m_positions = new (std::nothrow) float[m_max_part_nbr * 3];
	if (!m_positions) {
		allocFailed(size);
		return true;
	}
	m_speed = new (std::nothrow) float[m_max_part_nbr * 3];
	if (!m_speed) {
		allocFailed(size);
		return true;
	}
	m_eightPos = new (std::nothrow) float[EIGHT_SIZE * 2];
	if (!m_eightPos) {
		allocFailed(size);
		return true;
	}

	for (uint i = 0; i < m_max_part_nbr; ++i) {
		initParticle(i);
	}
	float R = 3;
	float slices = EIGHT_SIZE / 2.0f;

	for (int i = 0; i < EIGHT_SIZE; ++i) {
		float theta = fmod(i, slices);
		float phi = (((i - phi) / slices) / 1) * (M_PI);
		theta = (theta / EIGHT_SIZE) * (2 * M_PI);
		float u = R * cos(theta);
		m_eightPos[i * 2] = sqrt(R * R - u * u) * cos(phi);
		m_eightPos[i * 2 + 1] = u;
	}
	return false;
}
Пример #6
0
bool init()
{
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
		return false;
	}

	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);

	// Create window
	gWindow = SDL_CreateWindow("ParticleTrails", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
	if (!gWindow)
	{
		printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
		return false;
	}

	// Create context
	gContext = SDL_GL_CreateContext(gWindow);
	if (!gContext)
	{
		printf("OpenGL context could not be created! SDL Error: %s\n", SDL_GetError());
		return false;
	}

	// Use Vsync
	if (SDL_GL_SetSwapInterval(1) < 0)
	{
		printf("Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError());
		return false;
	}

	initEngine();
	initParticle();

	return true;
}
Пример #7
0
static void addParticle(graphics_ParticleSystem *ps, float t) {
  graphics_Particle *p = ps->pFree++;
  initParticle(ps, p, t);

  switch(ps->insertMode) {
  case graphics_ParticleInsertMode_top:
    insertTop(ps, p);
    break;
  case graphics_ParticleInsertMode_bottom:
    insertBottom(ps, p);
    break;
  case graphics_ParticleInsertMode_random:
    insertRandom(ps, p);
    break;
  }

  ++ps->activeParticles;
}
Пример #8
0
void Matrix::step()
{
  for(uint8_t i = 0; i < 10; i++)
  {
    if(particle[i].delay)
    {
      particle[i].delay--;
    }
    else
    {
      particle[i].y--;
      particle[i].delay = particle[i].delaySetting;
      if(particle[i].y < 0)
      {
        initParticle(i);
      }
    }
  }
}
Пример #9
0
Particle* ParticleSystem :: addParticle() { 
	
	Particle * p ;
	
	if(spareParticles.size() > 0 ){
		p = spareParticles.back(); 
		spareParticles.pop_back();
	} else { 
		
		p = new Particle(); 
	
		particles.push_back(p); 
	}
	
	numParticlesCreated++; 
	initParticle(p);
	return p; 

}
Пример #10
0
void ParticleEngine::stepEnginePlatform(int start, int end) {
	// Begin loop
	for (uint i = start; i < end; ++i) {
		if (m_active[i] == 1) {
			POS_X(i) += PSPEED_X(i);
			POS_Y(i) += PSPEED_Y(i);
			POS_Z(i) += PSPEED_Z(i);
			PSPEED_Y(i) -= m_gravity;

			if (POS_Y(i) > LIMIT_Y || POS_Y(i) < -LIMIT_Y || POS_X(i) > LIMIT_X
					|| POS_X(i) < -LIMIT_X || POS_Z(i) > LIMIT_Z
					|| POS_Z(i) < -LIMIT_Z)
				initParticle(i);

			// consider the center of the cone at 0,CONE_TOP_Y,0
			// it was supposed to be a cone....
			if (POS_Y(i) <= CONE_TOP_Y && POS_Y(i) >= CONE_BOT_Y
					&& PSPEED_Y(i) <= 0) {
				// compute distance to the center
				float dist = (POS_X(i) * POS_X(i)) + (POS_Z(i) * POS_Z(i));
				float maxDist = ((CONE_TOP_Y - POS_Y(i)) / CONE_DELTA)
						* CONE_RAD_SQR;
				if (dist <= maxDist) {
					if (m_hasWave && dist <= m_waveDist + WAVE_WIDTH
							&& dist >= m_waveDist - WAVE_WIDTH)
						PSPEED_Y(i) = m_waveIntensity;
					else
						PSPEED_Y(i) = -PSPEED_Y(i) * BOUNCYNESS;
				}
			}
			if (POS_Y(i) <= m_randomCursor - m_randomSpeed
					&& POS_Y(i) >= m_randomCursor + m_randomSpeed)
				setRandomSpeed(i);
		} else if (m_emit) {
			++(m_active[i]);
		}
	}
}
Пример #11
0
Particle* ParticleSystem :: addParticle() { 
	
	Particle * p  = ParticleFactory :: instance()->getParticle(); 
	
	initParticle(p);

	
	
	if(firstParticle == NULL) {
		firstParticle = p;
		lastParticle = p; 
	} else {
		
		lastParticle->next = p;
		p->previous = lastParticle;
		lastParticle = p; 
	}
	
	numParticlesCreated++; 

	return p;

}
Пример #12
0
void ParticleEngine::stepEngineGravity(int start, int end) {
	for (uint i = start; i < end; ++i) {

		if (POS_Y(i) > LIMIT_X || POS_Y(i) < -LIMIT_X || POS_X(i) > LIMIT_X
				|| POS_X(i) < -LIMIT_X || POS_Z(i) > LIMIT_Z
				|| POS_Z(i) < -LIMIT_Z)
			initParticle(i);

		POS_X(i) += PSPEED_X(i);
		POS_Y(i) += PSPEED_Y(i);
		POS_Z(i) += PSPEED_Z(i);

		const float x = POS_X(i);
		const float y = POS_Y(i) + 3;
		const float z = POS_Z(i);

		float distance = x * x + y * y + z * z;

		PSPEED_X(i) -= (.001f / distance) * x;
		PSPEED_Y(i) -= (.001f / distance) * y;
		PSPEED_Z(i) -= (.001f / distance) * z;
	}
}
Пример #13
0
void CParticleSystem::update(float time) {
	totalTime -= time;
	bool act = false;

	for (int loop = 0; loop < numParts; loop++) {
		if (particle[loop].active == false) continue;
		act = true;

		particle[loop].x += particle[loop].xi*time;  // Move On The X Axis By X Speed
		particle[loop].y += particle[loop].yi*time;  // Move On The Y Axis By Y Speed
		particle[loop].z += particle[loop].zi*time;  // Move On The Z Axis By Z Speed

		particle[loop].xi += particle[loop].xg;			// Take Pull On X Axis Into Account
		particle[loop].yi += particle[loop].yg;			// Take Pull On Y Axis Into Account
		particle[loop].zi += particle[loop].zg;			// Take Pull On Z Axis Into Account
		particle[loop].life -= particle[loop].fade*time;// Reduce Particles Life By 'Fade'

		particle[loop].r = (particle[loop].r0*particle[loop].life
							+particle[loop].rf*(1.0f-particle[loop].life));
		particle[loop].g = (particle[loop].g0*particle[loop].life
							+particle[loop].gf*(1.0f-particle[loop].life));
		particle[loop].b = (particle[loop].b0*particle[loop].life
							+particle[loop].bf*(1.0f-particle[loop].life));

		if (particle[loop].life < 0.0f) {
			if ( tipusEmisor == PS_LOOP || 
			    (tipusEmisor == PS_TIMED && totalTime > 0.0f))
			{
				initParticle(loop);
			}
			else particle[loop].active = false;
		}
	}

	actiu = act;
}
Пример #14
0
void ParticleGenerator::render() {   
    glBegin(GL_QUADS);
        for(GLuint i = 0; i < m_max; i++) {   
            Particle& p = m_particles[i];
            if(p.getLife() > 0.0f && p.isActive()) {
                p.setX(p.getX() + p.getXv());
                p.setY(p.getY() + p.getYv());
                p.setZ(p.getZ() + p.getZv());
                
                p.setYv(p.getYv() + p.getGravity());
                
                p.setLife(p.getLife() + p.getFadeSpeed());
                
                // determine color:
                GLfloat percentage = (p.getLife() / m_particleLife) * 100.0f;
                Color c;
                if(percentage >= 70.0f) {
                    c = Color::RED;
                } else if (percentage >= 50.0f && percentage < 70.0f) {
                    c = Color::ORANGE;
                } else if (percentage >= 0.0f && percentage < 50.0f) {
                    c = Color::YELLOW;
                }
                // set alpha value based on percentage of life. Lesser life, 
                // lesser alpha, it will dissapear eventually.
                c.setA(p.getLife() / m_particleLife);
                p.setColor(c);
                
                // last but not least, render the individual particle.
                p.render();
            } else {
                initParticle(p);
            }
        }
    glEnd();
}
Пример #15
0
/**
* Initialize an EGL context for the current display.
*/
static int engine_init_display(struct engine* engine) {
	// initialize OpenGL ES and EGL

	/*
	* Here specify the attributes of the desired configuration.
	* Below, we select an EGLConfig with at least 8 bits per color
	* component compatible with on-screen windows
	*/
	const EGLint attribs[] = {
		EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
		EGL_BLUE_SIZE, 8,
		EGL_GREEN_SIZE, 8,
		EGL_RED_SIZE, 8,
		EGL_ALPHA_SIZE, 8,
		EGL_DEPTH_SIZE, 16,
		EGL_NONE
	};
	EGLint w, h, format;
	EGLint numConfigs;
	EGLConfig config;
	EGLSurface surface;
	EGLContext context;

	EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

	eglInitialize(display, 0, 0);

	/* Here, the application chooses the configuration it desires. In this
	* sample, we have a very simplified selection process, where we pick
	* the first EGLConfig that matches our criteria */
	eglChooseConfig(display, attribs, &config, 1, &numConfigs);

	/* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
	* guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
	* As soon as we picked a EGLConfig, we can safely reconfigure the
	* ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
	eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);

	ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);

	surface = eglCreateWindowSurface(display, config, engine->app->window, NULL);

	const EGLint attribList[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
	context = eglCreateContext(display, config, EGL_NO_CONTEXT, attribList);

	if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
		LOGW("Unable to eglMakeCurrent");
		return -1;
	}

	eglQuerySurface(display, surface, EGL_WIDTH, &w);
	eglQuerySurface(display, surface, EGL_HEIGHT, &h);

	engine->display = display;
	engine->context = context;
	engine->surface = surface;
	engine->width = w;
	engine->height = h;

	initEngine(engine);
	initLights(engine);
	initScene(engine);
	initParticle(engine);

	return 0;
}
Пример #16
0
	bool Group::updateParticles(float deltaTime)
	{
		// Prepares the additionnal data
		prepareAdditionnalData();

		size_t nbAutoBorn = 0;
		size_t nbManualBorn = nbBufferedParticles;

		// Checks the number of born particles
		bool hasAliveEmitters = false;
		activeEmitters.clear();

		for (std::vector<Ref<Emitter> >::const_iterator it = emitters.begin(); it != emitters.end(); ++it)
			if ((*it)->isActive())
			{
				int nb = (*it)->updateTankFromTime(deltaTime);
				if (nb > 0)
				{
					activeEmitters.push_back(WeakEmitterPair(*it,nb));
					nbAutoBorn += nb;
				}

				hasAliveEmitters |= ((*it)->getCurrentTank() != 0); // An emitter with some particles in its tank is still potentially alive
			}

		size_t emitterIndex = 0;
		size_t nbBorn = nbAutoBorn + nbManualBorn;

		// Updates the age of the particles function of the delta time
		for (size_t i = 0; i < particleData.nbParticles; ++i)
			particleData.ages[i] += deltaTime;

		// Computes the energy of the particles (if they are not immortal)
		if (!immortal)
			for (size_t i = 0; i < particleData.nbParticles; ++i)
				particleData.energies[i] = 1.0f - particleData.ages[i] / particleData.lifeTimes[i];

		// Updates the position of particles function of their velocity
		if (!still)
			for (size_t i = 0; i < particleData.nbParticles; ++i)
			{
				particleData.oldPositions[i] = particleData.positions[i];
				particleData.positions[i] += particleData.velocities[i] * deltaTime;
			}

		// Interpolates the parameters
		if (colorInterpolator.obj)
			colorInterpolator.obj->interpolate(particleData.colors,*this,colorInterpolator.dataSet);
		for (size_t i = 0; i < nbEnabledParameters; ++i)
		{
			FloatInterpolatorDef& interpolator = paramInterpolators[enabledParamIndices[i]];
			interpolator.obj->interpolate(particleData.parameters[enabledParamIndices[i]],*this,interpolator.dataSet);
		}

		// Updates the octree if one
		if (octree != NULL)
			octree->update();

		// Modifies the particles with specific active modifiers behavior
		for (std::vector<WeakModifierDef>::const_iterator it = activeModifiers.begin(); it != activeModifiers.end(); ++it)
			it->obj->modify(*this,it->dataSet,deltaTime);

		// Updates the renderer data
		if (renderer.obj)
			renderer.obj->update(*this,renderer.dataSet);

		// Checks dead particles and reinits or swaps
		for (size_t i = 0; i < particleData.nbParticles; ++i)
			if (particleData.energies[i] <= 0.0f)
			{
				// Death action
				if (deathAction && deathAction->isActive())
				{
				    Particle particle = getParticle(i); // fix for gcc
					deathAction->apply(particle);
				}

				bool replaceDeadParticle = false;
				while (!replaceDeadParticle && nbBorn > 0)
				{
					if (initParticle(i,emitterIndex,nbManualBorn))
						replaceDeadParticle = true;
					--nbBorn;
				}

				if (!replaceDeadParticle)
				{
					swapParticles(i,particleData.nbParticles - 1);
					--particleData.nbParticles;
					--i; // As we need to test the swapped particle
				}
			}

		// Emits new particles if some left
		while (nbBorn > 0 && particleData.maxParticles - particleData.nbParticles > 0)
		{
			if (!initParticle(particleData.nbParticles++,emitterIndex,nbManualBorn))
				--particleData.nbParticles;
			--nbBorn;
		}

		// Computes the distance of particles from the camera
		if (distanceComputationEnabled)
		{
			for (size_t i = 0; i < particleData.nbParticles; ++i)
				particleData.sqrDists[i] = getSqrDist(particleData.positions[i],system->getCameraPosition());
		}

		emptyBufferedParticles();

		return hasAliveEmitters || particleData.nbParticles > 0;
	}
Пример #17
0
	void RandomForce::init(Particle& particle,DataSet* dataSet) const
	{
		initParticle(particle,dataSet);
	}