barrelDistortionPostEffect.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/input/oculusVR/barrelDistortionPostEffect.h"
  23. #include "console/consoleTypes.h"
  24. #include "console/engineAPI.h"
  25. #include "gfx/gfxDevice.h"
  26. #include "platform/input/oculusVR/oculusVRDevice.h"
  27. extern bool gEditingMission;
  28. ConsoleDocClass( BarrelDistortionPostEffect,
  29. "@brief A fullscreen shader effect used with the Oculus Rift.\n\n"
  30. "@section PFXTextureIdentifiers\n\n"
  31. "@ingroup Rendering\n"
  32. );
  33. IMPLEMENT_CONOBJECT(BarrelDistortionPostEffect);
  34. BarrelDistortionPostEffect::BarrelDistortionPostEffect()
  35. : PostEffect(),
  36. mHmdWarpParamSC(NULL),
  37. mHmdChromaAbSC(NULL),
  38. mScaleSC(NULL),
  39. mScaleInSC(NULL),
  40. mLensCenterSC(NULL),
  41. mScreenCenterSC(NULL)
  42. {
  43. mHMDIndex = 0;
  44. mSensorIndex = 0;
  45. mScaleOutput = 1.0f;
  46. }
  47. BarrelDistortionPostEffect::~BarrelDistortionPostEffect()
  48. {
  49. }
  50. void BarrelDistortionPostEffect::initPersistFields()
  51. {
  52. addField( "hmdIndex", TypeS32, Offset( mHMDIndex, BarrelDistortionPostEffect ),
  53. "Oculus VR HMD index to reference." );
  54. addField( "sensorIndex", TypeS32, Offset( mSensorIndex, BarrelDistortionPostEffect ),
  55. "Oculus VR sensor index to reference." );
  56. addField( "scaleOutput", TypeF32, Offset( mScaleOutput, BarrelDistortionPostEffect ),
  57. "Used to increase the size of the window into the world at the expense of apparent resolution." );
  58. Parent::initPersistFields();
  59. }
  60. bool BarrelDistortionPostEffect::onAdd()
  61. {
  62. if( !Parent::onAdd() )
  63. return false;
  64. return true;
  65. }
  66. void BarrelDistortionPostEffect::onRemove()
  67. {
  68. Parent::onRemove();
  69. }
  70. void BarrelDistortionPostEffect::_setupConstants( const SceneRenderState *state )
  71. {
  72. // Test if setup is required before calling the parent method as the parent method
  73. // will set up the shader constants buffer for us.
  74. bool setupRequired = mShaderConsts.isNull();
  75. Parent::_setupConstants(state);
  76. // Define the shader constants
  77. if(setupRequired)
  78. {
  79. mHmdWarpParamSC = mShader->getShaderConstHandle( "$HmdWarpParam" );
  80. mHmdChromaAbSC = mShader->getShaderConstHandle( "$HmdChromaAbParam" );
  81. mScaleSC = mShader->getShaderConstHandle( "$Scale" );
  82. mScaleInSC = mShader->getShaderConstHandle( "$ScaleIn" );
  83. mLensCenterSC = mShader->getShaderConstHandle( "$LensCenter" );
  84. mScreenCenterSC = mShader->getShaderConstHandle( "$ScreenCenter" );
  85. }
  86. const Point2I &resolution = GFX->getActiveRenderTarget()->getSize();
  87. F32 widthScale = 0.5f;
  88. F32 heightScale = 1.0f;
  89. F32 aspectRatio = (resolution.x * 0.5f) / resolution.y;
  90. // Set up the HMD dependant shader constants
  91. if(ManagedSingleton<OculusVRDevice>::instanceOrNull() && OCULUSVRDEV->getHMDDevice(mHMDIndex))
  92. {
  93. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(mHMDIndex);
  94. if(mHmdWarpParamSC->isValid())
  95. {
  96. const Point4F& distortion = hmd->getKDistortion();
  97. mShaderConsts->set( mHmdWarpParamSC, distortion );
  98. }
  99. if(mHmdChromaAbSC->isValid())
  100. {
  101. const Point4F& correction = hmd->getChromaticAbCorrection();
  102. mShaderConsts->set( mHmdChromaAbSC, correction );
  103. }
  104. if(mScaleSC->isValid())
  105. {
  106. F32 scaleFactor = hmd->getDistortionScale();
  107. if(!mIsZero(mScaleOutput))
  108. {
  109. scaleFactor /= mScaleOutput;
  110. }
  111. Point2F scale;
  112. scale.x = widthScale * 0.5f * scaleFactor;
  113. scale.y = heightScale * 0.5f * scaleFactor * aspectRatio;
  114. mShaderConsts->set( mScaleSC, scale );
  115. }
  116. if(mLensCenterSC->isValid())
  117. {
  118. F32 xCenterOffset = hmd->getCenterOffset();
  119. Point3F lensCenter;
  120. lensCenter.x = (widthScale + xCenterOffset * 0.5f) * 0.5f;
  121. lensCenter.y = (widthScale - xCenterOffset * 0.5f) * 0.5f;
  122. lensCenter.z = heightScale * 0.5f;
  123. mShaderConsts->set( mLensCenterSC, lensCenter );
  124. }
  125. }
  126. else
  127. {
  128. if(mHmdWarpParamSC->isValid())
  129. {
  130. mShaderConsts->set( mHmdWarpParamSC, Point4F(0.0f, 0.0f, 0.0f, 0.0f) );
  131. }
  132. if(mHmdChromaAbSC->isValid())
  133. {
  134. mShaderConsts->set( mHmdChromaAbSC, Point4F(1.0f, 0.0f, 1.0f, 0.0f) );
  135. }
  136. if(mScaleSC->isValid())
  137. {
  138. mShaderConsts->set( mScaleSC, Point2F(1.0f, 1.0f) );
  139. }
  140. if(mLensCenterSC->isValid())
  141. {
  142. Point3F lensCenter;
  143. lensCenter.x = widthScale * 0.5f;
  144. lensCenter.y = widthScale * 0.5f;
  145. lensCenter.z = heightScale * 0.5f;
  146. mShaderConsts->set( mLensCenterSC, lensCenter );
  147. }
  148. }
  149. if(mScaleInSC->isValid())
  150. {
  151. Point2F scaleIn;
  152. scaleIn.x = 2.0f / widthScale;
  153. scaleIn.y = 2.0f / heightScale / aspectRatio;
  154. mShaderConsts->set( mScaleInSC, scaleIn );
  155. }
  156. if(mScreenCenterSC->isValid())
  157. {
  158. mShaderConsts->set( mScreenCenterSC, Point2F(widthScale * 0.5f, heightScale * 0.5f) );
  159. }
  160. }
  161. void BarrelDistortionPostEffect::process(const SceneRenderState *state, GFXTexHandle &inOutTex, const RectI *inTexViewport)
  162. {
  163. // Don't draw the post effect if the editor is active
  164. if(gEditingMission)
  165. return;
  166. Parent::process(state, inOutTex, inTexViewport);
  167. }