OpenFrames
VRUtils.hpp
Go to the documentation of this file.
1 /***********************************
2  Copyright 2018 Ravishankar Mathur
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  ***********************************/
16 
21 #ifndef _OF_VRUTILS_
22 #define _OF_VRUTILS_
23 
24 #include <OpenFrames/Export.h>
26 
27 #include <osg/ref_ptr>
28 #include <osg/Referenced>
29 #include <osg/Camera>
30 #include <osg/Texture2D>
31 
32 namespace OpenFrames
33 {
34  class OpenVRDevice; // Used by VRCameraManager below
35 
42  struct OF_EXPORT VRTextureBuffer : public osg::Referenced
43  {
44  public:
46  VRTextureBuffer(int width = 1080, int height = 1200);
47  virtual ~VRTextureBuffer();
48 
49  // Textures for each eye's image
50  osg::ref_ptr<osg::Texture2D> _rightColorTex, _rightDepthTex;
51  osg::ref_ptr<osg::Texture2D> _leftColorTex, _leftDepthTex;
52 
53  // Cameras that allow color textures to be chained between VRCameras
54  // in case MSAA is being used
55  osg::ref_ptr<osg::Camera> _rightTexCamera, _leftTexCamera;
56  };
57 
64  class OF_EXPORT VRCamera : public osg::Referenced
65  {
66  public:
67  enum StereoMode
68  {
69  STEREO = 0, // Always stereo
70  MONO = 1, // Always mono
71  AUTO = 2 // Auto-switch between Stereo/Mono based on distance
72  };
73 
74  VRCamera(VRTextureBuffer *texBuffer, OpenVRDevice *ovrDevice, int camNum, StereoMode mode, bool useMSAA = false);
75 
76  // Get whether MSAA is being used
77  bool getUseMSAA() { return _useMSAA; }
78 
80  void setClearMask(GLbitfield mask);
81  GLbitfield getClearMask() { return _rightCamera->getClearMask(); }
82 
84  unsigned int getNumCameras();
85 
87  osg::Camera* getCamera(unsigned int pos);
88 
90  void disableCameras();
91 
93  void updateCameras(osg::Matrixd& rightProj,
94  osg::Matrixd& leftProj, osg::Matrixd& centerProj, const double &zNear);
95 
97  void addSlaveCamerasToView(osg::View *view, bool useMastersSceneData);
98 
99  protected:
100  virtual ~VRCamera();
101 
103  osg::ref_ptr<osg::Camera> _rightCamera, _leftCamera;
104 
106  osg::ref_ptr<osg::Camera> _monoCamera;
107 
109  osg::observer_ptr<VRTextureBuffer> _texBuffer;
110 
111  osg::observer_ptr<OpenVRDevice> _ovrDevice; // OpenVR interface
112 
113  StereoMode _mode; // Stereo rendering mode
114 
116  bool _useMSAA;
117  };
118 
126  {
127  VRCameraManager(VRTextureBuffer *texBuffer, OpenVRDevice *ovrDevice);
128  virtual ~VRCameraManager();
129 
130  virtual std::string getCameraName(unsigned int camNum);
131 
132  virtual void enableCamera(unsigned int camNum,
133  osg::Camera* mainCam,
134  const double &zNear, const double &zFar);
135  virtual void disableCameras(unsigned int start);
136  virtual void reset();
137  virtual double getMinZNear();
138 
139  typedef std::vector< osg::ref_ptr<VRCamera> > VRCameraList;
140  VRCameraList _vrCameraList;
141 
142  // The VR eye texture buffers and related utilities
143  osg::observer_ptr<VRTextureBuffer> _texBuffer;
144 
145  osg::observer_ptr<OpenVRDevice> _ovrDevice; // OpenVR interface
146  };
147 
148 } // !namespace OpenFrames
149 
150 #endif // !define _OF_VRUTILS_
Encapsulates textures used for VR offscreen rendering.
Definition: VRUtils.hpp:42
osg::ref_ptr< osg::Camera > _rightCamera
Definition: VRUtils.hpp:103
Represents data needed to use an OpenVR-supported HMD.
Definition: OpenVRDevice.hpp:87
Definition: CoordinateAxes.hpp:29
Definition: DepthPartitioner.hpp:95
osg::observer_ptr< VRTextureBuffer > _texBuffer
Definition: VRUtils.hpp:109
bool _useMSAA
Definition: VRUtils.hpp:116
Encapsulates cameras used for VR stereo rendering.
Definition: VRUtils.hpp:64
osg::ref_ptr< osg::Camera > _monoCamera
Definition: VRUtils.hpp:106
Creates and manages VR cameras for the depth partitioner.
Definition: VRUtils.hpp:125