CCTechnique.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /****************************************************************************
  2. Copyright (c) 2015-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. Ideas taken from:
  21. - GamePlay3D: http://gameplay3d.org/
  22. - OGRE3D: http://www.ogre3d.org/
  23. - Qt3D: http://qt-project.org/
  24. ****************************************************************************/
  25. #include "renderer/CCTechnique.h"
  26. #include "renderer/CCGLProgramState.h"
  27. #include "renderer/CCMaterial.h"
  28. #include "renderer/CCPass.h"
  29. NS_CC_BEGIN
  30. Technique* Technique::createWithGLProgramState(Material* parent, GLProgramState* state)
  31. {
  32. auto technique = new (std::nothrow) Technique();
  33. if (technique && technique->init(parent))
  34. {
  35. auto pass = Pass::createWithGLProgramState(technique, state);
  36. technique->addPass(pass);
  37. technique->autorelease();
  38. return technique;
  39. }
  40. return nullptr;
  41. }
  42. Technique* Technique::create(Material* material)
  43. {
  44. auto technique = new (std::nothrow) Technique();
  45. if (technique && technique->init(material))
  46. {
  47. technique->autorelease();
  48. return technique;
  49. }
  50. return nullptr;
  51. }
  52. Technique::Technique()
  53. : _name("")
  54. {
  55. }
  56. Technique::~Technique()
  57. {
  58. }
  59. bool Technique::init(Material* parent)
  60. {
  61. _parent = parent;
  62. return true;
  63. }
  64. Technique* Technique::clone() const
  65. {
  66. auto technique = new (std::nothrow) Technique();
  67. if (technique)
  68. {
  69. technique->_name = _name;
  70. RenderState::cloneInto(technique);
  71. for (const auto pass: _passes)
  72. {
  73. auto p = pass->clone();
  74. p->_parent = technique;
  75. technique->_passes.pushBack(p);
  76. }
  77. technique->autorelease();
  78. }
  79. return technique;
  80. }
  81. void Technique::addPass(Pass *pass)
  82. {
  83. _passes.pushBack(pass);
  84. }
  85. std::string Technique::getName() const
  86. {
  87. return _name;
  88. }
  89. void Technique::setName(const std::string &name)
  90. {
  91. _name = name;
  92. }
  93. Pass* Technique::getPassByIndex(ssize_t index) const
  94. {
  95. CC_ASSERT(index>=0 && index<_passes.size() && "Invalid index");
  96. return _passes.at(index);
  97. }
  98. ssize_t Technique::getPassCount() const
  99. {
  100. return _passes.size();
  101. }
  102. const Vector<Pass*>& Technique::getPasses() const
  103. {
  104. return _passes;
  105. }
  106. NS_CC_END