File: stage.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (333 lines) | stat: -rw-r--r-- 8,829 bytes parent folder | download | duplicates (4)
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <BALL/VIEW/KERNEL/stage.h>
#include <BALL/MATHS/plane3.h>
#include <BALL/MATHS/analyticalGeometry.h>

using std::endl;

namespace BALL
{
	namespace VIEW
	{

		LightSource::LightSource()
			:	position_(),
				direction_(0, 0, -1),
				attenuation_(1., 0., 0.),
				angle_(10),
				intensity_(0.8),
				color_(255, 255, 255, 255),
				type_(1),
				relative_(true)	
		{
		}

		LightSource::LightSource(const LightSource& light_source)
			: position_(light_source.position_),
				direction_(light_source.direction_),
				attenuation_(light_source.attenuation_),
				angle_(light_source.angle_),
				intensity_(light_source.intensity_),
				color_(light_source.color_),
				type_(light_source.type_),
				relative_(light_source.relative_)
		{
		}

		LightSource& LightSource::operator = (const LightSource& light) 
		{
				position_ = light.position_;
			 direction_ = light.direction_;
		 attenuation_ = light.attenuation_; 
					 angle_ = light.angle_,
			 intensity_ = light.intensity_;
					 color_ = light.color_,
						type_ = light.type_;
				relative_ = light.relative_;

			return *this;
		}


		bool LightSource::operator == (const LightSource& light_source) const
		{
			return position_ 		== light_source.position_ 	 &&
						 direction_ 	== light_source.direction_ 	 &&
						 attenuation_ == light_source.attenuation_ &&
						 angle_ 			== light_source.angle_ 		 	 &&
						 intensity_ 	== light_source.intensity_ 	 &&
						 color_ 			== light_source.color_ 			 &&
						 type_ 				== light_source.type_ 			 &&
						 relative_ 		== light_source.relative_;
		}
		
		bool LightSource::operator < (const LightSource& light) const
		{
			return this < &light;
		}


		void LightSource::dump(std::ostream& s, Size depth) const
		{
			BALL_DUMP_STREAM_PREFIX(s);
			
			BALL_DUMP_DEPTH(s, depth);
			BALL_DUMP_HEADER(s, this, this);

			BALL_DUMP_DEPTH(s, depth);
			s << "Position : " << position_ << std::endl;

			BALL_DUMP_DEPTH(s, depth);
			s << "Direction : " << direction_<< std::endl;

			BALL_DUMP_DEPTH(s, depth);
			s << "Attenuation : " << attenuation_<< std::endl;

			BALL_DUMP_DEPTH(s, depth);
			s << "Angle : " << angle_ << std::endl;

			BALL_DUMP_DEPTH(s, depth);
			s << "Intensity : " << intensity_ << std::endl;

			BALL_DUMP_DEPTH(s, depth);
			s << "Color : " << color_ << std::endl;

			BALL_DUMP_DEPTH(s, depth);
			s << "Type : " << type_ << std::endl;

			BALL_DUMP_DEPTH(s, depth);
			s << "Relative : " << relative_ << std::endl;

			BALL_DUMP_STREAM_SUFFIX(s);
		}

		Stage::Material::Material()
			: PersistentObject(),
				ambient_color(ColorRGBA(255, 255, 255, 255)),
				ambient_intensity(0.),
				specular_color(ColorRGBA(255, 255, 255, 255)),
				specular_intensity(1.),
				reflective_color(ColorRGBA(255, 255, 255, 255)),
				reflective_intensity(0.),
				shininess(60.),
				transparency(0.)
		{
		}

		void Stage::Material::persistentWrite(PersistenceManager& pm, const char* name) const
		{
			pm.writeObjectHeader(this, name);
				// ambient
				pm.writePrimitive((String)ambient_color, "ambient_color_");
				pm.writePrimitive(ambient_intensity,     "ambient_intensity_");
				// specular
				pm.writePrimitive((String)specular_color, "specular_color_");
				pm.writePrimitive(specular_intensity,     "specular_intensity_");
				// reflective
				pm.writePrimitive((String)reflective_color, "reflective_color_");
				pm.writePrimitive(reflective_intensity,     "reflective_intensity_");
				
				pm.writePrimitive(shininess,    "shininess_");
				pm.writePrimitive(transparency, "transparency_");
			pm.writeObjectTrailer(name);
		}
		
		void Stage::Material::persistentRead(PersistenceManager& pm)
		{
			String color;

			// ambient
			pm.readPrimitive(color, "ambient_color_");
			ambient_color.set(color);

			pm.readPrimitive(ambient_intensity,     "ambient_intensity_");
			
			// specular
			pm.readPrimitive(color, "specular_color_");
			specular_color.set(color);

			pm.readPrimitive(specular_intensity,     "specular_intensity_");

			// reflective
			pm.readPrimitive(color, "reflective_color_");
			reflective_color.set(color);

			pm.readPrimitive(reflective_intensity,     "reflective_intensity_");
			
			pm.readPrimitive(shininess,    "shininess_");
			pm.readPrimitive(transparency, "transparency_");
		}		
		
		Stage::Stage()
			: background_color_(ColorRGBA(1.,1.,1.,1.)),
				info_color_(0, 0, 255, 255),
				light_sources_(),
				camera_(),
				show_coordinate_system_(false),
				fog_intensity_(0),
				eye_distance_(2.0),
				focal_distance_(40),
				swap_side_by_side_stereo_(false)
		{}

		Stage::Stage(const Stage& stage)
			: background_color_(stage.background_color_),
				light_sources_(stage.light_sources_),
				camera_(stage.camera_),
				show_coordinate_system_(false),
				fog_intensity_(stage.fog_intensity_),
				eye_distance_(stage.eye_distance_),
				focal_distance_(stage.focal_distance_),
				swap_side_by_side_stereo_(stage.swap_side_by_side_stereo_),
				material_(stage.material_)
		{
		}

		void Stage::clear()
		{
			background_color_.clear();
			light_sources_.clear();
			camera_ = Camera();
			show_coordinate_system_ = false;
			eye_distance_ = 2.0;
			focal_distance_ = 40;
			swap_side_by_side_stereo_ = false;
			fog_intensity_ = 0;
			material_ = Material();
		}

		bool Stage::operator == (const Stage& stage) const
		{
			return light_sources_ 					== stage.light_sources_ 		&&
						 camera_ 					 				== stage.camera_ 						&&
						 background_color_ 				== stage.background_color_ 	&&
						 show_coordinate_system_ 	== stage.show_coordinate_system_ &&
						 eye_distance_ 						== stage.eye_distance_ 			&&
						 focal_distance_ 					== stage.focal_distance_ 		&&
						 swap_side_by_side_stereo_== stage.swap_side_by_side_stereo_;
		}

		void Stage::dump(std::ostream& s, Size depth) const
		{
			BALL_DUMP_STREAM_PREFIX(s);
			
			BALL_DUMP_DEPTH(s, depth);
			BALL_DUMP_HEADER(s, this, this);

			BALL_DUMP_DEPTH(s, depth);
			s << "Light sources: -------------------------------" << endl;

			list<LightSource>::const_iterator it = light_sources_.begin();
			for (; it != light_sources_.end(); it++)
			{
				it->dump(s, depth + 1);
			}

			BALL_DUMP_DEPTH(s, depth);
			s << "Camera: ---------------------------------------" << endl;
			camera_.dump(s, depth);

			BALL_DUMP_DEPTH(s, depth);
			s << "Background color:  " << background_color_ << endl;

			BALL_DUMP_DEPTH(s, depth);
			s << "Show coordinate system:  " << show_coordinate_system_ << endl;

			BALL_DUMP_DEPTH(s, depth);
			s << "Fog intensity:  " << fog_intensity_ << endl;

			BALL_DUMP_DEPTH(s, depth);
			s << "Eye distance :  " << eye_distance_<< endl;

			BALL_DUMP_DEPTH(s, depth);
			s << "Focal width:  " << focal_distance_<< endl;

			BALL_DUMP_DEPTH(s, depth);
			s << "Swap side by side:  " << swap_side_by_side_stereo_ << endl;

			BALL_DUMP_STREAM_SUFFIX(s);
		}

		Vector3 Stage::calculateRelativeCoordinates(Vector3 pos) const
		{
			// relative in units of right_vector , look_up_vector , view_vector
			// by calculating the normals to three planes
			
			// make sure the three planes are far enough, to be always on one side of them
			const float d = 1000000.0;

			try
			{
				Vector3 dr(camera_.getRightVector() * d);

				Vector3 dv(camera_.getViewVector());
				dv.normalize();
				dv *= d;

				Vector3 du(camera_.getLookUpVector() * d);

				// calculate the planes
				const Plane3 plane_rv(dr, dr);
				const Plane3 plane_uv(du, du);
				const Plane3 plane_vv(dv, dv);

				// distance of the destion of the light source from the three planes
				Vector3 result(
					GetDistance(plane_rv, pos),
					GetDistance(plane_uv, pos),
					GetDistance(plane_vv, pos));
				result -= Vector3(d);

				return -result;
			}
			catch(...)
			{
				Log.error() << "Could not calculate relative light coordinates, degenerated view vectors." << std::endl;
			}

			return Vector3(1.0);
		}

		Vector3 Stage::calculateAbsoluteCoordinates(Vector3 pos) const
		{
			try
			{
				Vector3 dv(camera_.getViewVector());
				dv.normalize();

				return (pos.x * camera_.getRightVector() + 
							  pos.y * camera_.getLookUpVector() +
								pos.z * dv);
			}
			catch(...)
			{
				Log.error() << "Could not calculate absolute light coordinates, degenerated view vectors." << std::endl;
			}

			return Vector3(1.0);
		}

		void Stage::addLightSource(const LightSource& light_source)
		{
			light_sources_.push_back(light_source);
		}

		void Stage::removeLightSource(const LightSource& light_source)
		{
			list<LightSource>::iterator it = light_sources_.begin();
			for (; it != light_sources_.end(); it++)
			{
				if (&*it == &light_source) 
				{
					light_sources_.erase(it);
					return;
				}
			}
		}

		void Stage::clearLightSources()
		{
			light_sources_.clear();
		}

	} // namespace VIEW
} // namespace BALL