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

The following examples show how to use com.jme3.renderer.Camera#setFrustum() . 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: ShadowCamera.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Updates the camera view direction and position based on the light
 */
public void updateLightCamera(Camera lightCam) {
    if (target.getType() == Light.Type.Directional) {
        DirectionalLight dl = (DirectionalLight) target;
        lightCam.setParallelProjection(true);
        lightCam.setLocation(Vector3f.ZERO);
        lightCam.lookAtDirection(dl.getDirection(), Vector3f.UNIT_Y);
        lightCam.setFrustum(-1, 1, -1, 1, 1, -1);
    } else {
        PointLight pl = (PointLight) target;
        lightCam.setParallelProjection(false);
        lightCam.setLocation(pl.getPosition());
        // direction will have to be calculated automatically
        lightCam.setFrustumPerspective(45, 1, 1, 300);
    }
}
 
Example 2
Source File: WaterUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void updateReflectionCam(Camera reflectionCam, Plane plane, Camera sceneCam){
    
    TempVars vars = TempVars.get();
     //Temp vects for reflection cam orientation calculation
    Vector3f sceneTarget =  vars.vect1;
    Vector3f  reflectDirection =  vars.vect2;
    Vector3f  reflectUp =  vars.vect3;
    Vector3f  reflectLeft = vars.vect4;
    Vector3f  camLoc = vars.vect5;
    camLoc = plane.reflect(sceneCam.getLocation(), camLoc);
    reflectionCam.setLocation(camLoc);
    reflectionCam.setFrustum(sceneCam.getFrustumNear(),
            sceneCam.getFrustumFar(),
            sceneCam.getFrustumLeft(),
            sceneCam.getFrustumRight(),
            sceneCam.getFrustumTop(),
            sceneCam.getFrustumBottom());
    reflectionCam.setParallelProjection(sceneCam.isParallelProjection());

    sceneTarget.set(sceneCam.getLocation()).addLocal(sceneCam.getDirection(vars.vect6));
    reflectDirection = plane.reflect(sceneTarget, reflectDirection);
    reflectDirection.subtractLocal(camLoc);

    sceneTarget.set(sceneCam.getLocation()).subtractLocal(sceneCam.getUp(vars.vect6));
    reflectUp = plane.reflect(sceneTarget, reflectUp);
    reflectUp.subtractLocal(camLoc);

    sceneTarget.set(sceneCam.getLocation()).addLocal(sceneCam.getLeft(vars.vect6));
    reflectLeft = plane.reflect(sceneTarget, reflectLeft);
    reflectLeft.subtractLocal(camLoc);

    reflectionCam.setAxes(reflectLeft, reflectUp, reflectDirection);

    vars.release();
}
 
Example 3
Source File: ViewPortDemoState.java    From Lemur with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 *  Creates an ortho viewport in the specified section of the screen.
 *  The viewport will be scaled such that adding things to its root
 *  scene will be in 1:1 pixel space.
 */
protected void createGuiViewPort( int x1, int y1, int x2, int y2, ColorRGBA bgColor ) {
 
    if( vp != null ) {
        disposeViewPort();
    }

    log.info("Creating demo GUI ViewPort"); 

    // Setup the viewport for a cloned camera
    Camera cam = getApplication().getCamera().clone();        
    float width = cam.getWidth();
    float height = cam.getHeight();
    cam.setViewPort(x1/width, x2/width, y1/height, y2/height);

    // Setup the ortho project.  I've found it doesn't play
    // nice unless the range spans 0.  We'll move its root node
    // so that 0,0 is the lower left corner like the regular gui bucket.
    float near = -1000;
    float far = 1000;
    float w = x2 - x1;
    float h = y2 - y1;        
    cam.setParallelProjection(true);
    cam.setFrustum(near, far, -w/2, w/2, h/2, -h/2);
    
    // Create the actual viewport
    vp = getApplication().getRenderManager().createPostView("ViewPort Demo", cam);
    vp.setClearFlags(true, true, true);
    vp.setBackgroundColor(bgColor);

    // We want the transparent bucket to act exactly like the GUI
    // bucket with respect to back-to-front sorting.
    vp.getQueue().setGeometryComparator(Bucket.Transparent, new GuiComparator());
 
    // Give it a root node that we can use to attach things.
    // Translate it so that 0,0 is the lower left corner.
    // Note: the is NOT in the Gui Bucket... but the transparent bucket.
    vpRoot = new Node("VP Root");
    vpRoot.setQueueBucket(Bucket.Transparent);
    vpRoot.setLocalTranslation(-w/2, -h/2, 0);
    vp.attachScene(vpRoot);
 
    // Let Lemur know to do picking in this viewport       
    getState(PickState.class).addCollisionRoot(vp, PickState.PICK_LAYER_GUI);
}
 
Example 4
Source File: ShadowUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
     * Updates the shadow camera to properly contain the given points (which
     * contain the eye camera frustum corners)
     *
     * @param shadowCam
     * @param points
     */
    public static void updateShadowCamera(Camera shadowCam, Vector3f[] points) {
        boolean ortho = shadowCam.isParallelProjection();
        shadowCam.setProjectionMatrix(null);

        if (ortho) {
            shadowCam.setFrustum(-1, 1, -1, 1, 1, -1);
        } else {
            shadowCam.setFrustumPerspective(45, 1, 1, 150);
        }

        Matrix4f viewProjMatrix = shadowCam.getViewProjectionMatrix();
        Matrix4f projMatrix = shadowCam.getProjectionMatrix();

        BoundingBox splitBB = computeBoundForPoints(points, viewProjMatrix);

        TempVars vars = TempVars.get();

        Vector3f splitMin = splitBB.getMin(vars.vect1);
        Vector3f splitMax = splitBB.getMax(vars.vect2);

//        splitMin.z = 0;

        // Create the crop matrix.
        float scaleX, scaleY, scaleZ;
        float offsetX, offsetY, offsetZ;

        scaleX = 2.0f / (splitMax.x - splitMin.x);
        scaleY = 2.0f / (splitMax.y - splitMin.y);
        offsetX = -0.5f * (splitMax.x + splitMin.x) * scaleX;
        offsetY = -0.5f * (splitMax.y + splitMin.y) * scaleY;
        scaleZ = 1.0f / (splitMax.z - splitMin.z);
        offsetZ = -splitMin.z * scaleZ;

        Matrix4f cropMatrix = vars.tempMat4;
        cropMatrix.set(scaleX, 0f, 0f, offsetX,
                0f, scaleY, 0f, offsetY,
                0f, 0f, scaleZ, offsetZ,
                0f, 0f, 0f, 1f);


        Matrix4f result = new Matrix4f();
        result.set(cropMatrix);
        result.multLocal(projMatrix);

        vars.release();
        shadowCam.setProjectionMatrix(result);
    }
 
Example 5
Source File: ShadowUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
     * Updates the shadow camera to properly contain the given
     * points (which contain the eye camera frustum corners)
     *
     * @param occluders
     * @param lightCam
     * @param points
     */
    public static void updateShadowCamera(Camera shadowCam, Vector3f[] points) {
        boolean ortho = shadowCam.isParallelProjection();
        shadowCam.setProjectionMatrix(null);

        if (ortho) {
            shadowCam.setFrustum(-1, 1, -1, 1, 1, -1);
        } else {
            shadowCam.setFrustumPerspective(45, 1, 1, 150);
        }

        Matrix4f viewProjMatrix = shadowCam.getViewProjectionMatrix();
        Matrix4f projMatrix = shadowCam.getProjectionMatrix();

        BoundingBox splitBB = computeBoundForPoints(points, viewProjMatrix);

        Vector3f splitMin = splitBB.getMin(null);
        Vector3f splitMax = splitBB.getMax(null);

//        splitMin.z = 0;

        // Create the crop matrix.
        float scaleX, scaleY, scaleZ;
        float offsetX, offsetY, offsetZ;

        scaleX = 2.0f / (splitMax.x - splitMin.x);
        scaleY = 2.0f / (splitMax.y - splitMin.y);
        offsetX = -0.5f * (splitMax.x + splitMin.x) * scaleX;
        offsetY = -0.5f * (splitMax.y + splitMin.y) * scaleY;
        scaleZ = 1.0f / (splitMax.z - splitMin.z);
        offsetZ = -splitMin.z * scaleZ;

        Matrix4f cropMatrix = new Matrix4f(scaleX, 0f, 0f, offsetX,
                0f, scaleY, 0f, offsetY,
                0f, 0f, scaleZ, offsetZ,
                0f, 0f, 0f, 1f);


        Matrix4f result = new Matrix4f();
        result.set(cropMatrix);
        result.multLocal(projMatrix);

        shadowCam.setProjectionMatrix(result);
    }