Search in sources :

Example 6 with VRAPI

use of com.jme3.input.vr.VRAPI in project jmonkeyengine by jMonkeyEngine.

the class VREnvironment method atttach.

/**
     * Attach the VR environment to the given app state and application. 
     * This method should be called within the {@link AppState#stateAttached(com.jme3.app.state.AppStateManager) stateAttached(com.jme3.app.state.AppStateManager)} method 
     * from the app state.
     * @param appState the app state to attach.
     * @param application the application to attach.
     */
public void atttach(AppState appState, Application application) {
    this.application = application;
    this.app = appState;
    // Instanciate view manager
    if (vrBinding == VRConstants.SETTING_VRAPI_OPENVR_VALUE) {
        viewmanager = new VRViewManagerOpenVR(this);
    } else if (vrBinding == VRConstants.SETTING_VRAPI_OSVR_VALUE) {
        viewmanager = new VRViewManagerOSVR(this);
    } else {
        logger.severe("Cannot instanciate view manager, unknown VRAPI type: " + vrBinding);
    }
}
Also used : VRViewManagerOSVR(com.jme3.util.VRViewManagerOSVR) VRViewManagerOpenVR(com.jme3.util.VRViewManagerOpenVR)

Example 7 with VRAPI

use of com.jme3.input.vr.VRAPI in project jmonkeyengine by jMonkeyEngine.

the class VRViewManagerOSVR method setupDistortionMesh.

/**
     * Setup a distortion mesh for the stereo view.
     * @param eye the eye to apply.
     * @param api the underlying VR api
     * @return the distorted mesh.
     */
public static Mesh setupDistortionMesh(int eye, VRAPI api) {
    Mesh distortionMesh = new Mesh();
    float m_iLensGridSegmentCountH = 43, m_iLensGridSegmentCountV = 43;
    float w = 1f / (m_iLensGridSegmentCountH - 1f);
    float h = 1f / (m_iLensGridSegmentCountV - 1f);
    float u, v;
    float[] verts = new float[(int) (m_iLensGridSegmentCountV * m_iLensGridSegmentCountH) * 3];
    float[] texcoordR = new float[(int) (m_iLensGridSegmentCountV * m_iLensGridSegmentCountH) * 2];
    float[] texcoordG = new float[(int) (m_iLensGridSegmentCountV * m_iLensGridSegmentCountH) * 2];
    float[] texcoordB = new float[(int) (m_iLensGridSegmentCountV * m_iLensGridSegmentCountH) * 2];
    int vertPos = 0, coordPos = 0;
    float Xoffset = eye == JOpenVRLibrary.EVREye.EVREye_Eye_Left ? -1f : 0;
    for (int y = 0; y < m_iLensGridSegmentCountV; y++) {
        for (int x = 0; x < m_iLensGridSegmentCountH; x++) {
            u = x * w;
            v = 1 - y * h;
            // x
            verts[vertPos] = Xoffset + u;
            // y
            verts[vertPos + 1] = -1 + 2 * y * h;
            // z
            verts[vertPos + 2] = 0f;
            vertPos += 3;
            DistortionCoordinates_t dc0 = new DistortionCoordinates_t();
            if (api.getVRSystem() == null) {
                // default to no distortion
                texcoordR[coordPos] = u;
                texcoordR[coordPos + 1] = 1 - v;
                texcoordG[coordPos] = u;
                texcoordG[coordPos + 1] = 1 - v;
                texcoordB[coordPos] = u;
                texcoordB[coordPos + 1] = 1 - v;
            } else {
                ((VR_IVRSystem_FnTable) api.getVRSystem()).ComputeDistortion.apply(eye, u, v, dc0);
                texcoordR[coordPos] = dc0.rfRed[0];
                texcoordR[coordPos + 1] = 1 - dc0.rfRed[1];
                texcoordG[coordPos] = dc0.rfGreen[0];
                texcoordG[coordPos + 1] = 1 - dc0.rfGreen[1];
                texcoordB[coordPos] = dc0.rfBlue[0];
                texcoordB[coordPos + 1] = 1 - dc0.rfBlue[1];
            }
            coordPos += 2;
        }
    }
    // have UV coordinates & positions, now to setup indices
    int[] indices = new int[(int) ((m_iLensGridSegmentCountV - 1) * (m_iLensGridSegmentCountH - 1)) * 6];
    int indexPos = 0;
    int a, b, c, d;
    int offset = 0;
    for (int y = 0; y < m_iLensGridSegmentCountV - 1; y++) {
        for (int x = 0; x < m_iLensGridSegmentCountH - 1; x++) {
            a = (int) (m_iLensGridSegmentCountH * y + x + offset);
            b = (int) (m_iLensGridSegmentCountH * y + x + 1 + offset);
            c = (int) ((y + 1) * m_iLensGridSegmentCountH + x + 1 + offset);
            d = (int) ((y + 1) * m_iLensGridSegmentCountH + x + offset);
            indices[indexPos] = a;
            indices[indexPos + 1] = b;
            indices[indexPos + 2] = c;
            indices[indexPos + 3] = a;
            indices[indexPos + 4] = c;
            indices[indexPos + 5] = d;
            indexPos += 6;
        }
    }
    // OK, create the mesh        
    distortionMesh.setBuffer(VertexBuffer.Type.Position, 3, verts);
    distortionMesh.setBuffer(VertexBuffer.Type.Index, 1, indices);
    distortionMesh.setBuffer(VertexBuffer.Type.TexCoord, 2, texcoordR);
    distortionMesh.setBuffer(VertexBuffer.Type.TexCoord2, 2, texcoordG);
    distortionMesh.setBuffer(VertexBuffer.Type.TexCoord3, 2, texcoordB);
    distortionMesh.setStatic();
    return distortionMesh;
}
Also used : DistortionCoordinates_t(com.jme3.system.jopenvr.DistortionCoordinates_t) Mesh(com.jme3.scene.Mesh)

Example 8 with VRAPI

use of com.jme3.input.vr.VRAPI in project jmonkeyengine by jMonkeyEngine.

the class VRViewManagerOSVR method update.

/**
     * Update the VR view manager. 
     * This method is called by the attached {@link VRApplication VR application} and should not be called manually.
     * @param tpf the time per frame.
     */
public void update(float tpf) {
    if (environment != null) {
        // grab the observer
        Object obs = environment.getObserver();
        Quaternion objRot;
        Vector3f objPos;
        if (obs instanceof Camera) {
            objRot = ((Camera) obs).getRotation();
            objPos = ((Camera) obs).getLocation();
        } else {
            objRot = ((Spatial) obs).getWorldRotation();
            objPos = ((Spatial) obs).getWorldTranslation();
        }
        // grab the hardware handle
        VRAPI dev = environment.getVRHardware();
        if (dev != null) {
            // update the HMD's position & orientation
            dev.updatePose();
            dev.getPositionAndOrientation(hmdPos, hmdRot);
            if (obs != null) {
                // update hmdPos based on obs rotation
                finalRotation.set(objRot);
                finalRotation.mult(hmdPos, hmdPos);
                finalRotation.multLocal(hmdRot);
            }
            finalizeCamera(dev.getHMDVectorPoseLeftEye(), objPos, leftCamera);
            finalizeCamera(dev.getHMDVectorPoseRightEye(), objPos, rightCamera);
        } else {
            leftCamera.setFrame(objPos, objRot);
            rightCamera.setFrame(objPos, objRot);
        }
        if (environment.hasTraditionalGUIOverlay()) {
            // update the mouse?
            environment.getVRMouseManager().update(tpf);
            // update GUI position?
            if (environment.getVRGUIManager().wantsReposition || environment.getVRGUIManager().getPositioningMode() != VRGUIPositioningMode.MANUAL) {
                environment.getVRGUIManager().positionGuiNow(tpf);
                environment.getVRGUIManager().updateGuiQuadGeometricState();
            }
        }
    } else {
        throw new IllegalStateException("This VR view manager is not attached to any VR environment.");
    }
}
Also used : Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f) Camera(com.jme3.renderer.Camera) VRAPI(com.jme3.input.vr.VRAPI)

Example 9 with VRAPI

use of com.jme3.input.vr.VRAPI in project jmonkeyengine by jMonkeyEngine.

the class VRViewManagerOpenVR method postRender.

@Override
public void postRender() {
    if (environment != null) {
        if (environment.isInVR()) {
            VRAPI api = environment.getVRHardware();
            if (api.getCompositor() != null) {
                // using the compositor...
                int errl = 0, errr = 0;
                if (environment.isInstanceRendering()) {
                    if (leftTextureType.handle == -1 || leftTextureType.handle != getFullTexId()) {
                        leftTextureType.handle = getFullTexId();
                        if (leftTextureType.handle != -1) {
                            leftTextureType.write();
                        }
                    } else {
                        if (api instanceof OpenVR) {
                            int submitFlag = JOpenVRLibrary.EVRSubmitFlags.EVRSubmitFlags_Submit_Default;
                            errr = ((OpenVR) api).getCompositor().Submit.apply(JOpenVRLibrary.EVREye.EVREye_Eye_Right, leftTextureType, rightTextureBounds, submitFlag);
                            errl = ((OpenVR) api).getCompositor().Submit.apply(JOpenVRLibrary.EVREye.EVREye_Eye_Left, leftTextureType, leftTextureBounds, submitFlag);
                        }
                    }
                } else if (leftTextureType.handle == -1 || rightTextureType.handle == -1 || leftTextureType.handle != getLeftTexId() || rightTextureType.handle != getRightTexId()) {
                    leftTextureType.handle = getLeftTexId();
                    if (leftTextureType.handle != -1) {
                        logger.fine("Writing Left texture to native memory at " + leftTextureType.getPointer());
                        leftTextureType.write();
                    }
                    rightTextureType.handle = getRightTexId();
                    if (rightTextureType.handle != -1) {
                        logger.fine("Writing Right texture to native memory at " + leftTextureType.getPointer());
                        rightTextureType.write();
                    }
                } else {
                    if (api instanceof OpenVR) {
                        errl = ((OpenVR) api).getCompositor().Submit.apply(JOpenVRLibrary.EVREye.EVREye_Eye_Left, leftTextureType, null, JOpenVRLibrary.EVRSubmitFlags.EVRSubmitFlags_Submit_Default);
                        errr = ((OpenVR) api).getCompositor().Submit.apply(JOpenVRLibrary.EVREye.EVREye_Eye_Right, rightTextureType, null, JOpenVRLibrary.EVRSubmitFlags.EVRSubmitFlags_Submit_Default);
                    } else {
                    }
                }
                if (errl != 0) {
                    logger.severe("Submit to left compositor error: " + OpenVRUtil.getEVRCompositorErrorString(errl) + " (" + Integer.toString(errl) + ")");
                    logger.severe("  Texture color space: " + OpenVRUtil.getEColorSpaceString(leftTextureType.eColorSpace));
                    logger.severe("  Texture type: " + OpenVRUtil.getETextureTypeString(leftTextureType.eType));
                    logger.severe("  Texture handle: " + leftTextureType.handle);
                    logger.severe("  Left eye texture " + leftEyeTexture.getName() + " (" + leftEyeTexture.getImage().getId() + ")");
                    logger.severe("                 Type: " + leftEyeTexture.getType());
                    logger.severe("                 Size: " + leftEyeTexture.getImage().getWidth() + "x" + leftEyeTexture.getImage().getHeight());
                    logger.severe("          Image depth: " + leftEyeTexture.getImage().getDepth());
                    logger.severe("         Image format: " + leftEyeTexture.getImage().getFormat());
                    logger.severe("    Image color space: " + leftEyeTexture.getImage().getColorSpace());
                }
                if (errr != 0) {
                    logger.severe("Submit to right compositor error: " + OpenVRUtil.getEVRCompositorErrorString(errl) + " (" + Integer.toString(errl) + ")");
                    logger.severe("  Texture color space: " + OpenVRUtil.getEColorSpaceString(rightTextureType.eColorSpace));
                    logger.severe("  Texture type: " + OpenVRUtil.getETextureTypeString(rightTextureType.eType));
                    logger.severe("  Texture handle: " + rightTextureType.handle);
                    logger.severe("  Right eye texture " + rightEyeTexture.getName() + " (" + rightEyeTexture.getImage().getId() + ")");
                    logger.severe("                 Type: " + rightEyeTexture.getType());
                    logger.severe("                 Size: " + rightEyeTexture.getImage().getWidth() + "x" + rightEyeTexture.getImage().getHeight());
                    logger.severe("          Image depth: " + rightEyeTexture.getImage().getDepth());
                    logger.severe("         Image format: " + rightEyeTexture.getImage().getFormat());
                    logger.severe("    Image color space: " + rightEyeTexture.getImage().getColorSpace());
                }
            }
        }
    } else {
        throw new IllegalStateException("This VR view manager is not attached to any VR environment.");
    }
}
Also used : VRAPI(com.jme3.input.vr.VRAPI) OpenVR(com.jme3.input.vr.OpenVR)

Aggregations

VRAPI (com.jme3.input.vr.VRAPI)6 Quaternion (com.jme3.math.Quaternion)2 Vector2f (com.jme3.math.Vector2f)2 Vector3f (com.jme3.math.Vector3f)2 Camera (com.jme3.renderer.Camera)2 Mesh (com.jme3.scene.Mesh)2 DistortionCoordinates_t (com.jme3.system.jopenvr.DistortionCoordinates_t)2 OSVR (com.jme3.input.vr.OSVR)1 OpenVR (com.jme3.input.vr.OpenVR)1 VRViewManagerOSVR (com.jme3.util.VRViewManagerOSVR)1 VRViewManagerOpenVR (com.jme3.util.VRViewManagerOpenVR)1