2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 ///\brief Represents any light entity (e.g. light).
25 /// This entity dislays a special 'light' model.
26 /// The "origin" key directly controls the position of the light model in local space.
27 /// The "_color" key controls the colour of the light model.
28 /// The "light" key is visualised with a sphere representing the approximate coverage of the light (except Doom3).
29 /// Doom3 special behaviour:
30 /// The entity behaves as a group.
31 /// The "origin" key is the translation to be applied to all brushes (not patches) grouped under this entity.
32 /// The "light_center" and "light_radius" keys are visualised with a point and a box when the light is selected.
33 /// The "rotation" key directly controls the orientation of the light bounding box in local space.
34 /// The "light_origin" key controls the position of the light independently of the "origin" key if it is specified.
35 /// The "light_rotation" key duplicates the behaviour of the "rotation" key if it is specified. This appears to be an unfinished feature in Doom3.
42 #include "renderable.h"
45 #include "math/frustum.h"
46 #include "selectionlib.h"
47 #include "instancelib.h"
48 #include "transformlib.h"
49 #include "entitylib.h"
51 #include "eclasslib.h"
54 #include "traverselib.h"
55 #include "dragplanes.h"
57 #include "targetable.h"
61 #include "namedentity.h"
62 #include "keyobservers.h"
67 extern bool g_newLightDraw;
70 void sphere_draw_fill(const Vector3& origin, float radius, int sides)
75 const double dt = c_2pi / static_cast<double>(sides);
76 const double dp = c_pi / static_cast<double>(sides);
78 glBegin(GL_TRIANGLES);
79 for (int i = 0; i <= sides - 1; ++i)
81 for (int j = 0; j <= sides - 2; ++j)
83 const double t = i * dt;
84 const double p = (j * dp) - (c_pi / 2.0);
87 Vector3 v(vector3_added(origin, vector3_scaled(vector3_for_spherical(t, p), radius)));
88 glVertex3fv(vector3_to_array(v));
92 Vector3 v(vector3_added(origin, vector3_scaled(vector3_for_spherical(t, p + dp), radius)));
93 glVertex3fv(vector3_to_array(v));
97 Vector3 v(vector3_added(origin, vector3_scaled(vector3_for_spherical(t + dt, p + dp), radius)));
98 glVertex3fv(vector3_to_array(v));
102 Vector3 v(vector3_added(origin, vector3_scaled(vector3_for_spherical(t, p), radius)));
103 glVertex3fv(vector3_to_array(v));
107 Vector3 v(vector3_added(origin, vector3_scaled(vector3_for_spherical(t + dt, p + dp), radius)));
108 glVertex3fv(vector3_to_array(v));
112 Vector3 v(vector3_added(origin, vector3_scaled(vector3_for_spherical(t + dt, p), radius)));
113 glVertex3fv(vector3_to_array(v));
119 const double p = (sides - 1) * dp - (c_pi / 2.0);
120 for (int i = 0; i <= sides - 1; ++i)
122 const double t = i * dt;
125 Vector3 v(vector3_added(origin, vector3_scaled(vector3_for_spherical(t, p), radius)));
126 glVertex3fv(vector3_to_array(v));
130 Vector3 v(vector3_added(origin, vector3_scaled(vector3_for_spherical(t + dt, p + dp), radius)));
131 glVertex3fv(vector3_to_array(v));
135 Vector3 v(vector3_added(origin, vector3_scaled(vector3_for_spherical(t + dt, p), radius)));
136 glVertex3fv(vector3_to_array(v));
143 void sphere_draw_wire(const Vector3& origin, float radius, int sides)
146 glBegin(GL_LINE_LOOP);
148 for (int i = 0; i <= sides; i++)
150 double ds = sin((i * 2 * c_pi) / sides);
151 double dc = cos((i * 2 * c_pi) / sides);
154 static_cast<float>(origin[0] + radius * dc),
155 static_cast<float>(origin[1] + radius * ds),
164 glBegin(GL_LINE_LOOP);
166 for (int i = 0; i <= sides; i++)
168 double ds = sin((i * 2 * c_pi) / sides);
169 double dc = cos((i * 2 * c_pi) / sides);
172 static_cast<float>(origin[0] + radius * dc),
174 static_cast<float>(origin[2] + radius * ds)
182 glBegin(GL_LINE_LOOP);
184 for (int i = 0; i <= sides; i++)
186 double ds = sin((i * 2 * c_pi) / sides);
187 double dc = cos((i * 2 * c_pi) / sides);
191 static_cast<float>(origin[1] + radius * dc),
192 static_cast<float>(origin[2] + radius * ds)
200 void light_draw_box_lines(const Vector3& origin, const Vector3 points[8])
202 //draw lines from the center of the bbox to the corners
205 glVertex3fv(vector3_to_array(origin));
206 glVertex3fv(vector3_to_array(points[1]));
208 glVertex3fv(vector3_to_array(origin));
209 glVertex3fv(vector3_to_array(points[5]));
211 glVertex3fv(vector3_to_array(origin));
212 glVertex3fv(vector3_to_array(points[2]));
214 glVertex3fv(vector3_to_array(origin));
215 glVertex3fv(vector3_to_array(points[6]));
217 glVertex3fv(vector3_to_array(origin));
218 glVertex3fv(vector3_to_array(points[0]));
220 glVertex3fv(vector3_to_array(origin));
221 glVertex3fv(vector3_to_array(points[4]));
223 glVertex3fv(vector3_to_array(origin));
224 glVertex3fv(vector3_to_array(points[3]));
226 glVertex3fv(vector3_to_array(origin));
227 glVertex3fv(vector3_to_array(points[7]));
232 void light_draw_radius_wire(const Vector3& origin, const float envelope[3])
235 sphere_draw_wire(origin, envelope[0], 24);
237 sphere_draw_wire(origin, envelope[1], 24);
239 sphere_draw_wire(origin, envelope[2], 24);
242 void light_draw_radius_fill(const Vector3& origin, const float envelope[3])
245 sphere_draw_fill(origin, envelope[0], 16);
247 sphere_draw_fill(origin, envelope[1], 16);
249 sphere_draw_fill(origin, envelope[2], 16);
252 void light_vertices(const AABB& aabb_light, Vector3 points[6])
254 Vector3 max(vector3_added(aabb_light.origin, aabb_light.extents));
255 Vector3 min(vector3_subtracted(aabb_light.origin, aabb_light.extents));
256 Vector3 mid(aabb_light.origin);
258 // top, bottom, tleft, tright, bright, bleft
259 points[0] = Vector3(mid[0], mid[1], max[2]);
260 points[1] = Vector3(mid[0], mid[1], min[2]);
261 points[2] = Vector3(min[0], max[1], mid[2]);
262 points[3] = Vector3(max[0], max[1], mid[2]);
263 points[4] = Vector3(max[0], min[1], mid[2]);
264 points[5] = Vector3(min[0], min[1], mid[2]);
267 void light_draw(const AABB& aabb_light, RenderStateFlags state)
270 light_vertices(aabb_light, points);
272 if(state & RENDER_LIGHTING)
274 const float f = 0.70710678f;
275 // North, East, South, West
276 const Vector3 normals[8] = {
287 #if !defined(USE_TRIANGLE_FAN)
288 glBegin(GL_TRIANGLES);
290 glBegin(GL_TRIANGLE_FAN);
292 glVertex3fv(vector3_to_array(points[0]));
293 glVertex3fv(vector3_to_array(points[2]));
294 glNormal3fv(vector3_to_array(normals[0]));
295 glVertex3fv(vector3_to_array(points[3]));
297 #if !defined(USE_TRIANGLE_FAN)
298 glVertex3fv(vector3_to_array(points[0]));
299 glVertex3fv(vector3_to_array(points[3]));
301 glNormal3fv(vector3_to_array(normals[1]));
302 glVertex3fv(vector3_to_array(points[4]));
304 #if !defined(USE_TRIANGLE_FAN)
305 glVertex3fv(vector3_to_array(points[0]));
306 glVertex3fv(vector3_to_array(points[4]));
308 glNormal3fv(vector3_to_array(normals[2]));
309 glVertex3fv(vector3_to_array(points[5]));
310 #if !defined(USE_TRIANGLE_FAN)
311 glVertex3fv(vector3_to_array(points[0]));
312 glVertex3fv(vector3_to_array(points[5]));
314 glNormal3fv(vector3_to_array(normals[3]));
315 glVertex3fv(vector3_to_array(points[2]));
316 #if defined(USE_TRIANGLE_FAN)
318 glBegin(GL_TRIANGLE_FAN);
321 glVertex3fv(vector3_to_array(points[1]));
322 glVertex3fv(vector3_to_array(points[2]));
323 glNormal3fv(vector3_to_array(normals[7]));
324 glVertex3fv(vector3_to_array(points[5]));
326 #if !defined(USE_TRIANGLE_FAN)
327 glVertex3fv(vector3_to_array(points[1]));
328 glVertex3fv(vector3_to_array(points[5]));
330 glNormal3fv(vector3_to_array(normals[6]));
331 glVertex3fv(vector3_to_array(points[4]));
333 #if !defined(USE_TRIANGLE_FAN)
334 glVertex3fv(vector3_to_array(points[1]));
335 glVertex3fv(vector3_to_array(points[4]));
337 glNormal3fv(vector3_to_array(normals[5]));
338 glVertex3fv(vector3_to_array(points[3]));
340 #if !defined(USE_TRIANGLE_FAN)
341 glVertex3fv(vector3_to_array(points[1]));
342 glVertex3fv(vector3_to_array(points[3]));
344 glNormal3fv(vector3_to_array(normals[4]));
345 glVertex3fv(vector3_to_array(points[2]));
351 typedef unsigned int index_t;
352 const index_t indices[24] = {
363 glVertexPointer(3, GL_FLOAT, 0, points);
364 glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(index_t), RenderIndexTypeID, indices);
366 glBegin(GL_TRIANGLES);
367 for(unsigned int i = 0; i < sizeof(indices)/sizeof(index_t); ++i)
369 glVertex3fv(points[indices[i]]);
376 // NOTE: prolly not relevant until some time..
377 // check for DOOM lights
379 if (strlen(ValueForKey(e, "light_right")) > 0) {
380 vec3_t vRight, vUp, vTarget, vTemp;
381 GetVectorForKey (e, "light_right", vRight);
382 GetVectorForKey (e, "light_up", vUp);
383 GetVectorForKey (e, "light_target", vTarget);
386 glBegin(GL_LINE_LOOP);
387 VectorAdd(vTarget, e->origin, vTemp);
388 VectorAdd(vTemp, vRight, vTemp);
389 VectorAdd(vTemp, vUp, vTemp);
390 glVertex3fv(e->origin);
392 VectorAdd(vTarget, e->origin, vTemp);
393 VectorAdd(vTemp, vUp, vTemp);
394 VectorSubtract(vTemp, vRight, vTemp);
395 glVertex3fv(e->origin);
397 VectorAdd(vTarget, e->origin, vTemp);
398 VectorAdd(vTemp, vRight, vTemp);
399 VectorSubtract(vTemp, vUp, vTemp);
400 glVertex3fv(e->origin);
402 VectorAdd(vTarget, e->origin, vTemp);
403 VectorSubtract(vTemp, vUp, vTemp);
404 VectorSubtract(vTemp, vRight, vTemp);
405 glVertex3fv(e->origin);
413 // These variables are tweakable on the q3map2 console, setting to q3map2
414 // default here as there is no way to find out what the user actually uses
415 // right now. Maybe move them to worldspawn?
416 float fPointScale = 7500.f;
417 float fLinearScale = 1.f / 8000.f;
419 float light_radius_linear(float fIntensity, float fFalloffTolerance)
421 return ((fIntensity * fPointScale * fLinearScale) - fFalloffTolerance);
424 float light_radius(float fIntensity, float fFalloffTolerance)
426 return sqrt(fIntensity * fPointScale / fFalloffTolerance);
430 LightType g_lightType = LIGHTTYPE_DEFAULT;
433 bool spawnflags_linear(int flags)
435 if( g_lightType == LIGHTTYPE_RTCW )
459 float m_primaryIntensity;
460 float m_secondaryIntensity;
465 void calculateRadii()
467 float intensity = 300.0f;
469 if(m_primaryIntensity != 0.0f)
471 intensity = m_primaryIntensity;
473 else if(m_secondaryIntensity != 0.0f)
475 intensity = m_secondaryIntensity;
478 intensity *= m_scale;
480 if(spawnflags_linear(m_flags))
482 m_radii[0] = light_radius_linear(intensity, 1.0f) / m_fade;
483 m_radii[1] = light_radius_linear(intensity, 48.0f) / m_fade;
484 m_radii[2] = light_radius_linear(intensity, 255.0f) / m_fade;
488 m_radii[0] = light_radius(intensity, 1.0f);
489 m_radii[1] = light_radius(intensity, 48.0f);
490 m_radii[2] = light_radius(intensity, 255.0f);
495 LightRadii() : m_primaryIntensity(0), m_secondaryIntensity(0), m_flags(0), m_fade(1), m_scale(1)
500 void primaryIntensityChanged(const char* value)
502 m_primaryIntensity = string_read_float(value);
505 typedef MemberCaller1<LightRadii, const char*, &LightRadii::primaryIntensityChanged> PrimaryIntensityChangedCaller;
506 void secondaryIntensityChanged(const char* value)
508 m_secondaryIntensity = string_read_float(value);
511 typedef MemberCaller1<LightRadii, const char*, &LightRadii::secondaryIntensityChanged> SecondaryIntensityChangedCaller;
512 void scaleChanged(const char* value)
514 m_scale = string_read_float(value);
521 typedef MemberCaller1<LightRadii, const char*, &LightRadii::scaleChanged> ScaleChangedCaller;
522 void fadeChanged(const char* value)
524 m_fade = string_read_float(value);
531 typedef MemberCaller1<LightRadii, const char*, &LightRadii::fadeChanged> FadeChangedCaller;
532 void flagsChanged(const char* value)
534 m_flags = string_read_int(value);
537 typedef MemberCaller1<LightRadii, const char*, &LightRadii::flagsChanged> FlagsChangedCaller;
540 const Vector3 c_defaultDoom3LightRadius = Vector3(300, 300, 300);
541 class Doom3LightRadius
545 Vector3 m_radiusTransformed;
550 Doom3LightRadius() : m_radius(c_defaultDoom3LightRadius), m_center(0, 0, 0), m_useCenterKey(false)
554 void lightRadiusChanged(const char* value)
556 if(!string_parse_vector3(value, m_radius))
558 m_radius = c_defaultDoom3LightRadius;
560 m_radiusTransformed = m_radius;
564 typedef MemberCaller1<Doom3LightRadius, const char*, &Doom3LightRadius::lightRadiusChanged> LightRadiusChangedCaller;
566 void lightCenterChanged(const char* value)
568 m_useCenterKey = string_parse_vector3(value, m_center);
571 m_center = Vector3(0, 0, 0);
575 typedef MemberCaller1<Doom3LightRadius, const char*, &Doom3LightRadius::lightCenterChanged> LightCenterChangedCaller;
578 class RenderLightRadiiWire : public OpenGLRenderable
581 const Vector3& m_origin;
583 RenderLightRadiiWire(LightRadii& radii, const Vector3& origin) : m_radii(radii), m_origin(origin)
586 void render(RenderStateFlags state) const
588 light_draw_radius_wire(m_origin, m_radii.m_radii);
592 class RenderLightRadiiFill : public OpenGLRenderable
595 const Vector3& m_origin;
597 static Shader* m_state;
599 RenderLightRadiiFill(LightRadii& radii, const Vector3& origin) : m_radii(radii), m_origin(origin)
602 void render(RenderStateFlags state) const
604 light_draw_radius_fill(m_origin, m_radii.m_radii);
608 class RenderLightRadiiBox : public OpenGLRenderable
610 const Vector3& m_origin;
612 mutable Vector3 m_points[8];
613 static Shader* m_state;
615 RenderLightRadiiBox(const Vector3& origin) : m_origin(origin)
618 void render(RenderStateFlags state) const
620 //draw the bounding box of light based on light_radius key
621 if((state & RENDER_FILL) != 0)
623 aabb_draw_flatshade(m_points);
627 aabb_draw_wire(m_points);
630 #if 1 //disable if you dont want lines going from the center of the light bbox to the corners
631 light_draw_box_lines(m_origin, m_points);
636 Shader* RenderLightRadiiFill::m_state = 0;
638 class RenderLightCenter : public OpenGLRenderable
640 const Vector3& m_center;
641 EntityClass& m_eclass;
643 static Shader* m_state;
645 RenderLightCenter(const Vector3& center, EntityClass& eclass) : m_center(center), m_eclass(eclass)
648 void render(RenderStateFlags state) const
651 glColor3fv(vector3_to_array(m_eclass.color));
652 glVertex3fv(vector3_to_array(m_center));
657 Shader* RenderLightCenter::m_state = 0;
659 class RenderLightProjection : public OpenGLRenderable
661 const Matrix4& m_projection;
664 RenderLightProjection(const Matrix4& projection) : m_projection(projection)
667 void render(RenderStateFlags state) const
669 Matrix4 unproject(matrix4_full_inverse(m_projection));
671 aabb_corners(AABB(Vector3(0.5f, 0.5f, 0.5f), Vector3(0.5f, 0.5f, 0.5f)), points);
672 points[0] = vector4_projected(matrix4_transformed_vector4(unproject, Vector4(points[0], 1)));
673 points[1] = vector4_projected(matrix4_transformed_vector4(unproject, Vector4(points[1], 1)));
674 points[2] = vector4_projected(matrix4_transformed_vector4(unproject, Vector4(points[2], 1)));
675 points[3] = vector4_projected(matrix4_transformed_vector4(unproject, Vector4(points[3], 1)));
676 points[4] = vector4_projected(matrix4_transformed_vector4(unproject, Vector4(points[4], 1)));
677 points[5] = vector4_projected(matrix4_transformed_vector4(unproject, Vector4(points[5], 1)));
678 points[6] = vector4_projected(matrix4_transformed_vector4(unproject, Vector4(points[6], 1)));
679 points[7] = vector4_projected(matrix4_transformed_vector4(unproject, Vector4(points[7], 1)));
680 Vector4 test1 = matrix4_transformed_vector4(unproject, Vector4(0.5f, 0.5f, 0.5f, 1));
681 Vector3 test2 = vector4_projected(test1);
682 aabb_draw_wire(points);
686 inline void default_extents(Vector3& extents)
688 extents = Vector3(8, 8, 8);
697 m_shader = GlobalShaderCache().capture(m_name.c_str());
701 GlobalShaderCache().release(m_name.c_str());
712 void setName(const char* name)
729 m_shader.setName(m_defaultShader);
732 static const char* m_defaultShader;
738 void valueChanged(const char* value)
740 if(string_empty(value))
746 m_shader.setName(value);
750 typedef MemberCaller1<LightShader, const char*, &LightShader::valueChanged> ValueChangedCaller;
754 return m_shader.get();
758 const char* LightShader::m_defaultShader = "";
760 inline const BasicVector4<double>& plane3_to_vector4(const Plane3& self)
762 return reinterpret_cast<const BasicVector4<double>&>(self);
765 inline BasicVector4<double>& plane3_to_vector4(Plane3& self)
767 return reinterpret_cast<BasicVector4<double>&>(self);
770 inline Matrix4 matrix4_from_planes(const Plane3& left, const Plane3& right, const Plane3& bottom, const Plane3& top, const Plane3& front, const Plane3& back)
773 (right.a - left.a) / 2,
774 (top.a - bottom.a) / 2,
775 (back.a - front.a) / 2,
776 right.a - (right.a - left.a) / 2,
777 (right.b - left.b) / 2,
778 (top.b - bottom.b) / 2,
779 (back.b - front.b) / 2,
780 right.b - (right.b - left.b) / 2,
781 (right.c - left.c) / 2,
782 (top.c - bottom.c) / 2,
783 (back.c - front.c) / 2,
784 right.c - (right.c - left.c) / 2,
785 (right.d - left.d) / 2,
786 (top.d - bottom.d) / 2,
787 (back.d - front.d) / 2,
788 right.d - (right.d - left.d) / 2
793 public OpenGLRenderable,
799 EntityKeyValues m_entity;
800 KeyObserverMap m_keyObservers;
801 TraversableNodeSet m_traverse;
802 IdentityTransform m_transform;
804 OriginKey m_originKey;
805 RotationKey m_rotationKey;
809 ClassnameFilter m_filter;
812 TraversableObserverPairRelay m_traverseObservers;
813 Doom3GroupOrigin m_funcStaticOrigin;
816 Doom3LightRadius m_doom3Radius;
818 RenderLightRadiiWire m_radii_wire;
819 RenderLightRadiiFill m_radii_fill;
820 RenderLightRadiiBox m_radii_box;
821 RenderLightCenter m_render_center;
822 RenderableNamedEntity m_renderName;
824 Vector3 m_lightOrigin;
825 bool m_useLightOrigin;
826 Float9 m_lightRotation;
827 bool m_useLightRotation;
829 Vector3 m_lightTarget;
830 bool m_useLightTarget;
833 Vector3 m_lightRight;
834 bool m_useLightRight;
835 Vector3 m_lightStart;
836 bool m_useLightStart;
840 mutable AABB m_doom3AABB;
841 mutable Matrix4 m_doom3Rotation;
842 mutable Matrix4 m_doom3Projection;
843 mutable Frustum m_doom3Frustum;
844 mutable bool m_doom3ProjectionChanged;
846 RenderLightProjection m_renderProjection;
848 LightShader m_shader;
852 Callback m_transformChanged;
853 Callback m_boundsChanged;
854 Callback m_evaluateTransform;
858 default_rotation(m_rotation);
859 m_aabb_light.origin = Vector3(0, 0, 0);
860 default_extents(m_aabb_light.extents);
862 m_keyObservers.insert("classname", ClassnameFilter::ClassnameChangedCaller(m_filter));
863 m_keyObservers.insert(Static<KeyIsName>::instance().m_nameKey, NamedEntity::IdentifierChangedCaller(m_named));
864 m_keyObservers.insert("_color", Colour::ColourChangedCaller(m_colour));
865 m_keyObservers.insert("origin", OriginKey::OriginChangedCaller(m_originKey));
866 m_keyObservers.insert("_light", LightRadii::PrimaryIntensityChangedCaller(m_radii));
867 m_keyObservers.insert("light", LightRadii::SecondaryIntensityChangedCaller(m_radii));
868 m_keyObservers.insert("fade", LightRadii::FadeChangedCaller(m_radii));
869 m_keyObservers.insert("scale", LightRadii::ScaleChangedCaller(m_radii));
870 m_keyObservers.insert("spawnflags", LightRadii::FlagsChangedCaller(m_radii));
872 if(g_lightType == LIGHTTYPE_DOOM3)
874 m_keyObservers.insert("angle", RotationKey::AngleChangedCaller(m_rotationKey));
875 m_keyObservers.insert("rotation", RotationKey::RotationChangedCaller(m_rotationKey));
876 m_keyObservers.insert("light_radius", Doom3LightRadius::LightRadiusChangedCaller(m_doom3Radius));
877 m_keyObservers.insert("light_center", Doom3LightRadius::LightCenterChangedCaller(m_doom3Radius));
878 m_keyObservers.insert("light_origin", Light::LightOriginChangedCaller(*this));
879 m_keyObservers.insert("light_rotation", Light::LightRotationChangedCaller(*this));
880 m_keyObservers.insert("light_target", Light::LightTargetChangedCaller(*this));
881 m_keyObservers.insert("light_up", Light::LightUpChangedCaller(*this));
882 m_keyObservers.insert("light_right", Light::LightRightChangedCaller(*this));
883 m_keyObservers.insert("light_start", Light::LightStartChangedCaller(*this));
884 m_keyObservers.insert("light_end", Light::LightEndChangedCaller(*this));
885 m_keyObservers.insert("texture", LightShader::ValueChangedCaller(m_shader));
886 m_useLightTarget = m_useLightUp = m_useLightRight = m_useLightStart = m_useLightEnd = false;
887 m_doom3ProjectionChanged = true;
890 if(g_lightType == LIGHTTYPE_DOOM3)
892 m_traverse.attach(&m_traverseObservers);
893 m_traverseObservers.attach(m_funcStaticOrigin);
895 m_entity.m_isContainer = true;
900 if(g_lightType == LIGHTTYPE_DOOM3)
902 m_traverseObservers.detach(m_funcStaticOrigin);
903 m_traverse.detach(&m_traverseObservers);
911 if(g_lightType == LIGHTTYPE_DOOM3)
913 m_funcStaticOrigin.originChanged();
916 m_doom3Radius.m_changed();
918 GlobalSelectionSystem().pivotChanged();
923 m_aabb_light.origin = m_useLightOrigin ? m_lightOrigin : m_originKey.m_origin;
926 typedef MemberCaller<Light, &Light::originChanged> OriginChangedCaller;
928 void lightOriginChanged(const char* value)
930 m_useLightOrigin = !string_empty(value);
933 read_origin(m_lightOrigin, value);
937 typedef MemberCaller1<Light, const char*, &Light::lightOriginChanged> LightOriginChangedCaller;
939 void lightTargetChanged(const char* value)
941 m_useLightTarget = !string_empty(value);
944 read_origin(m_lightTarget, value);
948 typedef MemberCaller1<Light, const char*, &Light::lightTargetChanged> LightTargetChangedCaller;
949 void lightUpChanged(const char* value)
951 m_useLightUp = !string_empty(value);
954 read_origin(m_lightUp, value);
958 typedef MemberCaller1<Light, const char*, &Light::lightUpChanged> LightUpChangedCaller;
959 void lightRightChanged(const char* value)
961 m_useLightRight = !string_empty(value);
964 read_origin(m_lightRight, value);
968 typedef MemberCaller1<Light, const char*, &Light::lightRightChanged> LightRightChangedCaller;
969 void lightStartChanged(const char* value)
971 m_useLightStart = !string_empty(value);
974 read_origin(m_lightStart, value);
978 typedef MemberCaller1<Light, const char*, &Light::lightStartChanged> LightStartChangedCaller;
979 void lightEndChanged(const char* value)
981 m_useLightEnd = !string_empty(value);
984 read_origin(m_lightEnd, value);
988 typedef MemberCaller1<Light, const char*, &Light::lightEndChanged> LightEndChangedCaller;
990 void writeLightOrigin()
992 write_origin(m_lightOrigin, &m_entity, "light_origin");
995 void updateLightRadiiBox() const
997 const Matrix4& rotation = rotation_toMatrix(m_rotation);
998 aabb_corners(AABB(Vector3(0, 0, 0), m_doom3Radius.m_radiusTransformed), m_radii_box.m_points);
999 matrix4_transform_point(rotation, m_radii_box.m_points[0]);
1000 vector3_add(m_radii_box.m_points[0], m_aabb_light.origin);
1001 matrix4_transform_point(rotation, m_radii_box.m_points[1]);
1002 vector3_add(m_radii_box.m_points[1], m_aabb_light.origin);
1003 matrix4_transform_point(rotation, m_radii_box.m_points[2]);
1004 vector3_add(m_radii_box.m_points[2], m_aabb_light.origin);
1005 matrix4_transform_point(rotation, m_radii_box.m_points[3]);
1006 vector3_add(m_radii_box.m_points[3], m_aabb_light.origin);
1007 matrix4_transform_point(rotation, m_radii_box.m_points[4]);
1008 vector3_add(m_radii_box.m_points[4], m_aabb_light.origin);
1009 matrix4_transform_point(rotation, m_radii_box.m_points[5]);
1010 vector3_add(m_radii_box.m_points[5], m_aabb_light.origin);
1011 matrix4_transform_point(rotation, m_radii_box.m_points[6]);
1012 vector3_add(m_radii_box.m_points[6], m_aabb_light.origin);
1013 matrix4_transform_point(rotation, m_radii_box.m_points[7]);
1014 vector3_add(m_radii_box.m_points[7], m_aabb_light.origin);
1017 void rotationChanged()
1019 rotation_assign(m_rotation, m_useLightRotation ? m_lightRotation : m_rotationKey.m_rotation);
1020 GlobalSelectionSystem().pivotChanged();
1022 typedef MemberCaller<Light, &Light::rotationChanged> RotationChangedCaller;
1024 void lightRotationChanged(const char* value)
1026 m_useLightRotation = !string_empty(value);
1027 if(m_useLightRotation)
1029 read_rotation(m_lightRotation, value);
1033 typedef MemberCaller1<Light, const char*, &Light::lightRotationChanged> LightRotationChangedCaller;
1037 Light(EntityClass* eclass, scene::Node& node, const Callback& transformChanged, const Callback& boundsChanged, const Callback& evaluateTransform) :
1039 m_originKey(OriginChangedCaller(*this)),
1040 m_rotationKey(RotationChangedCaller(*this)),
1041 m_colour(Callback()),
1042 m_filter(m_entity, node),
1044 m_nameKeys(m_entity),
1045 m_funcStaticOrigin(m_traverse, m_originKey.m_origin),
1046 m_radii_wire(m_radii, m_aabb_light.origin),
1047 m_radii_fill(m_radii, m_aabb_light.origin),
1048 m_radii_box(m_aabb_light.origin),
1049 m_render_center(m_doom3Radius.m_center, m_entity.getEntityClass()),
1050 m_renderName(m_named, m_aabb_light.origin),
1051 m_useLightOrigin(false),
1052 m_useLightRotation(false),
1053 m_renderProjection(m_doom3Projection),
1054 m_transformChanged(transformChanged),
1055 m_boundsChanged(boundsChanged),
1056 m_evaluateTransform(evaluateTransform)
1060 Light(const Light& other, scene::Node& node, const Callback& transformChanged, const Callback& boundsChanged, const Callback& evaluateTransform) :
1061 m_entity(other.m_entity),
1062 m_originKey(OriginChangedCaller(*this)),
1063 m_rotationKey(RotationChangedCaller(*this)),
1064 m_colour(Callback()),
1065 m_filter(m_entity, node),
1067 m_nameKeys(m_entity),
1068 m_funcStaticOrigin(m_traverse, m_originKey.m_origin),
1069 m_radii_wire(m_radii, m_aabb_light.origin),
1070 m_radii_fill(m_radii, m_aabb_light.origin),
1071 m_radii_box(m_aabb_light.origin),
1072 m_render_center(m_doom3Radius.m_center, m_entity.getEntityClass()),
1073 m_renderName(m_named, m_aabb_light.origin),
1074 m_useLightOrigin(false),
1075 m_useLightRotation(false),
1076 m_renderProjection(m_doom3Projection),
1077 m_transformChanged(transformChanged),
1078 m_boundsChanged(boundsChanged),
1079 m_evaluateTransform(evaluateTransform)
1088 InstanceCounter m_instanceCounter;
1089 void instanceAttach(const scene::Path& path)
1091 if(++m_instanceCounter.m_count == 1)
1093 m_filter.instanceAttach();
1094 m_entity.instanceAttach(path_find_mapfile(path.begin(), path.end()));
1095 if(g_lightType == LIGHTTYPE_DOOM3)
1097 m_traverse.instanceAttach(path_find_mapfile(path.begin(), path.end()));
1099 m_entity.attach(m_keyObservers);
1101 if(g_lightType == LIGHTTYPE_DOOM3)
1103 m_funcStaticOrigin.enable();
1107 void instanceDetach(const scene::Path& path)
1109 if(--m_instanceCounter.m_count == 0)
1111 if(g_lightType == LIGHTTYPE_DOOM3)
1113 m_funcStaticOrigin.disable();
1116 m_entity.detach(m_keyObservers);
1117 if(g_lightType == LIGHTTYPE_DOOM3)
1119 m_traverse.instanceDetach(path_find_mapfile(path.begin(), path.end()));
1121 m_entity.instanceDetach(path_find_mapfile(path.begin(), path.end()));
1122 m_filter.instanceDetach();
1126 EntityKeyValues& getEntity()
1130 const EntityKeyValues& getEntity() const
1135 scene::Traversable& getTraversable()
1139 Namespaced& getNamespaced()
1143 Nameable& getNameable()
1147 TransformNode& getTransformNode()
1152 void attach(scene::Traversable::Observer* observer)
1154 m_traverseObservers.attach(*observer);
1156 void detach(scene::Traversable::Observer* observer)
1158 m_traverseObservers.detach(*observer);
1161 void render(RenderStateFlags state) const
1165 aabb_draw(m_aabb_light, state);
1169 light_draw(m_aabb_light, state);
1173 VolumeIntersectionValue intersectVolume(const VolumeTest& volume, const Matrix4& localToWorld) const
1175 return volume.TestAABB(m_aabb_light, localToWorld);
1179 const AABB& localAABB() const
1181 return m_aabb_light;
1185 mutable Matrix4 m_projectionOrientation;
1187 void renderSolid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected) const
1189 renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly);
1190 renderer.SetState(m_colour.state(), Renderer::eFullMaterials);
1191 renderer.addRenderable(*this, localToWorld);
1193 if(selected && g_lightRadii && string_empty(m_entity.getKeyValue("target")))
1195 if(renderer.getStyle() == Renderer::eFullMaterials)
1197 renderer.SetState(RenderLightRadiiFill::m_state, Renderer::eFullMaterials);
1198 renderer.Highlight(Renderer::ePrimitive, false);
1199 renderer.addRenderable(m_radii_fill, localToWorld);
1203 renderer.addRenderable(m_radii_wire, localToWorld);
1207 renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eFullMaterials);
1209 if(g_lightType == LIGHTTYPE_DOOM3 && selected)
1214 m_projectionOrientation = rotation();
1215 vector4_to_vector3(m_projectionOrientation.t()) = localAABB().origin;
1216 renderer.addRenderable(m_renderProjection, m_projectionOrientation);
1220 updateLightRadiiBox();
1221 renderer.addRenderable(m_radii_box, localToWorld);
1224 //draw the center of the light
1225 if(m_doom3Radius.m_useCenterKey)
1227 renderer.Highlight(Renderer::ePrimitive, false);
1228 renderer.Highlight(Renderer::eFace, false);
1229 renderer.SetState(m_render_center.m_state, Renderer::eFullMaterials);
1230 renderer.SetState(m_render_center.m_state, Renderer::eWireframeOnly);
1231 renderer.addRenderable(m_render_center, localToWorld);
1235 void renderWireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected) const
1237 renderSolid(renderer, volume, localToWorld, selected);
1240 renderer.addRenderable(m_renderName, localToWorld);
1244 void testSelect(Selector& selector, SelectionTest& test, const Matrix4& localToWorld)
1246 test.BeginMesh(localToWorld);
1248 SelectionIntersection best;
1249 aabb_testselect(m_aabb_light, test, best);
1252 selector.addIntersection(best);
1256 void translate(const Vector3& translation)
1258 m_aabb_light.origin = origin_translated(m_aabb_light.origin, translation);
1260 void rotate(const Quaternion& rotation)
1262 rotation_rotate(m_rotation, rotation);
1264 void snapto(float snap)
1266 if(g_lightType == LIGHTTYPE_DOOM3 && !m_useLightOrigin && !m_traverse.empty())
1268 m_useLightOrigin = true;
1269 m_lightOrigin = m_originKey.m_origin;
1272 if(m_useLightOrigin)
1274 m_lightOrigin = origin_snapped(m_lightOrigin, snap);
1279 m_originKey.m_origin = origin_snapped(m_originKey.m_origin, snap);
1280 m_originKey.write(&m_entity);
1283 void setLightRadius(const AABB& aabb)
1285 m_aabb_light.origin = aabb.origin;
1286 m_doom3Radius.m_radiusTransformed = aabb.extents;
1288 void transformLightRadius(const Matrix4& transform)
1290 matrix4_transform_point(transform, m_aabb_light.origin);
1292 void revertTransform()
1294 m_aabb_light.origin = m_useLightOrigin ? m_lightOrigin : m_originKey.m_origin;
1295 rotation_assign(m_rotation, m_useLightRotation ? m_lightRotation : m_rotationKey.m_rotation);
1296 m_doom3Radius.m_radiusTransformed = m_doom3Radius.m_radius;
1298 void freezeTransform()
1300 if(g_lightType == LIGHTTYPE_DOOM3 && !m_useLightOrigin && !m_traverse.empty())
1302 m_useLightOrigin = true;
1305 if(m_useLightOrigin)
1307 m_lightOrigin = m_aabb_light.origin;
1312 m_originKey.m_origin = m_aabb_light.origin;
1313 m_originKey.write(&m_entity);
1316 if(g_lightType == LIGHTTYPE_DOOM3)
1318 if(!m_useLightRotation && !m_traverse.empty())
1320 m_useLightRotation = true;
1323 if(m_useLightRotation)
1325 rotation_assign(m_lightRotation, m_rotation);
1326 write_rotation(m_lightRotation, &m_entity, "light_rotation");
1329 rotation_assign(m_rotationKey.m_rotation, m_rotation);
1330 write_rotation(m_rotationKey.m_rotation, &m_entity);
1332 m_doom3Radius.m_radius = m_doom3Radius.m_radiusTransformed;
1333 if(m_doom3Radius.m_radius == c_defaultDoom3LightRadius)
1335 m_entity.setKeyValue("light_radius", "");
1339 write_origin(m_doom3Radius.m_radius, &m_entity, "light_radius");
1343 void transformChanged()
1346 m_evaluateTransform();
1349 typedef MemberCaller<Light, &Light::transformChanged> TransformChangedCaller;
1351 mutable Matrix4 m_localPivot;
1352 const Matrix4& getLocalPivot() const
1354 m_localPivot = rotation_toMatrix(m_rotation);
1355 vector4_to_vector3(m_localPivot.t()) = m_aabb_light.origin;
1356 return m_localPivot;
1359 void setLightChangedCallback(const Callback& callback)
1361 m_doom3Radius.m_changed = callback;
1364 const AABB& aabb() const
1366 m_doom3AABB = AABB(m_aabb_light.origin, m_doom3Radius.m_radiusTransformed);
1369 bool testAABB(const AABB& other) const
1373 Matrix4 transform = rotation();
1374 vector4_to_vector3(transform.t()) = localAABB().origin;
1376 Frustum frustum(frustum_transformed(m_doom3Frustum, transform));
1377 return frustum_test_aabb(frustum, other) != c_volumeOutside;
1379 // test against an AABB which contains the rotated bounds of this light.
1380 const AABB& bounds = aabb();
1381 return aabb_intersects_aabb(other, AABB(
1384 static_cast<float>(fabs(m_rotation[0] * bounds.extents[0])
1385 + fabs(m_rotation[3] * bounds.extents[1])
1386 + fabs(m_rotation[6] * bounds.extents[2])),
1387 static_cast<float>(fabs(m_rotation[1] * bounds.extents[0])
1388 + fabs(m_rotation[4] * bounds.extents[1])
1389 + fabs(m_rotation[7] * bounds.extents[2])),
1390 static_cast<float>(fabs(m_rotation[2] * bounds.extents[0])
1391 + fabs(m_rotation[5] * bounds.extents[1])
1392 + fabs(m_rotation[8] * bounds.extents[2]))
1397 const Matrix4& rotation() const
1399 m_doom3Rotation = rotation_toMatrix(m_rotation);
1400 return m_doom3Rotation;
1402 const Vector3& offset() const
1404 return m_doom3Radius.m_center;
1406 const Vector3& colour() const
1408 return m_colour.m_colour;
1411 bool isProjected() const
1413 return m_useLightTarget && m_useLightUp && m_useLightRight;
1415 void projectionChanged()
1417 m_doom3ProjectionChanged = true;
1418 m_doom3Radius.m_changed();
1419 SceneChangeNotify();
1422 const Matrix4& projection() const
1424 if(!m_doom3ProjectionChanged)
1426 return m_doom3Projection;
1428 m_doom3ProjectionChanged = false;
1429 m_doom3Projection = g_matrix4_identity;
1430 matrix4_translate_by_vec3(m_doom3Projection, Vector3(0.5f, 0.5f, 0));
1431 matrix4_scale_by_vec3(m_doom3Projection, Vector3(0.5f, 0.5f, 1));
1434 Vector3 right = vector3_cross(m_lightUp, vector3_normalised(m_lightTarget));
1435 Vector3 up = vector3_cross(vector3_normalised(m_lightTarget), m_lightRight);
1436 Vector3 target = m_lightTarget;
1438 -right.x(), -right.y(), -right.z(), 0,
1439 -up.x(), -up.y(), -up.z(), 0,
1440 -target.x(), -target.y(), -target.z(), 0,
1443 Matrix4 frustum = matrix4_frustum(-0.01, 0.01, -0.01, 0.01, 0.01, 1.0);
1444 test = matrix4_full_inverse(test);
1445 matrix4_premultiply_by_matrix4(test, frustum);
1446 matrix4_multiply_by_matrix4(m_doom3Projection, test);
1448 const float nearFar = 1 / 49.5f;
1449 Vector3 right = vector3_cross(m_lightUp, vector3_normalised(m_lightTarget + m_lightRight));
1450 Vector3 up = vector3_cross(vector3_normalised(m_lightTarget + m_lightUp), m_lightRight);
1451 Vector3 target = vector3_negated(m_lightTarget * (1 + nearFar));
1452 float scale = -1 / vector3_length(m_lightTarget);
1454 -inverse(right.x()), -inverse(up.x()), -inverse(target.x()), 0,
1455 -inverse(right.y()), -inverse(up.y()), -inverse(target.y()), 0,
1456 -inverse(right.z()), -inverse(up.z()), -inverse(target.z()), scale,
1459 matrix4_multiply_by_matrix4(m_doom3Projection, test);
1461 Vector3 leftA(m_lightTarget - m_lightRight);
1462 Vector3 leftB(m_lightRight + m_lightUp);
1463 Plane3 left(vector3_normalised(vector3_cross(leftA, leftB)) * (1.0 / 128), 0);
1464 Vector3 rightA(m_lightTarget + m_lightRight);
1465 Vector3 rightB(vector3_cross(rightA, m_lightTarget));
1466 Plane3 right(vector3_normalised(vector3_cross(rightA, rightB)) * (1.0 / 128), 0);
1467 Vector3 bottomA(m_lightTarget - m_lightUp);
1468 Vector3 bottomB(vector3_cross(bottomA, m_lightTarget));
1469 Plane3 bottom(vector3_normalised(vector3_cross(bottomA, bottomB)) * (1.0 / 128), 0);
1470 Vector3 topA(m_lightTarget + m_lightUp);
1471 Vector3 topB(vector3_cross(topA, m_lightTarget));
1472 Plane3 top(vector3_normalised(vector3_cross(topA, topB)) * (1.0 / 128), 0);
1473 Plane3 front(vector3_normalised(m_lightTarget) * (1.0 / 128), 1);
1474 Plane3 back(vector3_normalised(vector3_negated(m_lightTarget)) * (1.0 / 128), 0);
1475 Matrix4 test(matrix4_from_planes(plane3_flipped(left), plane3_flipped(right), plane3_flipped(bottom), plane3_flipped(top), plane3_flipped(front), plane3_flipped(back)));
1476 matrix4_multiply_by_matrix4(m_doom3Projection, test);
1479 Plane3 lightProject[4];
1481 Vector3 start = m_useLightStart && m_useLightEnd ? m_lightStart : vector3_normalised(m_lightTarget);
1482 Vector3 stop = m_useLightStart && m_useLightEnd ? m_lightEnd : m_lightTarget;
1484 float rLen = vector3_length(m_lightRight);
1485 Vector3 right = vector3_divided(m_lightRight, rLen);
1486 float uLen = vector3_length(m_lightUp);
1487 Vector3 up = vector3_divided(m_lightUp, uLen);
1488 Vector3 normal = vector3_normalised(vector3_cross(up, right));
1490 float dist = vector3_dot(m_lightTarget, normal);
1493 normal = vector3_negated(normal);
1496 right *= ( 0.5f * dist ) / rLen;
1497 up *= -( 0.5f * dist ) / uLen;
1499 lightProject[2] = Plane3(normal, 0);
1500 lightProject[0] = Plane3(right, 0);
1501 lightProject[1] = Plane3(up, 0);
1503 // now offset to center
1504 Vector4 targetGlobal(m_lightTarget, 1);
1506 float a = vector4_dot(targetGlobal, plane3_to_vector4(lightProject[0]));
1507 float b = vector4_dot(targetGlobal, plane3_to_vector4(lightProject[2]));
1508 float ofs = 0.5f - a / b;
1509 plane3_to_vector4(lightProject[0]) += plane3_to_vector4(lightProject[2]) * ofs;
1512 float a = vector4_dot(targetGlobal, plane3_to_vector4(lightProject[1]));
1513 float b = vector4_dot(targetGlobal, plane3_to_vector4(lightProject[2]));
1514 float ofs = 0.5f - a / b;
1515 plane3_to_vector4(lightProject[1]) += plane3_to_vector4(lightProject[2]) * ofs;
1518 // set the falloff vector
1519 Vector3 falloff = stop - start;
1520 float length = vector3_length(falloff);
1521 falloff = vector3_divided(falloff, length);
1522 if ( length <= 0 ) {
1525 falloff *= (1.0f / length);
1526 lightProject[3] = Plane3(falloff, -vector3_dot(start, falloff));
1528 // we want the planes of s=0, s=q, t=0, and t=q
1529 m_doom3Frustum.left = lightProject[0];
1530 m_doom3Frustum.bottom = lightProject[1];
1531 m_doom3Frustum.right = Plane3(lightProject[2].normal() - lightProject[0].normal(), lightProject[2].dist() - lightProject[0].dist());
1532 m_doom3Frustum.top = Plane3(lightProject[2].normal() - lightProject[1].normal(), lightProject[2].dist() - lightProject[1].dist());
1534 // we want the planes of s=0 and s=1 for front and rear clipping planes
1535 m_doom3Frustum.front = lightProject[3];
1537 m_doom3Frustum.back = lightProject[3];
1538 m_doom3Frustum.back.dist() -= 1.0f;
1539 m_doom3Frustum.back = plane3_flipped(m_doom3Frustum.back);
1541 Matrix4 test(matrix4_from_planes(m_doom3Frustum.left, m_doom3Frustum.right, m_doom3Frustum.bottom, m_doom3Frustum.top, m_doom3Frustum.front, m_doom3Frustum.back));
1542 matrix4_multiply_by_matrix4(m_doom3Projection, test);
1544 m_doom3Frustum.left = plane3_normalised(m_doom3Frustum.left);
1545 m_doom3Frustum.right = plane3_normalised(m_doom3Frustum.right);
1546 m_doom3Frustum.bottom = plane3_normalised(m_doom3Frustum.bottom);
1547 m_doom3Frustum.top = plane3_normalised(m_doom3Frustum.top);
1548 m_doom3Frustum.back = plane3_normalised(m_doom3Frustum.back);
1549 m_doom3Frustum.front = plane3_normalised(m_doom3Frustum.front);
1551 //matrix4_scale_by_vec3(m_doom3Projection, Vector3(1.0 / 128, 1.0 / 128, 1.0 / 128));
1552 return m_doom3Projection;
1555 Shader* getShader() const
1557 return m_shader.get();
1561 class LightInstance :
1562 public TargetableInstance,
1563 public TransformModifier,
1565 public SelectionTestable,
1566 public RendererLight,
1567 public PlaneSelectable,
1568 public ComponentSelectionTestable
1572 InstanceTypeCastTable m_casts;
1576 m_casts = TargetableInstance::StaticTypeCasts::instance().get();
1577 InstanceContainedCast<LightInstance, Bounded>::install(m_casts);
1578 //InstanceContainedCast<LightInstance, Cullable>::install(m_casts);
1579 InstanceStaticCast<LightInstance, Renderable>::install(m_casts);
1580 InstanceStaticCast<LightInstance, SelectionTestable>::install(m_casts);
1581 InstanceStaticCast<LightInstance, Transformable>::install(m_casts);
1582 InstanceStaticCast<LightInstance, PlaneSelectable>::install(m_casts);
1583 InstanceStaticCast<LightInstance, ComponentSelectionTestable>::install(m_casts);
1584 InstanceIdentityCast<LightInstance>::install(m_casts);
1586 InstanceTypeCastTable& get()
1593 DragPlanes m_dragPlanes;// dragplanes for lightresizing using mousedrag
1595 typedef LazyStatic<TypeCasts> StaticTypeCasts;
1597 Bounded& get(NullType<Bounded>)
1602 STRING_CONSTANT(Name, "LightInstance");
1604 LightInstance(const scene::Path& path, scene::Instance* parent, Light& contained) :
1605 TargetableInstance(path, parent, this, StaticTypeCasts::instance().get(), contained.getEntity(), *this),
1606 TransformModifier(Light::TransformChangedCaller(contained), ApplyTransformCaller(*this)),
1607 m_contained(contained),
1608 m_dragPlanes(SelectedChangedComponentCaller(*this))
1610 m_contained.instanceAttach(Instance::path());
1612 if(g_lightType == LIGHTTYPE_DOOM3)
1614 GlobalShaderCache().attach(*this);
1615 m_contained.setLightChangedCallback(LightChangedCaller(*this));
1618 StaticRenderableConnectionLines::instance().attach(*this);
1622 StaticRenderableConnectionLines::instance().detach(*this);
1624 if(g_lightType == LIGHTTYPE_DOOM3)
1626 m_contained.setLightChangedCallback(Callback());
1627 GlobalShaderCache().detach(*this);
1630 m_contained.instanceDetach(Instance::path());
1632 void renderSolid(Renderer& renderer, const VolumeTest& volume) const
1634 m_contained.renderSolid(renderer, volume, Instance::localToWorld(), getSelectable().isSelected());
1636 void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
1638 m_contained.renderWireframe(renderer, volume, Instance::localToWorld(), getSelectable().isSelected());
1640 void testSelect(Selector& selector, SelectionTest& test)
1642 m_contained.testSelect(selector, test, Instance::localToWorld());
1645 void selectPlanes(Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback)
1647 test.BeginMesh(localToWorld());
1648 m_dragPlanes.selectPlanes(m_contained.aabb(), selector, test, selectedPlaneCallback, rotation());
1650 void selectReversedPlanes(Selector& selector, const SelectedPlanes& selectedPlanes)
1652 m_dragPlanes.selectReversedPlanes(m_contained.aabb(), selector, selectedPlanes, rotation());
1655 bool isSelectedComponents() const
1657 return m_dragPlanes.isSelected();
1659 void setSelectedComponents(bool select, SelectionSystem::EComponentMode mode)
1661 if(mode == SelectionSystem::eFace)
1663 m_dragPlanes.setSelected(false);
1666 void testSelectComponents(Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode)
1670 void selectedChangedComponent(const Selectable& selectable)
1672 GlobalSelectionSystem().getObserver(SelectionSystem::eComponent)(selectable);
1673 GlobalSelectionSystem().onComponentSelection(*this, selectable);
1675 typedef MemberCaller1<LightInstance, const Selectable&, &LightInstance::selectedChangedComponent> SelectedChangedComponentCaller;
1677 void evaluateTransform()
1679 if(getType() == TRANSFORM_PRIMITIVE)
1681 m_contained.translate(getTranslation());
1682 m_contained.rotate(getRotation());
1686 //globalOutputStream() << getTranslation() << "\n";
1688 m_dragPlanes.m_bounds = m_contained.aabb();
1689 m_contained.setLightRadius(m_dragPlanes.evaluateResize(getTranslation(), rotation()));
1692 void applyTransform()
1694 m_contained.revertTransform();
1695 evaluateTransform();
1696 m_contained.freezeTransform();
1698 typedef MemberCaller<LightInstance, &LightInstance::applyTransform> ApplyTransformCaller;
1702 GlobalShaderCache().changed(*this);
1704 typedef MemberCaller<LightInstance, &LightInstance::lightChanged> LightChangedCaller;
1706 Shader* getShader() const
1708 return m_contained.getShader();
1710 const AABB& aabb() const
1712 return m_contained.aabb();
1714 bool testAABB(const AABB& other) const
1716 return m_contained.testAABB(other);
1718 const Matrix4& rotation() const
1720 return m_contained.rotation();
1722 const Vector3& offset() const
1724 return m_contained.offset();
1726 const Vector3& colour() const
1728 return m_contained.colour();
1731 bool isProjected() const
1733 return m_contained.isProjected();
1735 const Matrix4& projection() const
1737 return m_contained.projection();
1742 public scene::Node::Symbiot,
1743 public scene::Instantiable,
1744 public scene::Cloneable,
1745 public scene::Traversable::Observer
1749 NodeTypeCastTable m_casts;
1753 NodeStaticCast<LightNode, scene::Instantiable>::install(m_casts);
1754 NodeStaticCast<LightNode, scene::Cloneable>::install(m_casts);
1755 if(g_lightType == LIGHTTYPE_DOOM3)
1757 NodeContainedCast<LightNode, scene::Traversable>::install(m_casts);
1759 NodeContainedCast<LightNode, Editable>::install(m_casts);
1760 NodeContainedCast<LightNode, Snappable>::install(m_casts);
1761 NodeContainedCast<LightNode, TransformNode>::install(m_casts);
1762 NodeContainedCast<LightNode, Entity>::install(m_casts);
1763 NodeContainedCast<LightNode, Nameable>::install(m_casts);
1764 NodeContainedCast<LightNode, Namespaced>::install(m_casts);
1766 NodeTypeCastTable& get()
1774 InstanceSet m_instances;
1779 if(g_lightType == LIGHTTYPE_DOOM3)
1781 m_contained.attach(this);
1786 if(g_lightType == LIGHTTYPE_DOOM3)
1788 m_contained.detach(this);
1792 typedef LazyStatic<TypeCasts> StaticTypeCasts;
1794 scene::Traversable& get(NullType<scene::Traversable>)
1796 return m_contained.getTraversable();
1798 Editable& get(NullType<Editable>)
1802 Snappable& get(NullType<Snappable>)
1806 TransformNode& get(NullType<TransformNode>)
1808 return m_contained.getTransformNode();
1810 Entity& get(NullType<Entity>)
1812 return m_contained.getEntity();
1814 Nameable& get(NullType<Nameable>)
1816 return m_contained.getNameable();
1818 Namespaced& get(NullType<Namespaced>)
1820 return m_contained.getNamespaced();
1823 LightNode(EntityClass* eclass) :
1824 m_node(this, this, StaticTypeCasts::instance().get()),
1825 m_contained(eclass, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSet::BoundsChangedCaller(m_instances), InstanceSetEvaluateTransform<LightInstance>::Caller(m_instances))
1829 LightNode(const LightNode& other) :
1830 scene::Node::Symbiot(other),
1831 scene::Instantiable(other),
1832 scene::Cloneable(other),
1833 scene::Traversable::Observer(other),
1834 m_node(this, this, StaticTypeCasts::instance().get()),
1835 m_contained(other.m_contained, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSet::BoundsChangedCaller(m_instances), InstanceSetEvaluateTransform<LightInstance>::Caller(m_instances))
1853 scene::Node& clone() const
1855 return (new LightNode(*this))->node();
1858 void insert(scene::Node& child)
1860 m_instances.insert(child);
1862 void erase(scene::Node& child)
1864 m_instances.erase(child);
1867 scene::Instance* create(const scene::Path& path, scene::Instance* parent)
1869 return new LightInstance(path, parent, m_contained);
1871 void forEachInstance(const scene::Instantiable::Visitor& visitor)
1873 m_instances.forEachInstance(visitor);
1875 void insert(scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance)
1877 m_instances.insert(observer, path, instance);
1879 scene::Instance* erase(scene::Instantiable::Observer* observer, const scene::Path& path)
1881 return m_instances.erase(observer, path);
1885 void Light_Construct(LightType lightType)
1887 g_lightType = lightType;
1888 if(g_lightType == LIGHTTYPE_DOOM3)
1890 LightShader::m_defaultShader = "lights/defaultPointLight";
1892 LightShader::m_defaultShader = "lights/defaultProjectedLight";
1895 RenderLightRadiiFill::m_state = GlobalShaderCache().capture("$Q3MAP2_LIGHT_SPHERE");
1896 RenderLightCenter::m_state = GlobalShaderCache().capture("$BIGPOINT");
1898 void Light_Destroy()
1900 GlobalShaderCache().release("$Q3MAP2_LIGHT_SPHERE");
1901 GlobalShaderCache().release("$BIGPOINT");
1904 scene::Node& New_Light(EntityClass* eclass)
1906 return (new LightNode(eclass))->node();