CmMaterial.cpp 3.8 KB

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