forcedMaterialMeshMgr.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "renderInstance/forcedMaterialMeshMgr.h"
  23. #include "gfx/gfxTransformSaver.h"
  24. #include "gfx/gfxPrimitiveBuffer.h"
  25. #include "gfx/gfxDebugEvent.h"
  26. #include "materials/sceneData.h"
  27. #include "materials/materialManager.h"
  28. #include "materials/materialDefinition.h"
  29. #include "console/consoleTypes.h"
  30. #include "math/util/matrixSet.h"
  31. IMPLEMENT_CONOBJECT(ForcedMaterialMeshMgr);
  32. ConsoleDocClass( ForcedMaterialMeshMgr,
  33. "@brief Basically the same as RenderMeshMgr, but will override the material "
  34. "of the instance. Exists for backwards compatibility, not currently used, soon to be deprecated\n\n"
  35. "@internal"
  36. );
  37. ForcedMaterialMeshMgr::ForcedMaterialMeshMgr()
  38. {
  39. mOverrideInstance = NULL;
  40. mOverrideMaterial = NULL;
  41. }
  42. ForcedMaterialMeshMgr::ForcedMaterialMeshMgr(RenderInstType riType, F32 renderOrder, F32 processAddOrder, BaseMatInstance* overrideMaterial)
  43. : RenderMeshMgr(riType, renderOrder, processAddOrder)
  44. {
  45. mOverrideInstance = overrideMaterial;
  46. mOverrideMaterial = NULL;
  47. }
  48. void ForcedMaterialMeshMgr::setOverrideMaterial(BaseMatInstance* overrideMaterial)
  49. {
  50. SAFE_DELETE(mOverrideInstance);
  51. mOverrideInstance = overrideMaterial;
  52. }
  53. ForcedMaterialMeshMgr::~ForcedMaterialMeshMgr()
  54. {
  55. setOverrideMaterial(NULL);
  56. }
  57. void ForcedMaterialMeshMgr::initPersistFields()
  58. {
  59. docsURL;
  60. addProtectedField("material", TYPEID< Material >(), Offset(mOverrideMaterial, ForcedMaterialMeshMgr),
  61. &_setOverrideMat, &_getOverrideMat, "Material used to draw all meshes in the render bin.");
  62. Parent::initPersistFields();
  63. }
  64. void ForcedMaterialMeshMgr::render(SceneRenderState * state)
  65. {
  66. PROFILE_SCOPE(ForcedMaterialMeshMgr_render);
  67. if(!mOverrideInstance && mOverrideMaterial.isValid())
  68. {
  69. mOverrideInstance = mOverrideMaterial->createMatInstance();
  70. mOverrideInstance->init( MATMGR->getDefaultFeatures(), getGFXVertexFormat<GFXVertexPNTBT>() );
  71. }
  72. // Early out if nothing to draw.
  73. if(!mElementList.size() || !mOverrideInstance)
  74. return;
  75. GFXDEBUGEVENT_SCOPE(ForcedMaterialMeshMgr_Render, ColorI::RED);
  76. // Automagically save & restore our viewport and transforms.
  77. GFXTransformSaver saver;
  78. // init loop data
  79. SceneData sgData;
  80. sgData.init( state );
  81. MeshRenderInst *ri = static_cast<MeshRenderInst*>(mElementList[0].inst);
  82. setupSGData( ri, sgData );
  83. while (mOverrideInstance->setupPass(state, sgData))
  84. {
  85. for( U32 j=0; j<mElementList.size(); j++)
  86. {
  87. MeshRenderInst* passRI = static_cast<MeshRenderInst*>(mElementList[j].inst);
  88. if(passRI->primBuff->getPointer()->mPrimitiveArray[passRI->primBuffIndex].numVertices < 1)
  89. continue;
  90. getRenderPass()->getMatrixSet().setWorld(*passRI->objectToWorld);
  91. getRenderPass()->getMatrixSet().setView(*passRI->worldToCamera);
  92. getRenderPass()->getMatrixSet().setProjection(*passRI->projection);
  93. mOverrideInstance->setTransforms(getRenderPass()->getMatrixSet(), state);
  94. mOverrideInstance->setBuffers(passRI->vertBuff, passRI->primBuff);
  95. GFX->drawPrimitive( passRI->primBuffIndex );
  96. }
  97. }
  98. }
  99. const char* ForcedMaterialMeshMgr::_getOverrideMat( void *object, const char *data )
  100. {
  101. ForcedMaterialMeshMgr &mgr = *reinterpret_cast<ForcedMaterialMeshMgr *>( object );
  102. if( mgr.mOverrideMaterial.isValid() )
  103. return mgr.mOverrideMaterial->getIdString();
  104. else
  105. return "0";
  106. }
  107. bool ForcedMaterialMeshMgr::_setOverrideMat( void *object, const char *index, const char *data )
  108. {
  109. ForcedMaterialMeshMgr &mgr = *reinterpret_cast<ForcedMaterialMeshMgr *>( object );
  110. Material* material;
  111. Sim::findObject(data, material);
  112. mgr.mOverrideMaterial = material;
  113. return false;
  114. }