List of usage examples for org.jdom2 Element getText
public String getText()
From source file:com.archimatetool.templates.model.AbstractTemplate.java
License:Open Source License
private void loadManifest() { // Default first fManifestLoaded = true;/*from w w w.j a v a2 s. c o m*/ fName = ""; //$NON-NLS-1$ fDescription = ""; //$NON-NLS-1$ if (fFile != null && fFile.exists()) { try { // Manifest String manifest = ZipUtils.extractZipEntry(fFile, TemplateManager.ZIP_ENTRY_MANIFEST); if (manifest != null) { Document doc = JDOMUtils.readXMLString(manifest); Element rootElement = doc.getRootElement(); // Name Element nameElement = rootElement.getChild(XML_TEMPLATE_ELEMENT_NAME); if (nameElement != null) { fName = nameElement.getText(); } // Description Element descriptionElement = rootElement.getChild(XML_TEMPLATE_ELEMENT_DESCRIPTION); if (nameElement != null) { fDescription = descriptionElement.getText(); } // Key thumbnail Element keyThumbnailElement = rootElement.getChild(XML_TEMPLATE_ELEMENT_KEY_THUMBNAIL); if (keyThumbnailElement != null) { fKeyThumbnailPath = keyThumbnailElement.getText(); } } } catch (Exception ex) { ex.printStackTrace(); } } }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaAnimUtils.java
License:Open Source License
/** * Construct skin mesh(es) from the skin element and attach them (under a single new Node) to the given parent Node. * /*from w ww. ja va2s .com*/ * @param ardorParentNode * Ardor3D Node to attach our skin node to. * @param instanceController * the <instance_controller> element. We'll parse the skeleton reference from here. * @param controller * the referenced <controller> element. Used for naming purposes. * @param skin * our <skin> element. */ @SuppressWarnings("unchecked") private void buildSkinMeshes(final Node ardorParentNode, final Element instanceController, final Element controller, final Element skin) { final String skinSource = skin.getAttributeValue("source"); final Element skinNodeEL = _colladaDOMUtil.findTargetWithId(skinSource); if (skinNodeEL == null || !"geometry".equals(skinNodeEL.getName())) { throw new ColladaException( "Expected a mesh for skin source with url: " + skinSource + " got instead: " + skinNodeEL, skin); } final Element geometry = skinNodeEL; final Node meshNode = _colladaMeshUtils.buildMesh(geometry); if (meshNode != null) { // Look for skeleton entries in the original <instance_controller> element final List<Element> skeletonRoots = Lists.newArrayList(); for (final Element sk : instanceController.getChildren("skeleton")) { final Element skroot = _colladaDOMUtil.findTargetWithId(sk.getText()); if (skroot != null) { // add as a possible root for when we need to locate a joint by name later. skeletonRoots.add(skroot); } else { throw new ColladaException( "Unable to find node with id: " + sk.getText() + ", referenced from skeleton " + sk, sk); } } // Read in our joints node final Element jointsEL = skin.getChild("joints"); if (jointsEL == null) { throw new ColladaException("skin found without joints.", skin); } // Pull out our joint names and bind matrices final List<String> jointNames = Lists.newArrayList(); final List<Transform> bindMatrices = Lists.newArrayList(); final List<ColladaInputPipe.ParamType> paramTypes = Lists.newArrayList(); for (final Element inputEL : jointsEL.getChildren("input")) { final ColladaInputPipe pipe = new ColladaInputPipe(_colladaDOMUtil, inputEL); final ColladaInputPipe.SourceData sd = pipe.getSourceData(); if (pipe.getType() == ColladaInputPipe.Type.JOINT) { final String[] namesData = sd.stringArray; for (int i = sd.offset; i < namesData.length; i += sd.stride) { jointNames.add(namesData[i]); paramTypes.add(sd.paramType); } } else if (pipe.getType() == ColladaInputPipe.Type.INV_BIND_MATRIX) { final float[] floatData = sd.floatArray; final FloatBuffer source = BufferUtils.createFloatBufferOnHeap(16); for (int i = sd.offset; i < floatData.length; i += sd.stride) { source.rewind(); source.put(floatData, i, 16); source.flip(); final Matrix4 mat = new Matrix4().fromFloatBuffer(source); bindMatrices.add(new Transform().fromHomogeneousMatrix(mat)); } } } // Use the skeleton information from the instance_controller to set the parent array locations on the // joints. Skeleton ourSkeleton = null; // TODO: maybe not the best way. iterate final int[] order = new int[jointNames.size()]; for (int i = 0; i < jointNames.size(); i++) { final String name = jointNames.get(i); final ParamType paramType = paramTypes.get(i); final String searcher = paramType == ParamType.idref_param ? "id" : "sid"; Element found = null; for (final Element root : skeletonRoots) { if (name.equals(root.getAttributeValue(searcher))) { found = root; } else if (paramType == ParamType.idref_param) { found = _colladaDOMUtil.findTargetWithId(name); } else { found = (Element) _colladaDOMUtil.selectSingleNode(root, ".//*[@sid='" + name + "']"); } // Last resorts (bad exporters) if (found == null) { found = _colladaDOMUtil.findTargetWithId(name); } if (found == null) { found = (Element) _colladaDOMUtil.selectSingleNode(root, ".//*[@name='" + name + "']"); } if (found != null) { break; } } if (found == null) { if (paramType == ParamType.idref_param) { found = _colladaDOMUtil.findTargetWithId(name); } else { found = (Element) _colladaDOMUtil.selectSingleNode(geometry, "/*//visual_scene//*[@sid='" + name + "']"); } // Last resorts (bad exporters) if (found == null) { found = _colladaDOMUtil.findTargetWithId(name); } if (found == null) { found = (Element) _colladaDOMUtil.selectSingleNode(geometry, "/*//visual_scene//*[@name='" + name + "']"); } if (found == null) { throw new ColladaException("Unable to find joint with " + searcher + ": " + name, skin); } } final Joint joint = _dataCache.getElementJointMapping().get(found); if (joint == null) { logger.warning("unable to parse joint for: " + found.getName() + " " + name); return; } joint.setInverseBindPose(bindMatrices.get(i)); ourSkeleton = _dataCache.getJointSkeletonMapping().get(joint); order[i] = joint.getIndex(); } // Make our skeleton pose SkeletonPose skPose = _dataCache.getSkeletonPoseMapping().get(ourSkeleton); if (skPose == null) { skPose = new SkeletonPose(ourSkeleton); _dataCache.getSkeletonPoseMapping().put(ourSkeleton, skPose); // attach any attachment points found for the skeleton's joints addAttachments(skPose); // Skeleton's default to bind position, so update the global transforms. skPose.updateTransforms(); } // Read in our vertex_weights node final Element weightsEL = skin.getChild("vertex_weights"); if (weightsEL == null) { throw new ColladaException("skin found without vertex_weights.", skin); } // Pull out our per vertex joint indices and weights final List<Short> jointIndices = Lists.newArrayList(); final List<Float> jointWeights = Lists.newArrayList(); int indOff = 0, weightOff = 0; int maxOffset = 0; for (final Element inputEL : weightsEL.getChildren("input")) { final ColladaInputPipe pipe = new ColladaInputPipe(_colladaDOMUtil, inputEL); final ColladaInputPipe.SourceData sd = pipe.getSourceData(); if (pipe.getOffset() > maxOffset) { maxOffset = pipe.getOffset(); } if (pipe.getType() == ColladaInputPipe.Type.JOINT) { indOff = pipe.getOffset(); final String[] namesData = sd.stringArray; for (int i = sd.offset; i < namesData.length; i += sd.stride) { // XXX: the Collada spec says this could be -1? final String name = namesData[i]; final int index = jointNames.indexOf(name); if (index >= 0) { jointIndices.add((short) index); } else { throw new ColladaException("Unknown joint accessed: " + name, inputEL); } } } else if (pipe.getType() == ColladaInputPipe.Type.WEIGHT) { weightOff = pipe.getOffset(); final float[] floatData = sd.floatArray; for (int i = sd.offset; i < floatData.length; i += sd.stride) { jointWeights.add(floatData[i]); } } } // Pull our values array int firstIndex = 0, count = 0; final int[] vals = _colladaDOMUtil.parseIntArray(weightsEL.getChild("v")); try { count = weightsEL.getAttribute("count").getIntValue(); } catch (final DataConversionException e) { throw new ColladaException("Unable to parse count attribute.", weightsEL); } // use the vals to fill our vert weight map final int[][] vertWeightMap = new int[count][]; int index = 0; for (final int length : _colladaDOMUtil.parseIntArray(weightsEL.getChild("vcount"))) { final int[] entry = new int[(maxOffset + 1) * length]; vertWeightMap[index++] = entry; System.arraycopy(vals, (maxOffset + 1) * firstIndex, entry, 0, entry.length); firstIndex += length; } // Create a record for the global ColladaStorage. final String storeName = getSkinStoreName(instanceController, controller); final SkinData skinDataStore = new SkinData(storeName); // add pose to store skinDataStore.setPose(skPose); // Create a base Node for our skin meshes final Node skinNode = new Node(meshNode.getName()); // copy Node render states across. copyRenderStates(meshNode, skinNode); // add node to store skinDataStore.setSkinBaseNode(skinNode); // Grab the bind_shape_matrix from skin final Element bindShapeMatrixEL = skin.getChild("bind_shape_matrix"); final Transform bindShapeMatrix = new Transform(); if (bindShapeMatrixEL != null) { final double[] array = _colladaDOMUtil.parseDoubleArray(bindShapeMatrixEL); bindShapeMatrix.fromHomogeneousMatrix(new Matrix4().fromArray(array)); } // Visit our Node and pull out any Mesh children. Turn them into SkinnedMeshes for (final Spatial spat : meshNode.getChildren()) { if (spat instanceof Mesh && ((Mesh) spat).getMeshData().getVertexCount() > 0) { final Mesh sourceMesh = (Mesh) spat; final SkinnedMesh skMesh = new SkinnedMesh(sourceMesh.getName()); skMesh.setCurrentPose(skPose); // copy material info mapping for later use final String material = _dataCache.getMeshMaterialMap().get(sourceMesh); _dataCache.getMeshMaterialMap().put(skMesh, material); // copy mesh render states across. copyRenderStates(sourceMesh, skMesh); // copy hints across skMesh.getSceneHints().set(sourceMesh.getSceneHints()); try { // Use source mesh as bind pose data in the new SkinnedMesh final MeshData bindPose = copyMeshData(sourceMesh.getMeshData()); skMesh.setBindPoseData(bindPose); // Apply our BSM if (!bindShapeMatrix.isIdentity()) { bindPose.transformVertices(bindShapeMatrix); if (bindPose.getNormalBuffer() != null) { bindPose.transformNormals(bindShapeMatrix, true); } } // TODO: This is only needed for CPU skinning... consider a way of making it optional. // Copy bind pose to mesh data to setup for CPU skinning final MeshData meshData = copyMeshData(skMesh.getBindPoseData()); meshData.getVertexCoords().setVboAccessMode(VBOAccessMode.StreamDraw); if (meshData.getNormalCoords() != null) { meshData.getNormalCoords().setVboAccessMode(VBOAccessMode.StreamDraw); } skMesh.setMeshData(meshData); } catch (final IOException e) { e.printStackTrace(); throw new ColladaException("Unable to copy skeleton bind pose data.", geometry); } // Grab the MeshVertPairs from Global for this mesh. final Collection<MeshVertPairs> vertPairsList = _dataCache.getVertMappings().get(geometry); MeshVertPairs pairsMap = null; if (vertPairsList != null) { for (final MeshVertPairs pairs : vertPairsList) { if (pairs.getMesh() == sourceMesh) { pairsMap = pairs; break; } } } if (pairsMap == null) { throw new ColladaException("Unable to locate pair map for geometry.", geometry); } // Check for a remapping, if we optimized geometry final VertMap vertMap = _dataCache.getMeshVertMap().get(sourceMesh); // Use pairs map and vertWeightMap to build our weights and joint indices. { // count number of weights used int maxWeightsPerVert = 0; int weightCount; for (final int originalIndex : pairsMap.getIndices()) { weightCount = 0; // get weights and joints at original index and add weights up to get divisor sum // we'll assume 0's for vertices with no matching weight. if (vertWeightMap.length > originalIndex) { final int[] data = vertWeightMap[originalIndex]; for (int i = 0; i < data.length; i += maxOffset + 1) { final float weight = jointWeights.get(data[i + weightOff]); if (weight != 0) { weightCount++; } } if (weightCount > maxWeightsPerVert) { maxWeightsPerVert = weightCount; } } } final int verts = skMesh.getMeshData().getVertexCount(); final FloatBuffer weightBuffer = BufferUtils.createFloatBuffer(verts * maxWeightsPerVert); final ShortBuffer jointIndexBuffer = BufferUtils .createShortBuffer(verts * maxWeightsPerVert); int j; float sum = 0; final float[] weights = new float[maxWeightsPerVert]; final short[] indices = new short[maxWeightsPerVert]; int originalIndex; for (int x = 0; x < verts; x++) { if (vertMap != null) { originalIndex = pairsMap.getIndices()[vertMap.getFirstOldIndex(x)]; } else { originalIndex = pairsMap.getIndices()[x]; } j = 0; sum = 0; // get weights and joints at original index and add weights up to get divisor sum // we'll assume 0's for vertices with no matching weight. if (vertWeightMap.length > originalIndex) { final int[] data = vertWeightMap[originalIndex]; for (int i = 0; i < data.length; i += maxOffset + 1) { final float weight = jointWeights.get(data[i + weightOff]); if (weight != 0) { weights[j] = jointWeights.get(data[i + weightOff]); indices[j] = (short) order[jointIndices.get(data[i + indOff])]; sum += weights[j++]; } } } // add extra padding as needed while (j < maxWeightsPerVert) { weights[j] = 0; indices[j++] = 0; } // add weights to weightBuffer / sum for (final float w : weights) { weightBuffer.put(sum != 0 ? w / sum : 0); } // add joint indices to jointIndexBuffer jointIndexBuffer.put(indices); } final float[] totalWeights = new float[weightBuffer.capacity()]; weightBuffer.flip(); weightBuffer.get(totalWeights); skMesh.setWeights(totalWeights); final short[] totalIndices = new short[jointIndexBuffer.capacity()]; jointIndexBuffer.flip(); jointIndexBuffer.get(totalIndices); skMesh.setJointIndices(totalIndices); skMesh.setWeightsPerVert(maxWeightsPerVert); } // add to the skinNode. skinNode.attachChild(skMesh); // Manually apply our bind pose to the skin mesh. skMesh.applyPose(); // Update the model bounding. skMesh.updateModelBound(); // add mesh to store skinDataStore.getSkins().add(skMesh); } } // add to Node ardorParentNode.attachChild(skinNode); // Add skin record to storage. _colladaStorage.getSkins().add(skinDataStore); } }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaAnimUtils.java
License:Open Source License
@SuppressWarnings("unchecked") private static void getElementString(final Element e, final StringBuilder str, final int depth, final int maxDepth, final boolean showDots) { addSpacing(str, depth);/*from w w w .jav a 2s . c om*/ str.append('<'); str.append(e.getName()); str.append(' '); final List<Attribute> attrs = e.getAttributes(); for (int i = 0; i < attrs.size(); i++) { final Attribute attr = attrs.get(i); str.append(attr.getName()); str.append("=\""); str.append(attr.getValue()); str.append('"'); if (i < attrs.size() - 1) { str.append(' '); } } if (!e.getChildren().isEmpty() || !"".equals(e.getText())) { str.append('>'); if (depth < maxDepth) { str.append('\n'); for (final Element child : (List<Element>) e.getChildren()) { getElementString(child, str, depth + 1, maxDepth, showDots); } if (!"".equals(e.getText())) { addSpacing(str, depth + 1); str.append(e.getText()); str.append('\n'); } } else if (showDots) { str.append('\n'); addSpacing(str, depth + 1); str.append("..."); str.append('\n'); } addSpacing(str, depth); str.append("</"); str.append(e.getName()); str.append('>'); } else { str.append("/>"); } str.append('\n'); }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaDOMUtil.java
License:Open Source License
/** * Parses the text under a node and returns it as a float array. * /* w w w . j a v a 2s .c om*/ * @param node * node to parse content from * @return parsed float array */ public float[] parseFloatArray(final Element node) { if (_dataCache.getFloatArrays().containsKey(node)) { return _dataCache.getFloatArrays().get(node); } final String content = node.getText(); final List<String> list = new ArrayList<String>(); final StringTokenizer tokenizer = new StringTokenizer(content, " "); while (tokenizer.hasMoreTokens()) { list.add(tokenizer.nextToken()); } final int listSize = list.size(); final float[] floatArray = new float[listSize]; for (int i = 0; i < listSize; i++) { floatArray[i] = Float.parseFloat(list.get(i).replace(",", ".")); } _dataCache.getFloatArrays().put(node, floatArray); return floatArray; }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaDOMUtil.java
License:Open Source License
/** * Parses the text under a node and returns it as a double array. * /* w w w . j a v a2 s . c o m*/ * @param node * node to parse content from * @return parsed double array */ public double[] parseDoubleArray(final Element node) { if (_dataCache.getDoubleArrays().containsKey(node)) { return _dataCache.getDoubleArrays().get(node); } final String content = node.getText(); final List<String> list = new ArrayList<String>(); final StringTokenizer tokenizer = new StringTokenizer(content, " "); while (tokenizer.hasMoreTokens()) { list.add(tokenizer.nextToken()); } final int listSize = list.size(); final double[] doubleArray = new double[listSize]; for (int i = 0; i < listSize; i++) { doubleArray[i] = Double.parseDouble(list.get(i).replace(",", ".")); } _dataCache.getDoubleArrays().put(node, doubleArray); return doubleArray; }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaDOMUtil.java
License:Open Source License
/** * Parses the text under a node and returns it as an int array. * /*from w w w. j av a 2s .c o m*/ * @param node * node to parse content from * @return parsed int array */ public int[] parseIntArray(final Element node) { if (_dataCache.getIntArrays().containsKey(node)) { return _dataCache.getIntArrays().get(node); } final String content = node.getText(); final List<String> list = new ArrayList<String>(); final StringTokenizer tokenizer = new StringTokenizer(content, " "); while (tokenizer.hasMoreTokens()) { list.add(tokenizer.nextToken()); } final int listSize = list.size(); final int[] intArray = new int[listSize]; for (int i = 0; i < listSize; i++) { intArray[i] = Integer.parseInt(list.get(i)); } _dataCache.getIntArrays().put(node, intArray); return intArray; }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaDOMUtil.java
License:Open Source License
/** * Parses the text under a node and returns it as a boolean array. * //from www. j av a2s . c o m * @param node * node to parse content from * @return parsed boolean array */ public boolean[] parseBooleanArray(final Element node) { if (_dataCache.getDoubleArrays().containsKey(node)) { return _dataCache.getBooleanArrays().get(node); } final String content = node.getText(); final List<String> list = new ArrayList<String>(); final StringTokenizer tokenizer = new StringTokenizer(content, " "); while (tokenizer.hasMoreTokens()) { list.add(tokenizer.nextToken()); } final int listSize = list.size(); final boolean[] booleanArray = new boolean[listSize]; for (int i = 0; i < listSize; i++) { booleanArray[i] = Boolean.parseBoolean(list.get(i)); } _dataCache.getBooleanArrays().put(node, booleanArray); return booleanArray; }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaDOMUtil.java
License:Open Source License
/** * Parses the text under a node and returns it as a string array. * // w ww .j a v a 2 s .c om * @param node * node to parse content from * @return parsed string array */ public String[] parseStringArray(final Element node) { if (_dataCache.getStringArrays().containsKey(node)) { return _dataCache.getStringArrays().get(node); } final String content = node.getText(); final List<String> list = new ArrayList<String>(); final StringTokenizer tokenizer = new StringTokenizer(content, " "); while (tokenizer.hasMoreTokens()) { list.add(tokenizer.nextToken()); } final String[] stringArray = list.toArray(new String[list.size()]); _dataCache.getStringArrays().put(node, stringArray); return stringArray; }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaMaterialUtils.java
License:Open Source License
/** * Find and apply the given material to the given Mesh. * /*from ww w. ja va2s .c o m*/ * @param materialName * our material name * @param mesh * the mesh to apply material to. */ public void applyMaterial(final String materialName, final Mesh mesh) { if (materialName == null) { logger.warning("materialName is null"); return; } Element mat = _dataCache.getBoundMaterial(materialName); if (mat == null) { logger.warning("material not bound: " + materialName + ", trying search with id."); mat = _colladaDOMUtil.findTargetWithId(materialName); } if (mat == null || !"material".equals(mat.getName())) { logger.warning("material not found: " + materialName); return; } final String originalMaterial = mat.getAttributeValue("id"); MaterialInfo mInfo = null; if (!_dataCache.getMaterialInfoMap().containsKey(originalMaterial)) { mInfo = new MaterialInfo(); mInfo.setMaterialName(originalMaterial); _dataCache.getMaterialInfoMap().put(originalMaterial, mInfo); } _dataCache.getMeshMaterialMap().put(mesh, originalMaterial); final Element child = mat.getChild("instance_effect"); final Element effectNode = _colladaDOMUtil.findTargetWithId(child.getAttributeValue("url")); if (effectNode == null) { logger.warning( "material effect not found: " + mat.getChild("instance_material").getAttributeValue("url")); return; } if ("effect".equals(effectNode.getName())) { /* * temp cache for textures, we do not want to add textures twice (for example, transparant map might point * to diffuse texture) */ final HashMap<String, Texture> loadedTextures = new HashMap<String, Texture>(); final Element effect = effectNode; // XXX: For now, just grab the common technique: final Element common = effect.getChild("profile_COMMON"); if (common != null) { if (mInfo != null) { mInfo.setProfile("COMMON"); } final Element commonExtra = common.getChild("extra"); if (commonExtra != null) { // process with any plugins _importer.readExtra(commonExtra, mesh); } final Element technique = common.getChild("technique"); String type = "blinn"; if (technique.getChild(type) == null) { type = "phong"; if (technique.getChild(type) == null) { type = "lambert"; if (technique.getChild(type) == null) { type = "constant"; if (technique.getChild(type) == null) { ColladaMaterialUtils.logger.warning("COMMON material has unusuable techinque. " + child.getAttributeValue("url")); return; } } } } final Element blinnPhongLambert = technique.getChild(type); if (mInfo != null) { mInfo.setTechnique(type); } final MaterialState mState = new MaterialState(); // TODO: implement proper transparency handling Texture diffuseTexture = null; ColorRGBA transparent = new ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f); float transparency = 1.0f; boolean useTransparency = false; String opaqueMode = "A_ONE"; /* * place holder for current property, we import material properties in fixed order (for texture order) */ Element property = null; /* Diffuse property */ property = blinnPhongLambert.getChild("diffuse"); if (property != null) { final Element propertyValue = property.getChildren().get(0); if ("color".equals(propertyValue.getName())) { final ColorRGBA color = _colladaDOMUtil.getColor(propertyValue.getText()); mState.setDiffuse(MaterialFace.FrontAndBack, color); } else if ("texture".equals(propertyValue.getName()) && _loadTextures) { diffuseTexture = populateTextureState(mesh, propertyValue, effect, loadedTextures, mInfo, "diffuse"); } } /* Ambient property */ property = blinnPhongLambert.getChild("ambient"); if (property != null) { final Element propertyValue = property.getChildren().get(0); if ("color".equals(propertyValue.getName())) { final ColorRGBA color = _colladaDOMUtil.getColor(propertyValue.getText()); mState.setAmbient(MaterialFace.FrontAndBack, color); } else if ("texture".equals(propertyValue.getName()) && _loadTextures) { populateTextureState(mesh, propertyValue, effect, loadedTextures, mInfo, "ambient"); } } /* Transparent property */ property = blinnPhongLambert.getChild("transparent"); if (property != null) { final Element propertyValue = property.getChildren().get(0); if ("color".equals(propertyValue.getName())) { transparent = _colladaDOMUtil.getColor(propertyValue.getText()); // TODO: use this useTransparency = true; } else if ("texture".equals(propertyValue.getName()) && _loadTextures) { populateTextureState(mesh, propertyValue, effect, loadedTextures, mInfo, "transparent"); } opaqueMode = property.getAttributeValue("opaque", "A_ONE"); } /* Transparency property */ property = blinnPhongLambert.getChild("transparency"); if (property != null) { final Element propertyValue = property.getChildren().get(0); if ("float".equals(propertyValue.getName())) { transparency = Float.parseFloat(propertyValue.getText().replace(",", ".")); // TODO: use this if (_flipTransparency) { transparency = 1f - transparency; } useTransparency = true; } else if ("texture".equals(propertyValue.getName()) && _loadTextures) { populateTextureState(mesh, propertyValue, effect, loadedTextures, mInfo, "transparency"); } } /* Emission property */ property = blinnPhongLambert.getChild("emission"); if (property != null) { final Element propertyValue = property.getChildren().get(0); if ("color".equals(propertyValue.getName())) { mState.setEmissive(MaterialFace.FrontAndBack, _colladaDOMUtil.getColor(propertyValue.getText())); } else if ("texture".equals(propertyValue.getName()) && _loadTextures) { populateTextureState(mesh, propertyValue, effect, loadedTextures, mInfo, "emissive"); } } /* Specular property */ property = blinnPhongLambert.getChild("specular"); if (property != null) { final Element propertyValue = property.getChildren().get(0); if ("color".equals(propertyValue.getName())) { mState.setSpecular(MaterialFace.FrontAndBack, _colladaDOMUtil.getColor(propertyValue.getText())); } else if ("texture".equals(propertyValue.getName()) && _loadTextures) { populateTextureState(mesh, propertyValue, effect, loadedTextures, mInfo, "specular"); } } /* Shininess property */ property = blinnPhongLambert.getChild("shininess"); if (property != null) { final Element propertyValue = property.getChildren().get(0); if ("float".equals(propertyValue.getName())) { float shininess = Float.parseFloat(propertyValue.getText().replace(",", ".")); if (shininess >= 0.0f && shininess <= 1.0f) { final float oldShininess = shininess; shininess *= 128; logger.finest("Shininess - " + oldShininess + " - was in the [0,1] range. Scaling to [0, 128] - " + shininess); } else if (shininess < 0 || shininess > 128) { final float oldShininess = shininess; shininess = MathUtils.clamp(shininess, 0, 128); logger.warning("Shininess must be between 0 and 128. Shininess " + oldShininess + " was clamped to " + shininess); } mState.setShininess(MaterialFace.FrontAndBack, shininess); } else if ("texture".equals(propertyValue.getName()) && _loadTextures) { populateTextureState(mesh, propertyValue, effect, loadedTextures, mInfo, "shininess"); } } /* Reflectivity property */ float reflectivity = 1.0f; property = blinnPhongLambert.getChild("reflectivity"); if (property != null) { final Element propertyValue = property.getChildren().get(0); if ("float".equals(propertyValue.getName())) { reflectivity = Float.parseFloat(propertyValue.getText().replace(",", ".")); } } /* Reflective property. Texture only */ property = blinnPhongLambert.getChild("reflective"); if (property != null) { final Element propertyValue = property.getChildren().get(0); if ("texture".equals(propertyValue.getName()) && _loadTextures) { final Texture reflectiveTexture = populateTextureState(mesh, propertyValue, effect, loadedTextures, mInfo, "reflective"); reflectiveTexture.setEnvironmentalMapMode(Texture.EnvironmentalMapMode.SphereMap); reflectiveTexture.setApply(ApplyMode.Combine); reflectiveTexture.setCombineFuncRGB(CombinerFunctionRGB.Interpolate); // color 1 reflectiveTexture.setCombineSrc0RGB(CombinerSource.CurrentTexture); reflectiveTexture.setCombineOp0RGB(CombinerOperandRGB.SourceColor); // color 2 reflectiveTexture.setCombineSrc1RGB(CombinerSource.Previous); reflectiveTexture.setCombineOp1RGB(CombinerOperandRGB.SourceColor); // interpolate param will come from alpha of constant color reflectiveTexture.setCombineSrc2RGB(CombinerSource.Constant); reflectiveTexture.setCombineOp2RGB(CombinerOperandRGB.SourceAlpha); reflectiveTexture.setConstantColor(new ColorRGBA(1, 1, 1, reflectivity)); } } /* * An extra tag defines some materials not part of the collada standard. Since we're not able to parse * we simply extract the textures from the element (such that shaders etc can at least pick up on them) */ property = technique.getChild("extra"); if (property != null) { // process with any plugins if (!_importer.readExtra(property, mesh)) { // no plugin processed our mesh, so process ourselves. getTexturesFromElement(mesh, property, effect, loadedTextures, mInfo); } } // XXX: There are some issues with clarity on how to use alpha blending in OpenGL FFP. // The best interpretation I have seen is that if transparent has a texture == diffuse, // Turn on alpha blending and use diffuse alpha. // check to make sure we actually need this. // testing separately for a transparency of 0.0 is to hack around erroneous exports, since usually // there is no use in exporting something with 100% transparency. if ("A_ONE".equals(opaqueMode) && ColorRGBA.WHITE.equals(transparent) && transparency == 1.0 || transparency == 0.0) { useTransparency = false; } if (useTransparency) { if (diffuseTexture != null) { final BlendState blend = new BlendState(); blend.setBlendEnabled(true); blend.setTestEnabled(true); blend.setSourceFunction(BlendState.SourceFunction.SourceAlpha); blend.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha); mesh.setRenderState(blend); } else { final BlendState blend = new BlendState(); blend.setBlendEnabled(true); blend.setTestEnabled(true); transparent.setAlpha(transparent.getAlpha() * transparency); blend.setConstantColor(transparent); blend.setSourceFunction(BlendState.SourceFunction.ConstantAlpha); blend.setDestinationFunction(BlendState.DestinationFunction.OneMinusConstantAlpha); mesh.setRenderState(blend); } mesh.getSceneHints().setRenderBucketType(RenderBucketType.Transparent); } if (mInfo != null) { if (useTransparency) { mInfo.setUseTransparency(useTransparency); if (diffuseTexture == null) { mInfo.setTransparency(transparent.getAlpha() * transparency); } } mInfo.setMaterialState(mState); } mesh.setRenderState(mState); } } else { ColladaMaterialUtils.logger.warning( "material effect not found: " + mat.getChild("instance_material").getAttributeValue("url")); } }
From source file:com.ardor3d.extension.model.collada.jdom.ColladaMaterialUtils.java
License:Open Source License
/** * Convert a <texture> element to an Ardor3D representation and store in the given state. * /* w w w. ja v a 2 s. c o m*/ * @param mesh * the Ardor3D Mesh to add the Texture to. * @param daeTexture * our <texture> element * @param effect * our <instance_effect> element * @return the created Texture. */ private Texture populateTextureState(final Mesh mesh, final Element daeTexture, final Element effect, final HashMap<String, Texture> loadedTextures, final MaterialInfo info, String textureSlot) { // TODO: Use vert data to determine which texcoords and set to use. // final String uvName = daeTexture.getAttributeValue("texcoord"); TextureState tState = (TextureState) mesh.getLocalRenderState(RenderState.StateType.Texture); if (tState == null) { tState = new TextureState(); mesh.setRenderState(tState); } // Use texture attrib to find correct sampler final String textureReference = daeTexture.getAttributeValue("texture"); if (textureSlot == null) { // if we have no texture slot defined (like in the case of an "extra" texture), we'll use the // textureReference. textureSlot = textureReference; } /* only add the texture to the state once */ if (loadedTextures.containsKey(textureReference)) { final Texture tex = loadedTextures.get(textureReference); if (info != null) { info.setTextureSlot(textureSlot, textureReference, tex, null); } return tex; } Element node = _colladaDOMUtil.findTargetWithSid(textureReference); if (node == null) { // Not sure if this is quite right, but spec seems to indicate looking for global id node = _colladaDOMUtil.findTargetWithId("#" + textureReference); } if ("newparam".equals(node.getName())) { node = node.getChildren().get(0); } Element sampler = null; Element surface = null; Element image = null; Texture.MinificationFilter min = Texture.MinificationFilter.BilinearNoMipMaps; if ("sampler2D".equals(node.getName())) { sampler = node; if (sampler.getChild("minfilter") != null) { final String minfilter = sampler.getChild("minfilter").getText(); min = Enum.valueOf(SamplerTypes.MinFilterType.class, minfilter).getArdor3dFilter(); } // Use sampler to get correct surface node = _colladaDOMUtil.findTargetWithSid(sampler.getChild("source").getText()); // node = resolveSid(effect, sampler.getSource()); } if ("newparam".equals(node.getName())) { node = node.getChildren().get(0); } if ("surface".equals(node.getName())) { surface = node; // image(s) will come from surface. } else if ("image".equals(node.getName())) { image = node; } // Ok, a few possibilities here... Texture texture = null; String fileName = null; if (surface == null && image != null) { // Only an image found (no sampler). Assume 2d texture. Load. fileName = image.getChild("init_from").getText(); texture = loadTexture2D(fileName, min); } else if (surface != null) { // We have a surface, pull images from that. if ("2D".equals(surface.getAttributeValue("type"))) { // look for an init_from with lowest mip and use that. (usually 0) // TODO: mip? final Element lowest = surface.getChildren("init_from").get(0); // Element lowest = null; // for (final Element i : (List<Element>) surface.getChildren("init_from")) { // if (lowest == null || lowest.getMip() > i.getMip()) { // lowest = i; // } // } if (lowest == null) { logger.warning("surface given with no usable init_from: " + surface); return null; } image = _colladaDOMUtil.findTargetWithId("#" + lowest.getText()); // image = (DaeImage) root.resolveUrl("#" + lowest.getValue()); if (image != null) { fileName = image.getChild("init_from").getText(); texture = loadTexture2D(fileName, min); } // TODO: add support for mip map levels other than 0. } // TODO: add support for the other texture types. } else { // No surface OR image... warn. logger.warning("texture given with no matching <sampler*> or <image> found."); if (info != null) { info.setTextureSlot(textureSlot, textureReference, null, null); } return null; } if (texture != null) { if (sampler != null) { // Apply params from our sampler. applySampler(sampler, texture); } // Add to texture state. tState.setTexture(texture, tState.getNumberOfSetTextures()); loadedTextures.put(textureReference, texture); if (info != null) { info.setTextureSlot(textureSlot, textureReference, texture, fileName); } } else { logger.warning("unable to load texture: " + daeTexture); if (info != null) { info.setTextureSlot(textureSlot, textureReference, null, fileName); } } return texture; }