File: lights.cpp

package info (click to toggle)
k3d 0.4.3.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 51,716 kB
  • ctags: 81,610
  • sloc: cpp: 283,698; ansic: 64,095; xml: 61,533; sh: 9,026; makefile: 5,282; python: 431; perl: 308; awk: 130
file content (506 lines) | stat: -rw-r--r-- 18,014 bytes parent folder | download
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// K-3D
// Copyright (c) 1995-2004, Timothy M. Shead
//
// Contact: tshead@k-3d.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

/** \file
		\author Tim Shead <tshead@k-3d.com>
*/

#include "ilight.h"

#include <k3dsdk/color.h>
#include <k3dsdk/object.h>
#include <k3dsdk/persistence.h>
#include <k3dsdk/module.h>
#include <k3dsdk/transformable.h>
#include <k3dsdk/vectors.h>
#include <k3dsdk/viewport.h>

#include <sdpgl/sdpgl.h>

namespace libk3dyafray
{

/////////////////////////////////////////////////////////////////////////////
// light

template<typename base_t>
class light :
	public base_t,
	public ilight
{
public:
	light(k3d::idocument& Document) :
		base_t(Document),
		m_emit(k3d::init_name("emit") + k3d::init_description("Emit Light [boolean]") + k3d::init_value(true) + k3d::init_document(Document))
	{
		base_t::enable_serialization(k3d::persistence::proxy(m_emit));

		base_t::register_property(m_emit);
	}

protected:
	k3d_data_property(bool, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_emit;
};

/////////////////////////////////////////////////////////////////////////////
// colored_light

template<typename base_t>
class colored_light :
	public base_t
{
public:
	colored_light(k3d::idocument& Document) :
		base_t(Document),
		m_color(k3d::init_name("color") + k3d::init_description("Light color [object]") + k3d::init_value(k3d::color(1, 1, 1)) + k3d::init_document(Document))
	{
		base_t::enable_serialization(k3d::persistence::proxy(m_color));
		base_t::register_property(m_color);
	}

protected:
	k3d_data_property(k3d::color, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_color;
};

/////////////////////////////////////////////////////////////////////////////
// optional_shadows

template<typename base_t>
class optional_shadows :
	public base_t
{
public:
	optional_shadows(k3d::idocument& Document) :
		base_t(Document),
		m_cast_shadows(k3d::init_name("cast_shadows") + k3d::init_description("Cast shadows [boolean]") + k3d::init_value(true) + k3d::init_document(Document))
	{
		base_t::enable_serialization(k3d::persistence::proxy(m_cast_shadows));

		base_t::register_property(m_cast_shadows);
	}

protected:
	k3d_data_property(bool, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_cast_shadows;
};

/////////////////////////////////////////////////////////////////////////////
// drawable_light

template<typename base_t>
class drawable_light :
	public k3d::viewport::drawable<base_t>
{
	typedef k3d::viewport::drawable<base_t> base;

public:
	drawable_light(k3d::idocument& Document) :
		base(Document)
	{
	}

	void on_viewport_draw(const k3d::viewport::render_state& State)
	{
		sdpgl::store_attributes attributes();

		const bool emitting = base_t::m_emit.property_value();

		glDisable(GL_LIGHTING);
		glDisable(GL_TEXTURE_1D);
		glDisable(GL_TEXTURE_2D);

		if(emitting)
			glColor3d(1, 1, 1);
		else
			glColor3d(0, 0, 0);

		glLineWidth(1.0f);
		glDisable(GL_LINE_STIPPLE);

		draw_geometry();
	}

	void on_viewport_select(const k3d::viewport::render_state& State)
	{
		sdpgl::store_attributes attributes();
		draw_geometry();
	}

private:
	void draw_geometry()
	{
		glBegin(GL_LINES);

		k3d::vector3 coords(0, 0, 0);

		glVertex3d(coords[0] + 1.0, coords[1], coords[2]);
		glVertex3d(coords[0] - 1.0, coords[1], coords[2]);
		glVertex3d(coords[0], coords[1] + 1.0, coords[2]);
		glVertex3d(coords[0], coords[1] - 1.0, coords[2]);
		glVertex3d(coords[0], coords[1], coords[2] + 1.0);
		glVertex3d(coords[0], coords[1], coords[2] - 1.0);

		glVertex3d(coords[0] + 0.4, coords[1] + 0.4, coords[2] + 0.4);
		glVertex3d(coords[0] - 0.4, coords[1] - 0.4, coords[2] - 0.4);
		glVertex3d(coords[0] - 0.4, coords[1] + 0.4, coords[2] + 0.4);
		glVertex3d(coords[0] + 0.4, coords[1] - 0.4, coords[2] - 0.4);
		glVertex3d(coords[0] + 0.4, coords[1] + 0.4, coords[2] - 0.4);
		glVertex3d(coords[0] - 0.4, coords[1] - 0.4, coords[2] + 0.4);
		glVertex3d(coords[0] - 0.4, coords[1] + 0.4, coords[2] - 0.4);
		glVertex3d(coords[0] + 0.4, coords[1] - 0.4, coords[2] + 0.4);

		glEnd();
	}
};

/////////////////////////////////////////////////////////////////////////////
// point_light

class point_light :
	public drawable_light<optional_shadows<colored_light<light<k3d::transformable<k3d::persistent<k3d::object> > > > > >
{
	typedef drawable_light<optional_shadows<colored_light<light<k3d::transformable<k3d::persistent<k3d::object> > > > > > base;

public:
	point_light(k3d::idocument& Document) :
		base(Document),
		m_power(k3d::init_name("power") + k3d::init_description("Power [number]") + k3d::init_value(400.0) + k3d::init_document(Document))
	{
		enable_serialization(k3d::persistence::proxy(m_power));
		register_property(m_power);
	}

	void setup_light(std::ostream& Stream)
	{
		if(!m_emit.property_value())
			return;

		const k3d::vector3 position = k3d::object_to_world_matrix(*this) * k3d::vector3(0, 0, 0);
		const k3d::color color = m_color.property_value();

		Stream << "<light type=\"pointlight\" name=\"" << name() << "\" power=\"" << m_power.property_value() << "\" cast_shadows=\"" << (m_cast_shadows.property_value() ? "yes" : "no") << "\">" << std::endl;
		Stream << "	<from x=\"" << std::fixed << -position[0] << "\" y=\"" << std::fixed << position[1] << "\" z=\"" << std::fixed << position[2] << "\"/>" << std::endl;
		Stream << "	<color r=\"" << color.red << "\" g=\"" << color.green << "\" b=\"" << color.blue << "\"/>" << std::endl;
		Stream << "</light>" << std::endl;
	}

	k3d::iplugin_factory& factory()
	{
		return get_factory();
	}

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::plugin_factory<
			k3d::document_plugin<point_light>,
			k3d::interface_list<k3d::itransform_source,
			k3d::interface_list<k3d::itransform_sink > > > factory(
			k3d::uuid(0xd693bb64, 0xd73943ce, 0x80852061, 0x24fd242e),
			"YafRayPointLight",
			"YafRay Point Light",
			"Objects",
			k3d::iplugin_factory::STABLE);

		return factory;
	}

private:
	k3d_data_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_power;
};

/////////////////////////////////////////////////////////////////////////////
// sun_light

class sun_light :
	public drawable_light<optional_shadows<colored_light<light<k3d::transformable<k3d::persistent<k3d::object> > > > > >
{
	typedef drawable_light<optional_shadows<colored_light<light<k3d::transformable<k3d::persistent<k3d::object> > > > > > base;

public:
	sun_light(k3d::idocument& Document) :
		base(Document),
		m_power(k3d::init_name("power") + k3d::init_description("Power [number]") + k3d::init_value(40.0) + k3d::init_document(Document))
	{
		enable_serialization(k3d::persistence::proxy(m_power));
		register_property(m_power);
	}

	void setup_light(std::ostream& Stream)
	{
		if(!m_emit.property_value())
			return;

		const k3d::vector3 position = k3d::object_to_world_matrix(*this) * k3d::vector3(0, 0, 0);
		const k3d::color color = m_color.property_value();

		Stream << "<light type=\"sunlight\" name=\"" << name() << "\" power=\"" << m_power.property_value() << "\" cast_shadows=\"" << (m_cast_shadows.property_value() ? "yes" : "no") << "\">" << std::endl;
		Stream << "	<from x=\"" << std::fixed << -position[0] << "\" y=\"" << std::fixed << position[1] << "\" z=\"" << std::fixed << position[2] << "\"/>" << std::endl;
		Stream << "	<color r=\"" << color.red << "\" g=\"" << color.green << "\" b=\"" << color.blue << "\"/>" << std::endl;
		Stream << "</light>" << std::endl;
	}

	k3d::iplugin_factory& factory()
	{
		return get_factory();
	}

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::plugin_factory<
			k3d::document_plugin<sun_light>,
			k3d::interface_list<k3d::itransform_source,
			k3d::interface_list<k3d::itransform_sink > > > factory(
			k3d::uuid(0x9a48777d, 0xa68345b5, 0xacb9fc07, 0x8af3e7f6),
			"YafRaySunLight",
			"YafRay Sun Light",
			"Objects",
			k3d::iplugin_factory::STABLE);

		return factory;
	}

private:
	k3d_data_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_power;
};

/////////////////////////////////////////////////////////////////////////////
// soft_light

class soft_light :
	public drawable_light<colored_light<light<k3d::transformable<k3d::persistent<k3d::object> > > > >
{
	typedef drawable_light<colored_light<light<k3d::transformable<k3d::persistent<k3d::object> > > > > base;

public:
	soft_light(k3d::idocument& Document) :
		base(Document),
		m_power(k3d::init_name("power") + k3d::init_description("Power [number]") + k3d::init_value(400.0) + k3d::init_document(Document)),
		m_radius(k3d::init_name("radius") + k3d::init_description("Softness [number]") + k3d::init_value(5.0) + k3d::init_document(Document)),
		m_resolution(k3d::init_name("resolution") + k3d::init_description("Shadowmap resolution [positive integer]") + k3d::init_value(256) + k3d::init_document(Document) + k3d::init_constraint(k3d::constraint::minimum(0UL, k3d::constraint::maximum(4096UL)))),
		m_bias(k3d::init_name("bias") + k3d::init_description("Shadowmap bias [number]") + k3d::init_value(0.01) + k3d::init_document(Document))
	{
		enable_serialization(k3d::persistence::proxy(m_power));
		enable_serialization(k3d::persistence::proxy(m_radius));
		enable_serialization(k3d::persistence::proxy(m_resolution));
		enable_serialization(k3d::persistence::proxy(m_bias));

		register_property(m_power);
		register_property(m_radius);
		register_property(m_resolution);
		register_property(m_bias);
	}

	void setup_light(std::ostream& Stream)
	{
		if(!m_emit.property_value())
			return;

		const k3d::vector3 position = k3d::object_to_world_matrix(*this) * k3d::vector3(0, 0, 0);
		const k3d::color color = m_color.property_value();

		Stream << "<light type=\"softlight\" name=\"" << name() << "\" power=\"" << m_power.property_value() << "\" res=\"" << m_resolution.property_value()  << "\" radius=\"" << m_radius.property_value() << "\" bias=\"" << m_bias.property_value() << "\">" << std::endl;
		Stream << "	<from x=\"" << std::fixed << -position[0] << "\" y=\"" << std::fixed << position[1] << "\" z=\"" << std::fixed << position[2] << "\"/>" << std::endl;
		Stream << "	<color r=\"" << color.red << "\" g=\"" << color.green << "\" b=\"" << color.blue << "\"/>" << std::endl;
		Stream << "</light>" << std::endl;
	}

	k3d::iplugin_factory& factory()
	{
		return get_factory();
	}

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::plugin_factory<
			k3d::document_plugin<soft_light>,
			k3d::interface_list<k3d::itransform_source,
			k3d::interface_list<k3d::itransform_sink > > > factory(
			k3d::uuid(0x2fcaffb5, 0xed294a0d, 0x82133a8f, 0x48df4988),
			"YafRaySoftLight",
			"YafRay Soft Light",
			"Objects",
			k3d::iplugin_factory::STABLE);

		return factory;
	}

private:
	k3d_data_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_power;
	k3d_data_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_radius;
	k3d_data_property(unsigned long, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::with_constraint) m_resolution;
	k3d_data_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_bias;
};

/////////////////////////////////////////////////////////////////////////////
// spot_light

class spot_light :
	public drawable_light<colored_light<light<k3d::transformable<k3d::persistent<k3d::object> > > > >
{
	typedef drawable_light<colored_light<light<k3d::transformable<k3d::persistent<k3d::object> > > > > base;

public:
	spot_light(k3d::idocument& Document) :
		base(Document),
		m_power(k3d::init_name("power") + k3d::init_description("Power [number]") + k3d::init_value(400.0) + k3d::init_document(Document)),
		m_size(k3d::init_name("size") + k3d::init_description("Angle [number]") + k3d::init_value(k3d::radians(30.0)) + k3d::init_document(Document)),
		m_nblend(k3d::init_name("nblend") + k3d::init_description("Edge softness [number]") + k3d::init_value(5.0) + k3d::init_document(Document)),
		m_beam_falloff(k3d::init_name("beam_falloff") + k3d::init_description("Beam falloff [number]") + k3d::init_value(10.0) + k3d::init_document(Document))
	{
		enable_serialization(k3d::persistence::proxy(m_power));
		enable_serialization(k3d::persistence::proxy(m_size));
		enable_serialization(k3d::persistence::proxy(m_nblend));
		enable_serialization(k3d::persistence::proxy(m_beam_falloff));

		register_property(m_power);
		register_property(m_size);
		register_property(m_nblend);
		register_property(m_beam_falloff);
	}

	void setup_light(std::ostream& Stream)
	{
		if(!m_emit.property_value())
			return;

		const k3d::vector3 position = k3d::object_to_world_matrix(*this) * k3d::vector3(0, 0, 0);
		const k3d::color color = m_color.property_value();
		const double power = m_power.property_value();
		const double size = k3d::degrees(m_size.property_value());
		const double nblend = m_nblend.property_value();
		const double beam_falloff = m_beam_falloff.property_value();

		Stream << "<light type=\"spotlight\" name=\"" << name() << "\" power=\"" << power << "\" size=\"" << size << "\" nblend=\"" << nblend << "\" beam_falloff=\"" << beam_falloff << "\">" << std::endl;
		Stream << "	<from x=\"" << std::fixed << -position[0] << "\" y=\"" << std::fixed << position[1] << "\" z=\"" << std::fixed << position[2] << "\"/>" << std::endl;
		Stream << "	<color r=\"" << color.red << "\" g=\"" << color.green << "\" b=\"" << color.blue << "\"/>" << std::endl;
		Stream << "</light>" << std::endl;
	}

	k3d::iplugin_factory& factory()
	{
		return get_factory();
	}

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::plugin_factory<
			k3d::document_plugin<spot_light>,
			k3d::interface_list<k3d::itransform_source,
			k3d::interface_list<k3d::itransform_sink > > > factory(
			k3d::uuid(0x5e363371, 0xf8464895, 0x99f0ddf0, 0x4e26ee4a),
			"YafRaySpotLight",
			"YafRay Spot Light",
			"Objects",
			k3d::iplugin_factory::STABLE);

		return factory;
	}

private:
	k3d_data_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_power;
	k3d_data_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_size;
	k3d_data_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_nblend;
	k3d_data_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_beam_falloff;
};

/////////////////////////////////////////////////////////////////////////////
// hemi_light

class hemi_light :
	public colored_light<light<k3d::persistent<k3d::object> > >
{
	typedef colored_light<light<k3d::persistent<k3d::object> > > base;

public:
	hemi_light(k3d::idocument& Document) :
		base(Document),
		m_power(k3d::init_name("power") + k3d::init_description("Power [number]") + k3d::init_value(20.0) + k3d::init_document(Document)),
		m_samples(k3d::init_name("samples") + k3d::init_description("Samples [positive integer]") + k3d::init_value(256) + k3d::init_document(Document) + k3d::init_constraint(k3d::constraint::minimum(0UL, k3d::constraint::maximum(1024UL))))
	{
		enable_serialization(k3d::persistence::proxy(m_power));
		enable_serialization(k3d::persistence::proxy(m_samples));

		register_property(m_power);
		register_property(m_samples);

		// We override the default white color ...
		m_color.value() = k3d::color(0.9, 0.9, 1.0);
	}

	void setup_light(std::ostream& Stream)
	{
		if(!m_emit.property_value())
			return;

		const k3d::color color = m_color.property_value();

		Stream << "<light type=\"hemilight\" name=\"" << name() << "\" power=\"" << m_power.property_value() << "\" samples=\"" << m_samples.property_value() << "\">" << std::endl;
		Stream << "	<color r=\"" << color.red << "\" g=\"" << color.green << "\" b=\"" << color.blue << "\"/>" << std::endl;
		Stream << "</light>" << std::endl;
	}

	k3d::iplugin_factory& factory()
	{
		return get_factory();
	}

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::plugin_factory<k3d::document_plugin<hemi_light> > factory(
			k3d::uuid(0xa0661dc7, 0x52cd4990, 0x8e6a0aa8, 0x87bdd89d),
			"YafRayHemiLight",
			"YafRay Hemi Light",
			"Objects",
			k3d::iplugin_factory::STABLE);

		return factory;
	}

private:
	k3d_data_property(double, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::no_constraint) m_power;
	k3d_data_property(unsigned long, k3d::immutable_name, k3d::change_signal, k3d::with_undo, k3d::local_storage, k3d::with_constraint) m_samples;
};

k3d::iplugin_factory& point_light_factory()
{
	return point_light::get_factory();
}

k3d::iplugin_factory& sun_light_factory()
{
	return sun_light::get_factory();
}

k3d::iplugin_factory& soft_light_factory()
{
	return soft_light::get_factory();
}

k3d::iplugin_factory& spot_light_factory()
{
	return spot_light::get_factory();
}

k3d::iplugin_factory& hemi_light_factory()
{
	return hemi_light::get_factory();
}

} // namespace libk3dyafray