barrelDistortionPostEffect.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. mScaleSC(NULL),
  38. mScaleInSC(NULL),
  39. mLensCenterSC(NULL),
  40. mScreenCenterSC(NULL)
  41. {
  42. mHMDIndex = 0;
  43. mSensorIndex = 0;
  44. mScaleOutput = 1.0f;
  45. }
  46. BarrelDistortionPostEffect::~BarrelDistortionPostEffect()
  47. {
  48. }
  49. void BarrelDistortionPostEffect::initPersistFields()
  50. {
  51. addField( "hmdIndex", TypeS32, Offset( mHMDIndex, BarrelDistortionPostEffect ),
  52. "Oculus VR HMD index to reference." );
  53. addField( "sensorIndex", TypeS32, Offset( mSensorIndex, BarrelDistortionPostEffect ),
  54. "Oculus VR sensor index to reference." );
  55. addField( "scaleOutput", TypeF32, Offset( mScaleOutput, BarrelDistortionPostEffect ),
  56. "Used to increase the size of the window into the world at the expense of apparent resolution." );
  57. Parent::initPersistFields();
  58. }
  59. bool BarrelDistortionPostEffect::onAdd()
  60. {
  61. if( !Parent::onAdd() )
  62. return false;
  63. return true;
  64. }
  65. void BarrelDistortionPostEffect::onRemove()
  66. {
  67. Parent::onRemove();
  68. }
  69. void BarrelDistortionPostEffect::_setupConstants( const SceneRenderState *state )
  70. {
  71. Parent::_setupConstants(state);
  72. // Define the shader constants
  73. if(!mHmdWarpParamSC)
  74. mHmdWarpParamSC = mShader->getShaderConstHandle( "$HmdWarpParam" );
  75. if(!mScaleSC)
  76. mScaleSC = mShader->getShaderConstHandle( "$Scale" );
  77. if(!mScaleInSC)
  78. mScaleInSC = mShader->getShaderConstHandle( "$ScaleIn" );
  79. if(!mLensCenterSC)
  80. mLensCenterSC = mShader->getShaderConstHandle( "$LensCenter" );
  81. if(!mScreenCenterSC)
  82. mScreenCenterSC = mShader->getShaderConstHandle( "$ScreenCenter" );
  83. const Point2I &resolution = GFX->getActiveRenderTarget()->getSize();
  84. F32 widthScale = 0.5f;
  85. F32 heightScale = 1.0f;
  86. F32 aspectRatio = (resolution.x * 0.5f) / resolution.y;
  87. // Set up the HMD dependant shader constants
  88. if(ManagedSingleton<OculusVRDevice>::instanceOrNull() && OCULUSVRDEV->getHMDDevice(mHMDIndex))
  89. {
  90. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(mHMDIndex);
  91. if(mHmdWarpParamSC->isValid())
  92. {
  93. const Point4F& distortion = hmd->getKDistortion();
  94. mShaderConsts->set( mHmdWarpParamSC, distortion );
  95. }
  96. if(mScaleSC->isValid())
  97. {
  98. F32 scaleFactor = hmd->getDistortionScale();
  99. if(!mIsZero(mScaleOutput))
  100. {
  101. scaleFactor /= mScaleOutput;
  102. }
  103. Point2F scale;
  104. scale.x = widthScale * 0.5f * scaleFactor;
  105. scale.y = heightScale * 0.5f * scaleFactor * aspectRatio;
  106. mShaderConsts->set( mScaleSC, scale );
  107. }
  108. if(mLensCenterSC->isValid())
  109. {
  110. F32 xCenterOffset = hmd->getCenterOffset();
  111. Point3F lensCenter;
  112. lensCenter.x = (widthScale + xCenterOffset * 0.5f) * 0.5f;
  113. lensCenter.y = (widthScale - xCenterOffset * 0.5f) * 0.5f;
  114. lensCenter.z = heightScale * 0.5f;
  115. mShaderConsts->set( mLensCenterSC, lensCenter );
  116. }
  117. }
  118. else
  119. {
  120. if(mHmdWarpParamSC->isValid())
  121. {
  122. mShaderConsts->set( mHmdWarpParamSC, Point4F(0.0f, 0.0f, 0.0f, 0.0f) );
  123. }
  124. if(mScaleSC->isValid())
  125. {
  126. mShaderConsts->set( mScaleSC, Point2F(1.0f, 1.0f) );
  127. }
  128. if(mLensCenterSC->isValid())
  129. {
  130. Point3F lensCenter;
  131. lensCenter.x = widthScale * 0.5f;
  132. lensCenter.y = widthScale * 0.5f;
  133. lensCenter.z = heightScale * 0.5f;
  134. mShaderConsts->set( mLensCenterSC, lensCenter );
  135. }
  136. }
  137. if(mScaleInSC->isValid())
  138. {
  139. Point2F scaleIn;
  140. scaleIn.x = 2.0f / widthScale;
  141. scaleIn.y = 2.0f / heightScale / aspectRatio;
  142. mShaderConsts->set( mScaleInSC, scaleIn );
  143. }
  144. if(mScreenCenterSC->isValid())
  145. {
  146. mShaderConsts->set( mScreenCenterSC, Point2F(widthScale * 0.5f, heightScale * 0.5f) );
  147. }
  148. }
  149. void BarrelDistortionPostEffect::process(const SceneRenderState *state, GFXTexHandle &inOutTex, const RectI *inTexViewport)
  150. {
  151. // Don't draw the post effect if the editor is active
  152. if(gEditingMission)
  153. return;
  154. Parent::process(state, inOutTex, inTexViewport);
  155. }