QGearsManualObject.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <OgreColourValue.h>
  17. #include <OgreMesh.h>
  18. #include <OgreSubMesh.h>
  19. #include <Ogre.h>
  20. #include "common/TypeDefine.h"
  21. namespace QGears{
  22. /**
  23. * A manually handled object.
  24. *
  25. * When parsing files this is used to create the rendering engine object.
  26. * E.g. when a bone is found in the file being parsed, then bone() is
  27. * called to add it.
  28. */
  29. class ManualObject{
  30. public:
  31. /**
  32. * Constructor.
  33. *
  34. * @param mesh[in] The object mesh
  35. */
  36. explicit ManualObject(Ogre::Mesh *mesh);
  37. /**
  38. * Destructor.
  39. */
  40. virtual ~ManualObject();
  41. /**
  42. * Buffer indexes for object properties.
  43. */
  44. enum BufferBinding{
  45. /**
  46. * Object position.
  47. */
  48. BB_POSITION,
  49. /**
  50. * Object normal.
  51. */
  52. BB_NORMAL,
  53. /**
  54. * Object colour.
  55. */
  56. BB_COLOUR,
  57. /**
  58. * Object texture.
  59. */
  60. BB_TEXTURE,
  61. /**
  62. * Number of binds for the object.
  63. */
  64. BINDING_COUNT
  65. };
  66. /**
  67. * Begins creating a object section.
  68. *
  69. * It doesn't set position, normal or texture.
  70. *
  71. * @param name[in] Name for the object.
  72. * @param material_name[in] Name of the object's material.
  73. * @param vertex_count[in] Number of vertices in the object.
  74. * @param index_count[in]
  75. * @todo Undestand and document index_count.
  76. */
  77. virtual void begin(
  78. const String &name, const String &material_name,
  79. size_t vertex_count, size_t index_count
  80. );
  81. /**
  82. * Sets the object position.
  83. *
  84. * @param position[in] Position vector.
  85. * @todo It applies to the object or the current section?
  86. */
  87. virtual void position(const Ogre::Vector3 &position);
  88. /**
  89. * Sets the object normal vector.
  90. *
  91. * @param normal[in] Normal vector.
  92. * @todo It applies to the object or the current section?
  93. */
  94. virtual void normal(const Ogre::Vector3 &normal);
  95. /**
  96. * Sets the object colour.
  97. *
  98. * @param colour Object colour.
  99. * @todo It applies to the object or the current section?
  100. */
  101. virtual void colour(const Ogre::ColourValue &colour);
  102. /**
  103. * Sets the object texture coordinates.
  104. *
  105. * @param texture_coordinate[in] Coordinate vector.
  106. * @todo It applies to the object or the current section?
  107. */
  108. virtual void textureCoord(const Ogre::Vector2 &texture_coordinate);
  109. /**
  110. * Sets the object index.
  111. *
  112. * @param idx[in] Object index.
  113. * @todo It applies to the object or the current section?
  114. */
  115. virtual void index( const uint32 idx );
  116. /**
  117. * Adds a bone to the object.
  118. *
  119. * @param idx[in] Index of the vertex index to add the bone to.
  120. * @param bone_handle[in] Index of the bone objct.
  121. * @param weight[in] Bone weight. In most cases, 1 is ok.
  122. * @todo It applies to the object or the current section?
  123. */
  124. virtual void bone(
  125. const uint32 idx, const uint16 bone_handle,
  126. const Ogre::Real weight = 1
  127. );
  128. /**
  129. * End the current section.
  130. *
  131. * A section must have been started with {@see begin}
  132. *
  133. * @throws Ogre::Exception::ERR_INVALIDPARAMS if a section has not
  134. * been started.
  135. */
  136. virtual void end();
  137. protected:
  138. /**
  139. * Creates a buffer
  140. *
  141. * @param binding[in] Buffer binding.
  142. * @param type[in] Type Buffer type.
  143. * @param semantic[in] Buffer semantic.
  144. * @todo I'm no really sure what this function does...
  145. */
  146. template<typename BufferType>
  147. BufferType* createBuffer(
  148. const BufferBinding binding, Ogre::VertexElementType type,
  149. Ogre::VertexElementSemantic semantic
  150. );
  151. /**
  152. * Creates a position buffer in {@see BB_POSITION}.
  153. */
  154. virtual void createPositionBuffer();
  155. /**
  156. * Creates a normal buffer in {@see BB_NORMAL}.
  157. */
  158. virtual void createNormalBuffer();
  159. /**
  160. * Creates a colour buffer in {@see BB_COLOUR}.
  161. */
  162. virtual void createColourBuffer();
  163. /**
  164. * Creates a texture coordinate buffer in {@see BB_TEXTURE}.
  165. */
  166. virtual void createTextureCoordinateBuffer();
  167. /**
  168. * Creates a index buffer in {@see BB_INDEX}.
  169. */
  170. virtual void createIndexBuffer();
  171. /**
  172. * Resets all buffers in {@see BufferBinding}.
  173. */
  174. virtual void resetBuffers();
  175. /**
  176. * Resets any of the buffers in {@see BufferBinding}.
  177. *
  178. * @param buffer Buffer to reset.
  179. */
  180. template<typename BufferSharedPtr>
  181. void resetBuffer(BufferSharedPtr &buffer) const;
  182. private:
  183. typedef Ogre::HardwareVertexBufferSharedPtr VertexBuffer;
  184. typedef Ogre::HardwareIndexBufferSharedPtr IndexBuffer;
  185. /**
  186. * The object mesh.
  187. */
  188. Ogre::Mesh *_mesh;
  189. /**
  190. * The currently open section.
  191. */
  192. Ogre::SubMesh *_section;
  193. /**
  194. * @todo
  195. */
  196. Ogre::AxisAlignedBox _bbox;
  197. /**
  198. * @todo
  199. */
  200. Ogre::Real _radius;
  201. /**
  202. * The object vetex buffer.
  203. */
  204. VertexBuffer _vertex_buffers[BINDING_COUNT];
  205. /**
  206. * The index buffer.
  207. */
  208. IndexBuffer _indexbuffer_;
  209. /**
  210. * The position buffer.
  211. */
  212. Ogre::Vector3 *_position;
  213. /**
  214. * The normal buffer.
  215. */
  216. Ogre::Vector3 *_normal;
  217. /**
  218. * The colour buffer.
  219. */
  220. uint32 *colour_;
  221. /**
  222. * The texture buffer.
  223. */
  224. Ogre::Vector2 *_texture_coordinate;
  225. /**
  226. * The object index.
  227. */
  228. uint16 *_index;
  229. /**
  230. * The colour type.
  231. */
  232. const Ogre::VertexElementType colour_type_;
  233. };
  234. }