Java Code Examples for com.jme3.renderer.Camera#getUp()

The following examples show how to use com.jme3.renderer.Camera#getUp() . 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: UniformBindingManager.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void setCamera(Camera cam, Matrix4f viewMatrix, Matrix4f projMatrix, Matrix4f viewProjMatrix) {
    this.viewMatrix.set(viewMatrix);
    this.projMatrix.set(projMatrix);
    this.viewProjMatrix.set(viewProjMatrix);

    camLoc.set(cam.getLocation());
    cam.getLeft(camLeft);
    cam.getUp(camUp);
    cam.getDirection(camDir);

    near = cam.getFrustumNear();
    far = cam.getFrustumFar();
}
 
Example 2
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 3
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);
        }
    }
}