reflectionMatHook.cpp
Engine/source/scene/reflectionMatHook.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "scene/reflectionMatHook.h" 26 27#include "materials/materialManager.h" 28#include "materials/customMaterialDefinition.h" 29#include "materials/materialFeatureTypes.h" 30#include "materials/materialFeatureData.h" 31#include "shaderGen/featureType.h" 32#include "shaderGen/featureMgr.h" 33#include "scene/sceneRenderState.h" 34 35 36const MatInstanceHookType ReflectionMaterialHook::Type( "Reflection" ); 37 38ReflectionMaterialHook::ReflectionMaterialHook() : 39 mReflectMat(NULL) 40{ 41 42} 43 44ReflectionMaterialHook::~ReflectionMaterialHook() 45{ 46 SAFE_DELETE(mReflectMat); 47} 48 49void ReflectionMaterialHook::init( BaseMatInstance *inMat ) 50{ 51 if( !inMat->isValid() ) 52 return; 53 54 Material *reflectMat = (Material*)inMat->getMaterial(); 55 if ( inMat->isCustomMaterial() ) 56 { 57 // This is a custom material... who knows what it really does...do something 58 // smart here later. 59 } 60 61 // We may want to disable some states that the material might enable for us. 62 GFXStateBlockDesc refractState = inMat->getUserStateBlock(); 63 64 // Always z-read, and z-write if the material isn't translucent 65 refractState.setZReadWrite( true, reflectMat->isTranslucent() ? false : true ); 66 67 // Create reflection material instance. 68 BaseMatInstance *newMat = new ReflectionMatInstance( reflectMat ); 69 newMat->setUserObject( inMat->getUserObject() ); 70 newMat->getFeaturesDelegate().bind( &ReflectionMaterialHook::_overrideFeatures ); 71 newMat->addStateBlockDesc( refractState ); 72 if( !newMat->init( inMat->getFeatures(), inMat->getVertexFormat() ) ) 73 { 74 SAFE_DELETE( newMat ); 75 newMat = MATMGR->createWarningMatInstance(); 76 } 77 78 mReflectMat = newMat; 79} 80 81void ReflectionMaterialHook::_overrideFeatures( ProcessedMaterial *mat, 82 U32 stageNum, 83 MaterialFeatureData &fd, 84 const FeatureSet &features ) 85{ 86 // First stage only in reflections 87 if( stageNum != 0 ) 88 { 89 fd.features.clear(); 90 return; 91 } 92 93 fd.features.addFeature( MFT_Fog ); 94} 95 96//------------------------------------------------------------------------------ 97 98ReflectionMatInstance::ReflectionMatInstance( Material *mat ) 99 : MatInstance( *mat ) 100{ 101 102} 103 104bool ReflectionMatInstance::setupPass( SceneRenderState *state, const SceneData &sgData ) 105{ 106 return Parent::setupPass(state, sgData); 107} 108