reflectionMatHook.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/platform.h"
  23. #include "scene/reflectionMatHook.h"
  24. #include "materials/materialManager.h"
  25. #include "materials/customMaterialDefinition.h"
  26. #include "materials/materialFeatureTypes.h"
  27. #include "materials/materialFeatureData.h"
  28. #include "shaderGen/featureType.h"
  29. #include "shaderGen/featureMgr.h"
  30. #include "scene/sceneRenderState.h"
  31. const MatInstanceHookType ReflectionMaterialHook::Type( "Reflection" );
  32. ReflectionMaterialHook::ReflectionMaterialHook() :
  33. mReflectMat(NULL)
  34. {
  35. }
  36. ReflectionMaterialHook::~ReflectionMaterialHook()
  37. {
  38. SAFE_DELETE(mReflectMat);
  39. }
  40. void ReflectionMaterialHook::init( BaseMatInstance *inMat )
  41. {
  42. if( !inMat->isValid() )
  43. return;
  44. Material *reflectMat = (Material*)inMat->getMaterial();
  45. if ( inMat->isCustomMaterial() )
  46. {
  47. // This is a custom material... who knows what it really does...do something
  48. // smart here later.
  49. }
  50. // We may want to disable some states that the material might enable for us.
  51. GFXStateBlockDesc refractState = inMat->getUserStateBlock();
  52. // Always z-read, and z-write if the material isn't translucent
  53. refractState.setZReadWrite( true, reflectMat->isTranslucent() ? false : true );
  54. // Create reflection material instance.
  55. BaseMatInstance *newMat = new ReflectionMatInstance( reflectMat );
  56. newMat->setUserObject( inMat->getUserObject() );
  57. newMat->getFeaturesDelegate().bind( &ReflectionMaterialHook::_overrideFeatures );
  58. newMat->addStateBlockDesc( refractState );
  59. if( !newMat->init( inMat->getFeatures(), inMat->getVertexFormat() ) )
  60. {
  61. SAFE_DELETE( newMat );
  62. newMat = MATMGR->createWarningMatInstance();
  63. }
  64. mReflectMat = newMat;
  65. }
  66. void ReflectionMaterialHook::_overrideFeatures( ProcessedMaterial *mat,
  67. U32 stageNum,
  68. MaterialFeatureData &fd,
  69. const FeatureSet &features )
  70. {
  71. // First stage only in reflections
  72. if( stageNum != 0 )
  73. {
  74. fd.features.clear();
  75. return;
  76. }
  77. fd.features.addFeature( MFT_Fog );
  78. }
  79. //------------------------------------------------------------------------------
  80. ReflectionMatInstance::ReflectionMatInstance( Material *mat )
  81. : MatInstance( *mat )
  82. {
  83. }
  84. bool ReflectionMatInstance::setupPass( SceneRenderState *state, const SceneData &sgData )
  85. {
  86. return Parent::setupPass(state, sgData);
  87. }