Package com.ardor3d.math

Examples of com.ardor3d.math.Matrix4


        }

        // init palette
        _matrixPalette = new Matrix4[jointCount];
        for (int i = 0; i < jointCount; i++) {
            _matrixPalette[i] = new Matrix4();
        }

        // start off in bind pose.
        setToBindPose();
    }
View Full Code Here


            field3.set(this, globalTransforms);

            // init palette
            final Matrix4[] matrixPalette = new Matrix4[jointCount];
            for (int i = 0; i < jointCount; i++) {
                matrixPalette[i] = new Matrix4();
            }
            final Field field4 = SkeletonPose.class.getDeclaredField("_matrixPalette");
            field4.setAccessible(true);
            field4.set(this, matrixPalette);
View Full Code Here

                } else {
                    storeNorms.rewind();
                }
            }

            Matrix4 jntMat;
            double bindVX, bindVY, bindVZ;
            double bindNX = 0, bindNY = 0, bindNZ = 0;
            double vSumX, vSumY, vSumZ;
            double nSumX = 0, nSumY = 0, nSumZ = 0;
            double tempX, tempY, tempZ;
            float weight;
            int jointIndex;

            // Cycle through each vertex
            for (int i = 0; i < _bindPoseData.getVertexCount(); i++) {
                // zero out our sum var
                vSumX = 0;
                vSumY = 0;
                vSumZ = 0;

                // Grab the bind pose vertex Vbp from _bindPoseData
                bindVX = bindVerts.get();
                bindVY = bindVerts.get();
                bindVZ = bindVerts.get();

                // See if we should do the corresponding normal as well
                if (bindNorms != null) {
                    // zero out our sum var
                    nSumX = 0;
                    nSumY = 0;
                    nSumZ = 0;

                    // Grab the bind pose norm Nbp from _bindPoseData
                    bindNX = bindNorms.get();
                    bindNY = bindNorms.get();
                    bindNZ = bindNorms.get();
                }

                // for each joint where the weight != 0
                for (int j = 0; j < getWeightsPerVert(); j++) {
                    final int index = i * getWeightsPerVert() + j;
                    if (_weights[index] == 0) {
                        continue;
                    }

                    jointIndex = _jointIndices[index];
                    jntMat = _currentPose.getMatrixPalette()[jointIndex];
                    weight = _weights[index];

                    // Multiply our vertex by the matrix palette entry
                    tempX = jntMat.getM00() * bindVX + jntMat.getM01() * bindVY + jntMat.getM02() * bindVZ
                            + jntMat.getM03();
                    tempY = jntMat.getM10() * bindVX + jntMat.getM11() * bindVY + jntMat.getM12() * bindVZ
                            + jntMat.getM13();
                    tempZ = jntMat.getM20() * bindVX + jntMat.getM21() * bindVY + jntMat.getM22() * bindVZ
                            + jntMat.getM23();

                    // Sum, weighted.
                    vSumX += tempX * weight;
                    vSumY += tempY * weight;
                    vSumZ += tempZ * weight;

                    if (bindNorms != null) {
                        // Multiply our normal by the matrix palette entry
                        tempX = jntMat.getM00() * bindNX + jntMat.getM01() * bindNY + jntMat.getM02() * bindNZ;
                        tempY = jntMat.getM10() * bindNX + jntMat.getM11() * bindNY + jntMat.getM12() * bindNZ;
                        tempZ = jntMat.getM20() * bindNX + jntMat.getM21() * bindNY + jntMat.getM22() * bindNZ;

                        // Sum, weighted.
                        nSumX += tempX * weight;
                        nSumY += tempY * weight;
                        nSumZ += tempZ * weight;
 
View Full Code Here

        if (_storeImage) {
            capsule.write(_image, "image", null);
        }
        capsule.write(_constantColor, "constantColor", new ColorRGBA(ColorRGBA.BLACK_NO_ALPHA));
        capsule.write(_borderColor, "borderColor", new ColorRGBA(ColorRGBA.BLACK_NO_ALPHA));
        capsule.write(_texMatrix, "texMatrix", new Matrix4(Matrix4.IDENTITY));
        capsule.write(_hasBorder, "hasBorder", false);
        capsule.write(_anisotropicFilterPercent, "anisotropicFilterPercent", 0.0f);
        capsule.write(_lodBias, "lodBias", 0.0f);
        capsule.write(_minificationFilter, "minificationFilter", MinificationFilter.NearestNeighborNoMipMaps);
        capsule.write(_magnificationFilter, "magnificationFilter", MagnificationFilter.Bilinear);
View Full Code Here

            TextureManager.loadFromKey(_key, null, this);
        }

        _constantColor.set((ColorRGBA) capsule.readSavable("constantColor", new ColorRGBA(ColorRGBA.BLACK_NO_ALPHA)));
        _borderColor.set((ColorRGBA) capsule.readSavable("borderColor", new ColorRGBA(ColorRGBA.BLACK_NO_ALPHA)));
        _texMatrix.set((Matrix4) capsule.readSavable("texMatrix", new Matrix4(Matrix4.IDENTITY)));
        _hasBorder = capsule.readBoolean("hasBorder", false);
        _anisotropicFilterPercent = capsule.readFloat("anisotropicFilterPercent", 0.0f);
        _lodBias = capsule.readFloat("lodBias", 0.0f);
        _magnificationFilter = capsule.readEnum("magnificationFilter", MagnificationFilter.class,
                MagnificationFilter.Bilinear);
View Full Code Here

    public TextureProjector() {
        super(1, 1);
    }

    public void updateTextureMatrix(final Texture texture) {
        final Matrix4 texMat = Matrix4.fetchTempInstance();
        updateTextureMatrix(texMat);
        texture.setTextureMatrix(texMat);
        Matrix4.releaseTempInstance(texMat);
    }
View Full Code Here

        }

        _transformBuffer.rewind();
        _transformBuffer.limit(nrOfFloats);

        final Matrix4 mat = Matrix4.fetchTempInstance();

        for (int i = 0; i < _maxBatchSize && _meshesToDraw > 0; i++) {
            final Mesh mesh = _visibleMeshes.get(--_meshesToDraw);
            final Matrix4 transform = mesh.getWorldTransform().getHomogeneousMatrix(mat);
            transform.toFloatBuffer(_transformBuffer, false);
        }

        Matrix4.releaseTempInstance(mat);
        _transformBuffer.rewind();
View Full Code Here

        // Add a texture to the scene.
        final TextureState ts = new TextureState();
        projectedTexture = TextureManager.load("images/ardor3d_white_256.jpg", Texture.MinificationFilter.Trilinear,
                true);
        ts.setTexture(projectedTexture);
        projectedTexture.setTextureMatrix(new Matrix4());
        projectedTexture.setWrap(Texture.WrapMode.BorderClamp);
        projectedTexture.setEnvironmentalMapMode(Texture.EnvironmentalMapMode.EyeLinear);
        projectedTexture.setApply(ApplyMode.Add);
        projectedTexture.setConstantColor(ColorRGBA.WHITE);
        _root.setRenderState(ts);
View Full Code Here

                    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) {
View Full Code Here

     *
     * @param transforms
     * @return
     */
    private Transform bakeTransforms(final List<TransformElement> transforms) {
        final Matrix4 workingMat = Matrix4.fetchTempInstance();
        final Matrix4 finalMat = Matrix4.fetchTempInstance();
        finalMat.setIdentity();
        for (final TransformElement transform : transforms) {
            final double[] array = transform.getArray();
            final TransformElementType type = transform.getType();
            if (type == TransformElementType.Translation) {
                workingMat.setIdentity();
                workingMat.setColumn(3, new Vector4(array[0], array[1], array[2], 1.0));
                finalMat.multiplyLocal(workingMat);
            } else if (type == TransformElementType.Rotation) {
                if (array[3] != 0) {
                    workingMat.setIdentity();
                    final Matrix3 rotate = new Matrix3().fromAngleAxis(array[3] * MathUtils.DEG_TO_RAD, new Vector3(
                            array[0], array[1], array[2]));
                    workingMat.set(rotate);
                    finalMat.multiplyLocal(workingMat);
                }
            } else if (type == TransformElementType.Scale) {
                workingMat.setIdentity();
                workingMat.scale(new Vector4(array[0], array[1], array[2], 1), workingMat);
                finalMat.multiplyLocal(workingMat);
            } else if (type == TransformElementType.Matrix) {
                workingMat.fromArray(array);
                finalMat.multiplyLocal(workingMat);
            } else if (type == TransformElementType.Lookat) {
                final Vector3 pos = new Vector3(array[0], array[1], array[2]);
                final Vector3 target = new Vector3(array[3], array[4], array[5]);
                final Vector3 up = new Vector3(array[6], array[7], array[8]);
                final Matrix3 rot = new Matrix3();
                rot.lookAt(target.subtractLocal(pos), up);
                workingMat.set(rot);
                workingMat.setColumn(3, new Vector4(array[0], array[1], array[2], 1.0));
                finalMat.multiplyLocal(workingMat);
            } else {
                if (logger.isLoggable(Level.WARNING)) {
                    logger.warning("transform not currently supported: " + transform.getClass().getCanonicalName());
                }
            }
View Full Code Here

TOP

Related Classes of com.ardor3d.math.Matrix4

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.