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 "signal/signal.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"
55 TextOutputStream& ostream_write( TextOutputStream& t, const Vector4& v ){
56 return t << "[ " << v.x() << " " << v.y() << " " << v.z() << " " << v.w() << " ]";
59 TextOutputStream& ostream_write( TextOutputStream& t, const Matrix4& m ){
60 return t << "[ " << m.x() << " " << m.y() << " " << m.z() << " " << m.t() << " ]";
66 Matrix4 m_viewpointSpace;
67 Matrix4 m_viewplaneSpace;
68 Vector3 m_axis_screen;
70 void update( const Matrix4& pivot2world, const Matrix4& modelview, const Matrix4& projection, const Matrix4& viewport ){
71 Pivot2World_worldSpace( m_worldSpace, pivot2world, modelview, projection, viewport );
72 Pivot2World_viewpointSpace( m_viewpointSpace, m_axis_screen, pivot2world, modelview, projection, viewport );
73 Pivot2World_viewplaneSpace( m_viewplaneSpace, pivot2world, modelview, projection, viewport );
78 void point_for_device_point( Vector3& point, const Matrix4& device2object, const float x, const float y, const float z ){
79 // transform from normalised device coords to object coords
80 point = vector4_projected( matrix4_transformed_vector4( device2object, Vector4( x, y, z, 1 ) ) );
83 void ray_for_device_point( Ray& ray, const Matrix4& device2object, const float x, const float y ){
84 // point at x, y, zNear
85 point_for_device_point( ray.origin, device2object, x, y, -1 );
87 // point at x, y, zFar
88 point_for_device_point( ray.direction, device2object, x, y, 1 );
91 vector3_subtract( ray.direction, ray.origin );
92 vector3_normalise( ray.direction );
95 bool sphere_intersect_ray( const Vector3& origin, float radius, const Ray& ray, Vector3& intersection ){
96 intersection = vector3_subtracted( origin, ray.origin );
97 const double a = vector3_dot( intersection, ray.direction );
98 const double d = radius * radius - ( vector3_dot( intersection, intersection ) - a * a );
101 intersection = vector3_added( ray.origin, vector3_scaled( ray.direction, a - sqrt( d ) ) );
106 intersection = vector3_added( ray.origin, vector3_scaled( ray.direction, a ) );
111 void ray_intersect_ray( const Ray& ray, const Ray& other, Vector3& intersection ){
112 intersection = vector3_subtracted( ray.origin, other.origin );
113 //float a = 1;//vector3_dot(ray.direction, ray.direction); // always >= 0
114 double dot = vector3_dot( ray.direction, other.direction );
115 //float c = 1;//vector3_dot(other.direction, other.direction); // always >= 0
116 double d = vector3_dot( ray.direction, intersection );
117 double e = vector3_dot( other.direction, intersection );
118 double D = 1 - dot * dot; //a*c - dot*dot; // always >= 0
120 if ( D < 0.000001 ) {
121 // the lines are almost parallel
122 intersection = vector3_added( other.origin, vector3_scaled( other.direction, e ) );
126 intersection = vector3_added( other.origin, vector3_scaled( other.direction, ( e - dot * d ) / D ) );
130 const Vector3 g_origin( 0, 0, 0 );
131 const float g_radius = 64;
133 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 ){
141 ray_for_device_point( ray, device2object, x, y );
142 ray_intersect_ray( ray, Ray( Vector3( 0, 0, 0 ), axis ), point );
145 void point_on_plane( Vector3& point, const Matrix4& device2object, const float x, const float y ){
146 Matrix4 object2device( matrix4_full_inverse( device2object ) );
147 point = vector4_projected( matrix4_transformed_vector4( device2object, Vector4( x, y, object2device[14] / object2device[15], 1 ) ) );
150 //! a and b are unit vectors .. returns angle in radians
151 inline float angle_between( const Vector3& a, const Vector3& b ){
152 return static_cast<float>( 2.0 * atan2(
153 vector3_length( vector3_subtracted( a, b ) ),
154 vector3_length( vector3_added( a, b ) )
159 #if defined( _DEBUG )
163 test_quat( const Vector3& from, const Vector3& to ){
164 Vector4 quaternion( quaternion_for_unit_vectors( from, to ) );
165 Matrix4 matrix( matrix4_rotation_for_quaternion( quaternion_multiplied_by_quaternion( quaternion, c_quaternion_identity ) ) );
170 static test_quat bleh( g_vector3_axis_x, g_vector3_axis_y );
173 //! axis is a unit vector
174 inline void constrain_to_axis( Vector3& vec, const Vector3& axis ){
175 vec = vector3_normalised( vector3_added( vec, vector3_scaled( axis, -vector3_dot( vec, axis ) ) ) );
178 //! a and b are unit vectors .. a and b must be orthogonal to axis .. returns angle in radians
179 float angle_for_axis( const Vector3& a, const Vector3& b, const Vector3& axis ){
180 if ( vector3_dot( axis, vector3_cross( a, b ) ) > 0.0 ) {
181 return angle_between( a, b );
184 return -angle_between( a, b );
188 float distance_for_axis( const Vector3& a, const Vector3& b, const Vector3& axis ){
189 return static_cast<float>( vector3_dot( b, axis ) - vector3_dot( a, axis ) );
195 virtual void Construct( const Matrix4& device2manip, const float x, const float y ) = 0;
196 virtual void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ) = 0;
199 void transform_local2object( Matrix4& object, const Matrix4& local, const Matrix4& local2object ){
200 object = matrix4_multiplied_by_matrix4(
201 matrix4_multiplied_by_matrix4( local2object, local ),
202 matrix4_full_inverse( local2object )
209 virtual void rotate( const Quaternion& rotation ) = 0;
212 class RotateFree : public Manipulatable
215 Rotatable& m_rotatable;
217 RotateFree( Rotatable& rotatable )
218 : m_rotatable( rotatable ){
220 void Construct( const Matrix4& device2manip, const float x, const float y ){
221 point_on_sphere( m_start, device2manip, x, y );
222 vector3_normalise( m_start );
224 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
227 point_on_sphere( current, device2manip, x, y );
228 vector3_normalise( current );
230 m_rotatable.rotate( quaternion_for_unit_vectors( m_start, current ) );
234 class RotateAxis : public Manipulatable
238 Rotatable& m_rotatable;
240 RotateAxis( Rotatable& rotatable )
241 : m_rotatable( rotatable ){
243 void Construct( const Matrix4& device2manip, const float x, const float y ){
244 point_on_sphere( m_start, device2manip, x, y );
245 constrain_to_axis( m_start, m_axis );
247 /// \brief Converts current position to a normalised vector orthogonal to axis.
248 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
250 point_on_sphere( current, device2manip, x, y );
251 constrain_to_axis( current, m_axis );
253 m_rotatable.rotate( quaternion_for_axisangle( m_axis, angle_for_axis( m_start, current, m_axis ) ) );
256 void SetAxis( const Vector3& axis ){
261 void translation_local2object( Vector3& object, const Vector3& local, const Matrix4& local2object ){
262 object = matrix4_get_translation_vec3(
263 matrix4_multiplied_by_matrix4(
264 matrix4_translated_by_vec3( local2object, local ),
265 matrix4_full_inverse( local2object )
273 virtual void translate( const Vector3& translation ) = 0;
276 class TranslateAxis : public Manipulatable
280 Translatable& m_translatable;
282 TranslateAxis( Translatable& translatable )
283 : m_translatable( translatable ){
285 void Construct( const Matrix4& device2manip, const float x, const float y ){
286 point_on_axis( m_start, m_axis, device2manip, x, y );
288 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
290 point_on_axis( current, m_axis, device2manip, x, y );
291 current = vector3_scaled( m_axis, distance_for_axis( m_start, current, m_axis ) );
293 translation_local2object( current, current, manip2object );
294 vector3_snap( current, GetSnapGridSize() );
296 m_translatable.translate( current );
299 void SetAxis( const Vector3& axis ){
304 class TranslateFree : public Manipulatable
308 Translatable& m_translatable;
310 TranslateFree( Translatable& translatable )
311 : m_translatable( translatable ){
313 void Construct( const Matrix4& device2manip, const float x, const float y ){
314 point_on_plane( m_start, device2manip, x, y );
316 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
318 point_on_plane( current, device2manip, x, y );
319 current = vector3_subtracted( current, m_start );
321 translation_local2object( current, current, manip2object );
322 vector3_snap( current, GetSnapGridSize() );
324 m_translatable.translate( current );
328 void GetSelectionAABB( AABB& bounds );
329 const Matrix4& ssGetPivot2World();
334 virtual void scale( const Vector3& scaling ) = 0;
338 class ScaleAxis : public Manipulatable
343 Scalable& m_scalable;
346 Vector3 m_transform_origin;
347 Vector3 m_choosen_extent;
350 ScaleAxis( Scalable& scalable )
351 : m_scalable( scalable ){
353 void Construct( const Matrix4& device2manip, const float x, const float y ){
354 point_on_axis( m_start, m_axis, device2manip, x, y );
356 GetSelectionAABB( m_aabb );
357 m_transform_origin = vector4_to_vector3( ssGetPivot2World().t() );
358 m_choosen_extent = Vector3( std::max( m_aabb.origin[0] + m_aabb.extents[0] - m_transform_origin[0], - m_aabb.origin[0] + m_aabb.extents[0] + m_transform_origin[0] ),
359 std::max( m_aabb.origin[1] + m_aabb.extents[1] - m_transform_origin[1], - m_aabb.origin[1] + m_aabb.extents[1] + m_transform_origin[1] ),
360 std::max( m_aabb.origin[2] + m_aabb.extents[2] - m_transform_origin[2], - m_aabb.origin[2] + m_aabb.extents[2] + m_transform_origin[2] )
364 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
365 //globalOutputStream() << "manip2object: " << manip2object << " device2manip: " << device2manip << " x: " << x << " y:" << y <<"\n";
367 point_on_axis( current, m_axis, device2manip, x, y );
368 Vector3 delta = vector3_subtracted( current, m_start );
370 translation_local2object( delta, delta, manip2object );
371 vector3_snap( delta, GetSnapGridSize() );
373 Vector3 start( vector3_snapped( m_start, GetSnapGridSize() ) );
374 //globalOutputStream() << "start: " << start << " delta: " << delta <<"\n";
376 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
377 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
378 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
381 for( std::size_t i = 0; i < 3; i++ ){
382 if( m_choosen_extent[i] > 0.0625 ){ //epsilon to prevent too high scale for set of models, having really small extent, formed by origins
383 scale[i] = ( m_choosen_extent[i] + delta[i] ) / m_choosen_extent[i];
387 m_scalable.scale( scale );
390 void SetAxis( const Vector3& axis ){
395 class ScaleFree : public Manipulatable
399 Scalable& m_scalable;
402 Vector3 m_transform_origin;
403 Vector3 m_choosen_extent;
406 ScaleFree( Scalable& scalable )
407 : m_scalable( scalable ){
409 void Construct( const Matrix4& device2manip, const float x, const float y ){
410 point_on_plane( m_start, device2manip, x, y );
412 GetSelectionAABB( m_aabb );
413 m_transform_origin = vector4_to_vector3( ssGetPivot2World().t() );
414 m_choosen_extent = Vector3( std::max( m_aabb.origin[0] + m_aabb.extents[0] - m_transform_origin[0], - m_aabb.origin[0] + m_aabb.extents[0] + m_transform_origin[0] ),
415 std::max( m_aabb.origin[1] + m_aabb.extents[1] - m_transform_origin[1], - m_aabb.origin[1] + m_aabb.extents[1] + m_transform_origin[1] ),
416 std::max( m_aabb.origin[2] + m_aabb.extents[2] - m_transform_origin[2], - m_aabb.origin[2] + m_aabb.extents[2] + m_transform_origin[2] )
419 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
421 point_on_plane( current, device2manip, x, y );
422 Vector3 delta = vector3_subtracted( current, m_start );
424 translation_local2object( delta, delta, manip2object );
425 vector3_snap( delta, GetSnapGridSize() );
427 Vector3 start( vector3_snapped( m_start, GetSnapGridSize() ) );
429 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
430 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
431 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
434 for( std::size_t i = 0; i < 3; i++ ){
435 if( m_choosen_extent[i] > 0.0625 ){
436 scale[i] = ( m_choosen_extent[i] + delta[i] ) / m_choosen_extent[i];
440 m_scalable.scale( scale );
453 class RenderableClippedPrimitive : public OpenGLRenderable
457 PointVertex m_points[9];
461 std::vector<primitive_t> m_primitives;
465 void render( RenderStateFlags state ) const {
466 for ( std::size_t i = 0; i < m_primitives.size(); ++i )
468 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_primitives[i].m_points[0].colour );
469 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_primitives[i].m_points[0].vertex );
470 switch ( m_primitives[i].m_count )
473 case 2: glDrawArrays( GL_LINES, 0, GLsizei( m_primitives[i].m_count ) ); break;
474 default: glDrawArrays( GL_POLYGON, 0, GLsizei( m_primitives[i].m_count ) ); break;
479 void construct( const Matrix4& world2device ){
480 m_inverse = matrix4_full_inverse( world2device );
481 m_world = g_matrix4_identity;
484 void insert( const Vector4 clipped[9], std::size_t count ){
487 m_primitives.back().m_count = count;
488 for ( std::size_t i = 0; i < count; ++i )
490 Vector3 world_point( vector4_projected( matrix4_transformed_vector4( m_inverse, clipped[i] ) ) );
491 m_primitives.back().m_points[i].vertex = vertex3f_for_vector3( world_point );
496 m_primitives.clear();
500 m_primitives.push_back( primitive_t() );
502 const Colour4b colour_clipped( 255, 127, 0, 255 );
504 for ( std::size_t i = 0; i < 9; ++i )
505 m_primitives.back().m_points[i].colour = colour_clipped;
509 #if defined( _DEBUG )
510 #define DEBUG_SELECTION
513 #if defined( DEBUG_SELECTION )
514 Shader* g_state_clipped;
515 RenderableClippedPrimitive g_render_clipped;
520 // dist_Point_to_Line(): get the distance of a point to a line.
521 // Input: a Point P and a Line L (in any dimension)
522 // Return: the shortest distance from P to L
524 dist_Point_to_Line( Point P, Line L ){
525 Vector v = L.P1 - L.P0;
528 double c1 = dot( w,v );
529 double c2 = dot( v,v );
532 Point Pb = L.P0 + b * v;
539 typedef Vector3 point_type;
541 Segment3D( const point_type& _p0, const point_type& _p1 )
542 : p0( _p0 ), p1( _p1 ){
548 typedef Vector3 Point3D;
550 inline double vector3_distance_squared( const Point3D& a, const Point3D& b ){
551 return vector3_length_squared( b - a );
554 // get the distance of a point to a segment.
555 Point3D segment_closest_point_to_point( const Segment3D& segment, const Point3D& point ){
556 Vector3 v = segment.p1 - segment.p0;
557 Vector3 w = point - segment.p0;
559 double c1 = vector3_dot( w,v );
564 double c2 = vector3_dot( v,v );
569 return Point3D( segment.p0 + v * ( c1 / c2 ) );
572 double segment_dist_to_point_3d( const Segment3D& segment, const Point3D& point ){
573 return vector3_distance_squared( point, segment_closest_point_to_point( segment, point ) );
576 typedef Vector3 point_t;
577 typedef const Vector3* point_iterator_t;
579 // crossing number test for a point in a polygon
580 // This code is patterned after [Franklin, 2000]
581 bool point_test_polygon_2d( const point_t& P, point_iterator_t start, point_iterator_t finish ){
582 std::size_t crossings = 0;
584 // loop through all edges of the polygon
585 for ( point_iterator_t prev = finish - 1, cur = start; cur != finish; prev = cur, ++cur )
586 { // edge from (*prev) to (*cur)
587 if ( ( ( ( *prev )[1] <= P[1] ) && ( ( *cur )[1] > P[1] ) ) // an upward crossing
588 || ( ( ( *prev )[1] > P[1] ) && ( ( *cur )[1] <= P[1] ) ) ) { // a downward crossing
589 // compute the actual edge-ray intersect x-coordinate
590 float vt = (float)( P[1] - ( *prev )[1] ) / ( ( *cur )[1] - ( *prev )[1] );
591 if ( P[0] < ( *prev )[0] + vt * ( ( *cur )[0] - ( *prev )[0] ) ) { // P[0] < intersect
592 ++crossings; // a valid crossing of y=P[1] right of P[0]
596 return ( crossings & 0x1 ) != 0; // 0 if even (out), and 1 if odd (in)
599 inline double triangle_signed_area_XY( const Vector3& p0, const Vector3& p1, const Vector3& p2 ){
600 return ( ( p1[0] - p0[0] ) * ( p2[1] - p0[1] ) ) - ( ( p2[0] - p0[0] ) * ( p1[1] - p0[1] ) );
611 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 ){
616 Vector3 normalised[9];
619 for ( std::size_t i = 0; i < count; ++i )
621 normalised[i][0] = clipped[i][0] / clipped[i][3];
622 normalised[i][1] = clipped[i][1] / clipped[i][3];
623 normalised[i][2] = clipped[i][2] / clipped[i][3];
627 if ( cull != eClipCullNone && count > 2 ) {
628 double signed_area = triangle_signed_area_XY( normalised[0], normalised[1], normalised[2] );
630 if ( ( cull == eClipCullCW && signed_area > 0 )
631 || ( cull == eClipCullCCW && signed_area < 0 ) ) {
637 Segment3D segment( normalised[0], normalised[1] );
638 Point3D point = segment_closest_point_to_point( segment, Vector3( 0, 0, 0 ) );
639 assign_if_closer( best, SelectionIntersection( point.z(), 0 ) );
641 else if ( count > 2 && !point_test_polygon_2d( Vector3( 0, 0, 0 ), normalised, normalised + count ) ) {
642 point_iterator_t end = normalised + count;
643 for ( point_iterator_t previous = end - 1, current = normalised; current != end; previous = current, ++current )
645 Segment3D segment( *previous, *current );
646 Point3D point = segment_closest_point_to_point( segment, Vector3( 0, 0, 0 ) );
647 float depth = point.z();
649 float distance = static_cast<float>( vector3_length_squared( point ) );
651 assign_if_closer( best, SelectionIntersection( depth, distance ) );
654 else if ( count > 2 ) {
657 SelectionIntersection(
658 static_cast<float>( ray_distance_to_plane(
659 Ray( Vector3( 0, 0, 0 ), Vector3( 0, 0, 1 ) ),
660 plane3_for_points( normalised[0], normalised[1], normalised[2] )
667 #if defined( DEBUG_SELECTION )
669 g_render_clipped.insert( clipped, count );
674 void LineStrip_BestPoint( const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
676 for ( std::size_t i = 0; ( i + 1 ) < size; ++i )
678 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[i + 1].vertex ), clipped );
679 BestPoint( count, clipped, best, eClipCullNone );
683 void LineLoop_BestPoint( const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
685 for ( std::size_t i = 0; i < size; ++i )
687 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[( i + 1 ) % size].vertex ), clipped );
688 BestPoint( count, clipped, best, eClipCullNone );
692 void Line_BestPoint( const Matrix4& local2view, const PointVertex vertices[2], SelectionIntersection& best ){
694 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[0].vertex ), vertex3f_to_vector3( vertices[1].vertex ), clipped );
695 BestPoint( count, clipped, best, eClipCullNone );
698 void Circle_BestPoint( const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
700 for ( std::size_t i = 0; i < size; ++i )
702 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 );
703 BestPoint( count, clipped, best, cull );
707 void Quad_BestPoint( const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, SelectionIntersection& best ){
710 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 );
711 BestPoint( count, clipped, best, cull );
714 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 );
715 BestPoint( count, clipped, best, cull );
719 struct FlatShadedVertex
730 typedef FlatShadedVertex* FlatShadedVertexIterator;
731 void Triangles_BestPoint( const Matrix4& local2view, clipcull_t cull, FlatShadedVertexIterator first, FlatShadedVertexIterator last, SelectionIntersection& best ){
732 for ( FlatShadedVertexIterator x( first ), y( first + 1 ), z( first + 2 ); x != last; x += 3, y += 3, z += 3 )
736 matrix4_clip_triangle(
738 reinterpret_cast<const Vector3&>( ( *x ).vertex ),
739 reinterpret_cast<const Vector3&>( ( *y ).vertex ),
740 reinterpret_cast<const Vector3&>( ( *z ).vertex ),
751 typedef std::multimap<SelectionIntersection, Selectable*> SelectableSortedSet;
753 class SelectionPool : public Selector
755 SelectableSortedSet m_pool;
756 SelectionIntersection m_intersection;
757 Selectable* m_selectable;
760 void pushSelectable( Selectable& selectable ){
761 m_intersection = SelectionIntersection();
762 m_selectable = &selectable;
764 void popSelectable(){
765 addSelectable( m_intersection, m_selectable );
766 m_intersection = SelectionIntersection();
768 void addIntersection( const SelectionIntersection& intersection ){
769 assign_if_closer( m_intersection, intersection );
771 void addSelectable( const SelectionIntersection& intersection, Selectable* selectable ){
772 if ( intersection.valid() ) {
773 m_pool.insert( SelectableSortedSet::value_type( intersection, selectable ) );
777 typedef SelectableSortedSet::iterator iterator;
780 return m_pool.begin();
787 return m_pool.empty();
792 const Colour4b g_colour_sphere( 0, 0, 0, 255 );
793 const Colour4b g_colour_screen( 0, 255, 255, 255 );
794 const Colour4b g_colour_selected( 255, 255, 0, 255 );
796 inline const Colour4b& colourSelected( const Colour4b& colour, bool selected ){
797 return ( selected ) ? g_colour_selected : colour;
800 template<typename remap_policy>
801 inline void draw_semicircle( const std::size_t segments, const float radius, PointVertex* vertices, remap_policy remap ){
802 const double increment = c_pi / double(segments << 2);
804 std::size_t count = 0;
807 remap_policy::set( vertices[segments << 2].vertex, -radius, 0, 0 );
808 while ( count < segments )
810 PointVertex* i = vertices + count;
811 PointVertex* j = vertices + ( ( segments << 1 ) - ( count + 1 ) );
813 PointVertex* k = i + ( segments << 1 );
814 PointVertex* l = j + ( segments << 1 );
817 PointVertex* m = i + ( segments << 2 );
818 PointVertex* n = j + ( segments << 2 );
819 PointVertex* o = k + ( segments << 2 );
820 PointVertex* p = l + ( segments << 2 );
823 remap_policy::set( i->vertex, x,-y, 0 );
824 remap_policy::set( k->vertex,-y,-x, 0 );
826 remap_policy::set( m->vertex,-x, y, 0 );
827 remap_policy::set( o->vertex, y, x, 0 );
833 const double theta = increment * count;
834 x = static_cast<float>( radius * cos( theta ) );
835 y = static_cast<float>( radius * sin( theta ) );
838 remap_policy::set( j->vertex, y,-x, 0 );
839 remap_policy::set( l->vertex,-x,-y, 0 );
841 remap_policy::set( n->vertex,-y, x, 0 );
842 remap_policy::set( p->vertex, x, y, 0 );
850 virtual Manipulatable* GetManipulatable() = 0;
851 virtual void testSelect( const View& view, const Matrix4& pivot2world ){
853 virtual void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
855 virtual void setSelected( bool select ) = 0;
856 virtual bool isSelected() const = 0;
860 inline Vector3 normalised_safe( const Vector3& self ){
861 if ( vector3_equal( self, g_vector3_identity ) ) {
862 return g_vector3_identity;
864 return vector3_normalised( self );
868 class RotateManipulator : public Manipulator
870 struct RenderableCircle : public OpenGLRenderable
872 Array<PointVertex> m_vertices;
874 RenderableCircle( std::size_t size ) : m_vertices( size ){
876 void render( RenderStateFlags state ) const {
877 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
878 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
879 glDrawArrays( GL_LINE_LOOP, 0, GLsizei( m_vertices.size() ) );
881 void setColour( const Colour4b& colour ){
882 for ( Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
884 ( *i ).colour = colour;
889 struct RenderableSemiCircle : public OpenGLRenderable
891 Array<PointVertex> m_vertices;
893 RenderableSemiCircle( std::size_t size ) : m_vertices( size ){
895 void render( RenderStateFlags state ) const {
896 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
897 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
898 glDrawArrays( GL_LINE_STRIP, 0, GLsizei( m_vertices.size() ) );
900 void setColour( const Colour4b& colour ){
901 for ( Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
903 ( *i ).colour = colour;
910 Vector3 m_axis_screen;
911 RenderableSemiCircle m_circle_x;
912 RenderableSemiCircle m_circle_y;
913 RenderableSemiCircle m_circle_z;
914 RenderableCircle m_circle_screen;
915 RenderableCircle m_circle_sphere;
916 SelectableBool m_selectable_x;
917 SelectableBool m_selectable_y;
918 SelectableBool m_selectable_z;
919 SelectableBool m_selectable_screen;
920 SelectableBool m_selectable_sphere;
922 Matrix4 m_local2world_x;
923 Matrix4 m_local2world_y;
924 Matrix4 m_local2world_z;
925 bool m_circle_x_visible;
926 bool m_circle_y_visible;
927 bool m_circle_z_visible;
929 static Shader* m_state_outer;
931 RotateManipulator( Rotatable& rotatable, std::size_t segments, float radius ) :
934 m_circle_x( ( segments << 2 ) + 1 ),
935 m_circle_y( ( segments << 2 ) + 1 ),
936 m_circle_z( ( segments << 2 ) + 1 ),
937 m_circle_screen( segments << 3 ),
938 m_circle_sphere( segments << 3 ){
939 draw_semicircle( segments, radius, m_circle_x.m_vertices.data(), RemapYZX() );
940 draw_semicircle( segments, radius, m_circle_y.m_vertices.data(), RemapZXY() );
941 draw_semicircle( segments, radius, m_circle_z.m_vertices.data(), RemapXYZ() );
943 draw_circle( segments, radius * 1.15f, m_circle_screen.m_vertices.data(), RemapXYZ() );
944 draw_circle( segments, radius, m_circle_sphere.m_vertices.data(), RemapXYZ() );
948 void UpdateColours(){
949 m_circle_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
950 m_circle_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
951 m_circle_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
952 m_circle_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
953 m_circle_sphere.setColour( colourSelected( g_colour_sphere, false ) );
956 void updateCircleTransforms(){
957 Vector3 localViewpoint( matrix4_transformed_direction( matrix4_transposed( m_pivot.m_worldSpace ), vector4_to_vector3( m_pivot.m_viewpointSpace.z() ) ) );
959 m_circle_x_visible = !vector3_equal_epsilon( g_vector3_axis_x, localViewpoint, 1e-6f );
960 if ( m_circle_x_visible ) {
961 m_local2world_x = g_matrix4_identity;
962 vector4_to_vector3( m_local2world_x.y() ) = normalised_safe(
963 vector3_cross( g_vector3_axis_x, localViewpoint )
965 vector4_to_vector3( m_local2world_x.z() ) = normalised_safe(
966 vector3_cross( vector4_to_vector3( m_local2world_x.x() ), vector4_to_vector3( m_local2world_x.y() ) )
968 matrix4_premultiply_by_matrix4( m_local2world_x, m_pivot.m_worldSpace );
971 m_circle_y_visible = !vector3_equal_epsilon( g_vector3_axis_y, localViewpoint, 1e-6f );
972 if ( m_circle_y_visible ) {
973 m_local2world_y = g_matrix4_identity;
974 vector4_to_vector3( m_local2world_y.z() ) = normalised_safe(
975 vector3_cross( g_vector3_axis_y, localViewpoint )
977 vector4_to_vector3( m_local2world_y.x() ) = normalised_safe(
978 vector3_cross( vector4_to_vector3( m_local2world_y.y() ), vector4_to_vector3( m_local2world_y.z() ) )
980 matrix4_premultiply_by_matrix4( m_local2world_y, m_pivot.m_worldSpace );
983 m_circle_z_visible = !vector3_equal_epsilon( g_vector3_axis_z, localViewpoint, 1e-6f );
984 if ( m_circle_z_visible ) {
985 m_local2world_z = g_matrix4_identity;
986 vector4_to_vector3( m_local2world_z.x() ) = normalised_safe(
987 vector3_cross( g_vector3_axis_z, localViewpoint )
989 vector4_to_vector3( m_local2world_z.y() ) = normalised_safe(
990 vector3_cross( vector4_to_vector3( m_local2world_z.z() ), vector4_to_vector3( m_local2world_z.x() ) )
992 matrix4_premultiply_by_matrix4( m_local2world_z, m_pivot.m_worldSpace );
996 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
997 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
998 updateCircleTransforms();
1003 renderer.SetState( m_state_outer, Renderer::eWireframeOnly );
1004 renderer.SetState( m_state_outer, Renderer::eFullMaterials );
1006 renderer.addRenderable( m_circle_screen, m_pivot.m_viewpointSpace );
1007 renderer.addRenderable( m_circle_sphere, m_pivot.m_viewpointSpace );
1009 if ( m_circle_x_visible ) {
1010 renderer.addRenderable( m_circle_x, m_local2world_x );
1012 if ( m_circle_y_visible ) {
1013 renderer.addRenderable( m_circle_y, m_local2world_y );
1015 if ( m_circle_z_visible ) {
1016 renderer.addRenderable( m_circle_z, m_local2world_z );
1019 void testSelect( const View& view, const Matrix4& pivot2world ){
1020 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1021 updateCircleTransforms();
1023 SelectionPool selector;
1027 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_x ) );
1029 #if defined( DEBUG_SELECTION )
1030 g_render_clipped.construct( view.GetViewMatrix() );
1033 SelectionIntersection best;
1034 LineStrip_BestPoint( local2view, m_circle_x.m_vertices.data(), m_circle_x.m_vertices.size(), best );
1035 selector.addSelectable( best, &m_selectable_x );
1039 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_y ) );
1041 #if defined( DEBUG_SELECTION )
1042 g_render_clipped.construct( view.GetViewMatrix() );
1045 SelectionIntersection best;
1046 LineStrip_BestPoint( local2view, m_circle_y.m_vertices.data(), m_circle_y.m_vertices.size(), best );
1047 selector.addSelectable( best, &m_selectable_y );
1051 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_z ) );
1053 #if defined( DEBUG_SELECTION )
1054 g_render_clipped.construct( view.GetViewMatrix() );
1057 SelectionIntersection best;
1058 LineStrip_BestPoint( local2view, m_circle_z.m_vertices.data(), m_circle_z.m_vertices.size(), best );
1059 selector.addSelectable( best, &m_selectable_z );
1064 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1067 SelectionIntersection best;
1068 LineLoop_BestPoint( local2view, m_circle_screen.m_vertices.data(), m_circle_screen.m_vertices.size(), best );
1069 selector.addSelectable( best, &m_selectable_screen );
1073 SelectionIntersection best;
1074 Circle_BestPoint( local2view, eClipCullCW, m_circle_sphere.m_vertices.data(), m_circle_sphere.m_vertices.size(), best );
1075 selector.addSelectable( best, &m_selectable_sphere );
1079 m_axis_screen = m_pivot.m_axis_screen;
1081 if ( !selector.failed() ) {
1082 ( *selector.begin() ).second->setSelected( true );
1086 Manipulatable* GetManipulatable(){
1087 if ( m_selectable_x.isSelected() ) {
1088 m_axis.SetAxis( g_vector3_axis_x );
1091 else if ( m_selectable_y.isSelected() ) {
1092 m_axis.SetAxis( g_vector3_axis_y );
1095 else if ( m_selectable_z.isSelected() ) {
1096 m_axis.SetAxis( g_vector3_axis_z );
1099 else if ( m_selectable_screen.isSelected() ) {
1100 m_axis.SetAxis( m_axis_screen );
1108 void setSelected( bool select ){
1109 m_selectable_x.setSelected( select );
1110 m_selectable_y.setSelected( select );
1111 m_selectable_z.setSelected( select );
1112 m_selectable_screen.setSelected( select );
1114 bool isSelected() const {
1115 return m_selectable_x.isSelected()
1116 | m_selectable_y.isSelected()
1117 | m_selectable_z.isSelected()
1118 | m_selectable_screen.isSelected()
1119 | m_selectable_sphere.isSelected();
1123 Shader* RotateManipulator::m_state_outer;
1126 const float arrowhead_length = 16;
1127 const float arrowhead_radius = 4;
1129 inline void draw_arrowline( const float length, PointVertex* line, const std::size_t axis ){
1130 ( *line++ ).vertex = vertex3f_identity;
1131 ( *line ).vertex = vertex3f_identity;
1132 vertex3f_to_array( ( *line ).vertex )[axis] = length - arrowhead_length;
1135 template<typename VertexRemap, typename NormalRemap>
1136 inline void draw_arrowhead( const std::size_t segments, const float length, FlatShadedVertex* vertices, VertexRemap, NormalRemap ){
1137 std::size_t head_tris = ( segments << 3 );
1138 const double head_segment = c_2pi / head_tris;
1139 for ( std::size_t i = 0; i < head_tris; ++i )
1142 FlatShadedVertex& point = vertices[i * 6 + 0];
1143 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1144 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( i * head_segment ) );
1145 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( i * head_segment ) );
1146 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1147 NormalRemap::y( point.normal ) = static_cast<float>( cos( i * head_segment ) );
1148 NormalRemap::z( point.normal ) = static_cast<float>( sin( i * head_segment ) );
1151 FlatShadedVertex& point = vertices[i * 6 + 1];
1152 VertexRemap::x( point.vertex ) = length;
1153 VertexRemap::y( point.vertex ) = 0;
1154 VertexRemap::z( point.vertex ) = 0;
1155 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1156 NormalRemap::y( point.normal ) = static_cast<float>( cos( ( i + 0.5 ) * head_segment ) );
1157 NormalRemap::z( point.normal ) = static_cast<float>( sin( ( i + 0.5 ) * head_segment ) );
1160 FlatShadedVertex& point = vertices[i * 6 + 2];
1161 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1162 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1163 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1164 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1165 NormalRemap::y( point.normal ) = static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1166 NormalRemap::z( point.normal ) = static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1170 FlatShadedVertex& point = vertices[i * 6 + 3];
1171 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1172 VertexRemap::y( point.vertex ) = 0;
1173 VertexRemap::z( point.vertex ) = 0;
1174 NormalRemap::x( point.normal ) = -1;
1175 NormalRemap::y( point.normal ) = 0;
1176 NormalRemap::z( point.normal ) = 0;
1179 FlatShadedVertex& point = vertices[i * 6 + 4];
1180 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1181 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( i * head_segment ) );
1182 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( i * head_segment ) );
1183 NormalRemap::x( point.normal ) = -1;
1184 NormalRemap::y( point.normal ) = 0;
1185 NormalRemap::z( point.normal ) = 0;
1188 FlatShadedVertex& point = vertices[i * 6 + 5];
1189 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1190 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1191 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1192 NormalRemap::x( point.normal ) = -1;
1193 NormalRemap::y( point.normal ) = 0;
1194 NormalRemap::z( point.normal ) = 0;
1199 template<typename Triple>
1200 class TripleRemapXYZ
1203 static float& x( Triple& triple ){
1206 static float& y( Triple& triple ){
1209 static float& z( Triple& triple ){
1214 template<typename Triple>
1215 class TripleRemapYZX
1218 static float& x( Triple& triple ){
1221 static float& y( Triple& triple ){
1224 static float& z( Triple& triple ){
1229 template<typename Triple>
1230 class TripleRemapZXY
1233 static float& x( Triple& triple ){
1236 static float& y( Triple& triple ){
1239 static float& z( Triple& triple ){
1244 void vector3_print( const Vector3& v ){
1245 globalOutputStream() << "( " << v.x() << " " << v.y() << " " << v.z() << " )";
1248 class TranslateManipulator : public Manipulator
1250 struct RenderableArrowLine : public OpenGLRenderable
1252 PointVertex m_line[2];
1254 RenderableArrowLine(){
1256 void render( RenderStateFlags state ) const {
1257 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_line[0].colour );
1258 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_line[0].vertex );
1259 glDrawArrays( GL_LINES, 0, 2 );
1261 void setColour( const Colour4b& colour ){
1262 m_line[0].colour = colour;
1263 m_line[1].colour = colour;
1266 struct RenderableArrowHead : public OpenGLRenderable
1268 Array<FlatShadedVertex> m_vertices;
1270 RenderableArrowHead( std::size_t size )
1271 : m_vertices( size ){
1273 void render( RenderStateFlags state ) const {
1274 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( FlatShadedVertex ), &m_vertices.data()->colour );
1275 glVertexPointer( 3, GL_FLOAT, sizeof( FlatShadedVertex ), &m_vertices.data()->vertex );
1276 glNormalPointer( GL_FLOAT, sizeof( FlatShadedVertex ), &m_vertices.data()->normal );
1277 glDrawArrays( GL_TRIANGLES, 0, GLsizei( m_vertices.size() ) );
1279 void setColour( const Colour4b& colour ){
1280 for ( Array<FlatShadedVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
1282 ( *i ).colour = colour;
1286 struct RenderableQuad : public OpenGLRenderable
1288 PointVertex m_quad[4];
1289 void render( RenderStateFlags state ) const {
1290 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_quad[0].colour );
1291 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_quad[0].vertex );
1292 glDrawArrays( GL_LINE_LOOP, 0, 4 );
1294 void setColour( const Colour4b& colour ){
1295 m_quad[0].colour = colour;
1296 m_quad[1].colour = colour;
1297 m_quad[2].colour = colour;
1298 m_quad[3].colour = colour;
1302 TranslateFree m_free;
1303 TranslateAxis m_axis;
1304 RenderableArrowLine m_arrow_x;
1305 RenderableArrowLine m_arrow_y;
1306 RenderableArrowLine m_arrow_z;
1307 RenderableArrowHead m_arrow_head_x;
1308 RenderableArrowHead m_arrow_head_y;
1309 RenderableArrowHead m_arrow_head_z;
1310 RenderableQuad m_quad_screen;
1311 SelectableBool m_selectable_x;
1312 SelectableBool m_selectable_y;
1313 SelectableBool m_selectable_z;
1314 SelectableBool m_selectable_screen;
1315 Pivot2World m_pivot;
1317 static Shader* m_state_wire;
1318 static Shader* m_state_fill;
1320 TranslateManipulator( Translatable& translatable, std::size_t segments, float length ) :
1321 m_free( translatable ),
1322 m_axis( translatable ),
1323 m_arrow_head_x( 3 * 2 * ( segments << 3 ) ),
1324 m_arrow_head_y( 3 * 2 * ( segments << 3 ) ),
1325 m_arrow_head_z( 3 * 2 * ( segments << 3 ) ){
1326 draw_arrowline( length, m_arrow_x.m_line, 0 );
1327 draw_arrowhead( segments, length, m_arrow_head_x.m_vertices.data(), TripleRemapXYZ<Vertex3f>(), TripleRemapXYZ<Normal3f>() );
1328 draw_arrowline( length, m_arrow_y.m_line, 1 );
1329 draw_arrowhead( segments, length, m_arrow_head_y.m_vertices.data(), TripleRemapYZX<Vertex3f>(), TripleRemapYZX<Normal3f>() );
1330 draw_arrowline( length, m_arrow_z.m_line, 2 );
1331 draw_arrowhead( segments, length, m_arrow_head_z.m_vertices.data(), TripleRemapZXY<Vertex3f>(), TripleRemapZXY<Normal3f>() );
1333 draw_quad( 16, m_quad_screen.m_quad );
1336 void UpdateColours(){
1337 m_arrow_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1338 m_arrow_head_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1339 m_arrow_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1340 m_arrow_head_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1341 m_arrow_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1342 m_arrow_head_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1343 m_quad_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1346 bool manipulator_show_axis( const Pivot2World& pivot, const Vector3& axis ){
1347 return fabs( vector3_dot( pivot.m_axis_screen, axis ) ) < 0.95;
1350 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1351 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1356 Vector3 x = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.x() ) );
1357 bool show_x = manipulator_show_axis( m_pivot, x );
1359 Vector3 y = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.y() ) );
1360 bool show_y = manipulator_show_axis( m_pivot, y );
1362 Vector3 z = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.z() ) );
1363 bool show_z = manipulator_show_axis( m_pivot, z );
1365 renderer.SetState( m_state_wire, Renderer::eWireframeOnly );
1366 renderer.SetState( m_state_wire, Renderer::eFullMaterials );
1369 renderer.addRenderable( m_arrow_x, m_pivot.m_worldSpace );
1372 renderer.addRenderable( m_arrow_y, m_pivot.m_worldSpace );
1375 renderer.addRenderable( m_arrow_z, m_pivot.m_worldSpace );
1378 renderer.addRenderable( m_quad_screen, m_pivot.m_viewplaneSpace );
1380 renderer.SetState( m_state_fill, Renderer::eWireframeOnly );
1381 renderer.SetState( m_state_fill, Renderer::eFullMaterials );
1384 renderer.addRenderable( m_arrow_head_x, m_pivot.m_worldSpace );
1387 renderer.addRenderable( m_arrow_head_y, m_pivot.m_worldSpace );
1390 renderer.addRenderable( m_arrow_head_z, m_pivot.m_worldSpace );
1393 void testSelect( const View& view, const Matrix4& pivot2world ){
1394 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1396 SelectionPool selector;
1398 Vector3 x = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.x() ) );
1399 bool show_x = manipulator_show_axis( m_pivot, x );
1401 Vector3 y = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.y() ) );
1402 bool show_y = manipulator_show_axis( m_pivot, y );
1404 Vector3 z = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.z() ) );
1405 bool show_z = manipulator_show_axis( m_pivot, z );
1408 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1411 SelectionIntersection best;
1412 Quad_BestPoint( local2view, eClipCullCW, m_quad_screen.m_quad, best );
1413 if ( best.valid() ) {
1414 best = SelectionIntersection( 0, 0 );
1415 selector.addSelectable( best, &m_selectable_screen );
1421 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_worldSpace ) );
1423 #if defined( DEBUG_SELECTION )
1424 g_render_clipped.construct( view.GetViewMatrix() );
1428 SelectionIntersection best;
1429 Line_BestPoint( local2view, m_arrow_x.m_line, best );
1430 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_x.m_vertices.begin(), m_arrow_head_x.m_vertices.end(), best );
1431 selector.addSelectable( best, &m_selectable_x );
1435 SelectionIntersection best;
1436 Line_BestPoint( local2view, m_arrow_y.m_line, best );
1437 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_y.m_vertices.begin(), m_arrow_head_y.m_vertices.end(), best );
1438 selector.addSelectable( best, &m_selectable_y );
1442 SelectionIntersection best;
1443 Line_BestPoint( local2view, m_arrow_z.m_line, best );
1444 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_z.m_vertices.begin(), m_arrow_head_z.m_vertices.end(), best );
1445 selector.addSelectable( best, &m_selectable_z );
1449 if ( !selector.failed() ) {
1450 ( *selector.begin() ).second->setSelected( true );
1454 Manipulatable* GetManipulatable(){
1455 if ( m_selectable_x.isSelected() ) {
1456 m_axis.SetAxis( g_vector3_axis_x );
1459 else if ( m_selectable_y.isSelected() ) {
1460 m_axis.SetAxis( g_vector3_axis_y );
1463 else if ( m_selectable_z.isSelected() ) {
1464 m_axis.SetAxis( g_vector3_axis_z );
1473 void setSelected( bool select ){
1474 m_selectable_x.setSelected( select );
1475 m_selectable_y.setSelected( select );
1476 m_selectable_z.setSelected( select );
1477 m_selectable_screen.setSelected( select );
1479 bool isSelected() const {
1480 return m_selectable_x.isSelected()
1481 | m_selectable_y.isSelected()
1482 | m_selectable_z.isSelected()
1483 | m_selectable_screen.isSelected();
1487 Shader* TranslateManipulator::m_state_wire;
1488 Shader* TranslateManipulator::m_state_fill;
1490 class ScaleManipulator : public Manipulator
1492 struct RenderableArrow : public OpenGLRenderable
1494 PointVertex m_line[2];
1496 void render( RenderStateFlags state ) const {
1497 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_line[0].colour );
1498 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_line[0].vertex );
1499 glDrawArrays( GL_LINES, 0, 2 );
1501 void setColour( const Colour4b& colour ){
1502 m_line[0].colour = colour;
1503 m_line[1].colour = colour;
1506 struct RenderableQuad : public OpenGLRenderable
1508 PointVertex m_quad[4];
1509 void render( RenderStateFlags state ) const {
1510 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_quad[0].colour );
1511 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_quad[0].vertex );
1512 glDrawArrays( GL_QUADS, 0, 4 );
1514 void setColour( const Colour4b& colour ){
1515 m_quad[0].colour = colour;
1516 m_quad[1].colour = colour;
1517 m_quad[2].colour = colour;
1518 m_quad[3].colour = colour;
1524 RenderableArrow m_arrow_x;
1525 RenderableArrow m_arrow_y;
1526 RenderableArrow m_arrow_z;
1527 RenderableQuad m_quad_screen;
1528 SelectableBool m_selectable_x;
1529 SelectableBool m_selectable_y;
1530 SelectableBool m_selectable_z;
1531 SelectableBool m_selectable_screen;
1532 Pivot2World m_pivot;
1534 ScaleManipulator( Scalable& scalable, std::size_t segments, float length ) :
1537 draw_arrowline( length, m_arrow_x.m_line, 0 );
1538 draw_arrowline( length, m_arrow_y.m_line, 1 );
1539 draw_arrowline( length, m_arrow_z.m_line, 2 );
1541 draw_quad( 16, m_quad_screen.m_quad );
1544 Pivot2World& getPivot(){
1548 void UpdateColours(){
1549 m_arrow_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1550 m_arrow_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1551 m_arrow_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1552 m_quad_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1555 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1556 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1561 renderer.addRenderable( m_arrow_x, m_pivot.m_worldSpace );
1562 renderer.addRenderable( m_arrow_y, m_pivot.m_worldSpace );
1563 renderer.addRenderable( m_arrow_z, m_pivot.m_worldSpace );
1565 renderer.addRenderable( m_quad_screen, m_pivot.m_viewpointSpace );
1567 void testSelect( const View& view, const Matrix4& pivot2world ){
1568 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1570 SelectionPool selector;
1573 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_worldSpace ) );
1575 #if defined( DEBUG_SELECTION )
1576 g_render_clipped.construct( view.GetViewMatrix() );
1580 SelectionIntersection best;
1581 Line_BestPoint( local2view, m_arrow_x.m_line, best );
1582 selector.addSelectable( best, &m_selectable_x );
1586 SelectionIntersection best;
1587 Line_BestPoint( local2view, m_arrow_y.m_line, best );
1588 selector.addSelectable( best, &m_selectable_y );
1592 SelectionIntersection best;
1593 Line_BestPoint( local2view, m_arrow_z.m_line, best );
1594 selector.addSelectable( best, &m_selectable_z );
1599 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1602 SelectionIntersection best;
1603 Quad_BestPoint( local2view, eClipCullCW, m_quad_screen.m_quad, best );
1604 selector.addSelectable( best, &m_selectable_screen );
1608 if ( !selector.failed() ) {
1609 ( *selector.begin() ).second->setSelected( true );
1613 Manipulatable* GetManipulatable(){
1614 if ( m_selectable_x.isSelected() ) {
1615 m_axis.SetAxis( g_vector3_axis_x );
1618 else if ( m_selectable_y.isSelected() ) {
1619 m_axis.SetAxis( g_vector3_axis_y );
1622 else if ( m_selectable_z.isSelected() ) {
1623 m_axis.SetAxis( g_vector3_axis_z );
1631 void setSelected( bool select ){
1632 m_selectable_x.setSelected( select );
1633 m_selectable_y.setSelected( select );
1634 m_selectable_z.setSelected( select );
1635 m_selectable_screen.setSelected( select );
1637 bool isSelected() const {
1638 return m_selectable_x.isSelected()
1639 | m_selectable_y.isSelected()
1640 | m_selectable_z.isSelected()
1641 | m_selectable_screen.isSelected();
1646 inline PlaneSelectable* Instance_getPlaneSelectable( scene::Instance& instance ){
1647 return InstanceTypeCast<PlaneSelectable>::cast( instance );
1650 class PlaneSelectableSelectPlanes : public scene::Graph::Walker
1652 Selector& m_selector;
1653 SelectionTest& m_test;
1654 PlaneCallback m_selectedPlaneCallback;
1656 PlaneSelectableSelectPlanes( Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback )
1657 : m_selector( selector ), m_test( test ), m_selectedPlaneCallback( selectedPlaneCallback ){
1659 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1660 if ( path.top().get().visible() ) {
1661 Selectable* selectable = Instance_getSelectable( instance );
1662 if ( selectable != 0 && selectable->isSelected() ) {
1663 PlaneSelectable* planeSelectable = Instance_getPlaneSelectable( instance );
1664 if ( planeSelectable != 0 ) {
1665 planeSelectable->selectPlanes( m_selector, m_test, m_selectedPlaneCallback );
1673 class PlaneSelectableSelectReversedPlanes : public scene::Graph::Walker
1675 Selector& m_selector;
1676 const SelectedPlanes& m_selectedPlanes;
1678 PlaneSelectableSelectReversedPlanes( Selector& selector, const SelectedPlanes& selectedPlanes )
1679 : m_selector( selector ), m_selectedPlanes( selectedPlanes ){
1681 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1682 if ( path.top().get().visible() ) {
1683 Selectable* selectable = Instance_getSelectable( instance );
1684 if ( selectable != 0 && selectable->isSelected() ) {
1685 PlaneSelectable* planeSelectable = Instance_getPlaneSelectable( instance );
1686 if ( planeSelectable != 0 ) {
1687 planeSelectable->selectReversedPlanes( m_selector, m_selectedPlanes );
1695 void Scene_forEachPlaneSelectable_selectPlanes( scene::Graph& graph, Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback ){
1696 graph.traverse( PlaneSelectableSelectPlanes( selector, test, selectedPlaneCallback ) );
1699 void Scene_forEachPlaneSelectable_selectReversedPlanes( scene::Graph& graph, Selector& selector, const SelectedPlanes& selectedPlanes ){
1700 graph.traverse( PlaneSelectableSelectReversedPlanes( selector, selectedPlanes ) );
1707 bool operator()( const Plane3& plane, const Plane3& other ) const {
1708 if ( plane.a < other.a ) {
1711 if ( other.a < plane.a ) {
1715 if ( plane.b < other.b ) {
1718 if ( other.b < plane.b ) {
1722 if ( plane.c < other.c ) {
1725 if ( other.c < plane.c ) {
1729 if ( plane.d < other.d ) {
1732 if ( other.d < plane.d ) {
1740 typedef std::set<Plane3, PlaneLess> PlaneSet;
1742 inline void PlaneSet_insert( PlaneSet& self, const Plane3& plane ){
1743 self.insert( plane );
1746 inline bool PlaneSet_contains( const PlaneSet& self, const Plane3& plane ){
1747 return self.find( plane ) != self.end();
1751 class SelectedPlaneSet : public SelectedPlanes
1753 PlaneSet m_selectedPlanes;
1755 bool empty() const {
1756 return m_selectedPlanes.empty();
1759 void insert( const Plane3& plane ){
1760 PlaneSet_insert( m_selectedPlanes, plane );
1762 bool contains( const Plane3& plane ) const {
1763 return PlaneSet_contains( m_selectedPlanes, plane );
1765 typedef MemberCaller1<SelectedPlaneSet, const Plane3&, &SelectedPlaneSet::insert> InsertCaller;
1769 bool Scene_forEachPlaneSelectable_selectPlanes( scene::Graph& graph, Selector& selector, SelectionTest& test ){
1770 SelectedPlaneSet selectedPlanes;
1772 Scene_forEachPlaneSelectable_selectPlanes( graph, selector, test, SelectedPlaneSet::InsertCaller( selectedPlanes ) );
1773 Scene_forEachPlaneSelectable_selectReversedPlanes( graph, selector, selectedPlanes );
1775 return !selectedPlanes.empty();
1778 void Scene_Translate_Component_Selected( scene::Graph& graph, const Vector3& translation );
1779 void Scene_Translate_Selected( scene::Graph& graph, const Vector3& translation );
1780 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume );
1781 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode );
1782 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode );
1783 void Scene_SelectAll_Component( bool select, SelectionSystem::EComponentMode componentMode );
1785 class ResizeTranslatable : public Translatable
1787 void translate( const Vector3& translation ){
1788 Scene_Translate_Component_Selected( GlobalSceneGraph(), translation );
1792 class DragTranslatable : public Translatable
1794 void translate( const Vector3& translation ){
1795 if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
1796 Scene_Translate_Component_Selected( GlobalSceneGraph(), translation );
1800 Scene_Translate_Selected( GlobalSceneGraph(), translation );
1805 class SelectionVolume : public SelectionTest
1807 Matrix4 m_local2view;
1813 SelectionVolume( const View& view )
1817 const VolumeTest& getVolume() const {
1821 const Vector3& getNear() const {
1824 const Vector3& getFar() const {
1828 void BeginMesh( const Matrix4& localToWorld, bool twoSided ){
1829 m_local2view = matrix4_multiplied_by_matrix4( m_view.GetViewMatrix(), localToWorld );
1831 // Cull back-facing polygons based on winding being clockwise or counter-clockwise.
1832 // Don't cull if the view is wireframe and the polygons are two-sided.
1833 m_cull = twoSided && !m_view.fill() ? eClipCullNone : ( matrix4_handedness( localToWorld ) == MATRIX4_RIGHTHANDED ) ? eClipCullCW : eClipCullCCW;
1836 Matrix4 screen2world( matrix4_full_inverse( m_local2view ) );
1838 m_near = vector4_projected(
1839 matrix4_transformed_vector4(
1841 Vector4( 0, 0, -1, 1 )
1845 m_far = vector4_projected(
1846 matrix4_transformed_vector4(
1848 Vector4( 0, 0, 1, 1 )
1853 #if defined( DEBUG_SELECTION )
1854 g_render_clipped.construct( m_view.GetViewMatrix() );
1857 void TestPoint( const Vector3& point, SelectionIntersection& best ){
1859 if ( matrix4_clip_point( m_local2view, point, clipped ) == c_CLIP_PASS ) {
1860 best = select_point_from_clipped( clipped );
1863 void TestPolygon( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1865 for ( std::size_t i = 0; i + 2 < count; ++i )
1868 matrix4_clip_triangle(
1870 reinterpret_cast<const Vector3&>( vertices[0] ),
1871 reinterpret_cast<const Vector3&>( vertices[i + 1] ),
1872 reinterpret_cast<const Vector3&>( vertices[i + 2] ),
1881 void TestLineLoop( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1886 for ( VertexPointer::iterator i = vertices.begin(), end = i + count, prev = i + ( count - 1 ); i != end; prev = i, ++i )
1891 reinterpret_cast<const Vector3&>( ( *prev ) ),
1892 reinterpret_cast<const Vector3&>( ( *i ) ),
1901 void TestLineStrip( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1906 for ( VertexPointer::iterator i = vertices.begin(), end = i + count, next = i + 1; next != end; i = next, ++next )
1911 reinterpret_cast<const Vector3&>( ( *i ) ),
1912 reinterpret_cast<const Vector3&>( ( *next ) ),
1921 void TestLines( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1926 for ( VertexPointer::iterator i = vertices.begin(), end = i + count; i != end; i += 2 )
1931 reinterpret_cast<const Vector3&>( ( *i ) ),
1932 reinterpret_cast<const Vector3&>( ( *( i + 1 ) ) ),
1941 void TestTriangles( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
1943 for ( IndexPointer::iterator i( indices.begin() ); i != indices.end(); i += 3 )
1946 matrix4_clip_triangle(
1948 reinterpret_cast<const Vector3&>( vertices[*i] ),
1949 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1950 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
1959 void TestQuads( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
1961 for ( IndexPointer::iterator i( indices.begin() ); i != indices.end(); i += 4 )
1964 matrix4_clip_triangle(
1966 reinterpret_cast<const Vector3&>( vertices[*i] ),
1967 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1968 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
1976 matrix4_clip_triangle(
1978 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1979 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
1980 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
1989 void TestQuadStrip( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
1991 for ( IndexPointer::iterator i( indices.begin() ); i + 2 != indices.end(); i += 2 )
1994 matrix4_clip_triangle(
1996 reinterpret_cast<const Vector3&>( vertices[*i] ),
1997 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1998 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2006 matrix4_clip_triangle(
2008 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2009 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2010 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
2021 class SelectionCounter
2024 typedef const Selectable& first_argument_type;
2026 SelectionCounter( const SelectionChangeCallback& onchanged )
2027 : m_count( 0 ), m_onchanged( onchanged ){
2029 void operator()( const Selectable& selectable ){
2030 if ( selectable.isSelected() ) {
2035 ASSERT_MESSAGE( m_count != 0, "selection counter underflow" );
2039 m_onchanged( selectable );
2041 bool empty() const {
2042 return m_count == 0;
2044 std::size_t size() const {
2048 std::size_t m_count;
2049 SelectionChangeCallback m_onchanged;
2052 inline void ConstructSelectionTest( View& view, const rect_t selection_box ){
2053 view.EnableScissor( selection_box.min[0], selection_box.max[0], selection_box.min[1], selection_box.max[1] );
2056 inline const rect_t SelectionBoxForPoint( const float device_point[2], const float device_epsilon[2] ){
2057 rect_t selection_box;
2058 selection_box.min[0] = device_point[0] - device_epsilon[0];
2059 selection_box.min[1] = device_point[1] - device_epsilon[1];
2060 selection_box.max[0] = device_point[0] + device_epsilon[0];
2061 selection_box.max[1] = device_point[1] + device_epsilon[1];
2062 return selection_box;
2065 inline const rect_t SelectionBoxForArea( const float device_point[2], const float device_delta[2] ){
2066 rect_t selection_box;
2067 selection_box.min[0] = ( device_delta[0] < 0 ) ? ( device_point[0] + device_delta[0] ) : ( device_point[0] );
2068 selection_box.min[1] = ( device_delta[1] < 0 ) ? ( device_point[1] + device_delta[1] ) : ( device_point[1] );
2069 selection_box.max[0] = ( device_delta[0] > 0 ) ? ( device_point[0] + device_delta[0] ) : ( device_point[0] );
2070 selection_box.max[1] = ( device_delta[1] > 0 ) ? ( device_point[1] + device_delta[1] ) : ( device_point[1] );
2071 return selection_box;
2074 Quaternion construct_local_rotation( const Quaternion& world, const Quaternion& localToWorld ){
2075 return quaternion_normalised( quaternion_multiplied_by_quaternion(
2076 quaternion_normalised( quaternion_multiplied_by_quaternion(
2077 quaternion_inverse( localToWorld ),
2084 inline void matrix4_assign_rotation( Matrix4& matrix, const Matrix4& other ){
2085 matrix[0] = other[0];
2086 matrix[1] = other[1];
2087 matrix[2] = other[2];
2088 matrix[4] = other[4];
2089 matrix[5] = other[5];
2090 matrix[6] = other[6];
2091 matrix[8] = other[8];
2092 matrix[9] = other[9];
2093 matrix[10] = other[10];
2096 void matrix4_assign_rotation_for_pivot( Matrix4& matrix, scene::Instance& instance ){
2097 Editable* editable = Node_getEditable( instance.path().top() );
2098 if ( editable != 0 ) {
2099 matrix4_assign_rotation( matrix, matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ) );
2103 matrix4_assign_rotation( matrix, instance.localToWorld() );
2107 inline bool Instance_isSelectedComponents( scene::Instance& instance ){
2108 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
2109 return componentSelectionTestable != 0
2110 && componentSelectionTestable->isSelectedComponents();
2113 class TranslateSelected : public SelectionSystem::Visitor
2115 const Vector3& m_translate;
2117 TranslateSelected( const Vector3& translate )
2118 : m_translate( translate ){
2120 void visit( scene::Instance& instance ) const {
2121 Transformable* transform = Instance_getTransformable( instance );
2122 if ( transform != 0 ) {
2123 transform->setType( TRANSFORM_PRIMITIVE );
2124 transform->setTranslation( m_translate );
2129 void Scene_Translate_Selected( scene::Graph& graph, const Vector3& translation ){
2130 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2131 GlobalSelectionSystem().foreachSelected( TranslateSelected( translation ) );
2135 Vector3 get_local_pivot( const Vector3& world_pivot, const Matrix4& localToWorld ){
2137 matrix4_transformed_point(
2138 matrix4_full_inverse( localToWorld ),
2144 void translation_for_pivoted_matrix_transform( Vector3& parent_translation, const Matrix4& local_transform, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2145 // we need a translation inside the parent system to move the origin of this object to the right place
2147 // mathematically, it must fulfill:
2149 // local_translation local_transform local_pivot = local_pivot
2150 // local_translation = local_pivot - local_transform local_pivot
2153 // local_transform local_translation local_pivot = local_pivot
2154 // local_translation local_pivot = local_transform^-1 local_pivot
2155 // local_translation + local_pivot = local_transform^-1 local_pivot
2156 // local_translation = local_transform^-1 local_pivot - local_pivot
2158 Vector3 local_pivot( get_local_pivot( world_pivot, localToWorld ) );
2160 Vector3 local_translation(
2163 matrix4_transformed_point(
2168 matrix4_transformed_point(
2169 matrix4_full_inverse(local_transform),
2177 translation_local2object( parent_translation, local_translation, localToParent );
2181 globalOutputStream() << "World pivot is at " << world_pivot << "\n";
2182 globalOutputStream() << "Local pivot is at " << local_pivot << "\n";
2183 globalOutputStream() << "Transformation " << local_transform << " moves it to: " << matrix4_transformed_point(local_transform, local_pivot) << "\n";
2184 globalOutputStream() << "Must move by " << local_translation << " in the local system" << "\n";
2185 globalOutputStream() << "Must move by " << parent_translation << " in the parent system" << "\n";
2189 void translation_for_pivoted_rotation( Vector3& parent_translation, const Quaternion& local_rotation, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2190 translation_for_pivoted_matrix_transform( parent_translation, matrix4_rotation_for_quaternion_quantised( local_rotation ), world_pivot, localToWorld, localToParent );
2193 void translation_for_pivoted_scale( Vector3& parent_translation, const Vector3& world_scale, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2194 Matrix4 local_transform(
2195 matrix4_multiplied_by_matrix4(
2196 matrix4_full_inverse( localToWorld ),
2197 matrix4_multiplied_by_matrix4(
2198 matrix4_scale_for_vec3( world_scale ),
2203 local_transform.tx() = local_transform.ty() = local_transform.tz() = 0; // cancel translation parts
2204 translation_for_pivoted_matrix_transform( parent_translation, local_transform, world_pivot, localToWorld, localToParent );
2207 class rotate_selected : public SelectionSystem::Visitor
2209 const Quaternion& m_rotate;
2210 const Vector3& m_world_pivot;
2212 rotate_selected( const Quaternion& rotation, const Vector3& world_pivot )
2213 : m_rotate( rotation ), m_world_pivot( world_pivot ){
2215 void visit( scene::Instance& instance ) const {
2216 TransformNode* transformNode = Node_getTransformNode( instance.path().top() );
2217 if ( transformNode != 0 ) {
2218 Transformable* transform = Instance_getTransformable( instance );
2219 if ( transform != 0 ) {
2220 transform->setType( TRANSFORM_PRIMITIVE );
2221 transform->setScale( c_scale_identity );
2222 transform->setTranslation( c_translation_identity );
2224 transform->setType( TRANSFORM_PRIMITIVE );
2225 transform->setRotation( m_rotate );
2228 Editable* editable = Node_getEditable( instance.path().top() );
2229 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2231 Vector3 parent_translation;
2232 translation_for_pivoted_rotation(
2236 matrix4_multiplied_by_matrix4( instance.localToWorld(), localPivot ),
2237 matrix4_multiplied_by_matrix4( transformNode->localToParent(), localPivot )
2240 transform->setTranslation( parent_translation );
2247 void Scene_Rotate_Selected( scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot ){
2248 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2249 GlobalSelectionSystem().foreachSelected( rotate_selected( rotation, world_pivot ) );
2253 class scale_selected : public SelectionSystem::Visitor
2255 const Vector3& m_scale;
2256 const Vector3& m_world_pivot;
2258 scale_selected( const Vector3& scaling, const Vector3& world_pivot )
2259 : m_scale( scaling ), m_world_pivot( world_pivot ){
2261 void visit( scene::Instance& instance ) const {
2262 TransformNode* transformNode = Node_getTransformNode( instance.path().top() );
2263 if ( transformNode != 0 ) {
2264 Transformable* transform = Instance_getTransformable( instance );
2265 if ( transform != 0 ) {
2266 transform->setType( TRANSFORM_PRIMITIVE );
2267 transform->setScale( c_scale_identity );
2268 transform->setTranslation( c_translation_identity );
2270 transform->setType( TRANSFORM_PRIMITIVE );
2271 transform->setScale( m_scale );
2273 Editable* editable = Node_getEditable( instance.path().top() );
2274 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2276 Vector3 parent_translation;
2277 translation_for_pivoted_scale(
2281 matrix4_multiplied_by_matrix4( instance.localToWorld(), localPivot ),
2282 matrix4_multiplied_by_matrix4( transformNode->localToParent(), localPivot )
2285 transform->setTranslation( parent_translation );
2292 void Scene_Scale_Selected( scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot ){
2293 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2294 GlobalSelectionSystem().foreachSelected( scale_selected( scaling, world_pivot ) );
2299 class translate_component_selected : public SelectionSystem::Visitor
2301 const Vector3& m_translate;
2303 translate_component_selected( const Vector3& translate )
2304 : m_translate( translate ){
2306 void visit( scene::Instance& instance ) const {
2307 Transformable* transform = Instance_getTransformable( instance );
2308 if ( transform != 0 ) {
2309 transform->setType( TRANSFORM_COMPONENT );
2310 transform->setTranslation( m_translate );
2315 void Scene_Translate_Component_Selected( scene::Graph& graph, const Vector3& translation ){
2316 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2317 GlobalSelectionSystem().foreachSelectedComponent( translate_component_selected( translation ) );
2321 class rotate_component_selected : public SelectionSystem::Visitor
2323 const Quaternion& m_rotate;
2324 const Vector3& m_world_pivot;
2326 rotate_component_selected( const Quaternion& rotation, const Vector3& world_pivot )
2327 : m_rotate( rotation ), m_world_pivot( world_pivot ){
2329 void visit( scene::Instance& instance ) const {
2330 Transformable* transform = Instance_getTransformable( instance );
2331 if ( transform != 0 ) {
2332 Vector3 parent_translation;
2333 translation_for_pivoted_rotation( parent_translation, m_rotate, m_world_pivot, instance.localToWorld(), Node_getTransformNode( instance.path().top() )->localToParent() );
2335 transform->setType( TRANSFORM_COMPONENT );
2336 transform->setRotation( m_rotate );
2337 transform->setTranslation( parent_translation );
2342 void Scene_Rotate_Component_Selected( scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot ){
2343 if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
2344 GlobalSelectionSystem().foreachSelectedComponent( rotate_component_selected( rotation, world_pivot ) );
2348 class scale_component_selected : public SelectionSystem::Visitor
2350 const Vector3& m_scale;
2351 const Vector3& m_world_pivot;
2353 scale_component_selected( const Vector3& scaling, const Vector3& world_pivot )
2354 : m_scale( scaling ), m_world_pivot( world_pivot ){
2356 void visit( scene::Instance& instance ) const {
2357 Transformable* transform = Instance_getTransformable( instance );
2358 if ( transform != 0 ) {
2359 Vector3 parent_translation;
2360 translation_for_pivoted_scale( parent_translation, m_scale, m_world_pivot, instance.localToWorld(), Node_getTransformNode( instance.path().top() )->localToParent() );
2362 transform->setType( TRANSFORM_COMPONENT );
2363 transform->setScale( m_scale );
2364 transform->setTranslation( parent_translation );
2369 void Scene_Scale_Component_Selected( scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot ){
2370 if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
2371 GlobalSelectionSystem().foreachSelectedComponent( scale_component_selected( scaling, world_pivot ) );
2376 class BooleanSelector : public Selector
2379 SelectionIntersection m_intersection;
2380 Selectable* m_selectable;
2382 BooleanSelector() : m_selected( false ){
2385 void pushSelectable( Selectable& selectable ){
2386 m_intersection = SelectionIntersection();
2387 m_selectable = &selectable;
2389 void popSelectable(){
2390 if ( m_intersection.valid() ) {
2393 m_intersection = SelectionIntersection();
2395 void addIntersection( const SelectionIntersection& intersection ){
2396 if ( m_selectable->isSelected() ) {
2397 assign_if_closer( m_intersection, intersection );
2406 class BestSelector : public Selector
2408 SelectionIntersection m_intersection;
2409 Selectable* m_selectable;
2410 SelectionIntersection m_bestIntersection;
2411 std::list<Selectable*> m_bestSelectable;
2413 BestSelector() : m_bestIntersection( SelectionIntersection() ), m_bestSelectable( 0 ){
2416 void pushSelectable( Selectable& selectable ){
2417 m_intersection = SelectionIntersection();
2418 m_selectable = &selectable;
2420 void popSelectable(){
2421 if ( m_intersection.equalEpsilon( m_bestIntersection, 0.25f, 0.001f ) ) {
2422 m_bestSelectable.push_back( m_selectable );
2423 m_bestIntersection = m_intersection;
2425 else if ( m_intersection < m_bestIntersection ) {
2426 m_bestSelectable.clear();
2427 m_bestSelectable.push_back( m_selectable );
2428 m_bestIntersection = m_intersection;
2430 m_intersection = SelectionIntersection();
2432 void addIntersection( const SelectionIntersection& intersection ){
2433 assign_if_closer( m_intersection, intersection );
2436 std::list<Selectable*>& best(){
2437 return m_bestSelectable;
2441 class DragManipulator : public Manipulator
2443 TranslateFree m_freeResize;
2444 TranslateFree m_freeDrag;
2445 ResizeTranslatable m_resize;
2446 DragTranslatable m_drag;
2447 SelectableBool m_dragSelectable;
2452 DragManipulator() : m_freeResize( m_resize ), m_freeDrag( m_drag ), m_selected( false ){
2455 Manipulatable* GetManipulatable(){
2456 return m_dragSelectable.isSelected() ? &m_freeDrag : &m_freeResize;
2459 void testSelect( const View& view, const Matrix4& pivot2world ){
2460 SelectionPool selector;
2462 SelectionVolume test( view );
2464 if ( GlobalSelectionSystem().Mode() == SelectionSystem::ePrimitive ) {
2465 BooleanSelector booleanSelector;
2467 Scene_TestSelect_Primitive( booleanSelector, test, view );
2469 if ( booleanSelector.isSelected() ) {
2470 selector.addSelectable( SelectionIntersection( 0, 0 ), &m_dragSelectable );
2475 m_selected = Scene_forEachPlaneSelectable_selectPlanes( GlobalSceneGraph(), selector, test );
2480 BestSelector bestSelector;
2481 Scene_TestSelect_Component_Selected( bestSelector, test, view, GlobalSelectionSystem().ComponentMode() );
2482 for ( std::list<Selectable*>::iterator i = bestSelector.best().begin(); i != bestSelector.best().end(); ++i )
2484 if ( !( *i )->isSelected() ) {
2485 GlobalSelectionSystem().setSelectedAllComponents( false );
2488 selector.addSelectable( SelectionIntersection( 0, 0 ), ( *i ) );
2489 m_dragSelectable.setSelected( true );
2493 for ( SelectionPool::iterator i = selector.begin(); i != selector.end(); ++i )
2495 ( *i ).second->setSelected( true );
2499 void setSelected( bool select ){
2500 m_selected = select;
2501 m_dragSelectable.setSelected( select );
2503 bool isSelected() const {
2504 return m_selected || m_dragSelectable.isSelected();
2508 class ClipManipulator : public Manipulator
2512 Manipulatable* GetManipulatable(){
2513 ERROR_MESSAGE( "clipper is not manipulatable" );
2517 void setSelected( bool select ){
2519 bool isSelected() const {
2524 class select_all : public scene::Graph::Walker
2528 select_all( bool select )
2529 : m_select( select ){
2531 bool pre( const scene::Path& path, scene::Instance& instance ) const {
2532 Selectable* selectable = Instance_getSelectable( instance );
2533 if ( selectable != 0 ) {
2534 selectable->setSelected( m_select );
2540 class select_all_component : public scene::Graph::Walker
2543 SelectionSystem::EComponentMode m_mode;
2545 select_all_component( bool select, SelectionSystem::EComponentMode mode )
2546 : m_select( select ), m_mode( mode ){
2548 bool pre( const scene::Path& path, scene::Instance& instance ) const {
2549 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
2550 if ( componentSelectionTestable ) {
2551 componentSelectionTestable->setSelectedComponents( m_select, m_mode );
2557 void Scene_SelectAll_Component( bool select, SelectionSystem::EComponentMode componentMode ){
2558 GlobalSceneGraph().traverse( select_all_component( select, componentMode ) );
2562 // RadiantSelectionSystem
2563 class RadiantSelectionSystem :
2564 public SelectionSystem,
2565 public Translatable,
2570 mutable Matrix4 m_pivot2world;
2571 Matrix4 m_pivot2world_start;
2572 Matrix4 m_manip2pivot_start;
2573 Translation m_translation;
2574 Rotation m_rotation;
2577 static Shader* m_state;
2579 EManipulatorMode m_manipulator_mode;
2580 Manipulator* m_manipulator;
2585 EComponentMode m_componentmode;
2587 SelectionCounter m_count_primitive;
2588 SelectionCounter m_count_component;
2590 TranslateManipulator m_translate_manipulator;
2591 RotateManipulator m_rotate_manipulator;
2592 ScaleManipulator m_scale_manipulator;
2593 DragManipulator m_drag_manipulator;
2594 ClipManipulator m_clip_manipulator;
2596 typedef SelectionList<scene::Instance> selection_t;
2597 selection_t m_selection;
2598 selection_t m_component_selection;
2600 Signal1<const Selectable&> m_selectionChanged_callbacks;
2602 void ConstructPivot() const;
2603 void setCustomPivotOrigin( Vector3& point ) const;
2605 void getSelectionAABB( AABB& bounds ) const;
2607 mutable bool m_pivotChanged;
2608 bool m_pivot_moving;
2609 mutable bool m_pivotIsCustom;
2611 void Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode );
2613 bool nothingSelected() const {
2614 return ( Mode() == eComponent && m_count_component.empty() )
2615 || ( Mode() == ePrimitive && m_count_primitive.empty() );
2630 RadiantSelectionSystem() :
2631 m_undo_begun( false ),
2632 m_mode( ePrimitive ),
2633 m_componentmode( eDefault ),
2634 m_count_primitive( SelectionChangedCaller( *this ) ),
2635 m_count_component( SelectionChangedCaller( *this ) ),
2636 m_translate_manipulator( *this, 2, 64 ),
2637 m_rotate_manipulator( *this, 8, 64 ),
2638 m_scale_manipulator( *this, 0, 64 ),
2639 m_pivotChanged( false ),
2640 m_pivot_moving( false ),
2641 m_pivotIsCustom( false ){
2642 SetManipulatorMode( eTranslate );
2644 addSelectionChangeCallback( PivotChangedSelectionCaller( *this ) );
2645 AddGridChangeCallback( PivotChangedCaller( *this ) );
2647 void pivotChanged() const {
2648 m_pivotChanged = true;
2649 SceneChangeNotify();
2651 typedef ConstMemberCaller<RadiantSelectionSystem, &RadiantSelectionSystem::pivotChanged> PivotChangedCaller;
2652 void pivotChangedSelection( const Selectable& selectable ){
2655 typedef MemberCaller1<RadiantSelectionSystem, const Selectable&, &RadiantSelectionSystem::pivotChangedSelection> PivotChangedSelectionCaller;
2657 void SetMode( EMode mode ){
2658 if ( m_mode != mode ) {
2663 EMode Mode() const {
2666 void SetComponentMode( EComponentMode mode ){
2667 m_componentmode = mode;
2669 EComponentMode ComponentMode() const {
2670 return m_componentmode;
2672 void SetManipulatorMode( EManipulatorMode mode ){
2673 m_pivotIsCustom = false;
2674 m_manipulator_mode = mode;
2675 switch ( m_manipulator_mode )
2677 case eTranslate: m_manipulator = &m_translate_manipulator; break;
2678 case eRotate: m_manipulator = &m_rotate_manipulator; break;
2679 case eScale: m_manipulator = &m_scale_manipulator; break;
2680 case eDrag: m_manipulator = &m_drag_manipulator; break;
2681 case eClip: m_manipulator = &m_clip_manipulator; break;
2685 EManipulatorMode ManipulatorMode() const {
2686 return m_manipulator_mode;
2689 SelectionChangeCallback getObserver( EMode mode ){
2690 if ( mode == ePrimitive ) {
2691 return makeCallback1( m_count_primitive );
2695 return makeCallback1( m_count_component );
2698 std::size_t countSelected() const {
2699 return m_count_primitive.size();
2701 std::size_t countSelectedComponents() const {
2702 return m_count_component.size();
2704 void onSelectedChanged( scene::Instance& instance, const Selectable& selectable ){
2705 if ( selectable.isSelected() ) {
2706 m_selection.append( instance );
2710 m_selection.erase( instance );
2713 ASSERT_MESSAGE( m_selection.size() == m_count_primitive.size(), "selection-tracking error" );
2715 void onComponentSelection( scene::Instance& instance, const Selectable& selectable ){
2716 if ( selectable.isSelected() ) {
2717 m_component_selection.append( instance );
2721 m_component_selection.erase( instance );
2724 ASSERT_MESSAGE( m_component_selection.size() == m_count_component.size(), "selection-tracking error" );
2726 scene::Instance& ultimateSelected() const {
2727 ASSERT_MESSAGE( m_selection.size() > 0, "no instance selected" );
2728 return m_selection.back();
2730 scene::Instance& penultimateSelected() const {
2731 ASSERT_MESSAGE( m_selection.size() > 1, "only one instance selected" );
2732 return *( *( --( --m_selection.end() ) ) );
2734 void setSelectedAll( bool selected ){
2735 GlobalSceneGraph().traverse( select_all( selected ) );
2737 m_manipulator->setSelected( selected );
2739 void setSelectedAllComponents( bool selected ){
2740 Scene_SelectAll_Component( selected, SelectionSystem::eVertex );
2741 Scene_SelectAll_Component( selected, SelectionSystem::eEdge );
2742 Scene_SelectAll_Component( selected, SelectionSystem::eFace );
2744 m_manipulator->setSelected( selected );
2747 void foreachSelected( const Visitor& visitor ) const {
2748 selection_t::const_iterator i = m_selection.begin();
2749 while ( i != m_selection.end() )
2751 visitor.visit( *( *( i++ ) ) );
2754 void foreachSelectedComponent( const Visitor& visitor ) const {
2755 selection_t::const_iterator i = m_component_selection.begin();
2756 while ( i != m_component_selection.end() )
2758 visitor.visit( *( *( i++ ) ) );
2762 void addSelectionChangeCallback( const SelectionChangeHandler& handler ){
2763 m_selectionChanged_callbacks.connectLast( handler );
2765 void selectionChanged( const Selectable& selectable ){
2766 m_selectionChanged_callbacks( selectable );
2768 typedef MemberCaller1<RadiantSelectionSystem, const Selectable&, &RadiantSelectionSystem::selectionChanged> SelectionChangedCaller;
2772 m_pivot2world_start = GetPivot2World();
2775 bool SelectManipulator( const View& view, const float device_point[2], const float device_epsilon[2] ){
2776 if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
2777 #if defined ( DEBUG_SELECTION )
2778 g_render_clipped.destroy();
2781 m_manipulator->setSelected( false );
2783 if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
2784 View scissored( view );
2785 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2786 m_manipulator->testSelect( scissored, GetPivot2World() );
2791 m_pivot_moving = m_manipulator->isSelected();
2793 if ( m_pivot_moving ) {
2795 pivot.update( GetPivot2World(), view.GetModelview(), view.GetProjection(), view.GetViewport() );
2797 m_manip2pivot_start = matrix4_multiplied_by_matrix4( matrix4_full_inverse( m_pivot2world_start ), pivot.m_worldSpace );
2799 Matrix4 device2manip;
2800 ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
2801 m_manipulator->GetManipulatable()->Construct( device2manip, device_point[0], device_point[1] );
2803 m_undo_begun = false;
2806 SceneChangeNotify();
2809 return m_pivot_moving;
2813 if ( Mode() == eComponent ) {
2814 setSelectedAllComponents( false );
2818 setSelectedAll( false );
2822 void deselectComponentsOrAll( bool components ){
2824 setSelectedAllComponents( false );
2832 void SelectPoint( const View& view, const float device_point[2], const float device_epsilon[2], RadiantSelectionSystem::EModifier modifier, bool face ){
2833 //globalOutputStream() << device_point[0] << " " << device_point[1] << "\n";
2834 ASSERT_MESSAGE( fabs( device_point[0] ) <= 1.0f && fabs( device_point[1] ) <= 1.0f, "point-selection error" );
2836 if ( modifier == eReplace ) {
2837 deselectComponentsOrAll( face );
2840 //nothingSelected() doesn't consider faces, selected in non-component mode, m
2841 if ( modifier == eCycle && nothingSelected() ){
2842 modifier = eReplace;
2845 #if defined ( DEBUG_SELECTION )
2846 g_render_clipped.destroy();
2850 View scissored( view );
2851 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2853 SelectionVolume volume( scissored );
2854 SelectionPool selector;
2856 Scene_TestSelect_Component( selector, volume, scissored, eFace );
2860 Scene_TestSelect( selector, volume, scissored, Mode(), ComponentMode() );
2863 if ( !selector.failed() ) {
2866 case RadiantSelectionSystem::eToggle:
2868 SelectableSortedSet::iterator best = selector.begin();
2869 // toggle selection of the object with least depth
2870 if ( ( *best ).second->isSelected() ) {
2871 ( *best ).second->setSelected( false );
2874 ( *best ).second->setSelected( true );
2878 // if cycle mode not enabled, enable it
2879 case RadiantSelectionSystem::eReplace:
2882 ( *selector.begin() ).second->setSelected( true );
2885 // select the next object in the list from the one already selected
2886 case RadiantSelectionSystem::eCycle:
2888 bool CycleSelectionOccured = false;
2889 SelectionPool::iterator i = selector.begin();
2890 while ( i != selector.end() )
2892 if ( ( *i ).second->isSelected() ) {
2893 deselectComponentsOrAll( face );
2895 if ( i != selector.end() ) {
2896 i->second->setSelected( true );
2900 selector.begin()->second->setSelected( true );
2902 CycleSelectionOccured = true;
2907 if( !CycleSelectionOccured ){
2908 deselectComponentsOrAll( face );
2909 ( *selector.begin() ).second->setSelected( true );
2913 case RadiantSelectionSystem::eSelect:
2915 SelectionPool::iterator best = selector.begin();
2916 if( !( *best ).second->isSelected() ){
2917 ( *best ).second->setSelected( true );
2919 SelectionPool::iterator i = best;
2921 while ( i != selector.end() )
2923 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
2924 if( !( *i ).second->isSelected() ){
2925 ( *i ).second->setSelected( true );
2935 case RadiantSelectionSystem::eDeselect:
2937 SelectionPool::iterator best = selector.begin();
2938 if( ( *best ).second->isSelected() ){
2939 ( *best ).second->setSelected( false );
2941 SelectionPool::iterator i = best;
2943 while ( i != selector.end() )
2945 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
2946 if( ( *i ).second->isSelected() ){
2947 ( *i ).second->setSelected( false );
2961 else if( modifier == eCycle ){
2962 deselectComponentsOrAll( face );
2967 bool SelectPoint_InitPaint( const View& view, const float device_point[2], const float device_epsilon[2], bool face ){
2968 ASSERT_MESSAGE( fabs( device_point[0] ) <= 1.0f && fabs( device_point[1] ) <= 1.0f, "point-selection error" );
2969 #if defined ( DEBUG_SELECTION )
2970 g_render_clipped.destroy();
2974 View scissored( view );
2975 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2977 SelectionVolume volume( scissored );
2978 SelectionPool selector;
2980 Scene_TestSelect_Component( selector, volume, scissored, eFace );
2984 Scene_TestSelect( selector, volume, scissored, Mode(), ComponentMode() );
2987 if ( !selector.failed() ) {
2988 SelectableSortedSet::iterator best = selector.begin();
2989 const bool wasSelected = ( *best ).second->isSelected();
2990 ( *best ).second->setSelected( !wasSelected );
2991 SelectableSortedSet::iterator i = best;
2993 while ( i != selector.end() )
2995 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
2996 ( *i ).second->setSelected( !wasSelected );
3003 return !wasSelected;
3011 void SelectArea( const View& view, const float device_point[2], const float device_delta[2], RadiantSelectionSystem::EModifier modifier, bool face ){
3012 if ( modifier == eReplace ) {
3013 deselectComponentsOrAll( face );
3016 #if defined ( DEBUG_SELECTION )
3017 g_render_clipped.destroy();
3021 View scissored( view );
3022 ConstructSelectionTest( scissored, SelectionBoxForArea( device_point, device_delta ) );
3024 SelectionVolume volume( scissored );
3027 Scene_TestSelect_Component( pool, volume, scissored, eFace );
3031 Scene_TestSelect( pool, volume, scissored, Mode(), ComponentMode() );
3034 for ( SelectionPool::iterator i = pool.begin(); i != pool.end(); ++i )
3036 ( *i ).second->setSelected( !( modifier == RadiantSelectionSystem::eToggle && ( *i ).second->isSelected() ) );
3042 void translate( const Vector3& translation ){
3043 if ( !nothingSelected() ) {
3044 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
3046 m_translation = translation;
3048 m_pivot2world = m_pivot2world_start;
3049 matrix4_translate_by_vec3( m_pivot2world, translation );
3051 if ( Mode() == eComponent ) {
3052 Scene_Translate_Component_Selected( GlobalSceneGraph(), m_translation );
3056 Scene_Translate_Selected( GlobalSceneGraph(), m_translation );
3059 SceneChangeNotify();
3062 void outputTranslation( TextOutputStream& ostream ){
3063 ostream << " -xyz " << m_translation.x() << " " << m_translation.y() << " " << m_translation.z();
3065 void rotate( const Quaternion& rotation ){
3066 if ( !nothingSelected() ) {
3067 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
3069 m_rotation = rotation;
3071 if ( Mode() == eComponent ) {
3072 Scene_Rotate_Component_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
3074 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3078 Scene_Rotate_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
3080 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3083 SceneChangeNotify();
3086 void outputRotation( TextOutputStream& ostream ){
3087 ostream << " -eulerXYZ " << m_rotation.x() << " " << m_rotation.y() << " " << m_rotation.z();
3089 void scale( const Vector3& scaling ){
3090 if ( !nothingSelected() ) {
3093 if ( Mode() == eComponent ) {
3094 Scene_Scale_Component_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
3098 Scene_Scale_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
3101 SceneChangeNotify();
3104 void outputScale( TextOutputStream& ostream ){
3105 ostream << " -scale " << m_scale.x() << " " << m_scale.y() << " " << m_scale.z();
3108 void rotateSelected( const Quaternion& rotation ){
3113 void translateSelected( const Vector3& translation ){
3115 translate( translation );
3118 void scaleSelected( const Vector3& scaling ){
3124 void MoveSelected( const View& view, const float device_point[2] ){
3125 if ( m_manipulator->isSelected() ) {
3126 if ( !m_undo_begun ) {
3127 m_undo_begun = true;
3128 GlobalUndoSystem().start();
3131 Matrix4 device2manip;
3132 ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
3133 m_manipulator->GetManipulatable()->Transform( m_manip2pivot_start, device2manip, device_point[0], device_point[1] );
3137 /// \todo Support view-dependent nudge.
3138 void NudgeManipulator( const Vector3& nudge, const Vector3& view ){
3139 if ( ManipulatorMode() == eTranslate || ManipulatorMode() == eDrag ) {
3140 translateSelected( nudge );
3145 void freezeTransforms();
3147 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const;
3148 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
3149 renderSolid( renderer, volume );
3152 const Matrix4& GetPivot2World() const {
3154 return m_pivot2world;
3157 static void constructStatic(){
3158 m_state = GlobalShaderCache().capture( "$POINT" );
3159 #if defined( DEBUG_SELECTION )
3160 g_state_clipped = GlobalShaderCache().capture( "$DEBUG_CLIPPED" );
3162 TranslateManipulator::m_state_wire = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3163 TranslateManipulator::m_state_fill = GlobalShaderCache().capture( "$FLATSHADE_OVERLAY" );
3164 RotateManipulator::m_state_outer = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3167 static void destroyStatic(){
3168 #if defined( DEBUG_SELECTION )
3169 GlobalShaderCache().release( "$DEBUG_CLIPPED" );
3171 GlobalShaderCache().release( "$WIRE_OVERLAY" );
3172 GlobalShaderCache().release( "$FLATSHADE_OVERLAY" );
3173 GlobalShaderCache().release( "$WIRE_OVERLAY" );
3174 GlobalShaderCache().release( "$POINT" );
3178 Shader* RadiantSelectionSystem::m_state = 0;
3183 RadiantSelectionSystem* g_RadiantSelectionSystem;
3185 inline RadiantSelectionSystem& getSelectionSystem(){
3186 return *g_RadiantSelectionSystem;
3192 class testselect_entity_visible : public scene::Graph::Walker
3194 Selector& m_selector;
3195 SelectionTest& m_test;
3197 testselect_entity_visible( Selector& selector, SelectionTest& test )
3198 : m_selector( selector ), m_test( test ){
3200 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3201 Selectable* selectable = Instance_getSelectable( instance );
3202 if ( selectable != 0
3203 && Node_isEntity( path.top() ) ) {
3204 m_selector.pushSelectable( *selectable );
3207 SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3208 if ( selectionTestable ) {
3209 selectionTestable->testSelect( m_selector, m_test );
3214 void post( const scene::Path& path, scene::Instance& instance ) const {
3215 Selectable* selectable = Instance_getSelectable( instance );
3216 if ( selectable != 0
3217 && Node_isEntity( path.top() ) ) {
3218 m_selector.popSelectable();
3223 class testselect_primitive_visible : public scene::Graph::Walker
3225 Selector& m_selector;
3226 SelectionTest& m_test;
3228 testselect_primitive_visible( Selector& selector, SelectionTest& test )
3229 : m_selector( selector ), m_test( test ){
3231 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3232 Selectable* selectable = Instance_getSelectable( instance );
3233 if ( selectable != 0 ) {
3234 m_selector.pushSelectable( *selectable );
3237 SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3238 if ( selectionTestable ) {
3239 selectionTestable->testSelect( m_selector, m_test );
3244 void post( const scene::Path& path, scene::Instance& instance ) const {
3245 Selectable* selectable = Instance_getSelectable( instance );
3246 if ( selectable != 0 ) {
3247 m_selector.popSelectable();
3252 class testselect_component_visible : public scene::Graph::Walker
3254 Selector& m_selector;
3255 SelectionTest& m_test;
3256 SelectionSystem::EComponentMode m_mode;
3258 testselect_component_visible( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3259 : m_selector( selector ), m_test( test ), m_mode( mode ){
3261 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3262 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3263 if ( componentSelectionTestable ) {
3264 componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3272 class testselect_component_visible_selected : public scene::Graph::Walker
3274 Selector& m_selector;
3275 SelectionTest& m_test;
3276 SelectionSystem::EComponentMode m_mode;
3278 testselect_component_visible_selected( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3279 : m_selector( selector ), m_test( test ), m_mode( mode ){
3281 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3282 Selectable* selectable = Instance_getSelectable( instance );
3283 if ( selectable != 0 && selectable->isSelected() ) {
3284 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3285 if ( componentSelectionTestable ) {
3286 componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3294 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume ){
3295 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_primitive_visible( selector, test ) );
3298 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3299 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible_selected( selector, test, componentMode ) );
3302 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3303 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible( selector, test, componentMode ) );
3306 void RadiantSelectionSystem::Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode ){
3311 Scene_forEachVisible( GlobalSceneGraph(), view, testselect_entity_visible( selector, test ) );
3315 Scene_TestSelect_Primitive( selector, test, view );
3318 Scene_TestSelect_Component_Selected( selector, test, view, componentMode );
3323 class FreezeTransforms : public scene::Graph::Walker
3326 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3327 TransformNode* transformNode = Node_getTransformNode( path.top() );
3328 if ( transformNode != 0 ) {
3329 Transformable* transform = Instance_getTransformable( instance );
3330 if ( transform != 0 ) {
3331 transform->freezeTransform();
3338 void RadiantSelectionSystem::freezeTransforms(){
3339 GlobalSceneGraph().traverse( FreezeTransforms() );
3343 void RadiantSelectionSystem::endMove(){
3346 if ( Mode() == ePrimitive ) {
3347 if ( ManipulatorMode() == eDrag ) {
3348 Scene_SelectAll_Component( false, SelectionSystem::eFace );
3352 m_pivot_moving = false;
3355 SceneChangeNotify();
3357 if ( m_undo_begun ) {
3358 StringOutputStream command;
3360 if ( ManipulatorMode() == eTranslate ) {
3361 command << "translateTool";
3362 outputTranslation( command );
3364 else if ( ManipulatorMode() == eRotate ) {
3365 command << "rotateTool";
3366 outputRotation( command );
3368 else if ( ManipulatorMode() == eScale ) {
3369 command << "scaleTool";
3370 outputScale( command );
3372 else if ( ManipulatorMode() == eDrag ) {
3373 command << "dragTool";
3376 GlobalUndoSystem().finish( command.c_str() );
3381 inline AABB Instance_getPivotBounds( scene::Instance& instance ){
3382 Entity* entity = Node_getEntity( instance.path().top() );
3384 && ( entity->getEntityClass().fixedsize
3385 || !node_is_group( instance.path().top() ) ) ) {
3386 Editable* editable = Node_getEditable( instance.path().top() );
3387 if ( editable != 0 ) {
3388 return AABB( vector4_to_vector3( matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ).t() ), Vector3( 0, 0, 0 ) );
3392 return AABB( vector4_to_vector3( instance.localToWorld().t() ), Vector3( 0, 0, 0 ) );
3396 return instance.worldAABB();
3399 class bounds_selected : public scene::Graph::Walker
3403 bounds_selected( AABB& bounds )
3404 : m_bounds( bounds ){
3407 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3408 Selectable* selectable = Instance_getSelectable( instance );
3409 if ( selectable != 0
3410 && selectable->isSelected() ) {
3411 aabb_extend_by_aabb_safe( m_bounds, Instance_getPivotBounds( instance ) );
3417 class bounds_selected_component : public scene::Graph::Walker
3421 bounds_selected_component( AABB& bounds )
3422 : m_bounds( bounds ){
3425 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3426 Selectable* selectable = Instance_getSelectable( instance );
3427 if ( selectable != 0
3428 && selectable->isSelected() ) {
3429 ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3430 if ( componentEditable ) {
3431 aabb_extend_by_aabb_safe( m_bounds, aabb_for_oriented_aabb_safe( componentEditable->getSelectedComponentsBounds(), instance.localToWorld() ) );
3438 void Scene_BoundsSelected( scene::Graph& graph, AABB& bounds ){
3439 graph.traverse( bounds_selected( bounds ) );
3442 void Scene_BoundsSelectedComponent( scene::Graph& graph, AABB& bounds ){
3443 graph.traverse( bounds_selected_component( bounds ) );
3447 inline void pivot_for_node( Matrix4& pivot, scene::Node& node, scene::Instance& instance ){
3448 ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3449 if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent
3450 && componentEditable != 0 ) {
3451 pivot = matrix4_translation_for_vec3( componentEditable->getSelectedComponentsBounds().origin );
3455 Bounded* bounded = Instance_getBounded( instance );
3456 if ( bounded != 0 ) {
3457 pivot = matrix4_translation_for_vec3( bounded->localAABB().origin );
3461 pivot = g_matrix4_identity;
3467 void RadiantSelectionSystem::ConstructPivot() const {
3468 if ( !m_pivotChanged || m_pivot_moving || m_pivotIsCustom ) {
3471 m_pivotChanged = false;
3473 Vector3 m_object_pivot;
3475 if ( !nothingSelected() ) {
3478 if ( Mode() == eComponent ) {
3479 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3483 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3485 m_object_pivot = bounds.origin;
3488 //vector3_snap( m_object_pivot, GetSnapGridSize() );
3489 //globalOutputStream() << m_object_pivot << "\n";
3490 m_pivot2world = matrix4_translation_for_vec3( m_object_pivot );
3492 switch ( m_manipulator_mode )
3497 if ( Mode() == eComponent ) {
3498 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3502 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3506 if ( Mode() == eComponent ) {
3507 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3511 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3520 void RadiantSelectionSystem::setCustomPivotOrigin( Vector3& point ) const {
3521 /*if ( !m_pivotChanged || m_pivot_moving ) {
3524 //m_pivotChanged = false;
3526 if ( !nothingSelected() && ( m_manipulator_mode == eTranslate || m_manipulator_mode == eRotate || m_manipulator_mode == eScale ) ) {
3528 if ( Mode() == eComponent ) {
3529 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3533 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3535 //globalOutputStream() << point << "\n";
3536 const float gridsize = GetSnapGridSize();
3537 //const float bbox_epsilon = gridsize / 4.0;
3539 for( std::size_t i = 0; i < 3; i++ ){
3540 if( point[i] < 900000 ){
3541 float bestsnapDist = fabs( bounds.origin[i] - point[i] );
3542 float bestsnapTo = bounds.origin[i];
3543 float othersnapDist = fabs( bounds.origin[i] + bounds.extents[i] - point[i] );
3544 if( othersnapDist < bestsnapDist ){
3545 bestsnapDist = othersnapDist;
3546 bestsnapTo = bounds.origin[i] + bounds.extents[i];
3548 othersnapDist = fabs( bounds.origin[i] - bounds.extents[i] - point[i] );
3549 if( othersnapDist < bestsnapDist ){
3550 bestsnapDist = othersnapDist;
3551 bestsnapTo = bounds.origin[i] - bounds.extents[i];
3553 othersnapDist = fabs( float_snapped( point[i], gridsize ) - point[i] );
3554 if( othersnapDist < bestsnapDist ){
3555 bestsnapDist = othersnapDist;
3556 bestsnapTo = float_snapped( point[i], gridsize );
3558 point[i] = bestsnapTo;
3560 /* if( float_equal_epsilon( point[i], bestsnapTo, bbox_epsilon ) ){
3561 point[i] = bestsnapTo;
3564 point[i] = float_snapped( point[i], gridsize );
3567 m_pivot2world[i + 12] = point[i]; //m_pivot2world.tx() .ty() .tz()
3571 switch ( m_manipulator_mode )
3576 if ( Mode() == eComponent ) {
3577 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3581 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3585 if ( Mode() == eComponent ) {
3586 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3590 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3597 m_pivotIsCustom = true;
3600 void RadiantSelectionSystem::getSelectionAABB( AABB& bounds ) const {
3601 if ( !nothingSelected() ) {
3602 if ( Mode() == eComponent ) {
3603 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3607 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3612 void GetSelectionAABB( AABB& bounds ){
3613 getSelectionSystem().getSelectionAABB( bounds );
3616 const Matrix4& ssGetPivot2World(){
3617 return getSelectionSystem().GetPivot2World();
3620 void RadiantSelectionSystem::renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
3621 //if(view->TestPoint(m_object_pivot))
3622 if ( !nothingSelected() ) {
3623 renderer.Highlight( Renderer::ePrimitive, false );
3624 renderer.Highlight( Renderer::eFace, false );
3626 renderer.SetState( m_state, Renderer::eWireframeOnly );
3627 renderer.SetState( m_state, Renderer::eFullMaterials );
3629 m_manipulator->render( renderer, volume, GetPivot2World() );
3632 #if defined( DEBUG_SELECTION )
3633 renderer.SetState( g_state_clipped, Renderer::eWireframeOnly );
3634 renderer.SetState( g_state_clipped, Renderer::eFullMaterials );
3635 renderer.addRenderable( g_render_clipped, g_render_clipped.m_world );
3640 void SelectionSystem_OnBoundsChanged(){
3641 getSelectionSystem().pivotChanged();
3645 SignalHandlerId SelectionSystem_boundsChanged;
3647 void SelectionSystem_Construct(){
3648 RadiantSelectionSystem::constructStatic();
3650 g_RadiantSelectionSystem = new RadiantSelectionSystem;
3652 SelectionSystem_boundsChanged = GlobalSceneGraph().addBoundsChangedCallback( FreeCaller<SelectionSystem_OnBoundsChanged>() );
3654 GlobalShaderCache().attachRenderable( getSelectionSystem() );
3657 void SelectionSystem_Destroy(){
3658 GlobalShaderCache().detachRenderable( getSelectionSystem() );
3660 GlobalSceneGraph().removeBoundsChangedCallback( SelectionSystem_boundsChanged );
3662 delete g_RadiantSelectionSystem;
3664 RadiantSelectionSystem::destroyStatic();
3670 inline float screen_normalised( float pos, std::size_t size ){
3671 return ( ( 2.0f * pos ) / size ) - 1.0f;
3674 typedef Vector2 DeviceVector;
3676 inline DeviceVector window_to_normalised_device( WindowVector window, std::size_t width, std::size_t height ){
3677 return DeviceVector( screen_normalised( window.x(), width ), screen_normalised( height - 1 - window.y(), height ) );
3680 inline float device_constrained( float pos ){
3681 return std::min( 1.0f, std::max( -1.0f, pos ) );
3684 inline DeviceVector device_constrained( DeviceVector device ){
3685 return DeviceVector( device_constrained( device.x() ), device_constrained( device.y() ) );
3688 inline float window_constrained( float pos, std::size_t origin, std::size_t size ){
3689 return std::min( static_cast<float>( origin + size ), std::max( static_cast<float>( origin ), pos ) );
3692 inline WindowVector window_constrained( WindowVector window, std::size_t x, std::size_t y, std::size_t width, std::size_t height ){
3693 return WindowVector( window_constrained( window.x(), x, width ), window_constrained( window.y(), y, height ) );
3696 typedef Callback1<DeviceVector> MouseEventCallback;
3698 Single<MouseEventCallback> g_mouseMovedCallback;
3699 Single<MouseEventCallback> g_mouseUpCallback;
3702 const ButtonIdentifier c_button_select = c_buttonLeft;
3703 const ButtonIdentifier c_button_select2 = c_buttonRight;
3704 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3705 const ModifierFlags c_modifier_toggle = c_modifierShift;
3706 const ModifierFlags c_modifier_replace = c_modifierShift | c_modifierAlt;
3707 const ModifierFlags c_modifier_face = c_modifierControl;
3709 const ButtonIdentifier c_button_select = c_buttonLeft;
3710 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3711 const ModifierFlags c_modifier_toggle = c_modifierControl;
3712 const ModifierFlags c_modifier_replace = c_modifierNone;
3713 const ModifierFlags c_modifier_face = c_modifierShift;
3715 const ModifierFlags c_modifier_toggle_face = c_modifier_toggle | c_modifier_face;
3716 const ModifierFlags c_modifier_replace_face = c_modifier_replace | c_modifier_face;
3718 const ButtonIdentifier c_button_texture = c_buttonMiddle;
3719 const ModifierFlags c_modifier_apply_texture1 = c_modifierControl | c_modifierShift;
3720 const ModifierFlags c_modifier_apply_texture2 = c_modifierControl;
3721 const ModifierFlags c_modifier_apply_texture3 = c_modifierShift;
3722 const ModifierFlags c_modifier_copy_texture = c_modifierNone;
3726 RadiantSelectionSystem::EModifier modifier_for_state( ModifierFlags state ){
3727 if ( ( state == c_modifier_toggle || state == c_modifier_toggle_face || state == c_modifier_face ) ) {
3729 return RadiantSelectionSystem::eReplace;
3732 return RadiantSelectionSystem::eToggle;
3735 return RadiantSelectionSystem::eManipulator;
3738 rect_t getDeviceArea() const {
3739 DeviceVector delta( m_current - m_start );
3740 if ( selecting() && fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
3741 return SelectionBoxForArea( &m_start[0], &delta[0] );
3745 rect_t default_area = { { 0, 0, }, { 0, 0, }, };
3746 return default_area;
3751 DeviceVector m_start;
3752 DeviceVector m_current;
3753 DeviceVector m_epsilon;
3754 ModifierFlags m_state;
3757 bool m_mouseMovedWhilePressed;
3760 RectangleCallback m_window_update;
3762 Selector_() : m_start( 0.0f, 0.0f ), m_current( 0.0f, 0.0f ), m_state( c_modifierNone ), m_mouse2( false ), m_mouseMoved( false ), m_mouseMovedWhilePressed( false ){
3766 m_window_update( getDeviceArea() );
3769 void testSelect( DeviceVector position ){
3770 RadiantSelectionSystem::EModifier modifier = modifier_for_state( m_state );
3771 if ( modifier != RadiantSelectionSystem::eManipulator ) {
3772 DeviceVector delta( position - m_start );
3773 if ( fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
3774 DeviceVector delta( position - m_start );
3775 //getSelectionSystem().SelectArea( *m_view, &m_start[0], &delta[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
3776 getSelectionSystem().SelectArea( *m_view, &m_start[0], &delta[0], RadiantSelectionSystem::eToggle, ( m_state & c_modifier_face ) != c_modifierNone );
3778 else if( !m_mouseMovedWhilePressed ){
3779 if ( modifier == RadiantSelectionSystem::eReplace && !m_mouseMoved ) {
3780 modifier = RadiantSelectionSystem::eCycle;
3782 getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
3786 m_start = m_current = DeviceVector( 0.0f, 0.0f );
3790 void testSelect_simpleM1( DeviceVector position ){
3791 /*RadiantSelectionSystem::EModifier modifier = RadiantSelectionSystem::eReplace;
3792 DeviceVector delta( position - m_start );
3793 if ( fabs( delta.x() ) < m_epsilon.x() && fabs( delta.y() ) < m_epsilon.y() ) {
3794 modifier = RadiantSelectionSystem::eCycle;
3796 getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], modifier, false );*/
3797 getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], m_mouseMoved ? RadiantSelectionSystem::eReplace : RadiantSelectionSystem::eCycle, false );
3798 m_start = m_current = device_constrained( position );
3802 bool selecting() const {
3803 return m_state != c_modifier_manipulator && m_mouse2;
3806 void setState( ModifierFlags state ){
3807 bool was_selecting = selecting();
3809 if ( was_selecting ^ selecting() ) {
3814 ModifierFlags getState() const {
3818 void modifierEnable( ModifierFlags type ){
3819 setState( bitfield_enable( getState(), type ) );
3821 void modifierDisable( ModifierFlags type ){
3822 setState( bitfield_disable( getState(), type ) );
3825 void mouseDown( DeviceVector position ){
3826 m_start = m_current = device_constrained( position );
3827 if( !m_mouse2 && m_state != c_modifierNone ){
3828 m_paintSelect = getSelectionSystem().SelectPoint_InitPaint( *m_view, &position[0], &m_epsilon[0], ( m_state & c_modifier_face ) != c_modifierNone );
3832 void mouseMoved( DeviceVector position ){
3833 m_current = device_constrained( position );
3834 m_mouseMovedWhilePressed = true;
3838 else if( m_state != c_modifier_manipulator ){
3839 getSelectionSystem().SelectPoint( *m_view, &m_current[0], &m_epsilon[0],
3840 m_paintSelect ? RadiantSelectionSystem::eSelect : RadiantSelectionSystem::eDeselect,
3841 ( m_state & c_modifier_face ) != c_modifierNone );
3844 typedef MemberCaller1<Selector_, DeviceVector, &Selector_::mouseMoved> MouseMovedCaller;
3846 void mouseUp( DeviceVector position ){
3848 testSelect( device_constrained( position ) );
3851 m_start = m_current = DeviceVector( 0.0f, 0.0f );
3854 g_mouseMovedCallback.clear();
3855 g_mouseUpCallback.clear();
3857 typedef MemberCaller1<Selector_, DeviceVector, &Selector_::mouseUp> MouseUpCaller;
3864 DeviceVector m_epsilon;
3867 bool mouseDown( DeviceVector position ){
3868 return getSelectionSystem().SelectManipulator( *m_view, &position[0], &m_epsilon[0] );
3871 void mouseMoved( DeviceVector position ){
3872 getSelectionSystem().MoveSelected( *m_view, &position[0] );
3874 typedef MemberCaller1<Manipulator_, DeviceVector, &Manipulator_::mouseMoved> MouseMovedCaller;
3876 void mouseUp( DeviceVector position ){
3877 getSelectionSystem().endMove();
3878 g_mouseMovedCallback.clear();
3879 g_mouseUpCallback.clear();
3881 typedef MemberCaller1<Manipulator_, DeviceVector, &Manipulator_::mouseUp> MouseUpCaller;
3884 void Scene_copyClosestTexture( SelectionTest& test );
3885 void Scene_applyClosestTexture( SelectionTest& test );
3887 class RadiantWindowObserver : public SelectionSystemWindowObserver
3900 Selector_ m_selector;
3901 Manipulator_ m_manipulator;
3903 RadiantWindowObserver() : m_mouse_down( false ){
3908 void setView( const View& view ){
3909 m_selector.m_view = &view;
3910 m_manipulator.m_view = &view;
3912 void setRectangleDrawCallback( const RectangleCallback& callback ){
3913 m_selector.m_window_update = callback;
3915 void onSizeChanged( int width, int height ){
3918 DeviceVector epsilon( SELECT_EPSILON / static_cast<float>( m_width ), SELECT_EPSILON / static_cast<float>( m_height ) );
3919 m_selector.m_epsilon = m_manipulator.m_epsilon = epsilon;
3921 void onMouseDown( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
3922 if ( button == c_button_select || ( button == c_button_select2 && modifiers != c_modifierNone ) ) {
3923 m_mouse_down = true;
3924 //m_selector.m_mouseMoved = false;
3926 DeviceVector devicePosition( window_to_normalised_device( position, m_width, m_height ) );
3927 if ( modifiers == c_modifier_manipulator && m_manipulator.mouseDown( devicePosition ) ) {
3928 g_mouseMovedCallback.insert( MouseEventCallback( Manipulator_::MouseMovedCaller( m_manipulator ) ) );
3929 g_mouseUpCallback.insert( MouseEventCallback( Manipulator_::MouseUpCaller( m_manipulator ) ) );
3933 if ( button == c_button_select ) {
3934 m_selector.m_mouse2 = false;
3937 m_selector.m_mouse2 = true;
3939 m_selector.mouseDown( devicePosition );
3940 g_mouseMovedCallback.insert( MouseEventCallback( Selector_::MouseMovedCaller( m_selector ) ) );
3941 g_mouseUpCallback.insert( MouseEventCallback( Selector_::MouseUpCaller( m_selector ) ) );
3944 else if ( button == c_button_texture ) {
3945 DeviceVector devicePosition( device_constrained( window_to_normalised_device( position, m_width, m_height ) ) );
3947 View scissored( *m_selector.m_view );
3948 ConstructSelectionTest( scissored, SelectionBoxForPoint( &devicePosition[0], &m_selector.m_epsilon[0] ) );
3949 SelectionVolume volume( scissored );
3951 if ( modifiers == c_modifier_apply_texture1 || modifiers == c_modifier_apply_texture2 || modifiers == c_modifier_apply_texture3 ) {
3952 Scene_applyClosestTexture( volume );
3954 else if ( modifiers == c_modifier_copy_texture ) {
3955 Scene_copyClosestTexture( volume );
3959 void onMouseMotion( const WindowVector& position, ModifierFlags modifiers ){
3960 m_selector.m_mouseMoved = true;
3961 if ( m_mouse_down && !g_mouseMovedCallback.empty() ) {
3962 m_selector.m_mouseMovedWhilePressed = true;
3963 g_mouseMovedCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
3966 void onMouseUp( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
3967 if ( ( button == c_button_select || button == c_button_select2 ) && !g_mouseUpCallback.empty() ) {
3968 m_mouse_down = false;
3970 g_mouseUpCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
3972 //L button w/o scene changed = tunnel selection
3973 if( // !getSelectionSystem().m_undo_begun &&
3974 modifiers == c_modifierNone && button == c_button_select &&
3975 //( !m_selector.m_mouseMoved || !m_mouse_down ) &&
3976 !m_selector.m_mouseMovedWhilePressed &&
3977 ( getSelectionSystem().Mode() != SelectionSystem::eComponent || getSelectionSystem().ManipulatorMode() != SelectionSystem::eDrag ) ){
3978 m_selector.testSelect_simpleM1( device_constrained( window_to_normalised_device( position, m_width, m_height ) ) );
3980 //getSelectionSystem().m_undo_begun = false;
3981 m_selector.m_mouseMoved = false;
3982 m_selector.m_mouseMovedWhilePressed = false;
3984 void onModifierDown( ModifierFlags type ){
3985 m_selector.modifierEnable( type );
3987 void onModifierUp( ModifierFlags type ){
3988 m_selector.modifierDisable( type );
3994 SelectionSystemWindowObserver* NewWindowObserver(){
3995 return new RadiantWindowObserver;
4000 #include "modulesystem/singletonmodule.h"
4001 #include "modulesystem/moduleregistry.h"
4003 class SelectionDependencies :
4004 public GlobalSceneGraphModuleRef,
4005 public GlobalShaderCacheModuleRef,
4006 public GlobalOpenGLModuleRef
4010 class SelectionAPI : public TypeSystemRef
4012 SelectionSystem* m_selection;
4014 typedef SelectionSystem Type;
4015 STRING_CONSTANT( Name, "*" );
4018 SelectionSystem_Construct();
4020 m_selection = &getSelectionSystem();
4023 SelectionSystem_Destroy();
4025 SelectionSystem* getTable(){
4030 typedef SingletonModule<SelectionAPI, SelectionDependencies> SelectionModule;
4031 typedef Static<SelectionModule> StaticSelectionModule;
4032 StaticRegisterModule staticRegisterSelection( StaticSelectionModule::instance() );