OSDN Git Service

Remove multiple model support, static instance and add begin/end checking
authorXoppa <contact@xoppa.nl>
Tue, 26 Mar 2013 22:39:46 +0000 (23:39 +0100)
committerXoppa <contact@xoppa.nl>
Tue, 26 Mar 2013 22:39:46 +0000 (23:39 +0100)
gdx/src/com/badlogic/gdx/graphics/g3d/utils/MeshBuilder.java
gdx/src/com/badlogic/gdx/graphics/g3d/utils/ModelBuilder.java
tests/gdx-tests/src/com/badlogic/gdx/tests/bullet/BaseBulletTest.java
tests/gdx-tests/src/com/badlogic/gdx/tests/bullet/ConstraintsTest.java
tests/gdx-tests/src/com/badlogic/gdx/tests/bullet/RayPickRagdollTest.java

index 1f8af38..eb541d9 100644 (file)
@@ -72,6 +72,8 @@ public class MeshBuilder implements MeshPartBuilder {
        
        /** Begin building a mesh */
        public void begin(final VertexAttributes attributes) {
+               if (this.attributes != null)
+                       throw new RuntimeException("Call end() first");
                this.attributes = attributes;
                this.vertices.clear();
                this.indices.clear();
@@ -105,6 +107,8 @@ public class MeshBuilder implements MeshPartBuilder {
        
        /** Starts a new MeshPart. The mesh part is not usable until end() is called */
        public MeshPart part(final String id) {
+               if (this.attributes == null)
+                       throw new RuntimeException("Call begin() first");
                endpart();
                
                part = new MeshPart();
@@ -116,6 +120,8 @@ public class MeshBuilder implements MeshPartBuilder {
        
        /** End building the mesh and results the mesh */
        public Mesh end() {
+               if (this.attributes == null)
+                       throw new RuntimeException("Call begin() first");
                endpart();
                
                final Mesh mesh = new Mesh(true, vertices.size, indices.size, attributes);
index bb3758a..8543dfb 100644 (file)
@@ -27,8 +27,6 @@ public class ModelBuilder {
        private Model model;
        /** The node currently being build */
        private Node node;
-       /** The model created between begin and end */
-       private Array<Model> models = new Array<Model>();
        /** The mesh builders created between begin and end */
        private Array<MeshBuilder> builders = new Array<MeshBuilder>();
        
@@ -44,56 +42,35 @@ public class ModelBuilder {
        
        /** Begin builder models */
        public void begin() {
+               if (model != null)
+                       throw new GdxRuntimeException("Call end() first");
                node = null;
-               model = null;
-               models.clear();
+               model = new Model();
                builders.clear();
        }
        
        /** End building model(s) */
        public Model end() {
+               if (model == null)
+                       throw new GdxRuntimeException("Call begin() first");
                final Model result = model;
-               endmodel();
+               endnode();
+               model = null;
                
-               Gdx.app.log("Test", "Builders: "+builders.size);
                for (final MeshBuilder mb : builders)
                        mb.end();
                builders.clear();
                
-               Gdx.app.log("Test", "model = "+models.size);
-               for (final Model m : models) {
-                       for (final MeshPart mp : m.meshParts) {
-                               if (!m.meshes.contains(mp.mesh, true))
-                                       m.meshes.add(mp.mesh);
-                       }
-                       Gdx.app.log("Test", "meshparts = "+m.meshParts.size+" meshes = "+m.meshes.size);
+               for (final MeshPart mp : result.meshParts) {
+                       if (!result.meshes.contains(mp.mesh, true))
+                               result.meshes.add(mp.mesh);
                }
-               models.clear();
                return result;
        }
-       
-       /** Start building a new model, the model is not usable until the call to end().
-        * Call node() after this method to start building this model */
-       public Model model() {
-               endmodel();
-               
-               model = new Model();
-               models.add(model);
-               return model;
-       }
-       
-       private void endmodel() {
-               if (model != null) {
-                       endnode();
-                       
-                       model = null;
-               }
-       }
 
        public Node node() {
-               // Allow to add nodes rights after the call to begin() for building just one model
                if (model == null)
-                       model();
+                       throw new GdxRuntimeException("Call begin() first");
                
                endnode();
                
@@ -129,42 +106,32 @@ public class ModelBuilder {
                part(builder.part(id), material);
                return builder;
        }
-
-       protected static ModelBuilder instance;
-       public static ModelBuilder getInstance() {
-               if (instance == null)
-                       instance = new ModelBuilder();
-               return instance;
-       }
        
-       public static Model createBox(float width, float height, float depth, final NewMaterial material, final VertexAttributes attributes) {
-               ModelBuilder mb = getInstance();
-               mb.begin();
-               mb.part("box", attributes, material).box(width, height, depth);
-               return mb.end();
+       public Model createBox(float width, float height, float depth, final NewMaterial material, final VertexAttributes attributes) {
+               begin();
+               part("box", attributes, material).box(width, height, depth);
+               return end();
        }
        
-       public static Model createRect(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4, float normalX, float normalY, float normalZ, final NewMaterial material, final VertexAttributes attributes) {
-               ModelBuilder mb = getInstance();
-               mb.begin();
-               mb.part("rect", attributes, material).rect(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, normalX, normalY, normalZ);
-               return mb.end();
+       public Model createRect(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4, float normalX, float normalY, float normalZ, final NewMaterial material, final VertexAttributes attributes) {
+               begin();
+               part("rect", attributes, material).rect(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, normalX, normalY, normalZ);
+               return end();
        }
 
        // FIXME: Add transform
-       public static Model createCylinder(float width, float height, float depth, int divisions, final NewMaterial material, final VertexAttributes attributes) {
-               ModelBuilder mb = getInstance();
-               mb.begin();
-               mb.part("cylinder", attributes, material).cylinder(width, height, depth, divisions);
-               return mb.end();
+       public Model createCylinder(float width, float height, float depth, int divisions, final NewMaterial material, final VertexAttributes attributes) {
+               begin();
+               part("cylinder", attributes, material).cylinder(width, height, depth, divisions);
+               return end();
        }
        
        // Old code below this line, as for now still useful for testing. 
-       
+       @Deprecated
        public static Model createFromMesh(final Mesh mesh, int primitiveType, final NewMaterial material) {
                return createFromMesh(mesh, 0, mesh.getNumIndices(), primitiveType, material);
        }
-       
+       @Deprecated
        public static Model createFromMesh(final Mesh mesh, int indexOffset, int vertexCount, int primitiveType, final NewMaterial material) {
                Model result = new Model();
                MeshPart meshPart = new MeshPart();
@@ -187,74 +154,11 @@ public class ModelBuilder {
                result.meshParts.add(meshPart);
                return result;
        }
-       
+       @Deprecated
        public static Model createFromMesh(final float[] vertices, final VertexAttribute[] attributes, final short[] indices, int primitiveType, final NewMaterial material) {
                final Mesh mesh = new Mesh(false, vertices.length, indices.length, attributes);
                mesh.setVertices(vertices);
                mesh.setIndices(indices);
                return createFromMesh(mesh, 0, indices.length, primitiveType, material);
        }
-       
-       // along Y axis
-       /*
-       public static Mesh createCylinderMesh(float width, float height, float depth, int divisions) {
-               final int stride = 6;
-               final float hw = width * 0.5f;
-               final float hh = height * 0.5f;
-               final float hd = depth * 0.5f;
-               final float step = MathUtils.PI2 / divisions;
-               for (int i = 0; i < divisions; i++) {
-                       final float angle = step * i;
-                       vectorArray.add(vectorPool.obtain().set(MathUtils.cos(angle) * hw, 0f, MathUtils.sin(angle) * hd));
-               }
-               final float[] vertices = new float[divisions * 4 * stride];
-               final short[] indices = new short[divisions * 6];
-               int voffset = 0;
-               int ioffset = 0;
-               for (int i = 0; i < divisions; i++) {
-                       final Vector3 v1 = vectorArray.get(i);
-                       final Vector3 v2 = vectorArray.get((i+1)%divisions);
-                       final Vector3 n = tempV1.set(v1).lerp(v2, 0.5f).nor();
-                       vertices[voffset++] = v1.x;
-                       vertices[voffset++] = -hh;
-                       vertices[voffset++] = v1.z;
-                       vertices[voffset++] = n.x;
-                       vertices[voffset++] = n.y;
-                       vertices[voffset++] = n.z;
-                       
-                       vertices[voffset++] = v2.x;
-                       vertices[voffset++] = -hh;
-                       vertices[voffset++] = v2.z;
-                       vertices[voffset++] = n.x;
-                       vertices[voffset++] = n.y;
-                       vertices[voffset++] = n.z;
-                       
-                       vertices[voffset++] = v1.x;
-                       vertices[voffset++] = hh;
-                       vertices[voffset++] = v1.z;
-                       vertices[voffset++] = n.x;
-                       vertices[voffset++] = n.y;
-                       vertices[voffset++] = n.z;
-                       
-                       vertices[voffset++] = v2.x;
-                       vertices[voffset++] = hh;
-                       vertices[voffset++] = v2.z;
-                       vertices[voffset++] = n.x;
-                       vertices[voffset++] = n.y;
-                       vertices[voffset++] = n.z;
-                       
-                       indices[ioffset++] = (short)(i * 4);
-                       indices[ioffset++] = (short)(i * 4 + 1);
-                       indices[ioffset++] = (short)(i * 4 + 2);
-                       indices[ioffset++] = (short)(i * 4 + 1);
-                       indices[ioffset++] = (short)(i * 4 + 2);
-                       indices[ioffset++] = (short)(i * 4 + 3);
-               }
-               vectorPool.freeAll(vectorArray);
-               vectorArray.clear();
-               final Mesh result = new Mesh(true, vertices.length, indices.length, new VertexAttribute(Usage.Position, 3, "a_position"), new VertexAttribute(Usage.Normal, 3, "a_normal"));
-               result.setVertices(vertices);
-               result.setIndices(indices);
-               return result;
-       }*/
 }
index ffa0749..07da529 100644 (file)
@@ -76,6 +76,7 @@ public class BaseBulletTest extends BulletTest {
        public PerspectiveCamera camera;
        public BulletWorld world;
        public ObjLoader objLoader = new ObjLoader();
+       public ModelBuilder modelBuilder = new ModelBuilder();
        public ModelBatch modelBatch;
                        
        public BulletWorld createWorld() {
@@ -108,27 +109,8 @@ public class BaseBulletTest extends BulletTest {
                camera.update();
                
                // Create some simple meshes
-               final Model groundModel = ModelBuilder.createRect(20f, 0f, 20f, 20f, 0f, -20f, -20f, 0f, 20f, -20f, 0f, -20f, 0, 1, 0, new NewMaterial(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE)), new VertexAttributes(new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE))); 
-//                     createSimpleModel(new VertexAttribute[] { new VertexAttribute(Usage.Position, 3, "a_position") },
-//                     new float[] {20f, 0f, 20f, 20f, 0f, -20f, -20f, 0f, 20f, -20f, 0f, -20f},
-//                     new short[] {0, 1, 2, 1, 2, 3}); 
-
-               //ModelBuilder b = new ModelBuilder(24, 36, new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE));
-               //b.begin();
-               //b.box(1f, 1f, 1f);
-               //final Model boxModel = b.end(new NewMaterial(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE)));
-               final Model boxModel = ModelBuilder.createBox(1f, 1f, 1f, new NewMaterial(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE)), new VertexAttributes(new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE))); 
-                       
-               /*      createSimpleModel(new VertexAttribute[] { new VertexAttribute(Usage.Position, 3, "a_position") },
-                       new float[] {0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f,
-                                               0.5f, -0.5f, 0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f},
-                       new short[] {0, 1, 2, 1, 2, 3, // top
-                                               4, 5, 6, 5, 6, 7, // bottom
-                                               0, 2, 4, 4, 6, 2, // front
-                                               1, 3, 5, 5, 7, 3, // back
-                                               2, 3, 6, 6, 7, 3, // left
-                                               0, 1, 4, 4, 5, 1 // right
-                               });*/
+               final Model groundModel = modelBuilder.createRect(20f, 0f, 20f, 20f, 0f, -20f, -20f, 0f, 20f, -20f, 0f, -20f, 0, 1, 0, new NewMaterial(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE)), new VertexAttributes(new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE)));
+               final Model boxModel = modelBuilder.createBox(1f, 1f, 1f, new NewMaterial(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE)), new VertexAttributes(new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE))); 
 
                // Add the constructors
                world.addConstructor("ground", new BulletConstructor(groundModel, 0f)); // mass = 0: static body
index b2080e0..59b31d5 100644 (file)
@@ -44,7 +44,7 @@ public class ConstraintsTest extends BaseBulletTest {
        public void create () {
                super.create();
 
-               final Model barModel = ModelBuilder.createBox(10f, 1f, 1f, new NewMaterial(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE)), new VertexAttributes(new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE))); 
+               final Model barModel = modelBuilder.createBox(10f, 1f, 1f, new NewMaterial(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE)), new VertexAttributes(new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE))); 
 /*                     createSimpleModel(new VertexAttribute[] { new VertexAttribute(Usage.Position, 3, "a_position")},
                        new float[] {5f, 0.5f, 0.5f, 5f, 0.5f, -0.5f, -5f, 0.5f, 0.5f, -5f, 0.5f, -0.5f,
                                5f, -0.5f, 0.5f, 5f, -0.5f, -0.5f, -5f, -0.5f, 0.5f, -5f, -0.5f, -0.5f},
index 76f5dd5..368a52c 100644 (file)
@@ -256,7 +256,7 @@ public class RayPickRagdollTest extends BaseBulletTest {
        protected Model createCapsuleModel(float radius, float height) {
                final float hh = radius + 0.5f * height;
                // return ModelBuilder
-               return ModelBuilder.createCylinder(radius * 2, hh * 2f, radius * 2f, 16, new NewMaterial(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE)), new VertexAttributes(new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE)));
+               return modelBuilder.createCylinder(radius * 2, hh * 2f, radius * 2f, 16, new NewMaterial(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE)), new VertexAttributes(new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE)));
                // return ModelBuilder.createBox(radius*2f, hh*2f, radius*2f, new NewMaterial(new ColorAttribute(ColorAttribute.Diffuse, Color.WHITE)));
        }
 }