Search in sources :

Example 6 with NodePart

use of com.badlogic.gdx.graphics.g3d.model.NodePart in project bdx by GoranM.

the class GameObject method join.

public void join(HashMap<Mesh, ArrayList<Matrix4f>> map) {
    // Collect transformed vertex arrays for each material & calculate number of indices
    int VERT_STRIDE = Bdx.VERT_STRIDE;
    HashMap<Material, ArrayList<float[]>> tvaMap = new HashMap<Material, ArrayList<float[]>>();
    HashMap<Material, Integer> lenMap = new HashMap<Material, Integer>();
    Mesh m;
    Node node;
    Material mat;
    MeshPart meshPart;
    float[] va, tva;
    int numIndices, numVertices, offset, j, len;
    Vector3f p = new Vector3f();
    Vector3f s = new Vector3f();
    Matrix3f o = new Matrix3f();
    Vector3f vP = new Vector3f();
    Vector3f nP = new Vector3f();
    Vector3f vPT = new Vector3f();
    Vector3f nPT = new Vector3f();
    Vector3f pos = position();
    Vector3f sca = scale();
    Matrix3f oriInv = orientation().inverted();
    for (Map.Entry<Mesh, ArrayList<Matrix4f>> e : map.entrySet()) {
        m = e.getKey();
        node = m.model.nodes.get(0);
        for (Matrix4f t : e.getValue()) {
            t.get(p);
            p.sub(pos);
            p = oriInv.mult(p.div(sca));
            t.getRotationScale(o);
            o = oriInv.mult(o);
            s.set(t.m30, t.m31, t.m32);
            if (s.length() == 0) {
                s.set(1, 1, 1);
            }
            s = s.div(sca);
            for (NodePart nodePart : node.parts) {
                meshPart = nodePart.meshPart;
                numIndices = meshPart.size;
                numVertices = numIndices * VERT_STRIDE;
                offset = meshPart.offset * VERT_STRIDE;
                va = meshPart.mesh.getVertices(offset, numVertices, new float[numVertices]);
                tva = new float[numVertices];
                j = 0;
                for (int i = 0; i < numIndices; i++) {
                    vP.set(va[j], va[j + 1], va[j + 2]);
                    nP.set(va[j + 3], va[j + 4], va[j + 5]);
                    vPT.set(o.mult(vP.mul(s)));
                    vPT.add(p);
                    nPT.set(o.mult(vP.plus(nP)));
                    nPT.sub(o.mult(vP));
                    tva[j] = vPT.x;
                    tva[j + 1] = vPT.y;
                    tva[j + 2] = vPT.z;
                    tva[j + 3] = nPT.x;
                    tva[j + 4] = nPT.y;
                    tva[j + 5] = nPT.z;
                    tva[j + 6] = va[j + 6];
                    tva[j + 7] = va[j + 7];
                    j += VERT_STRIDE;
                }
                mat = m.materials.get(nodePart.material.id);
                ArrayList<float[]> l;
                if (tvaMap.containsKey(mat)) {
                    l = tvaMap.get(mat);
                    len = lenMap.get(mat);
                } else {
                    l = new ArrayList<float[]>();
                    tvaMap.put(mat, l);
                    len = 0;
                }
                l.add(tva);
                lenMap.put(mat, len + tva.length);
            }
        }
    }
    // Build a unique model out of meshparts for each material
    ModelBuilder builder = new ModelBuilder();
    builder.begin();
    short idx = 0;
    MeshPartBuilder mpb;
    for (Map.Entry<Material, ArrayList<float[]>> e : tvaMap.entrySet()) {
        mat = e.getKey();
        len = lenMap.get(mat);
        tva = new float[len];
        j = 0;
        for (float[] verts : e.getValue()) {
            numVertices = verts.length;
            for (int i = 0; i < numVertices; i++) {
                tva[i + j] = verts[i];
            }
            j += numVertices;
        }
        mpb = builder.part(mat.name(), GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.TextureCoordinates, mat);
        mpb.vertex(tva);
        try {
            for (short i = 0; i < len / VERT_STRIDE; i++) {
                mpb.index(idx);
                idx += 1;
            }
        } catch (Error error) {
            throw new RuntimeException("MODEL ERROR: Models with more than 32767 vertices are not supported. Decrease the number of objects to join.");
        }
    }
    Model finishedModel = builder.end();
    // Update mesh
    mesh(new Mesh(finishedModel, scene));
    // Update dimensionsNoScale and origin
    com.badlogic.gdx.graphics.Mesh mesh = finishedModel.meshes.first();
    BoundingBox bbox = mesh.calculateBoundingBox();
    Vector3 dimensions = bbox.getDimensions(new Vector3());
    Vector3 center = bbox.getCenter(new Vector3());
    dimensionsNoScale = new Vector3f(dimensions.x, dimensions.y, dimensions.z);
    origin = new Vector3f(center.x, center.y, center.z);
    // Update body
    updateBody();
    if (json.get("mesh_name").asString() == null) {
        visible = json.get("visible").asBoolean();
    }
}
Also used : HashMap(java.util.HashMap) Node(com.badlogic.gdx.graphics.g3d.model.Node) ArrayList(java.util.ArrayList) MeshPartBuilder(com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder) MeshPart(com.badlogic.gdx.graphics.g3d.model.MeshPart) ModelBuilder(com.badlogic.gdx.graphics.g3d.utils.ModelBuilder) Matrix3f(javax.vecmath.Matrix3f) BoundingBox(com.badlogic.gdx.math.collision.BoundingBox) Mesh(com.nilunder.bdx.gl.Mesh) Material(com.nilunder.bdx.gl.Material) Vector3(com.badlogic.gdx.math.Vector3) ManifoldPoint(com.bulletphysics.collision.narrowphase.ManifoldPoint) Matrix4f(javax.vecmath.Matrix4f) Vector3f(javax.vecmath.Vector3f) Model(com.badlogic.gdx.graphics.g3d.Model) NodePart(com.badlogic.gdx.graphics.g3d.model.NodePart) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

NodePart (com.badlogic.gdx.graphics.g3d.model.NodePart)6 Node (com.badlogic.gdx.graphics.g3d.model.Node)4 Model (com.badlogic.gdx.graphics.g3d.Model)3 MeshPart (com.badlogic.gdx.graphics.g3d.model.MeshPart)2 ModelBuilder (com.badlogic.gdx.graphics.g3d.utils.ModelBuilder)2 Material (com.badlogic.gdx.graphics.g3d.Material)1 Renderable (com.badlogic.gdx.graphics.g3d.Renderable)1 ModelMaterial (com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial)1 ModelMeshPart (com.badlogic.gdx.graphics.g3d.model.data.ModelMeshPart)1 ModelNode (com.badlogic.gdx.graphics.g3d.model.data.ModelNode)1 ModelNodePart (com.badlogic.gdx.graphics.g3d.model.data.ModelNodePart)1 CameraInputController (com.badlogic.gdx.graphics.g3d.utils.CameraInputController)1 DefaultTextureBinder (com.badlogic.gdx.graphics.g3d.utils.DefaultTextureBinder)1 MeshPartBuilder (com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder)1 RenderContext (com.badlogic.gdx.graphics.g3d.utils.RenderContext)1 Matrix4 (com.badlogic.gdx.math.Matrix4)1 Vector3 (com.badlogic.gdx.math.Vector3)1 BoundingBox (com.badlogic.gdx.math.collision.BoundingBox)1 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)1 JsonReader (com.badlogic.gdx.utils.JsonReader)1