Search in sources :

Example 36 with Vector3f

use of com.jme3.math.Vector3f in project jmonkeyengine by jMonkeyEngine.

the class MeshBuffers method getPositionsBuffer.

/**
     * @return positions buffer
     */
public VertexBuffer getPositionsBuffer() {
    VertexBuffer positionBuffer = new VertexBuffer(Type.Position);
    Vector3f[] data = verts.toArray(new Vector3f[verts.size()]);
    positionBuffer.setupData(Usage.Static, 3, Format.Float, BufferUtils.createFloatBuffer(data));
    return positionBuffer;
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) Vector3f(com.jme3.math.Vector3f)

Example 37 with Vector3f

use of com.jme3.math.Vector3f in project jmonkeyengine by jMonkeyEngine.

the class MeshHelper method loadVerticesAndNormals.

/**
     * This method returns the vertices.
     * 
     * @param meshStructure
     *            the structure containing the mesh data
     * @return a list of two - element arrays, the first element is the vertex and the second - its normal
     * @throws BlenderFileException
     *             this exception is thrown when the blend file structure is somehow invalid or corrupted
     */
@SuppressWarnings("unchecked")
public void loadVerticesAndNormals(Structure meshStructure, List<Vector3f> vertices, List<Vector3f> normals) throws BlenderFileException {
    LOGGER.log(Level.FINE, "Loading vertices and normals from mesh: {0}.", meshStructure.getName());
    int count = ((Number) meshStructure.getFieldValue("totvert")).intValue();
    if (count > 0) {
        Pointer pMVert = (Pointer) meshStructure.getFieldValue("mvert");
        List<Structure> mVerts = pMVert.fetchData();
        Vector3f co = null, no = null;
        if (fixUpAxis) {
            for (int i = 0; i < count; ++i) {
                DynamicArray<Number> coordinates = (DynamicArray<Number>) mVerts.get(i).getFieldValue("co");
                co = new Vector3f(coordinates.get(0).floatValue(), coordinates.get(2).floatValue(), -coordinates.get(1).floatValue());
                vertices.add(co);
                DynamicArray<Number> norm = (DynamicArray<Number>) mVerts.get(i).getFieldValue("no");
                no = new Vector3f(norm.get(0).shortValue() / 32767.0f, norm.get(2).shortValue() / 32767.0f, -norm.get(1).shortValue() / 32767.0f);
                normals.add(no);
            }
        } else {
            for (int i = 0; i < count; ++i) {
                DynamicArray<Number> coordinates = (DynamicArray<Number>) mVerts.get(i).getFieldValue("co");
                co = new Vector3f(coordinates.get(0).floatValue(), coordinates.get(1).floatValue(), coordinates.get(2).floatValue());
                vertices.add(co);
                DynamicArray<Number> norm = (DynamicArray<Number>) mVerts.get(i).getFieldValue("no");
                no = new Vector3f(norm.get(0).shortValue() / 32767.0f, norm.get(1).shortValue() / 32767.0f, norm.get(2).shortValue() / 32767.0f);
                normals.add(no);
            }
        }
    }
    LOGGER.log(Level.FINE, "Loaded {0} vertices and normals.", vertices.size());
}
Also used : DynamicArray(com.jme3.scene.plugins.blender.file.DynamicArray) Vector3f(com.jme3.math.Vector3f) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure)

Example 38 with Vector3f

use of com.jme3.math.Vector3f in project jmonkeyengine by jMonkeyEngine.

the class ArrayModifier method apply.

@Override
public void apply(Node node, BlenderContext blenderContext) {
    if (invalid) {
        LOGGER.log(Level.WARNING, "Array modifier is invalid! Cannot be applied to: {0}", node.getName());
    } else {
        TemporalMesh temporalMesh = this.getTemporalMesh(node);
        if (temporalMesh != null) {
            LOGGER.log(Level.FINE, "Applying array modifier to: {0}", temporalMesh);
            if (offset == null) {
                // the node will be repeated several times in the same place
                offset = new float[] { 0.0f, 0.0f, 0.0f };
            }
            if (scale == null) {
                // the node will be repeated several times in the same place
                scale = new float[] { 0.0f, 0.0f, 0.0f };
            } else {
                // getting bounding box
                temporalMesh.updateModelBound();
                BoundingVolume boundingVolume = temporalMesh.getWorldBound();
                if (boundingVolume instanceof BoundingBox) {
                    scale[0] *= ((BoundingBox) boundingVolume).getXExtent() * 2.0f;
                    scale[1] *= ((BoundingBox) boundingVolume).getYExtent() * 2.0f;
                    scale[2] *= ((BoundingBox) boundingVolume).getZExtent() * 2.0f;
                } else if (boundingVolume instanceof BoundingSphere) {
                    float radius = ((BoundingSphere) boundingVolume).getRadius();
                    scale[0] *= radius * 2.0f;
                    scale[1] *= radius * 2.0f;
                    scale[2] *= radius * 2.0f;
                } else {
                    throw new IllegalStateException("Unknown bounding volume type: " + boundingVolume.getClass().getName());
                }
            }
            // adding object's offset
            float[] objectOffset = new float[] { 0.0f, 0.0f, 0.0f };
            if (pOffsetObject != null && pOffsetObject.isNotNull()) {
                FileBlockHeader offsetObjectBlock = blenderContext.getFileBlock(pOffsetObject.getOldMemoryAddress());
                ObjectHelper objectHelper = blenderContext.getHelper(ObjectHelper.class);
                try {
                    // we take the structure in case the object was not yet loaded
                    Structure offsetStructure = offsetObjectBlock.getStructure(blenderContext);
                    Vector3f translation = objectHelper.getTransformation(offsetStructure, blenderContext).getTranslation();
                    objectOffset[0] = translation.x;
                    objectOffset[1] = translation.y;
                    objectOffset[2] = translation.z;
                } catch (BlenderFileException e) {
                    LOGGER.log(Level.WARNING, "Problems in blender file structure! Object offset cannot be applied! The problem: {0}", e.getMessage());
                }
            }
            // getting start and end caps
            MeshHelper meshHelper = blenderContext.getHelper(MeshHelper.class);
            TemporalMesh[] caps = new TemporalMesh[] { null, null };
            Pointer[] pCaps = new Pointer[] { pStartCap, pEndCap };
            for (int i = 0; i < pCaps.length; ++i) {
                if (pCaps[i].isNotNull()) {
                    FileBlockHeader capBlock = blenderContext.getFileBlock(pCaps[i].getOldMemoryAddress());
                    try {
                        // we take the structure in case the object was not yet loaded
                        Structure capStructure = capBlock.getStructure(blenderContext);
                        Pointer pMesh = (Pointer) capStructure.getFieldValue("data");
                        List<Structure> meshesArray = pMesh.fetchData();
                        caps[i] = meshHelper.toTemporalMesh(meshesArray.get(0), blenderContext);
                    } catch (BlenderFileException e) {
                        LOGGER.log(Level.WARNING, "Problems in blender file structure! Cap object cannot be applied! The problem: {0}", e.getMessage());
                    }
                }
            }
            Vector3f translationVector = new Vector3f(offset[0] + scale[0] + objectOffset[0], offset[1] + scale[1] + objectOffset[1], offset[2] + scale[2] + objectOffset[2]);
            if (blenderContext.getBlenderKey().isFixUpAxis()) {
                float y = translationVector.y;
                translationVector.y = translationVector.z;
                translationVector.z = y == 0 ? 0 : -y;
            }
            // getting/calculating repeats amount
            int count = 0;
            if (fittype == 0) {
                // Fixed count
                count = this.count - 1;
            } else if (fittype == 1) {
                // Fixed length
                float length = this.length;
                if (translationVector.length() > 0.0f) {
                    count = (int) (length / translationVector.length()) - 1;
                }
            } else if (fittype == 2) {
                // Fit curve
                throw new IllegalStateException("Fit curve should be transformed to Fixed Length array type!");
            } else {
                throw new IllegalStateException("Unknown fit type: " + fittype);
            }
            // adding translated nodes and caps
            Vector3f totalTranslation = new Vector3f(translationVector);
            if (count > 0) {
                TemporalMesh originalMesh = temporalMesh.clone();
                for (int i = 0; i < count; ++i) {
                    TemporalMesh clone = originalMesh.clone();
                    for (Vector3f v : clone.getVertices()) {
                        v.addLocal(totalTranslation);
                    }
                    temporalMesh.append(clone);
                    totalTranslation.addLocal(translationVector);
                }
            }
            if (caps[0] != null) {
                translationVector.multLocal(-1);
                TemporalMesh capsClone = caps[0].clone();
                for (Vector3f v : capsClone.getVertices()) {
                    v.addLocal(translationVector);
                }
                temporalMesh.append(capsClone);
            }
            if (caps[1] != null) {
                TemporalMesh capsClone = caps[1].clone();
                for (Vector3f v : capsClone.getVertices()) {
                    v.addLocal(totalTranslation);
                }
                temporalMesh.append(capsClone);
            }
        } else {
            LOGGER.log(Level.WARNING, "Cannot find temporal mesh for node: {0}. The modifier will NOT be applied!", node);
        }
    }
}
Also used : ObjectHelper(com.jme3.scene.plugins.blender.objects.ObjectHelper) BoundingSphere(com.jme3.bounding.BoundingSphere) FileBlockHeader(com.jme3.scene.plugins.blender.file.FileBlockHeader) BlenderFileException(com.jme3.scene.plugins.blender.file.BlenderFileException) Pointer(com.jme3.scene.plugins.blender.file.Pointer) TemporalMesh(com.jme3.scene.plugins.blender.meshes.TemporalMesh) BoundingBox(com.jme3.bounding.BoundingBox) Vector3f(com.jme3.math.Vector3f) BoundingVolume(com.jme3.bounding.BoundingVolume) Structure(com.jme3.scene.plugins.blender.file.Structure) MeshHelper(com.jme3.scene.plugins.blender.meshes.MeshHelper)

Example 39 with Vector3f

use of com.jme3.math.Vector3f in project jmonkeyengine by jMonkeyEngine.

the class BetterCharacterControl method read.

@Override
public void read(JmeImporter im) throws IOException {
    super.read(im);
    InputCapsule in = im.getCapsule(this);
    this.radius = in.readFloat("radius", 1);
    this.height = in.readFloat("height", 2);
    this.mass = in.readFloat("mass", 80);
    this.physicsDamping = in.readFloat("physicsDamping", 0.9f);
    this.jumpForce.set((Vector3f) in.readSavable("jumpForce", new Vector3f(0, mass * 5, 0)));
    rigidBody = new PhysicsRigidBody(getShape(), mass);
    jumpForce.set(new Vector3f(0, mass * 5, 0));
    rigidBody.setAngularFactor(0);
}
Also used : InputCapsule(com.jme3.export.InputCapsule) Vector3f(com.jme3.math.Vector3f) PhysicsRigidBody(com.jme3.bullet.objects.PhysicsRigidBody)

Example 40 with Vector3f

use of com.jme3.math.Vector3f in project jmonkeyengine by jMonkeyEngine.

the class BetterCharacterControl method checkCanUnDuck.

/**
     * This checks if the character can go from ducked to unducked state by
     * doing a ray test.
     */
protected boolean checkCanUnDuck() {
    TempVars vars = TempVars.get();
    Vector3f location = vars.vect1;
    Vector3f rayVector = vars.vect2;
    location.set(localUp).multLocal(FastMath.ZERO_TOLERANCE).addLocal(this.location);
    rayVector.set(localUp).multLocal(height + FastMath.ZERO_TOLERANCE).addLocal(location);
    List<PhysicsRayTestResult> results = space.rayTest(location, rayVector);
    vars.release();
    for (PhysicsRayTestResult physicsRayTestResult : results) {
        if (!physicsRayTestResult.getCollisionObject().equals(rigidBody)) {
            return false;
        }
    }
    return true;
}
Also used : PhysicsRayTestResult(com.jme3.bullet.collision.PhysicsRayTestResult) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Aggregations

Vector3f (com.jme3.math.Vector3f)536 Material (com.jme3.material.Material)126 Geometry (com.jme3.scene.Geometry)118 DirectionalLight (com.jme3.light.DirectionalLight)95 Quaternion (com.jme3.math.Quaternion)94 TempVars (com.jme3.util.TempVars)67 ColorRGBA (com.jme3.math.ColorRGBA)64 Node (com.jme3.scene.Node)63 Spatial (com.jme3.scene.Spatial)57 Box (com.jme3.scene.shape.Box)57 Sphere (com.jme3.scene.shape.Sphere)51 Texture (com.jme3.texture.Texture)41 KeyTrigger (com.jme3.input.controls.KeyTrigger)36 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)35 Vector2f (com.jme3.math.Vector2f)35 FilterPostProcessor (com.jme3.post.FilterPostProcessor)34 FloatBuffer (java.nio.FloatBuffer)34 InputCapsule (com.jme3.export.InputCapsule)33 BoundingBox (com.jme3.bounding.BoundingBox)30 AmbientLight (com.jme3.light.AmbientLight)30