S3DVertex.h 8.5 KB

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