From 4fc046a0e1d1c6ba0b07fccf5c57dfb8e872d828 Mon Sep 17 00:00:00 2001 From: "Sha..rd" Date: Sat, 12 May 2012 19:48:49 +0000 Subject: [PATCH] * Particle meshes now call updateCounts() when number of particles changes. This might fix native crashes in some cases. * Removed completely useless merge sort on the entire particle array from ParticleTriMesh ... * Added a small note in the javadoc of VertexBuffer.updateData() regarding bizarre issues git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9380 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../com/jme3/effect/ParticlePointMesh.java | 24 ++++++----- .../core/com/jme3/effect/ParticleTriMesh.java | 42 +++++++++---------- .../src/core/com/jme3/scene/VertexBuffer.java | 6 ++- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/engine/src/core/com/jme3/effect/ParticlePointMesh.java b/engine/src/core/com/jme3/effect/ParticlePointMesh.java index 28db3e6dd..d6e4222e9 100644 --- a/engine/src/core/com/jme3/effect/ParticlePointMesh.java +++ b/engine/src/core/com/jme3/effect/ParticlePointMesh.java @@ -62,53 +62,55 @@ public class ParticlePointMesh extends ParticleMesh { // set positions FloatBuffer pb = BufferUtils.createVector3Buffer(numParticles); - VertexBuffer pvb = new VertexBuffer(VertexBuffer.Type.Position); - pvb.setupData(Usage.Stream, 3, Format.Float, pb); - + //if the buffer is already set only update the data VertexBuffer buf = getBuffer(VertexBuffer.Type.Position); if (buf != null) { buf.updateData(pb); } else { + VertexBuffer pvb = new VertexBuffer(VertexBuffer.Type.Position); + pvb.setupData(Usage.Stream, 3, Format.Float, pb); setBuffer(pvb); } // set colors ByteBuffer cb = BufferUtils.createByteBuffer(numParticles * 4); - VertexBuffer cvb = new VertexBuffer(VertexBuffer.Type.Color); - cvb.setupData(Usage.Stream, 4, Format.UnsignedByte, cb); - cvb.setNormalized(true); buf = getBuffer(VertexBuffer.Type.Color); if (buf != null) { buf.updateData(cb); } else { + VertexBuffer cvb = new VertexBuffer(VertexBuffer.Type.Color); + cvb.setupData(Usage.Stream, 4, Format.UnsignedByte, cb); + cvb.setNormalized(true); setBuffer(cvb); } // set sizes FloatBuffer sb = BufferUtils.createFloatBuffer(numParticles); - VertexBuffer svb = new VertexBuffer(VertexBuffer.Type.Size); - svb.setupData(Usage.Stream, 1, Format.Float, sb); - + buf = getBuffer(VertexBuffer.Type.Size); if (buf != null) { buf.updateData(sb); } else { + VertexBuffer svb = new VertexBuffer(VertexBuffer.Type.Size); + svb.setupData(Usage.Stream, 1, Format.Float, sb); setBuffer(svb); } // set UV-scale FloatBuffer tb = BufferUtils.createFloatBuffer(numParticles*4); - VertexBuffer tvb = new VertexBuffer(VertexBuffer.Type.TexCoord); - tvb.setupData(Usage.Stream, 4, Format.Float, tb); buf = getBuffer(VertexBuffer.Type.TexCoord); if (buf != null) { buf.updateData(tb); } else { + VertexBuffer tvb = new VertexBuffer(VertexBuffer.Type.TexCoord); + tvb.setupData(Usage.Stream, 4, Format.Float, tb); setBuffer(tvb); } + + updateCounts(); } @Override diff --git a/engine/src/core/com/jme3/effect/ParticleTriMesh.java b/engine/src/core/com/jme3/effect/ParticleTriMesh.java index 8d27838da..735a77aed 100644 --- a/engine/src/core/com/jme3/effect/ParticleTriMesh.java +++ b/engine/src/core/com/jme3/effect/ParticleTriMesh.java @@ -40,7 +40,6 @@ import com.jme3.scene.VertexBuffer; import com.jme3.scene.VertexBuffer.Format; import com.jme3.scene.VertexBuffer.Usage; import com.jme3.util.BufferUtils; -import com.jme3.util.SortUtil; import java.nio.ByteBuffer; import java.nio.FloatBuffer; import java.nio.ShortBuffer; @@ -50,9 +49,9 @@ public class ParticleTriMesh extends ParticleMesh { private int imagesX = 1; private int imagesY = 1; private boolean uniqueTexCoords = false; - private ParticleComparator comparator = new ParticleComparator(); +// private ParticleComparator comparator = new ParticleComparator(); private ParticleEmitter emitter; - private Particle[] particlesCopy; +// private Particle[] particlesCopy; @Override public void initParticleData(ParticleEmitter emitter, int numParticles) { @@ -60,38 +59,34 @@ public class ParticleTriMesh extends ParticleMesh { this.emitter = emitter; - particlesCopy = new Particle[numParticles]; +// particlesCopy = new Particle[numParticles]; // set positions FloatBuffer pb = BufferUtils.createVector3Buffer(numParticles * 4); - VertexBuffer pvb = new VertexBuffer(VertexBuffer.Type.Position); - pvb.setupData(Usage.Stream, 3, Format.Float, pb); - - //if the buffer is already set only update the data + // if the buffer is already set only update the data VertexBuffer buf = getBuffer(VertexBuffer.Type.Position); if (buf != null) { buf.updateData(pb); } else { + VertexBuffer pvb = new VertexBuffer(VertexBuffer.Type.Position); + pvb.setupData(Usage.Stream, 3, Format.Float, pb); setBuffer(pvb); } // set colors ByteBuffer cb = BufferUtils.createByteBuffer(numParticles * 4 * 4); - VertexBuffer cvb = new VertexBuffer(VertexBuffer.Type.Color); - cvb.setupData(Usage.Stream, 4, Format.UnsignedByte, cb); - cvb.setNormalized(true); - buf = getBuffer(VertexBuffer.Type.Color); if (buf != null) { buf.updateData(cb); } else { + VertexBuffer cvb = new VertexBuffer(VertexBuffer.Type.Color); + cvb.setupData(Usage.Stream, 4, Format.UnsignedByte, cb); + cvb.setNormalized(true); setBuffer(cvb); } // set texcoords - VertexBuffer tvb = new VertexBuffer(VertexBuffer.Type.TexCoord); FloatBuffer tb = BufferUtils.createVector2Buffer(numParticles * 4); - uniqueTexCoords = false; for (int i = 0; i < numParticles; i++){ tb.put(0f).put(1f); @@ -100,12 +95,13 @@ public class ParticleTriMesh extends ParticleMesh { tb.put(1f).put(0f); } tb.flip(); - tvb.setupData(Usage.Static, 2, Format.Float, tb); buf = getBuffer(VertexBuffer.Type.TexCoord); if (buf != null) { buf.updateData(tb); } else { + VertexBuffer tvb = new VertexBuffer(VertexBuffer.Type.TexCoord); + tvb.setupData(Usage.Static, 2, Format.Float, tb); setBuffer(tvb); } @@ -125,17 +121,17 @@ public class ParticleTriMesh extends ParticleMesh { .put((short)(startIdx + 3)); } ib.flip(); - - VertexBuffer ivb = new VertexBuffer(VertexBuffer.Type.Index); - ivb.setupData(Usage.Static, 3, Format.UnsignedShort, ib); - + buf = getBuffer(VertexBuffer.Type.Index); if (buf != null) { buf.updateData(ib); } else { + VertexBuffer ivb = new VertexBuffer(VertexBuffer.Type.Index); + ivb.setupData(Usage.Static, 3, Format.UnsignedShort, ib); setBuffer(ivb); } + updateCounts(); } @Override @@ -150,12 +146,12 @@ public class ParticleTriMesh extends ParticleMesh { @Override public void updateParticleData(Particle[] particles, Camera cam, Matrix3f inverseRotation) { - System.arraycopy(particles, 0, particlesCopy, 0, particlesCopy.length); - comparator.setCamera(cam); +// System.arraycopy(particles, 0, particlesCopy, 0, particlesCopy.length); +// comparator.setCamera(cam); // Arrays.sort(particlesCopy, comparator); // SortUtil.qsort(particlesCopy, comparator); - SortUtil.msort(particles, particlesCopy, comparator); - particles = particlesCopy; +// SortUtil.msort(particles, particlesCopy, comparator); +// particles = particlesCopy; VertexBuffer pvb = getBuffer(VertexBuffer.Type.Position); FloatBuffer positions = (FloatBuffer) pvb.getData(); diff --git a/engine/src/core/com/jme3/scene/VertexBuffer.java b/engine/src/core/com/jme3/scene/VertexBuffer.java index 1a844bb51..64a75b495 100644 --- a/engine/src/core/com/jme3/scene/VertexBuffer.java +++ b/engine/src/core/com/jme3/scene/VertexBuffer.java @@ -542,9 +542,11 @@ public class VertexBuffer extends NativeObject implements Savable, Cloneable { * data already set, e.g. vb.updateData(vb.getData()), this will just * set the proper update flag indicating the data should be sent to the GPU * again. + *

* It is allowed to specify a buffer with different capacity than the - * originally set buffer. - * + * originally set buffer, HOWEVER, if you do so, you must + * call Mesh.updateCounts() otherwise bizarre errors can occur. + * * @param data The data buffer to set */ public void updateData(Buffer data){