SVertexManipulator.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // Copyright (C) 2009-2012 Christian Stehno
  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_VERTEX_MANIPULATOR_H_INCLUDED
  5. #define S_VERTEX_MANIPULATOR_H_INCLUDED
  6. #include "matrix4.h"
  7. #include "S3DVertex.h"
  8. #include "SColor.h"
  9. namespace irr
  10. {
  11. namespace scene
  12. {
  13. class IMesh;
  14. class IMeshBuffer;
  15. struct SMesh;
  16. //! Interface for vertex manipulators.
  17. /** You should derive your manipulator from this class if it shall be called for every vertex, getting as parameter just the vertex.
  18. */
  19. struct IVertexManipulator
  20. {
  21. };
  22. //! Vertex manipulator to set color to a fixed color for all vertices
  23. class SVertexColorSetManipulator : public IVertexManipulator
  24. {
  25. public:
  26. SVertexColorSetManipulator(video::SColor color) : Color(color) {}
  27. void operator()(video::S3DVertex& vertex) const
  28. {
  29. vertex.Color=Color;
  30. }
  31. private:
  32. video::SColor Color;
  33. };
  34. //! Vertex manipulator to set the alpha value of the vertex color to a fixed value
  35. class SVertexColorSetAlphaManipulator : public IVertexManipulator
  36. {
  37. public:
  38. SVertexColorSetAlphaManipulator(u32 alpha) : Alpha(alpha) {}
  39. void operator()(video::S3DVertex& vertex) const
  40. {
  41. vertex.Color.setAlpha(Alpha);
  42. }
  43. private:
  44. u32 Alpha;
  45. };
  46. //! Vertex manipulator which inverts the RGB values
  47. class SVertexColorInvertManipulator : public IVertexManipulator
  48. {
  49. public:
  50. void operator()(video::S3DVertex& vertex) const
  51. {
  52. vertex.Color.setRed(255-vertex.Color.getRed());
  53. vertex.Color.setGreen(255-vertex.Color.getGreen());
  54. vertex.Color.setBlue(255-vertex.Color.getBlue());
  55. }
  56. };
  57. //! Vertex manipulator to set vertex color to one of two values depending on a given threshold
  58. /** If average of the color value is >Threshold the High color is chosen, else Low. */
  59. class SVertexColorThresholdManipulator : public IVertexManipulator
  60. {
  61. public:
  62. SVertexColorThresholdManipulator(u8 threshold, video::SColor low,
  63. video::SColor high) : Threshold(threshold), Low(low), High(high) {}
  64. void operator()(video::S3DVertex& vertex) const
  65. {
  66. vertex.Color = ((u8)vertex.Color.getAverage()>Threshold)?High:Low;
  67. }
  68. private:
  69. u8 Threshold;
  70. video::SColor Low;
  71. video::SColor High;
  72. };
  73. //! Vertex manipulator which adjusts the brightness by the given amount
  74. /** A positive value increases brightness, a negative value darkens the colors. */
  75. class SVertexColorBrightnessManipulator : public IVertexManipulator
  76. {
  77. public:
  78. SVertexColorBrightnessManipulator(s32 amount) : Amount(amount) {}
  79. void operator()(video::S3DVertex& vertex) const
  80. {
  81. vertex.Color.setRed(core::clamp(vertex.Color.getRed()+Amount, 0u, 255u));
  82. vertex.Color.setGreen(core::clamp(vertex.Color.getGreen()+Amount, 0u, 255u));
  83. vertex.Color.setBlue(core::clamp(vertex.Color.getBlue()+Amount, 0u, 255u));
  84. }
  85. private:
  86. s32 Amount;
  87. };
  88. //! Vertex manipulator which adjusts the contrast by the given factor
  89. /** Factors over 1 increase contrast, below 1 reduce it. */
  90. class SVertexColorContrastManipulator : public IVertexManipulator
  91. {
  92. public:
  93. SVertexColorContrastManipulator(f32 factor) : Factor(factor) {}
  94. void operator()(video::S3DVertex& vertex) const
  95. {
  96. vertex.Color.setRed(core::clamp(core::round32((vertex.Color.getRed()-128)*Factor)+128, 0, 255));
  97. vertex.Color.setGreen(core::clamp(core::round32((vertex.Color.getGreen()-128)*Factor)+128, 0, 255));
  98. vertex.Color.setBlue(core::clamp(core::round32((vertex.Color.getBlue()-128)*Factor)+128, 0, 255));
  99. }
  100. private:
  101. f32 Factor;
  102. };
  103. //! Vertex manipulator which adjusts the contrast by the given factor and brightness by a signed amount.
  104. /** Factors over 1 increase contrast, below 1 reduce it.
  105. A positive amount increases brightness, a negative one darkens the colors. */
  106. class SVertexColorContrastBrightnessManipulator : public IVertexManipulator
  107. {
  108. public:
  109. SVertexColorContrastBrightnessManipulator(f32 factor, s32 amount) : Factor(factor), Amount(amount+128) {}
  110. void operator()(video::S3DVertex& vertex) const
  111. {
  112. vertex.Color.setRed(core::clamp(core::round32((vertex.Color.getRed()-128)*Factor)+Amount, 0, 255));
  113. vertex.Color.setGreen(core::clamp(core::round32((vertex.Color.getGreen()-128)*Factor)+Amount, 0, 255));
  114. vertex.Color.setBlue(core::clamp(core::round32((vertex.Color.getBlue()-128)*Factor)+Amount, 0, 255));
  115. }
  116. private:
  117. f32 Factor;
  118. s32 Amount;
  119. };
  120. //! Vertex manipulator which adjusts the brightness by a gamma operation
  121. /** A value over one increases brightness, one below darkens the colors. */
  122. class SVertexColorGammaManipulator : public IVertexManipulator
  123. {
  124. public:
  125. SVertexColorGammaManipulator(f32 gamma) : Gamma(1.f)
  126. {
  127. if (gamma != 0.f)
  128. Gamma = 1.f/gamma;
  129. }
  130. void operator()(video::S3DVertex& vertex) const
  131. {
  132. vertex.Color.setRed(core::clamp(core::round32(powf((f32)(vertex.Color.getRed()),Gamma)), 0, 255));
  133. vertex.Color.setGreen(core::clamp(core::round32(powf((f32)(vertex.Color.getGreen()),Gamma)), 0, 255));
  134. vertex.Color.setBlue(core::clamp(core::round32(powf((f32)(vertex.Color.getBlue()),Gamma)), 0, 255));
  135. }
  136. private:
  137. f32 Gamma;
  138. };
  139. //! Vertex manipulator which scales the color values
  140. /** Can e.g be used for white balance, factor would be 255.f/brightest color. */
  141. class SVertexColorScaleManipulator : public IVertexManipulator
  142. {
  143. public:
  144. SVertexColorScaleManipulator(f32 factor) : Factor(factor) {}
  145. void operator()(video::S3DVertex& vertex) const
  146. {
  147. vertex.Color.setRed(core::clamp(core::round32(vertex.Color.getRed()*Factor), 0, 255));
  148. vertex.Color.setGreen(core::clamp(core::round32(vertex.Color.getGreen()*Factor), 0, 255));
  149. vertex.Color.setBlue(core::clamp(core::round32(vertex.Color.getBlue()*Factor), 0, 255));
  150. }
  151. private:
  152. f32 Factor;
  153. };
  154. //! Vertex manipulator which desaturates the color values
  155. /** Uses the lightness value of the color. */
  156. class SVertexColorDesaturateToLightnessManipulator : public IVertexManipulator
  157. {
  158. public:
  159. void operator()(video::S3DVertex& vertex) const
  160. {
  161. vertex.Color=core::round32(vertex.Color.getLightness());
  162. }
  163. };
  164. //! Vertex manipulator which desaturates the color values
  165. /** Uses the average value of the color. */
  166. class SVertexColorDesaturateToAverageManipulator : public IVertexManipulator
  167. {
  168. public:
  169. void operator()(video::S3DVertex& vertex) const
  170. {
  171. vertex.Color=vertex.Color.getAverage();
  172. }
  173. };
  174. //! Vertex manipulator which desaturates the color values
  175. /** Uses the luminance value of the color. */
  176. class SVertexColorDesaturateToLuminanceManipulator : public IVertexManipulator
  177. {
  178. public:
  179. void operator()(video::S3DVertex& vertex) const
  180. {
  181. vertex.Color=core::round32(vertex.Color.getLuminance());
  182. }
  183. };
  184. //! Vertex manipulator which interpolates the color values
  185. /** Uses linear interpolation. */
  186. class SVertexColorInterpolateLinearManipulator : public IVertexManipulator
  187. {
  188. public:
  189. SVertexColorInterpolateLinearManipulator(video::SColor color, f32 factor) :
  190. Color(color), Factor(factor) {}
  191. void operator()(video::S3DVertex& vertex) const
  192. {
  193. vertex.Color=vertex.Color.getInterpolated(Color, Factor);
  194. }
  195. private:
  196. video::SColor Color;
  197. f32 Factor;
  198. };
  199. //! Vertex manipulator which interpolates the color values
  200. /** Uses linear interpolation. */
  201. class SVertexColorInterpolateQuadraticManipulator : public IVertexManipulator
  202. {
  203. public:
  204. SVertexColorInterpolateQuadraticManipulator(video::SColor color1, video::SColor color2, f32 factor) :
  205. Color1(color1), Color2(color2), Factor(factor) {}
  206. void operator()(video::S3DVertex& vertex) const
  207. {
  208. vertex.Color=vertex.Color.getInterpolated_quadratic(Color1, Color2, Factor);
  209. }
  210. private:
  211. video::SColor Color1;
  212. video::SColor Color2;
  213. f32 Factor;
  214. };
  215. //! Vertex manipulator which scales the position of the vertex
  216. class SVertexPositionScaleManipulator : public IVertexManipulator
  217. {
  218. public:
  219. SVertexPositionScaleManipulator(const core::vector3df& factor) : Factor(factor) {}
  220. template <typename VType>
  221. void operator()(VType& vertex) const
  222. {
  223. vertex.Pos *= Factor;
  224. }
  225. private:
  226. core::vector3df Factor;
  227. };
  228. //! Vertex manipulator which scales the position of the vertex along the normals
  229. /** This can look more pleasing than the usual Scale operator, but
  230. depends on the mesh geometry.
  231. */
  232. class SVertexPositionScaleAlongNormalsManipulator : public IVertexManipulator
  233. {
  234. public:
  235. SVertexPositionScaleAlongNormalsManipulator(const core::vector3df& factor) : Factor(factor) {}
  236. template <typename VType>
  237. void operator()(VType& vertex) const
  238. {
  239. vertex.Pos += vertex.Normal*Factor;
  240. }
  241. private:
  242. core::vector3df Factor;
  243. };
  244. //! Vertex manipulator which transforms the position of the vertex
  245. class SVertexPositionTransformManipulator : public IVertexManipulator
  246. {
  247. public:
  248. SVertexPositionTransformManipulator(const core::matrix4& m) : Transformation(m) {}
  249. template <typename VType>
  250. void operator()(VType& vertex) const
  251. {
  252. Transformation.transformVect(vertex.Pos);
  253. }
  254. private:
  255. core::matrix4 Transformation;
  256. };
  257. //! Vertex manipulator which transforms the normal of the vertex
  258. class SVertexNormalTransformManipulator : public IVertexManipulator
  259. {
  260. public:
  261. SVertexNormalTransformManipulator(const core::matrix4& m) : Transformation(m) {}
  262. template <typename VType>
  263. void operator()(VType& vertex) const
  264. {
  265. Transformation.transformVect(vertex.Normal);
  266. }
  267. private:
  268. core::matrix4 Transformation;
  269. };
  270. //! Vertex manipulator which transforms the normal of the vertex with the rotate/scale part of the given matrix (inner 3x3)
  271. class SVertexNormalRotateScaleManipulator : public IVertexManipulator
  272. {
  273. public:
  274. SVertexNormalRotateScaleManipulator(const core::matrix4& m) : Transformation(m) {}
  275. template <typename VType>
  276. void operator()(VType& vertex) const
  277. {
  278. Transformation.rotateVect(vertex.Normal);
  279. }
  280. private:
  281. core::matrix4 Transformation;
  282. };
  283. //! Vertex manipulator which normalizes the normal of the vertex
  284. class SVertexNormalizeNormalManipulator : public IVertexManipulator
  285. {
  286. public:
  287. SVertexNormalizeNormalManipulator() {}
  288. template <typename VType>
  289. void operator()(VType& vertex) const
  290. {
  291. vertex.Normal.normalize();
  292. }
  293. };
  294. //! Vertex manipulator which scales the TCoords of the vertex
  295. class SVertexTCoordsScaleManipulator : public IVertexManipulator
  296. {
  297. public:
  298. SVertexTCoordsScaleManipulator(const core::vector2df& factor, u32 uvSet=1) : Factor(factor), UVSet(uvSet) {}
  299. void operator()(video::S3DVertex2TCoords& vertex) const
  300. {
  301. if (1==UVSet)
  302. vertex.TCoords *= Factor;
  303. else if (2==UVSet)
  304. vertex.TCoords2 *= Factor;
  305. }
  306. template <typename VType>
  307. void operator()(VType& vertex) const
  308. {
  309. if (1==UVSet)
  310. vertex.TCoords *= Factor;
  311. }
  312. private:
  313. core::vector2df Factor;
  314. u32 UVSet;
  315. };
  316. } // end namespace scene
  317. } // end namespace irr
  318. #endif