com.jme3.renderer.Camera Java Examples

The following examples show how to use com.jme3.renderer.Camera. 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: OrientedBoxProbeArea.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public boolean intersectsFrustum(Camera camera, TempVars vars) {

    // extract the scaled axis
    // this allows a small optimization.
    Vector3f axis1 = getScaledAxis(0, vars.vect1);
    Vector3f axis2 = getScaledAxis(1, vars.vect2);
    Vector3f axis3 = getScaledAxis(2, vars.vect3);

    Vector3f tn = vars.vect4;

    for (int i = 5; i >= 0; i--) {
        Plane p = camera.getWorldPlane(i);
        if (!insidePlane(p, axis1, axis2, axis3, tn)) return false;
    }
    return true;
}
 
Example #2
Source File: BillboardControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Rotate the billboard so it points directly opposite the direction the
 * camera's facing
 *
 * @param camera
 *            Camera
 */
private void rotateScreenAligned(Camera camera) {
    // coopt diff for our in direction:
    look.set(camera.getDirection()).negateLocal();
    // coopt loc for our left direction:
    left.set(camera.getLeft()).negateLocal();
    orient.fromAxes(left, camera.getUp(), look);
    Node parent = spatial.getParent();
    Quaternion rot=new Quaternion().fromRotationMatrix(orient);
    if ( parent != null ) {
        rot =  parent.getWorldRotation().inverse().multLocal(rot);
        rot.normalizeLocal();
    }
    spatial.setLocalRotation(rot);
    fixRefreshFlags();
}
 
Example #3
Source File: RenderQueue.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void renderQueue(Bucket bucket, RenderManager rm, Camera cam, boolean clear) {
    switch (bucket) {
        case Gui:
            renderGeometryList(guiList, rm, cam, clear);
            break;
        case Opaque:
            renderGeometryList(opaqueList, rm, cam, clear);
            break;
        case Sky:
            renderGeometryList(skyList, rm, cam, clear);
            break;
        case Transparent:
            renderGeometryList(transparentList, rm, cam, clear);
            break;
        case Translucent:
            renderGeometryList(translucentList, rm, cam, clear);
            break;

        default:
            throw new UnsupportedOperationException("Unsupported bucket type: " + bucket);
    }
}
 
Example #4
Source File: LegacyApplication.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates the camera to use for rendering. Default values are perspective
 * projection with 45° field of view, with near and far values 1 and 1000
 * units respectively.
 */
private void initCamera(){
    cam = new Camera(settings.getWidth(), settings.getHeight());

    cam.setFrustumPerspective(45f, (float)cam.getWidth() / cam.getHeight(), 1f, 1000f);
    cam.setLocation(new Vector3f(0f, 0f, 10f));
    cam.lookAt(new Vector3f(0f, 0f, 0f), Vector3f.UNIT_Y);

    renderManager = new RenderManager(renderer);
    //Remy - 09/14/2010 setted the timer in the renderManager
    renderManager.setTimer(timer);

    if (prof != null) {
        renderManager.setAppProfiler(prof);
    }

    viewPort = renderManager.createMainView("Default", cam);
    viewPort.setClearFlags(true, true, true);

    // Create a new cam for the gui
    Camera guiCam = new Camera(settings.getWidth(), settings.getHeight());
    guiViewPort = renderManager.createPostView("Gui Default", guiCam);
    guiViewPort.setClearFlags(false, false, false);
}
 
Example #5
Source File: OculusViewManager.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void prepareCameraSize(Camera cam, float xMult) {
    // TODO this function is identical to that in VRViewManagerOpenVR; merge the two.
    if (environment != null) {
        if (environment.getApplication() != null) {
            Vector2f size = new Vector2f();
            VRAPI vrhmd = environment.getVRHardware();

            if (vrhmd == null) {
                size.x = 1280f;
                size.y = 720f;
            } else {
                vrhmd.getRenderSize(size);
            }

            if (size.x < environment.getApplication().getContext().getSettings().getWidth()) {
                size.x = environment.getApplication().getContext().getSettings().getWidth();
            }
            if (size.y < environment.getApplication().getContext().getSettings().getHeight()) {
                size.y = environment.getApplication().getContext().getSettings().getHeight();
            }

            if (environment.isInstanceRendering()) {
                size.x *= 2f;
            }

            // other adjustments
            size.x *= xMult;
            size.x *= getResolutionMuliplier();
            size.y *= getResolutionMuliplier();

            if (cam.getWidth() != size.x || cam.getHeight() != size.y) {
                cam.resize((int) size.x, (int) size.y, false);
            }
        } else {
            throw new IllegalStateException("This VR environment is not attached to any application.");
        }
    } else {
        throw new IllegalStateException("This VR view manager is not attached to any VR environment.");
    }
}
 
Example #6
Source File: TestRenderToTexture.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Texture setupOffscreenView(){
    Camera offCamera = new Camera(512, 512);

    offView = renderManager.createPreView("Offscreen View", offCamera);
    offView.setClearFlags(true, true, true);
    offView.setBackgroundColor(ColorRGBA.DarkGray);

    // create offscreen framebuffer
    FrameBuffer offBuffer = new FrameBuffer(512, 512, 1);

    //setup framebuffer's cam
    offCamera.setFrustumPerspective(45f, 1f, 1f, 1000f);
    offCamera.setLocation(new Vector3f(0f, 0f, -5f));
    offCamera.lookAt(new Vector3f(0f, 0f, 0f), Vector3f.UNIT_Y);

    //setup framebuffer's texture
    Texture2D offTex = new Texture2D(512, 512, Format.RGBA8);
    offTex.setMinFilter(Texture.MinFilter.Trilinear);
    offTex.setMagFilter(Texture.MagFilter.Bilinear);

    //setup framebuffer to use texture
    offBuffer.setDepthBuffer(Format.Depth);
    offBuffer.setColorTexture(offTex);
    
    //set viewport to render to offscreen framebuffer
    offView.setOutputFrameBuffer(offBuffer);

    // setup framebuffer's scene
    Box boxMesh = new Box(Vector3f.ZERO, 1,1,1);
    Material material = assetManager.loadMaterial("Interface/Logo/Logo.j3m");
    offBox = new Geometry("box", boxMesh);
    offBox.setMaterial(material);

    // attach the scene to the viewport to be rendered
    offView.attachScene(offBox);
    
    return offTex;
}
 
Example #7
Source File: OSVRInput.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Vector3f getFinalObserverPosition(int index) {
	OSVRViewManager vrvm = (OSVRViewManager) environment.getVRViewManager();
    if( vrvm == null || isInputDeviceTracking(index) == false ) return null;
    Object obs = environment.getObserver();
    Vector3f pos = getPosition(index);
    if( obs instanceof Camera ) {
        ((Camera)obs).getRotation().mult(pos, pos);
        return pos.addLocal(((Camera)obs).getLocation());
    } else {
        ((Spatial)obs).getWorldRotation().mult(pos, pos);
        return pos.addLocal(((Spatial)obs).getWorldTranslation());
    }
}
 
Example #8
Source File: EditorTransformSupport.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
@Override
@JmeThread
public void prepareToMove(@NotNull final Node parent, @NotNull final Node child,
                          @NotNull final Transform transform, @NotNull final Camera camera) {
    parent.setLocalTranslation(Vector3f.ZERO);
    parent.setLocalRotation(Quaternion.IDENTITY);
    child.setLocalTranslation(transform.getTranslation());
    child.setLocalRotation(transform.getRotation());
}
 
Example #9
Source File: Octnode.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void generateRenderSetNoCheck(Set<Geometry> renderSet, Camera cam){
    if (geoms != null){
        renderSet.addAll(Arrays.asList(geoms));
    }
    for (int i = 0; i < 8; i++){
        if (children[i] != null){
            children[i].generateRenderSetNoCheck(renderSet, cam);
        }
    }
}
 
Example #10
Source File: OSVR.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Matrix4f getHMDMatrixProjectionLeftEye(Camera cam) {
    if( eyeLeftInfo == null ) return cam.getProjectionMatrix();
    if( eyeMatrix[EYE_LEFT] == null ) {
        FloatBuffer tfb = FloatBuffer.allocate(16);
        com.jme3.system.osvr.osvrdisplay.OsvrDisplayLibrary.osvrClientGetViewerEyeSurfaceProjectionMatrixf(displayConfig, 0, (byte)EYE_LEFT, 0, cam.getFrustumNear(), cam.getFrustumFar(), (short)0, tfb);
        eyeMatrix[EYE_LEFT] = new Matrix4f();
        eyeMatrix[EYE_LEFT].set(tfb.get(0), tfb.get(4), tfb.get(8), tfb.get(12),
                                tfb.get(1), tfb.get(5), tfb.get(9), tfb.get(13),
                                tfb.get(2), tfb.get(6), tfb.get(10), tfb.get(14),
                                tfb.get(3), tfb.get(7), tfb.get(11), tfb.get(15));
    }
    return eyeMatrix[EYE_LEFT];
}
 
Example #11
Source File: TerrainLodControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public TerrainLodControl() {
    hasResetLod = false;
    forceUpdate = true;
    previousCameraLocation = new Vector3f();
    cameras = new SafeArrayList<>(Camera.class);
    cameraLocations = new SafeArrayList<>(Vector3f.class);
    lastCameraLocations = new SafeArrayList<>(Vector3f.class);
    lodCalcRunning = new AtomicBoolean(false);
    lodOffCount = 0;
    lodCalculator = makeLodCalculator(); // a default calculator
}
 
Example #12
Source File: LWJGLOpenVRViewManager.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private ViewPort setupMirrorBuffers(Camera cam, Texture2D tex, boolean expand) {

        if (environment != null) {
            if (environment.getApplication() != null) {
                Camera clonecam = cam.clone();
                ViewPort viewPort = environment.getApplication().getRenderManager().createPostView("MirrorView", clonecam);
                clonecam.setParallelProjection(true);
                viewPort.setClearFlags(true, true, true);
                viewPort.setBackgroundColor(ColorRGBA.Black);
                Picture pic = new Picture("fullscene");
                pic.setLocalTranslation(-0.75f, -0.5f, 0f);
                if (expand) {
                    pic.setLocalScale(3f, 1f, 1f);
                } else {
                    pic.setLocalScale(1.5f, 1f, 1f);
                }
                pic.setQueueBucket(Bucket.Opaque);
                pic.setTexture(environment.getApplication().getAssetManager(), tex, false);
                viewPort.attachScene(pic);
                viewPort.setOutputFrameBuffer(null);

                pic.updateGeometricState();

                return viewPort;
            } else {
                throw new IllegalStateException("This VR environment is not attached to any application.");
            }
        } else {
            throw new IllegalStateException("This VR view manager is not attached to any VR environment.");
        }
    }
 
Example #13
Source File: Cinematic.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public CameraNode bindCamera(String cameraName, Camera cam) {
    CameraNode node = new CameraNode(cameraName, cam);
    node.setControlDir(ControlDirection.SpatialToCamera);
    node.getControl(CameraControl.class).setEnabled(false);
    cameras.put(cameraName, node);
    scene.attachChild(node);
    return node;
}
 
Example #14
Source File: PssmShadowRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void displayShadowMap(Renderer r) {
    Camera cam = viewPort.getCamera();
    renderManager.setCamera(cam, true);
    int h = cam.getHeight();
    for (int i = 0; i < dispPic.length; i++) {
        dispPic[i].setPosition(64 * (i + 1) + 128 * i, h / 20f);
        dispPic[i].setWidth(128);
        dispPic[i].setHeight(128);
        dispPic[i].updateGeometricState();
        renderManager.renderGeometry(dispPic[i]);
    }
    renderManager.setCamera(cam, false);
}
 
Example #15
Source File: EditorTransformSupport.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
@Override
@JmeThread
public void prepareToMove(@NotNull final Node parent, @NotNull final Node child,
                          @NotNull final Transform transform, @NotNull final Camera camera) {
    parent.setLocalRotation(camera.getRotation());
    parent.setLocalTranslation(transform.getTranslation());
    child.setLocalTranslation(Vector3f.ZERO);
    child.setLocalRotation(Quaternion.IDENTITY);
}
 
Example #16
Source File: SceneEditTool.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected Vector3f pickWorldLocation(Camera cam, Vector2f mouseLoc, Node rootNode) {
    CollisionResult cr = pick(cam, mouseLoc, rootNode);
    if (cr != null) {
        return cr.getContactPoint();
    } else {
        return null;
    }
}
 
Example #17
Source File: LWJGLOpenVRInput.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Vector3f getFinalObserverPosition(int index) {

    if (environment != null) {
        VRViewManager vrvm = environment.getVRViewManager();

        if (vrvm != null) {
            if (isInputDeviceTracking(index) == false) {
                return null;
            }
            Object obs = environment.getObserver();
            Vector3f pos = getPosition(index);
            if (obs instanceof Camera) {
                ((Camera) obs).getRotation().mult(pos, pos);
                return pos.addLocal(((Camera) obs).getLocation());
            } else {
                ((Spatial) obs).getWorldRotation().mult(pos, pos);
                return pos.addLocal(((Spatial) obs).getWorldTranslation());
            }
        } else {
            throw new IllegalStateException("VR environment has no valid view manager.");
        }

    } else {
        throw new IllegalStateException("VR input is not attached to a VR environment.");
    }
}
 
Example #18
Source File: SelectionState.java    From Lemur with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected Ray getPickRay( Vector2f cursor ) {    
    Camera cam = getApplication().getCamera();
    Vector3f clickFar  = cam.getWorldCoordinates(cursor, 1);
    Vector3f clickNear = cam.getWorldCoordinates(cursor, 0);
    ray.setOrigin(clickNear);
    ray.setDirection(clickFar.subtractLocal(clickNear).normalizeLocal());
    return ray;
}
 
Example #19
Source File: PssmShadowUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Compute the Zfar in the model vieuw to adjust the Zfar distance for the splits calculation
 */
public static float computeZFar(GeometryList occ, GeometryList recv, Camera cam) {
    Matrix4f mat = cam.getViewMatrix();
    BoundingBox bbOcc = ShadowUtil.computeUnionBound(occ, mat);
    BoundingBox bbRecv = ShadowUtil.computeUnionBound(recv, mat);

    return min(max(bbOcc.getZExtent() - bbOcc.getCenter().z, bbRecv.getZExtent() - bbRecv.getCenter().z), cam.getFrustumFar());
}
 
Example #20
Source File: CameraTweens.java    From Lemur with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public MoveCamera( Camera target, Vector3f from, Vector3f to, double length ) {
    super(length);
    this.target = target;
    this.from = from.clone();
    this.to = to.clone();
    this.value = new Vector3f(from);
}
 
Example #21
Source File: GroundController.java    From OpenRTS with MIT License 5 votes vote down vote up
public GroundController(EditorView view, Nifty nifty, InputManager inputManager, Camera cam) {
	super(view, inputManager, cam);

	inputInterpreter = new GroundInputInterpreter(this);
	cameraManager = new GroundCameraManager(cam);
	guiController = new GroundGUIController(nifty, this);
}
 
Example #22
Source File: Spatial.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * <code>checkCulling</code> checks the spatial with the camera to see if it
 * should be culled.
 * <p>
 * This method is called by the renderer. Usually it should not be called
 * directly.
 *
 * @param cam The camera to check against.
 * @return true if inside or intersecting camera frustum
 * (should be rendered), false if outside.
 */
public boolean checkCulling(Camera cam) {
    if (refreshFlags != 0) {
        throw new IllegalStateException("Scene graph is not properly updated for rendering.\n"
                + "State was changed after rootNode.updateGeometricState() call. \n"
                + "Make sure you do not modify the scene from another thread!\n"
                + "Problem spatial name: " + getName());
    }

    if (!isVisible()) {
        setLastFrustumIntersection(Camera.FrustumIntersect.Outside);
        return false;
    }

    CullHint cm = getCullHint();
    assert cm != CullHint.Inherit;
    if (cm == Spatial.CullHint.Always) {
        setLastFrustumIntersection(Camera.FrustumIntersect.Outside);
        return false;
    } else if (cm == Spatial.CullHint.Never) {
        setLastFrustumIntersection(Camera.FrustumIntersect.Intersects);
        return true;
    }

    // check to see if we can cull this node
    frustrumIntersects = (parent != null ? parent.frustrumIntersects
            : Camera.FrustumIntersect.Intersects);

    if (frustrumIntersects == Camera.FrustumIntersect.Intersects) {
        if (getQueueBucket() == Bucket.Gui) {
            return cam.containsGui(getWorldBound());
        } else {
            frustrumIntersects = cam.contains(getWorldBound());
        }
    }

    return frustrumIntersects != Camera.FrustumIntersect.Outside;
}
 
Example #23
Source File: DirectionalLightShadowRendererVR.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void updateShadowCams(Camera viewCam) {

    float zFar = zFarOverride;
    if (zFar == 0) {
        zFar = viewCam.getFrustumFar();
    }

    //We prevent computing the frustum points and splits with zeroed or negative near clip value
    float frustumNear = Math.max(viewCam.getFrustumNear(), 0.001f);
    ShadowUtil.updateFrustumPoints(viewCam, frustumNear, zFar, 1.0f, points);

    //shadowCam.setDirection(direction);
    shadowCam.getRotation().lookAt(light.getDirection(), shadowCam.getUp());
    shadowCam.update();
    shadowCam.updateViewProjection();

    PssmShadowUtil.updateFrustumSplits(splitsArray, frustumNear, zFar, lambda);

    // in parallel projection shadow position goe from 0 to 1
    if(viewCam.isParallelProjection()){
        for (int i = 0; i < nbShadowMaps; i++) {
            splitsArray[i] = splitsArray[i]/(zFar- frustumNear);
        }
    }

    switch (splitsArray.length) {
        case 5:
            splits.a = splitsArray[4];
        case 4:
            splits.b = splitsArray[3];
        case 3:
            splits.g = splitsArray[2];
        case 2:
        case 1:
            splits.r = splitsArray[1];
            break;
    }

}
 
Example #24
Source File: PopupState.java    From Lemur with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  Returns the size of the screen based on the app's main camera size
 *  and the current scale of the guiNode.
 */   
public Vector2f getGuiSize() {
    Camera cam = getApplication().getCamera();
    float width = cam.getWidth() / getGuiNode().getLocalScale().x;
    float height = cam.getHeight() / getGuiNode().getLocalScale().y;
    return new Vector2f(width, height);        
}
 
Example #25
Source File: AbstractShadowRendererVR.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * For debugging purposes, display depth shadow maps.
 */
protected void displayShadowMap(Renderer r) {
    Camera cam = viewPort.getCamera();
    renderManager.setCamera(cam, true);
    int h = cam.getHeight();
    for (int i = 0; i < dispPic.length; i++) {
        dispPic[i].setPosition((128 * i) + (150 + 64 * (i + 1)), h / 20f);
        dispPic[i].setWidth(128);
        dispPic[i].setHeight(128);
        dispPic[i].updateGeometricState();
        renderManager.renderGeometry(dispPic[i]);
    }
    renderManager.setCamera(cam, false);
}
 
Example #26
Source File: TestRenderToTexture.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Texture setupOffscreenView(){
    Camera offCamera = new Camera(512, 512);

    offView = renderManager.createPreView("Offscreen View", offCamera);
    offView.setClearFlags(true, true, true);
    offView.setBackgroundColor(ColorRGBA.DarkGray);

    // create offscreen framebuffer
    FrameBuffer offBuffer = new FrameBuffer(512, 512, 1);

    //setup framebuffer's cam
    offCamera.setFrustumPerspective(45f, 1f, 1f, 1000f);
    offCamera.setLocation(new Vector3f(0f, 0f, -5f));
    offCamera.lookAt(new Vector3f(0f, 0f, 0f), Vector3f.UNIT_Y);

    //setup framebuffer's texture
    Texture2D offTex = new Texture2D(512, 512, Format.RGBA8);
    offTex.setMinFilter(Texture.MinFilter.Trilinear);
    offTex.setMagFilter(Texture.MagFilter.Bilinear);

    //setup framebuffer to use texture
    offBuffer.setDepthBuffer(Format.Depth);
    offBuffer.setColorTexture(offTex);
    
    //set viewport to render to offscreen framebuffer
    offView.setOutputFrameBuffer(offBuffer);

    // setup framebuffer's scene
    Box boxMesh = new Box(1, 1, 1);
    Material material = assetManager.loadMaterial("Interface/Logo/Logo.j3m");
    offBox = new Geometry("box", boxMesh);
    offBox.setMaterial(material);

    // attach the scene to the viewport to be rendered
    offView.attachScene(offBox);
    
    return offTex;
}
 
Example #27
Source File: TestNiftyToMesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void simpleInitApp() {
   ViewPort niftyView = renderManager.createPreView("NiftyView", new Camera(1024, 768));
   niftyView.setClearFlags(true, true, true);
    NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
                                                      inputManager,
                                                      audioRenderer,
                                                      niftyView);
    nifty = niftyDisplay.getNifty();
    nifty.fromXml("all/intro.xml", "start");
    niftyView.addProcessor(niftyDisplay);

    Texture2D depthTex = new Texture2D(1024, 768, Format.Depth);
    FrameBuffer fb = new FrameBuffer(1024, 768, 1);
    fb.setDepthTexture(depthTex);

    Texture2D tex = new Texture2D(1024, 768, Format.RGBA8);
    tex.setMinFilter(MinFilter.Trilinear);
    tex.setMagFilter(MagFilter.Bilinear);

    fb.setColorTexture(tex);
    niftyView.setClearFlags(true, true, true);
    niftyView.setOutputFrameBuffer(fb);

    Box b = new Box(Vector3f.ZERO, 1, 1, 1);
    Geometry geom = new Geometry("Box", b);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setTexture("ColorMap", tex);
    geom.setMaterial(mat);
    rootNode.attachChild(geom);
}
 
Example #28
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 #29
Source File: OSVRViewManager.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private ViewPort setupViewBuffers(Camera cam, String viewName){
	
	if (environment != null){
		if (environment.getApplication() != null){
	        // create offscreen framebuffer
	        FrameBuffer offBufferLeft = new FrameBuffer(cam.getWidth(), cam.getHeight(), 1);
	        //offBufferLeft.setSrgb(true);
	        
	        //setup framebuffer's texture
	        Texture2D offTex = new Texture2D(cam.getWidth(), cam.getHeight(), Image.Format.RGBA8);
	        offTex.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
	        offTex.setMagFilter(Texture.MagFilter.Bilinear);

	        //setup framebuffer to use texture
	        offBufferLeft.setDepthBuffer(Image.Format.Depth);
	        offBufferLeft.setColorTexture(offTex);        
	        
	        ViewPort viewPort = environment.getApplication().getRenderManager().createPreView(viewName, cam);
	        viewPort.setClearFlags(true, true, true);
	        viewPort.setBackgroundColor(ColorRGBA.Black);
	        
	        Iterator<Spatial> spatialIter = environment.getApplication().getViewPort().getScenes().iterator();
	        while(spatialIter.hasNext()){
	        	viewPort.attachScene(spatialIter.next());
	        }

	        //set viewport to render to offscreen framebuffer
	        viewPort.setOutputFrameBuffer(offBufferLeft);
	        return viewPort;
		} else {
			throw new IllegalStateException("This VR environment is not attached to any application.");
		}
	} else {
		throw new IllegalStateException("This VR view manager is not attached to any VR environment.");
	}  
}
 
Example #30
Source File: WaterFilter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void preFrame(float tpf) {
    time = time + (tpf * speed);
    material.setFloat("Time", time);
    Camera sceneCam = viewPort.getCamera();
    biasMatrix.mult(sceneCam.getViewProjectionMatrix(), textureProjMatrix);
    material.setMatrix4("TextureProjMatrix", textureProjMatrix);
    material.setVector3("CameraPosition", sceneCam.getLocation());
    //material.setFloat("WaterHeight", waterHeight);

    //update reflection cam      
    //plane = new Plane(Vector3f.UNIT_Y, new Vector3f(0, waterHeight, 0).dot(Vector3f.UNIT_Y));
    //reflectionProcessor.setReflectionClipPlane(plane);        
    WaterUtils.updateReflectionCam(reflectionCam, plane, sceneCam);
  

    //if we're under water no need to compute reflection
    if (sceneCam.getLocation().y >= waterHeight) {
        boolean rtb = true;
        if (!renderManager.isHandleTranslucentBucket()) {
            renderManager.setHandleTranslucentBucket(true);
            rtb = false;
        }
        renderManager.renderViewPort(reflectionView, tpf);
        if (!rtb) {
            renderManager.setHandleTranslucentBucket(false);
        }
        renderManager.setCamera(sceneCam, false);
        renderManager.getRenderer().setFrameBuffer(viewPort.getOutputFrameBuffer());


        underWater = false;
    } else {
        underWater = true;
    }
}