Search in sources :

Example 26 with TempVars

use of com.jme3.util.TempVars in project jmonkeyengine by jMonkeyEngine.

the class InstancedGeometry method updateInstances.

public void updateInstances() {
    FloatBuffer fb = (FloatBuffer) transformInstanceData.getData();
    fb.limit(fb.capacity());
    fb.position(0);
    TempVars vars = TempVars.get();
    {
        float[] temp = vars.matrixWrite;
        for (int i = 0; i < firstUnusedIndex; i++) {
            Geometry geom = geometries[i];
            if (geom == null) {
                geom = geometries[firstUnusedIndex - 1];
                if (geom == null) {
                    throw new AssertionError();
                }
                swap(i, firstUnusedIndex - 1);
                while (geometries[firstUnusedIndex - 1] == null) {
                    firstUnusedIndex--;
                }
            }
            Matrix4f worldMatrix = geom.getWorldMatrix();
            updateInstance(worldMatrix, temp, 0, vars.tempMat3, vars.quat1);
            fb.put(temp);
        }
    }
    vars.release();
    fb.flip();
    if (fb.limit() / INSTANCE_SIZE != firstUnusedIndex) {
        throw new AssertionError();
    }
    transformInstanceData.updateData(fb);
}
Also used : Geometry(com.jme3.scene.Geometry) Matrix4f(com.jme3.math.Matrix4f) FloatBuffer(java.nio.FloatBuffer) TempVars(com.jme3.util.TempVars)

Example 27 with TempVars

use of com.jme3.util.TempVars in project jmonkeyengine by jMonkeyEngine.

the class Camera method lookAt.

/**
     * <code>lookAt</code> is a convenience method for auto-setting the frame
     * based on a world position the user desires the camera to look at. It
     * repoints the camera towards the given position using the difference
     * between the position and the current camera location as a direction
     * vector and the worldUpVector to compute up and left camera vectors.
     *
     * @param pos           where to look at in terms of world coordinates
     * @param worldUpVector a normalized vector indicating the up direction of the world.
     *                      (typically {0, 1, 0} in jME.)
     */
public void lookAt(Vector3f pos, Vector3f worldUpVector) {
    TempVars vars = TempVars.get();
    Vector3f newDirection = vars.vect1;
    Vector3f newUp = vars.vect2;
    Vector3f newLeft = vars.vect3;
    newDirection.set(pos).subtractLocal(location).normalizeLocal();
    newUp.set(worldUpVector).normalizeLocal();
    if (newUp.equals(Vector3f.ZERO)) {
        newUp.set(Vector3f.UNIT_Y);
    }
    newLeft.set(newUp).crossLocal(newDirection).normalizeLocal();
    if (newLeft.equals(Vector3f.ZERO)) {
        if (newDirection.x != 0) {
            newLeft.set(newDirection.y, -newDirection.x, 0f);
        } else {
            newLeft.set(0f, newDirection.z, -newDirection.y);
        }
    }
    newUp.set(newDirection).crossLocal(newLeft).normalizeLocal();
    this.rotation.fromAxes(newLeft, newUp, newDirection);
    this.rotation.normalizeLocal();
    vars.release();
    onFrameChange();
}
Also used : TempVars(com.jme3.util.TempVars)

Example 28 with TempVars

use of com.jme3.util.TempVars in project jmonkeyengine by jMonkeyEngine.

the class Camera method onFrameChange.

/**
     * <code>onFrameChange</code> updates the view frame of the camera.
     */
public void onFrameChange() {
    TempVars vars = TempVars.get();
    Vector3f left = getLeft(vars.vect1);
    Vector3f direction = getDirection(vars.vect2);
    Vector3f up = getUp(vars.vect3);
    float dirDotLocation = direction.dot(location);
    // left plane
    Vector3f leftPlaneNormal = worldPlane[LEFT_PLANE].getNormal();
    leftPlaneNormal.x = left.x * coeffLeft[0];
    leftPlaneNormal.y = left.y * coeffLeft[0];
    leftPlaneNormal.z = left.z * coeffLeft[0];
    leftPlaneNormal.addLocal(direction.x * coeffLeft[1], direction.y * coeffLeft[1], direction.z * coeffLeft[1]);
    worldPlane[LEFT_PLANE].setConstant(location.dot(leftPlaneNormal));
    // right plane
    Vector3f rightPlaneNormal = worldPlane[RIGHT_PLANE].getNormal();
    rightPlaneNormal.x = left.x * coeffRight[0];
    rightPlaneNormal.y = left.y * coeffRight[0];
    rightPlaneNormal.z = left.z * coeffRight[0];
    rightPlaneNormal.addLocal(direction.x * coeffRight[1], direction.y * coeffRight[1], direction.z * coeffRight[1]);
    worldPlane[RIGHT_PLANE].setConstant(location.dot(rightPlaneNormal));
    // bottom plane
    Vector3f bottomPlaneNormal = worldPlane[BOTTOM_PLANE].getNormal();
    bottomPlaneNormal.x = up.x * coeffBottom[0];
    bottomPlaneNormal.y = up.y * coeffBottom[0];
    bottomPlaneNormal.z = up.z * coeffBottom[0];
    bottomPlaneNormal.addLocal(direction.x * coeffBottom[1], direction.y * coeffBottom[1], direction.z * coeffBottom[1]);
    worldPlane[BOTTOM_PLANE].setConstant(location.dot(bottomPlaneNormal));
    // top plane
    Vector3f topPlaneNormal = worldPlane[TOP_PLANE].getNormal();
    topPlaneNormal.x = up.x * coeffTop[0];
    topPlaneNormal.y = up.y * coeffTop[0];
    topPlaneNormal.z = up.z * coeffTop[0];
    topPlaneNormal.addLocal(direction.x * coeffTop[1], direction.y * coeffTop[1], direction.z * coeffTop[1]);
    worldPlane[TOP_PLANE].setConstant(location.dot(topPlaneNormal));
    if (isParallelProjection()) {
        worldPlane[LEFT_PLANE].setConstant(worldPlane[LEFT_PLANE].getConstant() + frustumLeft);
        worldPlane[RIGHT_PLANE].setConstant(worldPlane[RIGHT_PLANE].getConstant() - frustumRight);
        worldPlane[TOP_PLANE].setConstant(worldPlane[TOP_PLANE].getConstant() - frustumTop);
        worldPlane[BOTTOM_PLANE].setConstant(worldPlane[BOTTOM_PLANE].getConstant() + frustumBottom);
    }
    // far plane
    worldPlane[FAR_PLANE].setNormal(left);
    worldPlane[FAR_PLANE].setNormal(-direction.x, -direction.y, -direction.z);
    worldPlane[FAR_PLANE].setConstant(-(dirDotLocation + frustumFar));
    // near plane
    worldPlane[NEAR_PLANE].setNormal(direction.x, direction.y, direction.z);
    worldPlane[NEAR_PLANE].setConstant(dirDotLocation + frustumNear);
    viewMatrix.fromFrame(location, direction, up, left);
    vars.release();
    //        viewMatrix.transposeLocal();
    updateViewProjection();
}
Also used : TempVars(com.jme3.util.TempVars)

Example 29 with TempVars

use of com.jme3.util.TempVars in project jmonkeyengine by jMonkeyEngine.

the class BatchNode method doTransformsTangents.

private void doTransformsTangents(FloatBuffer bindBufPos, FloatBuffer bindBufNorm, FloatBuffer bindBufTangents, FloatBuffer bufPos, FloatBuffer bufNorm, FloatBuffer bufTangents, int start, int end, Matrix4f transform) {
    TempVars vars = TempVars.get();
    Vector3f pos = vars.vect1;
    Vector3f norm = vars.vect2;
    Vector3f tan = vars.vect3;
    int length = (end - start) * 3;
    int tanLength = (end - start) * 4;
    // offset is given in element units
    // convert to be in component units
    int offset = start * 3;
    int tanOffset = start * 4;
    bindBufPos.rewind();
    bindBufNorm.rewind();
    bindBufTangents.rewind();
    bindBufPos.get(tmpFloat, 0, length);
    bindBufNorm.get(tmpFloatN, 0, length);
    bindBufTangents.get(tmpFloatT, 0, tanLength);
    int index = 0;
    int tanIndex = 0;
    while (index < length) {
        pos.x = tmpFloat[index];
        norm.x = tmpFloatN[index++];
        pos.y = tmpFloat[index];
        norm.y = tmpFloatN[index++];
        pos.z = tmpFloat[index];
        norm.z = tmpFloatN[index];
        tan.x = tmpFloatT[tanIndex++];
        tan.y = tmpFloatT[tanIndex++];
        tan.z = tmpFloatT[tanIndex++];
        transform.mult(pos, pos);
        transform.multNormal(norm, norm);
        transform.multNormal(tan, tan);
        index -= 2;
        tanIndex -= 3;
        tmpFloat[index] = pos.x;
        tmpFloatN[index++] = norm.x;
        tmpFloat[index] = pos.y;
        tmpFloatN[index++] = norm.y;
        tmpFloat[index] = pos.z;
        tmpFloatN[index++] = norm.z;
        tmpFloatT[tanIndex++] = tan.x;
        tmpFloatT[tanIndex++] = tan.y;
        tmpFloatT[tanIndex++] = tan.z;
        //Skipping 4th element of tangent buffer (handedness)
        tanIndex++;
    }
    vars.release();
    bufPos.position(offset);
    //using bulk put as it's faster
    bufPos.put(tmpFloat, 0, length);
    bufNorm.position(offset);
    //using bulk put as it's faster
    bufNorm.put(tmpFloatN, 0, length);
    bufTangents.position(tanOffset);
    //using bulk put as it's faster
    bufTangents.put(tmpFloatT, 0, tanLength);
}
Also used : Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 30 with TempVars

use of com.jme3.util.TempVars in project jmonkeyengine by jMonkeyEngine.

the class BatchNode method doTransforms.

private void doTransforms(FloatBuffer bindBufPos, FloatBuffer bindBufNorm, FloatBuffer bufPos, FloatBuffer bufNorm, int start, int end, Matrix4f transform) {
    TempVars vars = TempVars.get();
    Vector3f pos = vars.vect1;
    Vector3f norm = vars.vect2;
    int length = (end - start) * 3;
    // offset is given in element units
    // convert to be in component units
    int offset = start * 3;
    bindBufPos.rewind();
    bindBufNorm.rewind();
    //bufPos.position(offset);
    //bufNorm.position(offset);
    bindBufPos.get(tmpFloat, 0, length);
    bindBufNorm.get(tmpFloatN, 0, length);
    int index = 0;
    while (index < length) {
        pos.x = tmpFloat[index];
        norm.x = tmpFloatN[index++];
        pos.y = tmpFloat[index];
        norm.y = tmpFloatN[index++];
        pos.z = tmpFloat[index];
        norm.z = tmpFloatN[index];
        transform.mult(pos, pos);
        transform.multNormal(norm, norm);
        index -= 2;
        tmpFloat[index] = pos.x;
        tmpFloatN[index++] = norm.x;
        tmpFloat[index] = pos.y;
        tmpFloatN[index++] = norm.y;
        tmpFloat[index] = pos.z;
        tmpFloatN[index++] = norm.z;
    }
    vars.release();
    bufPos.position(offset);
    //using bulk put as it's faster
    bufPos.put(tmpFloat, 0, length);
    bufNorm.position(offset);
    //using bulk put as it's faster
    bufNorm.put(tmpFloatN, 0, length);
}
Also used : Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Aggregations

TempVars (com.jme3.util.TempVars)103 Vector3f (com.jme3.math.Vector3f)50 Quaternion (com.jme3.math.Quaternion)13 Matrix4f (com.jme3.math.Matrix4f)12 BoundingBox (com.jme3.bounding.BoundingBox)10 Bone (com.jme3.animation.Bone)8 Spatial (com.jme3.scene.Spatial)7 CollisionResult (com.jme3.collision.CollisionResult)6 Vector2f (com.jme3.math.Vector2f)6 FloatBuffer (java.nio.FloatBuffer)6 BoundingSphere (com.jme3.bounding.BoundingSphere)5 BoundingVolume (com.jme3.bounding.BoundingVolume)5 Transform (com.jme3.math.Transform)5 DirectionalLight (com.jme3.light.DirectionalLight)4 PointLight (com.jme3.light.PointLight)4 Geometry (com.jme3.scene.Geometry)4 SpotLight (com.jme3.light.SpotLight)3 ColorRGBA (com.jme3.math.ColorRGBA)3 Matrix3f (com.jme3.math.Matrix3f)3 Vector4f (com.jme3.math.Vector4f)3