CmMaterial.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "CmMaterial.h"
  2. #include "CmException.h"
  3. #include "CmShader.h"
  4. #include "CmTechnique.h"
  5. #include "CmPass.h"
  6. #include "CmRenderSystemManager.h"
  7. #include "CmRenderSystem.h"
  8. #include "CmGpuProgramParams.h"
  9. #include "CmGpuProgram.h"
  10. #include "CmMaterialRTTI.h"
  11. namespace CamelotEngine
  12. {
  13. Material::Material()
  14. {
  15. // Material doesn't do anything render thread specific, so we can just initialize right away
  16. initialize_internal();
  17. }
  18. void Material::initialize_internal()
  19. {
  20. Resource::initialize_internal();
  21. }
  22. void Material::setShader(ShaderPtr shader)
  23. {
  24. mShader = shader;
  25. initBestTechnique();
  26. }
  27. void Material::initBestTechnique()
  28. {
  29. mBestTechnique = nullptr;
  30. mParameters.clear();
  31. if(mShader)
  32. {
  33. mBestTechnique = mShader->getBestTechnique();
  34. if(mBestTechnique)
  35. {
  36. for(UINT32 i = 0; i < mBestTechnique->getNumPasses(); i++)
  37. {
  38. PassPtr curPass = mBestTechnique->getPass(i);
  39. PassParametersPtr params = PassParametersPtr(new PassParameters());
  40. GpuProgramHandle vertProgram = curPass->getVertexProgram();
  41. if(vertProgram)
  42. {
  43. vertProgram.waitUntilLoaded();
  44. params->mVertParams = vertProgram->createParameters();
  45. }
  46. GpuProgramHandle fragProgram = curPass->getFragmentProgram();
  47. if(fragProgram)
  48. {
  49. fragProgram.waitUntilLoaded();
  50. params->mFragParams = fragProgram->createParameters();
  51. }
  52. GpuProgramHandle geomProgram = curPass->getGeometryProgram();
  53. if(geomProgram)
  54. {
  55. geomProgram.waitUntilLoaded();
  56. params->mGeomParams = geomProgram->createParameters();
  57. }
  58. mParameters.push_back(params);
  59. }
  60. }
  61. }
  62. }
  63. void Material::throwIfNotInitialized() const
  64. {
  65. if(mShader == nullptr)
  66. {
  67. CM_EXCEPT(InternalErrorException, "Material does not have shader set.");
  68. }
  69. if(mBestTechnique == nullptr)
  70. {
  71. CM_EXCEPT(InternalErrorException, "Shader does not contain a supported technique.");
  72. }
  73. }
  74. void Material::setTexture(const String& name, TextureHandle& value)
  75. {
  76. throwIfNotInitialized();
  77. setParam(name, value);
  78. }
  79. void Material::setFloat(const String& name, float value)
  80. {
  81. throwIfNotInitialized();
  82. setParam(name, value);
  83. }
  84. void Material::setColor(const String& name, const Color& value)
  85. {
  86. throwIfNotInitialized();
  87. setParam(name, value);
  88. }
  89. void Material::setVec2(const String& name, const Vector2& value)
  90. {
  91. throwIfNotInitialized();
  92. setParam(name, value);
  93. }
  94. void Material::setVec3(const String& name, const Vector3& value)
  95. {
  96. throwIfNotInitialized();
  97. setParam(name, value);
  98. }
  99. void Material::setVec4(const String& name, const Vector4& value)
  100. {
  101. throwIfNotInitialized();
  102. setParam(name, value);
  103. }
  104. void Material::setMat3(const String& name, const Matrix3& value)
  105. {
  106. throwIfNotInitialized();
  107. setParam(name, value);
  108. }
  109. void Material::setMat4(const String& name, const Matrix4& value)
  110. {
  111. throwIfNotInitialized();
  112. setParam(name, value);
  113. }
  114. UINT32 Material::getNumPasses() const
  115. {
  116. throwIfNotInitialized();
  117. return mShader->getBestTechnique()->getNumPasses();
  118. }
  119. PassPtr Material::getPass(UINT32 passIdx) const
  120. {
  121. if(passIdx < 0 || passIdx >= mShader->getBestTechnique()->getNumPasses())
  122. CM_EXCEPT(InvalidParametersException, "Invalid pass index.");
  123. return mShader->getBestTechnique()->getPass(passIdx);
  124. }
  125. PassParametersPtr Material::getPassParameters(UINT32 passIdx) const
  126. {
  127. if(passIdx < 0 || passIdx >= mParameters.size())
  128. CM_EXCEPT(InvalidParametersException, "Invalid pass index.");
  129. return mParameters[passIdx];
  130. }
  131. RTTITypeBase* Material::getRTTIStatic()
  132. {
  133. return MaterialRTTI::instance();
  134. }
  135. RTTITypeBase* Material::getRTTI() const
  136. {
  137. return Material::getRTTIStatic();
  138. }
  139. }