Visualization Library 2.0.0-b3

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
StereoCamera.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2017, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef StereoCamera_INCLUDE_ONCE
33 #define StereoCamera_INCLUDE_ONCE
34 
35 #include <vlGraphics/Camera.hpp>
36 
37 namespace vl
38 {
47  class StereoCamera: public Object
48  {
50 
51  public:
53  {
54  mConvergence = 20;
55  mEyeSeparation = 1;
56  }
57 
60  void setConvergence( float convergence ) { mConvergence = convergence; }
63  float convergence() const { return mConvergence; }
64 
66  void setEyeSeparation( float eye_separation ) { mEyeSeparation = eye_separation; }
68  float eyeSeparation() const { return mEyeSeparation; }
69 
72  void setMonoCamera(Camera* camera) { mMonoCamera = camera; }
75  Camera* monoCamera() { return mMonoCamera.get(); }
78  const Camera* monoCamera() const { return mMonoCamera.get(); }
79 
81  void setLeftCamera(Camera* camera) { mLeftCamera = camera; }
83  Camera* leftCamera() { return mLeftCamera.get(); }
85  const Camera* leftCamera() const { return mLeftCamera.get(); }
86 
88  void setRightCamera(Camera* camera) { mRightCamera = camera; }
90  Camera* rigthCamera() { return mRightCamera.get(); }
92  const Camera* rightCamera() const { return mRightCamera.get(); }
93 
96  {
97  mLeftCamera->setViewport( mMonoCamera->viewport() );
98  mRightCamera->setViewport( mMonoCamera->viewport() );
99 
100  float aspect_ratio = (float)mMonoCamera->viewport()->width()/mMonoCamera->viewport()->height();
101  float near_clip = mMonoCamera->nearPlane();
102  float far_clip = mMonoCamera->farPlane();
103  float radians = mMonoCamera->fov()/2*fDEG_TO_RAD;
104  float wd2 = near_clip * tan(radians);
105  float ndfl = near_clip / mConvergence;
106  float top, bottom, left, right;
107  top = wd2;
108  bottom = - wd2;
109 
110  left = - aspect_ratio * wd2 - mEyeSeparation/2 * ndfl;
111  right = aspect_ratio * wd2 - mEyeSeparation/2 * ndfl;
112  mLeftCamera->setProjectionFrustum(left, right, bottom, top, near_clip, far_clip);
113  mLeftCamera->setViewMatrix( mat4::getTranslation(-mEyeSeparation/2, 0, 0)*mMonoCamera->viewMatrix() );
114 
115  left = - aspect_ratio * wd2 + mEyeSeparation/2 * ndfl;
116  right = aspect_ratio * wd2 + mEyeSeparation/2 * ndfl;
117  mRightCamera->setProjectionFrustum(left, right, bottom, top, near_clip, far_clip);
118  mRightCamera->setViewMatrix( mat4::getTranslation(+mEyeSeparation/2, 0, 0)*mMonoCamera->viewMatrix() );
119  }
120 
121  private:
122  ref<Camera> mMonoCamera;
123  ref<Camera> mLeftCamera;
124  ref<Camera> mRightCamera;
125  float mConvergence;
126  float mEyeSeparation;
127  };
128 }
129 
130 #endif
T radians(T degrees)
Definition: glsl_math.hpp:147
Camera * monoCamera()
The Camera used to drive the left and right cameras.
Camera * leftCamera()
The Camera representing the left eye.
void setRightCamera(Camera *camera)
The Camera representing the right eye.
const Camera * leftCamera() const
The Camera representing the left eye.
void setEyeSeparation(float eye_separation)
The distance between the center of the two eyes.
void setConvergence(float convergence)
Distance of the convergence plane from the camera.
void setMonoCamera(Camera *camera)
The Camera used to drive the left and right cameras.
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
Visualization Library main namespace.
Utility class to setup a pair of left/right cameras for stereo rendering.
The base class for all the reference counted objects.
Definition: Object.hpp:158
float eyeSeparation() const
The distance between the center of the two eyes.
const float fDEG_TO_RAD
Constant to convert degree into radian using float precision.
Definition: std_types.hpp:73
const Camera * monoCamera() const
The Camera used to drive the left and right cameras.
static Matrix4 & getTranslation(Matrix4 &out, const Vector3< float > &v)
Definition: Matrix4.hpp:548
void updateLeftRightCameras()
Updates the left and right cameras based on the mono camera view matrix and viewport.
Camera * rigthCamera()
The Camera representing the right eye.
const Camera * rightCamera() const
The Camera representing the right eye.
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
float convergence() const
Distance of the convergence plane from the camera.
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:50
void setLeftCamera(Camera *camera)
The Camera representing the left eye.
T tan(T a)
Definition: glsl_math.hpp:251