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 class Doom3LightRadius
543 Vector3 m_defaultRadius;
545 Vector3 m_radiusTransformed;
550 Doom3LightRadius(const char* defaultRadius) : m_defaultRadius(300, 300, 300), m_center(0, 0, 0), m_useCenterKey(false)
552 if(!string_parse_vector3(defaultRadius, m_defaultRadius))
554 globalErrorStream() << "Doom3LightRadius: failed to parse default light radius\n";
556 m_radius = m_defaultRadius;
559 void lightRadiusChanged(const char* value)
561 if(!string_parse_vector3(value, m_radius))
563 m_radius = m_defaultRadius;
565 m_radiusTransformed = m_radius;
569 typedef MemberCaller1<Doom3LightRadius, const char*, &Doom3LightRadius::lightRadiusChanged> LightRadiusChangedCaller;
571 void lightCenterChanged(const char* value)
573 m_useCenterKey = string_parse_vector3(value, m_center);
576 m_center = Vector3(0, 0, 0);
580 typedef MemberCaller1<Doom3LightRadius, const char*, &Doom3LightRadius::lightCenterChanged> LightCenterChangedCaller;
583 class RenderLightRadiiWire : public OpenGLRenderable
586 const Vector3& m_origin;
588 RenderLightRadiiWire(LightRadii& radii, const Vector3& origin) : m_radii(radii), m_origin(origin)
591 void render(RenderStateFlags state) const
593 light_draw_radius_wire(m_origin, m_radii.m_radii);
597 class RenderLightRadiiFill : public OpenGLRenderable
600 const Vector3& m_origin;
602 static Shader* m_state;
604 RenderLightRadiiFill(LightRadii& radii, const Vector3& origin) : m_radii(radii), m_origin(origin)
607 void render(RenderStateFlags state) const
609 light_draw_radius_fill(m_origin, m_radii.m_radii);
613 class RenderLightRadiiBox : public OpenGLRenderable
615 const Vector3& m_origin;
617 mutable Vector3 m_points[8];
618 static Shader* m_state;
620 RenderLightRadiiBox(const Vector3& origin) : m_origin(origin)
623 void render(RenderStateFlags state) const
625 //draw the bounding box of light based on light_radius key
626 if((state & RENDER_FILL) != 0)
628 aabb_draw_flatshade(m_points);
632 aabb_draw_wire(m_points);
635 #if 1 //disable if you dont want lines going from the center of the light bbox to the corners
636 light_draw_box_lines(m_origin, m_points);
641 Shader* RenderLightRadiiFill::m_state = 0;
643 class RenderLightCenter : public OpenGLRenderable
645 const Vector3& m_center;
646 EntityClass& m_eclass;
648 static Shader* m_state;
650 RenderLightCenter(const Vector3& center, EntityClass& eclass) : m_center(center), m_eclass(eclass)
653 void render(RenderStateFlags state) const
656 glColor3fv(vector3_to_array(m_eclass.color));
657 glVertex3fv(vector3_to_array(m_center));
662 Shader* RenderLightCenter::m_state = 0;
664 class RenderLightProjection : public OpenGLRenderable
666 const Matrix4& m_projection;
669 RenderLightProjection(const Matrix4& projection) : m_projection(projection)
672 void render(RenderStateFlags state) const
674 Matrix4 unproject(matrix4_full_inverse(m_projection));
676 aabb_corners(AABB(Vector3(0.5f, 0.5f, 0.5f), Vector3(0.5f, 0.5f, 0.5f)), points);
677 points[0] = vector4_projected(matrix4_transformed_vector4(unproject, Vector4(points[0], 1)));
678 points[1] = vector4_projected(matrix4_transformed_vector4(unproject, Vector4(points[1], 1)));
679 points[2] = vector4_projected(matrix4_transformed_vector4(unproject, Vector4(points[2], 1)));
680 points[3] = vector4_projected(matrix4_transformed_vector4(unproject, Vector4(points[3], 1)));
681 points[4] = vector4_projected(matrix4_transformed_vector4(unproject, Vector4(points[4], 1)));
682 points[5] = vector4_projected(matrix4_transformed_vector4(unproject, Vector4(points[5], 1)));
683 points[6] = vector4_projected(matrix4_transformed_vector4(unproject, Vector4(points[6], 1)));
684 points[7] = vector4_projected(matrix4_transformed_vector4(unproject, Vector4(points[7], 1)));
685 Vector4 test1 = matrix4_transformed_vector4(unproject, Vector4(0.5f, 0.5f, 0.5f, 1));
686 Vector3 test2 = vector4_projected(test1);
687 aabb_draw_wire(points);
691 inline void default_extents(Vector3& extents)
693 extents = Vector3(8, 8, 8);
702 m_shader = GlobalShaderCache().capture(m_name.c_str());
706 GlobalShaderCache().release(m_name.c_str());
717 void setName(const char* name)
734 m_shader.setName(m_defaultShader);
737 static const char* m_defaultShader;
743 void valueChanged(const char* value)
745 if(string_empty(value))
751 m_shader.setName(value);
755 typedef MemberCaller1<LightShader, const char*, &LightShader::valueChanged> ValueChangedCaller;
759 return m_shader.get();
763 const char* LightShader::m_defaultShader = "";
765 inline const BasicVector4<double>& plane3_to_vector4(const Plane3& self)
767 return reinterpret_cast<const BasicVector4<double>&>(self);
770 inline BasicVector4<double>& plane3_to_vector4(Plane3& self)
772 return reinterpret_cast<BasicVector4<double>&>(self);
775 inline Matrix4 matrix4_from_planes(const Plane3& left, const Plane3& right, const Plane3& bottom, const Plane3& top, const Plane3& front, const Plane3& back)
778 (right.a - left.a) / 2,
779 (top.a - bottom.a) / 2,
780 (back.a - front.a) / 2,
781 right.a - (right.a - left.a) / 2,
782 (right.b - left.b) / 2,
783 (top.b - bottom.b) / 2,
784 (back.b - front.b) / 2,
785 right.b - (right.b - left.b) / 2,
786 (right.c - left.c) / 2,
787 (top.c - bottom.c) / 2,
788 (back.c - front.c) / 2,
789 right.c - (right.c - left.c) / 2,
790 (right.d - left.d) / 2,
791 (top.d - bottom.d) / 2,
792 (back.d - front.d) / 2,
793 right.d - (right.d - left.d) / 2
798 public OpenGLRenderable,
804 EntityKeyValues m_entity;
805 KeyObserverMap m_keyObservers;
806 TraversableNodeSet m_traverse;
807 IdentityTransform m_transform;
809 OriginKey m_originKey;
810 RotationKey m_rotationKey;
814 ClassnameFilter m_filter;
817 TraversableObserverPairRelay m_traverseObservers;
818 Doom3GroupOrigin m_funcStaticOrigin;
821 Doom3LightRadius m_doom3Radius;
823 RenderLightRadiiWire m_radii_wire;
824 RenderLightRadiiFill m_radii_fill;
825 RenderLightRadiiBox m_radii_box;
826 RenderLightCenter m_render_center;
827 RenderableNamedEntity m_renderName;
829 Vector3 m_lightOrigin;
830 bool m_useLightOrigin;
831 Float9 m_lightRotation;
832 bool m_useLightRotation;
834 Vector3 m_lightTarget;
835 bool m_useLightTarget;
838 Vector3 m_lightRight;
839 bool m_useLightRight;
840 Vector3 m_lightStart;
841 bool m_useLightStart;
845 mutable AABB m_doom3AABB;
846 mutable Matrix4 m_doom3Rotation;
847 mutable Matrix4 m_doom3Projection;
848 mutable Frustum m_doom3Frustum;
849 mutable bool m_doom3ProjectionChanged;
851 RenderLightProjection m_renderProjection;
853 LightShader m_shader;
857 Callback m_transformChanged;
858 Callback m_boundsChanged;
859 Callback m_evaluateTransform;
863 default_rotation(m_rotation);
864 m_aabb_light.origin = Vector3(0, 0, 0);
865 default_extents(m_aabb_light.extents);
867 m_keyObservers.insert("classname", ClassnameFilter::ClassnameChangedCaller(m_filter));
868 m_keyObservers.insert(Static<KeyIsName>::instance().m_nameKey, NamedEntity::IdentifierChangedCaller(m_named));
869 m_keyObservers.insert("_color", Colour::ColourChangedCaller(m_colour));
870 m_keyObservers.insert("origin", OriginKey::OriginChangedCaller(m_originKey));
871 m_keyObservers.insert("_light", LightRadii::PrimaryIntensityChangedCaller(m_radii));
872 m_keyObservers.insert("light", LightRadii::SecondaryIntensityChangedCaller(m_radii));
873 m_keyObservers.insert("fade", LightRadii::FadeChangedCaller(m_radii));
874 m_keyObservers.insert("scale", LightRadii::ScaleChangedCaller(m_radii));
875 m_keyObservers.insert("spawnflags", LightRadii::FlagsChangedCaller(m_radii));
877 if(g_lightType == LIGHTTYPE_DOOM3)
879 m_keyObservers.insert("angle", RotationKey::AngleChangedCaller(m_rotationKey));
880 m_keyObservers.insert("rotation", RotationKey::RotationChangedCaller(m_rotationKey));
881 m_keyObservers.insert("light_radius", Doom3LightRadius::LightRadiusChangedCaller(m_doom3Radius));
882 m_keyObservers.insert("light_center", Doom3LightRadius::LightCenterChangedCaller(m_doom3Radius));
883 m_keyObservers.insert("light_origin", Light::LightOriginChangedCaller(*this));
884 m_keyObservers.insert("light_rotation", Light::LightRotationChangedCaller(*this));
885 m_keyObservers.insert("light_target", Light::LightTargetChangedCaller(*this));
886 m_keyObservers.insert("light_up", Light::LightUpChangedCaller(*this));
887 m_keyObservers.insert("light_right", Light::LightRightChangedCaller(*this));
888 m_keyObservers.insert("light_start", Light::LightStartChangedCaller(*this));
889 m_keyObservers.insert("light_end", Light::LightEndChangedCaller(*this));
890 m_keyObservers.insert("texture", LightShader::ValueChangedCaller(m_shader));
891 m_useLightTarget = m_useLightUp = m_useLightRight = m_useLightStart = m_useLightEnd = false;
892 m_doom3ProjectionChanged = true;
895 if(g_lightType == LIGHTTYPE_DOOM3)
897 m_traverse.attach(&m_traverseObservers);
898 m_traverseObservers.attach(m_funcStaticOrigin);
900 m_entity.m_isContainer = true;
905 if(g_lightType == LIGHTTYPE_DOOM3)
907 m_traverseObservers.detach(m_funcStaticOrigin);
908 m_traverse.detach(&m_traverseObservers);
912 // vc 2k5 compiler fix
921 if(g_lightType == LIGHTTYPE_DOOM3)
923 m_funcStaticOrigin.originChanged();
926 m_doom3Radius.m_changed();
928 GlobalSelectionSystem().pivotChanged();
933 m_aabb_light.origin = m_useLightOrigin ? m_lightOrigin : m_originKey.m_origin;
936 typedef MemberCaller<Light, &Light::originChanged> OriginChangedCaller;
938 void lightOriginChanged(const char* value)
940 m_useLightOrigin = !string_empty(value);
943 read_origin(m_lightOrigin, value);
947 typedef MemberCaller1<Light, const char*, &Light::lightOriginChanged> LightOriginChangedCaller;
949 void lightTargetChanged(const char* value)
951 m_useLightTarget = !string_empty(value);
954 read_origin(m_lightTarget, value);
958 typedef MemberCaller1<Light, const char*, &Light::lightTargetChanged> LightTargetChangedCaller;
959 void lightUpChanged(const char* value)
961 m_useLightUp = !string_empty(value);
964 read_origin(m_lightUp, value);
968 typedef MemberCaller1<Light, const char*, &Light::lightUpChanged> LightUpChangedCaller;
969 void lightRightChanged(const char* value)
971 m_useLightRight = !string_empty(value);
974 read_origin(m_lightRight, value);
978 typedef MemberCaller1<Light, const char*, &Light::lightRightChanged> LightRightChangedCaller;
979 void lightStartChanged(const char* value)
981 m_useLightStart = !string_empty(value);
984 read_origin(m_lightStart, value);
988 typedef MemberCaller1<Light, const char*, &Light::lightStartChanged> LightStartChangedCaller;
989 void lightEndChanged(const char* value)
991 m_useLightEnd = !string_empty(value);
994 read_origin(m_lightEnd, value);
998 typedef MemberCaller1<Light, const char*, &Light::lightEndChanged> LightEndChangedCaller;
1000 void writeLightOrigin()
1002 write_origin(m_lightOrigin, &m_entity, "light_origin");
1005 void updateLightRadiiBox() const
1007 const Matrix4& rotation = rotation_toMatrix(m_rotation);
1008 aabb_corners(AABB(Vector3(0, 0, 0), m_doom3Radius.m_radiusTransformed), m_radii_box.m_points);
1009 matrix4_transform_point(rotation, m_radii_box.m_points[0]);
1010 vector3_add(m_radii_box.m_points[0], m_aabb_light.origin);
1011 matrix4_transform_point(rotation, m_radii_box.m_points[1]);
1012 vector3_add(m_radii_box.m_points[1], m_aabb_light.origin);
1013 matrix4_transform_point(rotation, m_radii_box.m_points[2]);
1014 vector3_add(m_radii_box.m_points[2], m_aabb_light.origin);
1015 matrix4_transform_point(rotation, m_radii_box.m_points[3]);
1016 vector3_add(m_radii_box.m_points[3], m_aabb_light.origin);
1017 matrix4_transform_point(rotation, m_radii_box.m_points[4]);
1018 vector3_add(m_radii_box.m_points[4], m_aabb_light.origin);
1019 matrix4_transform_point(rotation, m_radii_box.m_points[5]);
1020 vector3_add(m_radii_box.m_points[5], m_aabb_light.origin);
1021 matrix4_transform_point(rotation, m_radii_box.m_points[6]);
1022 vector3_add(m_radii_box.m_points[6], m_aabb_light.origin);
1023 matrix4_transform_point(rotation, m_radii_box.m_points[7]);
1024 vector3_add(m_radii_box.m_points[7], m_aabb_light.origin);
1027 void rotationChanged()
1029 rotation_assign(m_rotation, m_useLightRotation ? m_lightRotation : m_rotationKey.m_rotation);
1030 GlobalSelectionSystem().pivotChanged();
1032 typedef MemberCaller<Light, &Light::rotationChanged> RotationChangedCaller;
1034 void lightRotationChanged(const char* value)
1036 m_useLightRotation = !string_empty(value);
1037 if(m_useLightRotation)
1039 read_rotation(m_lightRotation, value);
1043 typedef MemberCaller1<Light, const char*, &Light::lightRotationChanged> LightRotationChangedCaller;
1047 Light(EntityClass* eclass, scene::Node& node, const Callback& transformChanged, const Callback& boundsChanged, const Callback& evaluateTransform) :
1049 m_originKey(OriginChangedCaller(*this)),
1050 m_rotationKey(RotationChangedCaller(*this)),
1051 m_colour(Callback()),
1052 m_filter(m_entity, node),
1054 m_nameKeys(m_entity),
1055 m_funcStaticOrigin(m_traverse, m_originKey.m_origin),
1056 m_doom3Radius(EntityClass_valueForKey(m_entity.getEntityClass(), "light_radius")),
1057 m_radii_wire(m_radii, m_aabb_light.origin),
1058 m_radii_fill(m_radii, m_aabb_light.origin),
1059 m_radii_box(m_aabb_light.origin),
1060 m_render_center(m_doom3Radius.m_center, m_entity.getEntityClass()),
1061 m_renderName(m_named, m_aabb_light.origin),
1062 m_useLightOrigin(false),
1063 m_useLightRotation(false),
1064 m_renderProjection(m_doom3Projection),
1065 m_transformChanged(transformChanged),
1066 m_boundsChanged(boundsChanged),
1067 m_evaluateTransform(evaluateTransform)
1071 Light(const Light& other, scene::Node& node, const Callback& transformChanged, const Callback& boundsChanged, const Callback& evaluateTransform) :
1072 m_entity(other.m_entity),
1073 m_originKey(OriginChangedCaller(*this)),
1074 m_rotationKey(RotationChangedCaller(*this)),
1075 m_colour(Callback()),
1076 m_filter(m_entity, node),
1078 m_nameKeys(m_entity),
1079 m_funcStaticOrigin(m_traverse, m_originKey.m_origin),
1080 m_doom3Radius(EntityClass_valueForKey(m_entity.getEntityClass(), "light_radius")),
1081 m_radii_wire(m_radii, m_aabb_light.origin),
1082 m_radii_fill(m_radii, m_aabb_light.origin),
1083 m_radii_box(m_aabb_light.origin),
1084 m_render_center(m_doom3Radius.m_center, m_entity.getEntityClass()),
1085 m_renderName(m_named, m_aabb_light.origin),
1086 m_useLightOrigin(false),
1087 m_useLightRotation(false),
1088 m_renderProjection(m_doom3Projection),
1089 m_transformChanged(transformChanged),
1090 m_boundsChanged(boundsChanged),
1091 m_evaluateTransform(evaluateTransform)
1100 InstanceCounter m_instanceCounter;
1101 void instanceAttach(const scene::Path& path)
1103 if(++m_instanceCounter.m_count == 1)
1105 m_filter.instanceAttach();
1106 m_entity.instanceAttach(path_find_mapfile(path.begin(), path.end()));
1107 if(g_lightType == LIGHTTYPE_DOOM3)
1109 m_traverse.instanceAttach(path_find_mapfile(path.begin(), path.end()));
1111 m_entity.attach(m_keyObservers);
1113 if(g_lightType == LIGHTTYPE_DOOM3)
1115 m_funcStaticOrigin.enable();
1119 void instanceDetach(const scene::Path& path)
1121 if(--m_instanceCounter.m_count == 0)
1123 if(g_lightType == LIGHTTYPE_DOOM3)
1125 m_funcStaticOrigin.disable();
1128 m_entity.detach(m_keyObservers);
1129 if(g_lightType == LIGHTTYPE_DOOM3)
1131 m_traverse.instanceDetach(path_find_mapfile(path.begin(), path.end()));
1133 m_entity.instanceDetach(path_find_mapfile(path.begin(), path.end()));
1134 m_filter.instanceDetach();
1138 EntityKeyValues& getEntity()
1142 const EntityKeyValues& getEntity() const
1147 scene::Traversable& getTraversable()
1151 Namespaced& getNamespaced()
1155 Nameable& getNameable()
1159 TransformNode& getTransformNode()
1164 void attach(scene::Traversable::Observer* observer)
1166 m_traverseObservers.attach(*observer);
1168 void detach(scene::Traversable::Observer* observer)
1170 m_traverseObservers.detach(*observer);
1173 void render(RenderStateFlags state) const
1177 aabb_draw(m_aabb_light, state);
1181 light_draw(m_aabb_light, state);
1185 VolumeIntersectionValue intersectVolume(const VolumeTest& volume, const Matrix4& localToWorld) const
1187 return volume.TestAABB(m_aabb_light, localToWorld);
1191 const AABB& localAABB() const
1193 return m_aabb_light;
1197 mutable Matrix4 m_projectionOrientation;
1199 void renderSolid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected) const
1201 renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly);
1202 renderer.SetState(m_colour.state(), Renderer::eFullMaterials);
1203 renderer.addRenderable(*this, localToWorld);
1205 if(selected && g_lightRadii && string_empty(m_entity.getKeyValue("target")))
1207 if(renderer.getStyle() == Renderer::eFullMaterials)
1209 renderer.SetState(RenderLightRadiiFill::m_state, Renderer::eFullMaterials);
1210 renderer.Highlight(Renderer::ePrimitive, false);
1211 renderer.addRenderable(m_radii_fill, localToWorld);
1215 renderer.addRenderable(m_radii_wire, localToWorld);
1219 renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eFullMaterials);
1221 if(g_lightType == LIGHTTYPE_DOOM3 && selected)
1226 m_projectionOrientation = rotation();
1227 vector4_to_vector3(m_projectionOrientation.t()) = localAABB().origin;
1228 renderer.addRenderable(m_renderProjection, m_projectionOrientation);
1232 updateLightRadiiBox();
1233 renderer.addRenderable(m_radii_box, localToWorld);
1236 //draw the center of the light
1237 if(m_doom3Radius.m_useCenterKey)
1239 renderer.Highlight(Renderer::ePrimitive, false);
1240 renderer.Highlight(Renderer::eFace, false);
1241 renderer.SetState(m_render_center.m_state, Renderer::eFullMaterials);
1242 renderer.SetState(m_render_center.m_state, Renderer::eWireframeOnly);
1243 renderer.addRenderable(m_render_center, localToWorld);
1247 void renderWireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected) const
1249 renderSolid(renderer, volume, localToWorld, selected);
1252 renderer.addRenderable(m_renderName, localToWorld);
1256 void testSelect(Selector& selector, SelectionTest& test, const Matrix4& localToWorld)
1258 test.BeginMesh(localToWorld);
1260 SelectionIntersection best;
1261 aabb_testselect(m_aabb_light, test, best);
1264 selector.addIntersection(best);
1268 void translate(const Vector3& translation)
1270 m_aabb_light.origin = origin_translated(m_aabb_light.origin, translation);
1272 void rotate(const Quaternion& rotation)
1274 rotation_rotate(m_rotation, rotation);
1276 void snapto(float snap)
1278 if(g_lightType == LIGHTTYPE_DOOM3 && !m_useLightOrigin && !m_traverse.empty())
1280 m_useLightOrigin = true;
1281 m_lightOrigin = m_originKey.m_origin;
1284 if(m_useLightOrigin)
1286 m_lightOrigin = origin_snapped(m_lightOrigin, snap);
1291 m_originKey.m_origin = origin_snapped(m_originKey.m_origin, snap);
1292 m_originKey.write(&m_entity);
1295 void setLightRadius(const AABB& aabb)
1297 m_aabb_light.origin = aabb.origin;
1298 m_doom3Radius.m_radiusTransformed = aabb.extents;
1300 void transformLightRadius(const Matrix4& transform)
1302 matrix4_transform_point(transform, m_aabb_light.origin);
1304 void revertTransform()
1306 m_aabb_light.origin = m_useLightOrigin ? m_lightOrigin : m_originKey.m_origin;
1307 rotation_assign(m_rotation, m_useLightRotation ? m_lightRotation : m_rotationKey.m_rotation);
1308 m_doom3Radius.m_radiusTransformed = m_doom3Radius.m_radius;
1310 void freezeTransform()
1312 if(g_lightType == LIGHTTYPE_DOOM3 && !m_useLightOrigin && !m_traverse.empty())
1314 m_useLightOrigin = true;
1317 if(m_useLightOrigin)
1319 m_lightOrigin = m_aabb_light.origin;
1324 m_originKey.m_origin = m_aabb_light.origin;
1325 m_originKey.write(&m_entity);
1328 if(g_lightType == LIGHTTYPE_DOOM3)
1330 if(!m_useLightRotation && !m_traverse.empty())
1332 m_useLightRotation = true;
1335 if(m_useLightRotation)
1337 rotation_assign(m_lightRotation, m_rotation);
1338 write_rotation(m_lightRotation, &m_entity, "light_rotation");
1341 rotation_assign(m_rotationKey.m_rotation, m_rotation);
1342 write_rotation(m_rotationKey.m_rotation, &m_entity);
1344 m_doom3Radius.m_radius = m_doom3Radius.m_radiusTransformed;
1345 write_origin(m_doom3Radius.m_radius, &m_entity, "light_radius");
1348 void transformChanged()
1351 m_evaluateTransform();
1354 typedef MemberCaller<Light, &Light::transformChanged> TransformChangedCaller;
1356 mutable Matrix4 m_localPivot;
1357 const Matrix4& getLocalPivot() const
1359 m_localPivot = rotation_toMatrix(m_rotation);
1360 vector4_to_vector3(m_localPivot.t()) = m_aabb_light.origin;
1361 return m_localPivot;
1364 void setLightChangedCallback(const Callback& callback)
1366 m_doom3Radius.m_changed = callback;
1369 const AABB& aabb() const
1371 m_doom3AABB = AABB(m_aabb_light.origin, m_doom3Radius.m_radiusTransformed);
1374 bool testAABB(const AABB& other) const
1378 Matrix4 transform = rotation();
1379 vector4_to_vector3(transform.t()) = localAABB().origin;
1381 Frustum frustum(frustum_transformed(m_doom3Frustum, transform));
1382 return frustum_test_aabb(frustum, other) != c_volumeOutside;
1384 // test against an AABB which contains the rotated bounds of this light.
1385 const AABB& bounds = aabb();
1386 return aabb_intersects_aabb(other, AABB(
1389 static_cast<float>(fabs(m_rotation[0] * bounds.extents[0])
1390 + fabs(m_rotation[3] * bounds.extents[1])
1391 + fabs(m_rotation[6] * bounds.extents[2])),
1392 static_cast<float>(fabs(m_rotation[1] * bounds.extents[0])
1393 + fabs(m_rotation[4] * bounds.extents[1])
1394 + fabs(m_rotation[7] * bounds.extents[2])),
1395 static_cast<float>(fabs(m_rotation[2] * bounds.extents[0])
1396 + fabs(m_rotation[5] * bounds.extents[1])
1397 + fabs(m_rotation[8] * bounds.extents[2]))
1402 const Matrix4& rotation() const
1404 m_doom3Rotation = rotation_toMatrix(m_rotation);
1405 return m_doom3Rotation;
1407 const Vector3& offset() const
1409 return m_doom3Radius.m_center;
1411 const Vector3& colour() const
1413 return m_colour.m_colour;
1416 bool isProjected() const
1418 return m_useLightTarget && m_useLightUp && m_useLightRight;
1420 void projectionChanged()
1422 m_doom3ProjectionChanged = true;
1423 m_doom3Radius.m_changed();
1424 SceneChangeNotify();
1427 const Matrix4& projection() const
1429 if(!m_doom3ProjectionChanged)
1431 return m_doom3Projection;
1433 m_doom3ProjectionChanged = false;
1434 m_doom3Projection = g_matrix4_identity;
1435 matrix4_translate_by_vec3(m_doom3Projection, Vector3(0.5f, 0.5f, 0));
1436 matrix4_scale_by_vec3(m_doom3Projection, Vector3(0.5f, 0.5f, 1));
1439 Vector3 right = vector3_cross(m_lightUp, vector3_normalised(m_lightTarget));
1440 Vector3 up = vector3_cross(vector3_normalised(m_lightTarget), m_lightRight);
1441 Vector3 target = m_lightTarget;
1443 -right.x(), -right.y(), -right.z(), 0,
1444 -up.x(), -up.y(), -up.z(), 0,
1445 -target.x(), -target.y(), -target.z(), 0,
1448 Matrix4 frustum = matrix4_frustum(-0.01, 0.01, -0.01, 0.01, 0.01, 1.0);
1449 test = matrix4_full_inverse(test);
1450 matrix4_premultiply_by_matrix4(test, frustum);
1451 matrix4_multiply_by_matrix4(m_doom3Projection, test);
1453 const float nearFar = 1 / 49.5f;
1454 Vector3 right = vector3_cross(m_lightUp, vector3_normalised(m_lightTarget + m_lightRight));
1455 Vector3 up = vector3_cross(vector3_normalised(m_lightTarget + m_lightUp), m_lightRight);
1456 Vector3 target = vector3_negated(m_lightTarget * (1 + nearFar));
1457 float scale = -1 / vector3_length(m_lightTarget);
1459 -inverse(right.x()), -inverse(up.x()), -inverse(target.x()), 0,
1460 -inverse(right.y()), -inverse(up.y()), -inverse(target.y()), 0,
1461 -inverse(right.z()), -inverse(up.z()), -inverse(target.z()), scale,
1464 matrix4_multiply_by_matrix4(m_doom3Projection, test);
1466 Vector3 leftA(m_lightTarget - m_lightRight);
1467 Vector3 leftB(m_lightRight + m_lightUp);
1468 Plane3 left(vector3_normalised(vector3_cross(leftA, leftB)) * (1.0 / 128), 0);
1469 Vector3 rightA(m_lightTarget + m_lightRight);
1470 Vector3 rightB(vector3_cross(rightA, m_lightTarget));
1471 Plane3 right(vector3_normalised(vector3_cross(rightA, rightB)) * (1.0 / 128), 0);
1472 Vector3 bottomA(m_lightTarget - m_lightUp);
1473 Vector3 bottomB(vector3_cross(bottomA, m_lightTarget));
1474 Plane3 bottom(vector3_normalised(vector3_cross(bottomA, bottomB)) * (1.0 / 128), 0);
1475 Vector3 topA(m_lightTarget + m_lightUp);
1476 Vector3 topB(vector3_cross(topA, m_lightTarget));
1477 Plane3 top(vector3_normalised(vector3_cross(topA, topB)) * (1.0 / 128), 0);
1478 Plane3 front(vector3_normalised(m_lightTarget) * (1.0 / 128), 1);
1479 Plane3 back(vector3_normalised(vector3_negated(m_lightTarget)) * (1.0 / 128), 0);
1480 Matrix4 test(matrix4_from_planes(plane3_flipped(left), plane3_flipped(right), plane3_flipped(bottom), plane3_flipped(top), plane3_flipped(front), plane3_flipped(back)));
1481 matrix4_multiply_by_matrix4(m_doom3Projection, test);
1484 Plane3 lightProject[4];
1486 Vector3 start = m_useLightStart && m_useLightEnd ? m_lightStart : vector3_normalised(m_lightTarget);
1487 Vector3 stop = m_useLightStart && m_useLightEnd ? m_lightEnd : m_lightTarget;
1489 float rLen = vector3_length(m_lightRight);
1490 Vector3 right = vector3_divided(m_lightRight, rLen);
1491 float uLen = vector3_length(m_lightUp);
1492 Vector3 up = vector3_divided(m_lightUp, uLen);
1493 Vector3 normal = vector3_normalised(vector3_cross(up, right));
1495 float dist = vector3_dot(m_lightTarget, normal);
1498 normal = vector3_negated(normal);
1501 right *= ( 0.5f * dist ) / rLen;
1502 up *= -( 0.5f * dist ) / uLen;
1504 lightProject[2] = Plane3(normal, 0);
1505 lightProject[0] = Plane3(right, 0);
1506 lightProject[1] = Plane3(up, 0);
1508 // now offset to center
1509 Vector4 targetGlobal(m_lightTarget, 1);
1511 float a = vector4_dot(targetGlobal, plane3_to_vector4(lightProject[0]));
1512 float b = vector4_dot(targetGlobal, plane3_to_vector4(lightProject[2]));
1513 float ofs = 0.5f - a / b;
1514 plane3_to_vector4(lightProject[0]) += plane3_to_vector4(lightProject[2]) * ofs;
1517 float a = vector4_dot(targetGlobal, plane3_to_vector4(lightProject[1]));
1518 float b = vector4_dot(targetGlobal, plane3_to_vector4(lightProject[2]));
1519 float ofs = 0.5f - a / b;
1520 plane3_to_vector4(lightProject[1]) += plane3_to_vector4(lightProject[2]) * ofs;
1523 // set the falloff vector
1524 Vector3 falloff = stop - start;
1525 float length = vector3_length(falloff);
1526 falloff = vector3_divided(falloff, length);
1527 if ( length <= 0 ) {
1530 falloff *= (1.0f / length);
1531 lightProject[3] = Plane3(falloff, -vector3_dot(start, falloff));
1533 // we want the planes of s=0, s=q, t=0, and t=q
1534 m_doom3Frustum.left = lightProject[0];
1535 m_doom3Frustum.bottom = lightProject[1];
1536 m_doom3Frustum.right = Plane3(lightProject[2].normal() - lightProject[0].normal(), lightProject[2].dist() - lightProject[0].dist());
1537 m_doom3Frustum.top = Plane3(lightProject[2].normal() - lightProject[1].normal(), lightProject[2].dist() - lightProject[1].dist());
1539 // we want the planes of s=0 and s=1 for front and rear clipping planes
1540 m_doom3Frustum.front = lightProject[3];
1542 m_doom3Frustum.back = lightProject[3];
1543 m_doom3Frustum.back.dist() -= 1.0f;
1544 m_doom3Frustum.back = plane3_flipped(m_doom3Frustum.back);
1546 Matrix4 test(matrix4_from_planes(m_doom3Frustum.left, m_doom3Frustum.right, m_doom3Frustum.bottom, m_doom3Frustum.top, m_doom3Frustum.front, m_doom3Frustum.back));
1547 matrix4_multiply_by_matrix4(m_doom3Projection, test);
1549 m_doom3Frustum.left = plane3_normalised(m_doom3Frustum.left);
1550 m_doom3Frustum.right = plane3_normalised(m_doom3Frustum.right);
1551 m_doom3Frustum.bottom = plane3_normalised(m_doom3Frustum.bottom);
1552 m_doom3Frustum.top = plane3_normalised(m_doom3Frustum.top);
1553 m_doom3Frustum.back = plane3_normalised(m_doom3Frustum.back);
1554 m_doom3Frustum.front = plane3_normalised(m_doom3Frustum.front);
1556 //matrix4_scale_by_vec3(m_doom3Projection, Vector3(1.0 / 128, 1.0 / 128, 1.0 / 128));
1557 return m_doom3Projection;
1560 Shader* getShader() const
1562 return m_shader.get();
1566 class LightInstance :
1567 public TargetableInstance,
1568 public TransformModifier,
1570 public SelectionTestable,
1571 public RendererLight,
1572 public PlaneSelectable,
1573 public ComponentSelectionTestable
1577 InstanceTypeCastTable m_casts;
1581 m_casts = TargetableInstance::StaticTypeCasts::instance().get();
1582 InstanceContainedCast<LightInstance, Bounded>::install(m_casts);
1583 //InstanceContainedCast<LightInstance, Cullable>::install(m_casts);
1584 InstanceStaticCast<LightInstance, Renderable>::install(m_casts);
1585 InstanceStaticCast<LightInstance, SelectionTestable>::install(m_casts);
1586 InstanceStaticCast<LightInstance, Transformable>::install(m_casts);
1587 InstanceStaticCast<LightInstance, PlaneSelectable>::install(m_casts);
1588 InstanceStaticCast<LightInstance, ComponentSelectionTestable>::install(m_casts);
1589 InstanceIdentityCast<LightInstance>::install(m_casts);
1591 InstanceTypeCastTable& get()
1598 DragPlanes m_dragPlanes;// dragplanes for lightresizing using mousedrag
1600 typedef LazyStatic<TypeCasts> StaticTypeCasts;
1602 Bounded& get(NullType<Bounded>)
1607 STRING_CONSTANT(Name, "LightInstance");
1609 LightInstance(const scene::Path& path, scene::Instance* parent, Light& contained) :
1610 TargetableInstance(path, parent, this, StaticTypeCasts::instance().get(), contained.getEntity(), *this),
1611 TransformModifier(Light::TransformChangedCaller(contained), ApplyTransformCaller(*this)),
1612 m_contained(contained),
1613 m_dragPlanes(SelectedChangedComponentCaller(*this))
1615 m_contained.instanceAttach(Instance::path());
1617 if(g_lightType == LIGHTTYPE_DOOM3)
1619 GlobalShaderCache().attach(*this);
1620 m_contained.setLightChangedCallback(LightChangedCaller(*this));
1623 StaticRenderableConnectionLines::instance().attach(*this);
1627 StaticRenderableConnectionLines::instance().detach(*this);
1629 if(g_lightType == LIGHTTYPE_DOOM3)
1631 m_contained.setLightChangedCallback(Callback());
1632 GlobalShaderCache().detach(*this);
1635 m_contained.instanceDetach(Instance::path());
1637 void renderSolid(Renderer& renderer, const VolumeTest& volume) const
1639 m_contained.renderSolid(renderer, volume, Instance::localToWorld(), getSelectable().isSelected());
1641 void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
1643 m_contained.renderWireframe(renderer, volume, Instance::localToWorld(), getSelectable().isSelected());
1645 void testSelect(Selector& selector, SelectionTest& test)
1647 m_contained.testSelect(selector, test, Instance::localToWorld());
1650 void selectPlanes(Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback)
1652 test.BeginMesh(localToWorld());
1653 m_dragPlanes.selectPlanes(m_contained.aabb(), selector, test, selectedPlaneCallback, rotation());
1655 void selectReversedPlanes(Selector& selector, const SelectedPlanes& selectedPlanes)
1657 m_dragPlanes.selectReversedPlanes(m_contained.aabb(), selector, selectedPlanes, rotation());
1660 bool isSelectedComponents() const
1662 return m_dragPlanes.isSelected();
1664 void setSelectedComponents(bool select, SelectionSystem::EComponentMode mode)
1666 if(mode == SelectionSystem::eFace)
1668 m_dragPlanes.setSelected(false);
1671 void testSelectComponents(Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode)
1675 void selectedChangedComponent(const Selectable& selectable)
1677 GlobalSelectionSystem().getObserver(SelectionSystem::eComponent)(selectable);
1678 GlobalSelectionSystem().onComponentSelection(*this, selectable);
1680 typedef MemberCaller1<LightInstance, const Selectable&, &LightInstance::selectedChangedComponent> SelectedChangedComponentCaller;
1682 void evaluateTransform()
1684 if(getType() == TRANSFORM_PRIMITIVE)
1686 m_contained.translate(getTranslation());
1687 m_contained.rotate(getRotation());
1691 //globalOutputStream() << getTranslation() << "\n";
1693 m_dragPlanes.m_bounds = m_contained.aabb();
1694 m_contained.setLightRadius(m_dragPlanes.evaluateResize(getTranslation(), rotation()));
1697 void applyTransform()
1699 m_contained.revertTransform();
1700 evaluateTransform();
1701 m_contained.freezeTransform();
1703 typedef MemberCaller<LightInstance, &LightInstance::applyTransform> ApplyTransformCaller;
1707 GlobalShaderCache().changed(*this);
1709 typedef MemberCaller<LightInstance, &LightInstance::lightChanged> LightChangedCaller;
1711 Shader* getShader() const
1713 return m_contained.getShader();
1715 const AABB& aabb() const
1717 return m_contained.aabb();
1719 bool testAABB(const AABB& other) const
1721 return m_contained.testAABB(other);
1723 const Matrix4& rotation() const
1725 return m_contained.rotation();
1727 const Vector3& offset() const
1729 return m_contained.offset();
1731 const Vector3& colour() const
1733 return m_contained.colour();
1736 bool isProjected() const
1738 return m_contained.isProjected();
1740 const Matrix4& projection() const
1742 return m_contained.projection();
1747 public scene::Node::Symbiot,
1748 public scene::Instantiable,
1749 public scene::Cloneable,
1750 public scene::Traversable::Observer
1754 NodeTypeCastTable m_casts;
1758 NodeStaticCast<LightNode, scene::Instantiable>::install(m_casts);
1759 NodeStaticCast<LightNode, scene::Cloneable>::install(m_casts);
1760 if(g_lightType == LIGHTTYPE_DOOM3)
1762 NodeContainedCast<LightNode, scene::Traversable>::install(m_casts);
1764 NodeContainedCast<LightNode, Editable>::install(m_casts);
1765 NodeContainedCast<LightNode, Snappable>::install(m_casts);
1766 NodeContainedCast<LightNode, TransformNode>::install(m_casts);
1767 NodeContainedCast<LightNode, Entity>::install(m_casts);
1768 NodeContainedCast<LightNode, Nameable>::install(m_casts);
1769 NodeContainedCast<LightNode, Namespaced>::install(m_casts);
1771 NodeTypeCastTable& get()
1779 InstanceSet m_instances;
1784 if(g_lightType == LIGHTTYPE_DOOM3)
1786 m_contained.attach(this);
1791 if(g_lightType == LIGHTTYPE_DOOM3)
1793 m_contained.detach(this);
1797 typedef LazyStatic<TypeCasts> StaticTypeCasts;
1799 scene::Traversable& get(NullType<scene::Traversable>)
1801 return m_contained.getTraversable();
1803 Editable& get(NullType<Editable>)
1807 Snappable& get(NullType<Snappable>)
1811 TransformNode& get(NullType<TransformNode>)
1813 return m_contained.getTransformNode();
1815 Entity& get(NullType<Entity>)
1817 return m_contained.getEntity();
1819 Nameable& get(NullType<Nameable>)
1821 return m_contained.getNameable();
1823 Namespaced& get(NullType<Namespaced>)
1825 return m_contained.getNamespaced();
1828 LightNode(EntityClass* eclass) :
1829 m_node(this, this, StaticTypeCasts::instance().get()),
1830 m_contained(eclass, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSet::BoundsChangedCaller(m_instances), InstanceSetEvaluateTransform<LightInstance>::Caller(m_instances))
1834 LightNode(const LightNode& other) :
1835 scene::Node::Symbiot(other),
1836 scene::Instantiable(other),
1837 scene::Cloneable(other),
1838 scene::Traversable::Observer(other),
1839 m_node(this, this, StaticTypeCasts::instance().get()),
1840 m_contained(other.m_contained, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSet::BoundsChangedCaller(m_instances), InstanceSetEvaluateTransform<LightInstance>::Caller(m_instances))
1858 scene::Node& clone() const
1860 return (new LightNode(*this))->node();
1863 void insert(scene::Node& child)
1865 m_instances.insert(child);
1867 void erase(scene::Node& child)
1869 m_instances.erase(child);
1872 scene::Instance* create(const scene::Path& path, scene::Instance* parent)
1874 return new LightInstance(path, parent, m_contained);
1876 void forEachInstance(const scene::Instantiable::Visitor& visitor)
1878 m_instances.forEachInstance(visitor);
1880 void insert(scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance)
1882 m_instances.insert(observer, path, instance);
1884 scene::Instance* erase(scene::Instantiable::Observer* observer, const scene::Path& path)
1886 return m_instances.erase(observer, path);
1890 void Light_Construct(LightType lightType)
1892 g_lightType = lightType;
1893 if(g_lightType == LIGHTTYPE_DOOM3)
1895 LightShader::m_defaultShader = "lights/defaultPointLight";
1897 LightShader::m_defaultShader = "lights/defaultProjectedLight";
1900 RenderLightRadiiFill::m_state = GlobalShaderCache().capture("$Q3MAP2_LIGHT_SPHERE");
1901 RenderLightCenter::m_state = GlobalShaderCache().capture("$BIGPOINT");
1903 void Light_Destroy()
1905 GlobalShaderCache().release("$Q3MAP2_LIGHT_SPHERE");
1906 GlobalShaderCache().release("$BIGPOINT");
1909 scene::Node& New_Light(EntityClass* eclass)
1911 return (new LightNode(eclass))->node();