CCVertexAttribBinding.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. Copyright 2013 BlackBerry Inc.
  3. Copyright (c) 2015-2017 Chukong Technologies
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. Original file from GamePlay3D: http://gameplay3d.org
  15. This file was modified to fit the cocos2d-x project
  16. */
  17. #ifndef CC_VERTEXATTRIBUTEBINDING_H_
  18. #define CC_VERTEXATTRIBUTEBINDING_H_
  19. #include <unordered_map>
  20. #include "base/CCRef.h"
  21. #include "renderer/CCGLProgramState.h"
  22. NS_CC_BEGIN
  23. class MeshIndexData;
  24. class VertexAttribValue;
  25. /**
  26. * Defines a binding between the vertex layout of a Mesh and the vertex
  27. * input attributes of a vertex shader (Effect).
  28. *
  29. * In a perfect world, this class would always be a binding directly between
  30. * a unique VertexFormat and an Effect, where the VertexFormat is simply the
  31. * definition of the layout of any anonymous vertex buffer. However, the OpenGL
  32. * mechanism for setting up these bindings is Vertex Array Objects (VAOs).
  33. * OpenGL requires a separate VAO per vertex buffer object (VBO), rather than per
  34. * vertex layout definition. Therefore, although we would like to define this
  35. * binding between a VertexFormat and Effect, we are specifying the binding
  36. * between a Mesh and Effect to satisfy the OpenGL requirement of one VAO per VBO.
  37. *
  38. * Note that this class still does provide a binding between a VertexFormat
  39. * and an Effect, however this binding is actually a client-side binding and
  40. * should only be used when writing custom code that use client-side vertex
  41. * arrays, since it is slower than the server-side VAOs used by OpenGL
  42. * (when creating a VertexAttribBinding between a Mesh and Effect).
  43. */
  44. class CC_DLL VertexAttribBinding : public Ref
  45. {
  46. public:
  47. /**
  48. * Creates a new VertexAttribBinding between the given MeshVertexData and GLProgramState.
  49. *
  50. * If a VertexAttribBinding matching the specified MeshVertexData and GLProgramState already
  51. * exists, it will be returned. Otherwise, a new VertexAttribBinding will
  52. * be returned. If OpenGL VAOs are enabled, the a new VAO will be created and
  53. * stored in the returned VertexAttribBinding, otherwise a client-side
  54. * array of vertex attribute bindings will be stored.
  55. *
  56. * @param mesh The mesh.
  57. * @param effect The effect.
  58. *
  59. * @return A VertexAttribBinding for the requested parameters.
  60. */
  61. static VertexAttribBinding* create(MeshIndexData* meshIndexData, GLProgramState* glProgramState);
  62. /**
  63. * Binds this vertex array object.
  64. */
  65. void bind();
  66. /**
  67. * Unbinds this vertex array object.
  68. */
  69. void unbind();
  70. /**
  71. * Returns the vertex attrib flags
  72. */
  73. uint32_t getVertexAttribsFlags() const;
  74. private:
  75. bool init(MeshIndexData* meshIndexData, GLProgramState* glProgramState);
  76. /**
  77. * Constructor.
  78. */
  79. VertexAttribBinding();
  80. /**
  81. * Destructor.
  82. */
  83. ~VertexAttribBinding();
  84. /**
  85. * Hidden copy assignment operator.
  86. */
  87. VertexAttribBinding& operator=(const VertexAttribBinding&);
  88. void setVertexAttribPointer(const std::string& name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid* pointer);
  89. VertexAttribValue* getVertexAttribValue(const std::string &name);
  90. void parseAttributes();
  91. GLuint _handle;
  92. MeshIndexData* _meshIndexData;
  93. GLProgramState* _glProgramState;
  94. std::unordered_map<std::string, VertexAttribValue> _attributes;
  95. uint32_t _vertexAttribsFlags;
  96. };
  97. /// @cond
  98. extern std::string CC_DLL s_attributeNames[];//attribute names array
  99. /// @endcond
  100. NS_CC_END
  101. #endif // CC_VERTEXATTRIBUTEBINDING_H_