processedFFMaterial.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 "materials/processedFFMaterial.h"
  24. #include "gfx/sim/cubemapData.h"
  25. #include "materials/sceneData.h"
  26. #include "materials/customMaterialDefinition.h"
  27. #include "materials/materialFeatureTypes.h"
  28. #include "gfx/sim/gfxStateBlockData.h"
  29. #include "gfx/gfxDevice.h"
  30. #include "gfx/genericConstBuffer.h"
  31. #include "materials/materialParameters.h"
  32. #include "lighting/lightInfo.h"
  33. #include "scene/sceneRenderState.h"
  34. #include "core/util/safeDelete.h"
  35. #include "math/util/matrixSet.h"
  36. class FFMaterialParameterHandle : public MaterialParameterHandle
  37. {
  38. public:
  39. virtual ~FFMaterialParameterHandle() {}
  40. virtual const String& getName() const { return mName; }
  41. virtual bool isValid() const { return false; }
  42. virtual S32 getSamplerRegister( U32 pass ) const { return -1; }
  43. private:
  44. String mName;
  45. };
  46. ProcessedFFMaterial::ProcessedFFMaterial()
  47. {
  48. VECTOR_SET_ASSOCIATION( mParamDesc );
  49. _construct();
  50. }
  51. ProcessedFFMaterial::ProcessedFFMaterial(Material &mat, const bool isLightingMaterial)
  52. {
  53. VECTOR_SET_ASSOCIATION( mParamDesc );
  54. _construct();
  55. mMaterial = &mat;
  56. mIsLightingMaterial = isLightingMaterial;
  57. }
  58. void ProcessedFFMaterial::_construct()
  59. {
  60. mHasSetStageData = false;
  61. mHasGlow = false;
  62. mHasAccumulation = false;
  63. mIsLightingMaterial = false;
  64. mDefaultHandle = new FFMaterialParameterHandle();
  65. mDefaultParameters = new MaterialParameters();
  66. mCurrentParams = mDefaultParameters;
  67. }
  68. ProcessedFFMaterial::~ProcessedFFMaterial()
  69. {
  70. SAFE_DELETE(mDefaultParameters);
  71. SAFE_DELETE( mDefaultHandle );
  72. }
  73. void ProcessedFFMaterial::_createPasses( U32 stageNum, const FeatureSet &features )
  74. {
  75. FixedFuncFeatureData featData;
  76. _determineFeatures(stageNum, featData, features);
  77. // Just create a simple pass!
  78. _addPass(0, featData);
  79. mFeatures.clear();
  80. if ( featData.features[FixedFuncFeatureData::DiffuseMap] )
  81. mFeatures.addFeature( MFT_DiffuseMap );
  82. if ( featData.features[FixedFuncFeatureData::LightMap] )
  83. mFeatures.addFeature( MFT_LightMap );
  84. if ( featData.features[FixedFuncFeatureData::ToneMap] )
  85. mFeatures.addFeature( MFT_ToneMap );
  86. }
  87. void ProcessedFFMaterial::_determineFeatures( U32 stageNum,
  88. FixedFuncFeatureData& featData,
  89. const FeatureSet &features )
  90. {
  91. if ( mStages[stageNum].getTex( MFT_DiffuseMap ) )
  92. featData.features[FixedFuncFeatureData::DiffuseMap] = true;
  93. if ( features.hasFeature( MFT_LightMap ) )
  94. featData.features[FixedFuncFeatureData::LightMap] = true;
  95. if ( features.hasFeature( MFT_ToneMap ))
  96. featData.features[FixedFuncFeatureData::ToneMap] = true;
  97. }
  98. U32 ProcessedFFMaterial::getNumStages()
  99. {
  100. // Loops through all stages to determine how many stages we actually use
  101. U32 numStages = 0;
  102. U32 i;
  103. for( i=0; i<Material::MAX_STAGES; i++ )
  104. {
  105. // Assume stage is inactive
  106. bool stageActive = false;
  107. // Cubemaps only on first stage
  108. if( i == 0 )
  109. {
  110. // If we have a cubemap the stage is active
  111. if( mMaterial->mCubemapData || mMaterial->mDynamicCubemap )
  112. {
  113. numStages++;
  114. continue;
  115. }
  116. }
  117. // If we have a texture for the a feature the
  118. // stage is active.
  119. if ( mStages[i].hasValidTex() )
  120. stageActive = true;
  121. // If this stage has specular lighting, it's active
  122. if ( mMaterial->mPixelSpecular[i] )
  123. stageActive = true;
  124. // If we have a Material that is vertex lit
  125. // then it may not have a texture
  126. if( mMaterial->mVertLit[i] )
  127. {
  128. stageActive = true;
  129. }
  130. // Increment the number of active stages
  131. numStages += stageActive;
  132. }
  133. return numStages;
  134. }
  135. bool ProcessedFFMaterial::setupPass( SceneRenderState *state, const SceneData &sgData, U32 pass )
  136. {
  137. PROFILE_SCOPE( ProcessedFFMaterial_SetupPass );
  138. // Make sure we have a pass
  139. if(pass >= mPasses.size())
  140. return false;
  141. _setRenderState( state, sgData, pass );
  142. // Bind our textures
  143. setTextureStages( state, sgData, pass );
  144. return true;
  145. }
  146. void ProcessedFFMaterial::setTextureStages(SceneRenderState * state, const SceneData& sgData, U32 pass)
  147. {
  148. // We may need to do some trickery in here for fixed function, this is just copy/paste from MatInstance
  149. #ifdef TORQUE_DEBUG
  150. AssertFatal( pass<mPasses.size(), "Pass out of bounds" );
  151. #endif
  152. RenderPassData *rpd = mPasses[pass];
  153. for( U32 i=0; i<rpd->mNumTex; i++ )
  154. {
  155. U32 currTexFlag = rpd->mTexType[i];
  156. if (!LIGHTMGR || !LIGHTMGR->setTextureStage(sgData, currTexFlag, i, NULL, NULL))
  157. {
  158. switch( currTexFlag )
  159. {
  160. case Material::NoTexture:
  161. if (rpd->mTexSlot[i].texObject)
  162. GFX->setTexture( i, rpd->mTexSlot[i].texObject );
  163. break;
  164. case Material::NormalizeCube:
  165. GFX->setCubeTexture(i, Material::GetNormalizeCube());
  166. break;
  167. case Material::Lightmap:
  168. GFX->setTexture( i, sgData.lightmap );
  169. break;
  170. case Material::Cube:
  171. // TODO: Is this right?
  172. GFX->setTexture( i, rpd->mTexSlot[0].texObject );
  173. break;
  174. case Material::SGCube:
  175. // No cubemap support just yet
  176. //GFX->setCubeTexture( i, sgData.cubemap );
  177. GFX->setTexture( i, rpd->mTexSlot[0].texObject );
  178. break;
  179. case Material::BackBuff:
  180. GFX->setTexture( i, sgData.backBuffTex );
  181. break;
  182. }
  183. }
  184. }
  185. }
  186. MaterialParameters* ProcessedFFMaterial::allocMaterialParameters()
  187. {
  188. return new MaterialParameters();
  189. }
  190. MaterialParameters* ProcessedFFMaterial::getDefaultMaterialParameters()
  191. {
  192. return mDefaultParameters;
  193. }
  194. MaterialParameterHandle* ProcessedFFMaterial::getMaterialParameterHandle(const String& name)
  195. {
  196. return mDefaultHandle;
  197. }
  198. void ProcessedFFMaterial::setTransforms(const MatrixSet &matrixSet, SceneRenderState *state, const U32 pass)
  199. {
  200. GFX->setWorldMatrix(matrixSet.getObjectToWorld());
  201. GFX->setViewMatrix(matrixSet.getWorldToCamera());
  202. GFX->setProjectionMatrix(matrixSet.getCameraToScreen());
  203. }
  204. void ProcessedFFMaterial::setSceneInfo(SceneRenderState * state, const SceneData& sgData, U32 pass)
  205. {
  206. _setPrimaryLightInfo(*sgData.objTrans, sgData.lights[0], pass);
  207. _setSecondaryLightInfo(*sgData.objTrans, sgData.lights[1]);
  208. }
  209. void ProcessedFFMaterial::_setPrimaryLightInfo(const MatrixF &_objTrans, LightInfo* light, U32 pass)
  210. {
  211. // Just in case
  212. GFX->setGlobalAmbientColor(ColorF(0.0f, 0.0f, 0.0f, 1.0f));
  213. if ( light->getType() == LightInfo::Ambient )
  214. {
  215. // Ambient light
  216. GFX->setGlobalAmbientColor( light->getAmbient() );
  217. return;
  218. }
  219. GFX->setLight(0, NULL);
  220. GFX->setLight(1, NULL);
  221. // This is a quick hack that lets us use FF lights
  222. GFXLightMaterial lightMat;
  223. lightMat.ambient = ColorF(1.0f, 1.0f, 1.0f, 1.0f);
  224. lightMat.diffuse = ColorF(1.0f, 1.0f, 1.0f, 1.0f);
  225. lightMat.emissive = ColorF(0.0f, 0.0f, 0.0f, 0.0f);
  226. lightMat.specular = ColorF(0.0f, 0.0f, 0.0f, 0.0f);
  227. lightMat.shininess = 128.0f;
  228. GFX->setLightMaterial(lightMat);
  229. // set object transform
  230. MatrixF objTrans = _objTrans;
  231. objTrans.inverse();
  232. // fill in primary light
  233. //-------------------------
  234. GFXLightInfo xlatedLight;
  235. light->setGFXLight(&xlatedLight);
  236. Point3F lightPos = light->getPosition();
  237. Point3F lightDir = light->getDirection();
  238. objTrans.mulP(lightPos);
  239. objTrans.mulV(lightDir);
  240. xlatedLight.mPos = lightPos;
  241. xlatedLight.mDirection = lightDir;
  242. GFX->setLight(0, &xlatedLight);
  243. }
  244. void ProcessedFFMaterial::_setSecondaryLightInfo(const MatrixF &_objTrans, LightInfo* light)
  245. {
  246. // set object transform
  247. MatrixF objTrans = _objTrans;
  248. objTrans.inverse();
  249. // fill in secondary light
  250. //-------------------------
  251. GFXLightInfo xlatedLight;
  252. light->setGFXLight(&xlatedLight);
  253. Point3F lightPos = light->getPosition();
  254. Point3F lightDir = light->getDirection();
  255. objTrans.mulP(lightPos);
  256. objTrans.mulV(lightDir);
  257. xlatedLight.mPos = lightPos;
  258. xlatedLight.mDirection = lightDir;
  259. GFX->setLight(1, &xlatedLight);
  260. }
  261. bool ProcessedFFMaterial::init( const FeatureSet &features,
  262. const GFXVertexFormat *vertexFormat,
  263. const MatFeaturesDelegate &featuresDelegate )
  264. {
  265. TORQUE_UNUSED( vertexFormat );
  266. TORQUE_UNUSED( featuresDelegate );
  267. _setStageData();
  268. // Just create a simple pass
  269. _createPasses(0, features);
  270. _initRenderPassDataStateBlocks();
  271. mStateHint.init( this );
  272. return true;
  273. }
  274. void ProcessedFFMaterial::_addPass(U32 stageNum, FixedFuncFeatureData& featData)
  275. {
  276. U32 numTex = 0;
  277. // Just creates a simple pass, but it can still glow!
  278. RenderPassData rpd;
  279. // Base texture, texunit 0
  280. if(featData.features[FixedFuncFeatureData::DiffuseMap])
  281. {
  282. rpd.mTexSlot[0].texObject = mStages[stageNum].getTex( MFT_DiffuseMap );
  283. rpd.mTexType[0] = Material::NoTexture;
  284. numTex++;
  285. }
  286. // lightmap, texunit 1
  287. if(featData.features[FixedFuncFeatureData::LightMap])
  288. {
  289. rpd.mTexType[1] = Material::Lightmap;
  290. numTex++;
  291. }
  292. rpd.mNumTex = numTex;
  293. rpd.mStageNum = stageNum;
  294. rpd.mGlow = false;
  295. mPasses.push_back( new RenderPassData(rpd) );
  296. }
  297. void ProcessedFFMaterial::_setPassBlendOp()
  298. {
  299. }
  300. void ProcessedFFMaterial::_initPassStateBlock( RenderPassData *rpd, GFXStateBlockDesc &result )
  301. {
  302. Parent::_initPassStateBlock( rpd, result );
  303. if ( mIsLightingMaterial )
  304. {
  305. result.ffLighting = true;
  306. result.blendDefined = true;
  307. result.blendEnable = true;
  308. result.blendSrc = GFXBlendOne;
  309. result.blendDest = GFXBlendZero;
  310. }
  311. // This is here for generic FF shader fallbacks.
  312. CustomMaterial* custmat = dynamic_cast<CustomMaterial*>(mMaterial);
  313. if (custmat && custmat->getStateBlockData() )
  314. result.addDesc(custmat->getStateBlockData()->getState());
  315. }