Search in sources :

Example 16 with Environment

use of com.jme3.audio.Environment in project jmonkeyengine by jMonkeyEngine.

the class VRAppState method stateAttached.

@Override
public void stateAttached(AppStateManager stateManager) {
    //To change body of generated methods, choose Tools | Templates.
    super.stateAttached(stateManager);
    if (settings == null) {
        settings = new AppSettings(true);
        logger.config("Using default settings.");
    } else {
        logger.config("Using given settings.");
    }
    // Attach VR environment to the application
    if (!environment.isInitialized()) {
        environment.initialize();
    }
    if (environment.isInitialized()) {
        environment.atttach(this, stateManager.getApplication());
    } else {
        logger.severe("Cannot attach VR environment to the VR app state as its not initialized.");
    }
    GraphicsDevice defDev = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    if (environment.isInVR() && !environment.compositorAllowed()) {
        // "easy extended" mode
        // setup experimental JFrame on external device
        // first, find the VR device
        GraphicsDevice VRdev = null;
        GraphicsDevice[] devs = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
        // pick the display that isn't the default one
        for (GraphicsDevice gd : devs) {
            if (gd != defDev) {
                VRdev = gd;
                break;
            }
        }
        // did we get the VR device?
        if (VRdev != null) {
            // set properties for VR acceleration
            try {
                java.awt.DisplayMode useDM = null;
                int max = 0;
                for (java.awt.DisplayMode dm : VRdev.getDisplayModes()) {
                    int check = dm.getHeight() + dm.getWidth() + dm.getRefreshRate() + dm.getBitDepth();
                    if (check > max) {
                        max = check;
                        useDM = dm;
                    }
                }
                // create a window for the VR device
                settings.setWidth(useDM.getWidth());
                settings.setHeight(useDM.getHeight());
                settings.setBitsPerPixel(useDM.getBitDepth());
                settings.setFrequency(useDM.getRefreshRate());
                settings.setSwapBuffers(true);
                // allow vsync on this display
                settings.setVSync(true);
                stateManager.getApplication().setSettings(settings);
                logger.config("Updated underlying application settings.");
                // make sure we are in the right display mode
                if (VRdev.getDisplayMode().equals(useDM) == false) {
                    VRdev.setDisplayMode(useDM);
                }
                return;
            } catch (Exception e) {
                logger.log(Level.SEVERE, e.getMessage(), e);
            }
        } else {
            logger.config("Cannot access to external screen.");
        }
    } else {
        if (!environment.isInVR()) {
            logger.config("Cannot switch to VR mode (VR disabled by user).");
        } else if (!environment.compositorAllowed()) {
            logger.warning("Cannot switch to VR mode (VR not supported).");
        }
    }
    if (!environment.isInVR()) {
        //FIXME: Handling GLFW workaround on MacOS
        boolean macOs = false;
        if (macOs) {
            // GLFW workaround on macs
            settings.setFrequency(defDev.getDisplayMode().getRefreshRate());
            settings.setDepthBits(24);
            settings.setVSync(true);
            // try and read resolution from file in local dir
            File resfile = new File("resolution.txt");
            if (resfile.exists()) {
                try {
                    BufferedReader br = new BufferedReader(new FileReader(resfile));
                    settings.setWidth(Integer.parseInt(br.readLine()));
                    settings.setHeight(Integer.parseInt(br.readLine()));
                    try {
                        settings.setFullscreen(br.readLine().toLowerCase(Locale.ENGLISH).contains("full"));
                    } catch (Exception e) {
                        settings.setFullscreen(false);
                    }
                    br.close();
                } catch (Exception e) {
                    settings.setWidth(1280);
                    settings.setHeight(720);
                }
            } else {
                settings.setWidth(1280);
                settings.setHeight(720);
                settings.setFullscreen(false);
            }
            settings.setResizable(false);
        }
        settings.setSwapBuffers(true);
    } else {
        // use basic mirroring window, skip settings window
        settings.setSamples(1);
        settings.setWidth(xWin);
        settings.setHeight(yWin);
        settings.setBitsPerPixel(32);
        settings.setFrameRate(0);
        settings.setFrequency(environment.getVRHardware().getDisplayFrequency());
        settings.setFullscreen(false);
        // stop vsyncing on primary monitor!
        settings.setVSync(false);
        settings.setSwapBuffers(environment.isSwapBuffers());
    }
    // Updating application settings
    stateManager.getApplication().setSettings(settings);
    logger.config("Updated underlying application settings.");
}
Also used : GraphicsDevice(java.awt.GraphicsDevice) AppSettings(com.jme3.system.AppSettings) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 17 with Environment

use of com.jme3.audio.Environment 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 18 with Environment

use of com.jme3.audio.Environment in project jmonkeyengine by jMonkeyEngine.

the class OpenVRInput method getFinalObserverPosition.

@Override
public Vector3f getFinalObserverPosition(int index) {
    if (environment != null) {
        VRViewManagerOpenVR vrvm = (VRViewManagerOpenVR) 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.");
    }
}
Also used : VRViewManagerOpenVR(com.jme3.util.VRViewManagerOpenVR) Vector3f(com.jme3.math.Vector3f) Camera(com.jme3.renderer.Camera)

Example 19 with Environment

use of com.jme3.audio.Environment 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 20 with Environment

use of com.jme3.audio.Environment in project jmonkeyengine by jMonkeyEngine.

the class HelloAudio method initAudio.

/** We create two audio nodes. */
private void initAudio() {
    /* gun shot sound is to be triggered by a mouse click. */
    audio_gun = new AudioNode(assetManager, "Sound/Effects/Gun.wav", false);
    audio_gun.setPositional(false);
    audio_gun.setLooping(false);
    audio_gun.setVolume(2);
    rootNode.attachChild(audio_gun);
    /* nature sound - keeps playing in a loop. */
    audio_nature = new AudioNode(assetManager, "Sound/Environment/Ocean Waves.ogg", true);
    // activate continuous playing
    audio_nature.setLooping(true);
    audio_nature.setPositional(true);
    audio_nature.setVolume(3);
    rootNode.attachChild(audio_nature);
    // play continuously!
    audio_nature.play();
}
Also used : AudioNode(com.jme3.audio.AudioNode)

Aggregations

Camera (com.jme3.renderer.Camera)9 Vector2f (com.jme3.math.Vector2f)8 Vector3f (com.jme3.math.Vector3f)8 Spatial (com.jme3.scene.Spatial)7 VRAPI (com.jme3.input.vr.VRAPI)6 ViewPort (com.jme3.renderer.ViewPort)6 Texture2D (com.jme3.texture.Texture2D)6 Material (com.jme3.material.Material)5 Geometry (com.jme3.scene.Geometry)5 Node (com.jme3.scene.Node)5 FrameBuffer (com.jme3.texture.FrameBuffer)5 AudioNode (com.jme3.audio.AudioNode)4 OSVR (com.jme3.input.vr.OSVR)4 Quaternion (com.jme3.math.Quaternion)4 OpenVR (com.jme3.input.vr.OpenVR)3 ColorRGBA (com.jme3.math.ColorRGBA)3 FilterPostProcessor (com.jme3.post.FilterPostProcessor)3 Environment (com.jme3.audio.Environment)2 AmbientLight (com.jme3.light.AmbientLight)2 CartoonSSAO (com.jme3.post.CartoonSSAO)2