CCVertexIndexData.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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_VERTEX_INDEX_DATA_H__
  22. #define __CC_VERTEX_INDEX_DATA_H__
  23. #include "base/CCRef.h"
  24. #include <map>
  25. /**
  26. * @addtogroup renderer
  27. * @{
  28. */
  29. NS_CC_BEGIN
  30. class VertexBuffer;
  31. /**
  32. VertexStreamAttribute is used to specify the vertex attribute for drawing, which is correspondent to
  33. glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid* ptr).
  34. _semantic -> index
  35. _size -> size
  36. _type -> type
  37. _normalize -> normalized
  38. _offset is used to compute the start offset in a interleaved array, take a V3F_C4B_T2F array,
  39. offset of vertex will be 0, offset of color would be 0 + sizeof(float) * 3 = 12,
  40. offset of texture coord would be 12 + sizeof(char) * 4 = 16.
  41. @js NA
  42. */
  43. struct CC_DLL VertexStreamAttribute
  44. {
  45. /**
  46. Constructor.
  47. */
  48. VertexStreamAttribute()
  49. : _normalize(false),_offset(0),_semantic(0),_type(0),_size(0)
  50. {
  51. }
  52. /**
  53. Constructor
  54. @param offset The offset of the attribute.
  55. @param semantic The semantic (Position, Texcoord, Color etc) of attribute.
  56. @param type The type of attribute, could be GL_FLOAT, GL_UNSIGNED_BYTE etc.
  57. @param size Describe how many elements of type in the attribute.
  58. */
  59. VertexStreamAttribute(int offset, int semantic, int type, int size)
  60. : _normalize(false),_offset(offset),_semantic(semantic),_type(type),_size(size)
  61. {
  62. }
  63. /**
  64. Constructor
  65. @param offset The offset of the attribute.
  66. @param semantic The semantic (Position, Texcoord, Color etc) of attribute.
  67. @param type The type of attribute, could be GL_FLOAT, GL_UNSIGNED_BYTE etc.
  68. @param size Describe how many elements of type in the attribute.
  69. @param normalize If true, the data will be normalized by dividing 255.
  70. */
  71. VertexStreamAttribute(int offset, int semantic, int type, int size, bool normalize)
  72. : _normalize(normalize),_offset(offset),_semantic(semantic),_type(type),_size(size)
  73. {
  74. }
  75. /**
  76. Whether the attribute should be normalized or not.
  77. */
  78. bool _normalize;
  79. /**
  80. The offset of the attribute in the buffer.
  81. */
  82. int _offset;
  83. /**
  84. Describe that the attribute usage, could be Position, Color etc.
  85. */
  86. int _semantic;
  87. /**
  88. Describe the type of attribute, could be GL_FLOAT, GL_UNSIGNED_BYTE etc.
  89. */
  90. int _type;
  91. /**
  92. Describe how many elements of type in the attribute.
  93. */
  94. int _size;
  95. };
  96. /**
  97. VertexData is a class used for specify input streams for GPU rendering pipeline,
  98. a VertexData will be composed by several streams, every stream will contain a VertexStreamAttribute
  99. and the binding VertexBuffer. Streams will be identified by semantic.
  100. @js NA
  101. */
  102. class CC_DLL VertexData : public Ref
  103. {
  104. public:
  105. /**
  106. Create function, used to create a instance of VertexData.
  107. */
  108. static VertexData* create();
  109. /**
  110. Get the number of streams in the VertexData.
  111. */
  112. size_t getVertexStreamCount() const;
  113. /**
  114. Set a stream to VertexData,given that stream is identified by semantic, so if the semantic is not
  115. specified before, it will add a stream, or it will override the old one.
  116. @param buffer The binding buffer of the stream.
  117. @param stream The binding vertex attribute, its member semantic will be used as the identifier.
  118. */
  119. bool setStream(VertexBuffer* buffer, const VertexStreamAttribute& stream);
  120. /**
  121. Remove the given streams.
  122. @param semantic The semantic of the stream.
  123. */
  124. void removeStream(int semantic);
  125. /**
  126. Get the attribute of stream, const version.
  127. @param semantic The semantic of the stream.
  128. */
  129. const VertexStreamAttribute* getStreamAttribute(int semantic) const;
  130. /**
  131. Get the attribute of stream.
  132. @param semantic The semantic of the stream.
  133. */
  134. VertexStreamAttribute* getStreamAttribute(int semantic);
  135. /**
  136. Get the binded buffer of the stream.
  137. @param semantic The semantic of the stream.
  138. */
  139. VertexBuffer* getStreamBuffer(int semantic) const;
  140. /**
  141. Called for rendering, it will bind the state of vertex data to current rendering pipeline.
  142. */
  143. void use();
  144. protected:
  145. /**
  146. Constructor.
  147. */
  148. VertexData();
  149. /**
  150. Destructor.
  151. */
  152. virtual ~VertexData();
  153. protected:
  154. /**
  155. Simple struct to bundle buffer and attribute.
  156. */
  157. struct BufferAttribute
  158. {
  159. VertexBuffer* _buffer;
  160. VertexStreamAttribute _stream;
  161. };
  162. /**
  163. Streams in the VertexData.
  164. */
  165. std::map<int, BufferAttribute> _vertexStreams;
  166. };
  167. NS_CC_END
  168. /**
  169. end of support group
  170. @}
  171. */
  172. #endif //__CC_VERTEX_INDEX_DATA_H__