libGDX can be used to render to virtual reality headsets using LWJGL’s OpenVR (HTC Vive) and OVR (Oculus) modules.

Required Dependencies

The Maven coordinates can be found using the LWJGL customize page.

Example code

Example code can be found at https://github.com/badlogic/gdx-vr.

Additionally, the following demos from LWJGL may be useful for debugging:

Also, see https://github.com/Zomby2D/gdx-vr-extension

OpenVR with offscreen GLFW window

Using an offscreen GLFW window allows to render to VR without rendering or maintaining a window on the monitor’s screen. This will allow achieving the fastest performance with OpenVR.

This isn’t supported yet by libGDX, because some hacking is required, but the general flow is the following.

  1. Lwjgl3NativesLoader.load();
  2. errorCallback = GLFWErrorCallback.createPrint(System.err);
  3. GLFW.glfwSetErrorCallback(errorCallback);
  4. if (!GLFW.glfwInit())
  5. {
  6. throw new RuntimeException("Unable to initialize GLFW");
  7. }
  8. GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE);
  9. windowHandle = GLFW.glfwCreateWindow(640, 480, "", MemoryUtil.NULL, MemoryUtil.NULL);
  10. GLFW.glfwMakeContextCurrent(windowHandle);
  11. GL.createCapabilities();
  12. Gdx.gl30 = new Lwjgl3GL30();
  13. Gdx.gl = Gdx.gl30;
  14. Gdx.gl20 = Gdx.gl30;
  15. Gdx.files = new Lwjgl3Files();
  16. Gdx.graphics = <minimal implementation of Graphics interface>
  17. Gdx.app = <minimal implementation of Application interface>
  18. // setup a scene and model batch
  19. while (running) // See https://github.com/ValveSoftware/openvr/wiki/IVRCompositor_Overview
  20. {
  21. // OpenVR waitGetPoses();
  22. // OpenVR poll events
  23. // OpenVR Submit eyes
  24. }
  25. GLFW.glfwDestroyWindow(windowHandle);
  26. errorCallback.free();
  27. errorCallback = null;
  28. GLFW.glfwTerminate();

Roadmap

VR support might become easier relatively soon via the use of OpenXR. The LWJGL project plans to support this in the future. This would remove the need to support both OVR and OpenVR and instead just using OpenXR to support all hardware.