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
22 #include "selection.h"
24 #include "debugging/debugging.h"
30 #include "windowobserver.h"
34 #include "renderable.h"
35 #include "selectable.h"
38 #include "math/frustum.h"
39 #include "generic/callback.h"
40 #include "generic/object.h"
41 #include "selectionlib.h"
45 #include "stream/stringstream.h"
46 #include "eclasslib.h"
47 #include "generic/bitfield.h"
48 #include "generic/static.h"
51 #include "container/container.h"
58 Matrix4 m_viewpointSpace;
59 Matrix4 m_viewplaneSpace;
60 Vector3 m_axis_screen;
62 void update(const Matrix4& pivot2world, const Matrix4& modelview, const Matrix4& projection, const Matrix4& viewport)
64 Pivot2World_worldSpace(m_worldSpace, pivot2world, modelview, projection, viewport);
65 Pivot2World_viewpointSpace(m_viewpointSpace, m_axis_screen, pivot2world, modelview, projection, viewport);
66 Pivot2World_viewplaneSpace(m_viewplaneSpace, pivot2world, modelview, projection, viewport);
71 void point_for_device_point(Vector3& point, const Matrix4& device2object, const float x, const float y, const float z)
73 // transform from normalised device coords to object coords
74 point = vector4_projected(matrix4_transformed_vector4(device2object, Vector4(x, y, z, 1)));
77 void ray_for_device_point(Ray& ray, const Matrix4& device2object, const float x, const float y)
79 // point at x, y, zNear
80 point_for_device_point(ray.origin, device2object, x, y, -1);
82 // point at x, y, zFar
83 point_for_device_point(ray.direction, device2object, x, y, 1);
86 vector3_subtract(ray.direction, ray.origin);
87 vector3_normalise(ray.direction);
90 bool sphere_intersect_ray(const Vector3& origin, float radius, const Ray& ray, Vector3& intersection)
92 intersection = vector3_subtracted(origin, ray.origin);
93 const double a = vector3_dot(intersection, ray.direction);
94 const double d = radius * radius - (vector3_dot(intersection, intersection) - a * a);
98 intersection = vector3_added(ray.origin, vector3_scaled(ray.direction, a - sqrt(d)));
103 intersection = vector3_added( ray.origin, vector3_scaled(ray.direction, a));
108 void ray_intersect_ray(const Ray& ray, const Ray& other, Vector3& intersection)
110 intersection = vector3_subtracted(ray.origin, other.origin);
111 //float a = 1;//vector3_dot(ray.direction, ray.direction); // always >= 0
112 double dot = vector3_dot(ray.direction, other.direction);
113 //float c = 1;//vector3_dot(other.direction, other.direction); // always >= 0
114 double d = vector3_dot(ray.direction, intersection);
115 double e = vector3_dot(other.direction, intersection);
116 double D = 1 - dot*dot;//a*c - dot*dot; // always >= 0
120 // the lines are almost parallel
121 intersection = vector3_added(other.origin, vector3_scaled(other.direction, e));
125 intersection = vector3_added(other.origin, vector3_scaled(other.direction, (e - dot*d) / D));
129 const Vector3 g_origin(0, 0, 0);
130 const float g_radius = 64;
132 void point_on_sphere(Vector3& point, const Matrix4& device2object, const float x, const float y)
135 ray_for_device_point(ray, device2object, x, y);
136 sphere_intersect_ray(g_origin, g_radius, ray, point);
139 void point_on_axis(Vector3& point, const Vector3& axis, const Matrix4& device2object, const float x, const float y)
142 ray_for_device_point(ray, device2object, x, y);
143 ray_intersect_ray(ray, Ray(Vector3(0, 0, 0), axis), point);
146 void point_on_plane(Vector3& point, const Matrix4& device2object, const float x, const float y)
148 Matrix4 object2device(matrix4_full_inverse(device2object));
149 point = vector4_projected(matrix4_transformed_vector4(device2object, Vector4(x, y, object2device[14] / object2device[15], 1)));
152 //! a and b are unit vectors .. returns angle in radians
153 inline float angle_between(const Vector3& a, const Vector3& b)
155 return static_cast<float>(2.0 * atan2(
156 vector3_length(vector3_subtracted(a, b)),
157 vector3_length(vector3_added(a, b))
166 test_quat(const Vector3& from, const Vector3& to)
168 Vector4 quaternion(quaternion_for_unit_vectors(from, to));
169 Matrix4 matrix(matrix4_rotation_for_quaternion(quaternion_multiplied_by_quaternion(quaternion, c_quaternion_identity)));
174 static test_quat bleh(g_vector3_axis_x, g_vector3_axis_y);
177 //! axis is a unit vector
178 inline void constrain_to_axis(Vector3& vec, const Vector3& axis)
180 vec = vector3_normalised(vector3_added(vec, vector3_scaled(axis, -vector3_dot(vec, axis))));
183 //! a and b are unit vectors .. a and b must be orthogonal to axis .. returns angle in radians
184 float angle_for_axis(const Vector3& a, const Vector3& b, const Vector3& axis)
186 if(vector3_dot(axis, vector3_cross(a, b)) > 0.0)
187 return angle_between(a, b);
189 return -angle_between(a, b);
192 float distance_for_axis(const Vector3& a, const Vector3& b, const Vector3& axis)
194 return static_cast<float>(vector3_dot(b, axis) - vector3_dot(a, axis));
200 virtual void Construct(const Matrix4& device2manip, const float x, const float y) = 0;
201 virtual void Transform(const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y) = 0;
204 void transform_local2object(Matrix4& object, const Matrix4& local, const Matrix4& local2object)
206 object = matrix4_multiplied_by_matrix4(
207 matrix4_multiplied_by_matrix4(local2object, local),
208 matrix4_full_inverse(local2object)
215 virtual void rotate(const Quaternion& rotation) = 0;
218 class RotateFree : public Manipulatable
221 Rotatable& m_rotatable;
223 RotateFree(Rotatable& rotatable)
224 : m_rotatable(rotatable)
227 void Construct(const Matrix4& device2manip, const float x, const float y)
229 point_on_sphere(m_start, device2manip, x, y);
230 vector3_normalise(m_start);
232 void Transform(const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y)
236 point_on_sphere(current, device2manip, x, y);
237 vector3_normalise(current);
239 m_rotatable.rotate(quaternion_for_unit_vectors(m_start, current));
243 class RotateAxis : public Manipulatable
247 Rotatable& m_rotatable;
249 RotateAxis(Rotatable& rotatable)
250 : m_rotatable(rotatable)
253 void Construct(const Matrix4& device2manip, const float x, const float y)
255 point_on_sphere(m_start, device2manip, x, y);
256 constrain_to_axis(m_start, m_axis);
258 /// \brief Converts current position to a normalised vector orthogonal to axis.
259 void Transform(const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y)
262 point_on_sphere(current, device2manip, x, y);
263 constrain_to_axis(current, m_axis);
265 m_rotatable.rotate(quaternion_for_axisangle(m_axis, angle_for_axis(m_start, current, m_axis)));
268 void SetAxis(const Vector3& axis)
274 void translation_local2object(Vector3& object, const Vector3& local, const Matrix4& local2object)
276 object = matrix4_get_translation_vec3(
277 matrix4_multiplied_by_matrix4(
278 matrix4_translated_by_vec3(local2object, local),
279 matrix4_full_inverse(local2object)
287 virtual void translate(const Vector3& translation) = 0;
290 class TranslateAxis : public Manipulatable
294 Translatable& m_translatable;
296 TranslateAxis(Translatable& translatable)
297 : m_translatable(translatable)
300 void Construct(const Matrix4& device2manip, const float x, const float y)
302 point_on_axis(m_start, m_axis, device2manip, x, y);
304 void Transform(const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y)
307 point_on_axis(current, m_axis, device2manip, x, y);
308 current = vector3_scaled(m_axis, distance_for_axis(m_start, current, m_axis));
310 translation_local2object(current, current, manip2object);
311 vector3_snap(current, GetGridSize());
313 m_translatable.translate(current);
316 void SetAxis(const Vector3& axis)
322 class TranslateFree : public Manipulatable
326 Translatable& m_translatable;
328 TranslateFree(Translatable& translatable)
329 : m_translatable(translatable)
332 void Construct(const Matrix4& device2manip, const float x, const float y)
334 point_on_plane(m_start, device2manip, x, y);
336 void Transform(const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y)
339 point_on_plane(current, device2manip, x, y);
340 current = vector3_subtracted(current, m_start);
342 translation_local2object(current, current, manip2object);
343 vector3_snap(current, GetGridSize());
345 m_translatable.translate(current);
353 virtual void scale(const Vector3& scaling) = 0;
357 class ScaleAxis : public Manipulatable
362 Scalable& m_scalable;
364 ScaleAxis(Scalable& scalable)
365 : m_scalable(scalable)
368 void Construct(const Matrix4& device2manip, const float x, const float y)
370 point_on_axis(m_start, m_axis, device2manip, x, y);
372 void Transform(const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y)
375 point_on_axis(current, m_axis, device2manip, x, y);
376 Vector3 delta = vector3_subtracted(current, m_start);
378 translation_local2object(delta, delta, manip2object);
379 vector3_snap(delta, GetGridSize());
381 Vector3 start(vector3_snapped(m_start, GetGridSize()));
383 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
384 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
385 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
387 m_scalable.scale(scale);
390 void SetAxis(const Vector3& axis)
396 class ScaleFree : public Manipulatable
400 Scalable& m_scalable;
402 ScaleFree(Scalable& scalable)
403 : m_scalable(scalable)
406 void Construct(const Matrix4& device2manip, const float x, const float y)
408 point_on_plane(m_start, device2manip, x, y);
410 void Transform(const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y)
413 point_on_plane(current, device2manip, x, y);
414 Vector3 delta = vector3_subtracted(current, m_start);
416 translation_local2object(delta, delta, manip2object);
417 vector3_snap(delta, GetGridSize());
419 Vector3 start(vector3_snapped(m_start, GetGridSize()));
421 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
422 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
423 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
425 m_scalable.scale(scale);
438 class RenderableClippedPrimitive : public OpenGLRenderable
442 PointVertex m_points[9];
446 std::vector<primitive_t> m_primitives;
450 void render(RenderStateFlags state) const
452 for(std::size_t i=0; i<m_primitives.size(); ++i)
454 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_primitives[i].m_points[0].colour);
455 glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_primitives[i].m_points[0].vertex);
456 switch(m_primitives[i].m_count)
459 case 2: glDrawArrays(GL_LINES, 0, GLsizei(m_primitives[i].m_count)); break;
460 default: glDrawArrays(GL_POLYGON, 0, GLsizei(m_primitives[i].m_count)); break;
465 void construct(const Matrix4& world2device)
467 m_inverse = matrix4_full_inverse(world2device);
468 m_world = g_matrix4_identity;
471 void insert(const Vector4 clipped[9], std::size_t count)
475 m_primitives.back().m_count = count;
476 for(std::size_t i=0; i<count; ++i)
478 Vector3 world_point(vector4_projected(matrix4_transformed_vector4(m_inverse, clipped[i])));
479 m_primitives.back().m_points[i].vertex.x = world_point[0];
480 m_primitives.back().m_points[i].vertex.y = world_point[1];
481 m_primitives.back().m_points[i].vertex.z = world_point[2];
487 m_primitives.clear();
492 m_primitives.push_back(primitive_t());
494 const Colour4b colour_clipped(255, 127, 0, 255);
496 for(std::size_t i=0; i<9; ++i)
497 m_primitives.back().m_points[i].colour = colour_clipped;
502 #define DEBUG_SELECTION
505 #if defined(DEBUG_SELECTION)
506 Shader* g_state_clipped;
507 RenderableClippedPrimitive g_render_clipped;
512 // dist_Point_to_Line(): get the distance of a point to a line.
513 // Input: a Point P and a Line L (in any dimension)
514 // Return: the shortest distance from P to L
516 dist_Point_to_Line( Point P, Line L)
518 Vector v = L.P1 - L.P0;
521 double c1 = dot(w,v);
522 double c2 = dot(v,v);
525 Point Pb = L.P0 + b * v;
532 typedef Vector3 point_type;
534 Segment3D(const point_type& _p0, const point_type& _p1)
542 typedef Vector3 Point3D;
544 inline double vector3_distance_squared(const Point3D& a, const Point3D& b)
546 return vector3_length_squared(b - a);
549 // get the distance of a point to a segment.
550 Point3D segment_closest_point_to_point(const Segment3D& segment, const Point3D& point)
552 Vector3 v = segment.p1 - segment.p0;
553 Vector3 w = point - segment.p0;
555 double c1 = vector3_dot(w,v);
559 double c2 = vector3_dot(v,v);
563 return Point3D(segment.p0 + v * (c1 / c2));
566 double segment_dist_to_point_3d(const Segment3D& segment, const Point3D& point)
568 return vector3_distance_squared(point, segment_closest_point_to_point(segment, point));
571 typedef Vector3 point_t;
572 typedef const Vector3* point_iterator_t;
574 // crossing number test for a point in a polygon
575 // This code is patterned after [Franklin, 2000]
576 bool point_test_polygon_2d( const point_t& P, point_iterator_t start, point_iterator_t finish )
578 std::size_t crossings = 0;
580 // loop through all edges of the polygon
581 for(point_iterator_t prev = finish-1, cur = start; cur != finish; prev = cur, ++cur)
582 { // edge from (*prev) to (*cur)
583 if ((((*prev)[1] <= P[1]) && ((*cur)[1] > P[1])) // an upward crossing
584 || (((*prev)[1] > P[1]) && ((*cur)[1] <= P[1])))
585 { // a downward crossing
586 // compute the actual edge-ray intersect x-coordinate
587 float vt = (float)(P[1] - (*prev)[1]) / ((*cur)[1] - (*prev)[1]);
588 if (P[0] < (*prev)[0] + vt * ((*cur)[0] - (*prev)[0])) // P[0] < intersect
590 ++crossings; // a valid crossing of y=P[1] right of P[0]
594 return (crossings & 0x1) != 0; // 0 if even (out), and 1 if odd (in)
597 inline double triangle_signed_area_XY(const Vector3& p0, const Vector3& p1, const Vector3& p2)
599 return ((p1[0] - p0[0]) * (p2[1] - p0[1])) - ((p2[0] - p0[0]) * (p1[1] - p0[1]));
610 inline SelectionIntersection select_point_from_clipped(Vector4& clipped)
612 return SelectionIntersection(clipped[2] / clipped[3], static_cast<float>(vector3_length_squared(Vector3(clipped[0] / clipped[3], clipped[1] / clipped[3], 0))));
615 void BestPoint(std::size_t count, Vector4 clipped[9], SelectionIntersection& best, clipcull_t cull)
617 Vector3 normalised[9];
620 for(std::size_t i=0; i<count; ++i)
622 normalised[i][0] = clipped[i][0] / clipped[i][3];
623 normalised[i][1] = clipped[i][1] / clipped[i][3];
624 normalised[i][2] = clipped[i][2] / clipped[i][3];
628 if(cull != eClipCullNone && count > 2)
630 double signed_area = triangle_signed_area_XY(normalised[0], normalised[1], normalised[2]);
632 if((cull == eClipCullCW && signed_area > 0)
633 || (cull == eClipCullCCW && signed_area < 0))
639 Segment3D segment(normalised[0], normalised[1]);
640 Point3D point = segment_closest_point_to_point(segment, Vector3(0, 0, 0));
641 assign_if_closer(best, SelectionIntersection(point.z(), 0));
643 else if(count > 2 && !point_test_polygon_2d(Vector4(0, 0, 0, 0), normalised, normalised + count))
645 point_iterator_t end = normalised + count;
646 for(point_iterator_t previous = end-1, current = normalised; current != end; previous = current, ++current)
648 Segment3D segment(*previous, *current);
649 Point3D point = segment_closest_point_to_point(segment, Vector3(0, 0, 0));
650 float depth = point.z();
652 float distance = static_cast<float>(vector3_length_squared(point));
654 assign_if_closer(best, SelectionIntersection(depth, distance));
661 SelectionIntersection(
662 static_cast<float>(ray_distance_to_plane(
663 Ray(Vector3(0, 0, 0), Vector3(0, 0, 1)),
664 plane3_for_points(normalised[0], normalised[1], normalised[2])
671 #if defined(DEBUG_SELECTION)
673 g_render_clipped.insert(clipped, count);
677 void LineStrip_BestPoint(const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best)
680 for(std::size_t i = 0; (i + 1) < size; ++i)
682 const std::size_t count = matrix4_clip_line(local2view, vertex3f_to_vector3(vertices[i].vertex), vertex3f_to_vector3(vertices[i + 1].vertex), clipped);
683 BestPoint(count, clipped, best, eClipCullNone);
687 void LineLoop_BestPoint(const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best)
690 for(std::size_t i = 0; i < size; ++i)
692 const std::size_t count = matrix4_clip_line(local2view, vertex3f_to_vector3(vertices[i].vertex), vertex3f_to_vector3(vertices[(i+1)%size].vertex), clipped);
693 BestPoint(count, clipped, best, eClipCullNone);
697 void Line_BestPoint(const Matrix4& local2view, const PointVertex vertices[2], SelectionIntersection& best)
700 const std::size_t count = matrix4_clip_line(local2view, vertex3f_to_vector3(vertices[0].vertex), vertex3f_to_vector3(vertices[1].vertex), clipped);
701 BestPoint(count, clipped, best, eClipCullNone);
704 void Circle_BestPoint(const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best)
707 for(std::size_t i=0; i<size; ++i)
709 const std::size_t count = matrix4_clip_triangle(local2view, g_vector3_identity, vertex3f_to_vector3(vertices[i].vertex), vertex3f_to_vector3(vertices[(i+1)%size].vertex), clipped);
710 BestPoint(count, clipped, best, cull);
714 void Quad_BestPoint(const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, SelectionIntersection& best)
718 const std::size_t count = matrix4_clip_triangle(local2view, vertex3f_to_vector3(vertices[0].vertex), vertex3f_to_vector3(vertices[1].vertex), vertex3f_to_vector3(vertices[3].vertex), clipped);
719 BestPoint(count, clipped, best, cull);
722 const std::size_t count = matrix4_clip_triangle(local2view, vertex3f_to_vector3(vertices[1].vertex), vertex3f_to_vector3(vertices[2].vertex), vertex3f_to_vector3(vertices[3].vertex), clipped);
723 BestPoint(count, clipped, best, cull);
727 struct FlatShadedVertex
739 typedef FlatShadedVertex* FlatShadedVertexIterator;
740 void Triangles_BestPoint(const Matrix4& local2view, clipcull_t cull, FlatShadedVertexIterator first, FlatShadedVertexIterator last, SelectionIntersection& best)
742 for(FlatShadedVertexIterator x(first), y(first+1), z(first+2); x != last; x += 3, y += 3, z +=3)
746 matrix4_clip_triangle(
748 reinterpret_cast<const Vector3&>((*x).vertex),
749 reinterpret_cast<const Vector3&>((*y).vertex),
750 reinterpret_cast<const Vector3&>((*z).vertex),
761 typedef std::multimap<SelectionIntersection, Selectable*> SelectableSortedSet;
763 class SelectionPool : public Selector
765 SelectableSortedSet m_pool;
766 SelectionIntersection m_intersection;
767 Selectable* m_selectable;
770 void pushSelectable(Selectable& selectable)
772 m_intersection = SelectionIntersection();
773 m_selectable = &selectable;
777 addSelectable(m_intersection, m_selectable);
778 m_intersection = SelectionIntersection();
780 void addIntersection(const SelectionIntersection& intersection)
782 assign_if_closer(m_intersection, intersection);
784 void addSelectable(const SelectionIntersection& intersection, Selectable* selectable)
786 if(intersection.valid())
788 m_pool.insert(SelectableSortedSet::value_type(intersection, selectable));
792 typedef SelectableSortedSet::iterator iterator;
796 return m_pool.begin();
805 return m_pool.empty();
810 const Colour4b g_colour_sphere(0, 0, 0, 255);
811 const Colour4b g_colour_screen(0, 255, 255, 255);
812 const Colour4b g_colour_selected(255, 255, 0, 255);
814 inline const Colour4b& colourSelected(const Colour4b& colour, bool selected)
816 return (selected) ? g_colour_selected : colour;
819 template<typename remap_policy>
820 inline void draw_semicircle(const std::size_t segments, const float radius, PointVertex* vertices, remap_policy remap)
822 const double increment = c_pi / double(segments << 2);
824 std::size_t count = 0;
827 remap_policy::set(vertices[segments << 2].vertex, -radius, 0, 0);
828 while(count < segments)
830 PointVertex* i = vertices + count;
831 PointVertex* j = vertices + ((segments << 1) - (count + 1));
833 PointVertex* k = i + (segments << 1);
834 PointVertex* l = j + (segments << 1);
837 PointVertex* m = i + (segments << 2);
838 PointVertex* n = j + (segments << 2);
839 PointVertex* o = k + (segments << 2);
840 PointVertex* p = l + (segments << 2);
843 remap_policy::set(i->vertex, x,-y, 0);
844 remap_policy::set(k->vertex,-y,-x, 0);
846 remap_policy::set(m->vertex,-x, y, 0);
847 remap_policy::set(o->vertex, y, x, 0);
853 const double theta = increment * count;
854 x = static_cast<float>(radius * cos(theta));
855 y = static_cast<float>(radius * sin(theta));
858 remap_policy::set(j->vertex, y,-x, 0);
859 remap_policy::set(l->vertex,-x,-y, 0);
861 remap_policy::set(n->vertex,-y, x, 0);
862 remap_policy::set(p->vertex, x, y, 0);
870 virtual Manipulatable* GetManipulatable() = 0;
871 virtual void testSelect(const View& view, const Matrix4& pivot2world)
874 virtual void render(Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world)
877 virtual void setSelected(bool select) = 0;
878 virtual bool isSelected() const = 0;
882 inline Vector3 normalised_safe(const Vector3& self)
884 if(vector3_equal(self, g_vector3_identity))
886 return g_vector3_identity;
888 return vector3_normalised(self);
892 class RotateManipulator : public Manipulator
894 struct RenderableCircle : public OpenGLRenderable
896 Array<PointVertex> m_vertices;
898 RenderableCircle(std::size_t size) : m_vertices(size)
901 void render(RenderStateFlags state) const
903 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_vertices.data()->colour);
904 glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_vertices.data()->vertex);
905 glDrawArrays(GL_LINE_LOOP, 0, GLsizei(m_vertices.size()));
907 void setColour(const Colour4b& colour)
909 for(Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i)
911 (*i).colour = colour;
916 struct RenderableSemiCircle : public OpenGLRenderable
918 Array<PointVertex> m_vertices;
920 RenderableSemiCircle(std::size_t size) : m_vertices(size)
923 void render(RenderStateFlags state) const
925 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_vertices.data()->colour);
926 glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_vertices.data()->vertex);
927 glDrawArrays(GL_LINE_STRIP, 0, GLsizei(m_vertices.size()));
929 void setColour(const Colour4b& colour)
931 for(Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i)
933 (*i).colour = colour;
940 Vector3 m_axis_screen;
941 RenderableSemiCircle m_circle_x;
942 RenderableSemiCircle m_circle_y;
943 RenderableSemiCircle m_circle_z;
944 RenderableCircle m_circle_screen;
945 RenderableCircle m_circle_sphere;
946 SelectableBool m_selectable_x;
947 SelectableBool m_selectable_y;
948 SelectableBool m_selectable_z;
949 SelectableBool m_selectable_screen;
950 SelectableBool m_selectable_sphere;
952 Matrix4 m_local2world_x;
953 Matrix4 m_local2world_y;
954 Matrix4 m_local2world_z;
955 bool m_circle_x_visible;
956 bool m_circle_y_visible;
957 bool m_circle_z_visible;
959 static Shader* m_state_outer;
961 RotateManipulator(Rotatable& rotatable, std::size_t segments, float radius) :
964 m_circle_x((segments << 2) + 1),
965 m_circle_y((segments << 2) + 1),
966 m_circle_z((segments << 2) + 1),
967 m_circle_screen(segments<<3),
968 m_circle_sphere(segments<<3)
970 draw_semicircle(segments, radius, m_circle_x.m_vertices.data(), RemapYZX());
971 draw_semicircle(segments, radius, m_circle_y.m_vertices.data(), RemapZXY());
972 draw_semicircle(segments, radius, m_circle_z.m_vertices.data(), RemapXYZ());
974 draw_circle(segments, radius * 1.15f, m_circle_screen.m_vertices.data(), RemapXYZ());
975 draw_circle(segments, radius, m_circle_sphere.m_vertices.data(), RemapXYZ());
981 m_circle_x.setColour(colourSelected(g_colour_x, m_selectable_x.isSelected()));
982 m_circle_y.setColour(colourSelected(g_colour_y, m_selectable_y.isSelected()));
983 m_circle_z.setColour(colourSelected(g_colour_z, m_selectable_z.isSelected()));
984 m_circle_screen.setColour(colourSelected(g_colour_screen, m_selectable_screen.isSelected()));
985 m_circle_sphere.setColour(colourSelected(g_colour_sphere, false));
988 void updateCircleTransforms()
990 Vector3 localViewpoint(matrix4_transformed_direction(matrix4_transposed(m_pivot.m_worldSpace), vector4_to_vector3(m_pivot.m_viewpointSpace.z())));
992 m_circle_x_visible = !vector3_equal_epsilon(g_vector3_axis_x, localViewpoint, 1e-6f);
993 if(m_circle_x_visible)
995 m_local2world_x = g_matrix4_identity;
996 vector4_to_vector3(m_local2world_x.y()) = normalised_safe(
997 vector3_cross(g_vector3_axis_x, localViewpoint)
999 vector4_to_vector3(m_local2world_x.z()) = normalised_safe(
1000 vector3_cross(vector4_to_vector3(m_local2world_x.x()), vector4_to_vector3(m_local2world_x.y()))
1002 matrix4_premultiply_by_matrix4(m_local2world_x, m_pivot.m_worldSpace);
1005 m_circle_y_visible = !vector3_equal_epsilon(g_vector3_axis_y, localViewpoint, 1e-6f);
1006 if(m_circle_y_visible)
1008 m_local2world_y = g_matrix4_identity;
1009 vector4_to_vector3(m_local2world_y.z()) = normalised_safe(
1010 vector3_cross(g_vector3_axis_y, localViewpoint)
1012 vector4_to_vector3(m_local2world_y.x()) = normalised_safe(
1013 vector3_cross(vector4_to_vector3(m_local2world_y.y()), vector4_to_vector3(m_local2world_y.z()))
1015 matrix4_premultiply_by_matrix4(m_local2world_y, m_pivot.m_worldSpace);
1018 m_circle_z_visible = !vector3_equal_epsilon(g_vector3_axis_z, localViewpoint, 1e-6f);
1019 if(m_circle_z_visible)
1021 m_local2world_z = g_matrix4_identity;
1022 vector4_to_vector3(m_local2world_z.x()) = normalised_safe(
1023 vector3_cross(g_vector3_axis_z, localViewpoint)
1025 vector4_to_vector3(m_local2world_z.y()) = normalised_safe(
1026 vector3_cross(vector4_to_vector3(m_local2world_z.z()), vector4_to_vector3(m_local2world_z.x()))
1028 matrix4_premultiply_by_matrix4(m_local2world_z, m_pivot.m_worldSpace);
1032 void render(Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world)
1034 m_pivot.update(pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport());
1035 updateCircleTransforms();
1040 renderer.SetState(m_state_outer, Renderer::eWireframeOnly);
1041 renderer.SetState(m_state_outer, Renderer::eFullMaterials);
1043 renderer.addRenderable(m_circle_screen, m_pivot.m_viewpointSpace);
1044 renderer.addRenderable(m_circle_sphere, m_pivot.m_viewpointSpace);
1046 if(m_circle_x_visible)
1048 renderer.addRenderable(m_circle_x, m_local2world_x);
1050 if(m_circle_y_visible)
1052 renderer.addRenderable(m_circle_y, m_local2world_y);
1054 if(m_circle_z_visible)
1056 renderer.addRenderable(m_circle_z, m_local2world_z);
1059 void testSelect(const View& view, const Matrix4& pivot2world)
1061 m_pivot.update(pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport());
1062 updateCircleTransforms();
1064 SelectionPool selector;
1068 Matrix4 local2view(matrix4_multiplied_by_matrix4(view.GetViewMatrix(), m_local2world_x));
1070 #if defined(DEBUG_SELECTION)
1071 g_render_clipped.construct(view.GetViewMatrix());
1074 SelectionIntersection best;
1075 LineStrip_BestPoint(local2view, m_circle_x.m_vertices.data(), m_circle_x.m_vertices.size(), best);
1076 selector.addSelectable(best, &m_selectable_x);
1080 Matrix4 local2view(matrix4_multiplied_by_matrix4(view.GetViewMatrix(), m_local2world_y));
1082 #if defined(DEBUG_SELECTION)
1083 g_render_clipped.construct(view.GetViewMatrix());
1086 SelectionIntersection best;
1087 LineStrip_BestPoint(local2view, m_circle_y.m_vertices.data(), m_circle_y.m_vertices.size(), best);
1088 selector.addSelectable(best, &m_selectable_y);
1092 Matrix4 local2view(matrix4_multiplied_by_matrix4(view.GetViewMatrix(), m_local2world_z));
1094 #if defined(DEBUG_SELECTION)
1095 g_render_clipped.construct(view.GetViewMatrix());
1098 SelectionIntersection best;
1099 LineStrip_BestPoint(local2view, m_circle_z.m_vertices.data(), m_circle_z.m_vertices.size(), best);
1100 selector.addSelectable(best, &m_selectable_z);
1105 Matrix4 local2view(matrix4_multiplied_by_matrix4(view.GetViewMatrix(), m_pivot.m_viewpointSpace));
1108 SelectionIntersection best;
1109 LineLoop_BestPoint(local2view, m_circle_screen.m_vertices.data(), m_circle_screen.m_vertices.size(), best);
1110 selector.addSelectable(best, &m_selectable_screen);
1114 SelectionIntersection best;
1115 Circle_BestPoint(local2view, eClipCullCW, m_circle_sphere.m_vertices.data(), m_circle_sphere.m_vertices.size(), best);
1116 selector.addSelectable(best, &m_selectable_sphere);
1120 m_axis_screen = m_pivot.m_axis_screen;
1122 if(!selector.failed())
1124 (*selector.begin()).second->setSelected(true);
1128 Manipulatable* GetManipulatable()
1130 if(m_selectable_x.isSelected())
1132 m_axis.SetAxis(g_vector3_axis_x);
1135 else if(m_selectable_y.isSelected())
1137 m_axis.SetAxis(g_vector3_axis_y);
1140 else if(m_selectable_z.isSelected())
1142 m_axis.SetAxis(g_vector3_axis_z);
1145 else if(m_selectable_screen.isSelected())
1147 m_axis.SetAxis(m_axis_screen);
1154 void setSelected(bool select)
1156 m_selectable_x.setSelected(select);
1157 m_selectable_y.setSelected(select);
1158 m_selectable_z.setSelected(select);
1159 m_selectable_screen.setSelected(select);
1161 bool isSelected() const
1163 return m_selectable_x.isSelected()
1164 | m_selectable_y.isSelected()
1165 | m_selectable_z.isSelected()
1166 | m_selectable_screen.isSelected()
1167 | m_selectable_sphere.isSelected();
1171 Shader* RotateManipulator::m_state_outer;
1174 const float arrowhead_length = 16;
1175 const float arrowhead_radius = 4;
1177 inline void draw_arrowline(const float length, PointVertex* line, const std::size_t axis)
1179 (*line++).vertex = vertex3f_identity;
1180 (*line).vertex = vertex3f_identity;
1181 vertex3f_to_array((*line).vertex)[axis] = length - arrowhead_length;
1184 template<typename VertexRemap, typename NormalRemap>
1185 inline void draw_arrowhead(const std::size_t segments, const float length, FlatShadedVertex* vertices, VertexRemap, NormalRemap)
1187 std::size_t head_tris = (segments << 3);
1188 const double head_segment = c_2pi / head_tris;
1189 for(std::size_t i = 0; i < head_tris; ++i)
1192 FlatShadedVertex& point = vertices[i*6+0];
1193 VertexRemap::x(point.vertex) = length - arrowhead_length;
1194 VertexRemap::y(point.vertex) = arrowhead_radius * static_cast<float>(cos(i * head_segment));
1195 VertexRemap::z(point.vertex) = arrowhead_radius * static_cast<float>(sin(i * head_segment));
1196 NormalRemap::x(point.normal) = arrowhead_radius / arrowhead_length;
1197 NormalRemap::y(point.normal) = static_cast<float>(cos(i * head_segment));
1198 NormalRemap::z(point.normal) = static_cast<float>(sin(i * head_segment));
1201 FlatShadedVertex& point = vertices[i*6+1];
1202 VertexRemap::x(point.vertex) = length;
1203 VertexRemap::y(point.vertex) = 0;
1204 VertexRemap::z(point.vertex) = 0;
1205 NormalRemap::x(point.normal) = arrowhead_radius / arrowhead_length;
1206 NormalRemap::y(point.normal) = static_cast<float>(cos((i + 0.5) * head_segment));
1207 NormalRemap::z(point.normal) = static_cast<float>(sin((i + 0.5) * head_segment));
1210 FlatShadedVertex& point = vertices[i*6+2];
1211 VertexRemap::x(point.vertex) = length - arrowhead_length;
1212 VertexRemap::y(point.vertex) = arrowhead_radius * static_cast<float>(cos((i+1) * head_segment));
1213 VertexRemap::z(point.vertex) = arrowhead_radius * static_cast<float>(sin((i+1) * head_segment));
1214 NormalRemap::x(point.normal) = arrowhead_radius / arrowhead_length;
1215 NormalRemap::y(point.normal) = static_cast<float>(cos((i+1) * head_segment));
1216 NormalRemap::z(point.normal) = static_cast<float>(sin((i+1) * head_segment));
1220 FlatShadedVertex& point = vertices[i*6+3];
1221 VertexRemap::x(point.vertex) = length - arrowhead_length;
1222 VertexRemap::y(point.vertex) = 0;
1223 VertexRemap::z(point.vertex) = 0;
1224 NormalRemap::x(point.normal) = -1;
1225 NormalRemap::y(point.normal) = 0;
1226 NormalRemap::z(point.normal) = 0;
1229 FlatShadedVertex& point = vertices[i*6+4];
1230 VertexRemap::x(point.vertex) = length - arrowhead_length;
1231 VertexRemap::y(point.vertex) = arrowhead_radius * static_cast<float>(cos(i * head_segment));
1232 VertexRemap::z(point.vertex) = arrowhead_radius * static_cast<float>(sin(i * head_segment));
1233 NormalRemap::x(point.normal) = -1;
1234 NormalRemap::y(point.normal) = 0;
1235 NormalRemap::z(point.normal) = 0;
1238 FlatShadedVertex& point = vertices[i*6+5];
1239 VertexRemap::x(point.vertex) = length - arrowhead_length;
1240 VertexRemap::y(point.vertex) = arrowhead_radius * static_cast<float>(cos((i+1) * head_segment));
1241 VertexRemap::z(point.vertex) = arrowhead_radius * static_cast<float>(sin((i+1) * head_segment));
1242 NormalRemap::x(point.normal) = -1;
1243 NormalRemap::y(point.normal) = 0;
1244 NormalRemap::z(point.normal) = 0;
1249 template<typename Triple>
1250 class TripleRemapXYZ
1253 static float& x(Triple& triple)
1257 static float& y(Triple& triple)
1261 static float& z(Triple& triple)
1267 template<typename Triple>
1268 class TripleRemapYZX
1271 static float& x(Triple& triple)
1275 static float& y(Triple& triple)
1279 static float& z(Triple& triple)
1285 template<typename Triple>
1286 class TripleRemapZXY
1289 static float& x(Triple& triple)
1293 static float& y(Triple& triple)
1297 static float& z(Triple& triple)
1303 void vector3_print(const Vector3& v)
1305 globalOutputStream() << "( " << v.x() << " " << v.y() << " " << v.z() << " )";
1308 class TranslateManipulator : public Manipulator
1310 struct RenderableArrowLine : public OpenGLRenderable
1312 PointVertex m_line[2];
1314 RenderableArrowLine()
1317 void render(RenderStateFlags state) const
1319 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_line[0].colour);
1320 glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_line[0].vertex);
1321 glDrawArrays(GL_LINES, 0, 2);
1323 void setColour(const Colour4b& colour)
1325 m_line[0].colour = colour;
1326 m_line[1].colour = colour;
1329 struct RenderableArrowHead : public OpenGLRenderable
1331 Array<FlatShadedVertex> m_vertices;
1333 RenderableArrowHead(std::size_t size)
1337 void render(RenderStateFlags state) const
1339 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(FlatShadedVertex), &m_vertices.data()->colour);
1340 glVertexPointer(3, GL_FLOAT, sizeof(FlatShadedVertex), &m_vertices.data()->vertex);
1341 glNormalPointer(GL_FLOAT, sizeof(FlatShadedVertex), &m_vertices.data()->normal);
1342 glDrawArrays(GL_TRIANGLES, 0, GLsizei(m_vertices.size()));
1344 void setColour(const Colour4b& colour)
1346 for(Array<FlatShadedVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i)
1348 (*i).colour = colour;
1352 struct RenderableQuad : public OpenGLRenderable
1354 PointVertex m_quad[4];
1355 void render(RenderStateFlags state) const
1357 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_quad[0].colour);
1358 glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_quad[0].vertex);
1359 glDrawArrays(GL_LINE_LOOP, 0, 4);
1361 void setColour(const Colour4b& colour)
1363 m_quad[0].colour = colour;
1364 m_quad[1].colour = colour;
1365 m_quad[2].colour = colour;
1366 m_quad[3].colour = colour;
1370 TranslateFree m_free;
1371 TranslateAxis m_axis;
1372 RenderableArrowLine m_arrow_x;
1373 RenderableArrowLine m_arrow_y;
1374 RenderableArrowLine m_arrow_z;
1375 RenderableArrowHead m_arrow_head_x;
1376 RenderableArrowHead m_arrow_head_y;
1377 RenderableArrowHead m_arrow_head_z;
1378 RenderableQuad m_quad_screen;
1379 SelectableBool m_selectable_x;
1380 SelectableBool m_selectable_y;
1381 SelectableBool m_selectable_z;
1382 SelectableBool m_selectable_screen;
1383 Pivot2World m_pivot;
1385 static Shader* m_state_wire;
1386 static Shader* m_state_fill;
1388 TranslateManipulator(Translatable& translatable, std::size_t segments, float length) :
1389 m_free(translatable),
1390 m_axis(translatable),
1391 m_arrow_head_x(3 * 2 * (segments << 3)),
1392 m_arrow_head_y(3 * 2 * (segments << 3)),
1393 m_arrow_head_z(3 * 2 * (segments << 3))
1395 draw_arrowline(length, m_arrow_x.m_line, 0);
1396 draw_arrowhead(segments, length, m_arrow_head_x.m_vertices.data(), TripleRemapXYZ<Vertex3f>(), TripleRemapXYZ<Normal3f>());
1397 draw_arrowline(length, m_arrow_y.m_line, 1);
1398 draw_arrowhead(segments, length, m_arrow_head_y.m_vertices.data(), TripleRemapYZX<Vertex3f>(), TripleRemapYZX<Normal3f>());
1399 draw_arrowline(length, m_arrow_z.m_line, 2);
1400 draw_arrowhead(segments, length, m_arrow_head_z.m_vertices.data(), TripleRemapZXY<Vertex3f>(), TripleRemapZXY<Normal3f>());
1402 draw_quad(16, m_quad_screen.m_quad);
1405 void UpdateColours()
1407 m_arrow_x.setColour(colourSelected(g_colour_x, m_selectable_x.isSelected()));
1408 m_arrow_head_x.setColour(colourSelected(g_colour_x, m_selectable_x.isSelected()));
1409 m_arrow_y.setColour(colourSelected(g_colour_y, m_selectable_y.isSelected()));
1410 m_arrow_head_y.setColour(colourSelected(g_colour_y, m_selectable_y.isSelected()));
1411 m_arrow_z.setColour(colourSelected(g_colour_z, m_selectable_z.isSelected()));
1412 m_arrow_head_z.setColour(colourSelected(g_colour_z, m_selectable_z.isSelected()));
1413 m_quad_screen.setColour(colourSelected(g_colour_screen, m_selectable_screen.isSelected()));
1416 bool manipulator_show_axis(const Pivot2World& pivot, const Vector3& axis)
1418 return fabs(vector3_dot(pivot.m_axis_screen, axis)) < 0.95;
1421 void render(Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world)
1423 m_pivot.update(pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport());
1428 Vector3 x = vector3_normalised(vector4_to_vector3(m_pivot.m_worldSpace.x()));
1429 bool show_x = manipulator_show_axis(m_pivot, x);
1431 Vector3 y = vector3_normalised(vector4_to_vector3(m_pivot.m_worldSpace.y()));
1432 bool show_y = manipulator_show_axis(m_pivot, y);
1434 Vector3 z = vector3_normalised(vector4_to_vector3(m_pivot.m_worldSpace.z()));
1435 bool show_z = manipulator_show_axis(m_pivot, z);
1437 renderer.SetState(m_state_wire, Renderer::eWireframeOnly);
1438 renderer.SetState(m_state_wire, Renderer::eFullMaterials);
1442 renderer.addRenderable(m_arrow_x, m_pivot.m_worldSpace);
1446 renderer.addRenderable(m_arrow_y, m_pivot.m_worldSpace);
1450 renderer.addRenderable(m_arrow_z, m_pivot.m_worldSpace);
1453 renderer.addRenderable(m_quad_screen, m_pivot.m_viewplaneSpace);
1455 renderer.SetState(m_state_fill, Renderer::eWireframeOnly);
1456 renderer.SetState(m_state_fill, Renderer::eFullMaterials);
1460 renderer.addRenderable(m_arrow_head_x, m_pivot.m_worldSpace);
1464 renderer.addRenderable(m_arrow_head_y, m_pivot.m_worldSpace);
1468 renderer.addRenderable(m_arrow_head_z, m_pivot.m_worldSpace);
1471 void testSelect(const View& view, const Matrix4& pivot2world)
1473 m_pivot.update(pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport());
1475 SelectionPool selector;
1477 Vector3 x = vector3_normalised(vector4_to_vector3(m_pivot.m_worldSpace.x()));
1478 bool show_x = manipulator_show_axis(m_pivot, x);
1480 Vector3 y = vector3_normalised(vector4_to_vector3(m_pivot.m_worldSpace.y()));
1481 bool show_y = manipulator_show_axis(m_pivot, y);
1483 Vector3 z = vector3_normalised(vector4_to_vector3(m_pivot.m_worldSpace.z()));
1484 bool show_z = manipulator_show_axis(m_pivot, z);
1487 Matrix4 local2view(matrix4_multiplied_by_matrix4(view.GetViewMatrix(), m_pivot.m_viewpointSpace));
1490 SelectionIntersection best;
1491 Quad_BestPoint(local2view, eClipCullCW, m_quad_screen.m_quad, best);
1494 best = SelectionIntersection(0, 0);
1495 selector.addSelectable(best, &m_selectable_screen);
1501 Matrix4 local2view(matrix4_multiplied_by_matrix4(view.GetViewMatrix(), m_pivot.m_worldSpace));
1503 #if defined(DEBUG_SELECTION)
1504 g_render_clipped.construct(view.GetViewMatrix());
1509 SelectionIntersection best;
1510 Line_BestPoint(local2view, m_arrow_x.m_line, best);
1511 Triangles_BestPoint(local2view, eClipCullCW, m_arrow_head_x.m_vertices.begin(), m_arrow_head_x.m_vertices.end(), best);
1512 selector.addSelectable(best, &m_selectable_x);
1517 SelectionIntersection best;
1518 Line_BestPoint(local2view, m_arrow_y.m_line, best);
1519 Triangles_BestPoint(local2view, eClipCullCW, m_arrow_head_y.m_vertices.begin(), m_arrow_head_y.m_vertices.end(), best);
1520 selector.addSelectable(best, &m_selectable_y);
1525 SelectionIntersection best;
1526 Line_BestPoint(local2view, m_arrow_z.m_line, best);
1527 Triangles_BestPoint(local2view, eClipCullCW, m_arrow_head_z.m_vertices.begin(), m_arrow_head_z.m_vertices.end(), best);
1528 selector.addSelectable(best, &m_selectable_z);
1532 if(!selector.failed())
1534 (*selector.begin()).second->setSelected(true);
1538 Manipulatable* GetManipulatable()
1540 if(m_selectable_x.isSelected())
1542 m_axis.SetAxis(g_vector3_axis_x);
1545 else if(m_selectable_y.isSelected())
1547 m_axis.SetAxis(g_vector3_axis_y);
1550 else if(m_selectable_z.isSelected())
1552 m_axis.SetAxis(g_vector3_axis_z);
1561 void setSelected(bool select)
1563 m_selectable_x.setSelected(select);
1564 m_selectable_y.setSelected(select);
1565 m_selectable_z.setSelected(select);
1566 m_selectable_screen.setSelected(select);
1568 bool isSelected() const
1570 return m_selectable_x.isSelected()
1571 | m_selectable_y.isSelected()
1572 | m_selectable_z.isSelected()
1573 | m_selectable_screen.isSelected();
1577 Shader* TranslateManipulator::m_state_wire;
1578 Shader* TranslateManipulator::m_state_fill;
1580 class ScaleManipulator : public Manipulator
1582 struct RenderableArrow : public OpenGLRenderable
1584 PointVertex m_line[2];
1586 void render(RenderStateFlags state) const
1588 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_line[0].colour);
1589 glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_line[0].vertex);
1590 glDrawArrays(GL_LINES, 0, 2);
1592 void setColour(const Colour4b& colour)
1594 m_line[0].colour = colour;
1595 m_line[1].colour = colour;
1598 struct RenderableQuad : public OpenGLRenderable
1600 PointVertex m_quad[4];
1601 void render(RenderStateFlags state) const
1603 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(PointVertex), &m_quad[0].colour);
1604 glVertexPointer(3, GL_FLOAT, sizeof(PointVertex), &m_quad[0].vertex);
1605 glDrawArrays(GL_QUADS, 0, 4);
1607 void setColour(const Colour4b& colour)
1609 m_quad[0].colour = colour;
1610 m_quad[1].colour = colour;
1611 m_quad[2].colour = colour;
1612 m_quad[3].colour = colour;
1618 RenderableArrow m_arrow_x;
1619 RenderableArrow m_arrow_y;
1620 RenderableArrow m_arrow_z;
1621 RenderableQuad m_quad_screen;
1622 SelectableBool m_selectable_x;
1623 SelectableBool m_selectable_y;
1624 SelectableBool m_selectable_z;
1625 SelectableBool m_selectable_screen;
1626 Pivot2World m_pivot;
1628 ScaleManipulator(Scalable& scalable, std::size_t segments, float length) :
1632 draw_arrowline(length, m_arrow_x.m_line, 0);
1633 draw_arrowline(length, m_arrow_y.m_line, 1);
1634 draw_arrowline(length, m_arrow_z.m_line, 2);
1636 draw_quad(16, m_quad_screen.m_quad);
1639 Pivot2World& getPivot()
1644 void UpdateColours()
1646 m_arrow_x.setColour(colourSelected(g_colour_x, m_selectable_x.isSelected()));
1647 m_arrow_y.setColour(colourSelected(g_colour_y, m_selectable_y.isSelected()));
1648 m_arrow_z.setColour(colourSelected(g_colour_z, m_selectable_z.isSelected()));
1649 m_quad_screen.setColour(colourSelected(g_colour_screen, m_selectable_screen.isSelected()));
1652 void render(Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world)
1654 m_pivot.update(pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport());
1659 renderer.addRenderable(m_arrow_x, m_pivot.m_worldSpace);
1660 renderer.addRenderable(m_arrow_y, m_pivot.m_worldSpace);
1661 renderer.addRenderable(m_arrow_z, m_pivot.m_worldSpace);
1663 renderer.addRenderable(m_quad_screen, m_pivot.m_viewpointSpace);
1665 void testSelect(const View& view, const Matrix4& pivot2world)
1667 m_pivot.update(pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport());
1669 SelectionPool selector;
1672 Matrix4 local2view(matrix4_multiplied_by_matrix4(view.GetViewMatrix(), m_pivot.m_worldSpace));
1674 #if defined(DEBUG_SELECTION)
1675 g_render_clipped.construct(view.GetViewMatrix());
1679 SelectionIntersection best;
1680 Line_BestPoint(local2view, m_arrow_x.m_line, best);
1681 selector.addSelectable(best, &m_selectable_x);
1685 SelectionIntersection best;
1686 Line_BestPoint(local2view, m_arrow_y.m_line, best);
1687 selector.addSelectable(best, &m_selectable_y);
1691 SelectionIntersection best;
1692 Line_BestPoint(local2view, m_arrow_z.m_line, best);
1693 selector.addSelectable(best, &m_selectable_z);
1698 Matrix4 local2view(matrix4_multiplied_by_matrix4(view.GetViewMatrix(), m_pivot.m_viewpointSpace));
1701 SelectionIntersection best;
1702 Quad_BestPoint(local2view, eClipCullCW, m_quad_screen.m_quad, best);
1703 selector.addSelectable(best, &m_selectable_screen);
1707 if(!selector.failed())
1709 (*selector.begin()).second->setSelected(true);
1713 Manipulatable* GetManipulatable()
1715 if(m_selectable_x.isSelected())
1717 m_axis.SetAxis(g_vector3_axis_x);
1720 else if(m_selectable_y.isSelected())
1722 m_axis.SetAxis(g_vector3_axis_y);
1725 else if(m_selectable_z.isSelected())
1727 m_axis.SetAxis(g_vector3_axis_z);
1734 void setSelected(bool select)
1736 m_selectable_x.setSelected(select);
1737 m_selectable_y.setSelected(select);
1738 m_selectable_z.setSelected(select);
1739 m_selectable_screen.setSelected(select);
1741 bool isSelected() const
1743 return m_selectable_x.isSelected()
1744 | m_selectable_y.isSelected()
1745 | m_selectable_z.isSelected()
1746 | m_selectable_screen.isSelected();
1751 inline PlaneSelectable* Instance_getPlaneSelectable(scene::Instance& instance)
1753 return InstanceTypeCast<PlaneSelectable>::cast(instance);
1756 class PlaneSelectableSelectPlanes : public scene::Graph::Walker
1758 Selector& m_selector;
1759 SelectionTest& m_test;
1760 PlaneCallback m_selectedPlaneCallback;
1762 PlaneSelectableSelectPlanes(Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback)
1763 : m_selector(selector), m_test(test), m_selectedPlaneCallback(selectedPlaneCallback)
1766 bool pre(const scene::Path& path, scene::Instance& instance) const
1768 if(path.top().get().visible())
1770 Selectable* selectable = Instance_getSelectable(instance);
1771 if(selectable != 0 && selectable->isSelected())
1773 PlaneSelectable* planeSelectable = Instance_getPlaneSelectable(instance);
1774 if(planeSelectable != 0)
1776 planeSelectable->selectPlanes(m_selector, m_test, m_selectedPlaneCallback);
1784 class PlaneSelectableSelectReversedPlanes : public scene::Graph::Walker
1786 Selector& m_selector;
1787 const SelectedPlanes& m_selectedPlanes;
1789 PlaneSelectableSelectReversedPlanes(Selector& selector, const SelectedPlanes& selectedPlanes)
1790 : m_selector(selector), m_selectedPlanes(selectedPlanes)
1793 bool pre(const scene::Path& path, scene::Instance& instance) const
1795 if(path.top().get().visible())
1797 Selectable* selectable = Instance_getSelectable(instance);
1798 if(selectable != 0 && selectable->isSelected())
1800 PlaneSelectable* planeSelectable = Instance_getPlaneSelectable(instance);
1801 if(planeSelectable != 0)
1803 planeSelectable->selectReversedPlanes(m_selector, m_selectedPlanes);
1811 void Scene_forEachPlaneSelectable_selectPlanes(scene::Graph& graph, Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback)
1813 graph.traverse(PlaneSelectableSelectPlanes(selector, test, selectedPlaneCallback));
1816 void Scene_forEachPlaneSelectable_selectReversedPlanes(scene::Graph& graph, Selector& selector, const SelectedPlanes& selectedPlanes)
1818 graph.traverse(PlaneSelectableSelectReversedPlanes(selector, selectedPlanes));
1825 bool operator()(const Plane3& plane, const Plane3& other) const
1827 if(plane.a < other.a)
1831 if(other.a < plane.a)
1836 if(plane.b < other.b)
1840 if(other.b < plane.b)
1845 if(plane.c < other.c)
1849 if(other.c < plane.c)
1854 if(plane.d < other.d)
1858 if(other.d < plane.d)
1867 typedef std::set<Plane3, PlaneLess> PlaneSet;
1869 inline void PlaneSet_insert(PlaneSet& self, const Plane3& plane)
1874 inline bool PlaneSet_contains(const PlaneSet& self, const Plane3& plane)
1876 return self.find(plane) != self.end();
1880 class SelectedPlaneSet : public SelectedPlanes
1882 PlaneSet m_selectedPlanes;
1886 return m_selectedPlanes.empty();
1889 void insert(const Plane3& plane)
1891 PlaneSet_insert(m_selectedPlanes, plane);
1893 bool contains(const Plane3& plane) const
1895 return PlaneSet_contains(m_selectedPlanes, plane);
1897 typedef MemberCaller1<SelectedPlaneSet, const Plane3&, &SelectedPlaneSet::insert> InsertCaller;
1901 bool Scene_forEachPlaneSelectable_selectPlanes(scene::Graph& graph, Selector& selector, SelectionTest& test)
1903 SelectedPlaneSet selectedPlanes;
1905 Scene_forEachPlaneSelectable_selectPlanes(graph, selector, test, SelectedPlaneSet::InsertCaller(selectedPlanes));
1906 Scene_forEachPlaneSelectable_selectReversedPlanes(graph, selector, selectedPlanes);
1908 return !selectedPlanes.empty();
1911 void Scene_Translate_Component_Selected(scene::Graph& graph, const Vector3& translation);
1912 void Scene_Translate_Selected(scene::Graph& graph, const Vector3& translation);
1913 void Scene_TestSelect_Primitive(Selector& selector, SelectionTest& test, const VolumeTest& volume);
1914 void Scene_TestSelect_Component(Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode);
1915 void Scene_TestSelect_Component_Selected(Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode);
1916 void Scene_SelectAll_Component(bool select, SelectionSystem::EComponentMode componentMode);
1918 class ResizeTranslatable : public Translatable
1920 void translate(const Vector3& translation)
1922 Scene_Translate_Component_Selected(GlobalSceneGraph(), translation);
1926 class DragTranslatable : public Translatable
1928 void translate(const Vector3& translation)
1930 if(GlobalSelectionSystem().Mode() == SelectionSystem::eComponent)
1932 Scene_Translate_Component_Selected(GlobalSceneGraph(), translation);
1936 Scene_Translate_Selected(GlobalSceneGraph(), translation);
1941 class SelectionVolume : public SelectionTest
1943 Matrix4 m_local2view;
1949 SelectionVolume(const View& view)
1954 const VolumeTest& getVolume() const
1959 const Vector3& getNear() const
1963 const Vector3& getFar() const
1968 void BeginMesh(const Matrix4& localToWorld, bool twoSided)
1970 m_local2view = matrix4_multiplied_by_matrix4(m_view.GetViewMatrix(), localToWorld);
1972 // Cull back-facing polygons based on winding being clockwise or counter-clockwise.
1973 // Don't cull if the view is wireframe and the polygons are two-sided.
1974 m_cull = twoSided && !m_view.fill() ? eClipCullNone : (matrix4_handedness(localToWorld) == MATRIX4_RIGHTHANDED) ? eClipCullCW : eClipCullCCW;
1977 Matrix4 screen2world(matrix4_full_inverse(m_local2view));
1979 m_near = vector4_projected(
1980 matrix4_transformed_vector4(
1982 Vector4(0, 0, -1, 1)
1986 m_far = vector4_projected(
1987 matrix4_transformed_vector4(
1994 #if defined(DEBUG_SELECTION)
1995 g_render_clipped.construct(m_view.GetViewMatrix());
1998 void TestPoint(const Vector3& point, SelectionIntersection& best)
2001 if(matrix4_clip_point(m_local2view, point, clipped) == c_CLIP_PASS)
2003 best = select_point_from_clipped(clipped);
2006 void TestPolygon(const VertexPointer& vertices, std::size_t count, SelectionIntersection& best)
2009 for(std::size_t i=0; i+2<count; ++i)
2012 matrix4_clip_triangle(
2014 reinterpret_cast<const Vector3&>(vertices[0]),
2015 reinterpret_cast<const Vector3&>(vertices[i+1]),
2016 reinterpret_cast<const Vector3&>(vertices[i+2]),
2025 void TestLineLoop(const VertexPointer& vertices, std::size_t count, SelectionIntersection& best)
2030 for(VertexPointer::iterator i = vertices.begin(), end = i + count, prev = i + (count-1); i != end; prev = i, ++i)
2035 reinterpret_cast<const Vector3&>((*prev)),
2036 reinterpret_cast<const Vector3&>((*i)),
2045 void TestLineStrip(const VertexPointer& vertices, std::size_t count, SelectionIntersection& best)
2050 for(VertexPointer::iterator i = vertices.begin(), end = i + count, next = i + 1; next != end; i = next, ++next)
2055 reinterpret_cast<const Vector3&>((*i)),
2056 reinterpret_cast<const Vector3&>((*next)),
2065 void TestLines(const VertexPointer& vertices, std::size_t count, SelectionIntersection& best)
2070 for(VertexPointer::iterator i = vertices.begin(), end = i + count; i != end; i += 2)
2075 reinterpret_cast<const Vector3&>((*i)),
2076 reinterpret_cast<const Vector3&>((*(i+1))),
2085 void TestTriangles(const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best)
2088 for(IndexPointer::iterator i(indices.begin()); i != indices.end(); i += 3)
2091 matrix4_clip_triangle(
2093 reinterpret_cast<const Vector3&>(vertices[*i]),
2094 reinterpret_cast<const Vector3&>(vertices[*(i+1)]),
2095 reinterpret_cast<const Vector3&>(vertices[*(i+2)]),
2104 void TestQuads(const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best)
2107 for(IndexPointer::iterator i(indices.begin()); i != indices.end(); i += 4)
2110 matrix4_clip_triangle(
2112 reinterpret_cast<const Vector3&>(vertices[*i]),
2113 reinterpret_cast<const Vector3&>(vertices[*(i+1)]),
2114 reinterpret_cast<const Vector3&>(vertices[*(i+3)]),
2122 matrix4_clip_triangle(
2124 reinterpret_cast<const Vector3&>(vertices[*(i+1)]),
2125 reinterpret_cast<const Vector3&>(vertices[*(i+2)]),
2126 reinterpret_cast<const Vector3&>(vertices[*(i+3)]),
2135 void TestQuadStrip(const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best)
2138 for(IndexPointer::iterator i(indices.begin()); i+2 != indices.end(); i += 2)
2141 matrix4_clip_triangle(
2143 reinterpret_cast<const Vector3&>(vertices[*i]),
2144 reinterpret_cast<const Vector3&>(vertices[*(i+1)]),
2145 reinterpret_cast<const Vector3&>(vertices[*(i+2)]),
2153 matrix4_clip_triangle(
2155 reinterpret_cast<const Vector3&>(vertices[*(i+2)]),
2156 reinterpret_cast<const Vector3&>(vertices[*(i+1)]),
2157 reinterpret_cast<const Vector3&>(vertices[*(i+3)]),
2168 class SelectionCounter
2171 typedef const Selectable& first_argument_type;
2173 SelectionCounter(const SelectionChangeCallback& onchanged)
2174 : m_count(0), m_onchanged(onchanged)
2177 void operator()(const Selectable& selectable)
2179 if(selectable.isSelected())
2185 ASSERT_MESSAGE(m_count != 0, "selection counter underflow");
2189 m_onchanged(selectable);
2193 return m_count == 0;
2195 std::size_t size() const
2200 std::size_t m_count;
2201 SelectionChangeCallback m_onchanged;
2204 inline void ConstructSelectionTest(View& view, const rect_t selection_box)
2206 view.EnableScissor(selection_box.min[0], selection_box.max[0], selection_box.min[1], selection_box.max[1]);
2209 inline const rect_t SelectionBoxForPoint(const float device_point[2], const float device_epsilon[2])
2211 rect_t selection_box;
2212 selection_box.min[0] = device_point[0] - device_epsilon[0];
2213 selection_box.min[1] = device_point[1] - device_epsilon[1];
2214 selection_box.max[0] = device_point[0] + device_epsilon[0];
2215 selection_box.max[1] = device_point[1] + device_epsilon[1];
2216 return selection_box;
2219 inline const rect_t SelectionBoxForArea(const float device_point[2], const float device_delta[2])
2221 rect_t selection_box;
2222 selection_box.min[0] = (device_delta[0] < 0) ? (device_point[0] + device_delta[0]) : (device_point[0]);
2223 selection_box.min[1] = (device_delta[1] < 0) ? (device_point[1] + device_delta[1]) : (device_point[1]);
2224 selection_box.max[0] = (device_delta[0] > 0) ? (device_point[0] + device_delta[0]) : (device_point[0]);
2225 selection_box.max[1] = (device_delta[1] > 0) ? (device_point[1] + device_delta[1]) : (device_point[1]);
2226 return selection_box;
2229 Quaternion construct_local_rotation(const Quaternion& world, const Quaternion& localToWorld)
2231 return quaternion_normalised(quaternion_multiplied_by_quaternion(
2232 quaternion_normalised(quaternion_multiplied_by_quaternion(
2233 quaternion_inverse(localToWorld),
2240 inline void matrix4_assign_rotation(Matrix4& matrix, const Matrix4& other)
2242 matrix[0] = other[0];
2243 matrix[1] = other[1];
2244 matrix[2] = other[2];
2245 matrix[4] = other[4];
2246 matrix[5] = other[5];
2247 matrix[6] = other[6];
2248 matrix[8] = other[8];
2249 matrix[9] = other[9];
2250 matrix[10] = other[10];
2253 void matrix4_assign_rotation_for_pivot(Matrix4& matrix, scene::Instance& instance)
2255 Editable* editable = Node_getEditable(instance.path().top());
2258 matrix4_assign_rotation(matrix, matrix4_multiplied_by_matrix4(instance.localToWorld(), editable->getLocalPivot()));
2262 matrix4_assign_rotation(matrix, instance.localToWorld());
2266 inline bool Instance_isSelectedComponents(scene::Instance& instance)
2268 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable(instance);
2269 return componentSelectionTestable != 0
2270 && componentSelectionTestable->isSelectedComponents();
2273 class TranslateSelected : public SelectionSystem::Visitor
2275 const Vector3& m_translate;
2277 TranslateSelected(const Vector3& translate)
2278 : m_translate(translate)
2281 void visit(scene::Instance& instance) const
2283 Transformable* transform = Instance_getTransformable(instance);
2286 transform->setType(TRANSFORM_PRIMITIVE);
2287 transform->setTranslation(m_translate);
2292 void Scene_Translate_Selected(scene::Graph& graph, const Vector3& translation)
2294 if(GlobalSelectionSystem().countSelected() != 0)
2296 GlobalSelectionSystem().foreachSelected(TranslateSelected(translation));
2300 Vector3 get_local_pivot(const Vector3& world_pivot, const Matrix4& localToWorld)
2303 matrix4_transformed_point(
2304 matrix4_full_inverse(localToWorld),
2310 void translation_for_pivoted_rotation(Vector3& parent_translation, const Quaternion& local_rotation, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent)
2312 Vector3 local_pivot(get_local_pivot(world_pivot, localToWorld));
2314 Vector3 translation(
2317 matrix4_transformed_point(
2318 matrix4_rotation_for_quaternion_quantised(local_rotation),
2319 vector3_negated(local_pivot)
2324 //globalOutputStream() << "translation: " << translation << "\n";
2326 translation_local2object(parent_translation, translation, localToParent);
2328 //globalOutputStream() << "parent_translation: " << parent_translation << "\n";
2331 void translation_for_pivoted_scale(Vector3& parent_translation, const Vector3& local_scale, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent)
2333 Vector3 local_pivot(get_local_pivot(world_pivot, localToWorld));
2335 Vector3 translation(
2339 vector3_negated(local_pivot),
2345 translation_local2object(parent_translation, translation, localToParent);
2348 class rotate_selected : public SelectionSystem::Visitor
2350 const Quaternion& m_rotate;
2351 const Vector3& m_world_pivot;
2353 rotate_selected(const Quaternion& rotation, const Vector3& world_pivot)
2354 : m_rotate(rotation), m_world_pivot(world_pivot)
2357 void visit(scene::Instance& instance) const
2359 TransformNode* transformNode = Node_getTransformNode(instance.path().top());
2360 if(transformNode != 0)
2362 Transformable* transform = Instance_getTransformable(instance);
2365 transform->setType(TRANSFORM_PRIMITIVE);
2366 transform->setScale(c_scale_identity);
2367 transform->setTranslation(c_translation_identity);
2369 transform->setType(TRANSFORM_PRIMITIVE);
2370 transform->setRotation(m_rotate);
2373 Editable* editable = Node_getEditable(instance.path().top());
2374 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2376 Vector3 parent_translation;
2377 translation_for_pivoted_rotation(
2381 matrix4_multiplied_by_matrix4(instance.localToWorld(), localPivot),
2382 matrix4_multiplied_by_matrix4(transformNode->localToParent(), localPivot)
2385 transform->setTranslation(parent_translation);
2392 void Scene_Rotate_Selected(scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot)
2394 if(GlobalSelectionSystem().countSelected() != 0)
2396 GlobalSelectionSystem().foreachSelected(rotate_selected(rotation, world_pivot));
2400 class scale_selected : public SelectionSystem::Visitor
2402 const Vector3& m_scale;
2403 const Vector3& m_world_pivot;
2405 scale_selected(const Vector3& scaling, const Vector3& world_pivot)
2406 : m_scale(scaling), m_world_pivot(world_pivot)
2409 void visit(scene::Instance& instance) const
2411 TransformNode* transformNode = Node_getTransformNode(instance.path().top());
2412 if(transformNode != 0)
2414 Transformable* transform = Instance_getTransformable(instance);
2417 transform->setType(TRANSFORM_PRIMITIVE);
2418 transform->setScale(c_scale_identity);
2419 transform->setTranslation(c_translation_identity);
2421 transform->setType(TRANSFORM_PRIMITIVE);
2422 transform->setScale(m_scale);
2424 Editable* editable = Node_getEditable(instance.path().top());
2425 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2427 Vector3 parent_translation;
2428 translation_for_pivoted_scale(
2432 matrix4_multiplied_by_matrix4(instance.localToWorld(), localPivot),
2433 matrix4_multiplied_by_matrix4(transformNode->localToParent(), localPivot)
2436 transform->setTranslation(parent_translation);
2443 void Scene_Scale_Selected(scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot)
2445 if(GlobalSelectionSystem().countSelected() != 0)
2447 GlobalSelectionSystem().foreachSelected(scale_selected(scaling, world_pivot));
2452 class translate_component_selected : public SelectionSystem::Visitor
2454 const Vector3& m_translate;
2456 translate_component_selected(const Vector3& translate)
2457 : m_translate(translate)
2460 void visit(scene::Instance& instance) const
2462 Transformable* transform = Instance_getTransformable(instance);
2465 transform->setType(TRANSFORM_COMPONENT);
2466 transform->setTranslation(m_translate);
2471 void Scene_Translate_Component_Selected(scene::Graph& graph, const Vector3& translation)
2473 if(GlobalSelectionSystem().countSelected() != 0)
2475 GlobalSelectionSystem().foreachSelectedComponent(translate_component_selected(translation));
2479 class rotate_component_selected : public SelectionSystem::Visitor
2481 const Quaternion& m_rotate;
2482 const Vector3& m_world_pivot;
2484 rotate_component_selected(const Quaternion& rotation, const Vector3& world_pivot)
2485 : m_rotate(rotation), m_world_pivot(world_pivot)
2488 void visit(scene::Instance& instance) const
2490 Transformable* transform = Instance_getTransformable(instance);
2493 Vector3 parent_translation;
2494 translation_for_pivoted_rotation(parent_translation, m_rotate, m_world_pivot, instance.localToWorld(), Node_getTransformNode(instance.path().top())->localToParent());
2496 transform->setType(TRANSFORM_COMPONENT);
2497 transform->setRotation(m_rotate);
2498 transform->setTranslation(parent_translation);
2503 void Scene_Rotate_Component_Selected(scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot)
2505 if(GlobalSelectionSystem().countSelectedComponents() != 0)
2507 GlobalSelectionSystem().foreachSelectedComponent(rotate_component_selected(rotation, world_pivot));
2511 class scale_component_selected : public SelectionSystem::Visitor
2513 const Vector3& m_scale;
2514 const Vector3& m_world_pivot;
2516 scale_component_selected(const Vector3& scaling, const Vector3& world_pivot)
2517 : m_scale(scaling), m_world_pivot(world_pivot)
2520 void visit(scene::Instance& instance) const
2522 Transformable* transform = Instance_getTransformable(instance);
2525 Vector3 parent_translation;
2526 translation_for_pivoted_scale(parent_translation, m_scale, m_world_pivot, instance.localToWorld(), Node_getTransformNode(instance.path().top())->localToParent());
2528 transform->setType(TRANSFORM_COMPONENT);
2529 transform->setScale(m_scale);
2530 transform->setTranslation(parent_translation);
2535 void Scene_Scale_Component_Selected(scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot)
2537 if(GlobalSelectionSystem().countSelectedComponents() != 0)
2539 GlobalSelectionSystem().foreachSelectedComponent(scale_component_selected(scaling, world_pivot));
2544 class BooleanSelector : public Selector
2547 SelectionIntersection m_intersection;
2548 Selectable* m_selectable;
2550 BooleanSelector() : m_selected(false)
2554 void pushSelectable(Selectable& selectable)
2556 m_intersection = SelectionIntersection();
2557 m_selectable = &selectable;
2559 void popSelectable()
2561 if(m_intersection.valid())
2565 m_intersection = SelectionIntersection();
2567 void addIntersection(const SelectionIntersection& intersection)
2569 if(m_selectable->isSelected())
2571 assign_if_closer(m_intersection, intersection);
2581 class BestSelector : public Selector
2583 SelectionIntersection m_intersection;
2584 Selectable* m_selectable;
2585 SelectionIntersection m_bestIntersection;
2586 std::list<Selectable*> m_bestSelectable;
2588 BestSelector() : m_bestIntersection(SelectionIntersection()), m_bestSelectable(0)
2592 void pushSelectable(Selectable& selectable)
2594 m_intersection = SelectionIntersection();
2595 m_selectable = &selectable;
2597 void popSelectable()
2599 if(m_intersection.equalEpsilon(m_bestIntersection, 0.25f, 0.001f))
2601 m_bestSelectable.push_back(m_selectable);
2602 m_bestIntersection = m_intersection;
2604 else if(m_intersection < m_bestIntersection)
2606 m_bestSelectable.clear();
2607 m_bestSelectable.push_back(m_selectable);
2608 m_bestIntersection = m_intersection;
2610 m_intersection = SelectionIntersection();
2612 void addIntersection(const SelectionIntersection& intersection)
2614 assign_if_closer(m_intersection, intersection);
2617 std::list<Selectable*>& best()
2619 return m_bestSelectable;
2623 class DragManipulator : public Manipulator
2625 TranslateFree m_freeResize;
2626 TranslateFree m_freeDrag;
2627 ResizeTranslatable m_resize;
2628 DragTranslatable m_drag;
2629 SelectableBool m_dragSelectable;
2634 DragManipulator() : m_freeResize(m_resize), m_freeDrag(m_drag), m_selected(false)
2638 Manipulatable* GetManipulatable()
2640 return m_dragSelectable.isSelected() ? &m_freeDrag : &m_freeResize;
2643 void testSelect(const View& view, const Matrix4& pivot2world)
2645 SelectionPool selector;
2647 SelectionVolume test(view);
2649 if(GlobalSelectionSystem().Mode() == SelectionSystem::ePrimitive)
2651 BooleanSelector booleanSelector;
2653 Scene_TestSelect_Primitive(booleanSelector, test, view);
2655 if(booleanSelector.isSelected())
2657 selector.addSelectable(SelectionIntersection(0, 0), &m_dragSelectable);
2662 m_selected = Scene_forEachPlaneSelectable_selectPlanes(GlobalSceneGraph(), selector, test);
2667 BestSelector bestSelector;
2668 Scene_TestSelect_Component_Selected(bestSelector, test, view, GlobalSelectionSystem().ComponentMode());
2669 for(std::list<Selectable*>::iterator i = bestSelector.best().begin(); i != bestSelector.best().end(); ++i)
2671 if(!(*i)->isSelected())
2673 GlobalSelectionSystem().setSelectedAllComponents(false);
2676 selector.addSelectable(SelectionIntersection(0, 0), (*i));
2677 m_dragSelectable.setSelected(true);
2681 for(SelectionPool::iterator i = selector.begin(); i != selector.end(); ++i)
2683 (*i).second->setSelected(true);
2687 void setSelected(bool select)
2689 m_selected = select;
2690 m_dragSelectable.setSelected(select);
2692 bool isSelected() const
2694 return m_selected || m_dragSelectable.isSelected();
2698 class ClipManipulator : public Manipulator
2702 Manipulatable* GetManipulatable()
2704 ERROR_MESSAGE("clipper is not manipulatable");
2708 void setSelected(bool select)
2711 bool isSelected() const
2717 class select_all : public scene::Graph::Walker
2721 select_all(bool select)
2725 bool pre(const scene::Path& path, scene::Instance& instance) const
2727 Selectable* selectable = Instance_getSelectable(instance);
2730 selectable->setSelected(m_select);
2736 class select_all_component : public scene::Graph::Walker
2739 SelectionSystem::EComponentMode m_mode;
2741 select_all_component(bool select, SelectionSystem::EComponentMode mode)
2742 : m_select(select), m_mode(mode)
2745 bool pre(const scene::Path& path, scene::Instance& instance) const
2747 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable(instance);
2748 if(componentSelectionTestable)
2750 componentSelectionTestable->setSelectedComponents(m_select, m_mode);
2756 void Scene_SelectAll_Component(bool select, SelectionSystem::EComponentMode componentMode)
2758 GlobalSceneGraph().traverse(select_all_component(select, componentMode));
2762 // RadiantSelectionSystem
2763 class RadiantSelectionSystem :
2764 public SelectionSystem,
2765 public Translatable,
2770 mutable Matrix4 m_pivot2world;
2771 Matrix4 m_pivot2world_start;
2772 Matrix4 m_manip2pivot_start;
2773 Translation m_translation;
2774 Rotation m_rotation;
2777 static Shader* m_state;
2779 EManipulatorMode m_manipulator_mode;
2780 Manipulator* m_manipulator;
2785 EComponentMode m_componentmode;
2787 SelectionCounter m_count_primitive;
2788 SelectionCounter m_count_component;
2790 TranslateManipulator m_translate_manipulator;
2791 RotateManipulator m_rotate_manipulator;
2792 ScaleManipulator m_scale_manipulator;
2793 DragManipulator m_drag_manipulator;
2794 ClipManipulator m_clip_manipulator;
2796 typedef SelectionList<scene::Instance> selection_t;
2797 selection_t m_selection;
2798 selection_t m_component_selection;
2800 std::vector<SelectionChangeCallback> m_selectionChanged_callbacks;
2802 void ConstructPivot() const;
2803 mutable bool m_pivotChanged;
2804 bool m_pivot_moving;
2806 void Scene_TestSelect(Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode);
2808 bool nothingSelected() const
2810 return (Mode() == eComponent && m_count_component.empty())
2811 || (Mode() == ePrimitive && m_count_primitive.empty());
2824 RadiantSelectionSystem() :
2825 m_undo_begun(false),
2827 m_componentmode(eDefault),
2828 m_count_primitive(SelectionChangedCaller(*this)),
2829 m_count_component(SelectionChangedCaller(*this)),
2830 m_translate_manipulator(*this, 2, 64),
2831 m_rotate_manipulator(*this, 8, 64),
2832 m_scale_manipulator(*this, 0, 64),
2833 m_pivotChanged(false),
2834 m_pivot_moving(false)
2836 SetManipulatorMode(eTranslate);
2838 addSelectionChangeCallback(pivotChangedSelectionCaller(*this));
2839 AddGridChangeCallback(PivotChangedCaller(*this));
2841 void pivotChanged() const
2843 m_pivotChanged = true;
2844 SceneChangeNotify();
2846 typedef ConstMemberCaller<RadiantSelectionSystem, &RadiantSelectionSystem::pivotChanged> PivotChangedCaller;
2847 void pivotChangedSelection(const Selectable& selectable)
2851 typedef MemberCaller1<RadiantSelectionSystem, const Selectable&, &RadiantSelectionSystem::pivotChangedSelection> pivotChangedSelectionCaller;
2853 void SetMode(EMode mode)
2865 void SetComponentMode(EComponentMode mode)
2867 m_componentmode = mode;
2869 EComponentMode ComponentMode() const
2871 return m_componentmode;
2873 void SetManipulatorMode(EManipulatorMode mode)
2875 m_manipulator_mode = mode;
2876 switch(m_manipulator_mode)
2878 case eTranslate: m_manipulator = &m_translate_manipulator; break;
2879 case eRotate: m_manipulator = &m_rotate_manipulator; break;
2880 case eScale: m_manipulator = &m_scale_manipulator; break;
2881 case eDrag: m_manipulator = &m_drag_manipulator; break;
2882 case eClip: m_manipulator = &m_clip_manipulator; break;
2886 EManipulatorMode ManipulatorMode() const
2888 return m_manipulator_mode;
2891 SelectionChangeCallback getObserver(EMode mode)
2893 if(mode == ePrimitive)
2895 return makeCallback1(m_count_primitive);
2899 return makeCallback1(m_count_component);
2902 std::size_t countSelected() const
2904 return m_count_primitive.size();
2906 std::size_t countSelectedComponents() const
2908 return m_count_component.size();
2910 void onSelectedChanged(scene::Instance& instance, const Selectable& selectable)
2912 if(selectable.isSelected())
2914 m_selection.append(instance);
2918 m_selection.erase(instance);
2921 ASSERT_MESSAGE(m_selection.size() == m_count_primitive.size(), "selection-tracking error");
2923 void onComponentSelection(scene::Instance& instance, const Selectable& selectable)
2925 if(selectable.isSelected())
2927 m_component_selection.append(instance);
2931 m_component_selection.erase(instance);
2934 ASSERT_MESSAGE(m_component_selection.size() == m_count_component.size(), "selection-tracking error");
2936 scene::Instance& ultimateSelected() const
2938 ASSERT_MESSAGE(m_selection.size() > 0, "no instance selected");
2939 return m_selection.back();
2941 scene::Instance& penultimateSelected() const
2943 ASSERT_MESSAGE(m_selection.size() > 1, "only one instance selected");
2944 return *(*(--(--m_selection.end())));
2946 void setSelectedAll(bool selected)
2948 GlobalSceneGraph().traverse(select_all(selected));
2950 m_manipulator->setSelected(selected);
2952 void setSelectedAllComponents(bool selected)
2954 Scene_SelectAll_Component(selected, SelectionSystem::eVertex);
2955 Scene_SelectAll_Component(selected, SelectionSystem::eEdge);
2956 Scene_SelectAll_Component(selected, SelectionSystem::eFace);
2958 m_manipulator->setSelected(selected);
2961 void foreachSelected(const Visitor& visitor) const
2963 selection_t::const_iterator i = m_selection.begin();
2964 while(i != m_selection.end())
2966 visitor.visit(*(*(i++)));
2969 void foreachSelectedComponent(const Visitor& visitor) const
2971 selection_t::const_iterator i = m_component_selection.begin();
2972 while(i != m_component_selection.end())
2974 visitor.visit(*(*(i++)));
2978 void addSelectionChangeCallback(const SelectionChangeCallback& callback)
2980 m_selectionChanged_callbacks.push_back(callback);
2982 void selectionChanged(const Selectable& selectable)
2984 typedef Functor1Invoke<SelectionChangeCallback, const Selectable&> SelectionChangeCallbackInvoke;
2985 std::for_each(m_selectionChanged_callbacks.begin(), m_selectionChanged_callbacks.end(), SelectionChangeCallbackInvoke(selectable));
2987 typedef MemberCaller1<RadiantSelectionSystem, const Selectable&, &RadiantSelectionSystem::selectionChanged> SelectionChangedCaller;
2992 m_pivot2world_start = GetPivot2World();
2995 bool SelectManipulator(const View& view, const float device_point[2], const float device_epsilon[2])
2997 if(!nothingSelected() || (ManipulatorMode() == eDrag && Mode() == eComponent))
2999 #if defined (DEBUG_SELECTION)
3000 g_render_clipped.destroy();
3003 m_manipulator->setSelected(false);
3005 if(!nothingSelected() || (ManipulatorMode() == eDrag && Mode() == eComponent))
3007 View scissored(view);
3008 ConstructSelectionTest(scissored, SelectionBoxForPoint(device_point, device_epsilon));
3009 m_manipulator->testSelect(scissored, GetPivot2World());
3014 m_pivot_moving = m_manipulator->isSelected();
3019 pivot.update(GetPivot2World(), view.GetModelview(), view.GetProjection(), view.GetViewport());
3021 m_manip2pivot_start = matrix4_multiplied_by_matrix4(matrix4_full_inverse(m_pivot2world_start), pivot.m_worldSpace);
3023 Matrix4 device2manip;
3024 ConstructDevice2Manip(device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport());
3025 m_manipulator->GetManipulatable()->Construct(device2manip, device_point[0], device_point[1]);
3027 m_undo_begun = false;
3030 SceneChangeNotify();
3033 return m_pivot_moving;
3038 if(Mode() == eComponent)
3040 setSelectedAllComponents(false);
3044 setSelectedAll(false);
3048 void SelectPoint(const View& view, const float device_point[2], const float device_epsilon[2], RadiantSelectionSystem::EModifier modifier, bool face)
3050 ASSERT_MESSAGE(fabs(device_point[0]) <= 1.0f && fabs(device_point[1]) <= 1.0f, "point-selection error");
3051 if(modifier == eReplace)
3055 setSelectedAllComponents(false);
3063 #if defined (DEBUG_SELECTION)
3064 g_render_clipped.destroy();
3068 View scissored(view);
3069 ConstructSelectionTest(scissored, SelectionBoxForPoint(device_point, device_epsilon));
3071 SelectionVolume volume(scissored);
3072 SelectionPool selector;
3075 Scene_TestSelect_Component(selector, volume, scissored, eFace);
3079 Scene_TestSelect(selector, volume, scissored, Mode(), ComponentMode());
3082 if(!selector.failed())
3086 case RadiantSelectionSystem::eToggle:
3088 SelectableSortedSet::iterator best = selector.begin();
3089 // toggle selection of the object with least depth
3090 if((*best).second->isSelected())
3091 (*best).second->setSelected(false);
3093 (*best).second->setSelected(true);
3096 // if cycle mode not enabled, enable it
3097 case RadiantSelectionSystem::eReplace:
3100 (*selector.begin()).second->setSelected(true);
3103 // select the next object in the list from the one already selected
3104 case RadiantSelectionSystem::eCycle:
3106 SelectionPool::iterator i = selector.begin();
3107 while(i != selector.end())
3109 if((*i).second->isSelected())
3111 (*i).second->setSelected(false);
3113 if(i != selector.end())
3115 i->second->setSelected(true);
3119 selector.begin()->second->setSelected(true);
3134 void SelectArea(const View& view, const float device_point[2], const float device_delta[2], RadiantSelectionSystem::EModifier modifier, bool face)
3136 if(modifier == eReplace)
3140 setSelectedAllComponents(false);
3148 #if defined (DEBUG_SELECTION)
3149 g_render_clipped.destroy();
3153 View scissored(view);
3154 ConstructSelectionTest(scissored, SelectionBoxForArea(device_point, device_delta));
3156 SelectionVolume volume(scissored);
3160 Scene_TestSelect_Component(pool, volume, scissored, eFace);
3164 Scene_TestSelect(pool, volume, scissored, Mode(), ComponentMode());
3167 for(SelectionPool::iterator i = pool.begin(); i != pool.end(); ++i)
3169 (*i).second->setSelected(!(modifier == RadiantSelectionSystem::eToggle && (*i).second->isSelected()));
3175 void translate(const Vector3& translation)
3177 if(!nothingSelected())
3179 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
3181 m_translation = translation;
3183 m_pivot2world = m_pivot2world_start;
3184 matrix4_translate_by_vec3(m_pivot2world, translation);
3186 if(Mode() == eComponent)
3188 Scene_Translate_Component_Selected(GlobalSceneGraph(), m_translation);
3192 Scene_Translate_Selected(GlobalSceneGraph(), m_translation);
3195 SceneChangeNotify();
3198 void outputTranslation(TextOutputStream& ostream)
3200 ostream << " -xyz " << m_translation.x() << " " << m_translation.y() << " " << m_translation.z();
3202 void rotate(const Quaternion& rotation)
3204 if(!nothingSelected())
3206 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
3208 m_rotation = rotation;
3210 if(Mode() == eComponent)
3212 Scene_Rotate_Component_Selected(GlobalSceneGraph(), m_rotation, vector4_to_vector3(m_pivot2world.t()));
3214 matrix4_assign_rotation_for_pivot(m_pivot2world, m_component_selection.back());
3218 Scene_Rotate_Selected(GlobalSceneGraph(), m_rotation, vector4_to_vector3(m_pivot2world.t()));
3220 matrix4_assign_rotation_for_pivot(m_pivot2world, m_selection.back());
3223 SceneChangeNotify();
3226 void outputRotation(TextOutputStream& ostream)
3228 ostream << " -eulerXYZ " << m_rotation.x() << " " << m_rotation.y() << " " << m_rotation.z();
3230 void scale(const Vector3& scaling)
3232 if(!nothingSelected())
3236 if(Mode() == eComponent)
3238 Scene_Scale_Component_Selected(GlobalSceneGraph(), m_scale, vector4_to_vector3(m_pivot2world.t()));
3242 Scene_Scale_Selected(GlobalSceneGraph(), m_scale, vector4_to_vector3(m_pivot2world.t()));
3245 SceneChangeNotify();
3248 void outputScale(TextOutputStream& ostream)
3250 ostream << " -scale " << m_scale.x() << " " << m_scale.y() << " " << m_scale.z();
3253 void rotateSelected(const Quaternion& rotation)
3259 void translateSelected(const Vector3& translation)
3262 translate(translation);
3265 void scaleSelected(const Vector3& scaling)
3272 void MoveSelected(const View& view, const float device_point[2])
3274 if(m_manipulator->isSelected())
3278 m_undo_begun = true;
3279 GlobalUndoSystem().start();
3282 Matrix4 device2manip;
3283 ConstructDevice2Manip(device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport());
3284 m_manipulator->GetManipulatable()->Transform(m_manip2pivot_start, device2manip, device_point[0], device_point[1]);
3288 /// \todo Support view-dependent nudge.
3289 void NudgeManipulator(const Vector3& nudge, const Vector3& view)
3291 if(ManipulatorMode() == eTranslate)
3293 translateSelected(nudge);
3298 void freezeTransforms();
3300 void renderSolid(Renderer& renderer, const VolumeTest& volume) const;
3301 void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
3303 renderSolid(renderer, volume);
3306 const Matrix4& GetPivot2World() const
3309 return m_pivot2world;
3312 static void constructStatic()
3314 m_state = GlobalShaderCache().capture("$POINT");
3315 #if defined(DEBUG_SELECTION)
3316 g_state_clipped = GlobalShaderCache().capture("$DEBUG_CLIPPED");
3318 TranslateManipulator::m_state_wire = GlobalShaderCache().capture("$WIRE_OVERLAY");
3319 TranslateManipulator::m_state_fill = GlobalShaderCache().capture("$FLATSHADE_OVERLAY");
3320 RotateManipulator::m_state_outer = GlobalShaderCache().capture("$WIRE_OVERLAY");
3323 static void destroyStatic()
3325 #if defined(DEBUG_SELECTION)
3326 GlobalShaderCache().release("$DEBUG_CLIPPED");
3328 GlobalShaderCache().release("$WIRE_OVERLAY");
3329 GlobalShaderCache().release("$FLATSHADE_OVERLAY");
3330 GlobalShaderCache().release("$WIRE_OVERLAY");
3331 GlobalShaderCache().release("$POINT");
3335 Shader* RadiantSelectionSystem::m_state = 0;
3340 RadiantSelectionSystem* g_RadiantSelectionSystem;
3342 inline RadiantSelectionSystem& getSelectionSystem()
3344 ASSERT_NOTNULL(g_RadiantSelectionSystem);
3345 return *g_RadiantSelectionSystem;
3351 class testselect_entity_visible : public scene::Graph::Walker
3353 Selector& m_selector;
3354 SelectionTest& m_test;
3356 testselect_entity_visible(Selector& selector, SelectionTest& test)
3357 : m_selector(selector), m_test(test)
3360 bool pre(const scene::Path& path, scene::Instance& instance) const
3362 Selectable* selectable = Instance_getSelectable(instance);
3364 && Node_isEntity(path.top()))
3366 m_selector.pushSelectable(*selectable);
3369 SelectionTestable* selectionTestable = Instance_getSelectionTestable(instance);
3370 if(selectionTestable)
3372 selectionTestable->testSelect(m_selector, m_test);
3377 void post(const scene::Path& path, scene::Instance& instance) const
3379 Selectable* selectable = Instance_getSelectable(instance);
3381 && Node_isEntity(path.top()))
3383 m_selector.popSelectable();
3388 class testselect_primitive_visible : public scene::Graph::Walker
3390 Selector& m_selector;
3391 SelectionTest& m_test;
3393 testselect_primitive_visible(Selector& selector, SelectionTest& test)
3394 : m_selector(selector), m_test(test)
3397 bool pre(const scene::Path& path, scene::Instance& instance) const
3399 Selectable* selectable = Instance_getSelectable(instance);
3402 m_selector.pushSelectable(*selectable);
3405 SelectionTestable* selectionTestable = Instance_getSelectionTestable(instance);
3406 if(selectionTestable)
3408 selectionTestable->testSelect(m_selector, m_test);
3413 void post(const scene::Path& path, scene::Instance& instance) const
3415 Selectable* selectable = Instance_getSelectable(instance);
3418 m_selector.popSelectable();
3423 class testselect_component_visible : public scene::Graph::Walker
3425 Selector& m_selector;
3426 SelectionTest& m_test;
3427 SelectionSystem::EComponentMode m_mode;
3429 testselect_component_visible(Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode)
3430 : m_selector(selector), m_test(test), m_mode(mode)
3433 bool pre(const scene::Path& path, scene::Instance& instance) const
3435 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable(instance);
3436 if(componentSelectionTestable)
3438 componentSelectionTestable->testSelectComponents(m_selector, m_test, m_mode);
3446 class testselect_component_visible_selected : public scene::Graph::Walker
3448 Selector& m_selector;
3449 SelectionTest& m_test;
3450 SelectionSystem::EComponentMode m_mode;
3452 testselect_component_visible_selected(Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode)
3453 : m_selector(selector), m_test(test), m_mode(mode)
3456 bool pre(const scene::Path& path, scene::Instance& instance) const
3458 Selectable* selectable = Instance_getSelectable(instance);
3459 if(selectable != 0 && selectable->isSelected())
3461 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable(instance);
3462 if(componentSelectionTestable)
3464 componentSelectionTestable->testSelectComponents(m_selector, m_test, m_mode);
3472 void Scene_TestSelect_Primitive(Selector& selector, SelectionTest& test, const VolumeTest& volume)
3474 Scene_forEachVisible(GlobalSceneGraph(), volume, testselect_primitive_visible(selector, test));
3477 void Scene_TestSelect_Component_Selected(Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode)
3479 Scene_forEachVisible(GlobalSceneGraph(), volume, testselect_component_visible_selected(selector, test, componentMode));
3482 void Scene_TestSelect_Component(Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode)
3484 Scene_forEachVisible(GlobalSceneGraph(), volume, testselect_component_visible(selector, test, componentMode));
3487 void RadiantSelectionSystem::Scene_TestSelect(Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode)
3493 Scene_forEachVisible(GlobalSceneGraph(), view, testselect_entity_visible(selector, test));
3497 Scene_TestSelect_Primitive(selector, test, view);
3500 Scene_TestSelect_Component_Selected(selector, test, view, componentMode);
3505 class FreezeTransforms : public scene::Graph::Walker
3508 bool pre(const scene::Path& path, scene::Instance& instance) const
3510 TransformNode* transformNode = Node_getTransformNode(path.top());
3511 if(transformNode != 0)
3513 Transformable* transform = Instance_getTransformable(instance);
3516 transform->freezeTransform();
3523 void RadiantSelectionSystem::freezeTransforms()
3525 GlobalSceneGraph().traverse(FreezeTransforms());
3529 void RadiantSelectionSystem::endMove()
3533 if(Mode() == ePrimitive)
3535 if(ManipulatorMode() == eDrag)
3537 Scene_SelectAll_Component(false, SelectionSystem::eFace);
3541 m_pivot_moving = false;
3544 SceneChangeNotify();
3548 StringOutputStream command;
3550 if(ManipulatorMode() == eTranslate)
3552 command << "translateTool";
3553 outputTranslation(command);
3555 else if(ManipulatorMode() == eRotate)
3557 command << "rotateTool";
3558 outputRotation(command);
3560 else if(ManipulatorMode() == eScale)
3562 command << "scaleTool";
3563 outputScale(command);
3565 else if(ManipulatorMode() == eDrag)
3567 command << "dragTool";
3570 GlobalUndoSystem().finish(command.c_str());
3575 inline AABB Instance_getPivotBounds(scene::Instance& instance)
3577 Entity* entity = Node_getEntity(instance.path().top());
3579 && (entity->getEntityClass().fixedsize
3580 || !node_is_group(instance.path().top())))
3582 Editable* editable = Node_getEditable(instance.path().top());
3585 return AABB(matrix4_multiplied_by_matrix4(instance.localToWorld(), editable->getLocalPivot()).t(), Vector3(0, 0, 0));
3589 return AABB(instance.localToWorld().t(), Vector3(0, 0, 0));
3593 return instance.worldAABB();
3596 class bounds_selected : public scene::Graph::Walker
3600 bounds_selected(AABB& bounds)
3605 bool pre(const scene::Path& path, scene::Instance& instance) const
3607 Selectable* selectable = Instance_getSelectable(instance);
3609 && selectable->isSelected())
3611 aabb_extend_by_aabb_safe(m_bounds, Instance_getPivotBounds(instance));
3617 class bounds_selected_component : public scene::Graph::Walker
3621 bounds_selected_component(AABB& bounds)
3626 bool pre(const scene::Path& path, scene::Instance& instance) const
3628 Selectable* selectable = Instance_getSelectable(instance);
3630 && selectable->isSelected())
3632 ComponentEditable* componentEditable = Instance_getComponentEditable(instance);
3633 if(componentEditable)
3635 aabb_extend_by_aabb_safe(m_bounds, aabb_for_oriented_aabb_safe(componentEditable->getSelectedComponentsBounds(), instance.localToWorld()));
3642 void Scene_BoundsSelected(scene::Graph& graph, AABB& bounds)
3644 graph.traverse(bounds_selected(bounds));
3647 void Scene_BoundsSelectedComponent(scene::Graph& graph, AABB& bounds)
3649 graph.traverse(bounds_selected_component(bounds));
3653 inline void pivot_for_node(Matrix4& pivot, scene::Node& node, scene::Instance& instance)
3655 ComponentEditable* componentEditable = Instance_getComponentEditable(instance);
3656 if(GlobalSelectionSystem().Mode() == SelectionSystem::eComponent
3657 && componentEditable != 0)
3659 pivot = matrix4_translation_for_vec3(componentEditable->getSelectedComponentsBounds().origin);
3663 Bounded* bounded = Instance_getBounded(instance);
3666 pivot = matrix4_translation_for_vec3(bounded->localAABB().origin);
3670 pivot = g_matrix4_identity;
3676 void RadiantSelectionSystem::ConstructPivot() const
3678 if(!m_pivotChanged || m_pivot_moving)
3680 m_pivotChanged = false;
3682 Vector3 m_object_pivot;
3684 if(!nothingSelected())
3688 if(Mode() == eComponent)
3690 Scene_BoundsSelectedComponent(GlobalSceneGraph(), bounds);
3694 Scene_BoundsSelected(GlobalSceneGraph(), bounds);
3696 m_object_pivot = bounds.origin;
3699 vector3_snap(m_object_pivot, GetGridSize());
3700 m_pivot2world = matrix4_translation_for_vec3(m_object_pivot);
3702 switch(m_manipulator_mode)
3707 if(Mode() == eComponent)
3709 matrix4_assign_rotation_for_pivot(m_pivot2world, m_component_selection.back());
3713 matrix4_assign_rotation_for_pivot(m_pivot2world, m_selection.back());
3717 if(Mode() == eComponent)
3719 matrix4_assign_rotation_for_pivot(m_pivot2world, m_component_selection.back());
3723 matrix4_assign_rotation_for_pivot(m_pivot2world, m_selection.back());
3732 void RadiantSelectionSystem::renderSolid(Renderer& renderer, const VolumeTest& volume) const
3734 //if(view->TestPoint(m_object_pivot))
3735 if(!nothingSelected())
3737 renderer.Highlight(Renderer::ePrimitive, false);
3738 renderer.Highlight(Renderer::eFace, false);
3740 renderer.SetState(m_state, Renderer::eWireframeOnly);
3741 renderer.SetState(m_state, Renderer::eFullMaterials);
3743 m_manipulator->render(renderer, volume, GetPivot2World());
3746 #if defined(DEBUG_SELECTION)
3747 renderer.SetState(g_state_clipped, Renderer::eWireframeOnly);
3748 renderer.SetState(g_state_clipped, Renderer::eFullMaterials);
3749 renderer.addRenderable(g_render_clipped, g_render_clipped.m_world);
3754 void SelectionSystem_OnBoundsChanged()
3756 getSelectionSystem().pivotChanged();
3760 void SelectionSystem_Construct()
3762 RadiantSelectionSystem::constructStatic();
3764 g_RadiantSelectionSystem = new RadiantSelectionSystem;
3766 GlobalSceneGraph().addBoundsChangedCallback(FreeCaller<SelectionSystem_OnBoundsChanged>());
3768 GlobalShaderCache().attachRenderable(getSelectionSystem());
3771 void SelectionSystem_Destroy()
3773 GlobalShaderCache().detachRenderable(getSelectionSystem());
3775 GlobalSceneGraph().removeBoundsChangedCallback(FreeCaller<SelectionSystem_OnBoundsChanged>());
3777 delete g_RadiantSelectionSystem;
3779 RadiantSelectionSystem::destroyStatic();
3785 inline float screen_normalised(float pos, std::size_t size)
3787 return ((2.0f * pos) / size) - 1.0f;
3790 typedef Vector2 DeviceVector;
3792 inline DeviceVector window_to_normalised_device(WindowVector window, std::size_t width, std::size_t height)
3794 return DeviceVector(screen_normalised(window.x(), width), screen_normalised(height - 1 - window.y(), height));
3797 inline float device_constrained(float pos)
3799 return std::min(1.0f, std::max(-1.0f, pos));
3802 inline DeviceVector device_constrained(DeviceVector device)
3804 return DeviceVector(device_constrained(device.x()), device_constrained(device.y()));
3807 inline float window_constrained(float pos, std::size_t origin, std::size_t size)
3809 return std::min(static_cast<float>(origin + size), std::max(static_cast<float>(origin), pos));
3812 inline WindowVector window_constrained(WindowVector window, std::size_t x, std::size_t y, std::size_t width, std::size_t height)
3814 return WindowVector(window_constrained(window.x(), x, width), window_constrained(window.y(), y, height));
3817 typedef Callback1<DeviceVector> MouseEventCallback;
3819 Single<MouseEventCallback> g_mouseMovedCallback;
3820 Single<MouseEventCallback> g_mouseUpCallback;
3823 const ButtonIdentifier c_button_select = c_buttonLeft;
3824 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3825 const ModifierFlags c_modifier_toggle = c_modifierShift;
3826 const ModifierFlags c_modifier_replace = c_modifierShift | c_modifierAlt;
3827 const ModifierFlags c_modifier_face = c_modifierControl;
3829 const ButtonIdentifier c_button_select = c_buttonLeft;
3830 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3831 const ModifierFlags c_modifier_toggle = c_modifierControl;
3832 const ModifierFlags c_modifier_replace = c_modifierNone;
3833 const ModifierFlags c_modifier_face = c_modifierShift;
3835 const ModifierFlags c_modifier_toggle_face = c_modifier_toggle | c_modifier_face;
3836 const ModifierFlags c_modifier_replace_face = c_modifier_replace | c_modifier_face;
3838 const ButtonIdentifier c_button_texture = c_buttonMiddle;
3839 const ModifierFlags c_modifier_apply_texture = c_modifierControl | c_modifierShift;
3840 const ModifierFlags c_modifier_copy_texture = c_modifierNone;
3844 RadiantSelectionSystem::EModifier modifier_for_state(ModifierFlags state)
3846 if(state == c_modifier_toggle || state == c_modifier_toggle_face)
3848 return RadiantSelectionSystem::eToggle;
3850 if(state == c_modifier_replace || state == c_modifier_replace_face)
3852 return RadiantSelectionSystem::eReplace;
3854 return RadiantSelectionSystem::eManipulator;
3857 rect_t getDeviceArea() const
3859 DeviceVector delta(m_current - m_start);
3860 if(selecting() && fabs(delta.x()) > m_epsilon.x() && fabs(delta.y()) > m_epsilon.y())
3862 return SelectionBoxForArea(&m_start[0], &delta[0]);
3866 rect_t default_area = { { 0, 0, }, { 0, 0, }, };
3867 return default_area;
3872 DeviceVector m_start;
3873 DeviceVector m_current;
3874 DeviceVector m_epsilon;
3875 std::size_t m_unmoved_replaces;
3876 ModifierFlags m_state;
3878 RectangleCallback m_window_update;
3880 Selector_() : m_start(0.0f, 0.0f), m_current(0.0f, 0.0f), m_unmoved_replaces(0), m_state(c_modifierNone)
3886 m_window_update(getDeviceArea());
3889 void testSelect(DeviceVector position)
3891 RadiantSelectionSystem::EModifier modifier = modifier_for_state(m_state);
3892 if(modifier != RadiantSelectionSystem::eManipulator)
3894 DeviceVector delta(position - m_start);
3895 if(fabs(delta.x()) > m_epsilon.x() && fabs(delta.y()) > m_epsilon.y())
3897 DeviceVector delta(position - m_start);
3898 getSelectionSystem().SelectArea(*m_view, &m_start[0], &delta[0], modifier, (m_state & c_modifier_face) != c_modifierNone);
3902 if(modifier == RadiantSelectionSystem::eReplace && m_unmoved_replaces++ > 0)
3904 modifier = RadiantSelectionSystem::eCycle;
3906 getSelectionSystem().SelectPoint(*m_view, &position[0], &m_epsilon[0], modifier, (m_state & c_modifier_face) != c_modifierNone);
3910 m_start = m_current = DeviceVector(0.0f, 0.0f);
3914 bool selecting() const
3916 return m_state != c_modifier_manipulator;
3919 void setState(ModifierFlags state)
3921 bool was_selecting = selecting();
3923 if(was_selecting ^ selecting())
3929 ModifierFlags getState() const
3934 void modifierEnable(ModifierFlags type)
3936 setState(bitfield_enable(getState(), type));
3938 void modifierDisable(ModifierFlags type)
3940 setState(bitfield_disable(getState(), type));
3943 void mouseDown(DeviceVector position)
3945 m_start = m_current = device_constrained(position);
3948 void mouseMoved(DeviceVector position)
3950 m_current = device_constrained(position);
3953 typedef MemberCaller1<Selector_, DeviceVector, &Selector_::mouseMoved> MouseMovedCaller;
3955 void mouseUp(DeviceVector position)
3957 testSelect(device_constrained(position));
3959 g_mouseMovedCallback.clear();
3960 g_mouseUpCallback.clear();
3962 typedef MemberCaller1<Selector_, DeviceVector, &Selector_::mouseUp> MouseUpCaller;
3969 DeviceVector m_epsilon;
3972 bool mouseDown(DeviceVector position)
3974 return getSelectionSystem().SelectManipulator(*m_view, &position[0], &m_epsilon[0]);
3977 void mouseMoved(DeviceVector position)
3979 getSelectionSystem().MoveSelected(*m_view, &position[0]);
3981 typedef MemberCaller1<Manipulator_, DeviceVector, &Manipulator_::mouseMoved> MouseMovedCaller;
3983 void mouseUp(DeviceVector position)
3985 getSelectionSystem().endMove();
3986 g_mouseMovedCallback.clear();
3987 g_mouseUpCallback.clear();
3989 typedef MemberCaller1<Manipulator_, DeviceVector, &Manipulator_::mouseUp> MouseUpCaller;
3992 void Scene_copyClosestFaceTexture(SelectionTest& test);
3993 void Scene_applyClosestFaceTexture(SelectionTest& test);
3995 class RadiantWindowObserver : public SelectionSystemWindowObserver
4008 Selector_ m_selector;
4009 Manipulator_ m_manipulator;
4011 RadiantWindowObserver() : m_mouse_down(false)
4018 void setView(const View& view)
4020 m_selector.m_view = &view;
4021 m_manipulator.m_view = &view;
4023 void setRectangleDrawCallback(const RectangleCallback& callback)
4025 m_selector.m_window_update = callback;
4027 void onSizeChanged(int width, int height)
4031 DeviceVector epsilon(SELECT_EPSILON / static_cast<float>(m_width), SELECT_EPSILON / static_cast<float>(m_height));
4032 m_selector.m_epsilon = m_manipulator.m_epsilon = epsilon;
4034 void onMouseDown(const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers)
4036 if(button == c_button_select)
4038 m_mouse_down = true;
4040 DeviceVector devicePosition(window_to_normalised_device(position, m_width, m_height));
4041 if(modifiers == c_modifier_manipulator && m_manipulator.mouseDown(devicePosition))
4043 g_mouseMovedCallback.insert(MouseEventCallback(Manipulator_::MouseMovedCaller(m_manipulator)));
4044 g_mouseUpCallback.insert(MouseEventCallback(Manipulator_::MouseUpCaller(m_manipulator)));
4048 m_selector.mouseDown(devicePosition);
4049 g_mouseMovedCallback.insert(MouseEventCallback(Selector_::MouseMovedCaller(m_selector)));
4050 g_mouseUpCallback.insert(MouseEventCallback(Selector_::MouseUpCaller(m_selector)));
4053 else if(button == c_button_texture)
4055 DeviceVector devicePosition(device_constrained(window_to_normalised_device(position, m_width, m_height)));
4057 View scissored(*m_selector.m_view);
4058 ConstructSelectionTest(scissored, SelectionBoxForPoint(&devicePosition[0], &m_selector.m_epsilon[0]));
4059 SelectionVolume volume(scissored);
4061 if(modifiers == c_modifier_apply_texture)
4063 Scene_applyClosestFaceTexture(volume);
4065 else if(modifiers == c_modifier_copy_texture)
4067 Scene_copyClosestFaceTexture(volume);
4071 void onMouseMotion(const WindowVector& position, ModifierFlags modifiers)
4073 m_selector.m_unmoved_replaces = 0;
4075 if(m_mouse_down && !g_mouseMovedCallback.empty())
4077 g_mouseMovedCallback.get()(window_to_normalised_device(position, m_width, m_height));
4080 void onMouseUp(const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers)
4082 if(button == c_button_select && !g_mouseUpCallback.empty())
4084 m_mouse_down = false;
4086 g_mouseUpCallback.get()(window_to_normalised_device(position, m_width, m_height));
4089 void onModifierDown(ModifierFlags type)
4091 m_selector.modifierEnable(type);
4093 void onModifierUp(ModifierFlags type)
4095 m_selector.modifierDisable(type);
4101 SelectionSystemWindowObserver* NewWindowObserver()
4103 return new RadiantWindowObserver;
4108 #include "modulesystem/singletonmodule.h"
4109 #include "modulesystem/moduleregistry.h"
4111 class SelectionDependencies :
4112 public GlobalSceneGraphModuleRef,
4113 public GlobalShaderCacheModuleRef,
4114 public GlobalOpenGLModuleRef
4118 class SelectionAPI : public TypeSystemRef
4120 SelectionSystem* m_selection;
4122 typedef SelectionSystem Type;
4123 STRING_CONSTANT(Name, "*");
4127 SelectionSystem_Construct();
4129 m_selection = &getSelectionSystem();
4133 SelectionSystem_Destroy();
4135 SelectionSystem* getTable()
4141 typedef SingletonModule<SelectionAPI, SelectionDependencies> SelectionModule;
4142 typedef Static<SelectionModule> StaticSelectionModule;
4143 StaticRegisterModule staticRegisterSelection(StaticSelectionModule::instance());