Browse Source

* It is now possible to modify an existing buffer through Mesh.setBuffer() (previous behavior was to crash)
* Add TestExpandingTorus to demonstrate this feature

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9208 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

Sha..rd 13 years ago
parent
commit
2cbbfaa3b9

+ 64 - 63
engine/src/core/com/jme3/scene/Mesh.java

@@ -572,6 +572,7 @@ public class Mesh implements Savable, Cloneable {
      * Some GPUs may prefer the data in this format, however it is a good idea
      * Some GPUs may prefer the data in this format, however it is a good idea
      * to <em>avoid</em> using this method as it disables some engine features.
      * to <em>avoid</em> using this method as it disables some engine features.
      */
      */
+    @Deprecated
     public void setInterleaved(){
     public void setInterleaved(){
         ArrayList<VertexBuffer> vbs = new ArrayList<VertexBuffer>();
         ArrayList<VertexBuffer> vbs = new ArrayList<VertexBuffer>();
         for (Entry<VertexBuffer> entry : buffers){
         for (Entry<VertexBuffer> entry : buffers){
@@ -859,6 +860,65 @@ public class Mesh implements Savable, Cloneable {
         return collisionTree.collideWith(other, worldMatrix, worldBound, results);
         return collisionTree.collideWith(other, worldMatrix, worldBound, results);
     }
     }
 
 
+    /**
+     * Sets the {@link VertexBuffer} on the mesh.
+     * This will update the vertex/triangle counts if needed.
+     * 
+     * @param vb The buffer to set
+     * @throws IllegalArgumentException If the buffer type is already set
+     */
+    public void setBuffer(VertexBuffer vb){
+        if (buffers.containsKey(vb.getBufferType().ordinal()))
+            throw new IllegalArgumentException("Buffer type already set: "+vb.getBufferType());
+
+        buffers.put(vb.getBufferType().ordinal(), vb);
+        buffersList.add(vb);
+        updateCounts();
+    }
+    
+    /**
+     * Unsets the {@link VertexBuffer} set on this mesh
+     * with the given type. Does nothing if the vertex buffer type is not set 
+     * initially.
+     * 
+     * @param type The buffer type to remove
+     */
+    public void clearBuffer(VertexBuffer.Type type){
+        VertexBuffer vb = buffers.remove(type.ordinal());
+        if (vb != null){
+            buffersList.remove(vb);
+            updateCounts();
+        }
+    }
+    
+    /**
+     * Creates a {@link VertexBuffer} for the mesh or modifies
+     * the existing one per the parameters given.
+     * 
+     * @param type The type of the buffer
+     * @param components Number of components
+     * @param format Data format
+     * @param buf The buffer data
+     * 
+     * @throws UnsupportedOperationException If the buffer already set is 
+     * incompatible with the parameters given.
+     */
+    public void setBuffer(Type type, int components, Format format, Buffer buf){
+        VertexBuffer vb = buffers.get(type.ordinal());
+        if (vb == null){
+            vb = new VertexBuffer(type);
+            vb.setupData(Usage.Dynamic, components, format, buf);
+            setBuffer(vb);
+        }else{
+            if (vb.getNumComponents() != components || vb.getFormat() != format){
+                throw new UnsupportedOperationException("The buffer already set "
+                        + "is incompatible with the given parameters");
+            }
+            vb.updateData(buf);
+            updateCounts();
+        }
+    }
+    
     /**
     /**
      * Set a floating point {@link VertexBuffer} on the mesh. 
      * Set a floating point {@link VertexBuffer} on the mesh. 
      * 
      * 
@@ -871,21 +931,7 @@ public class Mesh implements Savable, Cloneable {
      * @param buf The floating point data to contain
      * @param buf The floating point data to contain
      */
      */
     public void setBuffer(Type type, int components, FloatBuffer buf) {
     public void setBuffer(Type type, int components, FloatBuffer buf) {
-//        VertexBuffer vb = buffers.get(type);
-        VertexBuffer vb = buffers.get(type.ordinal());
-        if (vb == null){
-            if (buf == null)
-                return;
-
-            vb = new VertexBuffer(type);
-            vb.setupData(Usage.Dynamic, components, Format.Float, buf);
-//            buffers.put(type, vb);
-            buffers.put(type.ordinal(), vb);
-            buffersList.add(vb);
-        }else{
-            vb.setupData(Usage.Dynamic, components, Format.Float, buf);
-        }
-        updateCounts();
+        setBuffer(type, components, Format.Float, buf);
     }
     }
 
 
     public void setBuffer(Type type, int components, float[] buf){
     public void setBuffer(Type type, int components, float[] buf){
@@ -893,14 +939,7 @@ public class Mesh implements Savable, Cloneable {
     }
     }
 
 
     public void setBuffer(Type type, int components, IntBuffer buf) {
     public void setBuffer(Type type, int components, IntBuffer buf) {
-        VertexBuffer vb = buffers.get(type.ordinal());
-        if (vb == null){
-            vb = new VertexBuffer(type);
-            vb.setupData(Usage.Dynamic, components, Format.UnsignedInt, buf);
-            buffers.put(type.ordinal(), vb);
-            buffersList.add(vb);
-            updateCounts();
-        }
+        setBuffer(type, components, Format.UnsignedInt, buf);
     }
     }
 
 
     public void setBuffer(Type type, int components, int[] buf){
     public void setBuffer(Type type, int components, int[] buf){
@@ -908,14 +947,7 @@ public class Mesh implements Savable, Cloneable {
     }
     }
 
 
     public void setBuffer(Type type, int components, ShortBuffer buf) {
     public void setBuffer(Type type, int components, ShortBuffer buf) {
-        VertexBuffer vb = buffers.get(type.ordinal());
-        if (vb == null){
-            vb = new VertexBuffer(type);
-            vb.setupData(Usage.Dynamic, components, Format.UnsignedShort, buf);
-            buffers.put(type.ordinal(), vb);
-            buffersList.add(vb);
-            updateCounts();
-        }
+        setBuffer(type, components, Format.UnsignedShort, buf);
     }
     }
 
 
     public void setBuffer(Type type, int components, byte[] buf){
     public void setBuffer(Type type, int components, byte[] buf){
@@ -923,38 +955,7 @@ public class Mesh implements Savable, Cloneable {
     }
     }
 
 
     public void setBuffer(Type type, int components, ByteBuffer buf) {
     public void setBuffer(Type type, int components, ByteBuffer buf) {
-        VertexBuffer vb = buffers.get(type.ordinal());
-        if (vb == null){
-            vb = new VertexBuffer(type);
-            vb.setupData(Usage.Dynamic, components, Format.UnsignedByte, buf);
-            buffers.put(type.ordinal(), vb);
-            buffersList.add(vb);
-            updateCounts();
-        }
-    }
-
-    public void setBuffer(VertexBuffer vb){
-        if (buffers.containsKey(vb.getBufferType().ordinal()))
-            throw new IllegalArgumentException("Buffer type already set: "+vb.getBufferType());
-
-        buffers.put(vb.getBufferType().ordinal(), vb);
-        buffersList.add(vb);
-        updateCounts();
-    }
-
-    /**
-     * Clears or unsets the {@link VertexBuffer} set on this mesh
-     * with the given type.
-     * Does nothing if the vertex buffer type is not set initially
-     * 
-     * @param type The type to remove
-     */
-    public void clearBuffer(VertexBuffer.Type type){
-        VertexBuffer vb = buffers.remove(type.ordinal());
-        if (vb != null){
-            buffersList.remove(vb);
-            updateCounts();
-        }
+        setBuffer(type, components, Format.UnsignedByte, buf);
     }
     }
 
 
     public void setBuffer(Type type, int components, short[] buf){
     public void setBuffer(Type type, int components, short[] buf){

+ 45 - 0
engine/src/test/jme3test/model/shape/TestExpandingTorus.java

@@ -0,0 +1,45 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package jme3test.model.shape;
+
+import com.jme3.app.SimpleApplication;
+import com.jme3.material.Material;
+import com.jme3.scene.Geometry;
+import com.jme3.scene.shape.Torus;
+
+public class TestExpandingTorus extends SimpleApplication {
+
+    private float outerRadius = 1.5f;
+    private float rate = 1;
+    private Torus torus;
+    private Geometry geom;
+    
+    public static void main(String[] args) {
+        TestExpandingTorus app = new TestExpandingTorus();
+        app.start();
+    }
+
+    @Override
+    public void simpleInitApp() {
+        torus = new Torus(30, 10, .5f, 1f);
+        geom = new Geometry("Torus", torus);
+        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
+        geom.setMaterial(mat);
+        rootNode.attachChild(geom);
+    }
+    
+    @Override
+    public void simpleUpdate(float tpf){
+        if (outerRadius > 2.5f){
+            outerRadius = 2.5f;
+            rate = -rate;
+        }else if (outerRadius < 1f){
+            outerRadius = 1f;
+            rate = -rate;
+        }
+        outerRadius += rate * tpf;
+        torus.updateGeometry(30, 10, .5f, outerRadius);
+    }
+}