aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8.1/include/SVertexManipulator.h
blob: 6696ad018826e19e92caa3cbf84fa5c43d487184 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (C) 2009-2012 Christian Stehno
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h

#ifndef __S_VERTEX_MANIPULATOR_H_INCLUDED__
#define __S_VERTEX_MANIPULATOR_H_INCLUDED__

#include "S3DVertex.h"
#include "SColor.h"

namespace irr
{
namespace scene
{

	class IMesh;
	class IMeshBuffer;
	struct SMesh;

	//! Interface for vertex manipulators.
	/** You should derive your manipulator from this class if it shall be called for every vertex, getting as parameter just the vertex.
	*/
	struct IVertexManipulator
	{
	};
	//! Vertex manipulator to set color to a fixed color for all vertices
	class SVertexColorSetManipulator : public IVertexManipulator
	{
	public:
		SVertexColorSetManipulator(video::SColor color) : Color(color) {}
		void operator()(video::S3DVertex& vertex) const
		{
			vertex.Color=Color;
		}
	private:
		video::SColor Color;
	};
	//! Vertex manipulator to set the alpha value of the vertex color to a fixed value
	class SVertexColorSetAlphaManipulator : public IVertexManipulator
	{
	public:
		SVertexColorSetAlphaManipulator(u32 alpha) : Alpha(alpha) {}
		void operator()(video::S3DVertex& vertex) const
		{
			vertex.Color.setAlpha(Alpha);
		}
	private:
		u32 Alpha;
	};
	//! Vertex manipulator which invertes the RGB values
	class SVertexColorInvertManipulator : public IVertexManipulator
	{
	public:
		void operator()(video::S3DVertex& vertex) const
		{
			vertex.Color.setRed(255-vertex.Color.getRed());
			vertex.Color.setGreen(255-vertex.Color.getGreen());
			vertex.Color.setBlue(255-vertex.Color.getBlue());
		}
	};
	//! Vertex manipulator to set vertex color to one of two values depending on a given threshold
	/** If average of the color value is >Threshold the High color is chosen, else Low. */
	class SVertexColorThresholdManipulator : public IVertexManipulator
	{
	public:
		SVertexColorThresholdManipulator(u8 threshold, video::SColor low,
			video::SColor high) : Threshold(threshold), Low(low), High(high) {}
		void operator()(video::S3DVertex& vertex) const
		{
			vertex.Color = ((u8)vertex.Color.getAverage()>Threshold)?High:Low;
		}
	private:
		u8 Threshold;
		video::SColor Low;
		video::SColor High;
	};
	//! Vertex manipulator which adjusts the brightness by the given amount
	/** A positive value increases brightness, a negative value darkens the colors. */
	class SVertexColorBrightnessManipulator : public IVertexManipulator
	{
	public:
		SVertexColorBrightnessManipulator(s32 amount) : Amount(amount) {}
		void operator()(video::S3DVertex& vertex) const
		{
			vertex.Color.setRed(core::clamp(vertex.Color.getRed()+Amount, 0u, 255u));
			vertex.Color.setGreen(core::clamp(vertex.Color.getGreen()+Amount, 0u, 255u));
			vertex.Color.setBlue(core::clamp(vertex.Color.getBlue()+Amount, 0u, 255u));
		}
	private:
		s32 Amount;
	};
	//! Vertex manipulator which adjusts the contrast by the given factor
	/** Factors over 1 increase contrast, below 1 reduce it. */
	class SVertexColorContrastManipulator : public IVertexManipulator
	{
	public:
		SVertexColorContrastManipulator(f32 factor) : Factor(factor) {}
		void operator()(video::S3DVertex& vertex) const
		{
			vertex.Color.setRed(core::clamp(core::round32((vertex.Color.getRed()-128)*Factor)+128, 0, 255));
			vertex.Color.setGreen(core::clamp(core::round32((vertex.Color.getGreen()-128)*Factor)+128, 0, 255));
			vertex.Color.setBlue(core::clamp(core::round32((vertex.Color.getBlue()-128)*Factor)+128, 0, 255));
		}
	private:
		f32 Factor;
	};
	//! Vertex manipulator which adjusts the contrast by the given factor and brightness by a signed amount.
	/** Factors over 1 increase contrast, below 1 reduce it.
	A positive amount increases brightness, a negative one darkens the colors. */
	class SVertexColorContrastBrightnessManipulator : public IVertexManipulator
	{
	public:
		SVertexColorContrastBrightnessManipulator(f32 factor, s32 amount) : Factor(factor), Amount(amount+128) {}
		void operator()(video::S3DVertex& vertex) const
		{
			vertex.Color.setRed(core::clamp(core::round32((vertex.Color.getRed()-128)*Factor)+Amount, 0, 255));
			vertex.Color.setGreen(core::clamp(core::round32((vertex.Color.getGreen()-128)*Factor)+Amount, 0, 255));
			vertex.Color.setBlue(core::clamp(core::round32((vertex.Color.getBlue()-128)*Factor)+Amount, 0, 255));
		}
	private:
		f32 Factor;
		s32 Amount;
	};
	//! Vertex manipulator which adjusts the brightness by a gamma operation
	/** A value over one increases brightness, one below darkens the colors. */
	class SVertexColorGammaManipulator : public IVertexManipulator
	{
	public:
		SVertexColorGammaManipulator(f32 gamma) : Gamma(1.f)
		{
			if (gamma != 0.f)
				Gamma = 1.f/gamma;
		}
		void operator()(video::S3DVertex& vertex) const
		{
			vertex.Color.setRed(core::clamp(core::round32(powf((f32)(vertex.Color.getRed()),Gamma)), 0, 255));
			vertex.Color.setGreen(core::clamp(core::round32(powf((f32)(vertex.Color.getGreen()),Gamma)), 0, 255));
			vertex.Color.setBlue(core::clamp(core::round32(powf((f32)(vertex.Color.getBlue()),Gamma)), 0, 255));
		}
	private:
		f32 Gamma;
	};
	//! Vertex manipulator which scales the color values
	/** Can e.g be used for white balance, factor would be 255.f/brightest color. */
	class SVertexColorScaleManipulator : public IVertexManipulator
	{
	public:
		SVertexColorScaleManipulator(f32 factor) : Factor(factor) {}
		void operator()(video::S3DVertex& vertex) const
		{
			vertex.Color.setRed(core::clamp(core::round32(vertex.Color.getRed()*Factor), 0, 255));
			vertex.Color.setGreen(core::clamp(core::round32(vertex.Color.getGreen()*Factor), 0, 255));
			vertex.Color.setBlue(core::clamp(core::round32(vertex.Color.getBlue()*Factor), 0, 255));
		}
	private:
		f32 Factor;
	};
	//! Vertex manipulator which desaturates the color values
	/** Uses the lightness value of the color. */
	class SVertexColorDesaturateToLightnessManipulator : public IVertexManipulator
	{
	public:
		void operator()(video::S3DVertex& vertex) const
		{
			vertex.Color=core::round32(vertex.Color.getLightness());
		}
	};
	//! Vertex manipulator which desaturates the color values
	/** Uses the average value of the color. */
	class SVertexColorDesaturateToAverageManipulator : public IVertexManipulator
	{
	public:
		void operator()(video::S3DVertex& vertex) const
		{
			vertex.Color=vertex.Color.getAverage();
		}
	};
	//! Vertex manipulator which desaturates the color values
	/** Uses the luminance value of the color. */
	class SVertexColorDesaturateToLuminanceManipulator : public IVertexManipulator
	{
	public:
		void operator()(video::S3DVertex& vertex) const
		{
			vertex.Color=core::round32(vertex.Color.getLuminance());
		}
	};
	//! Vertex manipulator which interpolates the color values
	/** Uses linear interpolation. */
	class SVertexColorInterpolateLinearManipulator : public IVertexManipulator
	{
	public:
		SVertexColorInterpolateLinearManipulator(video::SColor color, f32 factor) :
		  Color(color), Factor(factor) {}
		void operator()(video::S3DVertex& vertex) const
		{
			vertex.Color=vertex.Color.getInterpolated(Color, Factor);
		}
	private:
		video::SColor Color;
		f32 Factor;
	};
	//! Vertex manipulator which interpolates the color values
	/** Uses linear interpolation. */
	class SVertexColorInterpolateQuadraticManipulator : public IVertexManipulator
	{
	public:
		SVertexColorInterpolateQuadraticManipulator(video::SColor color1, video::SColor color2, f32 factor) :
		  Color1(color1), Color2(color2), Factor(factor) {}
		void operator()(video::S3DVertex& vertex) const
		{
			vertex.Color=vertex.Color.getInterpolated_quadratic(Color1, Color2, Factor);
		}
	private:
		video::SColor Color1;
		video::SColor Color2;
		f32 Factor;
	};

	//! Vertex manipulator which scales the position of the vertex
	class SVertexPositionScaleManipulator : public IVertexManipulator
	{
	public:
		SVertexPositionScaleManipulator(const core::vector3df& factor) : Factor(factor) {}
		template <typename VType>
		void operator()(VType& vertex) const
		{
			vertex.Pos *= Factor;
		}
	private:
		core::vector3df Factor;
	};

	//! Vertex manipulator which scales the position of the vertex along the normals
	/** This can look more pleasing than the usual Scale operator, but
	depends on the mesh geometry.
	*/
	class SVertexPositionScaleAlongNormalsManipulator : public IVertexManipulator
	{
	public:
		SVertexPositionScaleAlongNormalsManipulator(const core::vector3df& factor) : Factor(factor) {}
		template <typename VType>
		void operator()(VType& vertex) const
		{
			vertex.Pos += vertex.Normal*Factor;
		}
	private:
		core::vector3df Factor;
	};

	//! Vertex manipulator which transforms the position of the vertex
	class SVertexPositionTransformManipulator : public IVertexManipulator
	{
	public:
		SVertexPositionTransformManipulator(const core::matrix4& m) : Transformation(m) {}
		template <typename VType>
		void operator()(VType& vertex) const
		{
			Transformation.transformVect(vertex.Pos);
		}
	private:
		core::matrix4 Transformation;
	};

	//! Vertex manipulator which scales the TCoords of the vertex
	class SVertexTCoordsScaleManipulator : public IVertexManipulator
	{
	public:
		SVertexTCoordsScaleManipulator(const core::vector2df& factor, u32 uvSet=1) : Factor(factor), UVSet(uvSet) {}
		void operator()(video::S3DVertex2TCoords& vertex) const
		{
			if (1==UVSet)
				vertex.TCoords *= Factor;
			else if (2==UVSet)
				vertex.TCoords2 *= Factor;
		}
		template <typename VType>
		void operator()(VType& vertex) const
		{
			if (1==UVSet)
				vertex.TCoords *= Factor;
		}
	private:
		core::vector2df Factor;
		u32 UVSet;
	};

} // end namespace scene
} // end namespace irr


#endif