List of usage examples for org.jdom2 Element getAttributeValue
public String getAttributeValue(final String attname)
This returns the attribute value for the attribute with the given name and within no namespace, null if there is no such attribute, and the empty string if the attribute value is empty.
From source file:com.ardor3d.extension.model.collada.jdom.ColladaMeshUtils.java
License:Open Source License
/** * Builds geometry from an instance_geometry element. * //from w w w. j a v a 2 s. c o m * @param instanceGeometry * @return our Spatial */ public Spatial getGeometryMesh(final Element instanceGeometry) { final Element geometry = _colladaDOMUtil.findTargetWithId(instanceGeometry.getAttributeValue("url")); if (geometry != null) { return buildMesh(geometry); } return null; }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaMeshUtils.java
License:Open Source License
public Mesh buildMeshPolygons(final Element colladaGeometry, final Element polys) { if (polys == null || polys.getChild("input") == null) { return null; }//ww w .j a va 2s .c om final Mesh polyMesh = new Mesh(extractName(colladaGeometry, polys)); polyMesh.getMeshData().setIndexMode(IndexMode.Triangles); // Build and set RenderStates for our material _colladaMaterialUtils.applyMaterial(polys.getAttributeValue("material"), polyMesh); final LinkedList<ColladaInputPipe> pipes = new LinkedList<ColladaInputPipe>(); final int maxOffset = extractPipes(polys, pipes); final int interval = maxOffset + 1; // use interval & sum of sizes of p entries to determine buffer sizes. int numEntries = 0; int numIndices = 0; for (final Element vals : polys.getChildren("p")) { final int length = _colladaDOMUtil.parseIntArray(vals).length; numEntries += length; numIndices += (length / interval - 2) * 3; } numEntries /= interval; // Construct nio buffers for specified inputs. for (final ColladaInputPipe pipe : pipes) { pipe.setupBuffer(numEntries, polyMesh.getMeshData(), _dataCache); } // Add to vert mapping final int[] indices = new int[numEntries]; final MeshVertPairs mvp = new MeshVertPairs(polyMesh, indices); _dataCache.getVertMappings().put(colladaGeometry, mvp); // Prepare indices buffer final IndexBufferData<?> meshIndices = BufferUtils.createIndexBufferData(numIndices, polyMesh.getMeshData().getVertexCount() - 1); polyMesh.getMeshData().setIndices(meshIndices); // go through the polygon entries int firstIndex = 0, vecIndex; final int[] currentVal = new int[interval]; for (final Element dia : polys.getChildren("p")) { // for each p, iterate using max offset final int[] vals = _colladaDOMUtil.parseIntArray(dia); final int first = firstIndex + 0; System.arraycopy(vals, 0, currentVal, 0, interval); vecIndex = processPipes(pipes, currentVal); if (vecIndex != Integer.MIN_VALUE) { indices[firstIndex + 0] = vecIndex; } int prev = firstIndex + 1; System.arraycopy(vals, interval, currentVal, 0, interval); vecIndex = processPipes(pipes, currentVal); if (vecIndex != Integer.MIN_VALUE) { indices[firstIndex + 1] = vecIndex; } // first add the first two entries to the buffers. // Now go through remaining entries and create a polygon as a triangle fan. for (int j = 2, max = vals.length / interval; j < max; j++) { // add first as index meshIndices.put(first); // add prev as index meshIndices.put(prev); // set prev to current prev = firstIndex + j; // add current to buffers System.arraycopy(vals, j * interval, currentVal, 0, interval); vecIndex = processPipes(pipes, currentVal); if (vecIndex != Integer.MIN_VALUE) { indices[firstIndex + j] = vecIndex; } // add current as index meshIndices.put(prev); } firstIndex += vals.length / interval; } if (_optimizeMeshes) { final VertMap map = _geometryTool.minimizeVerts(polyMesh, _optimizeSettings); _dataCache.setMeshVertMap(polyMesh, map); } // update bounds polyMesh.updateModelBound(); // return return polyMesh; }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaMeshUtils.java
License:Open Source License
public Mesh buildMeshPolylist(final Element colladaGeometry, final Element polys) { if (polys == null || polys.getChild("input") == null) { return null; }/*from ww w . j a v a 2 s . co m*/ final Mesh polyMesh = new Mesh(extractName(colladaGeometry, polys)); polyMesh.getMeshData().setIndexMode(IndexMode.Triangles); // Build and set RenderStates for our material _colladaMaterialUtils.applyMaterial(polys.getAttributeValue("material"), polyMesh); final LinkedList<ColladaInputPipe> pipes = new LinkedList<ColladaInputPipe>(); final int maxOffset = extractPipes(polys, pipes); final int interval = maxOffset + 1; // use interval & sum of sizes of vcount to determine buffer sizes. int numEntries = 0; int numIndices = 0; for (final int length : _colladaDOMUtil.parseIntArray(polys.getChild("vcount"))) { numEntries += length; numIndices += (length - 2) * 3; } // Construct nio buffers for specified inputs. for (final ColladaInputPipe pipe : pipes) { pipe.setupBuffer(numEntries, polyMesh.getMeshData(), _dataCache); } // Add to vert mapping final int[] indices = new int[numEntries]; final MeshVertPairs mvp = new MeshVertPairs(polyMesh, indices); _dataCache.getVertMappings().put(colladaGeometry, mvp); // Prepare indices buffer final IndexBufferData<?> meshIndices = BufferUtils.createIndexBufferData(numIndices, polyMesh.getMeshData().getVertexCount() - 1); polyMesh.getMeshData().setIndices(meshIndices); // go through the polygon entries int firstIndex = 0; int vecIndex; final int[] vals = _colladaDOMUtil.parseIntArray(polys.getChild("p")); for (final int length : _colladaDOMUtil.parseIntArray(polys.getChild("vcount"))) { final int[] currentVal = new int[interval]; // first add the first two entries to the buffers. final int first = firstIndex + 0; System.arraycopy(vals, (first * interval), currentVal, 0, interval); vecIndex = processPipes(pipes, currentVal); if (vecIndex != Integer.MIN_VALUE) { indices[firstIndex + 0] = vecIndex; } int prev = firstIndex + 1; System.arraycopy(vals, (prev * interval), currentVal, 0, interval); vecIndex = processPipes(pipes, currentVal); if (vecIndex != Integer.MIN_VALUE) { indices[firstIndex + 1] = vecIndex; } // Now go through remaining entries and create a polygon as a triangle fan. for (int j = 2, max = length; j < max; j++) { // add first as index meshIndices.put(first); // add prev as index meshIndices.put(prev); // set prev to current prev = firstIndex + j; // add current to buffers System.arraycopy(vals, (prev * interval), currentVal, 0, interval); vecIndex = processPipes(pipes, currentVal); if (vecIndex != Integer.MIN_VALUE) { indices[firstIndex + j] = vecIndex; } // add current as index meshIndices.put(prev); } firstIndex += length; } if (_optimizeMeshes) { final VertMap map = _geometryTool.minimizeVerts(polyMesh, _optimizeSettings); _dataCache.setMeshVertMap(polyMesh, map); } // update bounds polyMesh.updateModelBound(); // return return polyMesh; }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaMeshUtils.java
License:Open Source License
public Mesh buildMeshTriangles(final Element colladaGeometry, final Element tris) { if (tris == null || tris.getChild("input") == null || tris.getChild("p") == null) { return null; }/*from w ww . ja v a 2 s . c om*/ final Mesh triMesh = new Mesh(extractName(colladaGeometry, tris)); triMesh.getMeshData().setIndexMode(IndexMode.Triangles); // Build and set RenderStates for our material _colladaMaterialUtils.applyMaterial(tris.getAttributeValue("material"), triMesh); final LinkedList<ColladaInputPipe> pipes = new LinkedList<ColladaInputPipe>(); final int maxOffset = extractPipes(tris, pipes); final int interval = maxOffset + 1; // use interval & size of p array to determine buffer sizes. final int[] vals = _colladaDOMUtil.parseIntArray(tris.getChild("p")); final int numEntries = vals.length / interval; // Construct nio buffers for specified inputs. for (final ColladaInputPipe pipe : pipes) { pipe.setupBuffer(numEntries, triMesh.getMeshData(), _dataCache); } // Add to vert mapping final int[] indices = new int[numEntries]; final MeshVertPairs mvp = new MeshVertPairs(triMesh, indices); _dataCache.getVertMappings().put(colladaGeometry, mvp); // go through the p entry // for each p, iterate using max offset final int[] currentVal = new int[interval]; // Go through entries and add to buffers. for (int j = 0, max = numEntries; j < max; j++) { // add entry to buffers System.arraycopy(vals, j * interval, currentVal, 0, interval); final int rVal = processPipes(pipes, currentVal); if (rVal != Integer.MIN_VALUE) { indices[j] = rVal; } } if (_optimizeMeshes) { final VertMap map = _geometryTool.minimizeVerts(triMesh, _optimizeSettings); _dataCache.setMeshVertMap(triMesh, map); } triMesh.updateModelBound(); return triMesh; }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaMeshUtils.java
License:Open Source License
public Line buildMeshLines(final Element colladaGeometry, final Element lines) { if (lines == null || lines.getChild("input") == null || lines.getChild("p") == null) { return null; }//from w ww .j a va2 s. c o m final Line lineMesh = new Line(extractName(colladaGeometry, lines)); // Build and set RenderStates for our material _colladaMaterialUtils.applyMaterial(lines.getAttributeValue("material"), lineMesh); final LinkedList<ColladaInputPipe> pipes = new LinkedList<ColladaInputPipe>(); final int maxOffset = extractPipes(lines, pipes); final int interval = maxOffset + 1; // use interval & size of p array to determine buffer sizes. final int[] vals = _colladaDOMUtil.parseIntArray(lines.getChild("p")); final int numEntries = vals.length / interval; // Construct nio buffers for specified inputs. for (final ColladaInputPipe pipe : pipes) { pipe.setupBuffer(numEntries, lineMesh.getMeshData(), _dataCache); } // Add to vert mapping final int[] indices = new int[numEntries]; final MeshVertPairs mvp = new MeshVertPairs(lineMesh, indices); _dataCache.getVertMappings().put(colladaGeometry, mvp); // go through the p entry // for each p, iterate using max offset final int[] currentVal = new int[interval]; // Go through entries and add to buffers. for (int j = 0, max = numEntries; j < max; j++) { // add entry to buffers System.arraycopy(vals, j * interval, currentVal, 0, interval); final int rVal = processPipes(pipes, currentVal); if (rVal != Integer.MIN_VALUE) { indices[j] = rVal; } } if (_optimizeMeshes) { final VertMap map = _geometryTool.minimizeVerts(lineMesh, _optimizeSettings); _dataCache.setMeshVertMap(lineMesh, map); } lineMesh.updateModelBound(); return lineMesh; }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaMeshUtils.java
License:Open Source License
/** * Extract our pipes from the given parent element. * /*from w ww . j a v a 2 s. c o m*/ * @param inputsParent * @param pipesStore * the store for our pipes * @return the max offset of our pipes. */ private int extractPipes(final Element inputsParent, final LinkedList<ColladaInputPipe> pipesStore) { int maxOffset = 0; int texCount = 0; for (final Element input : inputsParent.getChildren("input")) { maxOffset = Math.max(maxOffset, _colladaDOMUtil.getAttributeIntValue(input, "offset", 0)); try { final Type type = Type.valueOf(input.getAttributeValue("semantic")); if (type == Type.VERTEX) { final Element vertexElement = _colladaDOMUtil .findTargetWithId(input.getAttributeValue("source")); for (final Element vertexInput : vertexElement.getChildren("input")) { vertexInput.setAttribute("offset", input.getAttributeValue("offset")); vertexInput.setAttribute("isVertexDefined", "true"); final ColladaInputPipe pipe = new ColladaInputPipe(_colladaDOMUtil, vertexInput); if (pipe.getType() == Type.TEXCOORD) { pipe.setTexCoord(texCount++); } pipesStore.add(pipe); } } else { final ColladaInputPipe pipe = new ColladaInputPipe(_colladaDOMUtil, input); if (pipe.getType() == Type.TEXCOORD) { pipe.setTexCoord(texCount++); } pipesStore.add(pipe); } } catch (final Exception ex) { logger.warning("Unknown input type: " + input.getAttributeValue("semantic")); continue; } } return maxOffset; }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaMeshUtils.java
License:Open Source License
/** * Extract name from xml element, some exporters don't support 'name' attribute, so we better use the material * instead of a generic name.// www . ja v a 2s.c o m * * @param element * @return value from 'name' or 'material' attribute */ private String extractName(final Element colladaGeometry, final Element element) { // Try to get mesh name String name = element.getAttributeValue("name"); if (name == null || name.isEmpty()) { // No mesh name found, try to get mesh id name = element.getAttributeValue("id"); } if (name == null || name.isEmpty()) { // No mesh name or id found, try to get parent geometry name name = colladaGeometry.getAttributeValue("name"); if (name == null || name.isEmpty()) { // No parent geometry name found, try to get geometry id (mandatory according to spec) name = colladaGeometry.getAttributeValue("id"); } if (name == null) { name = ""; } // Since we have retrieved the parent geometry name/id, we append the material(if any), // to make identification unique. final String materialName = element.getAttributeValue("material"); if (materialName != null && !materialName.isEmpty()) { name += "[" + materialName + "]"; } } return name; }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaNodeUtils.java
License:Open Source License
/** * Retrieves the scene and returns it as an Ardor3D Node. * //from w w w . j av a 2 s .c o m * @param colladaRoot * The collada root element * @return Scene as an Node or null if not found */ @SuppressWarnings("unchecked") public Node getVisualScene(final Element colladaRoot) { if (colladaRoot.getChild("scene") == null) { logger.warning("No scene found in collada file!"); return null; } final Element instance_visual_scene = colladaRoot.getChild("scene").getChild("instance_visual_scene"); if (instance_visual_scene == null) { logger.warning("No instance_visual_scene found in collada file!"); return null; } final Element visualScene = _colladaDOMUtil .findTargetWithId(instance_visual_scene.getAttributeValue("url")); if (visualScene != null) { final Node sceneRoot = new Node( visualScene.getAttributeValue("name") != null ? visualScene.getAttributeValue("name") : "Collada Root"); // Load each sub node and attach final JointNode baseJointNode = new JointNode(null); _dataCache.setRootJointNode(baseJointNode); for (final Element n : visualScene.getChildren("node")) { final Node subNode = buildNode(n, baseJointNode); if (subNode != null) { sceneRoot.attachChild(subNode); } } // build a list of joints - one list per skeleton - and build a skeleton for each joint list. for (final JointNode jointChildNode : _dataCache.getRootJointNode().getChildren()) { final List<Joint> jointList = Lists.newArrayList(); buildJointLists(jointChildNode, jointList); final Joint[] joints = jointList.toArray(new Joint[jointList.size()]); final Skeleton skeleton = new Skeleton(joints[0].getName() + "_skeleton", joints); if (logger.isLoggable(Level.FINE)) { logger.fine("skeleton created: " + skeleton.getName()); } for (final Joint joint : jointList) { _dataCache.getJointSkeletonMapping().put(joint, skeleton); if (logger.isLoggable(Level.FINE)) { logger.fine("- Joint " + joint.getName() + " - index: " + joint.getIndex() + " parent index: " + joint.getParentIndex()); } } _dataCache.addSkeleton(skeleton); } // update our world transforms so we can use them to init the default bind matrix of any joints. sceneRoot.updateWorldTransform(true); initDefaultJointTransforms(baseJointNode); // Setup our skinned mesh objects. for (final ControllerStore controllerStore : _dataCache.getControllers()) { _colladaMaterialUtils.bindMaterials(controllerStore.instanceController.getChild("bind_material")); _colladaAnimUtils.buildController(controllerStore.ardorParentNode, controllerStore.instanceController); _colladaMaterialUtils.unbindMaterials(controllerStore.instanceController.getChild("bind_material")); } return sceneRoot; } return null; }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaNodeUtils.java
License:Open Source License
/** * Parse an asset element into an AssetData object. * /*from w w w .j av a2 s .c om*/ * @param asset * @return */ @SuppressWarnings("unchecked") public AssetData parseAsset(final Element asset) { final AssetData assetData = new AssetData(); for (final Element child : asset.getChildren()) { if ("contributor".equals(child.getName())) { parseContributor(assetData, child); } else if ("created".equals(child.getName())) { assetData.setCreated(child.getText()); } else if ("keywords".equals(child.getName())) { assetData.setKeywords(child.getText()); } else if ("modified".equals(child.getName())) { assetData.setModified(child.getText()); } else if ("revision".equals(child.getName())) { assetData.setRevision(child.getText()); } else if ("subject".equals(child.getName())) { assetData.setSubject(child.getText()); } else if ("title".equals(child.getName())) { assetData.setTitle(child.getText()); } else if ("unit".equals(child.getName())) { final String name = child.getAttributeValue("name"); if (name != null) { assetData.setUnitName(name); } final String meter = child.getAttributeValue("meter"); if (meter != null) { assetData.setUnitMeter(Float.parseFloat(meter.replace(",", "."))); } } else if ("up_axis".equals(child.getName())) { final String axis = child.getText(); if ("X_UP".equals(axis)) { assetData.setUpAxis(new Vector3()); } else if ("Y_UP".equals(axis)) { assetData.setUpAxis(Vector3.UNIT_Y); } else if ("Z_UP".equals(axis)) { assetData.setUpAxis(Vector3.UNIT_Z); } } } return assetData; }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaNodeUtils.java
License:Open Source License
/** * @param instanceNode//from w w w.ja v a2s. co m * @return a new Ardor3D node, created from the <node> pointed to by the given <instance_node> element */ public Node getNode(final Element instanceNode, final JointNode jointNode) { final Element node = _colladaDOMUtil.findTargetWithId(instanceNode.getAttributeValue("url")); if (node == null) { throw new ColladaException("No node with id: " + instanceNode.getAttributeValue("url") + " found", instanceNode); } return buildNode(node, jointNode); }