Java Code Examples for com.jme3.math.Vector3f#divideLocal()

The following examples show how to use com.jme3.math.Vector3f#divideLocal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VehicleWheel.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Apply this wheel's physics location and orientation to its associated
 * spatial, if any.
 */
public void applyWheelTransform() {
    if (wheelSpatial == null) {
        return;
    }
    Quaternion localRotationQuat = wheelSpatial.getLocalRotation();
    Vector3f localLocation = wheelSpatial.getLocalTranslation();
    if (!applyLocal && wheelSpatial.getParent() != null) {
        localLocation.set(wheelWorldLocation).subtractLocal(wheelSpatial.getParent().getWorldTranslation());
        localLocation.divideLocal(wheelSpatial.getParent().getWorldScale());
        tmp_inverseWorldRotation.set(wheelSpatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);

        localRotationQuat.set(wheelWorldRotation);
        tmp_inverseWorldRotation.set(wheelSpatial.getParent().getWorldRotation()).inverseLocal().mult(localRotationQuat, localRotationQuat);

        wheelSpatial.setLocalTranslation(localLocation);
        wheelSpatial.setLocalRotation(localRotationQuat);
    } else {
        wheelSpatial.setLocalTranslation(wheelWorldLocation);
        wheelSpatial.setLocalRotation(wheelWorldRotation);
    }
}
 
Example 2
Source File: SceneEditorController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void doMoveSpatial(Spatial selected, Vector3f translation) {
    Vector3f localTranslation = selected.getLocalTranslation();
    Vector3f before = new Vector3f(localTranslation);
    Node parent = selected.getParent();
    if (parent != null) {
        localTranslation.set(translation).subtractLocal(parent.getWorldTranslation());
        localTranslation.divideLocal(parent.getWorldScale());
        //TODO: reuse quaternion..
        new Quaternion().set(parent.getWorldRotation()).inverseLocal().multLocal(localTranslation);
    } else {
        localTranslation.set(translation);
    }
    Vector3f after = new Vector3f(localTranslation);
    selected.setLocalTranslation(localTranslation);
    AbstractSceneExplorerNode selectedSpat = this.selectedSpat;
    moveUndo(selected, before, after, selectedSpat);
}
 
Example 3
Source File: UVCoordinatesGenerator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * This method returns the bounding tube of the given mesh.
 * 
 * @param mesh
 *            the mesh
 * @return bounding tube of the given mesh
 */
/* package */static BoundingTube getBoundingTube(Mesh mesh) {
    Vector3f center = new Vector3f();
    float maxx = -Float.MAX_VALUE, minx = Float.MAX_VALUE;
    float maxy = -Float.MAX_VALUE, miny = Float.MAX_VALUE;
    float maxz = -Float.MAX_VALUE, minz = Float.MAX_VALUE;

    FloatBuffer positions = mesh.getFloatBuffer(VertexBuffer.Type.Position);
    int limit = positions.limit();
    for (int i = 0; i < limit; i += 3) {
        float x = positions.get(i);
        float y = positions.get(i + 1);
        float z = positions.get(i + 2);
        center.addLocal(x, y, z);
        maxx = x > maxx ? x : maxx;
        minx = x < minx ? x : minx;
        maxy = y > maxy ? y : maxy;
        miny = y < miny ? y : miny;
        maxz = z > maxz ? z : maxz;
        minz = z < minz ? z : minz;
    }
    center.divideLocal(limit / 3);

    float radius = Math.max(maxx - minx, maxy - miny) * 0.5f;
    return new BoundingTube(radius, maxz - minz, center);
}
 
Example 4
Source File: VehicleWheel.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public synchronized void applyWheelTransform() {
    if (wheelSpatial == null) {
        return;
    }
    Quaternion localRotationQuat = wheelSpatial.getLocalRotation();
    Vector3f localLocation = wheelSpatial.getLocalTranslation();
    if (!applyLocal && wheelSpatial.getParent() != null) {
        localLocation.set(wheelWorldLocation).subtractLocal(wheelSpatial.getParent().getWorldTranslation());
        localLocation.divideLocal(wheelSpatial.getParent().getWorldScale());
        tmp_inverseWorldRotation.set(wheelSpatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);

        localRotationQuat.set(wheelWorldRotation);
        tmp_inverseWorldRotation.set(wheelSpatial.getParent().getWorldRotation()).inverseLocal().mult(localRotationQuat, localRotationQuat);

        wheelSpatial.setLocalTranslation(localLocation);
        wheelSpatial.setLocalRotation(localRotationQuat);
    } else {
        wheelSpatial.setLocalTranslation(wheelWorldLocation);
        wheelSpatial.setLocalRotation(wheelWorldRotation);
    }
}
 
Example 5
Source File: CharacterControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void update(float tpf) {
    if (enabled && spatial != null) {
        Quaternion localRotationQuat = spatial.getLocalRotation();
        Vector3f localLocation = spatial.getLocalTranslation();
        if (!applyLocal && spatial.getParent() != null) {
            getPhysicsLocation(localLocation);
            localLocation.subtractLocal(spatial.getParent().getWorldTranslation());
            localLocation.divideLocal(spatial.getParent().getWorldScale());
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);
            spatial.setLocalTranslation(localLocation);

            if (useViewDirection) {
                localRotationQuat.lookAt(viewDirection, Vector3f.UNIT_Y);
                spatial.setLocalRotation(localRotationQuat);
            }
        } else {
            spatial.setLocalTranslation(getPhysicsLocation());
            localRotationQuat.lookAt(viewDirection, Vector3f.UNIT_Y);
            spatial.setLocalRotation(localRotationQuat);
        }
    }
}
 
Example 6
Source File: VehicleWheel.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public synchronized void applyWheelTransform() {
    if (wheelSpatial == null) {
        return;
    }
    Quaternion localRotationQuat = wheelSpatial.getLocalRotation();
    Vector3f localLocation = wheelSpatial.getLocalTranslation();
    if (!applyLocal && wheelSpatial.getParent() != null) {
        localLocation.set(wheelWorldLocation).subtractLocal(wheelSpatial.getParent().getWorldTranslation());
        localLocation.divideLocal(wheelSpatial.getParent().getWorldScale());
        tmp_inverseWorldRotation.set(wheelSpatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);

        localRotationQuat.set(wheelWorldRotation);
        tmp_inverseWorldRotation.set(wheelSpatial.getParent().getWorldRotation()).inverseLocal().mult(localRotationQuat, localRotationQuat);

        wheelSpatial.setLocalTranslation(localLocation);
        wheelSpatial.setLocalRotation(localRotationQuat);
    } else {
        wheelSpatial.setLocalTranslation(wheelWorldLocation);
        wheelSpatial.setLocalRotation(wheelWorldRotation);
    }
}
 
Example 7
Source File: CharacterControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void update(float tpf) {
    if (enabled && spatial != null) {
        Quaternion localRotationQuat = spatial.getLocalRotation();
        Vector3f localLocation = spatial.getLocalTranslation();
        if (!applyLocal && spatial.getParent() != null) {
            getPhysicsLocation(localLocation);
            localLocation.subtractLocal(spatial.getParent().getWorldTranslation());
            localLocation.divideLocal(spatial.getParent().getWorldScale());
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);
            spatial.setLocalTranslation(localLocation);

            if (useViewDirection) {
                localRotationQuat.lookAt(viewDirection, Vector3f.UNIT_Y);
                spatial.setLocalRotation(localRotationQuat);
            }
        } else {
            spatial.setLocalTranslation(getPhysicsLocation());
            localRotationQuat.lookAt(viewDirection, Vector3f.UNIT_Y);
            spatial.setLocalRotation(localRotationQuat);
        }
    }
}
 
Example 8
Source File: VehicleWheel.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void applyWheelTransform() {
    if (wheelSpatial == null) {
        return;
    }
    Quaternion localRotationQuat = wheelSpatial.getLocalRotation();
    Vector3f localLocation = wheelSpatial.getLocalTranslation();
    if (!applyLocal && wheelSpatial.getParent() != null) {
        localLocation.set(wheelWorldLocation).subtractLocal(wheelSpatial.getParent().getWorldTranslation());
        localLocation.divideLocal(wheelSpatial.getParent().getWorldScale());
        tmp_inverseWorldRotation.set(wheelSpatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);

        localRotationQuat.set(wheelWorldRotation);
        tmp_inverseWorldRotation.set(wheelSpatial.getParent().getWorldRotation()).inverseLocal().mult(localRotationQuat, localRotationQuat);

        wheelSpatial.setLocalTranslation(localLocation);
        wheelSpatial.setLocalRotation(localRotationQuat);
    } else {
        wheelSpatial.setLocalTranslation(wheelWorldLocation);
        wheelSpatial.setLocalRotation(wheelWorldRotation);
    }
}
 
Example 9
Source File: RagUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Convert a transform from the mesh coordinate system to the local
 * coordinate system of the specified bone.
 *
 * @param parentBone (not null)
 * @param transform the transform to convert (not null, modified)
 */
static void meshToLocal(Joint parentBone, Transform transform) {
    Vector3f location = transform.getTranslation();
    Quaternion orientation = transform.getRotation();
    Vector3f scale = transform.getScale();

    Transform pmx = parentBone.getModelTransform();
    Vector3f pmTranslate = pmx.getTranslation();
    Quaternion pmRotInv = pmx.getRotation().inverse();
    Vector3f pmScale = pmx.getScale();

    location.subtractLocal(pmTranslate);
    location.divideLocal(pmScale);
    pmRotInv.mult(location, location);
    scale.divideLocal(pmScale);
    pmRotInv.mult(orientation, orientation);
}
 
Example 10
Source File: AbstractPhysicsControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Apply a physics transform to the spatial.
 *
 * @param worldLocation location vector (in physics-space coordinates, not
 * null, unaffected)
 * @param worldRotation orientation (in physics-space coordinates, not null,
 * unaffected)
 */
protected void applyPhysicsTransform(Vector3f worldLocation, Quaternion worldRotation) {
    if (enabled && spatial != null) {
        Vector3f localLocation = spatial.getLocalTranslation();
        Quaternion localRotationQuat = spatial.getLocalRotation();
        if (!applyLocal && spatial.getParent() != null) {
            localLocation.set(worldLocation).subtractLocal(spatial.getParent().getWorldTranslation());
            localLocation.divideLocal(spatial.getParent().getWorldScale());
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);
            localRotationQuat.set(worldRotation);
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().mult(localRotationQuat, localRotationQuat);

            spatial.setLocalTranslation(localLocation);
            spatial.setLocalRotation(localRotationQuat);
        } else {
            spatial.setLocalTranslation(worldLocation);
            spatial.setLocalRotation(worldRotation);
        }
    }

}
 
Example 11
Source File: CharacterControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void update(float tpf) {
    if (enabled && spatial != null) {
        Quaternion localRotationQuat = spatial.getLocalRotation();
        Vector3f localLocation = spatial.getLocalTranslation();
        if (!applyLocal && spatial.getParent() != null) {
            getPhysicsLocation(localLocation);
            localLocation.subtractLocal(spatial.getParent().getWorldTranslation());
            localLocation.divideLocal(spatial.getParent().getWorldScale());
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);
            spatial.setLocalTranslation(localLocation);

            if (useViewDirection) {
                localRotationQuat.lookAt(viewDirection, Vector3f.UNIT_Y);
                spatial.setLocalRotation(localRotationQuat);
            }
        } else {
            spatial.setLocalTranslation(getPhysicsLocation());
            localRotationQuat.lookAt(viewDirection, Vector3f.UNIT_Y);
            spatial.setLocalRotation(localRotationQuat);
        }
    }
}
 
Example 12
Source File: AbstractPhysicsDebugControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Apply the specified location and orientation to the specified spatial.
 *
 * @param worldLocation location vector (in physics-space coordinates, not
 * null, unaffected)
 * @param worldRotation orientation (in physics-space coordinates, not null,
 * unaffected)
 * @param spatial where to apply (may be null)
 */
protected void applyPhysicsTransform(Vector3f worldLocation, Quaternion worldRotation, Spatial spatial) {
    if (spatial != null) {
        Vector3f localLocation = spatial.getLocalTranslation();
        Quaternion localRotationQuat = spatial.getLocalRotation();
        if (spatial.getParent() != null) {
            localLocation.set(worldLocation).subtractLocal(spatial.getParent().getWorldTranslation());
            localLocation.divideLocal(spatial.getParent().getWorldScale());
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);
            localRotationQuat.set(worldRotation);
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().mult(localRotationQuat, localRotationQuat);
            spatial.setLocalTranslation(localLocation);
            spatial.setLocalRotation(localRotationQuat);
        } else {
            spatial.setLocalTranslation(worldLocation);
            spatial.setLocalRotation(worldRotation);
        }
    }

}
 
Example 13
Source File: LightScatteringFilter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Vector3f getClipCoordinates(Vector3f worldPosition, Vector3f store, Camera cam) {

        float w = cam.getViewProjectionMatrix().multProj(worldPosition, store);
        store.divideLocal(w);

        store.x = ((store.x + 1f) * (cam.getViewPortRight() - cam.getViewPortLeft()) / 2f + cam.getViewPortLeft());
        store.y = ((store.y + 1f) * (cam.getViewPortTop() - cam.getViewPortBottom()) / 2f + cam.getViewPortBottom());
        store.z = (store.z + 1f) / 2f;

        return store;
    }
 
Example 14
Source File: RigidBodyMotionState.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
     * applies the current transform to the given jme Node if the location has been updated on the physics side
     * @param spatial
     */
    public synchronized boolean applyTransform(Spatial spatial) {
        Vector3f localLocation = spatial.getLocalTranslation();
        Quaternion localRotationQuat = spatial.getLocalRotation();
        boolean physicsLocationDirty = applyTransform(motionStateId, localLocation, localRotationQuat);
        if (!physicsLocationDirty) {
            return false;
        }
        if (!applyPhysicsLocal && spatial.getParent() != null) {
            localLocation.subtractLocal(spatial.getParent().getWorldTranslation());
            localLocation.divideLocal(spatial.getParent().getWorldScale());
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);

//            localRotationQuat.set(worldRotationQuat);
            tmp_inverseWorldRotation.mult(localRotationQuat, localRotationQuat);

            spatial.setLocalTranslation(localLocation);
            spatial.setLocalRotation(localRotationQuat);
        } else {
            spatial.setLocalTranslation(localLocation);
            spatial.setLocalRotation(localRotationQuat);
//            spatial.setLocalTranslation(worldLocation);
//            spatial.setLocalRotation(worldRotationQuat);
        }
        if (vehicle != null) {
            vehicle.updateWheels();
        }
        return true;
    }
 
Example 15
Source File: TextureHelper.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * This method converts the given texture into normal-map texture.
 * 
 * @param source
 *            the source texture
 * @param strengthFactor
 *            the normal strength factor
 * @return normal-map texture
 */
public Image convertToNormalMapTexture(Image source, float strengthFactor) {
    BufferedImage sourceImage = ImageToAwt.convert(source, false, false, 0);

    BufferedImage heightMap = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
    BufferedImage bumpMap = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
    ColorConvertOp gscale = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
    gscale.filter(sourceImage, heightMap);

    Vector3f S = new Vector3f();
    Vector3f T = new Vector3f();
    Vector3f N = new Vector3f();

    for (int x = 0; x < bumpMap.getWidth(); ++x) {
        for (int y = 0; y < bumpMap.getHeight(); ++y) {
            // generating bump pixel
            S.x = 1;
            S.y = 0;
            S.z = strengthFactor * this.getHeight(heightMap, x + 1, y) - strengthFactor * this.getHeight(heightMap, x - 1, y);
            T.x = 0;
            T.y = 1;
            T.z = strengthFactor * this.getHeight(heightMap, x, y + 1) - strengthFactor * this.getHeight(heightMap, x, y - 1);

            float den = (float) Math.sqrt(S.z * S.z + T.z * T.z + 1);
            N.x = -S.z;
            N.y = -T.z;
            N.z = 1;
            N.divideLocal(den);

            // setting thge pixel in the result image
            bumpMap.setRGB(x, y, this.vectorToColor(N.x, N.y, N.z));
        }
    }
    ByteBuffer byteBuffer = BufferUtils.createByteBuffer(source.getWidth() * source.getHeight() * 3);
    ImageToAwt.convert(bumpMap, Format.RGB8, byteBuffer);
    return new Image(Format.RGB8, source.getWidth(), source.getHeight(), byteBuffer);
}
 
Example 16
Source File: LightScatteringFilter.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private Vector3f getClipCoordinates(Vector3f worldPosition, Vector3f store, Camera cam) {

        float w = cam.getViewProjectionMatrix().multProj(worldPosition, store);
        store.divideLocal(w);

        store.x = ((store.x + 1f) * (cam.getViewPortRight() - cam.getViewPortLeft()) / 2f + cam.getViewPortLeft());
        store.y = ((store.y + 1f) * (cam.getViewPortTop() - cam.getViewPortBottom()) / 2f + cam.getViewPortBottom());
        store.z = (store.z + 1f) / 2f;

        return store;
    }
 
Example 17
Source File: RigidBodyMotionState.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
     * If the motion state has been updated, apply the new transform to the
     * specified spatial.
     *
     * @param spatial where to apply the physics transform (not null, modified)
     * @return true if changed
     */
    public boolean applyTransform(Spatial spatial) {
        Vector3f localLocation = spatial.getLocalTranslation();
        Quaternion localRotationQuat = spatial.getLocalRotation();
        boolean physicsLocationDirty = applyTransform(motionStateId, localLocation, localRotationQuat);
        if (!physicsLocationDirty) {
            return false;
        }
        if (!applyPhysicsLocal && spatial.getParent() != null) {
            localLocation.subtractLocal(spatial.getParent().getWorldTranslation());
            localLocation.divideLocal(spatial.getParent().getWorldScale());
            tmp_inverseWorldRotation.set(spatial.getParent().getWorldRotation()).inverseLocal().multLocal(localLocation);

//            localRotationQuat.set(worldRotationQuat);
            tmp_inverseWorldRotation.mult(localRotationQuat, localRotationQuat);

            spatial.setLocalTranslation(localLocation);
            spatial.setLocalRotation(localRotationQuat);
        } else {
            spatial.setLocalTranslation(localLocation);
            spatial.setLocalRotation(localRotationQuat);
//            spatial.setLocalTranslation(worldLocation);
//            spatial.setLocalRotation(worldRotationQuat);
        }
        if (vehicle != null) {
            vehicle.updateWheels();
        }
        return true;
    }
 
Example 18
Source File: ShadowUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Updates the points array to contain the frustum corners of the given
 * camera. The nearOverride and farOverride variables can be used to
 * override the camera's near/far values with own values.
 *
 * TODO: Reduce creation of new vectors
 *
 * @param viewCam
 * @param nearOverride
 * @param farOverride
 */
public static void updateFrustumPoints(Camera viewCam,
        float nearOverride,
        float farOverride,
        float scale,
        Vector3f[] points) {

    Vector3f pos = viewCam.getLocation();
    Vector3f dir = viewCam.getDirection();
    Vector3f up = viewCam.getUp();

    float depthHeightRatio = viewCam.getFrustumTop() / viewCam.getFrustumNear();
    float near = nearOverride;
    float far = farOverride;
    float ftop = viewCam.getFrustumTop();
    float fright = viewCam.getFrustumRight();
    float ratio = fright / ftop;

    float near_height;
    float near_width;
    float far_height;
    float far_width;

    if (viewCam.isParallelProjection()) {
        near_height = ftop;
        near_width = near_height * ratio;
        far_height = ftop;
        far_width = far_height * ratio;
    } else {
        near_height = depthHeightRatio * near;
        near_width = near_height * ratio;
        far_height = depthHeightRatio * far;
        far_width = far_height * ratio;
    }

    Vector3f right = dir.cross(up).normalizeLocal();

    Vector3f temp = new Vector3f();
    temp.set(dir).multLocal(far).addLocal(pos);
    Vector3f farCenter = temp.clone();
    temp.set(dir).multLocal(near).addLocal(pos);
    Vector3f nearCenter = temp.clone();

    Vector3f nearUp = temp.set(up).multLocal(near_height).clone();
    Vector3f farUp = temp.set(up).multLocal(far_height).clone();
    Vector3f nearRight = temp.set(right).multLocal(near_width).clone();
    Vector3f farRight = temp.set(right).multLocal(far_width).clone();

    points[0].set(nearCenter).subtractLocal(nearUp).subtractLocal(nearRight);
    points[1].set(nearCenter).addLocal(nearUp).subtractLocal(nearRight);
    points[2].set(nearCenter).addLocal(nearUp).addLocal(nearRight);
    points[3].set(nearCenter).subtractLocal(nearUp).addLocal(nearRight);

    points[4].set(farCenter).subtractLocal(farUp).subtractLocal(farRight);
    points[5].set(farCenter).addLocal(farUp).subtractLocal(farRight);
    points[6].set(farCenter).addLocal(farUp).addLocal(farRight);
    points[7].set(farCenter).subtractLocal(farUp).addLocal(farRight);

    if (scale != 1.0f) {
        // find center of frustum
        Vector3f center = new Vector3f();
        for (int i = 0; i < 8; i++) {
            center.addLocal(points[i]);
        }
        center.divideLocal(8f);

        Vector3f cDir = new Vector3f();
        for (int i = 0; i < 8; i++) {
            cDir.set(points[i]).subtractLocal(center);
            cDir.multLocal(scale - 1.0f);
            points[i].addLocal(cDir);
        }
    }
}
 
Example 19
Source File: ShadowUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Updates the points array to contain the frustum corners of the given
 * camera. The nearOverride and farOverride variables can be used
 * to override the camera's near/far values with own values.
 *
 * TODO: Reduce creation of new vectors
 *
 * @param viewCam
 * @param nearOverride
 * @param farOverride
 */
public static void updateFrustumPoints(Camera viewCam,
        float nearOverride,
        float farOverride,
        float scale,
        Vector3f[] points) {

    Vector3f pos = viewCam.getLocation();
    Vector3f dir = viewCam.getDirection();
    Vector3f up = viewCam.getUp();

    float depthHeightRatio = viewCam.getFrustumTop() / viewCam.getFrustumNear();
    float near = nearOverride;
    float far = farOverride;
    float ftop = viewCam.getFrustumTop();
    float fright = viewCam.getFrustumRight();
    float ratio = fright / ftop;

    float near_height;
    float near_width;
    float far_height;
    float far_width;

    if (viewCam.isParallelProjection()) {
        near_height = ftop;
        near_width = near_height * ratio;
        far_height = ftop;
        far_width = far_height * ratio;
    } else {
        near_height = depthHeightRatio * near;
        near_width = near_height * ratio;
        far_height = depthHeightRatio * far;
        far_width = far_height * ratio;
    }

    Vector3f right = dir.cross(up).normalizeLocal();

    Vector3f temp = new Vector3f();
    temp.set(dir).multLocal(far).addLocal(pos);
    Vector3f farCenter = temp.clone();
    temp.set(dir).multLocal(near).addLocal(pos);
    Vector3f nearCenter = temp.clone();

    Vector3f nearUp = temp.set(up).multLocal(near_height).clone();
    Vector3f farUp = temp.set(up).multLocal(far_height).clone();
    Vector3f nearRight = temp.set(right).multLocal(near_width).clone();
    Vector3f farRight = temp.set(right).multLocal(far_width).clone();

    points[0].set(nearCenter).subtractLocal(nearUp).subtractLocal(nearRight);
    points[1].set(nearCenter).addLocal(nearUp).subtractLocal(nearRight);
    points[2].set(nearCenter).addLocal(nearUp).addLocal(nearRight);
    points[3].set(nearCenter).subtractLocal(nearUp).addLocal(nearRight);

    points[4].set(farCenter).subtractLocal(farUp).subtractLocal(farRight);
    points[5].set(farCenter).addLocal(farUp).subtractLocal(farRight);
    points[6].set(farCenter).addLocal(farUp).addLocal(farRight);
    points[7].set(farCenter).subtractLocal(farUp).addLocal(farRight);

    if (scale != 1.0f) {
        // find center of frustum
        Vector3f center = new Vector3f();
        for (int i = 0; i < 8; i++) {
            center.addLocal(points[i]);
        }
        center.divideLocal(8f);

        Vector3f cDir = new Vector3f();
        for (int i = 0; i < 8; i++) {
            cDir.set(points[i]).subtractLocal(center);
            cDir.multLocal(scale - 1.0f);
            points[i].addLocal(cDir);
        }
    }
}