• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.badlogic.gdx.graphics.g3d.particles.batches;
2 
3 import com.badlogic.gdx.graphics.Camera;
4 import com.badlogic.gdx.graphics.g3d.particles.ParticleSorter;
5 import com.badlogic.gdx.graphics.g3d.particles.renderers.ParticleControllerRenderData;
6 import com.badlogic.gdx.utils.Array;
7 
8 /** Base class of all the batches requiring to buffer {@link ParticleControllerRenderData}
9 * @author Inferno */
10 public abstract class BufferedParticleBatch<T extends ParticleControllerRenderData> implements ParticleBatch<T> {
11 	protected Array<T> renderData;
12 	protected int bufferedParticlesCount, currentCapacity = 0;
13 	protected ParticleSorter sorter;
14 	protected Camera camera;
15 
BufferedParticleBatch(Class<T> type)16 	protected BufferedParticleBatch(Class<T> type){
17 		this.sorter = new ParticleSorter.Distance();
18 		renderData = new com.badlogic.gdx.utils.Array<T>(false, 10, type);
19 	}
20 
begin()21 	public void begin(){
22 		renderData.clear();
23 		bufferedParticlesCount = 0;
24 	}
25 
26 	@Override
draw(T data)27 	public void draw (T data) {
28 		if(data.controller.particles.size > 0){
29 			renderData.add(data);
30 			bufferedParticlesCount += data.controller.particles.size;
31 		}
32 	}
33 
34 	/** */
end()35 	public void end(){
36 		if(bufferedParticlesCount > 0){
37 			ensureCapacity(bufferedParticlesCount);
38 			flush(sorter.sort(renderData));
39 		}
40 	}
41 
42 	/**Ensure the batch can contain the passed in amount of particles*/
ensureCapacity(int capacity)43 	public void ensureCapacity(int capacity){
44 		if(currentCapacity >= capacity) return;
45 		sorter.ensureCapacity(capacity);
46 		allocParticlesData(capacity);
47 		currentCapacity = capacity;
48 	}
49 
resetCapacity()50 	public void resetCapacity(){
51 		currentCapacity = bufferedParticlesCount = 0;
52 	}
53 
allocParticlesData(int capacity)54 	protected abstract void allocParticlesData(int capacity);
55 
setCamera(Camera camera)56 	public void setCamera(Camera camera){
57 		this.camera = camera;
58 		sorter.setCamera(camera);
59 	}
60 
getSorter()61 	public ParticleSorter getSorter(){
62 		return sorter;
63 	}
64 
setSorter(ParticleSorter sorter)65 	public void setSorter(ParticleSorter sorter){
66 		this.sorter = sorter;
67 		sorter.setCamera(camera);
68 		sorter.ensureCapacity(currentCapacity);
69 	}
70 
71 	/** Sends the data to the gpu.
72 	 * This method must use the calculated offsets to build the particles meshes.
73 	 * The offsets represent the position at which a particle should be placed into the vertex array.
74 	 * @param offsets the calculated offsets */
flush(int[] offsets)75 	protected abstract void flush(int[] offsets);
76 
getBufferedCount()77 	public int getBufferedCount () {
78 		return bufferedParticlesCount;
79 	}
80 }
81