PolySceneMesh.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyEntity.h"
  22. #include "PolyMesh.h"
  23. #include "PolyShader.h"
  24. #include "PolyRenderDataArray.h"
  25. namespace Polycode {
  26. class Material;
  27. class Mesh;
  28. class Texture;
  29. class Skeleton;
  30. class Image;
  31. class ResourcePool;
  32. /**
  33. * 3D polygonal mesh instance. The SceneMesh is the base for all polygonal 3d geometry. It can have simple textures or complex materials applied to it.
  34. */
  35. class _PolyExport SceneMesh : public Entity {
  36. public:
  37. /**
  38. * Construct a scene mesh from a mesh file.
  39. * @param fileName Path to mesh file to load.
  40. */
  41. explicit SceneMesh(const String& fileName);
  42. /**
  43. * Construct an empty scene mesh with the specified type.
  44. * @param meshType Mesh type to create. See Mesh for possible values.
  45. */
  46. explicit SceneMesh(int meshType);
  47. /**
  48. * Construct scene mesh from an existing Mesh instance.
  49. */
  50. explicit SceneMesh(Mesh *mesh);
  51. /**
  52. * Construct scene mesh from an existing Mesh instance.
  53. */
  54. static SceneMesh *SceneMeshFromMesh(Mesh *mesh);
  55. /**
  56. * Construct an empty scene mesh with the specified type.
  57. * @param meshType Mesh type to create. See Mesh for possible values.
  58. */
  59. static SceneMesh *SceneMeshWithType(int meshType);
  60. virtual ~SceneMesh();
  61. void Render();
  62. /**
  63. * Returns the local material binding options for this mesh.
  64. */
  65. ShaderBinding *getLocalShaderOptions();
  66. /**
  67. * Returns the Mesh instance of the actual mesh.
  68. */
  69. Mesh *getMesh();
  70. /**
  71. * Returns the texture applied.
  72. */
  73. Texture *getTexture() const;
  74. /**
  75. * Returns the material applied.
  76. */
  77. Material *getMaterial();
  78. /**
  79. * Loads a simple texture from a file name and applies it to the mesh.
  80. * @param fileName Filename to load the mesh from.
  81. * @param clamp If true, clamps the texture to edges. See Texture for details on that.
  82. */
  83. void loadTexture(const String& fileName);
  84. void loadTextureFromImage(Image *image);
  85. /**
  86. * Loads a skeleton from a file and applies it to the scene mesh.
  87. * @param fileName Filename to load the skeleton from.
  88. */
  89. Skeleton *loadSkeleton(const String& fileName);
  90. /**
  91. * Sets the texture from an existing Texture instance.
  92. * @param texture Texture to set.
  93. */
  94. void setTexture(Texture *texture);
  95. /**
  96. * Clears the currently applied material
  97. */
  98. void clearMaterial();
  99. /**
  100. * Set material from existing Material instance.
  101. * @param material Material to apply.
  102. */
  103. void setMaterial(Material *material);
  104. /**
  105. * Set material by name. You can create materials in material files and name them there, then use this to set a material by name to a scene mesh.
  106. * @param materialName Name of material to apply.
  107. */
  108. void setMaterialByName(const String& materialName, ResourcePool *resourcePool = NULL);
  109. /**
  110. * Set the mesh this scene mesh renders.
  111. * @param mesh Set a new mesh to render.
  112. */
  113. void setMesh(Mesh *mesh);
  114. /**
  115. * Sets a skeleton from an existing skeleton instance.
  116. * @param skeleton Skeleton to set to this mesh.
  117. */
  118. void setSkeleton(Skeleton *skeleton);
  119. /**
  120. * Returns the skeleton applied to this scene mesh.
  121. */
  122. Skeleton *getSkeleton();
  123. void renderMeshLocally();
  124. /**
  125. * If this is set to true, the mesh will be cached to a hardware vertex buffer if those are available. This can dramatically speed up rendering.
  126. */
  127. void cacheToVertexBuffer(bool cache);
  128. /**
  129. * Sets the line width for line-based meshes.
  130. */
  131. void setLineWidth(Number newWidth);
  132. /**
  133. * If this mesh was loaded form file, returns the filename of the loaded mesh.
  134. */
  135. String getFilename();
  136. /**
  137. * Sets the filename path of the mesh.
  138. */
  139. void setFilename(String fileName);
  140. /**
  141. * Loads mesh from file. Deletes current mesh if ownsMesh is set to true.
  142. */
  143. void loadFromFile(String fileName);
  144. /**
  145. * Line width for line-based meshes.
  146. */
  147. Number lineWidth;
  148. /**
  149. * If set to true, will antialias the lines in a line-based mesh. Defaults to false.
  150. */
  151. bool lineSmooth;
  152. /**
  153. * Point size for point-based meshes.
  154. */
  155. Number pointSize;
  156. /**
  157. * If setto true, will antialias points in a point-based mesh. Defaults to false.
  158. */
  159. bool pointSmooth;
  160. /**
  161. * If true, will delete its Mesh upon destruction or mesh loading. (defaults to true)
  162. */
  163. bool ownsMesh;
  164. /**
  165. * If true, will delete its Skeleton upon destruction. (defaults to true)
  166. */
  167. bool ownsSkeleton;
  168. /**
  169. * If set to true, will render the mesh wireframe on top of the mesh using wireFrameColor.
  170. * @see wireFrameColor
  171. */
  172. bool overlayWireframe;
  173. /*
  174. * If overlayWireframe is set to true, defines the color of the mesh wireframe.
  175. */
  176. Color wireFrameColor;
  177. /**
  178. * If set to true, will check against actual geometry polygons on ray hit detection. Defaults to false.
  179. */
  180. bool useGeometryHitDetection;
  181. bool customHitDetection(const Ray &ray);
  182. /**
  183. * The Renderer has an ability to set an override material that is set for all rendered entities. If forceMaterial is set to true, this entity will always use its assigned material, even if an override material is set.
  184. */
  185. bool forceMaterial;
  186. virtual Entity *Clone(bool deepClone, bool ignoreEditorOnly) const;
  187. virtual void applyClone(Entity *clone, bool deepClone, bool ignoreEditorOnly) const;
  188. /**
  189. * Normally, translucent textures do not affect the depth buffer, but if this flag is set to true, this entity's alpha channel is written to the depth buffer at a preset threshold. This flag is set to false by default.
  190. */
  191. bool alphaTest;
  192. /**
  193. * If this flag is set to false, backface culling is disabled when rendering this entity, rendering both sides of each face. Set to true by default.
  194. */
  195. bool backfaceCulled;
  196. bool sendBoneMatricesToMaterial;
  197. protected:
  198. bool useVertexBuffer;
  199. VertexBuffer *vertexBuffer;
  200. Mesh *mesh;
  201. Texture *texture;
  202. Material *material;
  203. Skeleton *skeleton;
  204. ShaderBinding *localShaderOptions;
  205. String fileName;
  206. std::vector<Matrix4> materialBoneMatrices;
  207. VertexDataArray skeletalVertexPositions;
  208. VertexDataArray skeletalVertexNormals;
  209. };
  210. }