materialList.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "platform/platform.h"
  27. #include "materials/materialList.h"
  28. #include "materials/matInstance.h"
  29. #include "materials/materialManager.h"
  30. #include "materials/materialFeatureTypes.h"
  31. #include "materials/processedMaterial.h"
  32. #include "core/volume.h"
  33. #include "console/simSet.h"
  34. MaterialList::MaterialList()
  35. {
  36. VECTOR_SET_ASSOCIATION(mMatInstList);
  37. VECTOR_SET_ASSOCIATION(mMaterialNames);
  38. }
  39. MaterialList::MaterialList(const MaterialList* pCopy)
  40. {
  41. VECTOR_SET_ASSOCIATION(mMatInstList);
  42. VECTOR_SET_ASSOCIATION(mMaterialNames);
  43. mMaterialNames.setSize(pCopy->mMaterialNames.size());
  44. S32 i;
  45. for (i = 0; i < mMaterialNames.size(); i++)
  46. {
  47. mMaterialNames[i] = pCopy->mMaterialNames[i];
  48. }
  49. clearMatInstList();
  50. mMatInstList.setSize(pCopy->size());
  51. for( i = 0; i < mMatInstList.size(); i++ )
  52. {
  53. if( i < pCopy->mMatInstList.size() && pCopy->mMatInstList[i] )
  54. {
  55. mMatInstList[i] = pCopy->mMatInstList[i]->getMaterial()->createMatInstance();
  56. }
  57. else
  58. {
  59. mMatInstList[i] = NULL;
  60. }
  61. }
  62. }
  63. MaterialList::MaterialList(U32 materialCount, const char **materialNames)
  64. {
  65. VECTOR_SET_ASSOCIATION(mMaterialNames);
  66. set(materialCount, materialNames);
  67. }
  68. //--------------------------------------
  69. void MaterialList::set(U32 materialCount, const char **materialNames)
  70. {
  71. free();
  72. mMaterialNames.setSize(materialCount);
  73. clearMatInstList();
  74. mMatInstList.setSize(materialCount);
  75. for(U32 i = 0; i < materialCount; i++)
  76. {
  77. mMaterialNames[i] = materialNames[i];
  78. mMatInstList[i] = NULL;
  79. }
  80. }
  81. //--------------------------------------
  82. MaterialList::~MaterialList()
  83. {
  84. free();
  85. }
  86. //--------------------------------------
  87. void MaterialList::setMaterialName(U32 index, const String& name)
  88. {
  89. if (index < mMaterialNames.size())
  90. mMaterialNames[index] = name;
  91. }
  92. //--------------------------------------
  93. GFXTextureObject *MaterialList::getDiffuseTexture(U32 index)
  94. {
  95. AssertFatal(index < (U32) mMatInstList.size(), "MaterialList::getDiffuseTex: index lookup out of range.");
  96. MatInstance *matInst = dynamic_cast<MatInstance*>(mMatInstList[index]);
  97. if (matInst && matInst->getProcessedMaterial())
  98. return matInst->getProcessedMaterial()->getStageTexture(0, MFT_DiffuseMap);
  99. else
  100. return NULL;
  101. }
  102. //--------------------------------------
  103. void MaterialList::free()
  104. {
  105. clearMatInstList();
  106. mMatInstList.clear();
  107. mMaterialNames.clear();
  108. }
  109. /*
  110. //--------------------------------------
  111. U32 MaterialList::push_back(GFXTexHandle textureHandle, const String &filename)
  112. {
  113. mMaterialNames.push_back(filename);
  114. mMatInstList.push_back(NULL);
  115. // return the index
  116. return mMaterialNames.size()-1;
  117. }
  118. */
  119. //--------------------------------------
  120. U32 MaterialList::push_back(const String &filename, Material* material)
  121. {
  122. mMaterialNames.push_back(filename);
  123. mMatInstList.push_back(material ? material->createMatInstance() : NULL);
  124. // return the index
  125. return mMaterialNames.size()-1;
  126. }
  127. //--------------------------------------
  128. bool MaterialList::read(Stream &stream)
  129. {
  130. free();
  131. // check the stream version
  132. U8 version;
  133. if ( stream.read(&version) && version != BINARY_FILE_VERSION)
  134. return readText(stream,version);
  135. // how many materials?
  136. U32 count;
  137. if ( !stream.read(&count) )
  138. return false;
  139. // pre-size the vectors for efficiency
  140. mMaterialNames.reserve(count);
  141. // read in the materials
  142. for (U32 i=0; i<count; i++)
  143. {
  144. // Load the bitmap name
  145. char buffer[256];
  146. stream.readString(buffer);
  147. if( !buffer[0] )
  148. {
  149. AssertWarn(0, "MaterialList::read: error reading stream");
  150. return false;
  151. }
  152. // Material paths are a legacy of Tribes tools,
  153. // strip them off...
  154. char *name = &buffer[dStrlen(buffer)];
  155. while (name != buffer && name[-1] != '/' && name[-1] != '\\')
  156. name--;
  157. // Add it to the list
  158. mMaterialNames.push_back(name);
  159. mMatInstList.push_back(NULL);
  160. }
  161. return (stream.getStatus() == Stream::Ok);
  162. }
  163. //--------------------------------------
  164. bool MaterialList::write(Stream &stream)
  165. {
  166. stream.write((U8)BINARY_FILE_VERSION); // version
  167. stream.write((U32)mMaterialNames.size()); // material count
  168. for(S32 i=0; i < mMaterialNames.size(); i++) // material names
  169. stream.writeString(mMaterialNames[i]);
  170. return (stream.getStatus() == Stream::Ok);
  171. }
  172. //--------------------------------------
  173. bool MaterialList::readText(Stream &stream, U8 firstByte)
  174. {
  175. free();
  176. if (!firstByte)
  177. return (stream.getStatus() == Stream::Ok || stream.getStatus() == Stream::EOS);
  178. char buf[1024];
  179. buf[0] = firstByte;
  180. U32 offset = 1;
  181. for(;;)
  182. {
  183. stream.readLine((U8*)(buf+offset), sizeof(buf)-offset);
  184. if(!buf[0])
  185. break;
  186. offset = 0;
  187. // Material paths are a legacy of Tribes tools,
  188. // strip them off...
  189. char *name = &buf[dStrlen(buf)];
  190. while (name != buf && name[-1] != '/' && name[-1] != '\\')
  191. name--;
  192. // Add it to the list
  193. mMaterialNames.push_back(name);
  194. mMatInstList.push_back(NULL);
  195. }
  196. return (stream.getStatus() == Stream::Ok || stream.getStatus() == Stream::EOS);
  197. }
  198. bool MaterialList::readText(Stream &stream)
  199. {
  200. U8 firstByte;
  201. stream.read(&firstByte);
  202. return readText(stream,firstByte);
  203. }
  204. //--------------------------------------
  205. bool MaterialList::writeText(Stream &stream)
  206. {
  207. for(S32 i=0; i < mMaterialNames.size(); i++)
  208. stream.writeLine((U8*)(mMaterialNames[i].c_str()));
  209. stream.writeLine((U8*)"");
  210. return (stream.getStatus() == Stream::Ok);
  211. }
  212. //--------------------------------------------------------------------------
  213. // Clear all materials in the mMatInstList member variable
  214. //--------------------------------------------------------------------------
  215. void MaterialList::clearMatInstList()
  216. {
  217. // clear out old materials. any non null element of the list should be pointing at deletable memory,
  218. // although multiple indexes may be pointing at the same memory so we have to be careful (see
  219. // comment in loop body)
  220. for (U32 i=0; i<mMatInstList.size(); i++)
  221. {
  222. if (mMatInstList[i])
  223. {
  224. BaseMatInstance* current = mMatInstList[i];
  225. // ok, since ts material lists can remap difference indexes to the same object
  226. // we need to make sure that we don't delete the same memory twice. walk the
  227. // rest of the list and null out any pointers that match the one we deleted.
  228. for (U32 j=0; j<mMatInstList.size(); j++)
  229. if (mMatInstList[j] == current)
  230. mMatInstList[j] = NULL;
  231. mMatInstList[i] = NULL;
  232. delete current;
  233. }
  234. }
  235. }
  236. //--------------------------------------------------------------------------
  237. // Map materials - map materials to the textures in the list
  238. //--------------------------------------------------------------------------
  239. void MaterialList::mapMaterials()
  240. {
  241. mMatInstList.setSize( mMaterialNames.size() );
  242. for( U32 i=0; i<mMaterialNames.size(); i++ )
  243. mapMaterial( i );
  244. }
  245. /// Map the material name at the given index to a material instance.
  246. ///
  247. /// @note The material instance that is created will <em>not be initialized.</em>
  248. void MaterialList::mapMaterial( U32 i )
  249. {
  250. AssertFatal( i < size(), "MaterialList::mapMaterialList - index out of bounds" );
  251. if( mMatInstList[i] != NULL )
  252. return;
  253. // lookup a material property entry
  254. const String &matName = getMaterialName(i);
  255. // JMQ: this code assumes that all materials have names.
  256. if( matName.isEmpty() )
  257. {
  258. mMatInstList[i] = NULL;
  259. return;
  260. }
  261. String materialName;
  262. // Skip past a leading '#' marker.
  263. if (matName.compare("#", 1) == 0)
  264. materialName = MATMGR->getMapEntry(matName.substr(1, matName.length()-1));
  265. else
  266. materialName = MATMGR->getMapEntry(matName);
  267. // IF we didn't find it, then look for a PolyStatic generated Material
  268. // [a little cheesy, but we need to allow for user override of generated Materials]
  269. if ( materialName.isEmpty() )
  270. materialName = MATMGR->getMapEntry( String::ToString( "polyMat_%s", matName.c_str() ) );
  271. if ( materialName.isNotEmpty() )
  272. {
  273. Material * mat = MATMGR->getMaterialDefinitionByName( materialName );
  274. mMatInstList[i] = mat ? mat->createMatInstance() : MATMGR->createWarningMatInstance();
  275. }
  276. else
  277. {
  278. if ( Con::getBoolVariable( "$Materials::createMissing", true ) )
  279. {
  280. // No Material found, create new "default" material with just a diffuseMap
  281. // First see if there is a valid diffuse texture
  282. GFXTexHandle texHandle;
  283. if (mLookupPath.isEmpty())
  284. {
  285. texHandle.set( mMaterialNames[i], &GFXStaticTextureSRGBProfile, avar("%s() - handle (line %d)", __FUNCTION__, __LINE__) );
  286. }
  287. else
  288. {
  289. // Should we strip off the extension of the path here before trying
  290. // to load the texture?
  291. String fullPath = String::ToString( "%s/%s", mLookupPath.c_str(), mMaterialNames[i].c_str() );
  292. texHandle.set( fullPath, &GFXStaticTextureSRGBProfile, avar("%s() - handle (line %d)", __FUNCTION__, __LINE__) );
  293. }
  294. if ( texHandle.isValid() )
  295. {
  296. String newMatName = Sim::getUniqueName( "DefaultMaterial" );
  297. Material *newMat = MATMGR->allocateAndRegister( newMatName, mMaterialNames[i] );
  298. // Flag this as an autogenerated Material
  299. newMat->mAutoGenerated = true;
  300. // Overwrite diffuseMap in new material
  301. newMat->mDiffuseMapFilename[0] = texHandle->mTextureLookupName;
  302. // Set up some defaults for transparent textures
  303. if (texHandle->mHasTransparency)
  304. {
  305. newMat->mTranslucent = true;
  306. newMat->mTranslucentBlendOp = Material::LerpAlpha;
  307. newMat->mTranslucentZWrite = true;
  308. newMat->mAlphaRef = 20;
  309. }
  310. // create a MatInstance for the new material
  311. mMatInstList[i] = newMat->createMatInstance();
  312. #ifndef TORQUE_SHIPPING
  313. Con::warnf( "[MaterialList::mapMaterials] Creating missing material for texture: %s", texHandle->mTextureLookupName.c_str() );
  314. #endif
  315. }
  316. else
  317. {
  318. Con::errorf( "[MaterialList::mapMaterials] Unable to find material for texture: %s", mMaterialNames[i].c_str() );
  319. mMatInstList[i] = MATMGR->createWarningMatInstance();
  320. }
  321. }
  322. else
  323. {
  324. mMatInstList[i] = MATMGR->createWarningMatInstance();
  325. }
  326. }
  327. }
  328. void MaterialList::initMatInstances( const FeatureSet &features,
  329. const GFXVertexFormat *vertexFormat )
  330. {
  331. for( U32 i=0; i < mMatInstList.size(); i++ )
  332. {
  333. BaseMatInstance *matInst = mMatInstList[i];
  334. if ( !matInst )
  335. continue;
  336. if ( !matInst->init( features, vertexFormat ) )
  337. {
  338. Con::errorf( "MaterialList::initMatInstances - failed to initialize material instance for '%s'",
  339. matInst->getMaterial()->getName() );
  340. // Fall back to warning material.
  341. SAFE_DELETE( matInst );
  342. matInst = MATMGR->createMatInstance( "WarningMaterial" );
  343. matInst->init( MATMGR->getDefaultFeatures(), vertexFormat );
  344. mMatInstList[ i ] = matInst;
  345. }
  346. }
  347. }
  348. void MaterialList::setMaterialInst( BaseMatInstance *matInst, U32 texIndex )
  349. {
  350. AssertFatal( texIndex < mMatInstList.size(), "MaterialList::setMaterialInst - index out of bounds" );
  351. mMatInstList[texIndex] = matInst;
  352. }