Search in sources :

Example 1 with VR_IVRSystem_FnTable

use of com.jme3.system.jopenvr.VR_IVRSystem_FnTable 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 2 with VR_IVRSystem_FnTable

use of com.jme3.system.jopenvr.VR_IVRSystem_FnTable in project jmonkeyengine by jMonkeyEngine.

the class OpenVR method initialize.

@Override
public boolean initialize() {
    logger.config("Initializing OpenVR system...");
    hmdErrorStore = new IntByReference();
    vrsystemFunctions = null;
    JOpenVRLibrary.VR_InitInternal(hmdErrorStore, JOpenVRLibrary.EVRApplicationType.EVRApplicationType_VRApplication_Scene);
    if (hmdErrorStore.getValue() == 0) {
        vrsystemFunctions = new VR_IVRSystem_FnTable(JOpenVRLibrary.VR_GetGenericInterface(JOpenVRLibrary.IVRSystem_Version, hmdErrorStore).getPointer());
    }
    if (vrsystemFunctions == null || hmdErrorStore.getValue() != 0) {
        logger.severe("OpenVR Initialize Result: " + JOpenVRLibrary.VR_GetVRInitErrorAsEnglishDescription(hmdErrorStore.getValue()).getString(0));
        logger.severe("Initializing OpenVR system [FAILED]");
        return false;
    } else {
        logger.config("OpenVR initialized & VR connected.");
        vrsystemFunctions.setAutoSynch(false);
        vrsystemFunctions.read();
        tlastVsync = new FloatByReference();
        _tframeCount = new LongByReference();
        hmdDisplayFrequency = IntBuffer.allocate(1);
        hmdDisplayFrequency.put((int) JOpenVRLibrary.ETrackedDeviceProperty.ETrackedDeviceProperty_Prop_DisplayFrequency_Float);
        hmdTrackedDevicePoseReference = new TrackedDevicePose_t.ByReference();
        hmdTrackedDevicePoses = (TrackedDevicePose_t[]) hmdTrackedDevicePoseReference.toArray(JOpenVRLibrary.k_unMaxTrackedDeviceCount);
        poseMatrices = new Matrix4f[JOpenVRLibrary.k_unMaxTrackedDeviceCount];
        for (int i = 0; i < poseMatrices.length; i++) poseMatrices[i] = new Matrix4f();
        timePerFrame = 1.0 / hmdDisplayFrequency.get(0);
        // disable all this stuff which kills performance
        hmdTrackedDevicePoseReference.setAutoRead(false);
        hmdTrackedDevicePoseReference.setAutoWrite(false);
        hmdTrackedDevicePoseReference.setAutoSynch(false);
        for (int i = 0; i < JOpenVRLibrary.k_unMaxTrackedDeviceCount; i++) {
            hmdTrackedDevicePoses[i].setAutoRead(false);
            hmdTrackedDevicePoses[i].setAutoWrite(false);
            hmdTrackedDevicePoses[i].setAutoSynch(false);
        }
        // init controllers for the first time
        VRinput = new OpenVRInput(environment);
        VRinput.init();
        VRinput.updateConnectedControllers();
        // init bounds & chaperone info
        VRBounds.init();
        logger.config("Initializing OpenVR system [SUCCESS]");
        initSuccess = true;
        return true;
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Matrix4f(com.jme3.math.Matrix4f) FloatByReference(com.sun.jna.ptr.FloatByReference) LongByReference(com.sun.jna.ptr.LongByReference) TrackedDevicePose_t(com.jme3.system.jopenvr.TrackedDevicePose_t) VR_IVRSystem_FnTable(com.jme3.system.jopenvr.VR_IVRSystem_FnTable)

Example 3 with VR_IVRSystem_FnTable

use of com.jme3.system.jopenvr.VR_IVRSystem_FnTable 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 4 with VR_IVRSystem_FnTable

use of com.jme3.system.jopenvr.VR_IVRSystem_FnTable in project jmonkeyengine by jMonkeyEngine.

the class OpenVRFilter method configureDistortionMesh.

/*
        function converted from:
        https://github.com/ValveSoftware/openvr/blob/master/samples/hellovr_opengl/hellovr_opengl_main.cpp#L1335
    */
private void configureDistortionMesh() {
    float m_iLensGridSegmentCountH = 43, m_iLensGridSegmentCountV = 43;
    float w = 1f / m_iLensGridSegmentCountH - 1f;
    float h = 1f / m_iLensGridSegmentCountV - 1f;
    float u, v;
    distortionMesh = new Mesh();
    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;
    //left eye distortion verts
    float Xoffset = -1f;
    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();
            ((VR_IVRSystem_FnTable) application.getVRHardware().getVRSystem()).ComputeDistortion.apply(JOpenVRLibrary.EVREye.EVREye_Eye_Left, 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;
        }
    }
    //right eye distortion verts
    Xoffset = 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();
            ((VR_IVRSystem_FnTable) application.getVRHardware().getVRSystem()).ComputeDistortion.apply(JOpenVRLibrary.EVREye.EVREye_Eye_Right, 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
    //std::vector<GLushort> vIndices;
    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;
        }
    }
    offset = (int) (m_iLensGridSegmentCountH * m_iLensGridSegmentCountV);
    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);
    // TODO: are TexCoord2 & TexCoord3 even implemented in jME3?
    distortionMesh.setBuffer(VertexBuffer.Type.TexCoord2, 2, texcoordG);
    distortionMesh.setBuffer(VertexBuffer.Type.TexCoord3, 2, texcoordB);
// TODO: make sure this distortion mesh is used instead of the fullscreen quad
// when filter gets rendered.. might require changes to jME3 core..?
}
Also used : DistortionCoordinates_t(com.jme3.system.jopenvr.DistortionCoordinates_t) Mesh(com.jme3.scene.Mesh)

Aggregations

Mesh (com.jme3.scene.Mesh)3 DistortionCoordinates_t (com.jme3.system.jopenvr.DistortionCoordinates_t)3 Matrix4f (com.jme3.math.Matrix4f)1 TrackedDevicePose_t (com.jme3.system.jopenvr.TrackedDevicePose_t)1 VR_IVRSystem_FnTable (com.jme3.system.jopenvr.VR_IVRSystem_FnTable)1 FloatByReference (com.sun.jna.ptr.FloatByReference)1 IntByReference (com.sun.jna.ptr.IntByReference)1 LongByReference (com.sun.jna.ptr.LongByReference)1