OPENVR Scol plugin
sOpenvr.h
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OpenSpace3D
4 For the latest info, see http://www.openspace3d.com
5 
6 Copyright (c) 2012 I-maginer
7 
8 This program is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12 
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20 http://www.gnu.org/copyleft/lesser.txt
21 
22 -----------------------------------------------------------------------------
23 */
24 
25 #ifndef S_OPENVR_H
26 #define S_OPENVR_H
27 
28 #include <scolPlugin.h>
29 #include <boost/thread/thread.hpp>
30 #include <boost/bind.hpp>
31 #include "vrmaths.h"
32 #include "openvr/openvr.h" // Include the OpenVR SDK
33 
34 #define USE_VR_INPUT 0
35 
36 #ifdef USE_VIVE_GESTURE
37 # include "interface_gesture.hpp"
38 #define MAX_VR_CONTROLLERS 10
39 #else
40 #define MAX_VR_CONTROLLERS 8
41 #endif
42 
43 
44 
45 enum VrControllerSide
46 {
47  Left,
48  Right,
49  Undefined
50 };
51 
56 {
57 public:
58  enum ControllerType
59  {
60  CLASSIC_CONTROLLER = 0,
61  TRACKER_CONTROLLER = 1,
62  HANDS_CONTROLLER = 2
63  };
64 protected:
65 private:
66  vr::IVRSystem* mHmdSystem;
67  unsigned int mDeviceId;
68  bool mVisible;
69  VrControllerSide mSide;
70  Matrix4 mMatrix;
71  vr::VRControllerState_t mControllerState;
72  std::vector<vr::EVRButtonId> mButtonsToHandle;
73  std::vector<bool> mButtonsStates;
74  Vector3 mAxisValues;
75  Vector3 mVelocity;
76  Vector3 mAngularVelocity;
77  int64 mLastRumble;
78  ControllerType mType;
79 
80 #if USE_VR_INPUT
81  vr::VRInputValueHandle_t mSource;
82  vr::VRActionHandle_t mActionPose;
83  vr::VRActionHandle_t mActionHaptic;
84  vr::VRActionHandle_t mPadAxis;
85  vr::VRActionHandle_t mTriggerAxis;
86 
87  std::vector<vr::VRActionHandle_t> mActionsToHandle;
88 #endif
89 
90 #ifdef USE_VIVE_GESTURE
91  GestureType mLastGesture;
92 #endif
93 
94 public:
98  sOpenvrController(vr::IVRSystem* hmdSystem, unsigned int id);
99 
104 
105  void Update(vr::TrackedDevicePose_t* trackedPoses, unsigned int deviceId);
106 
107 #ifdef USE_VIVE_GESTURE
108  void UpdateGesture(GestureResult result, unsigned int deviceId);
109  GestureType GetLastGesture();
110 #endif
111 
112  bool IsVisible();
113  void SetState(bool state);
114  void SetSide(VrControllerSide side);
115  VrControllerSide GetSide();
116  void SetType(ControllerType type);
117  ControllerType GetType();
118  Vector3 GetPosition();
119  Vector3 GetVelocity();
120  Vector3 GetAngularVelocity();
121  Quaternion GetOrientation();
122  void Rumble(float value);
123  std::vector<bool> GetButtonsState();
124  Vector3 GetAxisValues();
125 };
126 
130 class sOpenvr
131 {
132 public:
133 protected:
134  bool mConnected;
135 
136 private:
137  vr::IVRSystem* mHMDSystem;
138  vr::HmdError mHMDError;
139  vr::TrackedDevicePose_t mTrackedPoses[vr::k_unMaxTrackedDeviceCount];
140  std::array<vr::VRTextureBounds_t, 2> mTexturesBounds;
141  std::array<vr::Texture_t, 2> mTextures;
142  Quaternion mPreviousHmdQuat;
143  Vector3 mPreviousHmdPos;
144  std::array<sOpenvrController*, MAX_VR_CONTROLLERS> mControllers;
145  vr::IVRTrackedCamera* mCamera;
146  vr::TrackedCameraHandle_t mTrackedCameraHandle;
147  uint32_t mCameraWidth;
148  uint32_t mCameraHeight;
149  uint32_t mCameraFrameBufferSize;
150  uint8_t* mCameraFrameBuffer;
151  uint32_t mCameraLastFrameId;
152  bool mCameraEnable;
153  bool mVisible;
154 
155 #if USE_VR_INPUT
156  vr::VRActionSetHandle_t mActionSet;
157  std::string mInputPath;
158 #endif
159 
160 #ifdef USE_VIVE_GESTURE
161  bool mGestureInterface;
162  GestureOption mGestureOption;
163  int mLastGestureFrameIndex;
164 #endif
165 public:
169  sOpenvr(const char* inpuPath);
170 
174  ~sOpenvr();
175 
176  static Matrix4 getMatrix4FromSteamVRMatrix34(const vr::HmdMatrix34_t& mat);
177  bool Connect();
178  void Disconnect();
179  void ResetHmd();
180  bool IsConnected();
181  bool IsVisible();
182 
183  bool GetHmdOrientation(Quaternion &quat);
184  bool GetHmdPosition(Vector3 &vec);
185 
186  std::string GetHmdName();
187  bool GetProjectionMatrix(vr::EVREye eye, float nearclip, float farclip, Matrix4 &mat);
188  bool GetStereoTextureSize(unsigned int &w, unsigned int &h);
189  float GetStereoConfigAspect();
190  float GetStereoConfigFovY();
191  float GetStereoIPD();
192  bool Update();
193  void UpdateTextures(void* leftTexture, void* rightTexture, vr::ETextureType type);
194  sOpenvrController* GetController(unsigned int id);
195 
197  bool HaveCamera();
198  bool IsCameraEnable();
199  void GetCameraSize(uint32_t &w, uint32_t &h);
200  bool StartCamera();
201  void StopCamera();
202  bool GetCameraFrame(PtrObjBitmap scolBitmap);
203 
204 protected:
205 private:
206  void ProcessEvents();
207  void ProcessTrackedDevices();
208 };
209 
213 {
214 public:
216  {
217  public:
218  SConvertBuffer(uchar* _srcBuff, uchar* _srcBuffa, void* _dstBuff, const int &_width, const int &_height, const int &_sbpl, const int &_abpl, const int &_dbpl, const int &_bpp) :
219  srcBuff(_srcBuff),
220  srcBuffa(_srcBuffa),
221  dstBuff(_dstBuff),
222  width(_width),
223  height(_height),
224  sbpl(_sbpl),
225  abpl(_abpl),
226  dbpl(_dbpl),
227  bpp(_bpp)
228  {}
229 
230  ~SConvertBuffer(){}
231 
232  uchar* srcBuff;
233  uchar* srcBuffa;
234  void* dstBuff;
235  const int width;
236  const int height;
237  const int sbpl;
238  const int abpl;
239  const int dbpl;
240  const int bpp;
241  };
242 protected:
243 private:
244 
245 public:
248  static void BuffToBitmap(const int start, const int end, const SConvertBuffer &conv);
249 
250 protected:
251 private:
255  ConversionTools();
256 };
257 
258 #endif
sOpenvrController(vr::IVRSystem *hmdSystem, unsigned int id)
Definition: sOpenvr.cpp:672