void ParticleManager::loadParticleBuffer(int numParticles, int iniRadius){
	glGetError();

	GLuint bufferID;

	glGenBuffers(1, &bufferID);
	glBindBuffer(GL_SHADER_STORAGE_BUFFER, bufferID);
	glBufferData(GL_SHADER_STORAGE_BUFFER, numParticles*sizeof(Particle), NULL ,GL_STATIC_DRAW);

	if(glGetError() != GL_NO_ERROR){
		throw std::exception("ERROR: Could not generate or bind shader storage buffer for particles!");
	}

	_bufferData.saveBuffer("particleBuffer", bufferID);

	struct Particle* particles;

	try{
		particles = (struct Particle*) glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, numParticles*sizeof(Particle), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
	} catch(std::runtime_error e){
		std::cout << e.what() <<std::endl;
	}

	if(glGetError() != GL_NO_ERROR || particles == NULL){
		throw std::exception("ERROR: Could not map the shader storage buffer for particles!");
	}

	setParticles(particles, numParticles, iniRadius);

	if(glUnmapBuffer(GL_SHADER_STORAGE_BUFFER) == GL_FALSE){
		throw std::exception("ERROR: Could not unmap the shader storage buffer for particles!");
	}

}
Esempio n. 2
0
File: main.c Progetto: nfaralli/SPH
int main(int argc, char *argv[]){

  int    nbParts=20000;
  float  minMax[3][2]={{-1,1},{-1,1},{-1,1}};
  float  cellSize=2*0.02*1.001;// 2*particle radius + 0.1%
  float  dt=0.002,simTime=5;
  float  dtFrame=0.02; //0.04 <=> 25 frames per seconds
  int    nbDtPerFrame,i;
  char   filename[]="toto.par";
  float  mass;
  System *sys;
  clock_t time;

/*
  FILE *file;
  float r[3];

  file=fopen("filter.plt","w");
  fprintf(file,"VARIABLES = x w\n");
  r[1]=r[2]=0;
  for(i=0;i<=100;i++){
    r[0]=i/100.;
    fprintf(file,"%e\t%e\n",i/100.,kernelPoly6(r,1));
  }
  fclose(file);
  return 0;*/


  nbDtPerFrame=dtFrame/dt;
  sys=createSystem(nbParts,minMax);
  tmpAddWalls_1(sys);
  setGrid(sys, cellSize);
  //printGrid(sys); //test nico
  //return 0;
  mass=1000*(minMax[0][1]-minMax[0][0])*(minMax[1][1]-minMax[1][0])*(minMax[2][1]-minMax[2][0])/nbParts;
  setParticles(sys,0,nbParts-1,mass/4); //for water, ro0=1000kg/m3, so 1000 parts/Liter => mi=1e-3kg
  setForces(sys);
  generateParFile(sys,filename,CREATE);

  i=0;
  time=clock();
  while(sys->time<=simTime){
    printf("time = %e (i=%d)\n",sys->time,i);fflush(stdout);
    updateSys(sys,dt);
    if((++i)%nbDtPerFrame==0)
      generateParFile(sys,filename,UPDATE);
  }
  time=clock()-time;

  generateParFile(sys,filename,CLOSE);
  deleteSystem(sys);

  updateSys(NULL,0);
#ifdef USE_MULTI_THREADING
  cleanThreads();
#endif
  printf("done in %f sec!\n", (double)(time)/CLOCKS_PER_SEC);
  return 0;
}