S3DVertex.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. EVT_TANGENTS
  24. };
  25. //! Array holding the built in vertex type names
  26. const char* const sBuiltInVertexTypeNames[] =
  27. {
  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. S3DVertex() {}
  38. //! constructor
  39. S3DVertex(f32 x, f32 y, f32 z, f32 nx, f32 ny, f32 nz, SColor c, f32 tu, f32 tv)
  40. : Pos(x,y,z), Normal(nx,ny,nz), Color(c), TCoords(tu,tv) {}
  41. //! constructor
  42. S3DVertex(const core::vector3df& pos, const core::vector3df& normal,
  43. SColor color, const core::vector2d<f32>& tcoords)
  44. : Pos(pos), Normal(normal), Color(color), TCoords(tcoords) {}
  45. //! Position
  46. core::vector3df Pos;
  47. //! Normal vector
  48. core::vector3df Normal;
  49. //! Color
  50. SColor Color;
  51. //! Texture coordinates
  52. core::vector2d<f32> TCoords;
  53. bool operator==(const S3DVertex& other) const
  54. {
  55. return ((Pos == other.Pos) && (Normal == other.Normal) &&
  56. (Color == other.Color) && (TCoords == other.TCoords));
  57. }
  58. bool operator!=(const S3DVertex& other) const
  59. {
  60. return ((Pos != other.Pos) || (Normal != other.Normal) ||
  61. (Color != other.Color) || (TCoords != other.TCoords));
  62. }
  63. bool operator<(const S3DVertex& other) const
  64. {
  65. return ((Pos < other.Pos) ||
  66. ((Pos == other.Pos) && (Normal < other.Normal)) ||
  67. ((Pos == other.Pos) && (Normal == other.Normal) && (Color < other.Color)) ||
  68. ((Pos == other.Pos) && (Normal == other.Normal) && (Color == other.Color) && (TCoords < other.TCoords)));
  69. }
  70. E_VERTEX_TYPE getType() const
  71. {
  72. return EVT_STANDARD;
  73. }
  74. S3DVertex getInterpolated(const S3DVertex& other, f32 d)
  75. {
  76. d = core::clamp(d, 0.0f, 1.0f);
  77. return S3DVertex(Pos.getInterpolated(other.Pos, d),
  78. Normal.getInterpolated(other.Normal, d),
  79. Color.getInterpolated(other.Color, d),
  80. TCoords.getInterpolated(other.TCoords, d));
  81. }
  82. };
  83. //! Vertex with two texture coordinates.
  84. /** Usually used for geometry with lightmaps
  85. or other special materials.
  86. */
  87. struct S3DVertex2TCoords : public S3DVertex
  88. {
  89. //! default constructor
  90. S3DVertex2TCoords() : S3DVertex() {}
  91. //! constructor with two different texture coords, but no normal
  92. S3DVertex2TCoords(f32 x, f32 y, f32 z, SColor c, f32 tu, f32 tv, f32 tu2, f32 tv2)
  93. : S3DVertex(x,y,z, 0.0f, 0.0f, 0.0f, c, tu,tv), TCoords2(tu2,tv2) {}
  94. //! constructor with two different texture coords, but no normal
  95. S3DVertex2TCoords(const core::vector3df& pos, SColor color,
  96. const core::vector2d<f32>& tcoords, const core::vector2d<f32>& tcoords2)
  97. : S3DVertex(pos, core::vector3df(), color, tcoords), TCoords2(tcoords2) {}
  98. //! constructor with all values
  99. S3DVertex2TCoords(const core::vector3df& pos, const core::vector3df& normal, const SColor& color,
  100. const core::vector2d<f32>& tcoords, const core::vector2d<f32>& tcoords2)
  101. : S3DVertex(pos, normal, color, tcoords), TCoords2(tcoords2) {}
  102. //! constructor with all values
  103. S3DVertex2TCoords(f32 x, f32 y, f32 z, f32 nx, f32 ny, f32 nz, SColor c, f32 tu, f32 tv, f32 tu2, f32 tv2)
  104. : S3DVertex(x,y,z, nx,ny,nz, c, tu,tv), TCoords2(tu2,tv2) {}
  105. //! constructor with the same texture coords and normal
  106. S3DVertex2TCoords(f32 x, f32 y, f32 z, f32 nx, f32 ny, f32 nz, SColor c, f32 tu, f32 tv)
  107. : S3DVertex(x,y,z, nx,ny,nz, c, tu,tv), TCoords2(tu,tv) {}
  108. //! constructor with the same texture coords and normal
  109. S3DVertex2TCoords(const core::vector3df& pos, const core::vector3df& normal,
  110. SColor color, const core::vector2d<f32>& tcoords)
  111. : S3DVertex(pos, normal, color, tcoords), TCoords2(tcoords) {}
  112. //! constructor from S3DVertex
  113. S3DVertex2TCoords(S3DVertex& o) : S3DVertex(o) {}
  114. //! Second set of texture coordinates
  115. core::vector2d<f32> TCoords2;
  116. //! Equality operator
  117. bool operator==(const S3DVertex2TCoords& other) const
  118. {
  119. return ((static_cast<S3DVertex>(*this)==other) &&
  120. (TCoords2 == other.TCoords2));
  121. }
  122. //! Inequality operator
  123. bool operator!=(const S3DVertex2TCoords& other) const
  124. {
  125. return ((static_cast<S3DVertex>(*this)!=other) ||
  126. (TCoords2 != other.TCoords2));
  127. }
  128. bool operator<(const S3DVertex2TCoords& other) const
  129. {
  130. return ((static_cast<S3DVertex>(*this) < other) ||
  131. ((static_cast<S3DVertex>(*this) == other) && (TCoords2 < other.TCoords2)));
  132. }
  133. E_VERTEX_TYPE getType() const
  134. {
  135. return EVT_2TCOORDS;
  136. }
  137. S3DVertex2TCoords getInterpolated(const S3DVertex2TCoords& other, f32 d)
  138. {
  139. d = core::clamp(d, 0.0f, 1.0f);
  140. return S3DVertex2TCoords(Pos.getInterpolated(other.Pos, d),
  141. Normal.getInterpolated(other.Normal, d),
  142. Color.getInterpolated(other.Color, d),
  143. TCoords.getInterpolated(other.TCoords, d),
  144. TCoords2.getInterpolated(other.TCoords2, d));
  145. }
  146. };
  147. //! Vertex with a tangent and binormal vector.
  148. /** Usually used for tangent space normal mapping. */
  149. struct S3DVertexTangents : public S3DVertex
  150. {
  151. //! default constructor
  152. S3DVertexTangents() : S3DVertex() { }
  153. //! constructor
  154. S3DVertexTangents(f32 x, f32 y, f32 z, f32 nx=0.0f, f32 ny=0.0f, f32 nz=0.0f,
  155. SColor c = 0xFFFFFFFF, f32 tu=0.0f, f32 tv=0.0f,
  156. f32 tanx=0.0f, f32 tany=0.0f, f32 tanz=0.0f,
  157. f32 bx=0.0f, f32 by=0.0f, f32 bz=0.0f)
  158. : S3DVertex(x,y,z, nx,ny,nz, c, tu,tv), Tangent(tanx,tany,tanz), Binormal(bx,by,bz) { }
  159. //! constructor
  160. S3DVertexTangents(const core::vector3df& pos, SColor c,
  161. const core::vector2df& tcoords)
  162. : S3DVertex(pos, core::vector3df(), c, tcoords) { }
  163. //! constructor
  164. S3DVertexTangents(const core::vector3df& pos,
  165. const core::vector3df& normal, SColor c,
  166. const core::vector2df& tcoords,
  167. const core::vector3df& tangent=core::vector3df(),
  168. const core::vector3df& binormal=core::vector3df())
  169. : S3DVertex(pos, normal, c, tcoords), Tangent(tangent), Binormal(binormal) { }
  170. //! Tangent vector along the x-axis of the texture
  171. core::vector3df Tangent;
  172. //! Binormal vector (tangent x normal)
  173. core::vector3df Binormal;
  174. bool operator==(const S3DVertexTangents& other) const
  175. {
  176. return ((static_cast<S3DVertex>(*this)==other) &&
  177. (Tangent == other.Tangent) &&
  178. (Binormal == other.Binormal));
  179. }
  180. bool operator!=(const S3DVertexTangents& other) const
  181. {
  182. return ((static_cast<S3DVertex>(*this)!=other) ||
  183. (Tangent != other.Tangent) ||
  184. (Binormal != other.Binormal));
  185. }
  186. bool operator<(const S3DVertexTangents& other) const
  187. {
  188. return ((static_cast<S3DVertex>(*this) < other) ||
  189. ((static_cast<S3DVertex>(*this) == other) && (Tangent < other.Tangent)) ||
  190. ((static_cast<S3DVertex>(*this) == other) && (Tangent == other.Tangent) && (Binormal < other.Binormal)));
  191. }
  192. E_VERTEX_TYPE getType() const
  193. {
  194. return EVT_TANGENTS;
  195. }
  196. S3DVertexTangents getInterpolated(const S3DVertexTangents& other, f32 d)
  197. {
  198. d = core::clamp(d, 0.0f, 1.0f);
  199. return S3DVertexTangents(Pos.getInterpolated(other.Pos, d),
  200. Normal.getInterpolated(other.Normal, d),
  201. Color.getInterpolated(other.Color, d),
  202. TCoords.getInterpolated(other.TCoords, d),
  203. Tangent.getInterpolated(other.Tangent, d),
  204. Binormal.getInterpolated(other.Binormal, d));
  205. }
  206. };
  207. inline u32 getVertexPitchFromType(E_VERTEX_TYPE vertexType)
  208. {
  209. switch (vertexType)
  210. {
  211. case video::EVT_2TCOORDS:
  212. return sizeof(video::S3DVertex2TCoords);
  213. case video::EVT_TANGENTS:
  214. return sizeof(video::S3DVertexTangents);
  215. default:
  216. return sizeof(video::S3DVertex);
  217. }
  218. }
  219. } // end namespace video
  220. } // end namespace irr
  221. #endif