resourceLibrary.put(geom.getname().toString() + "VertMap", vertMap); TriMesh out = new TriMesh(geom.getname().toString());
resourceLibrary.put(geom.getid().toString() + "VertMap", vertMap); TriMesh out = new TriMesh(geom.getid().toString());
    private TriMesh processPolygonMesh(meshType mesh, geometryType geom)            throws Exception {        HashMap<Integer, ArrayList<BatchVertPair>> vertMap = new HashMap<Integer, ArrayList<BatchVertPair>>();        resourceLibrary.put(geom.getname().toString() + "VertMap", vertMap);        TriMesh out = new TriMesh(geom.getname().toString());        TriangleBatch[] batch = new TriangleBatch[mesh.getpolygonsCount()];        for (int batchIndex = 0; batchIndex < batch.length; batchIndex++) {            polygonsType poly = mesh.getpolygonsAt(batchIndex);            if (batchIndex > 0) {                batch[batchIndex] = new TriangleBatch();                out.addBatch(batch[batchIndex]);            }            out.getBatch(batchIndex).setName(                    poly.getmaterial().toString() + " Batch");            // first set the appropriate materials to this mesh.            String matKey = (String) resourceLibrary.get(poly.getmaterial()                    .toString());            ColladaMaterial cm = (ColladaMaterial) resourceLibrary.get(matKey);            if (cm != null) {                for (int i = 0; i < RenderState.RS_MAX_STATE; i++) {                    if (cm.getState(i) != null) {                        if (cm.getState(i).getType() == RenderState.RS_ALPHA) {                            out.getBatch(batchIndex).setRenderQueueMode(                                    Renderer.QUEUE_TRANSPARENT);                        }                        if (batch.length > 1) {                            out.getBatch(batchIndex).setRenderState(                                    cm.getState(i));                        } else {                            out.setRenderState(cm.getState(i));                        }                    }                }            }            // build the index buffer, this is going to be easy as it's only            // 0...N where N is the number of vertices in the model.            IntBuffer indexBuffer = BufferUtils.createIntBuffer(Integer                    .parseInt(poly.getcount().toString()) * 3);            for (int i = 0; i < indexBuffer.capacity(); i++) {                indexBuffer.put(i);            }            out.setIndexBuffer(batchIndex, indexBuffer);            // find the maximum offset to understand the stride            int maxOffset = -1;            for (int i = 0; i < poly.getinputCount(); i++) {                int temp = Integer.parseInt(poly.getinputAt(i).getoffset()                        .toString());                if (maxOffset < temp) {                    maxOffset = temp;                }            }            int stride = maxOffset + 1;            // next build the other buffers, based on the input semantic            for (int i = 0; i < poly.getinputCount(); i++) {                if ("VERTEX".equals(poly.getinputAt(i).getsemantic().toString())) {                    // build the vertex buffer                    String key = poly.getinputAt(i).getsource().getValue()                            .toString();                    if (key.startsWith("#")) {                        key = key.substring(1);                    }                    Object data = resourceLibrary.get(key);                    while (data instanceof String) {                        key = (String) data;                        if (key.startsWith("#")) {                            key = key.substring(1);                        }                        data = resourceLibrary.get(key);                    }                    if (data == null) {                        ErrorManager.getInstance().addError(Level.WARNING,                                "Invalid source: " + key);                        continue;                    }                    Vector3f[] v = (Vector3f[]) data;                    StringTokenizer st = new StringTokenizer(poly.getp()                            .getValue());                    int vertCount = Integer.parseInt(poly.getcount().toString()) * stride;                    FloatBuffer vertBuffer = BufferUtils                            .createVector3Buffer(vertCount);                    out.getBatch(batchIndex).setVertexCount(vertCount);                    for (int j = 0; j < vertCount; j++) {                        if ( j % stride == 0 ) {                            st = new StringTokenizer(poly.getpAt(j/stride).getValue());                        }                        String token = st.nextToken();                        // need to store the index in p to what j is for later                        // processing the index to the vert for bones                        int vertKey = Integer.parseInt(token);                        ArrayList<BatchVertPair> storage = vertMap.get(Integer                                .valueOf(vertKey));                        if (storage == null) {                            storage = new ArrayList<BatchVertPair>();                            storage.add(new BatchVertPair(batchIndex, j));                            vertMap.put(Integer.valueOf(vertKey), storage);                        } else {                            storage.add(new BatchVertPair(batchIndex, j));                        }                        BufferUtils.setInBuffer(v[vertKey], vertBuffer, j);                        for (int k = 0; k < maxOffset; k++) {                            st.nextToken();                        }                    }                    out.setVertexBuffer(batchIndex, vertBuffer);                } else if ("NORMAL".equals(poly.getinputAt(i).getsemantic()                        .toString())) {                    // build the normal buffer                    String key = poly.getinputAt(i).getsource().getValue()                            .toString();                    if (key.startsWith("#")) {                        key = key.substring(1);                    }                    Object data = resourceLibrary.get(key);                    while (data instanceof String) {                        key = (String) data;                        if (key.startsWith("#")) {                            key = key.substring(1);                        }                        data = resourceLibrary.get(key);                    }                    if (data == null) {                        ErrorManager.getInstance().addError(Level.WARNING,                                "Invalid source: " + key);                        continue;                    }                    Vector3f[] v = (Vector3f[]) data;                    StringTokenizer st = new StringTokenizer(poly.getp()                            .getValue());                    int normCount = Integer.parseInt(poly.getcount().toString()) * stride;                    FloatBuffer normBuffer = BufferUtils                            .createVector3Buffer(normCount);                    int offset = Integer.parseInt(poly.getinputAt(i).getoffset()                            .toString());                    String token;                    for (int j = offset; j < normCount; j++) {                        if ( j % stride == 0 ) {                            st = new StringTokenizer(poly.getpAt(j/stride).getValue());                            for (int k = 0; k < offset; k++) {                                if (st.hasMoreTokens()) {                                    st.nextToken();                                }                            }                        }                        token = st.nextToken();                        int index = Integer.parseInt(token);                        if (index < v.length)                            BufferUtils.setInBuffer(v[index], normBuffer, j);                        for (int k = 0; k < maxOffset; k++) {                            if (st.hasMoreTokens()) {                                st.nextToken();                            }                        }                    }                    out.setNormalBuffer(batchIndex, normBuffer);                } else if ("TEXCOORD".equals(poly.getinputAt(i).getsemantic()                        .toString())) {                    // build the texture buffer                    String key = poly.getinputAt(i).getsource().getValue()                            .toString();                    if (key.startsWith("#")) {                        key = key.substring(1);                    }                    Object data = resourceLibrary.get(key);                    while (data instanceof String) {                        key = (String) data;                        if (key.startsWith("#")) {                            key = key.substring(1);                        }                        data = resourceLibrary.get(key);                    }                    if (data == null) {                        ErrorManager.getInstance().addError(Level.WARNING,                                "Invalid source: " + key);                        continue;                    }                    Vector3f[] v = (Vector3f[]) data;                    StringTokenizer st = new StringTokenizer(poly.getp()                            .getValue());                    int texCount = Integer.parseInt(poly.getcount().toString()) * stride;                    FloatBuffer texBuffer = BufferUtils                            .createVector2Buffer(texCount);                    int offset = Integer.parseInt(poly.getinputAt(i).getoffset()                            .toString());                    int set = Integer.parseInt(poly.getinputAt(i).getset()                            .toString());                    // Keep a max to set the wrap mode (if it's 1, clamp, if                    // it's > 1 wrap it)                    float maxX = -1;                    float maxY = -1;                    Vector2f tempTexCoord = new Vector2f();                    for (int j = 0; j < texCount; j++) {                        if ( j % stride == 0 ) {                            st = new StringTokenizer(poly.getpAt(j/stride).getValue());                            for (int k = 0; k < offset; k++) {                                if (st.hasMoreTokens()) {                                    st.nextToken();                                }                            }                        }                        int index = Integer.parseInt(st.nextToken());                        Vector3f value = v[index];                        if (value.x > maxX) {                            maxX = value.x;                        }                        if (value.y > maxY) {                            maxY = value.y;                        }                        tempTexCoord.set(value.x, value.y);                        BufferUtils.setInBuffer(tempTexCoord, texBuffer, j);                        for (int k = 0; k < maxOffset; k++) {                            if (st.hasMoreTokens()) {                                st.nextToken();                            }                        }                    }                    int unit;                    if (set == 0) {                        unit = 0;                    } else {                        unit = set - 1;                    }                    out.setTextureBuffer(batchIndex, texBuffer, unit);                    // Set the wrap mode, check if the batch has a texture                    // first, if not                    // check the geometry.                    // Then, based on the texture coordinates, we may need to                    // change it from the                    // default.                    TextureState ts = (TextureState) out.getBatch(batchIndex)                            .getRenderState(RenderState.RS_TEXTURE);                    if (ts == null) {                        ts = (TextureState) out                                .getRenderState(RenderState.RS_TEXTURE);                    }                    if (ts != null) {                        Texture t = ts.getTexture(unit);                        if (t != null) {                            if (maxX > 1) {                                if (maxY > 1) {                                    t.setWrap(Texture.WM_WRAP_S_WRAP_T);                                } else {                                    t.setWrap(Texture.WM_WRAP_S_CLAMP_T);                                }                            } else if (maxY > 1) {                                t.setWrap(Texture.WM_CLAMP_S_WRAP_T);                            }                        }                    }                } else if ("COLOR".equals(poly.getinputAt(i).getsemantic()                        .toString())) {                    // build the texture buffer                    String key = poly.getinputAt(i).getsource().getValue()                            .toString();                    if (key.startsWith("#")) {                        key = key.substring(1);                    }                    Object data = resourceLibrary.get(key);                    while (data instanceof String) {                        key = (String) data;                        if (key.startsWith("#")) {                            key = key.substring(1);                        }                        data = resourceLibrary.get(key);                    }                    Vector3f[] v = (Vector3f[]) data;                    StringTokenizer st = new StringTokenizer(poly.getp()                            .getValue());                    int colorCount = Integer                            .parseInt(poly.getcount().toString()) * 3;                    FloatBuffer colorBuffer = BufferUtils                            .createColorBuffer(colorCount);                    int offset = Integer.parseInt(poly.getinputAt(i).getoffset()                            .toString());                    ColorRGBA tempColor = new ColorRGBA();                    for (int j = offset; j < colorCount; j++) {                        if ( j % stride == 0 ) {                            st = new StringTokenizer(poly.getpAt(j/stride).getValue());                            for (int k = 0; k < offset; k++) {                                if (st.hasMoreTokens()) {                                    st.nextToken();                                }                            }                        }                        int index = Integer.parseInt(st.nextToken());                        Vector3f value = v[index];                        tempColor.set(value.x, value.y, value.z, 1);                        BufferUtils.setInBuffer(tempColor, colorBuffer, j);                        for (int k = 0; k < maxOffset; k++) {                            if (st.hasMoreTokens()) {                                st.nextToken();                            }                        }                    }                    out.setColorBuffer(batchIndex, colorBuffer);                }            }        }        out.setModelBound(new BoundingBox());        out.updateModelBound();        return out;    }