oculus1

diff libovr/Src/Util/Util_Render_Stereo.h @ 3:b069a5c27388

added a couple more stuff, fixed all the LibOVR line endings
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Sep 2013 04:10:05 +0300
parents e2f9e4603129
children
line diff
     1.1 --- a/libovr/Src/Util/Util_Render_Stereo.h	Sat Sep 14 17:51:03 2013 +0300
     1.2 +++ b/libovr/Src/Util/Util_Render_Stereo.h	Sun Sep 15 04:10:05 2013 +0300
     1.3 @@ -1,1 +1,300 @@
     1.4 -/************************************************************************************
     1.5 
     1.6 PublicHeader:   OVR.h
     1.7 Filename    :   Util_Render_Stereo.h
     1.8 Content     :   Sample stereo rendering configuration classes.
     1.9 Created     :   October 22, 2012
    1.10 Authors     :   Michael Antonov
    1.11 
    1.12 Copyright   :   Copyright 2012 Oculus, Inc. All Rights reserved.
    1.13 
    1.14 Use of this software is subject to the terms of the Oculus Inc license
    1.15 agreement provided at the time of installation or download, or which
    1.16 otherwise accompanies this software in either electronic or hard copy form.
    1.17 
    1.18 *************************************************************************************/
    1.19 
    1.20 #ifndef OVR_Util_Render_Stereo_h
    1.21 #define OVR_Util_Render_Stereo_h
    1.22 
    1.23 #include "../OVR_Device.h"
    1.24 
    1.25 namespace OVR { namespace Util { namespace Render {
    1.26 
    1.27 
    1.28 //-----------------------------------------------------------------------------------
    1.29 // ***** Stereo Enumerations
    1.30 
    1.31 // StereoMode describes rendering modes that can be used by StereoConfig.
    1.32 // These modes control whether stereo rendering is used or not (Stereo_None),
    1.33 // and how it is implemented.
    1.34 enum StereoMode
    1.35 {
    1.36     Stereo_None                 = 0,
    1.37     Stereo_LeftRight_Multipass  = 1
    1.38 };
    1.39 
    1.40 
    1.41 // StereoEye specifies which eye we are rendering for; it is used to
    1.42 // retrieve StereoEyeParams.
    1.43 enum StereoEye
    1.44 {
    1.45     StereoEye_Center,
    1.46     StereoEye_Left,
    1.47     StereoEye_Right    
    1.48 };
    1.49 
    1.50 
    1.51 //-----------------------------------------------------------------------------------
    1.52 // ***** Viewport
    1.53 
    1.54 // Viewport describes a rectangular area used for rendering, in pixels.
    1.55 struct Viewport
    1.56 {
    1.57     int x, y;
    1.58     int w, h;
    1.59 
    1.60     Viewport() {}
    1.61     Viewport(int x1, int y1, int w1, int h1) : x(x1), y(y1), w(w1), h(h1) { }
    1.62 
    1.63     bool operator == (const Viewport& vp) const
    1.64     { return (x == vp.x) && (y == vp.y) && (w == vp.w) && (h == vp.h); }
    1.65     bool operator != (const Viewport& vp) const
    1.66     { return !operator == (vp); }
    1.67 };
    1.68 
    1.69 
    1.70 //-----------------------------------------------------------------------------------
    1.71 // ***** DistortionConfig
    1.72 
    1.73 // DistortionConfig Provides controls for the distortion shader.
    1.74 //  - K[0] - K[3] are coefficients for the distortion function.
    1.75 //  - XCenterOffset is the offset of lens distortion center from the 
    1.76 //    center of one-eye screen half. [-1, 1] Range.
    1.77 //  - Scale is a factor of how much larger will the input image be,
    1.78 //    with a factor of 1.0f being no scaling. An inverse of this 
    1.79 //    value is applied to sampled UV coordinates (1/Scale).
    1.80 //  - ChromaticAberration is an array of parameters for controlling
    1.81 //    additional Red and Blue scaling in order to reduce chromatic aberration
    1.82 //    caused by the Rift lenses.
    1.83 class DistortionConfig
    1.84 {
    1.85 public:
    1.86     DistortionConfig(float k0 = 1.0f, float k1 = 0.0f, float k2 = 0.0f, float k3 = 0.0f)
    1.87         : XCenterOffset(0), YCenterOffset(0), Scale(1.0f)
    1.88     { 
    1.89         SetCoefficients(k0, k1, k2, k3);
    1.90         SetChromaticAberration();
    1.91     }
    1.92 
    1.93     void SetCoefficients(float k0, float k1 = 0.0f, float k2 = 0.0f, float k3 = 0.0f)
    1.94     { K[0] = k0; K[1] = k1;  K[2] = k2; K[3] = k3; }
    1.95 
    1.96     void SetChromaticAberration(float red1 = 1.0f, float red2 = 0.0f, float blue1 = 1.0f, float blue2 = 0.0f)
    1.97     { ChromaticAberration[0] = red1; ChromaticAberration[1] = red2; ChromaticAberration[2] = blue1; ChromaticAberration[3] = blue2; }
    1.98 
    1.99 
   1.100     // DistortionFn applies distortion equation to the argument. The returned
   1.101     // value should match distortion equation used in shader.
   1.102     float  DistortionFn(float r) const
   1.103     {        
   1.104         float rsq   = r * r;
   1.105         float scale = r * (K[0] + K[1] * rsq + K[2] * rsq * rsq + K[3] * rsq * rsq * rsq);
   1.106         return scale;
   1.107     }
   1.108 
   1.109     // DistortionFnInverse computes the inverse of the distortion function on an argument.
   1.110     float DistortionFnInverse(float r);
   1.111 
   1.112     float   K[4];
   1.113     float   XCenterOffset, YCenterOffset;
   1.114     float   Scale;
   1.115 
   1.116     float   ChromaticAberration[4]; // Additional per-channel scaling is applied after distortion:
   1.117                                     // Index [0] - Red channel constant coefficient.
   1.118                                     // Index [1] - Red channel r^2 coefficient.
   1.119                                     // Index [2] - Blue channel constant coefficient.
   1.120                                     // Index [3] - Blue channel r^2 coefficient.
   1.121 };
   1.122 
   1.123 
   1.124 //-----------------------------------------------------------------------------------
   1.125 // ***** StereoEyeParams
   1.126 
   1.127 // StereoEyeParams describes RenderDevice configuration needed to render
   1.128 // the scene for one eye. 
   1.129 class StereoEyeParams
   1.130 {
   1.131 public:
   1.132     StereoEye                Eye;
   1.133     Viewport                 VP;               // Viewport that we are rendering to        
   1.134     const DistortionConfig*  pDistortion;
   1.135 
   1.136     Matrix4f                 ViewAdjust;       // Translation to be applied to view matrix.
   1.137     Matrix4f                 Projection;       // Projection matrix used with this eye.
   1.138     Matrix4f                 OrthoProjection;  // Orthographic projection used with this eye.
   1.139 
   1.140     void Init(StereoEye eye, const Viewport &vp, float vofs,
   1.141               const Matrix4f& proj, const Matrix4f& orthoProj,
   1.142               const DistortionConfig* distortion = 0)
   1.143     {
   1.144         Eye                    = eye;
   1.145         VP                     = vp;
   1.146         ViewAdjust             = Matrix4f::Translation(Vector3f(vofs,0,0));
   1.147         Projection             = proj;
   1.148         OrthoProjection        = orthoProj;
   1.149         pDistortion            = distortion;        
   1.150     }
   1.151 };
   1.152 
   1.153 
   1.154 //-----------------------------------------------------------------------------------
   1.155 // *****  StereoConfig
   1.156 
   1.157 // StereoConfig maintains a scene stereo state and allow switching between different
   1.158 // stereo rendering modes. To support rendering, StereoConfig keeps track of HMD
   1.159 // variables such as screen size, eye-to-screen distance and distortion, and computes
   1.160 // extra data such as FOV and distortion center offsets based on it. Rendering
   1.161 // parameters are returned though StereoEyeParams for each eye.
   1.162 //
   1.163 // Beyond regular 3D projection, this class supports rendering a 2D orthographic
   1.164 // surface for UI and text. The 2D surface will be defined as fitting within a 2D
   1.165 // field of view (85 degrees by default) and used [-1,1] coordinate system with
   1.166 // square pixels. The (0,0) coordinate corresponds to eye center location
   1.167 // that is properly adjusted during rendering through SterepRenderParams::Adjust2D.
   1.168 // Genreally speaking, text outside [-1,1] coordinate range will not be readable.
   1.169 
   1.170 class StereoConfig
   1.171 {
   1.172 public:
   1.173 
   1.174     StereoConfig(StereoMode mode = Stereo_LeftRight_Multipass,
   1.175                  const Viewport& fullViewport = Viewport(0,0, 1280,800));
   1.176  
   1.177 
   1.178     // *** Modifiable State Access
   1.179 
   1.180     // Sets a stereo rendering mode and updates internal cached
   1.181     // state (matrices, per-eye view) based on it.
   1.182     void        SetStereoMode(StereoMode mode)  { Mode = mode; DirtyFlag = true; }
   1.183     StereoMode  GetStereoMode() const           { return Mode; }
   1.184 
   1.185     // Sets HMD parameters; also initializes distortion coefficients.
   1.186     void        SetHMDInfo(const HMDInfo& hmd);
   1.187     const HMDInfo& GetHMDInfo() const           { return HMD; }
   1.188 
   1.189     // Query physical eye-to-screen distance in meters, which combines screen-to-lens and
   1.190     // and lens-to-eye pupil distances. Modifying this value adjusts FOV.
   1.191     float       GetEyeToScreenDistance() const  { return HMD.EyeToScreenDistance; }
   1.192     void        SetEyeToScreenDistance(float esd) { HMD.EyeToScreenDistance = esd; DirtyFlag = true; }
   1.193 
   1.194     // Interpupillary distance used for stereo, in meters. Default is 0.064m (64 mm).
   1.195     void        SetIPD(float ipd)               { InterpupillaryDistance = ipd; IPDOverride = DirtyFlag = true; }
   1.196     float       GetIPD() const                  { return InterpupillaryDistance; }
   1.197 
   1.198     // Set full render target viewport; for HMD this includes both eyes. 
   1.199     void        SetFullViewport(const Viewport& vp);
   1.200     const Viewport& GetFullViewport() const     { return FullView; }
   1.201 
   1.202     // Aspect ratio defaults to ((w/h)*multiplier) computed per eye.
   1.203     // Aspect multiplier allows adjusting aspect ratio consistently for Stereo/NoStereo.
   1.204     void        SetAspectMultiplier(float m)    { AspectMultiplier = m; DirtyFlag = true; }
   1.205     float       GetAspectMultiplier() const     { return AspectMultiplier; }
   1.206 
   1.207     
   1.208     // For the distorted image to fill rendered viewport, input texture render target needs to be
   1.209     // scaled by DistortionScale before sampling. The scale factor is computed by fitting a point
   1.210     // on of specified radius from a distortion center, more easily specified as a coordinate.
   1.211     // SetDistortionFitPointVP sets the (x,y) coordinate of the point that scale will be "fit" to,
   1.212     // assuming [-1,1] coordinate range for full left-eye viewport. A fit point is a location
   1.213     // where source (pre-distortion) and target (post-distortion) image match each other.
   1.214     // For the right eye, the interpretation of 'u' will be inverted.  
   1.215     void       SetDistortionFitPointVP(float x, float y);
   1.216     // SetDistortionFitPointPixels sets the (x,y) coordinate of the point that scale will be "fit" to,
   1.217     // specified in pixeld for full left-eye texture.
   1.218     void       SetDistortionFitPointPixels(float x, float y);
   1.219 
   1.220     // Changes all distortion settings.
   1.221     // Note that setting HMDInfo also changes Distortion coefficients.
   1.222     void        SetDistortionConfig(const DistortionConfig& d) { Distortion = d; DirtyFlag = true; }
   1.223     
   1.224     // Modify distortion coefficients; useful for adjustment tweaking.
   1.225     void        SetDistortionK(int i, float k)  { Distortion.K[i] = k; DirtyFlag = true; }
   1.226     float       GetDistortionK(int i) const     { return Distortion.K[i]; }
   1.227 
   1.228     // Sets the fieldOfView that the 2D coordinate area stretches to.
   1.229     void        Set2DAreaFov(float fovRadians);
   1.230 
   1.231 
   1.232     // *** Computed State
   1.233 
   1.234     // Return current aspect ratio.
   1.235     float      GetAspect()                      { updateIfDirty(); return Aspect; }
   1.236     
   1.237     // Return computed vertical FOV in radians/degrees.
   1.238     float      GetYFOVRadians()                 { updateIfDirty(); return YFov; }
   1.239     float      GetYFOVDegrees()                 { return RadToDegree(GetYFOVRadians()); }
   1.240 
   1.241     // Query horizontal projection center offset as a distance away from the
   1.242     // one-eye [-1,1] unit viewport.
   1.243     // Positive return value should be used for left eye, negative for right eye. 
   1.244     float      GetProjectionCenterOffset()      { updateIfDirty(); return ProjectionCenterOffset; }
   1.245 
   1.246     // GetDistortionConfig isn't const because XCenterOffset bay need to be recomputed.  
   1.247     const DistortionConfig& GetDistortionConfig() { updateIfDirty(); return Distortion; }
   1.248 
   1.249     // Returns DistortionScale factor by which input texture size is increased to make
   1.250     // post-distortion result distortion fit the viewport.
   1.251     float      GetDistortionScale()             { updateIfDirty(); return Distortion.Scale; }
   1.252 
   1.253     // Returns the size of a pixel within 2D coordinate system.
   1.254     float      Get2DUnitPixel()                 { updateIfDirty();  return (2.0f / (FovPixels * Distortion.Scale)); }
   1.255 
   1.256     // Returns full set of Stereo rendering parameters for the specified eye.
   1.257     const StereoEyeParams& GetEyeRenderParams(StereoEye eye);
   1.258    
   1.259 private:    
   1.260 
   1.261     void updateIfDirty()   { if (DirtyFlag) updateComputedState(); }
   1.262     void updateComputedState();
   1.263 
   1.264     void updateDistortionOffsetAndScale();
   1.265     void updateProjectionOffset();
   1.266     void update2D();
   1.267     void updateEyeParams();
   1.268 
   1.269 
   1.270     // *** Modifiable State
   1.271 
   1.272     StereoMode         Mode;
   1.273     float              InterpupillaryDistance;
   1.274     float              AspectMultiplier;               // Multiplied into aspect ratio to change it.
   1.275     HMDInfo            HMD;
   1.276     DistortionConfig   Distortion;
   1.277     float              DistortionFitX, DistortionFitY; // In [-1,1] half-screen viewport units.
   1.278     Viewport           FullView;                       // Entire window viewport.
   1.279 
   1.280     float              Area2DFov;                      // FOV range mapping to [-1, 1] 2D area.
   1.281  
   1.282     // *** Computed State
   1.283  
   1.284     bool               DirtyFlag;   // Set when any if the modifiable state changed.
   1.285     bool               IPDOverride; // True after SetIPD was called.    
   1.286     float              YFov;        // Vertical FOV.
   1.287     float              Aspect;      // Aspect ratio: (w/h)*AspectMultiplier.
   1.288     float              ProjectionCenterOffset;
   1.289     StereoEyeParams    EyeRenderParams[2];
   1.290 
   1.291   
   1.292     // ** 2D Rendering
   1.293 
   1.294     // Number of 2D pixels in the FOV. This defines [-1,1] coordinate range for 2D.  
   1.295     float              FovPixels;
   1.296     Matrix4f           OrthoCenter;
   1.297     float              OrthoPixelOffset;
   1.298 };
   1.299 
   1.300 
   1.301 }}}  // OVR::Util::Render
   1.302 
   1.303 #endif
   1.304 \ No newline at end of file
   1.305 +/************************************************************************************
   1.306 +
   1.307 +PublicHeader:   OVR.h
   1.308 +Filename    :   Util_Render_Stereo.h
   1.309 +Content     :   Sample stereo rendering configuration classes.
   1.310 +Created     :   October 22, 2012
   1.311 +Authors     :   Michael Antonov
   1.312 +
   1.313 +Copyright   :   Copyright 2012 Oculus, Inc. All Rights reserved.
   1.314 +
   1.315 +Use of this software is subject to the terms of the Oculus Inc license
   1.316 +agreement provided at the time of installation or download, or which
   1.317 +otherwise accompanies this software in either electronic or hard copy form.
   1.318 +
   1.319 +*************************************************************************************/
   1.320 +
   1.321 +#ifndef OVR_Util_Render_Stereo_h
   1.322 +#define OVR_Util_Render_Stereo_h
   1.323 +
   1.324 +#include "../OVR_Device.h"
   1.325 +
   1.326 +namespace OVR { namespace Util { namespace Render {
   1.327 +
   1.328 +
   1.329 +//-----------------------------------------------------------------------------------
   1.330 +// ***** Stereo Enumerations
   1.331 +
   1.332 +// StereoMode describes rendering modes that can be used by StereoConfig.
   1.333 +// These modes control whether stereo rendering is used or not (Stereo_None),
   1.334 +// and how it is implemented.
   1.335 +enum StereoMode
   1.336 +{
   1.337 +    Stereo_None                 = 0,
   1.338 +    Stereo_LeftRight_Multipass  = 1
   1.339 +};
   1.340 +
   1.341 +
   1.342 +// StereoEye specifies which eye we are rendering for; it is used to
   1.343 +// retrieve StereoEyeParams.
   1.344 +enum StereoEye
   1.345 +{
   1.346 +    StereoEye_Center,
   1.347 +    StereoEye_Left,
   1.348 +    StereoEye_Right    
   1.349 +};
   1.350 +
   1.351 +
   1.352 +//-----------------------------------------------------------------------------------
   1.353 +// ***** Viewport
   1.354 +
   1.355 +// Viewport describes a rectangular area used for rendering, in pixels.
   1.356 +struct Viewport
   1.357 +{
   1.358 +    int x, y;
   1.359 +    int w, h;
   1.360 +
   1.361 +    Viewport() {}
   1.362 +    Viewport(int x1, int y1, int w1, int h1) : x(x1), y(y1), w(w1), h(h1) { }
   1.363 +
   1.364 +    bool operator == (const Viewport& vp) const
   1.365 +    { return (x == vp.x) && (y == vp.y) && (w == vp.w) && (h == vp.h); }
   1.366 +    bool operator != (const Viewport& vp) const
   1.367 +    { return !operator == (vp); }
   1.368 +};
   1.369 +
   1.370 +
   1.371 +//-----------------------------------------------------------------------------------
   1.372 +// ***** DistortionConfig
   1.373 +
   1.374 +// DistortionConfig Provides controls for the distortion shader.
   1.375 +//  - K[0] - K[3] are coefficients for the distortion function.
   1.376 +//  - XCenterOffset is the offset of lens distortion center from the 
   1.377 +//    center of one-eye screen half. [-1, 1] Range.
   1.378 +//  - Scale is a factor of how much larger will the input image be,
   1.379 +//    with a factor of 1.0f being no scaling. An inverse of this 
   1.380 +//    value is applied to sampled UV coordinates (1/Scale).
   1.381 +//  - ChromaticAberration is an array of parameters for controlling
   1.382 +//    additional Red and Blue scaling in order to reduce chromatic aberration
   1.383 +//    caused by the Rift lenses.
   1.384 +class DistortionConfig
   1.385 +{
   1.386 +public:
   1.387 +    DistortionConfig(float k0 = 1.0f, float k1 = 0.0f, float k2 = 0.0f, float k3 = 0.0f)
   1.388 +        : XCenterOffset(0), YCenterOffset(0), Scale(1.0f)
   1.389 +    { 
   1.390 +        SetCoefficients(k0, k1, k2, k3);
   1.391 +        SetChromaticAberration();
   1.392 +    }
   1.393 +
   1.394 +    void SetCoefficients(float k0, float k1 = 0.0f, float k2 = 0.0f, float k3 = 0.0f)
   1.395 +    { K[0] = k0; K[1] = k1;  K[2] = k2; K[3] = k3; }
   1.396 +
   1.397 +    void SetChromaticAberration(float red1 = 1.0f, float red2 = 0.0f, float blue1 = 1.0f, float blue2 = 0.0f)
   1.398 +    { ChromaticAberration[0] = red1; ChromaticAberration[1] = red2; ChromaticAberration[2] = blue1; ChromaticAberration[3] = blue2; }
   1.399 +
   1.400 +
   1.401 +    // DistortionFn applies distortion equation to the argument. The returned
   1.402 +    // value should match distortion equation used in shader.
   1.403 +    float  DistortionFn(float r) const
   1.404 +    {        
   1.405 +        float rsq   = r * r;
   1.406 +        float scale = r * (K[0] + K[1] * rsq + K[2] * rsq * rsq + K[3] * rsq * rsq * rsq);
   1.407 +        return scale;
   1.408 +    }
   1.409 +
   1.410 +    // DistortionFnInverse computes the inverse of the distortion function on an argument.
   1.411 +    float DistortionFnInverse(float r);
   1.412 +
   1.413 +    float   K[4];
   1.414 +    float   XCenterOffset, YCenterOffset;
   1.415 +    float   Scale;
   1.416 +
   1.417 +    float   ChromaticAberration[4]; // Additional per-channel scaling is applied after distortion:
   1.418 +                                    // Index [0] - Red channel constant coefficient.
   1.419 +                                    // Index [1] - Red channel r^2 coefficient.
   1.420 +                                    // Index [2] - Blue channel constant coefficient.
   1.421 +                                    // Index [3] - Blue channel r^2 coefficient.
   1.422 +};
   1.423 +
   1.424 +
   1.425 +//-----------------------------------------------------------------------------------
   1.426 +// ***** StereoEyeParams
   1.427 +
   1.428 +// StereoEyeParams describes RenderDevice configuration needed to render
   1.429 +// the scene for one eye. 
   1.430 +class StereoEyeParams
   1.431 +{
   1.432 +public:
   1.433 +    StereoEye                Eye;
   1.434 +    Viewport                 VP;               // Viewport that we are rendering to        
   1.435 +    const DistortionConfig*  pDistortion;
   1.436 +
   1.437 +    Matrix4f                 ViewAdjust;       // Translation to be applied to view matrix.
   1.438 +    Matrix4f                 Projection;       // Projection matrix used with this eye.
   1.439 +    Matrix4f                 OrthoProjection;  // Orthographic projection used with this eye.
   1.440 +
   1.441 +    void Init(StereoEye eye, const Viewport &vp, float vofs,
   1.442 +              const Matrix4f& proj, const Matrix4f& orthoProj,
   1.443 +              const DistortionConfig* distortion = 0)
   1.444 +    {
   1.445 +        Eye                    = eye;
   1.446 +        VP                     = vp;
   1.447 +        ViewAdjust             = Matrix4f::Translation(Vector3f(vofs,0,0));
   1.448 +        Projection             = proj;
   1.449 +        OrthoProjection        = orthoProj;
   1.450 +        pDistortion            = distortion;        
   1.451 +    }
   1.452 +};
   1.453 +
   1.454 +
   1.455 +//-----------------------------------------------------------------------------------
   1.456 +// *****  StereoConfig
   1.457 +
   1.458 +// StereoConfig maintains a scene stereo state and allow switching between different
   1.459 +// stereo rendering modes. To support rendering, StereoConfig keeps track of HMD
   1.460 +// variables such as screen size, eye-to-screen distance and distortion, and computes
   1.461 +// extra data such as FOV and distortion center offsets based on it. Rendering
   1.462 +// parameters are returned though StereoEyeParams for each eye.
   1.463 +//
   1.464 +// Beyond regular 3D projection, this class supports rendering a 2D orthographic
   1.465 +// surface for UI and text. The 2D surface will be defined as fitting within a 2D
   1.466 +// field of view (85 degrees by default) and used [-1,1] coordinate system with
   1.467 +// square pixels. The (0,0) coordinate corresponds to eye center location
   1.468 +// that is properly adjusted during rendering through SterepRenderParams::Adjust2D.
   1.469 +// Genreally speaking, text outside [-1,1] coordinate range will not be readable.
   1.470 +
   1.471 +class StereoConfig
   1.472 +{
   1.473 +public:
   1.474 +
   1.475 +    StereoConfig(StereoMode mode = Stereo_LeftRight_Multipass,
   1.476 +                 const Viewport& fullViewport = Viewport(0,0, 1280,800));
   1.477 + 
   1.478 +
   1.479 +    // *** Modifiable State Access
   1.480 +
   1.481 +    // Sets a stereo rendering mode and updates internal cached
   1.482 +    // state (matrices, per-eye view) based on it.
   1.483 +    void        SetStereoMode(StereoMode mode)  { Mode = mode; DirtyFlag = true; }
   1.484 +    StereoMode  GetStereoMode() const           { return Mode; }
   1.485 +
   1.486 +    // Sets HMD parameters; also initializes distortion coefficients.
   1.487 +    void        SetHMDInfo(const HMDInfo& hmd);
   1.488 +    const HMDInfo& GetHMDInfo() const           { return HMD; }
   1.489 +
   1.490 +    // Query physical eye-to-screen distance in meters, which combines screen-to-lens and
   1.491 +    // and lens-to-eye pupil distances. Modifying this value adjusts FOV.
   1.492 +    float       GetEyeToScreenDistance() const  { return HMD.EyeToScreenDistance; }
   1.493 +    void        SetEyeToScreenDistance(float esd) { HMD.EyeToScreenDistance = esd; DirtyFlag = true; }
   1.494 +
   1.495 +    // Interpupillary distance used for stereo, in meters. Default is 0.064m (64 mm).
   1.496 +    void        SetIPD(float ipd)               { InterpupillaryDistance = ipd; IPDOverride = DirtyFlag = true; }
   1.497 +    float       GetIPD() const                  { return InterpupillaryDistance; }
   1.498 +
   1.499 +    // Set full render target viewport; for HMD this includes both eyes. 
   1.500 +    void        SetFullViewport(const Viewport& vp);
   1.501 +    const Viewport& GetFullViewport() const     { return FullView; }
   1.502 +
   1.503 +    // Aspect ratio defaults to ((w/h)*multiplier) computed per eye.
   1.504 +    // Aspect multiplier allows adjusting aspect ratio consistently for Stereo/NoStereo.
   1.505 +    void        SetAspectMultiplier(float m)    { AspectMultiplier = m; DirtyFlag = true; }
   1.506 +    float       GetAspectMultiplier() const     { return AspectMultiplier; }
   1.507 +
   1.508 +    
   1.509 +    // For the distorted image to fill rendered viewport, input texture render target needs to be
   1.510 +    // scaled by DistortionScale before sampling. The scale factor is computed by fitting a point
   1.511 +    // on of specified radius from a distortion center, more easily specified as a coordinate.
   1.512 +    // SetDistortionFitPointVP sets the (x,y) coordinate of the point that scale will be "fit" to,
   1.513 +    // assuming [-1,1] coordinate range for full left-eye viewport. A fit point is a location
   1.514 +    // where source (pre-distortion) and target (post-distortion) image match each other.
   1.515 +    // For the right eye, the interpretation of 'u' will be inverted.  
   1.516 +    void       SetDistortionFitPointVP(float x, float y);
   1.517 +    // SetDistortionFitPointPixels sets the (x,y) coordinate of the point that scale will be "fit" to,
   1.518 +    // specified in pixeld for full left-eye texture.
   1.519 +    void       SetDistortionFitPointPixels(float x, float y);
   1.520 +
   1.521 +    // Changes all distortion settings.
   1.522 +    // Note that setting HMDInfo also changes Distortion coefficients.
   1.523 +    void        SetDistortionConfig(const DistortionConfig& d) { Distortion = d; DirtyFlag = true; }
   1.524 +    
   1.525 +    // Modify distortion coefficients; useful for adjustment tweaking.
   1.526 +    void        SetDistortionK(int i, float k)  { Distortion.K[i] = k; DirtyFlag = true; }
   1.527 +    float       GetDistortionK(int i) const     { return Distortion.K[i]; }
   1.528 +
   1.529 +    // Sets the fieldOfView that the 2D coordinate area stretches to.
   1.530 +    void        Set2DAreaFov(float fovRadians);
   1.531 +
   1.532 +
   1.533 +    // *** Computed State
   1.534 +
   1.535 +    // Return current aspect ratio.
   1.536 +    float      GetAspect()                      { updateIfDirty(); return Aspect; }
   1.537 +    
   1.538 +    // Return computed vertical FOV in radians/degrees.
   1.539 +    float      GetYFOVRadians()                 { updateIfDirty(); return YFov; }
   1.540 +    float      GetYFOVDegrees()                 { return RadToDegree(GetYFOVRadians()); }
   1.541 +
   1.542 +    // Query horizontal projection center offset as a distance away from the
   1.543 +    // one-eye [-1,1] unit viewport.
   1.544 +    // Positive return value should be used for left eye, negative for right eye. 
   1.545 +    float      GetProjectionCenterOffset()      { updateIfDirty(); return ProjectionCenterOffset; }
   1.546 +
   1.547 +    // GetDistortionConfig isn't const because XCenterOffset bay need to be recomputed.  
   1.548 +    const DistortionConfig& GetDistortionConfig() { updateIfDirty(); return Distortion; }
   1.549 +
   1.550 +    // Returns DistortionScale factor by which input texture size is increased to make
   1.551 +    // post-distortion result distortion fit the viewport.
   1.552 +    float      GetDistortionScale()             { updateIfDirty(); return Distortion.Scale; }
   1.553 +
   1.554 +    // Returns the size of a pixel within 2D coordinate system.
   1.555 +    float      Get2DUnitPixel()                 { updateIfDirty();  return (2.0f / (FovPixels * Distortion.Scale)); }
   1.556 +
   1.557 +    // Returns full set of Stereo rendering parameters for the specified eye.
   1.558 +    const StereoEyeParams& GetEyeRenderParams(StereoEye eye);
   1.559 +   
   1.560 +private:    
   1.561 +
   1.562 +    void updateIfDirty()   { if (DirtyFlag) updateComputedState(); }
   1.563 +    void updateComputedState();
   1.564 +
   1.565 +    void updateDistortionOffsetAndScale();
   1.566 +    void updateProjectionOffset();
   1.567 +    void update2D();
   1.568 +    void updateEyeParams();
   1.569 +
   1.570 +
   1.571 +    // *** Modifiable State
   1.572 +
   1.573 +    StereoMode         Mode;
   1.574 +    float              InterpupillaryDistance;
   1.575 +    float              AspectMultiplier;               // Multiplied into aspect ratio to change it.
   1.576 +    HMDInfo            HMD;
   1.577 +    DistortionConfig   Distortion;
   1.578 +    float              DistortionFitX, DistortionFitY; // In [-1,1] half-screen viewport units.
   1.579 +    Viewport           FullView;                       // Entire window viewport.
   1.580 +
   1.581 +    float              Area2DFov;                      // FOV range mapping to [-1, 1] 2D area.
   1.582 + 
   1.583 +    // *** Computed State
   1.584 + 
   1.585 +    bool               DirtyFlag;   // Set when any if the modifiable state changed.
   1.586 +    bool               IPDOverride; // True after SetIPD was called.    
   1.587 +    float              YFov;        // Vertical FOV.
   1.588 +    float              Aspect;      // Aspect ratio: (w/h)*AspectMultiplier.
   1.589 +    float              ProjectionCenterOffset;
   1.590 +    StereoEyeParams    EyeRenderParams[2];
   1.591 +
   1.592 +  
   1.593 +    // ** 2D Rendering
   1.594 +
   1.595 +    // Number of 2D pixels in the FOV. This defines [-1,1] coordinate range for 2D.  
   1.596 +    float              FovPixels;
   1.597 +    Matrix4f           OrthoCenter;
   1.598 +    float              OrthoPixelOffset;
   1.599 +};
   1.600 +
   1.601 +
   1.602 +}}}  // OVR::Util::Render
   1.603 +
   1.604 +#endif