S3DVertex.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #pragma once
  5. #include "vector3d.h"
  6. #include "vector2d.h"
  7. #include "SColor.h"
  8. namespace irr
  9. {
  10. namespace video
  11. {
  12. //! Enumeration for all vertex types there are.
  13. enum E_VERTEX_TYPE
  14. {
  15. //! Standard vertex type used by the Irrlicht engine, video::S3DVertex.
  16. EVT_STANDARD = 0,
  17. //! Vertex with two texture coordinates, video::S3DVertex2TCoords.
  18. /** Usually used for geometry with lightmaps or other special materials. */
  19. EVT_2TCOORDS,
  20. //! Vertex with a tangent and binormal vector, video::S3DVertexTangents.
  21. /** Usually used for tangent space normal mapping.
  22. Usually tangent and binormal get send to shaders as texture coordinate sets 1 and 2.
  23. */
  24. EVT_TANGENTS
  25. };
  26. //! Array holding the built in vertex type names
  27. const char *const sBuiltInVertexTypeNames[] = {
  28. "standard",
  29. "2tcoords",
  30. "tangents",
  31. 0,
  32. };
  33. //! standard vertex used by the Irrlicht engine.
  34. struct S3DVertex
  35. {
  36. //! default constructor
  37. constexpr S3DVertex() :
  38. Color(0xffffffff) {}
  39. //! constructor
  40. constexpr S3DVertex(f32 x, f32 y, f32 z, f32 nx, f32 ny, f32 nz, SColor c, f32 tu, f32 tv) :
  41. Pos(x, y, z), Normal(nx, ny, nz), Color(c), TCoords(tu, tv) {}
  42. //! constructor
  43. constexpr S3DVertex(const core::vector3df &pos, const core::vector3df &normal,
  44. SColor color, const core::vector2df &tcoords) :
  45. Pos(pos),
  46. Normal(normal), Color(color), TCoords(tcoords) {}
  47. //! Position
  48. core::vector3df Pos;
  49. //! Normal vector
  50. core::vector3df Normal;
  51. //! Color
  52. SColor Color;
  53. //! Texture coordinates
  54. core::vector2df TCoords;
  55. constexpr bool operator==(const S3DVertex &other) const
  56. {
  57. return ((Pos == other.Pos) && (Normal == other.Normal) &&
  58. (Color == other.Color) && (TCoords == other.TCoords));
  59. }
  60. constexpr bool operator!=(const S3DVertex &other) const
  61. {
  62. return ((Pos != other.Pos) || (Normal != other.Normal) ||
  63. (Color != other.Color) || (TCoords != other.TCoords));
  64. }
  65. constexpr bool operator<(const S3DVertex &other) const
  66. {
  67. return ((Pos < other.Pos) ||
  68. ((Pos == other.Pos) && (Normal < other.Normal)) ||
  69. ((Pos == other.Pos) && (Normal == other.Normal) && (Color < other.Color)) ||
  70. ((Pos == other.Pos) && (Normal == other.Normal) && (Color == other.Color) && (TCoords < other.TCoords)));
  71. }
  72. //! Get type of the class
  73. static E_VERTEX_TYPE getType()
  74. {
  75. return EVT_STANDARD;
  76. }
  77. //\param d d=0 returns other, d=1 returns this, values between interpolate.
  78. S3DVertex getInterpolated(const S3DVertex &other, f32 d)
  79. {
  80. d = core::clamp(d, 0.0f, 1.0f);
  81. return S3DVertex(Pos.getInterpolated(other.Pos, d),
  82. Normal.getInterpolated(other.Normal, d),
  83. Color.getInterpolated(other.Color, d),
  84. TCoords.getInterpolated(other.TCoords, d));
  85. }
  86. };
  87. //! Vertex with two texture coordinates.
  88. /** Usually used for geometry with lightmaps
  89. or other special materials.
  90. */
  91. struct S3DVertex2TCoords : public S3DVertex
  92. {
  93. //! default constructor
  94. constexpr S3DVertex2TCoords() :
  95. S3DVertex() {}
  96. //! constructor with two different texture coords, but no normal
  97. constexpr S3DVertex2TCoords(f32 x, f32 y, f32 z, SColor c, f32 tu, f32 tv, f32 tu2, f32 tv2) :
  98. S3DVertex(x, y, z, 0.0f, 0.0f, 0.0f, c, tu, tv), TCoords2(tu2, tv2) {}
  99. //! constructor with two different texture coords, but no normal
  100. constexpr S3DVertex2TCoords(const core::vector3df &pos, SColor color,
  101. const core::vector2df &tcoords, const core::vector2df &tcoords2) :
  102. S3DVertex(pos, core::vector3df(), color, tcoords),
  103. TCoords2(tcoords2) {}
  104. //! constructor with all values
  105. constexpr S3DVertex2TCoords(const core::vector3df &pos, const core::vector3df &normal, const SColor &color,
  106. const core::vector2df &tcoords, const core::vector2df &tcoords2) :
  107. S3DVertex(pos, normal, color, tcoords),
  108. TCoords2(tcoords2) {}
  109. //! constructor with all values
  110. constexpr S3DVertex2TCoords(f32 x, f32 y, f32 z, f32 nx, f32 ny, f32 nz,
  111. SColor c, f32 tu, f32 tv, f32 tu2, f32 tv2) :
  112. S3DVertex(x, y, z, nx, ny, nz, c, tu, tv),
  113. TCoords2(tu2, tv2) {}
  114. //! constructor with the same texture coords and normal
  115. constexpr S3DVertex2TCoords(f32 x, f32 y, f32 z, f32 nx, f32 ny, f32 nz,
  116. SColor c, f32 tu, f32 tv) :
  117. S3DVertex(x, y, z, nx, ny, nz, c, tu, tv),
  118. TCoords2(tu, tv) {}
  119. //! constructor with the same texture coords and normal
  120. constexpr S3DVertex2TCoords(const core::vector3df &pos, const core::vector3df &normal,
  121. SColor color, const core::vector2df &tcoords) :
  122. S3DVertex(pos, normal, color, tcoords),
  123. TCoords2(tcoords) {}
  124. //! constructor from S3DVertex
  125. constexpr S3DVertex2TCoords(const S3DVertex &o) :
  126. S3DVertex(o) {}
  127. //! Second set of texture coordinates
  128. core::vector2df TCoords2;
  129. //! Equality operator
  130. constexpr bool operator==(const S3DVertex2TCoords &other) const
  131. {
  132. return ((static_cast<S3DVertex>(*this) == static_cast<const S3DVertex &>(other)) &&
  133. (TCoords2 == other.TCoords2));
  134. }
  135. //! Inequality operator
  136. constexpr bool operator!=(const S3DVertex2TCoords &other) const
  137. {
  138. return ((static_cast<S3DVertex>(*this) != static_cast<const S3DVertex &>(other)) ||
  139. (TCoords2 != other.TCoords2));
  140. }
  141. constexpr bool operator<(const S3DVertex2TCoords &other) const
  142. {
  143. return ((static_cast<S3DVertex>(*this) < other) ||
  144. ((static_cast<S3DVertex>(*this) == static_cast<const S3DVertex &>(other)) && (TCoords2 < other.TCoords2)));
  145. }
  146. static E_VERTEX_TYPE getType()
  147. {
  148. return EVT_2TCOORDS;
  149. }
  150. //\param d d=0 returns other, d=1 returns this, values between interpolate.
  151. S3DVertex2TCoords getInterpolated(const S3DVertex2TCoords &other, f32 d)
  152. {
  153. d = core::clamp(d, 0.0f, 1.0f);
  154. return S3DVertex2TCoords(Pos.getInterpolated(other.Pos, d),
  155. Normal.getInterpolated(other.Normal, d),
  156. Color.getInterpolated(other.Color, d),
  157. TCoords.getInterpolated(other.TCoords, d),
  158. TCoords2.getInterpolated(other.TCoords2, d));
  159. }
  160. };
  161. //! Vertex with a tangent and binormal vector.
  162. /** Usually used for tangent space normal mapping.
  163. Usually tangent and binormal get send to shaders as texture coordinate sets 1 and 2.
  164. */
  165. struct S3DVertexTangents : public S3DVertex
  166. {
  167. //! default constructor
  168. S3DVertexTangents() :
  169. S3DVertex() {}
  170. //! constructor
  171. constexpr S3DVertexTangents(f32 x, f32 y, f32 z, f32 nx = 0.0f, f32 ny = 0.0f, f32 nz = 0.0f,
  172. SColor c = 0xFFFFFFFF, f32 tu = 0.0f, f32 tv = 0.0f,
  173. f32 tanx = 0.0f, f32 tany = 0.0f, f32 tanz = 0.0f,
  174. f32 bx = 0.0f, f32 by = 0.0f, f32 bz = 0.0f) :
  175. S3DVertex(x, y, z, nx, ny, nz, c, tu, tv),
  176. Tangent(tanx, tany, tanz), Binormal(bx, by, bz) {}
  177. //! constructor
  178. constexpr S3DVertexTangents(const core::vector3df &pos, SColor c,
  179. const core::vector2df &tcoords) :
  180. S3DVertex(pos, core::vector3df(), c, tcoords) {}
  181. //! constructor
  182. constexpr S3DVertexTangents(const core::vector3df &pos,
  183. const core::vector3df &normal, SColor c,
  184. const core::vector2df &tcoords,
  185. const core::vector3df &tangent = core::vector3df(),
  186. const core::vector3df &binormal = core::vector3df()) :
  187. S3DVertex(pos, normal, c, tcoords),
  188. Tangent(tangent), Binormal(binormal) {}
  189. //! constructor from S3DVertex
  190. constexpr S3DVertexTangents(const S3DVertex &o) :
  191. S3DVertex(o) {}
  192. //! Tangent vector along the x-axis of the texture
  193. core::vector3df Tangent;
  194. //! Binormal vector (tangent x normal)
  195. core::vector3df Binormal;
  196. constexpr bool operator==(const S3DVertexTangents &other) const
  197. {
  198. return ((static_cast<S3DVertex>(*this) == static_cast<const S3DVertex &>(other)) &&
  199. (Tangent == other.Tangent) &&
  200. (Binormal == other.Binormal));
  201. }
  202. constexpr bool operator!=(const S3DVertexTangents &other) const
  203. {
  204. return ((static_cast<S3DVertex>(*this) != static_cast<const S3DVertex &>(other)) ||
  205. (Tangent != other.Tangent) ||
  206. (Binormal != other.Binormal));
  207. }
  208. constexpr bool operator<(const S3DVertexTangents &other) const
  209. {
  210. return ((static_cast<S3DVertex>(*this) < other) ||
  211. ((static_cast<S3DVertex>(*this) == static_cast<const S3DVertex &>(other)) && (Tangent < other.Tangent)) ||
  212. ((static_cast<S3DVertex>(*this) == static_cast<const S3DVertex &>(other)) && (Tangent == other.Tangent) && (Binormal < other.Binormal)));
  213. }
  214. static E_VERTEX_TYPE getType()
  215. {
  216. return EVT_TANGENTS;
  217. }
  218. S3DVertexTangents getInterpolated(const S3DVertexTangents &other, f32 d)
  219. {
  220. d = core::clamp(d, 0.0f, 1.0f);
  221. return S3DVertexTangents(Pos.getInterpolated(other.Pos, d),
  222. Normal.getInterpolated(other.Normal, d),
  223. Color.getInterpolated(other.Color, d),
  224. TCoords.getInterpolated(other.TCoords, d),
  225. Tangent.getInterpolated(other.Tangent, d),
  226. Binormal.getInterpolated(other.Binormal, d));
  227. }
  228. };
  229. inline u32 getVertexPitchFromType(E_VERTEX_TYPE vertexType)
  230. {
  231. switch (vertexType) {
  232. case video::EVT_2TCOORDS:
  233. return sizeof(video::S3DVertex2TCoords);
  234. case video::EVT_TANGENTS:
  235. return sizeof(video::S3DVertexTangents);
  236. default:
  237. return sizeof(video::S3DVertex);
  238. }
  239. }
  240. } // end namespace video
  241. } // end namespace irr