CCMaterial.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #ifndef __cocos2d_libs__CCMaterial__
  26. #define __cocos2d_libs__CCMaterial__
  27. #include <string>
  28. #include "renderer/CCRenderState.h"
  29. #include "renderer/CCTechnique.h"
  30. #include "base/CCRef.h"
  31. #include "base/CCVector.h"
  32. #include "math/Vec2.h"
  33. #include "math/Vec3.h"
  34. #include "math/Vec4.h"
  35. #include "math/Mat4.h"
  36. #include "platform/CCPlatformMacros.h"
  37. NS_CC_BEGIN
  38. class Technique;
  39. class Pass;
  40. class GLProgramState;
  41. class Node;
  42. class Properties;
  43. /// Material
  44. class CC_DLL Material : public RenderState
  45. {
  46. friend class Node;
  47. friend class Technique;
  48. friend class Pass;
  49. friend class MeshCommand;
  50. friend class Renderer;
  51. friend class Mesh;
  52. public:
  53. /**
  54. * Creates a Material using the data from the Properties object defined at the specified URL,
  55. * where the URL is of the format "<file-path>.<extension>#<namespace-id>/<namespace-id>/.../<namespace-id>"
  56. * (and "#<namespace-id>/<namespace-id>/.../<namespace-id>" is optional).
  57. *
  58. * @param url The URL pointing to the Properties object defining the material.
  59. *
  60. * @return A new Material or NULL if there was an error.
  61. */
  62. static Material* createWithFilename(const std::string& path);
  63. /** Creates a Material with a GLProgramState.
  64. It will only contain one Technique and one Pass.
  65. Added in order to support legacy code.
  66. */
  67. static Material* createWithGLStateProgram(GLProgramState* programState);
  68. /**
  69. * Creates a material from the specified properties object.
  70. *
  71. * @param materialProperties The properties object defining the
  72. * material (must have namespace equal to 'material').
  73. * @return A new Material.
  74. */
  75. static Material* createWithProperties(Properties* materialProperties);
  76. /// returns the material name
  77. std::string getName() const;
  78. /// sets the material name
  79. void setName(const std::string& name);
  80. /** Returns a Technique by its name.
  81. returns `nullptr` if the Technique can't be found.
  82. */
  83. Technique* getTechniqueByName(const std::string& name);
  84. /** Returns a Technique by index.
  85. returns `nullptr` if the index is invalid.
  86. */
  87. Technique* getTechniqueByIndex(ssize_t index);
  88. /** Returns the Technique used by the Material */
  89. Technique* getTechnique() const;
  90. /** Returns the list of Techniques */
  91. const Vector<Technique*>& getTechniques() const;
  92. /** Returns the number of Techniques in the Material. */
  93. ssize_t getTechniqueCount() const;
  94. /** Adds a Technique into the Material */
  95. void addTechnique(Technique* technique);
  96. /** Sets the current technique */
  97. void setTechnique(const std::string& techniqueName);
  98. /** returns a clone (deep-copy) of the material */
  99. virtual Material* clone() const;
  100. protected:
  101. Material();
  102. ~Material();
  103. bool initWithGLProgramState(GLProgramState* state);
  104. bool initWithFile(const std::string& file);
  105. bool initWithProperties(Properties* materialProperties);
  106. void setTarget(Node* target);
  107. bool parseProperties(Properties* properties);
  108. bool parseTechnique(Properties* properties);
  109. bool parsePass(Technique* technique, Properties* properties);
  110. bool parseShader(Pass* pass, Properties* properties);
  111. bool parseSampler(GLProgramState* glProgramState, Properties* properties);
  112. bool parseUniform(GLProgramState* programState, Properties* properties, const char* uniformName);
  113. bool parseRenderState(RenderState* renderState, Properties* properties);
  114. // material name
  115. std::string _name;
  116. // array of techniques
  117. Vector<Technique*> _techniques;
  118. // weak pointer since it is being help by _techniques
  119. Technique* _currentTechnique;
  120. // weak reference
  121. Node* _target;
  122. };
  123. NS_CC_END
  124. #endif /* defined(__cocos2d_libs__CCMaterial__) */