Irrlicht 3D Engine
SVertexManipulator.h
Go to the documentation of this file.
00001 // Copyright (C) 2009-2012 Christian Stehno
00002 // This file is part of the "Irrlicht Engine".
00003 // For conditions of distribution and use, see copyright notice in irrlicht.h
00004 
00005 #ifndef __S_VERTEX_MANIPULATOR_H_INCLUDED__
00006 #define __S_VERTEX_MANIPULATOR_H_INCLUDED__
00007 
00008 #include "S3DVertex.h"
00009 #include "SColor.h"
00010 
00011 namespace irr
00012 {
00013 namespace scene
00014 {
00015 
00016     class IMesh;
00017     class IMeshBuffer;
00018     struct SMesh;
00019 
00021 
00023     struct IVertexManipulator
00024     {
00025     };
00027     class SVertexColorSetManipulator : public IVertexManipulator
00028     {
00029     public:
00030         SVertexColorSetManipulator(video::SColor color) : Color(color) {}
00031         void operator()(video::S3DVertex& vertex) const
00032         {
00033             vertex.Color=Color;
00034         }
00035     private:
00036         video::SColor Color;
00037     };
00039     class SVertexColorSetAlphaManipulator : public IVertexManipulator
00040     {
00041     public:
00042         SVertexColorSetAlphaManipulator(u32 alpha) : Alpha(alpha) {}
00043         void operator()(video::S3DVertex& vertex) const
00044         {
00045             vertex.Color.setAlpha(Alpha);
00046         }
00047     private:
00048         u32 Alpha;
00049     };
00051     class SVertexColorInvertManipulator : public IVertexManipulator
00052     {
00053     public:
00054         void operator()(video::S3DVertex& vertex) const
00055         {
00056             vertex.Color.setRed(255-vertex.Color.getRed());
00057             vertex.Color.setGreen(255-vertex.Color.getGreen());
00058             vertex.Color.setBlue(255-vertex.Color.getBlue());
00059         }
00060     };
00062 
00063     class SVertexColorThresholdManipulator : public IVertexManipulator
00064     {
00065     public:
00066         SVertexColorThresholdManipulator(u8 threshold, video::SColor low,
00067             video::SColor high) : Threshold(threshold), Low(low), High(high) {}
00068         void operator()(video::S3DVertex& vertex) const
00069         {
00070             vertex.Color = ((u8)vertex.Color.getAverage()>Threshold)?High:Low;
00071         }
00072     private:
00073         u8 Threshold;
00074         video::SColor Low;
00075         video::SColor High;
00076     };
00078 
00079     class SVertexColorBrightnessManipulator : public IVertexManipulator
00080     {
00081     public:
00082         SVertexColorBrightnessManipulator(s32 amount) : Amount(amount) {}
00083         void operator()(video::S3DVertex& vertex) const
00084         {
00085             vertex.Color.setRed(core::clamp(vertex.Color.getRed()+Amount, 0u, 255u));
00086             vertex.Color.setGreen(core::clamp(vertex.Color.getGreen()+Amount, 0u, 255u));
00087             vertex.Color.setBlue(core::clamp(vertex.Color.getBlue()+Amount, 0u, 255u));
00088         }
00089     private:
00090         s32 Amount;
00091     };
00093 
00094     class SVertexColorContrastManipulator : public IVertexManipulator
00095     {
00096     public:
00097         SVertexColorContrastManipulator(f32 factor) : Factor(factor) {}
00098         void operator()(video::S3DVertex& vertex) const
00099         {
00100             vertex.Color.setRed(core::clamp(core::round32((vertex.Color.getRed()-128)*Factor)+128, 0, 255));
00101             vertex.Color.setGreen(core::clamp(core::round32((vertex.Color.getGreen()-128)*Factor)+128, 0, 255));
00102             vertex.Color.setBlue(core::clamp(core::round32((vertex.Color.getBlue()-128)*Factor)+128, 0, 255));
00103         }
00104     private:
00105         f32 Factor;
00106     };
00108 
00110     class SVertexColorContrastBrightnessManipulator : public IVertexManipulator
00111     {
00112     public:
00113         SVertexColorContrastBrightnessManipulator(f32 factor, s32 amount) : Factor(factor), Amount(amount+128) {}
00114         void operator()(video::S3DVertex& vertex) const
00115         {
00116             vertex.Color.setRed(core::clamp(core::round32((vertex.Color.getRed()-128)*Factor)+Amount, 0, 255));
00117             vertex.Color.setGreen(core::clamp(core::round32((vertex.Color.getGreen()-128)*Factor)+Amount, 0, 255));
00118             vertex.Color.setBlue(core::clamp(core::round32((vertex.Color.getBlue()-128)*Factor)+Amount, 0, 255));
00119         }
00120     private:
00121         f32 Factor;
00122         s32 Amount;
00123     };
00125 
00126     class SVertexColorGammaManipulator : public IVertexManipulator
00127     {
00128     public:
00129         SVertexColorGammaManipulator(f32 gamma) : Gamma(1.f)
00130         {
00131             if (gamma != 0.f)
00132                 Gamma = 1.f/gamma;
00133         }
00134         void operator()(video::S3DVertex& vertex) const
00135         {
00136             vertex.Color.setRed(core::clamp(core::round32(powf((f32)(vertex.Color.getRed()),Gamma)), 0, 255));
00137             vertex.Color.setGreen(core::clamp(core::round32(powf((f32)(vertex.Color.getGreen()),Gamma)), 0, 255));
00138             vertex.Color.setBlue(core::clamp(core::round32(powf((f32)(vertex.Color.getBlue()),Gamma)), 0, 255));
00139         }
00140     private:
00141         f32 Gamma;
00142     };
00144 
00145     class SVertexColorScaleManipulator : public IVertexManipulator
00146     {
00147     public:
00148         SVertexColorScaleManipulator(f32 factor) : Factor(factor) {}
00149         void operator()(video::S3DVertex& vertex) const
00150         {
00151             vertex.Color.setRed(core::clamp(core::round32(vertex.Color.getRed()*Factor), 0, 255));
00152             vertex.Color.setGreen(core::clamp(core::round32(vertex.Color.getGreen()*Factor), 0, 255));
00153             vertex.Color.setBlue(core::clamp(core::round32(vertex.Color.getBlue()*Factor), 0, 255));
00154         }
00155     private:
00156         f32 Factor;
00157     };
00159 
00160     class SVertexColorDesaturateToLightnessManipulator : public IVertexManipulator
00161     {
00162     public:
00163         void operator()(video::S3DVertex& vertex) const
00164         {
00165             vertex.Color=core::round32(vertex.Color.getLightness());
00166         }
00167     };
00169 
00170     class SVertexColorDesaturateToAverageManipulator : public IVertexManipulator
00171     {
00172     public:
00173         void operator()(video::S3DVertex& vertex) const
00174         {
00175             vertex.Color=vertex.Color.getAverage();
00176         }
00177     };
00179 
00180     class SVertexColorDesaturateToLuminanceManipulator : public IVertexManipulator
00181     {
00182     public:
00183         void operator()(video::S3DVertex& vertex) const
00184         {
00185             vertex.Color=core::round32(vertex.Color.getLuminance());
00186         }
00187     };
00189 
00190     class SVertexColorInterpolateLinearManipulator : public IVertexManipulator
00191     {
00192     public:
00193         SVertexColorInterpolateLinearManipulator(video::SColor color, f32 factor) :
00194           Color(color), Factor(factor) {}
00195         void operator()(video::S3DVertex& vertex) const
00196         {
00197             vertex.Color=vertex.Color.getInterpolated(Color, Factor);
00198         }
00199     private:
00200         video::SColor Color;
00201         f32 Factor;
00202     };
00204 
00205     class SVertexColorInterpolateQuadraticManipulator : public IVertexManipulator
00206     {
00207     public:
00208         SVertexColorInterpolateQuadraticManipulator(video::SColor color1, video::SColor color2, f32 factor) :
00209           Color1(color1), Color2(color2), Factor(factor) {}
00210         void operator()(video::S3DVertex& vertex) const
00211         {
00212             vertex.Color=vertex.Color.getInterpolated_quadratic(Color1, Color2, Factor);
00213         }
00214     private:
00215         video::SColor Color1;
00216         video::SColor Color2;
00217         f32 Factor;
00218     };
00219 
00221     class SVertexPositionScaleManipulator : public IVertexManipulator
00222     {
00223     public:
00224         SVertexPositionScaleManipulator(const core::vector3df& factor) : Factor(factor) {}
00225         template <typename VType>
00226         void operator()(VType& vertex) const
00227         {
00228             vertex.Pos *= Factor;
00229         }
00230     private:
00231         core::vector3df Factor;
00232     };
00233 
00235 
00238     class SVertexPositionScaleAlongNormalsManipulator : public IVertexManipulator
00239     {
00240     public:
00241         SVertexPositionScaleAlongNormalsManipulator(const core::vector3df& factor) : Factor(factor) {}
00242         template <typename VType>
00243         void operator()(VType& vertex) const
00244         {
00245             vertex.Pos += vertex.Normal*Factor;
00246         }
00247     private:
00248         core::vector3df Factor;
00249     };
00250 
00252     class SVertexPositionTransformManipulator : public IVertexManipulator
00253     {
00254     public:
00255         SVertexPositionTransformManipulator(const core::matrix4& m) : Transformation(m) {}
00256         template <typename VType>
00257         void operator()(VType& vertex) const
00258         {
00259             Transformation.transformVect(vertex.Pos);
00260         }
00261     private:
00262         core::matrix4 Transformation;
00263     };
00264 
00266     class SVertexTCoordsScaleManipulator : public IVertexManipulator
00267     {
00268     public:
00269         SVertexTCoordsScaleManipulator(const core::vector2df& factor, u32 uvSet=1) : Factor(factor), UVSet(uvSet) {}
00270         void operator()(video::S3DVertex2TCoords& vertex) const
00271         {
00272             if (1==UVSet)
00273                 vertex.TCoords *= Factor;
00274             else if (2==UVSet)
00275                 vertex.TCoords2 *= Factor;
00276         }
00277         template <typename VType>
00278         void operator()(VType& vertex) const
00279         {
00280             if (1==UVSet)
00281                 vertex.TCoords *= Factor;
00282         }
00283     private:
00284         core::vector2df Factor;
00285         u32 UVSet;
00286     };
00287 
00288 } // end namespace scene
00289 } // end namespace irr
00290 
00291 
00292 #endif