private void initScene() { //OpenCV Cube init objectList = new List <MCvPoint3D32f>(); for (int i = 0; i < _height; i++) { for (int j = 0; j < _width; j++) { objectList.Add(new MCvPoint3D32f(j * _squareSize, i * _squareSize, 0.0F)); } } axisPoints = new List <MCvPoint3D32f>(); axisPoints.Add(new MCvPoint3D32f(0.0f, 0.0f, 0.0f)); axisPoints.Add(new MCvPoint3D32f(3.0f, 0.0f, 0.0f)); axisPoints.Add(new MCvPoint3D32f(0.0f, 3.0f, 0.0f)); axisPoints.Add(new MCvPoint3D32f(0.0f, 0.0f, -3.0f)); cubePoints = new List <MCvPoint3D32f>(); cubePoints.Add(new MCvPoint3D32f(0.0f, 0.0f, 0.0f)); cubePoints.Add(new MCvPoint3D32f(0.0f, 3.0f, 0.0f)); cubePoints.Add(new MCvPoint3D32f(3.0f, 3.0f, 0.0f)); cubePoints.Add(new MCvPoint3D32f(3.0f, 0.0f, 0.0f)); cubePoints.Add(new MCvPoint3D32f(0.0f, 0.0f, -3.0f)); cubePoints.Add(new MCvPoint3D32f(0.0f, 3.0f, -3.0f)); cubePoints.Add(new MCvPoint3D32f(3.0f, 3.0f, -3.0f)); cubePoints.Add(new MCvPoint3D32f(3.0f, 0.0f, -3.0f)); //OpenGL objects init marker_cube_g = new Geometry.CubeGeometry(3.0f, 3.0f, -3.0f); marker_cube_m = new Material.TextureMaterial(mScene.rhinoDoc, "texture.jpg", false); controller_cube_g = new Geometry.CubeGeometry(0.05f, 0.05f, -0.05f); controller_cube_m = new Material.TextureMaterial(mScene.rhinoDoc, "texture.jpg", false); //TODO- how to deal with controllerPose ? //SceneNode controller_cube = new SceneNode("controller_cube", ref controller_cube_g, ref controller_cube_m); ; //mScene.staticGeometry.add(ref controller_cube); //we need eyepose before calibration if (mHMD == null) { mEyePosLeft = new Matrix4(); } Valve.VR.HmdMatrix34_t M_L = mHMD.GetEyeToHeadTransform(Valve.VR.EVREye.Eye_Left); mEyePosLeft = UtilOld.steamVRMatrixToMatrix4(M_L).Inverted(); Valve.VR.HmdMatrix34_t M_R = mHMD.GetEyeToHeadTransform(Valve.VR.EVREye.Eye_Right); mEyePosRight = UtilOld.steamVRMatrixToMatrix4(M_R).Inverted(); }
private OpenVRScene(float nearClip, float farClip, CVRSystem hmd, int leftIndex, int rightIndex) { // set up the hmd this.hmd = hmd; NearClip = nearClip; FarClip = farClip; leftEyeProj = hmd.GetProjectionMatrix(EVREye.Eye_Left, nearClip, farClip, EGraphicsAPIConvention.API_OpenGL).ToGLMatrix4(); rightEyeProj = hmd.GetProjectionMatrix(EVREye.Eye_Right, nearClip, farClip, EGraphicsAPIConvention.API_OpenGL).ToGLMatrix4(); leftEyeView = hmd.GetEyeToHeadTransform(EVREye.Eye_Left).ToGLMatrix4().Inverted(); rightEyeView = hmd.GetEyeToHeadTransform(EVREye.Eye_Right).ToGLMatrix4().Inverted(); leftHandControllerIndex = leftIndex; rightHandControllerIndex = rightIndex; OpenVRInput = new OpenVRInputObservable(hmd); }
public void RenderScene(Valve.VR.EVREye eye) { // Clear the screen to white GL.ClearColor(.1f, 0f, .1f, 1.0f); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); // use ovrvision camera if (enableAR) { ovrvision_controller.updateCamera(eye); Matrix4 vp = new Matrix4(); switch (eye) { case Valve.VR.EVREye.Eye_Left: if (ovrvision_controller.foundMarker_L) { ovrvision_controller.drawCubeGL(0); } //ovrvision_controller.drawController(0); ovrvision_controller.getOVRVPMatrix(0, ref vp); //Util.WriteLine(ref mScene.rhinoDoc, vp.ToString()); break; default: if (ovrvision_controller.foundMarker_R) { ovrvision_controller.drawCubeGL(1); } // ovrvision_controller.drawController(1); ovrvision_controller.getOVRVPMatrix(1, ref vp); break; } //already transpose //vp.Transpose(); mScene.render(ref vp); } else { mHMD.GetEyeToHeadTransform(eye); Matrix4 vp; switch (eye) { case Valve.VR.EVREye.Eye_Left: //Util.WriteLine(ref mScene.rhinoDoc, mScene.mHMDPose.ToString()); vp = mEyeProjLeft * mEyePosLeft * mScene.mHMDPose; break; default: vp = mEyeProjRight * mEyePosRight * mScene.mHMDPose; break; } vp.Transpose(); mScene.render(ref vp); } }
Matrix4 GetHMDMatrixPoseEye(ref Valve.VR.CVRSystem HMD, Valve.VR.EVREye eye) { if (HMD == null) { return(new Matrix4()); } Valve.VR.HmdMatrix34_t M = HMD.GetEyeToHeadTransform(eye); return(UtilOld.steamVRMatrixToMatrix4(M).Inverted()); }
private SteamVR(System.IntPtr pHmd, System.IntPtr pCompositor, System.IntPtr pOverlay) { hmd = new CVRSystem(pHmd); Debug.Log("Connected to " + hmd_TrackingSystemName + ":" + hmd_SerialNumber); compositor = new CVRCompositor(pCompositor); overlay = new CVROverlay(pOverlay); var capacity = compositor.GetLastError(null, 0); if (capacity > 1) { var result = new System.Text.StringBuilder((int)capacity); compositor.GetLastError(result, capacity); Debug.Log("Compositor - " + result); } // Setup render values uint w = 0, h = 0; hmd.GetRecommendedRenderTargetSize(ref w, ref h); sceneWidth = (float)w; sceneHeight = (float)h; float l_left = 0.0f, l_right = 0.0f, l_top = 0.0f, l_bottom = 0.0f; hmd.GetProjectionRaw(Hmd_Eye.Eye_Left, ref l_left, ref l_right, ref l_top, ref l_bottom); float r_left = 0.0f, r_right = 0.0f, r_top = 0.0f, r_bottom = 0.0f; hmd.GetProjectionRaw(Hmd_Eye.Eye_Right, ref r_left, ref r_right, ref r_top, ref r_bottom); tanHalfFov = new Vector2( Mathf.Max(-l_left, l_right, -r_left, r_right), Mathf.Max(-l_top, l_bottom, -r_top, r_bottom)); textureBounds = new VRTextureBounds_t[2]; textureBounds[0].uMin = 0.5f + 0.5f * l_left / tanHalfFov.x; textureBounds[0].uMax = 0.5f + 0.5f * l_right / tanHalfFov.x; textureBounds[0].vMin = 0.5f - 0.5f * l_bottom / tanHalfFov.y; textureBounds[0].vMax = 0.5f - 0.5f * l_top / tanHalfFov.y; textureBounds[1].uMin = 0.5f + 0.5f * r_left / tanHalfFov.x; textureBounds[1].uMax = 0.5f + 0.5f * r_right / tanHalfFov.x; textureBounds[1].vMin = 0.5f - 0.5f * r_bottom / tanHalfFov.y; textureBounds[1].vMax = 0.5f - 0.5f * r_top / tanHalfFov.y; Unity.SetSubmitParams(textureBounds[0], textureBounds[1], VRSubmitFlags_t.Submit_Default); // Grow the recommended size to account for the overlapping fov sceneWidth = sceneWidth / Mathf.Max(textureBounds[0].uMax - textureBounds[0].uMin, textureBounds[1].uMax - textureBounds[1].uMin); sceneHeight = sceneHeight / Mathf.Max(textureBounds[0].vMax - textureBounds[0].vMin, textureBounds[1].vMax - textureBounds[1].vMin); aspect = tanHalfFov.x / tanHalfFov.y; fieldOfView = 2.0f * Mathf.Atan(tanHalfFov.y) * Mathf.Rad2Deg; eyes = new SteamVR_Utils.RigidTransform[] { new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(Hmd_Eye.Eye_Left)), new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(Hmd_Eye.Eye_Right)) }; if (SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL")) graphicsAPI = GraphicsAPIConvention.API_OpenGL; else graphicsAPI = GraphicsAPIConvention.API_DirectX; SteamVR_Utils.Event.Listen("initializing", OnInitializing); SteamVR_Utils.Event.Listen("calibrating", OnCalibrating); SteamVR_Utils.Event.Listen("out_of_range", OnOutOfRange); SteamVR_Utils.Event.Listen("device_connected", OnDeviceConnected); SteamVR_Utils.Event.Listen("new_poses", OnNewPoses); }
private SteamVR(System.IntPtr pHmd, System.IntPtr pCompositor) { hmd = new CVRSystem(pHmd); Debug.Log("Connected to " + hmd_TrackingSystemName + ":" + hmd_SerialNumber); compositor = new CVRCompositor(pCompositor); var device = new UnityGraphicsDevice(); GetUnityGraphicsDevice(ref device); switch (device.type) { case GfxDeviceRenderer.kGfxRendererD3D11: compositor.SetGraphicsDevice(Compositor_DeviceType.D3D11, device.ptr); break; case GfxDeviceRenderer.kGfxRendererOpenGL: compositor.SetGraphicsDevice(Compositor_DeviceType.OpenGL, device.ptr); break; default: throw new System.Exception("Unsupported device type."); } var capacity = compositor.GetLastError(null, 0); if (capacity > 1) { var result = new System.Text.StringBuilder((int)capacity); compositor.GetLastError(result, capacity); Debug.Log("Compositor - " + result); } // Register for a callback if our graphics device goes away, so we can properly clean up. var resetDelegate = new UnityResetDelegate(SteamVR.SafeDispose); callbackHandle = GCHandle.Alloc(resetDelegate); SetUnityResetCallback(Marshal.GetFunctionPointerForDelegate(resetDelegate)); // Hook up the render thread present event just in case we wind up needing to use this. var error = HmdError.None; SetUnityRenderCallback(OpenVR.GetGenericInterface(IVRHmdDistortPresent_Version, ref error)); // Setup render values uint w = 0, h = 0; hmd.GetRecommendedRenderTargetSize(ref w, ref h); sceneWidth = (float)w; sceneHeight = (float)h; float l_left = 0.0f, l_right = 0.0f, l_top = 0.0f, l_bottom = 0.0f; hmd.GetProjectionRaw(Hmd_Eye.Eye_Left, ref l_left, ref l_right, ref l_top, ref l_bottom); float r_left = 0.0f, r_right = 0.0f, r_top = 0.0f, r_bottom = 0.0f; hmd.GetProjectionRaw(Hmd_Eye.Eye_Right, ref r_left, ref r_right, ref r_top, ref r_bottom); tanHalfFov = new Vector2( Mathf.Max(-l_left, l_right, -r_left, r_right), Mathf.Max(-l_top, l_bottom, -r_top, r_bottom)); textureBounds = new Compositor_TextureBounds[2]; textureBounds[0].uMin = 0.5f + 0.5f * l_left / tanHalfFov.x; textureBounds[0].uMax = 0.5f + 0.5f * l_right / tanHalfFov.x; textureBounds[0].vMin = 0.5f + 0.5f * l_bottom / tanHalfFov.y; textureBounds[0].vMax = 0.5f + 0.5f * l_top / tanHalfFov.y; textureBounds[1].uMin = 0.5f + 0.5f * r_left / tanHalfFov.x; textureBounds[1].uMax = 0.5f + 0.5f * r_right / tanHalfFov.x; textureBounds[1].vMin = 0.5f + 0.5f * r_bottom / tanHalfFov.y; textureBounds[1].vMax = 0.5f + 0.5f * r_top / tanHalfFov.y; aspect = tanHalfFov.x / tanHalfFov.y; fieldOfView = 2.0f * Mathf.Atan(tanHalfFov.y) * Mathf.Rad2Deg; eyes = new SteamVR_Utils.RigidTransform[] { new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(Hmd_Eye.Eye_Left)), new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(Hmd_Eye.Eye_Right)) }; SteamVR_Utils.Event.Listen("initializing", OnInitializing); SteamVR_Utils.Event.Listen("calibrating", OnCalibrating); SteamVR_Utils.Event.Listen("out_of_range", OnOutOfRange); SteamVR_Utils.Event.Listen("device_connected", OnDeviceConnected); SteamVR_Utils.Event.Listen("new_poses", OnNewPoses); }