processedFFMaterial.cpp 11 KB

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