CCTrianglesCommand.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /****************************************************************************
  2. Copyright (c) 2013-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. ****************************************************************************/
  21. #ifndef __CC_TRIANGLES_COMMAND__
  22. #define __CC_TRIANGLES_COMMAND__
  23. #include "renderer/CCRenderCommand.h"
  24. #include "renderer/CCGLProgramState.h"
  25. /**
  26. * @addtogroup renderer
  27. * @{
  28. */
  29. NS_CC_BEGIN
  30. /**
  31. Command used to render one or more Triangles, which is similar to QuadCommand.
  32. Every TrianglesCommand will have generate material ID by give textureID, glProgramState, Blend function
  33. if the material id is the same, these TrianglesCommands could be batched to save draw call.
  34. */
  35. class CC_DLL TrianglesCommand : public RenderCommand
  36. {
  37. public:
  38. /**The structure of Triangles. */
  39. struct Triangles
  40. {
  41. /**Vertex data pointer.*/
  42. V3F_C4B_T2F* verts;
  43. /**Index data pointer.*/
  44. unsigned short* indices;
  45. /**The number of vertices.*/
  46. int vertCount;
  47. /**The number of indices.*/
  48. int indexCount;
  49. };
  50. /**Constructor.*/
  51. TrianglesCommand();
  52. /**Destructor.*/
  53. ~TrianglesCommand();
  54. /** Initializes the command.
  55. @param globalOrder GlobalZOrder of the command.
  56. @param textureID The openGL handle of the used texture.
  57. @param glProgramState The specified glProgram and its uniform.
  58. @param blendType Blend function for the command.
  59. @param triangles Rendered triangles for the command.
  60. @param mv ModelView matrix for the command.
  61. @param flags to indicate that the command is using 3D rendering or not.
  62. */
  63. void init(float globalOrder, GLuint textureID, GLProgramState* glProgramState, BlendFunc blendType, const Triangles& triangles,const Mat4& mv, uint32_t flags);
  64. /**Deprecated function, the params is similar as the upper init function, with flags equals 0.*/
  65. CC_DEPRECATED_ATTRIBUTE void init(float globalOrder, GLuint textureID, GLProgramState* glProgramState, BlendFunc blendType, const Triangles& triangles,const Mat4& mv);
  66. void init(float globalOrder, Texture2D* textureID, GLProgramState* glProgramState, BlendFunc blendType, const Triangles& triangles, const Mat4& mv, uint32_t flags);
  67. /**Apply the texture, shaders, programs, blend functions to GPU pipeline.*/
  68. void useMaterial() const;
  69. /**Get the material id of command.*/
  70. uint32_t getMaterialID() const { return _materialID; }
  71. /**Get the openGL texture handle.*/
  72. GLuint getTextureID() const { return _textureID; }
  73. /**Get a const reference of triangles.*/
  74. const Triangles& getTriangles() const { return _triangles; }
  75. /**Get the vertex count in the triangles.*/
  76. ssize_t getVertexCount() const { return _triangles.vertCount; }
  77. /**Get the index count of the triangles.*/
  78. ssize_t getIndexCount() const { return _triangles.indexCount; }
  79. /**Get the vertex data pointer.*/
  80. const V3F_C4B_T2F* getVertices() const { return _triangles.verts; }
  81. /**Get the index data pointer.*/
  82. const unsigned short* getIndices() const { return _triangles.indices; }
  83. /**Get the glprogramstate.*/
  84. GLProgramState* getGLProgramState() const { return _glProgramState; }
  85. /**Get the blend function.*/
  86. BlendFunc getBlendType() const { return _blendType; }
  87. /**Get the model view matrix.*/
  88. const Mat4& getModelView() const { return _mv; }
  89. protected:
  90. /**Generate the material ID by textureID, glProgramState, and blend function.*/
  91. void generateMaterialID();
  92. /**Generated material id.*/
  93. uint32_t _materialID;
  94. /**OpenGL handle for texture.*/
  95. GLuint _textureID;
  96. /**GLprogramstate for the command. encapsulate shaders and uniforms.*/
  97. GLProgramState* _glProgramState;
  98. /**Blend function when rendering the triangles.*/
  99. BlendFunc _blendType;
  100. /**Rendered triangles.*/
  101. Triangles _triangles;
  102. /**Model view matrix when rendering the triangles.*/
  103. Mat4 _mv;
  104. GLuint _alphaTextureID; // ANDROID ETC1 ALPHA supports.
  105. };
  106. NS_CC_END
  107. /**
  108. end of support group
  109. @}
  110. */
  111. #endif // defined(__CC_TRIANGLES_COMMAND__)