Search in sources :

Example 1 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 2 with VRAPI

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

the class VRViewManagerOSVR method postRender.

/**
     * Send the textures to the two eyes.
     */
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();
                            if (api instanceof OSVR) {
                                osvr_renderBuffer[OSVR.EYE_LEFT].colorBufferName = leftTextureType.handle;
                                osvr_renderBuffer[OSVR.EYE_LEFT].depthStencilBufferName = dualEyeTex.getImage().getId();
                                osvr_renderBuffer[OSVR.EYE_LEFT].write();
                                registerOSVRBuffer(osvr_renderBuffer[OSVR.EYE_LEFT]);
                            }
                        }
                    } else {
                        if (api instanceof OSVR) {
                            ((OSVR) api).handleRenderBufferPresent(osvr_viewDescLeft, osvr_viewDescRight, osvr_renderBuffer[OSVR.EYE_LEFT], osvr_renderBuffer[OSVR.EYE_LEFT]);
                        }
                    }
                } 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();
                        if (api instanceof OSVR) {
                            osvr_renderBuffer[OSVR.EYE_LEFT].colorBufferName = leftTextureType.handle;
                            if (leftEyeDepth != null)
                                osvr_renderBuffer[OSVR.EYE_LEFT].depthStencilBufferName = leftEyeDepth.getImage().getId();
                            osvr_renderBuffer[OSVR.EYE_LEFT].write();
                            registerOSVRBuffer(osvr_renderBuffer[OSVR.EYE_LEFT]);
                        }
                    }
                    rightTextureType.handle = getRightTexId();
                    if (rightTextureType.handle != -1) {
                        logger.fine("Writing Right texture to native memory at " + leftTextureType.getPointer());
                        rightTextureType.write();
                        if (api instanceof OSVR) {
                            osvr_renderBuffer[OSVR.EYE_RIGHT].colorBufferName = rightTextureType.handle;
                            if (rightEyeDepth != null)
                                osvr_renderBuffer[OSVR.EYE_RIGHT].depthStencilBufferName = rightEyeDepth.getImage().getId();
                            osvr_renderBuffer[OSVR.EYE_RIGHT].write();
                            registerOSVRBuffer(osvr_renderBuffer[OSVR.EYE_RIGHT]);
                        }
                    }
                } else {
                    if (api instanceof OSVR) {
                        ((OSVR) api).handleRenderBufferPresent(osvr_viewDescFull, osvr_viewDescFull, osvr_renderBuffer[OSVR.EYE_LEFT], osvr_renderBuffer[OSVR.EYE_RIGHT]);
                    }
                }
                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 : OSVR(com.jme3.input.vr.OSVR) VRAPI(com.jme3.input.vr.VRAPI)

Example 3 with VRAPI

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

the class VRViewManagerOSVR method prepareCameraSize.

/**
     * Prepare the size of the given {@link Camera camera} to adapt it to the underlying rendering context.
     * @param cam the {@link Camera camera} to prepare.
     * @param xMult the camera width multiplier.
     */
private void prepareCameraSize(Camera cam, float xMult) {
    if (environment != null) {
        if (environment.getApplication() != null) {
        } else {
            throw new IllegalStateException("This VR environment is not attached to any application.");
        }
        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 *= resMult;
        size.y *= resMult;
        if (cam.getWidth() != size.x || cam.getHeight() != size.y) {
            cam.resize((int) size.x, (int) size.y, false);
        }
    } else {
        throw new IllegalStateException("This VR view manager is not attached to any VR environment.");
    }
}
Also used : Vector2f(com.jme3.math.Vector2f) VRAPI(com.jme3.input.vr.VRAPI)

Example 4 with VRAPI

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

the class VRViewManagerOpenVR 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 5 with VRAPI

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

the class VRViewManagerOpenVR method prepareCameraSize.

/**
     * Prepare the size of the given {@link Camera camera} to adapt it to the underlying rendering context.
     * @param cam the {@link Camera camera} to prepare.
     * @param xMult the camera width multiplier.
     */
private void prepareCameraSize(Camera cam, float xMult) {
    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.");
    }
}
Also used : Vector2f(com.jme3.math.Vector2f) VRAPI(com.jme3.input.vr.VRAPI)

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