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"
23 #include "globaldefs.h"
25 #include "debugging/debugging.h"
31 #include "windowobserver.h"
35 #include "renderable.h"
36 #include "selectable.h"
39 #include "math/frustum.h"
40 #include "signal/signal.h"
41 #include "generic/object.h"
42 #include "selectionlib.h"
46 #include "stream/stringstream.h"
47 #include "eclasslib.h"
48 #include "generic/bitfield.h"
49 #include "generic/static.h"
52 #include "container/container.h"
56 TextOutputStream& ostream_write( TextOutputStream& t, const Vector4& v ){
57 return t << "[ " << v.x() << " " << v.y() << " " << v.z() << " " << v.w() << " ]";
60 TextOutputStream& ostream_write( TextOutputStream& t, const Matrix4& m ){
61 return t << "[ " << m.x() << " " << m.y() << " " << m.z() << " " << m.t() << " ]";
67 Matrix4 m_viewpointSpace;
68 Matrix4 m_viewplaneSpace;
69 Vector3 m_axis_screen;
71 void update( const Matrix4& pivot2world, const Matrix4& modelview, const Matrix4& projection, const Matrix4& viewport ){
72 Pivot2World_worldSpace( m_worldSpace, pivot2world, modelview, projection, viewport );
73 Pivot2World_viewpointSpace( m_viewpointSpace, m_axis_screen, pivot2world, modelview, projection, viewport );
74 Pivot2World_viewplaneSpace( m_viewplaneSpace, pivot2world, modelview, projection, viewport );
79 void point_for_device_point( Vector3& point, const Matrix4& device2object, const float x, const float y, const float z ){
80 // transform from normalised device coords to object coords
81 point = vector4_projected( matrix4_transformed_vector4( device2object, Vector4( x, y, z, 1 ) ) );
84 void ray_for_device_point( Ray& ray, const Matrix4& device2object, const float x, const float y ){
85 // point at x, y, zNear
86 point_for_device_point( ray.origin, device2object, x, y, -1 );
88 // point at x, y, zFar
89 point_for_device_point( ray.direction, device2object, x, y, 1 );
92 vector3_subtract( ray.direction, ray.origin );
93 vector3_normalise( ray.direction );
96 bool sphere_intersect_ray( const Vector3& origin, float radius, const Ray& ray, Vector3& intersection ){
97 intersection = vector3_subtracted( origin, ray.origin );
98 const double a = vector3_dot( intersection, ray.direction );
99 const double d = radius * radius - ( vector3_dot( intersection, intersection ) - a * a );
102 intersection = vector3_added( ray.origin, vector3_scaled( ray.direction, a - sqrt( d ) ) );
107 intersection = vector3_added( ray.origin, vector3_scaled( ray.direction, a ) );
112 void ray_intersect_ray( const Ray& ray, const Ray& other, Vector3& intersection ){
113 intersection = vector3_subtracted( ray.origin, other.origin );
114 //float a = 1;//vector3_dot(ray.direction, ray.direction); // always >= 0
115 double dot = vector3_dot( ray.direction, other.direction );
116 //float c = 1;//vector3_dot(other.direction, other.direction); // always >= 0
117 double d = vector3_dot( ray.direction, intersection );
118 double e = vector3_dot( other.direction, intersection );
119 double D = 1 - dot * dot; //a*c - dot*dot; // always >= 0
121 if ( D < 0.000001 ) {
122 // the lines are almost parallel
123 intersection = vector3_added( other.origin, vector3_scaled( other.direction, e ) );
127 intersection = vector3_added( other.origin, vector3_scaled( other.direction, ( e - dot * d ) / D ) );
131 const Vector3 g_origin( 0, 0, 0 );
132 const float g_radius = 64;
134 void point_on_sphere( Vector3& point, const Matrix4& device2object, const float x, const float y ){
136 ray_for_device_point( ray, device2object, x, y );
137 sphere_intersect_ray( g_origin, g_radius, ray, point );
140 void point_on_axis( Vector3& point, const Vector3& axis, const Matrix4& device2object, const float x, const float y ){
142 ray_for_device_point( ray, device2object, x, y );
143 ray_intersect_ray( ray, Ray( Vector3( 0, 0, 0 ), axis ), point );
146 void point_on_plane( Vector3& point, const Matrix4& device2object, const float x, const float y ){
147 Matrix4 object2device( matrix4_full_inverse( device2object ) );
148 point = vector4_projected( matrix4_transformed_vector4( device2object, Vector4( x, y, object2device[14] / object2device[15], 1 ) ) );
151 //! a and b are unit vectors .. returns angle in radians
152 inline float angle_between( const Vector3& a, const Vector3& b ){
153 return static_cast<float>( 2.0 * atan2(
154 vector3_length( vector3_subtracted( a, b ) ),
155 vector3_length( vector3_added( a, b ) )
164 test_quat( const Vector3& from, const Vector3& to ){
165 Vector4 quaternion( quaternion_for_unit_vectors( from, to ) );
166 Matrix4 matrix( matrix4_rotation_for_quaternion( quaternion_multiplied_by_quaternion( quaternion, c_quaternion_identity ) ) );
171 static test_quat bleh( g_vector3_axis_x, g_vector3_axis_y );
174 //! axis is a unit vector
175 inline void constrain_to_axis( Vector3& vec, const Vector3& axis ){
176 vec = vector3_normalised( vector3_added( vec, vector3_scaled( axis, -vector3_dot( vec, axis ) ) ) );
179 //! a and b are unit vectors .. a and b must be orthogonal to axis .. returns angle in radians
180 float angle_for_axis( const Vector3& a, const Vector3& b, const Vector3& axis ){
181 if ( vector3_dot( axis, vector3_cross( a, b ) ) > 0.0 ) {
182 return angle_between( a, b );
185 return -angle_between( a, b );
189 float distance_for_axis( const Vector3& a, const Vector3& b, const Vector3& axis ){
190 return static_cast<float>( vector3_dot( b, axis ) - vector3_dot( a, axis ) );
196 virtual void Construct( const Matrix4& device2manip, const float x, const float y ) = 0;
197 virtual void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ) = 0;
200 void transform_local2object( Matrix4& object, const Matrix4& local, const Matrix4& local2object ){
201 object = matrix4_multiplied_by_matrix4(
202 matrix4_multiplied_by_matrix4( local2object, local ),
203 matrix4_full_inverse( local2object )
210 virtual ~Rotatable() = default;
211 virtual void rotate( const Quaternion& rotation ) = 0;
214 class RotateFree : public Manipulatable
217 Rotatable& m_rotatable;
219 RotateFree( Rotatable& rotatable )
220 : m_rotatable( rotatable ){
222 void Construct( const Matrix4& device2manip, const float x, const float y ){
223 point_on_sphere( m_start, device2manip, x, y );
224 vector3_normalise( m_start );
226 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
229 point_on_sphere( current, device2manip, x, y );
230 vector3_normalise( current );
232 m_rotatable.rotate( quaternion_for_unit_vectors( m_start, current ) );
236 class RotateAxis : public Manipulatable
240 Rotatable& m_rotatable;
242 RotateAxis( Rotatable& rotatable )
243 : m_rotatable( rotatable ){
245 void Construct( const Matrix4& device2manip, const float x, const float y ){
246 point_on_sphere( m_start, device2manip, x, y );
247 constrain_to_axis( m_start, m_axis );
249 /// \brief Converts current position to a normalised vector orthogonal to axis.
250 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
252 point_on_sphere( current, device2manip, x, y );
253 constrain_to_axis( current, m_axis );
255 m_rotatable.rotate( quaternion_for_axisangle( m_axis, angle_for_axis( m_start, current, m_axis ) ) );
258 void SetAxis( const Vector3& axis ){
263 void translation_local2object( Vector3& object, const Vector3& local, const Matrix4& local2object ){
264 object = matrix4_get_translation_vec3(
265 matrix4_multiplied_by_matrix4(
266 matrix4_translated_by_vec3( local2object, local ),
267 matrix4_full_inverse( local2object )
275 virtual ~Translatable() = default;
276 virtual void translate( const Vector3& translation ) = 0;
279 class TranslateAxis : public Manipulatable
283 Translatable& m_translatable;
285 TranslateAxis( Translatable& translatable )
286 : m_translatable( translatable ){
288 void Construct( const Matrix4& device2manip, const float x, const float y ){
289 point_on_axis( m_start, m_axis, device2manip, x, y );
291 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
293 point_on_axis( current, m_axis, device2manip, x, y );
294 current = vector3_scaled( m_axis, distance_for_axis( m_start, current, m_axis ) );
296 translation_local2object( current, current, manip2object );
297 vector3_snap( current, GetSnapGridSize() );
299 m_translatable.translate( current );
302 void SetAxis( const Vector3& axis ){
307 class TranslateFree : public Manipulatable
311 Translatable& m_translatable;
313 TranslateFree( Translatable& translatable )
314 : m_translatable( translatable ){
316 void Construct( const Matrix4& device2manip, const float x, const float y ){
317 point_on_plane( m_start, device2manip, x, y );
319 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
321 point_on_plane( current, device2manip, x, y );
322 current = vector3_subtracted( current, m_start );
324 translation_local2object( current, current, manip2object );
325 vector3_snap( current, GetSnapGridSize() );
327 m_translatable.translate( current );
331 void GetSelectionAABB( AABB& bounds );
332 const Matrix4& ssGetPivot2World();
337 virtual ~Scalable() = default;
338 virtual void scale( const Vector3& scaling ) = 0;
342 class ScaleAxis : public Manipulatable
347 Scalable& m_scalable;
350 Vector3 m_transform_origin;
351 Vector3 m_choosen_extent;
354 ScaleAxis( Scalable& scalable )
355 : m_scalable( scalable ){
357 void Construct( const Matrix4& device2manip, const float x, const float y ){
358 point_on_axis( m_start, m_axis, device2manip, x, y );
360 GetSelectionAABB( m_aabb );
361 m_transform_origin = vector4_to_vector3( ssGetPivot2World().t() );
362 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] ),
363 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] ),
364 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] )
368 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
369 //globalOutputStream() << "manip2object: " << manip2object << " device2manip: " << device2manip << " x: " << x << " y:" << y <<"\n";
371 point_on_axis( current, m_axis, device2manip, x, y );
372 Vector3 delta = vector3_subtracted( current, m_start );
374 translation_local2object( delta, delta, manip2object );
375 vector3_snap( delta, GetSnapGridSize() );
377 Vector3 start( vector3_snapped( m_start, GetSnapGridSize() ) );
378 //globalOutputStream() << "start: " << start << " delta: " << delta <<"\n";
380 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
381 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
382 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
385 for( std::size_t i = 0; i < 3; i++ ){
386 if( m_choosen_extent[i] > 0.0625 ){ //epsilon to prevent too high scale for set of models, having really small extent, formed by origins
387 scale[i] = ( m_choosen_extent[i] + delta[i] ) / m_choosen_extent[i];
391 m_scalable.scale( scale );
394 void SetAxis( const Vector3& axis ){
399 class ScaleFree : public Manipulatable
403 Scalable& m_scalable;
406 Vector3 m_transform_origin;
407 Vector3 m_choosen_extent;
410 ScaleFree( Scalable& scalable )
411 : m_scalable( scalable ){
413 void Construct( const Matrix4& device2manip, const float x, const float y ){
414 point_on_plane( m_start, device2manip, x, y );
416 GetSelectionAABB( m_aabb );
417 m_transform_origin = vector4_to_vector3( ssGetPivot2World().t() );
418 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] ),
419 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] ),
420 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] )
423 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y ){
425 point_on_plane( current, device2manip, x, y );
426 Vector3 delta = vector3_subtracted( current, m_start );
428 translation_local2object( delta, delta, manip2object );
429 vector3_snap( delta, GetSnapGridSize() );
431 Vector3 start( vector3_snapped( m_start, GetSnapGridSize() ) );
433 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
434 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
435 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
438 for( std::size_t i = 0; i < 3; i++ ){
439 if( m_choosen_extent[i] > 0.0625 ){
440 scale[i] = ( m_choosen_extent[i] + delta[i] ) / m_choosen_extent[i];
444 m_scalable.scale( scale );
457 class RenderableClippedPrimitive : public OpenGLRenderable
461 PointVertex m_points[9];
465 std::vector<primitive_t> m_primitives;
469 void render( RenderStateFlags state ) const {
470 for ( std::size_t i = 0; i < m_primitives.size(); ++i )
472 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_primitives[i].m_points[0].colour );
473 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_primitives[i].m_points[0].vertex );
474 switch ( m_primitives[i].m_count )
477 case 2: glDrawArrays( GL_LINES, 0, GLsizei( m_primitives[i].m_count ) ); break;
478 default: glDrawArrays( GL_POLYGON, 0, GLsizei( m_primitives[i].m_count ) ); break;
483 void construct( const Matrix4& world2device ){
484 m_inverse = matrix4_full_inverse( world2device );
485 m_world = g_matrix4_identity;
488 void insert( const Vector4 clipped[9], std::size_t count ){
491 m_primitives.back().m_count = count;
492 for ( std::size_t i = 0; i < count; ++i )
494 Vector3 world_point( vector4_projected( matrix4_transformed_vector4( m_inverse, clipped[i] ) ) );
495 m_primitives.back().m_points[i].vertex = vertex3f_for_vector3( world_point );
500 m_primitives.clear();
504 m_primitives.push_back( primitive_t() );
506 const Colour4b colour_clipped( 255, 127, 0, 255 );
508 for ( std::size_t i = 0; i < 9; ++i )
509 m_primitives.back().m_points[i].colour = colour_clipped;
514 #define DEBUG_SELECTION
517 #if defined( DEBUG_SELECTION )
518 Shader* g_state_clipped;
519 RenderableClippedPrimitive g_render_clipped;
524 // dist_Point_to_Line(): get the distance of a point to a line.
525 // Input: a Point P and a Line L (in any dimension)
526 // Return: the shortest distance from P to L
528 dist_Point_to_Line( Point P, Line L ){
529 Vector v = L.P1 - L.P0;
532 double c1 = dot( w,v );
533 double c2 = dot( v,v );
536 Point Pb = L.P0 + b * v;
543 typedef Vector3 point_type;
545 Segment3D( const point_type& _p0, const point_type& _p1 )
546 : p0( _p0 ), p1( _p1 ){
552 typedef Vector3 Point3D;
554 inline double vector3_distance_squared( const Point3D& a, const Point3D& b ){
555 return vector3_length_squared( b - a );
558 // get the distance of a point to a segment.
559 Point3D segment_closest_point_to_point( const Segment3D& segment, const Point3D& point ){
560 Vector3 v = segment.p1 - segment.p0;
561 Vector3 w = point - segment.p0;
563 double c1 = vector3_dot( w,v );
568 double c2 = vector3_dot( v,v );
573 return Point3D( segment.p0 + v * ( c1 / c2 ) );
576 double segment_dist_to_point_3d( const Segment3D& segment, const Point3D& point ){
577 return vector3_distance_squared( point, segment_closest_point_to_point( segment, point ) );
580 typedef Vector3 point_t;
581 typedef const Vector3* point_iterator_t;
583 // crossing number test for a point in a polygon
584 // This code is patterned after [Franklin, 2000]
585 bool point_test_polygon_2d( const point_t& P, point_iterator_t start, point_iterator_t finish ){
586 std::size_t crossings = 0;
588 // loop through all edges of the polygon
589 for ( point_iterator_t prev = finish - 1, cur = start; cur != finish; prev = cur, ++cur )
590 { // edge from (*prev) to (*cur)
591 if ( ( ( ( *prev )[1] <= P[1] ) && ( ( *cur )[1] > P[1] ) ) // an upward crossing
592 || ( ( ( *prev )[1] > P[1] ) && ( ( *cur )[1] <= P[1] ) ) ) { // a downward crossing
593 // compute the actual edge-ray intersect x-coordinate
594 float vt = (float)( P[1] - ( *prev )[1] ) / ( ( *cur )[1] - ( *prev )[1] );
595 if ( P[0] < ( *prev )[0] + vt * ( ( *cur )[0] - ( *prev )[0] ) ) { // P[0] < intersect
596 ++crossings; // a valid crossing of y=P[1] right of P[0]
600 return ( crossings & 0x1 ) != 0; // 0 if even (out), and 1 if odd (in)
603 inline double triangle_signed_area_XY( const Vector3& p0, const Vector3& p1, const Vector3& p2 ){
604 return ( ( p1[0] - p0[0] ) * ( p2[1] - p0[1] ) ) - ( ( p2[0] - p0[0] ) * ( p1[1] - p0[1] ) );
615 inline SelectionIntersection select_point_from_clipped( Vector4& clipped ){
616 return SelectionIntersection( clipped[2] / clipped[3], static_cast<float>( vector3_length_squared( Vector3( clipped[0] / clipped[3], clipped[1] / clipped[3], 0 ) ) ) );
619 void BestPoint( std::size_t count, Vector4 clipped[9], SelectionIntersection& best, clipcull_t cull ){
620 Vector3 normalised[9];
623 for ( std::size_t i = 0; i < count; ++i )
625 normalised[i][0] = clipped[i][0] / clipped[i][3];
626 normalised[i][1] = clipped[i][1] / clipped[i][3];
627 normalised[i][2] = clipped[i][2] / clipped[i][3];
631 if ( cull != eClipCullNone && count > 2 ) {
632 double signed_area = triangle_signed_area_XY( normalised[0], normalised[1], normalised[2] );
634 if ( ( cull == eClipCullCW && signed_area > 0 )
635 || ( cull == eClipCullCCW && signed_area < 0 ) ) {
641 Segment3D segment( normalised[0], normalised[1] );
642 Point3D point = segment_closest_point_to_point( segment, Vector3( 0, 0, 0 ) );
643 assign_if_closer( best, SelectionIntersection( point.z(), 0 ) );
645 else if ( count > 2 && !point_test_polygon_2d( Vector3( 0, 0, 0 ), normalised, normalised + count ) ) {
646 point_iterator_t end = normalised + count;
647 for ( point_iterator_t previous = end - 1, current = normalised; current != end; previous = current, ++current )
649 Segment3D segment( *previous, *current );
650 Point3D point = segment_closest_point_to_point( segment, Vector3( 0, 0, 0 ) );
651 float depth = point.z();
653 float distance = static_cast<float>( vector3_length_squared( point ) );
655 assign_if_closer( best, SelectionIntersection( depth, distance ) );
658 else if ( count > 2 ) {
661 SelectionIntersection(
662 static_cast<float>( ray_distance_to_plane(
663 Ray( Vector3( 0, 0, 0 ), Vector3( 0, 0, 1 ) ),
664 plane3_for_points( normalised[0], normalised[1], normalised[2] )
671 #if defined( DEBUG_SELECTION )
673 g_render_clipped.insert( clipped, count );
678 void LineStrip_BestPoint( const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
680 for ( std::size_t i = 0; ( i + 1 ) < size; ++i )
682 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[i + 1].vertex ), clipped );
683 BestPoint( count, clipped, best, eClipCullNone );
687 void LineLoop_BestPoint( const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
689 for ( std::size_t i = 0; i < size; ++i )
691 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[( i + 1 ) % size].vertex ), clipped );
692 BestPoint( count, clipped, best, eClipCullNone );
696 void Line_BestPoint( const Matrix4& local2view, const PointVertex vertices[2], SelectionIntersection& best ){
698 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[0].vertex ), vertex3f_to_vector3( vertices[1].vertex ), clipped );
699 BestPoint( count, clipped, best, eClipCullNone );
702 void Circle_BestPoint( const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
704 for ( std::size_t i = 0; i < size; ++i )
706 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 );
707 BestPoint( count, clipped, best, cull );
711 void Quad_BestPoint( const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, SelectionIntersection& best ){
714 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 );
715 BestPoint( count, clipped, best, cull );
718 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 );
719 BestPoint( count, clipped, best, cull );
723 struct FlatShadedVertex
734 typedef FlatShadedVertex* FlatShadedVertexIterator;
735 void Triangles_BestPoint( const Matrix4& local2view, clipcull_t cull, FlatShadedVertexIterator first, FlatShadedVertexIterator last, SelectionIntersection& best ){
736 for ( FlatShadedVertexIterator x( first ), y( first + 1 ), z( first + 2 ); x != last; x += 3, y += 3, z += 3 )
740 matrix4_clip_triangle(
742 reinterpret_cast<const Vector3&>( ( *x ).vertex ),
743 reinterpret_cast<const Vector3&>( ( *y ).vertex ),
744 reinterpret_cast<const Vector3&>( ( *z ).vertex ),
755 typedef std::multimap<SelectionIntersection, Selectable*> SelectableSortedSet;
757 class SelectionPool : public Selector
759 SelectableSortedSet m_pool;
760 SelectionIntersection m_intersection;
761 Selectable* m_selectable;
764 void pushSelectable( Selectable& selectable ){
765 m_intersection = SelectionIntersection();
766 m_selectable = &selectable;
768 void popSelectable(){
769 addSelectable( m_intersection, m_selectable );
770 m_intersection = SelectionIntersection();
772 void addIntersection( const SelectionIntersection& intersection ){
773 assign_if_closer( m_intersection, intersection );
775 void addSelectable( const SelectionIntersection& intersection, Selectable* selectable ){
776 if ( intersection.valid() ) {
777 m_pool.insert( SelectableSortedSet::value_type( intersection, selectable ) );
781 typedef SelectableSortedSet::iterator iterator;
784 return m_pool.begin();
791 return m_pool.empty();
796 const Colour4b g_colour_sphere( 0, 0, 0, 255 );
797 const Colour4b g_colour_screen( 0, 255, 255, 255 );
798 const Colour4b g_colour_selected( 255, 255, 0, 255 );
800 inline const Colour4b& colourSelected( const Colour4b& colour, bool selected ){
801 return ( selected ) ? g_colour_selected : colour;
804 template<typename remap_policy>
805 inline void draw_semicircle( const std::size_t segments, const float radius, PointVertex* vertices, remap_policy remap ){
806 const double increment = c_pi / double(segments << 2);
808 std::size_t count = 0;
811 remap_policy::set( vertices[segments << 2].vertex, -radius, 0, 0 );
812 while ( count < segments )
814 PointVertex* i = vertices + count;
815 PointVertex* j = vertices + ( ( segments << 1 ) - ( count + 1 ) );
817 PointVertex* k = i + ( segments << 1 );
818 PointVertex* l = j + ( segments << 1 );
821 PointVertex* m = i + ( segments << 2 );
822 PointVertex* n = j + ( segments << 2 );
823 PointVertex* o = k + ( segments << 2 );
824 PointVertex* p = l + ( segments << 2 );
827 remap_policy::set( i->vertex, x,-y, 0 );
828 remap_policy::set( k->vertex,-y,-x, 0 );
830 remap_policy::set( m->vertex,-x, y, 0 );
831 remap_policy::set( o->vertex, y, x, 0 );
837 const double theta = increment * count;
838 x = static_cast<float>( radius * cos( theta ) );
839 y = static_cast<float>( radius * sin( theta ) );
842 remap_policy::set( j->vertex, y,-x, 0 );
843 remap_policy::set( l->vertex,-x,-y, 0 );
845 remap_policy::set( n->vertex,-y, x, 0 );
846 remap_policy::set( p->vertex, x, y, 0 );
854 virtual Manipulatable* GetManipulatable() = 0;
855 virtual void testSelect( const View& view, const Matrix4& pivot2world ){
857 virtual void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
859 virtual void setSelected( bool select ) = 0;
860 virtual bool isSelected() const = 0;
864 inline Vector3 normalised_safe( const Vector3& self ){
865 if ( vector3_equal( self, g_vector3_identity ) ) {
866 return g_vector3_identity;
868 return vector3_normalised( self );
872 class RotateManipulator : public Manipulator
874 struct RenderableCircle : public OpenGLRenderable
876 Array<PointVertex> m_vertices;
878 RenderableCircle( std::size_t size ) : m_vertices( size ){
880 void render( RenderStateFlags state ) const {
881 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
882 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
883 glDrawArrays( GL_LINE_LOOP, 0, GLsizei( m_vertices.size() ) );
885 void setColour( const Colour4b& colour ){
886 for ( Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
888 ( *i ).colour = colour;
893 struct RenderableSemiCircle : public OpenGLRenderable
895 Array<PointVertex> m_vertices;
897 RenderableSemiCircle( std::size_t size ) : m_vertices( size ){
899 void render( RenderStateFlags state ) const {
900 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
901 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
902 glDrawArrays( GL_LINE_STRIP, 0, GLsizei( m_vertices.size() ) );
904 void setColour( const Colour4b& colour ){
905 for ( Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
907 ( *i ).colour = colour;
914 Vector3 m_axis_screen;
915 RenderableSemiCircle m_circle_x;
916 RenderableSemiCircle m_circle_y;
917 RenderableSemiCircle m_circle_z;
918 RenderableCircle m_circle_screen;
919 RenderableCircle m_circle_sphere;
920 SelectableBool m_selectable_x;
921 SelectableBool m_selectable_y;
922 SelectableBool m_selectable_z;
923 SelectableBool m_selectable_screen;
924 SelectableBool m_selectable_sphere;
926 Matrix4 m_local2world_x;
927 Matrix4 m_local2world_y;
928 Matrix4 m_local2world_z;
929 bool m_circle_x_visible;
930 bool m_circle_y_visible;
931 bool m_circle_z_visible;
933 static Shader* m_state_outer;
935 RotateManipulator( Rotatable& rotatable, std::size_t segments, float radius ) :
938 m_circle_x( ( segments << 2 ) + 1 ),
939 m_circle_y( ( segments << 2 ) + 1 ),
940 m_circle_z( ( segments << 2 ) + 1 ),
941 m_circle_screen( segments << 3 ),
942 m_circle_sphere( segments << 3 ){
943 draw_semicircle( segments, radius, m_circle_x.m_vertices.data(), RemapYZX() );
944 draw_semicircle( segments, radius, m_circle_y.m_vertices.data(), RemapZXY() );
945 draw_semicircle( segments, radius, m_circle_z.m_vertices.data(), RemapXYZ() );
947 draw_circle( segments, radius * 1.15f, m_circle_screen.m_vertices.data(), RemapXYZ() );
948 draw_circle( segments, radius, m_circle_sphere.m_vertices.data(), RemapXYZ() );
952 void UpdateColours(){
953 m_circle_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
954 m_circle_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
955 m_circle_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
956 m_circle_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
957 m_circle_sphere.setColour( colourSelected( g_colour_sphere, false ) );
960 void updateCircleTransforms(){
961 Vector3 localViewpoint( matrix4_transformed_direction( matrix4_transposed( m_pivot.m_worldSpace ), vector4_to_vector3( m_pivot.m_viewpointSpace.z() ) ) );
963 m_circle_x_visible = !vector3_equal_epsilon( g_vector3_axis_x, localViewpoint, 1e-6f );
964 if ( m_circle_x_visible ) {
965 m_local2world_x = g_matrix4_identity;
966 vector4_to_vector3( m_local2world_x.y() ) = normalised_safe(
967 vector3_cross( g_vector3_axis_x, localViewpoint )
969 vector4_to_vector3( m_local2world_x.z() ) = normalised_safe(
970 vector3_cross( vector4_to_vector3( m_local2world_x.x() ), vector4_to_vector3( m_local2world_x.y() ) )
972 matrix4_premultiply_by_matrix4( m_local2world_x, m_pivot.m_worldSpace );
975 m_circle_y_visible = !vector3_equal_epsilon( g_vector3_axis_y, localViewpoint, 1e-6f );
976 if ( m_circle_y_visible ) {
977 m_local2world_y = g_matrix4_identity;
978 vector4_to_vector3( m_local2world_y.z() ) = normalised_safe(
979 vector3_cross( g_vector3_axis_y, localViewpoint )
981 vector4_to_vector3( m_local2world_y.x() ) = normalised_safe(
982 vector3_cross( vector4_to_vector3( m_local2world_y.y() ), vector4_to_vector3( m_local2world_y.z() ) )
984 matrix4_premultiply_by_matrix4( m_local2world_y, m_pivot.m_worldSpace );
987 m_circle_z_visible = !vector3_equal_epsilon( g_vector3_axis_z, localViewpoint, 1e-6f );
988 if ( m_circle_z_visible ) {
989 m_local2world_z = g_matrix4_identity;
990 vector4_to_vector3( m_local2world_z.x() ) = normalised_safe(
991 vector3_cross( g_vector3_axis_z, localViewpoint )
993 vector4_to_vector3( m_local2world_z.y() ) = normalised_safe(
994 vector3_cross( vector4_to_vector3( m_local2world_z.z() ), vector4_to_vector3( m_local2world_z.x() ) )
996 matrix4_premultiply_by_matrix4( m_local2world_z, m_pivot.m_worldSpace );
1000 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1001 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1002 updateCircleTransforms();
1007 renderer.SetState( m_state_outer, Renderer::eWireframeOnly );
1008 renderer.SetState( m_state_outer, Renderer::eFullMaterials );
1010 renderer.addRenderable( m_circle_screen, m_pivot.m_viewpointSpace );
1011 renderer.addRenderable( m_circle_sphere, m_pivot.m_viewpointSpace );
1013 if ( m_circle_x_visible ) {
1014 renderer.addRenderable( m_circle_x, m_local2world_x );
1016 if ( m_circle_y_visible ) {
1017 renderer.addRenderable( m_circle_y, m_local2world_y );
1019 if ( m_circle_z_visible ) {
1020 renderer.addRenderable( m_circle_z, m_local2world_z );
1023 void testSelect( const View& view, const Matrix4& pivot2world ){
1024 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1025 updateCircleTransforms();
1027 SelectionPool selector;
1031 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_x ) );
1033 #if defined( DEBUG_SELECTION )
1034 g_render_clipped.construct( view.GetViewMatrix() );
1037 SelectionIntersection best;
1038 LineStrip_BestPoint( local2view, m_circle_x.m_vertices.data(), m_circle_x.m_vertices.size(), best );
1039 selector.addSelectable( best, &m_selectable_x );
1043 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_y ) );
1045 #if defined( DEBUG_SELECTION )
1046 g_render_clipped.construct( view.GetViewMatrix() );
1049 SelectionIntersection best;
1050 LineStrip_BestPoint( local2view, m_circle_y.m_vertices.data(), m_circle_y.m_vertices.size(), best );
1051 selector.addSelectable( best, &m_selectable_y );
1055 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_z ) );
1057 #if defined( DEBUG_SELECTION )
1058 g_render_clipped.construct( view.GetViewMatrix() );
1061 SelectionIntersection best;
1062 LineStrip_BestPoint( local2view, m_circle_z.m_vertices.data(), m_circle_z.m_vertices.size(), best );
1063 selector.addSelectable( best, &m_selectable_z );
1068 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1071 SelectionIntersection best;
1072 LineLoop_BestPoint( local2view, m_circle_screen.m_vertices.data(), m_circle_screen.m_vertices.size(), best );
1073 selector.addSelectable( best, &m_selectable_screen );
1077 SelectionIntersection best;
1078 Circle_BestPoint( local2view, eClipCullCW, m_circle_sphere.m_vertices.data(), m_circle_sphere.m_vertices.size(), best );
1079 selector.addSelectable( best, &m_selectable_sphere );
1083 m_axis_screen = m_pivot.m_axis_screen;
1085 if ( !selector.failed() ) {
1086 ( *selector.begin() ).second->setSelected( true );
1090 Manipulatable* GetManipulatable(){
1091 if ( m_selectable_x.isSelected() ) {
1092 m_axis.SetAxis( g_vector3_axis_x );
1095 else if ( m_selectable_y.isSelected() ) {
1096 m_axis.SetAxis( g_vector3_axis_y );
1099 else if ( m_selectable_z.isSelected() ) {
1100 m_axis.SetAxis( g_vector3_axis_z );
1103 else if ( m_selectable_screen.isSelected() ) {
1104 m_axis.SetAxis( m_axis_screen );
1112 void setSelected( bool select ){
1113 m_selectable_x.setSelected( select );
1114 m_selectable_y.setSelected( select );
1115 m_selectable_z.setSelected( select );
1116 m_selectable_screen.setSelected( select );
1118 bool isSelected() const {
1119 return m_selectable_x.isSelected()
1120 | m_selectable_y.isSelected()
1121 | m_selectable_z.isSelected()
1122 | m_selectable_screen.isSelected()
1123 | m_selectable_sphere.isSelected();
1127 Shader* RotateManipulator::m_state_outer;
1130 const float arrowhead_length = 16;
1131 const float arrowhead_radius = 4;
1133 inline void draw_arrowline( const float length, PointVertex* line, const std::size_t axis ){
1134 ( *line++ ).vertex = vertex3f_identity;
1135 ( *line ).vertex = vertex3f_identity;
1136 vertex3f_to_array( ( *line ).vertex )[axis] = length - arrowhead_length;
1139 template<typename VertexRemap, typename NormalRemap>
1140 inline void draw_arrowhead( const std::size_t segments, const float length, FlatShadedVertex* vertices, VertexRemap, NormalRemap ){
1141 std::size_t head_tris = ( segments << 3 );
1142 const double head_segment = c_2pi / head_tris;
1143 for ( std::size_t i = 0; i < head_tris; ++i )
1146 FlatShadedVertex& point = vertices[i * 6 + 0];
1147 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1148 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( i * head_segment ) );
1149 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( i * head_segment ) );
1150 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1151 NormalRemap::y( point.normal ) = static_cast<float>( cos( i * head_segment ) );
1152 NormalRemap::z( point.normal ) = static_cast<float>( sin( i * head_segment ) );
1155 FlatShadedVertex& point = vertices[i * 6 + 1];
1156 VertexRemap::x( point.vertex ) = length;
1157 VertexRemap::y( point.vertex ) = 0;
1158 VertexRemap::z( point.vertex ) = 0;
1159 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1160 NormalRemap::y( point.normal ) = static_cast<float>( cos( ( i + 0.5 ) * head_segment ) );
1161 NormalRemap::z( point.normal ) = static_cast<float>( sin( ( i + 0.5 ) * head_segment ) );
1164 FlatShadedVertex& point = vertices[i * 6 + 2];
1165 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1166 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1167 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1168 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1169 NormalRemap::y( point.normal ) = static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1170 NormalRemap::z( point.normal ) = static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1174 FlatShadedVertex& point = vertices[i * 6 + 3];
1175 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1176 VertexRemap::y( point.vertex ) = 0;
1177 VertexRemap::z( point.vertex ) = 0;
1178 NormalRemap::x( point.normal ) = -1;
1179 NormalRemap::y( point.normal ) = 0;
1180 NormalRemap::z( point.normal ) = 0;
1183 FlatShadedVertex& point = vertices[i * 6 + 4];
1184 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1185 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( i * head_segment ) );
1186 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( i * head_segment ) );
1187 NormalRemap::x( point.normal ) = -1;
1188 NormalRemap::y( point.normal ) = 0;
1189 NormalRemap::z( point.normal ) = 0;
1192 FlatShadedVertex& point = vertices[i * 6 + 5];
1193 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1194 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1195 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1196 NormalRemap::x( point.normal ) = -1;
1197 NormalRemap::y( point.normal ) = 0;
1198 NormalRemap::z( point.normal ) = 0;
1203 template<typename Triple>
1204 class TripleRemapXYZ
1207 static float& x( Triple& triple ){
1210 static float& y( Triple& triple ){
1213 static float& z( Triple& triple ){
1218 template<typename Triple>
1219 class TripleRemapYZX
1222 static float& x( Triple& triple ){
1225 static float& y( Triple& triple ){
1228 static float& z( Triple& triple ){
1233 template<typename Triple>
1234 class TripleRemapZXY
1237 static float& x( Triple& triple ){
1240 static float& y( Triple& triple ){
1243 static float& z( Triple& triple ){
1248 void vector3_print( const Vector3& v ){
1249 globalOutputStream() << "( " << v.x() << " " << v.y() << " " << v.z() << " )";
1252 class TranslateManipulator : public Manipulator
1254 struct RenderableArrowLine : public OpenGLRenderable
1256 PointVertex m_line[2];
1258 RenderableArrowLine(){
1260 void render( RenderStateFlags state ) const {
1261 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_line[0].colour );
1262 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_line[0].vertex );
1263 glDrawArrays( GL_LINES, 0, 2 );
1265 void setColour( const Colour4b& colour ){
1266 m_line[0].colour = colour;
1267 m_line[1].colour = colour;
1270 struct RenderableArrowHead : public OpenGLRenderable
1272 Array<FlatShadedVertex> m_vertices;
1274 RenderableArrowHead( std::size_t size )
1275 : m_vertices( size ){
1277 void render( RenderStateFlags state ) const {
1278 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( FlatShadedVertex ), &m_vertices.data()->colour );
1279 glVertexPointer( 3, GL_FLOAT, sizeof( FlatShadedVertex ), &m_vertices.data()->vertex );
1280 glNormalPointer( GL_FLOAT, sizeof( FlatShadedVertex ), &m_vertices.data()->normal );
1281 glDrawArrays( GL_TRIANGLES, 0, GLsizei( m_vertices.size() ) );
1283 void setColour( const Colour4b& colour ){
1284 for ( Array<FlatShadedVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
1286 ( *i ).colour = colour;
1290 struct RenderableQuad : public OpenGLRenderable
1292 PointVertex m_quad[4];
1293 void render( RenderStateFlags state ) const {
1294 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_quad[0].colour );
1295 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_quad[0].vertex );
1296 glDrawArrays( GL_LINE_LOOP, 0, 4 );
1298 void setColour( const Colour4b& colour ){
1299 m_quad[0].colour = colour;
1300 m_quad[1].colour = colour;
1301 m_quad[2].colour = colour;
1302 m_quad[3].colour = colour;
1306 TranslateFree m_free;
1307 TranslateAxis m_axis;
1308 RenderableArrowLine m_arrow_x;
1309 RenderableArrowLine m_arrow_y;
1310 RenderableArrowLine m_arrow_z;
1311 RenderableArrowHead m_arrow_head_x;
1312 RenderableArrowHead m_arrow_head_y;
1313 RenderableArrowHead m_arrow_head_z;
1314 RenderableQuad m_quad_screen;
1315 SelectableBool m_selectable_x;
1316 SelectableBool m_selectable_y;
1317 SelectableBool m_selectable_z;
1318 SelectableBool m_selectable_screen;
1319 Pivot2World m_pivot;
1321 static Shader* m_state_wire;
1322 static Shader* m_state_fill;
1324 TranslateManipulator( Translatable& translatable, std::size_t segments, float length ) :
1325 m_free( translatable ),
1326 m_axis( translatable ),
1327 m_arrow_head_x( 3 * 2 * ( segments << 3 ) ),
1328 m_arrow_head_y( 3 * 2 * ( segments << 3 ) ),
1329 m_arrow_head_z( 3 * 2 * ( segments << 3 ) ){
1330 draw_arrowline( length, m_arrow_x.m_line, 0 );
1331 draw_arrowhead( segments, length, m_arrow_head_x.m_vertices.data(), TripleRemapXYZ<Vertex3f>(), TripleRemapXYZ<Normal3f>() );
1332 draw_arrowline( length, m_arrow_y.m_line, 1 );
1333 draw_arrowhead( segments, length, m_arrow_head_y.m_vertices.data(), TripleRemapYZX<Vertex3f>(), TripleRemapYZX<Normal3f>() );
1334 draw_arrowline( length, m_arrow_z.m_line, 2 );
1335 draw_arrowhead( segments, length, m_arrow_head_z.m_vertices.data(), TripleRemapZXY<Vertex3f>(), TripleRemapZXY<Normal3f>() );
1337 draw_quad( 16, m_quad_screen.m_quad );
1340 void UpdateColours(){
1341 m_arrow_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1342 m_arrow_head_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1343 m_arrow_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1344 m_arrow_head_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1345 m_arrow_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1346 m_arrow_head_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1347 m_quad_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1350 bool manipulator_show_axis( const Pivot2World& pivot, const Vector3& axis ){
1351 return fabs( vector3_dot( pivot.m_axis_screen, axis ) ) < 0.95;
1354 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1355 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1360 Vector3 x = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.x() ) );
1361 bool show_x = manipulator_show_axis( m_pivot, x );
1363 Vector3 y = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.y() ) );
1364 bool show_y = manipulator_show_axis( m_pivot, y );
1366 Vector3 z = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.z() ) );
1367 bool show_z = manipulator_show_axis( m_pivot, z );
1369 renderer.SetState( m_state_wire, Renderer::eWireframeOnly );
1370 renderer.SetState( m_state_wire, Renderer::eFullMaterials );
1373 renderer.addRenderable( m_arrow_x, m_pivot.m_worldSpace );
1376 renderer.addRenderable( m_arrow_y, m_pivot.m_worldSpace );
1379 renderer.addRenderable( m_arrow_z, m_pivot.m_worldSpace );
1382 renderer.addRenderable( m_quad_screen, m_pivot.m_viewplaneSpace );
1384 renderer.SetState( m_state_fill, Renderer::eWireframeOnly );
1385 renderer.SetState( m_state_fill, Renderer::eFullMaterials );
1388 renderer.addRenderable( m_arrow_head_x, m_pivot.m_worldSpace );
1391 renderer.addRenderable( m_arrow_head_y, m_pivot.m_worldSpace );
1394 renderer.addRenderable( m_arrow_head_z, m_pivot.m_worldSpace );
1397 void testSelect( const View& view, const Matrix4& pivot2world ){
1398 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1400 SelectionPool selector;
1402 Vector3 x = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.x() ) );
1403 bool show_x = manipulator_show_axis( m_pivot, x );
1405 Vector3 y = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.y() ) );
1406 bool show_y = manipulator_show_axis( m_pivot, y );
1408 Vector3 z = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.z() ) );
1409 bool show_z = manipulator_show_axis( m_pivot, z );
1412 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1415 SelectionIntersection best;
1416 Quad_BestPoint( local2view, eClipCullCW, m_quad_screen.m_quad, best );
1417 if ( best.valid() ) {
1418 best = SelectionIntersection( 0, 0 );
1419 selector.addSelectable( best, &m_selectable_screen );
1425 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_worldSpace ) );
1427 #if defined( DEBUG_SELECTION )
1428 g_render_clipped.construct( view.GetViewMatrix() );
1432 SelectionIntersection best;
1433 Line_BestPoint( local2view, m_arrow_x.m_line, best );
1434 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_x.m_vertices.begin(), m_arrow_head_x.m_vertices.end(), best );
1435 selector.addSelectable( best, &m_selectable_x );
1439 SelectionIntersection best;
1440 Line_BestPoint( local2view, m_arrow_y.m_line, best );
1441 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_y.m_vertices.begin(), m_arrow_head_y.m_vertices.end(), best );
1442 selector.addSelectable( best, &m_selectable_y );
1446 SelectionIntersection best;
1447 Line_BestPoint( local2view, m_arrow_z.m_line, best );
1448 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_z.m_vertices.begin(), m_arrow_head_z.m_vertices.end(), best );
1449 selector.addSelectable( best, &m_selectable_z );
1453 if ( !selector.failed() ) {
1454 ( *selector.begin() ).second->setSelected( true );
1458 Manipulatable* GetManipulatable(){
1459 if ( m_selectable_x.isSelected() ) {
1460 m_axis.SetAxis( g_vector3_axis_x );
1463 else if ( m_selectable_y.isSelected() ) {
1464 m_axis.SetAxis( g_vector3_axis_y );
1467 else if ( m_selectable_z.isSelected() ) {
1468 m_axis.SetAxis( g_vector3_axis_z );
1477 void setSelected( bool select ){
1478 m_selectable_x.setSelected( select );
1479 m_selectable_y.setSelected( select );
1480 m_selectable_z.setSelected( select );
1481 m_selectable_screen.setSelected( select );
1483 bool isSelected() const {
1484 return m_selectable_x.isSelected()
1485 | m_selectable_y.isSelected()
1486 | m_selectable_z.isSelected()
1487 | m_selectable_screen.isSelected();
1491 Shader* TranslateManipulator::m_state_wire;
1492 Shader* TranslateManipulator::m_state_fill;
1494 class ScaleManipulator : public Manipulator
1496 struct RenderableArrow : public OpenGLRenderable
1498 PointVertex m_line[2];
1500 void render( RenderStateFlags state ) const {
1501 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_line[0].colour );
1502 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_line[0].vertex );
1503 glDrawArrays( GL_LINES, 0, 2 );
1505 void setColour( const Colour4b& colour ){
1506 m_line[0].colour = colour;
1507 m_line[1].colour = colour;
1510 struct RenderableQuad : public OpenGLRenderable
1512 PointVertex m_quad[4];
1513 void render( RenderStateFlags state ) const {
1514 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_quad[0].colour );
1515 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_quad[0].vertex );
1516 glDrawArrays( GL_QUADS, 0, 4 );
1518 void setColour( const Colour4b& colour ){
1519 m_quad[0].colour = colour;
1520 m_quad[1].colour = colour;
1521 m_quad[2].colour = colour;
1522 m_quad[3].colour = colour;
1528 RenderableArrow m_arrow_x;
1529 RenderableArrow m_arrow_y;
1530 RenderableArrow m_arrow_z;
1531 RenderableQuad m_quad_screen;
1532 SelectableBool m_selectable_x;
1533 SelectableBool m_selectable_y;
1534 SelectableBool m_selectable_z;
1535 SelectableBool m_selectable_screen;
1536 Pivot2World m_pivot;
1538 ScaleManipulator( Scalable& scalable, std::size_t segments, float length ) :
1541 draw_arrowline( length, m_arrow_x.m_line, 0 );
1542 draw_arrowline( length, m_arrow_y.m_line, 1 );
1543 draw_arrowline( length, m_arrow_z.m_line, 2 );
1545 draw_quad( 16, m_quad_screen.m_quad );
1548 Pivot2World& getPivot(){
1552 void UpdateColours(){
1553 m_arrow_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1554 m_arrow_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1555 m_arrow_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1556 m_quad_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1559 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1560 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1565 renderer.addRenderable( m_arrow_x, m_pivot.m_worldSpace );
1566 renderer.addRenderable( m_arrow_y, m_pivot.m_worldSpace );
1567 renderer.addRenderable( m_arrow_z, m_pivot.m_worldSpace );
1569 renderer.addRenderable( m_quad_screen, m_pivot.m_viewpointSpace );
1571 void testSelect( const View& view, const Matrix4& pivot2world ){
1572 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1574 SelectionPool selector;
1577 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_worldSpace ) );
1579 #if defined( DEBUG_SELECTION )
1580 g_render_clipped.construct( view.GetViewMatrix() );
1584 SelectionIntersection best;
1585 Line_BestPoint( local2view, m_arrow_x.m_line, best );
1586 selector.addSelectable( best, &m_selectable_x );
1590 SelectionIntersection best;
1591 Line_BestPoint( local2view, m_arrow_y.m_line, best );
1592 selector.addSelectable( best, &m_selectable_y );
1596 SelectionIntersection best;
1597 Line_BestPoint( local2view, m_arrow_z.m_line, best );
1598 selector.addSelectable( best, &m_selectable_z );
1603 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1606 SelectionIntersection best;
1607 Quad_BestPoint( local2view, eClipCullCW, m_quad_screen.m_quad, best );
1608 selector.addSelectable( best, &m_selectable_screen );
1612 if ( !selector.failed() ) {
1613 ( *selector.begin() ).second->setSelected( true );
1617 Manipulatable* GetManipulatable(){
1618 if ( m_selectable_x.isSelected() ) {
1619 m_axis.SetAxis( g_vector3_axis_x );
1622 else if ( m_selectable_y.isSelected() ) {
1623 m_axis.SetAxis( g_vector3_axis_y );
1626 else if ( m_selectable_z.isSelected() ) {
1627 m_axis.SetAxis( g_vector3_axis_z );
1635 void setSelected( bool select ){
1636 m_selectable_x.setSelected( select );
1637 m_selectable_y.setSelected( select );
1638 m_selectable_z.setSelected( select );
1639 m_selectable_screen.setSelected( select );
1641 bool isSelected() const {
1642 return m_selectable_x.isSelected()
1643 | m_selectable_y.isSelected()
1644 | m_selectable_z.isSelected()
1645 | m_selectable_screen.isSelected();
1650 inline PlaneSelectable* Instance_getPlaneSelectable( scene::Instance& instance ){
1651 return InstanceTypeCast<PlaneSelectable>::cast( instance );
1654 class PlaneSelectableSelectPlanes : public scene::Graph::Walker
1656 Selector& m_selector;
1657 SelectionTest& m_test;
1658 PlaneCallback m_selectedPlaneCallback;
1660 PlaneSelectableSelectPlanes( Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback )
1661 : m_selector( selector ), m_test( test ), m_selectedPlaneCallback( selectedPlaneCallback ){
1663 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1664 if ( path.top().get().visible() ) {
1665 Selectable* selectable = Instance_getSelectable( instance );
1666 if ( selectable != 0 && selectable->isSelected() ) {
1667 PlaneSelectable* planeSelectable = Instance_getPlaneSelectable( instance );
1668 if ( planeSelectable != 0 ) {
1669 planeSelectable->selectPlanes( m_selector, m_test, m_selectedPlaneCallback );
1677 class PlaneSelectableSelectReversedPlanes : public scene::Graph::Walker
1679 Selector& m_selector;
1680 const SelectedPlanes& m_selectedPlanes;
1682 PlaneSelectableSelectReversedPlanes( Selector& selector, const SelectedPlanes& selectedPlanes )
1683 : m_selector( selector ), m_selectedPlanes( selectedPlanes ){
1685 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1686 if ( path.top().get().visible() ) {
1687 Selectable* selectable = Instance_getSelectable( instance );
1688 if ( selectable != 0 && selectable->isSelected() ) {
1689 PlaneSelectable* planeSelectable = Instance_getPlaneSelectable( instance );
1690 if ( planeSelectable != 0 ) {
1691 planeSelectable->selectReversedPlanes( m_selector, m_selectedPlanes );
1699 void Scene_forEachPlaneSelectable_selectPlanes( scene::Graph& graph, Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback ){
1700 graph.traverse( PlaneSelectableSelectPlanes( selector, test, selectedPlaneCallback ) );
1703 void Scene_forEachPlaneSelectable_selectReversedPlanes( scene::Graph& graph, Selector& selector, const SelectedPlanes& selectedPlanes ){
1704 graph.traverse( PlaneSelectableSelectReversedPlanes( selector, selectedPlanes ) );
1711 bool operator()( const Plane3& plane, const Plane3& other ) const {
1712 if ( plane.a < other.a ) {
1715 if ( other.a < plane.a ) {
1719 if ( plane.b < other.b ) {
1722 if ( other.b < plane.b ) {
1726 if ( plane.c < other.c ) {
1729 if ( other.c < plane.c ) {
1733 if ( plane.d < other.d ) {
1736 if ( other.d < plane.d ) {
1744 typedef std::set<Plane3, PlaneLess> PlaneSet;
1746 inline void PlaneSet_insert( PlaneSet& self, const Plane3& plane ){
1747 self.insert( plane );
1750 inline bool PlaneSet_contains( const PlaneSet& self, const Plane3& plane ){
1751 return self.find( plane ) != self.end();
1755 class SelectedPlaneSet : public SelectedPlanes
1757 PlaneSet m_selectedPlanes;
1759 bool empty() const {
1760 return m_selectedPlanes.empty();
1763 void insert( const Plane3& plane ){
1764 PlaneSet_insert( m_selectedPlanes, plane );
1766 bool contains( const Plane3& plane ) const {
1767 return PlaneSet_contains( m_selectedPlanes, plane );
1769 typedef MemberCaller<SelectedPlaneSet, void(const Plane3&), &SelectedPlaneSet::insert> InsertCaller;
1773 bool Scene_forEachPlaneSelectable_selectPlanes( scene::Graph& graph, Selector& selector, SelectionTest& test ){
1774 SelectedPlaneSet selectedPlanes;
1776 Scene_forEachPlaneSelectable_selectPlanes( graph, selector, test, SelectedPlaneSet::InsertCaller( selectedPlanes ) );
1777 Scene_forEachPlaneSelectable_selectReversedPlanes( graph, selector, selectedPlanes );
1779 return !selectedPlanes.empty();
1782 void Scene_Translate_Component_Selected( scene::Graph& graph, const Vector3& translation );
1783 void Scene_Translate_Selected( scene::Graph& graph, const Vector3& translation );
1784 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume );
1785 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode );
1786 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode );
1787 void Scene_SelectAll_Component( bool select, SelectionSystem::EComponentMode componentMode );
1789 class ResizeTranslatable : public Translatable
1791 void translate( const Vector3& translation ){
1792 Scene_Translate_Component_Selected( GlobalSceneGraph(), translation );
1796 class DragTranslatable : public Translatable
1798 void translate( const Vector3& translation ){
1799 if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
1800 Scene_Translate_Component_Selected( GlobalSceneGraph(), translation );
1804 Scene_Translate_Selected( GlobalSceneGraph(), translation );
1809 class SelectionVolume : public SelectionTest
1811 Matrix4 m_local2view;
1817 SelectionVolume( const View& view )
1821 const VolumeTest& getVolume() const {
1825 const Vector3& getNear() const {
1828 const Vector3& getFar() const {
1832 void BeginMesh( const Matrix4& localToWorld, bool twoSided ){
1833 m_local2view = matrix4_multiplied_by_matrix4( m_view.GetViewMatrix(), localToWorld );
1835 // Cull back-facing polygons based on winding being clockwise or counter-clockwise.
1836 // Don't cull if the view is wireframe and the polygons are two-sided.
1837 m_cull = twoSided && !m_view.fill() ? eClipCullNone : ( matrix4_handedness( localToWorld ) == MATRIX4_RIGHTHANDED ) ? eClipCullCW : eClipCullCCW;
1840 Matrix4 screen2world( matrix4_full_inverse( m_local2view ) );
1842 m_near = vector4_projected(
1843 matrix4_transformed_vector4(
1845 Vector4( 0, 0, -1, 1 )
1849 m_far = vector4_projected(
1850 matrix4_transformed_vector4(
1852 Vector4( 0, 0, 1, 1 )
1857 #if defined( DEBUG_SELECTION )
1858 g_render_clipped.construct( m_view.GetViewMatrix() );
1861 void TestPoint( const Vector3& point, SelectionIntersection& best ){
1863 if ( matrix4_clip_point( m_local2view, point, clipped ) == c_CLIP_PASS ) {
1864 best = select_point_from_clipped( clipped );
1867 void TestPolygon( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1869 for ( std::size_t i = 0; i + 2 < count; ++i )
1872 matrix4_clip_triangle(
1874 reinterpret_cast<const Vector3&>( vertices[0] ),
1875 reinterpret_cast<const Vector3&>( vertices[i + 1] ),
1876 reinterpret_cast<const Vector3&>( vertices[i + 2] ),
1885 void TestLineLoop( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1890 for ( VertexPointer::iterator i = vertices.begin(), end = i + count, prev = i + ( count - 1 ); i != end; prev = i, ++i )
1895 reinterpret_cast<const Vector3&>( ( *prev ) ),
1896 reinterpret_cast<const Vector3&>( ( *i ) ),
1905 void TestLineStrip( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1910 for ( VertexPointer::iterator i = vertices.begin(), end = i + count, next = i + 1; next != end; i = next, ++next )
1915 reinterpret_cast<const Vector3&>( ( *i ) ),
1916 reinterpret_cast<const Vector3&>( ( *next ) ),
1925 void TestLines( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1930 for ( VertexPointer::iterator i = vertices.begin(), end = i + count; i != end; i += 2 )
1935 reinterpret_cast<const Vector3&>( ( *i ) ),
1936 reinterpret_cast<const Vector3&>( ( *( i + 1 ) ) ),
1945 void TestTriangles( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
1947 for ( IndexPointer::iterator i( indices.begin() ); i != indices.end(); i += 3 )
1950 matrix4_clip_triangle(
1952 reinterpret_cast<const Vector3&>( vertices[*i] ),
1953 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1954 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
1963 void TestQuads( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
1965 for ( IndexPointer::iterator i( indices.begin() ); i != indices.end(); i += 4 )
1968 matrix4_clip_triangle(
1970 reinterpret_cast<const Vector3&>( vertices[*i] ),
1971 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1972 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
1980 matrix4_clip_triangle(
1982 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
1983 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
1984 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
1993 void TestQuadStrip( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
1995 for ( IndexPointer::iterator i( indices.begin() ); i + 2 != indices.end(); i += 2 )
1998 matrix4_clip_triangle(
2000 reinterpret_cast<const Vector3&>( vertices[*i] ),
2001 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2002 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2010 matrix4_clip_triangle(
2012 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2013 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2014 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
2025 class SelectionCounter
2028 using func = void(const Selectable &);
2030 SelectionCounter( const SelectionChangeCallback& onchanged )
2031 : m_count( 0 ), m_onchanged( onchanged ){
2033 void operator()( const Selectable& selectable ){
2034 if ( selectable.isSelected() ) {
2039 ASSERT_MESSAGE( m_count != 0, "selection counter underflow" );
2043 m_onchanged( selectable );
2045 bool empty() const {
2046 return m_count == 0;
2048 std::size_t size() const {
2052 std::size_t m_count;
2053 SelectionChangeCallback m_onchanged;
2056 inline void ConstructSelectionTest( View& view, const rect_t selection_box ){
2057 view.EnableScissor( selection_box.min[0], selection_box.max[0], selection_box.min[1], selection_box.max[1] );
2060 inline const rect_t SelectionBoxForPoint( const float device_point[2], const float device_epsilon[2] ){
2061 rect_t selection_box;
2062 selection_box.min[0] = device_point[0] - device_epsilon[0];
2063 selection_box.min[1] = device_point[1] - device_epsilon[1];
2064 selection_box.max[0] = device_point[0] + device_epsilon[0];
2065 selection_box.max[1] = device_point[1] + device_epsilon[1];
2066 return selection_box;
2069 inline const rect_t SelectionBoxForArea( const float device_point[2], const float device_delta[2] ){
2070 rect_t selection_box;
2071 selection_box.min[0] = ( device_delta[0] < 0 ) ? ( device_point[0] + device_delta[0] ) : ( device_point[0] );
2072 selection_box.min[1] = ( device_delta[1] < 0 ) ? ( device_point[1] + device_delta[1] ) : ( device_point[1] );
2073 selection_box.max[0] = ( device_delta[0] > 0 ) ? ( device_point[0] + device_delta[0] ) : ( device_point[0] );
2074 selection_box.max[1] = ( device_delta[1] > 0 ) ? ( device_point[1] + device_delta[1] ) : ( device_point[1] );
2075 return selection_box;
2078 Quaternion construct_local_rotation( const Quaternion& world, const Quaternion& localToWorld ){
2079 return quaternion_normalised( quaternion_multiplied_by_quaternion(
2080 quaternion_normalised( quaternion_multiplied_by_quaternion(
2081 quaternion_inverse( localToWorld ),
2088 inline void matrix4_assign_rotation( Matrix4& matrix, const Matrix4& other ){
2089 matrix[0] = other[0];
2090 matrix[1] = other[1];
2091 matrix[2] = other[2];
2092 matrix[4] = other[4];
2093 matrix[5] = other[5];
2094 matrix[6] = other[6];
2095 matrix[8] = other[8];
2096 matrix[9] = other[9];
2097 matrix[10] = other[10];
2100 void matrix4_assign_rotation_for_pivot( Matrix4& matrix, scene::Instance& instance ){
2101 Editable* editable = Node_getEditable( instance.path().top() );
2102 if ( editable != 0 ) {
2103 matrix4_assign_rotation( matrix, matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ) );
2107 matrix4_assign_rotation( matrix, instance.localToWorld() );
2111 inline bool Instance_isSelectedComponents( scene::Instance& instance ){
2112 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
2113 return componentSelectionTestable != 0
2114 && componentSelectionTestable->isSelectedComponents();
2117 class TranslateSelected : public SelectionSystem::Visitor
2119 const Vector3& m_translate;
2121 TranslateSelected( const Vector3& translate )
2122 : m_translate( translate ){
2124 void visit( scene::Instance& instance ) const {
2125 Transformable* transform = Instance_getTransformable( instance );
2126 if ( transform != 0 ) {
2127 transform->setType( TRANSFORM_PRIMITIVE );
2128 transform->setTranslation( m_translate );
2133 void Scene_Translate_Selected( scene::Graph& graph, const Vector3& translation ){
2134 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2135 GlobalSelectionSystem().foreachSelected( TranslateSelected( translation ) );
2139 Vector3 get_local_pivot( const Vector3& world_pivot, const Matrix4& localToWorld ){
2141 matrix4_transformed_point(
2142 matrix4_full_inverse( localToWorld ),
2148 void translation_for_pivoted_matrix_transform( Vector3& parent_translation, const Matrix4& local_transform, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2149 // we need a translation inside the parent system to move the origin of this object to the right place
2151 // mathematically, it must fulfill:
2153 // local_translation local_transform local_pivot = local_pivot
2154 // local_translation = local_pivot - local_transform local_pivot
2157 // local_transform local_translation local_pivot = local_pivot
2158 // local_translation local_pivot = local_transform^-1 local_pivot
2159 // local_translation + local_pivot = local_transform^-1 local_pivot
2160 // local_translation = local_transform^-1 local_pivot - local_pivot
2162 Vector3 local_pivot( get_local_pivot( world_pivot, localToWorld ) );
2164 Vector3 local_translation(
2167 matrix4_transformed_point(
2172 matrix4_transformed_point(
2173 matrix4_full_inverse(local_transform),
2181 translation_local2object( parent_translation, local_translation, localToParent );
2185 globalOutputStream() << "World pivot is at " << world_pivot << "\n";
2186 globalOutputStream() << "Local pivot is at " << local_pivot << "\n";
2187 globalOutputStream() << "Transformation " << local_transform << " moves it to: " << matrix4_transformed_point(local_transform, local_pivot) << "\n";
2188 globalOutputStream() << "Must move by " << local_translation << " in the local system" << "\n";
2189 globalOutputStream() << "Must move by " << parent_translation << " in the parent system" << "\n";
2193 void translation_for_pivoted_rotation( Vector3& parent_translation, const Quaternion& local_rotation, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2194 translation_for_pivoted_matrix_transform( parent_translation, matrix4_rotation_for_quaternion_quantised( local_rotation ), world_pivot, localToWorld, localToParent );
2197 void translation_for_pivoted_scale( Vector3& parent_translation, const Vector3& world_scale, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2198 Matrix4 local_transform(
2199 matrix4_multiplied_by_matrix4(
2200 matrix4_full_inverse( localToWorld ),
2201 matrix4_multiplied_by_matrix4(
2202 matrix4_scale_for_vec3( world_scale ),
2207 local_transform.tx() = local_transform.ty() = local_transform.tz() = 0; // cancel translation parts
2208 translation_for_pivoted_matrix_transform( parent_translation, local_transform, world_pivot, localToWorld, localToParent );
2211 class rotate_selected : public SelectionSystem::Visitor
2213 const Quaternion& m_rotate;
2214 const Vector3& m_world_pivot;
2216 rotate_selected( const Quaternion& rotation, const Vector3& world_pivot )
2217 : m_rotate( rotation ), m_world_pivot( world_pivot ){
2219 void visit( scene::Instance& instance ) const {
2220 TransformNode* transformNode = Node_getTransformNode( instance.path().top() );
2221 if ( transformNode != 0 ) {
2222 Transformable* transform = Instance_getTransformable( instance );
2223 if ( transform != 0 ) {
2224 transform->setType( TRANSFORM_PRIMITIVE );
2225 transform->setScale( c_scale_identity );
2226 transform->setTranslation( c_translation_identity );
2228 transform->setType( TRANSFORM_PRIMITIVE );
2229 transform->setRotation( m_rotate );
2232 Editable* editable = Node_getEditable( instance.path().top() );
2233 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2235 Vector3 parent_translation;
2236 translation_for_pivoted_rotation(
2240 matrix4_multiplied_by_matrix4( instance.localToWorld(), localPivot ),
2241 matrix4_multiplied_by_matrix4( transformNode->localToParent(), localPivot )
2244 transform->setTranslation( parent_translation );
2251 void Scene_Rotate_Selected( scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot ){
2252 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2253 GlobalSelectionSystem().foreachSelected( rotate_selected( rotation, world_pivot ) );
2257 class scale_selected : public SelectionSystem::Visitor
2259 const Vector3& m_scale;
2260 const Vector3& m_world_pivot;
2262 scale_selected( const Vector3& scaling, const Vector3& world_pivot )
2263 : m_scale( scaling ), m_world_pivot( world_pivot ){
2265 void visit( scene::Instance& instance ) const {
2266 TransformNode* transformNode = Node_getTransformNode( instance.path().top() );
2267 if ( transformNode != 0 ) {
2268 Transformable* transform = Instance_getTransformable( instance );
2269 if ( transform != 0 ) {
2270 transform->setType( TRANSFORM_PRIMITIVE );
2271 transform->setScale( c_scale_identity );
2272 transform->setTranslation( c_translation_identity );
2274 transform->setType( TRANSFORM_PRIMITIVE );
2275 transform->setScale( m_scale );
2277 Editable* editable = Node_getEditable( instance.path().top() );
2278 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2280 Vector3 parent_translation;
2281 translation_for_pivoted_scale(
2285 matrix4_multiplied_by_matrix4( instance.localToWorld(), localPivot ),
2286 matrix4_multiplied_by_matrix4( transformNode->localToParent(), localPivot )
2289 transform->setTranslation( parent_translation );
2296 void Scene_Scale_Selected( scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot ){
2297 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2298 GlobalSelectionSystem().foreachSelected( scale_selected( scaling, world_pivot ) );
2303 class translate_component_selected : public SelectionSystem::Visitor
2305 const Vector3& m_translate;
2307 translate_component_selected( const Vector3& translate )
2308 : m_translate( translate ){
2310 void visit( scene::Instance& instance ) const {
2311 Transformable* transform = Instance_getTransformable( instance );
2312 if ( transform != 0 ) {
2313 transform->setType( TRANSFORM_COMPONENT );
2314 transform->setTranslation( m_translate );
2319 void Scene_Translate_Component_Selected( scene::Graph& graph, const Vector3& translation ){
2320 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2321 GlobalSelectionSystem().foreachSelectedComponent( translate_component_selected( translation ) );
2325 class rotate_component_selected : public SelectionSystem::Visitor
2327 const Quaternion& m_rotate;
2328 const Vector3& m_world_pivot;
2330 rotate_component_selected( const Quaternion& rotation, const Vector3& world_pivot )
2331 : m_rotate( rotation ), m_world_pivot( world_pivot ){
2333 void visit( scene::Instance& instance ) const {
2334 Transformable* transform = Instance_getTransformable( instance );
2335 if ( transform != 0 ) {
2336 Vector3 parent_translation;
2337 translation_for_pivoted_rotation( parent_translation, m_rotate, m_world_pivot, instance.localToWorld(), Node_getTransformNode( instance.path().top() )->localToParent() );
2339 transform->setType( TRANSFORM_COMPONENT );
2340 transform->setRotation( m_rotate );
2341 transform->setTranslation( parent_translation );
2346 void Scene_Rotate_Component_Selected( scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot ){
2347 if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
2348 GlobalSelectionSystem().foreachSelectedComponent( rotate_component_selected( rotation, world_pivot ) );
2352 class scale_component_selected : public SelectionSystem::Visitor
2354 const Vector3& m_scale;
2355 const Vector3& m_world_pivot;
2357 scale_component_selected( const Vector3& scaling, const Vector3& world_pivot )
2358 : m_scale( scaling ), m_world_pivot( world_pivot ){
2360 void visit( scene::Instance& instance ) const {
2361 Transformable* transform = Instance_getTransformable( instance );
2362 if ( transform != 0 ) {
2363 Vector3 parent_translation;
2364 translation_for_pivoted_scale( parent_translation, m_scale, m_world_pivot, instance.localToWorld(), Node_getTransformNode( instance.path().top() )->localToParent() );
2366 transform->setType( TRANSFORM_COMPONENT );
2367 transform->setScale( m_scale );
2368 transform->setTranslation( parent_translation );
2373 void Scene_Scale_Component_Selected( scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot ){
2374 if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
2375 GlobalSelectionSystem().foreachSelectedComponent( scale_component_selected( scaling, world_pivot ) );
2380 class BooleanSelector : public Selector
2383 SelectionIntersection m_intersection;
2384 Selectable* m_selectable;
2386 BooleanSelector() : m_selected( false ){
2389 void pushSelectable( Selectable& selectable ){
2390 m_intersection = SelectionIntersection();
2391 m_selectable = &selectable;
2393 void popSelectable(){
2394 if ( m_intersection.valid() ) {
2397 m_intersection = SelectionIntersection();
2399 void addIntersection( const SelectionIntersection& intersection ){
2400 if ( m_selectable->isSelected() ) {
2401 assign_if_closer( m_intersection, intersection );
2410 class BestSelector : public Selector
2412 SelectionIntersection m_intersection;
2413 Selectable* m_selectable;
2414 SelectionIntersection m_bestIntersection;
2415 std::list<Selectable*> m_bestSelectable;
2417 BestSelector() : m_bestIntersection( SelectionIntersection() ), m_bestSelectable( 0 ){
2420 void pushSelectable( Selectable& selectable ){
2421 m_intersection = SelectionIntersection();
2422 m_selectable = &selectable;
2424 void popSelectable(){
2425 if ( m_intersection.equalEpsilon( m_bestIntersection, 0.25f, 0.001f ) ) {
2426 m_bestSelectable.push_back( m_selectable );
2427 m_bestIntersection = m_intersection;
2429 else if ( m_intersection < m_bestIntersection ) {
2430 m_bestSelectable.clear();
2431 m_bestSelectable.push_back( m_selectable );
2432 m_bestIntersection = m_intersection;
2434 m_intersection = SelectionIntersection();
2436 void addIntersection( const SelectionIntersection& intersection ){
2437 assign_if_closer( m_intersection, intersection );
2440 std::list<Selectable*>& best(){
2441 return m_bestSelectable;
2445 class DragManipulator : public Manipulator
2447 TranslateFree m_freeResize;
2448 TranslateFree m_freeDrag;
2449 ResizeTranslatable m_resize;
2450 DragTranslatable m_drag;
2451 SelectableBool m_dragSelectable;
2456 DragManipulator() : m_freeResize( m_resize ), m_freeDrag( m_drag ), m_selected( false ){
2459 Manipulatable* GetManipulatable(){
2460 return m_dragSelectable.isSelected() ? &m_freeDrag : &m_freeResize;
2463 void testSelect( const View& view, const Matrix4& pivot2world ){
2464 SelectionPool selector;
2466 SelectionVolume test( view );
2468 if ( GlobalSelectionSystem().Mode() == SelectionSystem::ePrimitive ) {
2469 BooleanSelector booleanSelector;
2471 Scene_TestSelect_Primitive( booleanSelector, test, view );
2473 if ( booleanSelector.isSelected() ) {
2474 selector.addSelectable( SelectionIntersection( 0, 0 ), &m_dragSelectable );
2479 m_selected = Scene_forEachPlaneSelectable_selectPlanes( GlobalSceneGraph(), selector, test );
2484 BestSelector bestSelector;
2485 Scene_TestSelect_Component_Selected( bestSelector, test, view, GlobalSelectionSystem().ComponentMode() );
2486 for ( std::list<Selectable*>::iterator i = bestSelector.best().begin(); i != bestSelector.best().end(); ++i )
2488 if ( !( *i )->isSelected() ) {
2489 GlobalSelectionSystem().setSelectedAllComponents( false );
2492 selector.addSelectable( SelectionIntersection( 0, 0 ), ( *i ) );
2493 m_dragSelectable.setSelected( true );
2497 for ( SelectionPool::iterator i = selector.begin(); i != selector.end(); ++i )
2499 ( *i ).second->setSelected( true );
2503 void setSelected( bool select ){
2504 m_selected = select;
2505 m_dragSelectable.setSelected( select );
2507 bool isSelected() const {
2508 return m_selected || m_dragSelectable.isSelected();
2512 class ClipManipulator : public Manipulator
2516 Manipulatable* GetManipulatable(){
2517 ERROR_MESSAGE( "clipper is not manipulatable" );
2521 void setSelected( bool select ){
2523 bool isSelected() const {
2528 class select_all : public scene::Graph::Walker
2532 select_all( bool select )
2533 : m_select( select ){
2535 bool pre( const scene::Path& path, scene::Instance& instance ) const {
2536 Selectable* selectable = Instance_getSelectable( instance );
2537 if ( selectable != 0 ) {
2538 selectable->setSelected( m_select );
2544 class select_all_component : public scene::Graph::Walker
2547 SelectionSystem::EComponentMode m_mode;
2549 select_all_component( bool select, SelectionSystem::EComponentMode mode )
2550 : m_select( select ), m_mode( mode ){
2552 bool pre( const scene::Path& path, scene::Instance& instance ) const {
2553 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
2554 if ( componentSelectionTestable ) {
2555 componentSelectionTestable->setSelectedComponents( m_select, m_mode );
2561 void Scene_SelectAll_Component( bool select, SelectionSystem::EComponentMode componentMode ){
2562 GlobalSceneGraph().traverse( select_all_component( select, componentMode ) );
2566 // RadiantSelectionSystem
2567 class RadiantSelectionSystem :
2568 public SelectionSystem,
2569 public Translatable,
2574 mutable Matrix4 m_pivot2world;
2575 Matrix4 m_pivot2world_start;
2576 Matrix4 m_manip2pivot_start;
2577 Translation m_translation;
2578 Rotation m_rotation;
2581 static Shader* m_state;
2582 bool m_bPreferPointEntsIn2D;
2584 EManipulatorMode m_manipulator_mode;
2585 Manipulator* m_manipulator;
2590 EComponentMode m_componentmode;
2592 SelectionCounter m_count_primitive;
2593 SelectionCounter m_count_component;
2595 TranslateManipulator m_translate_manipulator;
2596 RotateManipulator m_rotate_manipulator;
2597 ScaleManipulator m_scale_manipulator;
2598 DragManipulator m_drag_manipulator;
2599 ClipManipulator m_clip_manipulator;
2601 typedef SelectionList<scene::Instance> selection_t;
2602 selection_t m_selection;
2603 selection_t m_component_selection;
2605 Signal1<const Selectable&> m_selectionChanged_callbacks;
2607 void ConstructPivot() const;
2608 void setCustomPivotOrigin( Vector3& point ) const;
2610 void getSelectionAABB( AABB& bounds ) const;
2612 mutable bool m_pivotChanged;
2613 bool m_pivot_moving;
2614 mutable bool m_pivotIsCustom;
2616 void Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode );
2618 bool nothingSelected() const {
2619 return ( Mode() == eComponent && m_count_component.empty() )
2620 || ( Mode() == ePrimitive && m_count_primitive.empty() );
2635 RadiantSelectionSystem() :
2636 m_bPreferPointEntsIn2D( true ),
2637 m_undo_begun( false ),
2638 m_mode( ePrimitive ),
2639 m_componentmode( eDefault ),
2640 m_count_primitive( SelectionChangedCaller( *this ) ),
2641 m_count_component( SelectionChangedCaller( *this ) ),
2642 m_translate_manipulator( *this, 2, 64 ),
2643 m_rotate_manipulator( *this, 8, 64 ),
2644 m_scale_manipulator( *this, 0, 64 ),
2645 m_pivotChanged( false ),
2646 m_pivot_moving( false ),
2647 m_pivotIsCustom( false ){
2648 SetManipulatorMode( eTranslate );
2650 addSelectionChangeCallback( PivotChangedSelectionCaller( *this ) );
2651 AddGridChangeCallback( PivotChangedCaller( *this ) );
2653 void pivotChanged() const {
2654 m_pivotChanged = true;
2655 SceneChangeNotify();
2657 typedef ConstMemberCaller<RadiantSelectionSystem, void(), &RadiantSelectionSystem::pivotChanged> PivotChangedCaller;
2658 void pivotChangedSelection( const Selectable& selectable ){
2661 typedef MemberCaller<RadiantSelectionSystem, void(const Selectable&), &RadiantSelectionSystem::pivotChangedSelection> PivotChangedSelectionCaller;
2663 void SetMode( EMode mode ){
2664 if ( m_mode != mode ) {
2669 EMode Mode() const {
2672 void SetComponentMode( EComponentMode mode ){
2673 m_componentmode = mode;
2675 EComponentMode ComponentMode() const {
2676 return m_componentmode;
2678 void SetManipulatorMode( EManipulatorMode mode ){
2679 m_pivotIsCustom = false;
2680 m_manipulator_mode = mode;
2681 switch ( m_manipulator_mode )
2683 case eTranslate: m_manipulator = &m_translate_manipulator; break;
2684 case eRotate: m_manipulator = &m_rotate_manipulator; break;
2685 case eScale: m_manipulator = &m_scale_manipulator; break;
2686 case eDrag: m_manipulator = &m_drag_manipulator; break;
2687 case eClip: m_manipulator = &m_clip_manipulator; break;
2691 EManipulatorMode ManipulatorMode() const {
2692 return m_manipulator_mode;
2695 SelectionChangeCallback getObserver( EMode mode ){
2696 if ( mode == ePrimitive ) {
2697 return makeCallback( m_count_primitive );
2701 return makeCallback( m_count_component );
2704 std::size_t countSelected() const {
2705 return m_count_primitive.size();
2707 std::size_t countSelectedComponents() const {
2708 return m_count_component.size();
2710 void onSelectedChanged( scene::Instance& instance, const Selectable& selectable ){
2711 if ( selectable.isSelected() ) {
2712 m_selection.append( instance );
2716 m_selection.erase( instance );
2719 ASSERT_MESSAGE( m_selection.size() == m_count_primitive.size(), "selection-tracking error" );
2721 void onComponentSelection( scene::Instance& instance, const Selectable& selectable ){
2722 if ( selectable.isSelected() ) {
2723 m_component_selection.append( instance );
2727 m_component_selection.erase( instance );
2730 ASSERT_MESSAGE( m_component_selection.size() == m_count_component.size(), "selection-tracking error" );
2732 scene::Instance& ultimateSelected() const {
2733 ASSERT_MESSAGE( m_selection.size() > 0, "no instance selected" );
2734 return m_selection.back();
2736 scene::Instance& penultimateSelected() const {
2737 ASSERT_MESSAGE( m_selection.size() > 1, "only one instance selected" );
2738 return *( *( --( --m_selection.end() ) ) );
2740 void setSelectedAll( bool selected ){
2741 GlobalSceneGraph().traverse( select_all( selected ) );
2743 m_manipulator->setSelected( selected );
2745 void setSelectedAllComponents( bool selected ){
2746 Scene_SelectAll_Component( selected, SelectionSystem::eVertex );
2747 Scene_SelectAll_Component( selected, SelectionSystem::eEdge );
2748 Scene_SelectAll_Component( selected, SelectionSystem::eFace );
2750 m_manipulator->setSelected( selected );
2753 void foreachSelected( const Visitor& visitor ) const {
2754 selection_t::const_iterator i = m_selection.begin();
2755 while ( i != m_selection.end() )
2757 visitor.visit( *( *( i++ ) ) );
2760 void foreachSelectedComponent( const Visitor& visitor ) const {
2761 selection_t::const_iterator i = m_component_selection.begin();
2762 while ( i != m_component_selection.end() )
2764 visitor.visit( *( *( i++ ) ) );
2768 void addSelectionChangeCallback( const SelectionChangeHandler& handler ){
2769 m_selectionChanged_callbacks.connectLast( handler );
2771 void selectionChanged( const Selectable& selectable ){
2772 m_selectionChanged_callbacks( selectable );
2774 typedef MemberCaller<RadiantSelectionSystem, void(const Selectable&), &RadiantSelectionSystem::selectionChanged> SelectionChangedCaller;
2778 m_pivot2world_start = GetPivot2World();
2781 bool SelectManipulator( const View& view, const float device_point[2], const float device_epsilon[2] ){
2782 if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
2783 #if defined ( DEBUG_SELECTION )
2784 g_render_clipped.destroy();
2787 m_manipulator->setSelected( false );
2789 if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
2790 View scissored( view );
2791 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2792 m_manipulator->testSelect( scissored, GetPivot2World() );
2797 m_pivot_moving = m_manipulator->isSelected();
2799 if ( m_pivot_moving ) {
2801 pivot.update( GetPivot2World(), view.GetModelview(), view.GetProjection(), view.GetViewport() );
2803 m_manip2pivot_start = matrix4_multiplied_by_matrix4( matrix4_full_inverse( m_pivot2world_start ), pivot.m_worldSpace );
2805 Matrix4 device2manip;
2806 ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
2807 m_manipulator->GetManipulatable()->Construct( device2manip, device_point[0], device_point[1] );
2809 m_undo_begun = false;
2812 SceneChangeNotify();
2815 return m_pivot_moving;
2819 if ( Mode() == eComponent ) {
2820 setSelectedAllComponents( false );
2824 setSelectedAll( false );
2828 void deselectComponentsOrAll( bool components ){
2830 setSelectedAllComponents( false );
2838 void SelectPoint( const View& view, const float device_point[2], const float device_epsilon[2], RadiantSelectionSystem::EModifier modifier, bool face ){
2839 //globalOutputStream() << device_point[0] << " " << device_point[1] << "\n";
2840 ASSERT_MESSAGE( fabs( device_point[0] ) <= 1.0f && fabs( device_point[1] ) <= 1.0f, "point-selection error" );
2842 if ( modifier == eReplace ) {
2843 deselectComponentsOrAll( face );
2846 //nothingSelected() doesn't consider faces, selected in non-component mode, m
2847 if ( modifier == eCycle && nothingSelected() ){
2848 modifier = eReplace;
2851 #if defined ( DEBUG_SELECTION )
2852 g_render_clipped.destroy();
2856 View scissored( view );
2857 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2859 SelectionVolume volume( scissored );
2860 SelectionPool selector;
2861 SelectionPool selector_point_ents;
2862 const bool prefer_point_ents = m_bPreferPointEntsIn2D && Mode() == ePrimitive && !view.fill() && !face
2863 && ( modifier == RadiantSelectionSystem::eReplace || modifier == RadiantSelectionSystem::eSelect || modifier == RadiantSelectionSystem::eDeselect );
2865 if( prefer_point_ents ){
2866 Scene_TestSelect( selector_point_ents, volume, scissored, eEntity, ComponentMode() );
2868 if( prefer_point_ents && !selector_point_ents.failed() ){
2871 // if cycle mode not enabled, enable it
2872 case RadiantSelectionSystem::eReplace:
2875 ( *selector_point_ents.begin() ).second->setSelected( true );
2878 case RadiantSelectionSystem::eSelect:
2880 SelectionPool::iterator best = selector_point_ents.begin();
2881 if( !( *best ).second->isSelected() ){
2882 ( *best ).second->setSelected( true );
2884 SelectionPool::iterator i = best;
2886 while ( i != selector_point_ents.end() )
2888 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
2889 if( !( *i ).second->isSelected() ){
2890 ( *i ).second->setSelected( true );
2900 case RadiantSelectionSystem::eDeselect:
2902 SelectionPool::iterator best = selector_point_ents.begin();
2903 if( ( *best ).second->isSelected() ){
2904 ( *best ).second->setSelected( false );
2906 SelectionPool::iterator i = best;
2908 while ( i != selector_point_ents.end() )
2910 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
2911 if( ( *i ).second->isSelected() ){
2912 ( *i ).second->setSelected( false );
2928 Scene_TestSelect_Component( selector, volume, scissored, eFace );
2931 Scene_TestSelect( selector, volume, scissored, Mode(), ComponentMode() );
2934 if ( !selector.failed() ) {
2937 case RadiantSelectionSystem::eToggle:
2939 SelectableSortedSet::iterator best = selector.begin();
2940 // toggle selection of the object with least depth
2941 if ( ( *best ).second->isSelected() ) {
2942 ( *best ).second->setSelected( false );
2945 ( *best ).second->setSelected( true );
2949 // if cycle mode not enabled, enable it
2950 case RadiantSelectionSystem::eReplace:
2953 ( *selector.begin() ).second->setSelected( true );
2956 // select the next object in the list from the one already selected
2957 case RadiantSelectionSystem::eCycle:
2959 bool CycleSelectionOccured = false;
2960 SelectionPool::iterator i = selector.begin();
2961 while ( i != selector.end() )
2963 if ( ( *i ).second->isSelected() ) {
2964 deselectComponentsOrAll( face );
2966 if ( i != selector.end() ) {
2967 i->second->setSelected( true );
2971 selector.begin()->second->setSelected( true );
2973 CycleSelectionOccured = true;
2978 if( !CycleSelectionOccured ){
2979 deselectComponentsOrAll( face );
2980 ( *selector.begin() ).second->setSelected( true );
2984 case RadiantSelectionSystem::eSelect:
2986 SelectionPool::iterator best = selector.begin();
2987 if( !( *best ).second->isSelected() ){
2988 ( *best ).second->setSelected( true );
2990 SelectionPool::iterator i = best;
2992 while ( i != selector.end() )
2994 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
2995 if( !( *i ).second->isSelected() ){
2996 ( *i ).second->setSelected( true );
3006 case RadiantSelectionSystem::eDeselect:
3008 SelectionPool::iterator best = selector.begin();
3009 if( ( *best ).second->isSelected() ){
3010 ( *best ).second->setSelected( false );
3012 SelectionPool::iterator i = best;
3014 while ( i != selector.end() )
3016 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3017 if( ( *i ).second->isSelected() ){
3018 ( *i ).second->setSelected( false );
3032 else if( modifier == eCycle ){
3033 deselectComponentsOrAll( face );
3039 bool SelectPoint_InitPaint( const View& view, const float device_point[2], const float device_epsilon[2], bool face ){
3040 ASSERT_MESSAGE( fabs( device_point[0] ) <= 1.0f && fabs( device_point[1] ) <= 1.0f, "point-selection error" );
3041 #if defined ( DEBUG_SELECTION )
3042 g_render_clipped.destroy();
3046 View scissored( view );
3047 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
3049 SelectionVolume volume( scissored );
3050 SelectionPool selector;
3051 SelectionPool selector_point_ents;
3052 const bool prefer_point_ents = m_bPreferPointEntsIn2D && Mode() == ePrimitive && !view.fill() && !face;
3054 if( prefer_point_ents ){
3055 Scene_TestSelect( selector_point_ents, volume, scissored, eEntity, ComponentMode() );
3057 if( prefer_point_ents && !selector_point_ents.failed() ){
3058 SelectableSortedSet::iterator best = selector_point_ents.begin();
3059 const bool wasSelected = ( *best ).second->isSelected();
3060 ( *best ).second->setSelected( !wasSelected );
3061 SelectableSortedSet::iterator i = best;
3063 while ( i != selector_point_ents.end() )
3065 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3066 ( *i ).second->setSelected( !wasSelected );
3073 return !wasSelected;
3075 else{//do primitives, if ents failed
3077 Scene_TestSelect_Component( selector, volume, scissored, eFace );
3080 Scene_TestSelect( selector, volume, scissored, Mode(), ComponentMode() );
3082 if ( !selector.failed() ){
3083 SelectableSortedSet::iterator best = selector.begin();
3084 const bool wasSelected = ( *best ).second->isSelected();
3085 ( *best ).second->setSelected( !wasSelected );
3086 SelectableSortedSet::iterator i = best;
3088 while ( i != selector.end() )
3090 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3091 ( *i ).second->setSelected( !wasSelected );
3098 return !wasSelected;
3107 void SelectArea( const View& view, const float device_point[2], const float device_delta[2], RadiantSelectionSystem::EModifier modifier, bool face ){
3108 if ( modifier == eReplace ) {
3109 deselectComponentsOrAll( face );
3112 #if defined ( DEBUG_SELECTION )
3113 g_render_clipped.destroy();
3117 View scissored( view );
3118 ConstructSelectionTest( scissored, SelectionBoxForArea( device_point, device_delta ) );
3120 SelectionVolume volume( scissored );
3123 Scene_TestSelect_Component( pool, volume, scissored, eFace );
3127 Scene_TestSelect( pool, volume, scissored, Mode(), ComponentMode() );
3130 for ( SelectionPool::iterator i = pool.begin(); i != pool.end(); ++i )
3132 ( *i ).second->setSelected( !( modifier == RadiantSelectionSystem::eToggle && ( *i ).second->isSelected() ) );
3138 void translate( const Vector3& translation ){
3139 if ( !nothingSelected() ) {
3140 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
3142 m_translation = translation;
3144 m_pivot2world = m_pivot2world_start;
3145 matrix4_translate_by_vec3( m_pivot2world, translation );
3147 if ( Mode() == eComponent ) {
3148 Scene_Translate_Component_Selected( GlobalSceneGraph(), m_translation );
3152 Scene_Translate_Selected( GlobalSceneGraph(), m_translation );
3155 SceneChangeNotify();
3158 void outputTranslation( TextOutputStream& ostream ){
3159 ostream << " -xyz " << m_translation.x() << " " << m_translation.y() << " " << m_translation.z();
3161 void rotate( const Quaternion& rotation ){
3162 if ( !nothingSelected() ) {
3163 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
3165 m_rotation = rotation;
3167 if ( Mode() == eComponent ) {
3168 Scene_Rotate_Component_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
3170 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3174 Scene_Rotate_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
3176 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3179 SceneChangeNotify();
3182 void outputRotation( TextOutputStream& ostream ){
3183 ostream << " -eulerXYZ " << m_rotation.x() << " " << m_rotation.y() << " " << m_rotation.z();
3185 void scale( const Vector3& scaling ){
3186 if ( !nothingSelected() ) {
3189 if ( Mode() == eComponent ) {
3190 Scene_Scale_Component_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
3194 Scene_Scale_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
3197 SceneChangeNotify();
3200 void outputScale( TextOutputStream& ostream ){
3201 ostream << " -scale " << m_scale.x() << " " << m_scale.y() << " " << m_scale.z();
3204 void rotateSelected( const Quaternion& rotation ){
3209 void translateSelected( const Vector3& translation ){
3211 translate( translation );
3214 void scaleSelected( const Vector3& scaling ){
3220 void MoveSelected( const View& view, const float device_point[2] ){
3221 if ( m_manipulator->isSelected() ) {
3222 if ( !m_undo_begun ) {
3223 m_undo_begun = true;
3224 GlobalUndoSystem().start();
3227 Matrix4 device2manip;
3228 ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
3229 m_manipulator->GetManipulatable()->Transform( m_manip2pivot_start, device2manip, device_point[0], device_point[1] );
3233 /// \todo Support view-dependent nudge.
3234 void NudgeManipulator( const Vector3& nudge, const Vector3& view ){
3235 if ( ManipulatorMode() == eTranslate || ManipulatorMode() == eDrag ) {
3236 translateSelected( nudge );
3241 void freezeTransforms();
3243 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const;
3244 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
3245 renderSolid( renderer, volume );
3248 const Matrix4& GetPivot2World() const {
3250 return m_pivot2world;
3253 static void constructStatic(){
3254 m_state = GlobalShaderCache().capture( "$POINT" );
3255 #if defined( DEBUG_SELECTION )
3256 g_state_clipped = GlobalShaderCache().capture( "$DEBUG_CLIPPED" );
3258 TranslateManipulator::m_state_wire = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3259 TranslateManipulator::m_state_fill = GlobalShaderCache().capture( "$FLATSHADE_OVERLAY" );
3260 RotateManipulator::m_state_outer = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3263 static void destroyStatic(){
3264 #if defined( DEBUG_SELECTION )
3265 GlobalShaderCache().release( "$DEBUG_CLIPPED" );
3267 GlobalShaderCache().release( "$WIRE_OVERLAY" );
3268 GlobalShaderCache().release( "$FLATSHADE_OVERLAY" );
3269 GlobalShaderCache().release( "$WIRE_OVERLAY" );
3270 GlobalShaderCache().release( "$POINT" );
3274 Shader* RadiantSelectionSystem::m_state = 0;
3279 RadiantSelectionSystem* g_RadiantSelectionSystem;
3281 inline RadiantSelectionSystem& getSelectionSystem(){
3282 return *g_RadiantSelectionSystem;
3288 class testselect_entity_visible : public scene::Graph::Walker
3290 Selector& m_selector;
3291 SelectionTest& m_test;
3293 testselect_entity_visible( Selector& selector, SelectionTest& test )
3294 : m_selector( selector ), m_test( test ){
3296 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3297 if( path.top().get_pointer() == Map_GetWorldspawn( g_map ) ||
3298 node_is_group( path.top().get() ) ){
3301 Selectable* selectable = Instance_getSelectable( instance );
3302 if ( selectable != 0
3303 && Node_isEntity( path.top() ) ) {
3304 m_selector.pushSelectable( *selectable );
3307 SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3308 if ( selectionTestable ) {
3309 selectionTestable->testSelect( m_selector, m_test );
3314 void post( const scene::Path& path, scene::Instance& instance ) const {
3315 Selectable* selectable = Instance_getSelectable( instance );
3316 if ( selectable != 0
3317 && Node_isEntity( path.top() ) ) {
3318 m_selector.popSelectable();
3323 class testselect_primitive_visible : public scene::Graph::Walker
3325 Selector& m_selector;
3326 SelectionTest& m_test;
3328 testselect_primitive_visible( Selector& selector, SelectionTest& test )
3329 : m_selector( selector ), m_test( test ){
3331 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3332 Selectable* selectable = Instance_getSelectable( instance );
3333 if ( selectable != 0 ) {
3334 m_selector.pushSelectable( *selectable );
3337 SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3338 if ( selectionTestable ) {
3339 selectionTestable->testSelect( m_selector, m_test );
3344 void post( const scene::Path& path, scene::Instance& instance ) const {
3345 Selectable* selectable = Instance_getSelectable( instance );
3346 if ( selectable != 0 ) {
3347 m_selector.popSelectable();
3352 class testselect_component_visible : public scene::Graph::Walker
3354 Selector& m_selector;
3355 SelectionTest& m_test;
3356 SelectionSystem::EComponentMode m_mode;
3358 testselect_component_visible( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3359 : m_selector( selector ), m_test( test ), m_mode( mode ){
3361 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3362 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3363 if ( componentSelectionTestable ) {
3364 componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3372 class testselect_component_visible_selected : public scene::Graph::Walker
3374 Selector& m_selector;
3375 SelectionTest& m_test;
3376 SelectionSystem::EComponentMode m_mode;
3378 testselect_component_visible_selected( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3379 : m_selector( selector ), m_test( test ), m_mode( mode ){
3381 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3382 Selectable* selectable = Instance_getSelectable( instance );
3383 if ( selectable != 0 && selectable->isSelected() ) {
3384 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3385 if ( componentSelectionTestable ) {
3386 componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3394 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume ){
3395 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_primitive_visible( selector, test ) );
3398 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3399 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible_selected( selector, test, componentMode ) );
3402 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3403 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible( selector, test, componentMode ) );
3406 void RadiantSelectionSystem::Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode ){
3411 Scene_forEachVisible( GlobalSceneGraph(), view, testselect_entity_visible( selector, test ) );
3415 Scene_TestSelect_Primitive( selector, test, view );
3418 Scene_TestSelect_Component_Selected( selector, test, view, componentMode );
3423 class FreezeTransforms : public scene::Graph::Walker
3426 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3427 TransformNode* transformNode = Node_getTransformNode( path.top() );
3428 if ( transformNode != 0 ) {
3429 Transformable* transform = Instance_getTransformable( instance );
3430 if ( transform != 0 ) {
3431 transform->freezeTransform();
3438 void RadiantSelectionSystem::freezeTransforms(){
3439 GlobalSceneGraph().traverse( FreezeTransforms() );
3443 void RadiantSelectionSystem::endMove(){
3446 if ( Mode() == ePrimitive ) {
3447 if ( ManipulatorMode() == eDrag ) {
3448 Scene_SelectAll_Component( false, SelectionSystem::eFace );
3452 m_pivot_moving = false;
3455 SceneChangeNotify();
3457 if ( m_undo_begun ) {
3458 StringOutputStream command;
3460 if ( ManipulatorMode() == eTranslate ) {
3461 command << "translateTool";
3462 outputTranslation( command );
3464 else if ( ManipulatorMode() == eRotate ) {
3465 command << "rotateTool";
3466 outputRotation( command );
3468 else if ( ManipulatorMode() == eScale ) {
3469 command << "scaleTool";
3470 outputScale( command );
3472 else if ( ManipulatorMode() == eDrag ) {
3473 command << "dragTool";
3476 GlobalUndoSystem().finish( command.c_str() );
3481 inline AABB Instance_getPivotBounds( scene::Instance& instance ){
3482 Entity* entity = Node_getEntity( instance.path().top() );
3484 && ( entity->getEntityClass().fixedsize
3485 || !node_is_group( instance.path().top() ) ) ) {
3486 Editable* editable = Node_getEditable( instance.path().top() );
3487 if ( editable != 0 ) {
3488 return AABB( vector4_to_vector3( matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ).t() ), Vector3( 0, 0, 0 ) );
3492 return AABB( vector4_to_vector3( instance.localToWorld().t() ), Vector3( 0, 0, 0 ) );
3496 return instance.worldAABB();
3499 class bounds_selected : public scene::Graph::Walker
3503 bounds_selected( AABB& bounds )
3504 : m_bounds( bounds ){
3507 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3508 Selectable* selectable = Instance_getSelectable( instance );
3509 if ( selectable != 0
3510 && selectable->isSelected() ) {
3511 aabb_extend_by_aabb_safe( m_bounds, Instance_getPivotBounds( instance ) );
3517 class bounds_selected_component : public scene::Graph::Walker
3521 bounds_selected_component( AABB& bounds )
3522 : m_bounds( bounds ){
3525 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3526 Selectable* selectable = Instance_getSelectable( instance );
3527 if ( selectable != 0
3528 && selectable->isSelected() ) {
3529 ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3530 if ( componentEditable ) {
3531 aabb_extend_by_aabb_safe( m_bounds, aabb_for_oriented_aabb_safe( componentEditable->getSelectedComponentsBounds(), instance.localToWorld() ) );
3538 void Scene_BoundsSelected( scene::Graph& graph, AABB& bounds ){
3539 graph.traverse( bounds_selected( bounds ) );
3542 void Scene_BoundsSelectedComponent( scene::Graph& graph, AABB& bounds ){
3543 graph.traverse( bounds_selected_component( bounds ) );
3547 inline void pivot_for_node( Matrix4& pivot, scene::Node& node, scene::Instance& instance ){
3548 ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3549 if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent
3550 && componentEditable != 0 ) {
3551 pivot = matrix4_translation_for_vec3( componentEditable->getSelectedComponentsBounds().origin );
3555 Bounded* bounded = Instance_getBounded( instance );
3556 if ( bounded != 0 ) {
3557 pivot = matrix4_translation_for_vec3( bounded->localAABB().origin );
3561 pivot = g_matrix4_identity;
3567 void RadiantSelectionSystem::ConstructPivot() const {
3568 if ( !m_pivotChanged || m_pivot_moving || m_pivotIsCustom ) {
3571 m_pivotChanged = false;
3573 Vector3 m_object_pivot;
3575 if ( !nothingSelected() ) {
3578 if ( Mode() == eComponent ) {
3579 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3583 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3585 m_object_pivot = bounds.origin;
3588 //vector3_snap( m_object_pivot, GetSnapGridSize() );
3589 //globalOutputStream() << m_object_pivot << "\n";
3590 m_pivot2world = matrix4_translation_for_vec3( m_object_pivot );
3592 switch ( m_manipulator_mode )
3597 if ( Mode() == eComponent ) {
3598 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3602 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3606 if ( Mode() == eComponent ) {
3607 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3611 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3620 void RadiantSelectionSystem::setCustomPivotOrigin( Vector3& point ) const {
3621 /*if ( !m_pivotChanged || m_pivot_moving ) {
3624 //m_pivotChanged = false;
3626 if ( !nothingSelected() && ( m_manipulator_mode == eTranslate || m_manipulator_mode == eRotate || m_manipulator_mode == eScale ) ) {
3628 if ( Mode() == eComponent ) {
3629 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3633 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3635 //globalOutputStream() << point << "\n";
3636 const float gridsize = GetSnapGridSize();
3637 //const float bbox_epsilon = gridsize / 4.0;
3639 for( std::size_t i = 0; i < 3; i++ ){
3640 if( point[i] < 900000 ){
3641 float bestsnapDist = fabs( bounds.origin[i] - point[i] );
3642 float bestsnapTo = bounds.origin[i];
3643 float othersnapDist = fabs( bounds.origin[i] + bounds.extents[i] - point[i] );
3644 if( othersnapDist < bestsnapDist ){
3645 bestsnapDist = othersnapDist;
3646 bestsnapTo = bounds.origin[i] + bounds.extents[i];
3648 othersnapDist = fabs( bounds.origin[i] - bounds.extents[i] - point[i] );
3649 if( othersnapDist < bestsnapDist ){
3650 bestsnapDist = othersnapDist;
3651 bestsnapTo = bounds.origin[i] - bounds.extents[i];
3653 othersnapDist = fabs( float_snapped( point[i], gridsize ) - point[i] );
3654 if( othersnapDist < bestsnapDist ){
3655 bestsnapDist = othersnapDist;
3656 bestsnapTo = float_snapped( point[i], gridsize );
3658 point[i] = bestsnapTo;
3660 /* if( float_equal_epsilon( point[i], bestsnapTo, bbox_epsilon ) ){
3661 point[i] = bestsnapTo;
3664 point[i] = float_snapped( point[i], gridsize );
3667 m_pivot2world[i + 12] = point[i]; //m_pivot2world.tx() .ty() .tz()
3671 switch ( m_manipulator_mode )
3676 if ( Mode() == eComponent ) {
3677 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3681 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3685 if ( Mode() == eComponent ) {
3686 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3690 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3697 m_pivotIsCustom = true;
3700 void RadiantSelectionSystem::getSelectionAABB( AABB& bounds ) const {
3701 if ( !nothingSelected() ) {
3702 if ( Mode() == eComponent ) {
3703 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3707 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3712 void GetSelectionAABB( AABB& bounds ){
3713 getSelectionSystem().getSelectionAABB( bounds );
3716 const Matrix4& ssGetPivot2World(){
3717 return getSelectionSystem().GetPivot2World();
3720 void RadiantSelectionSystem::renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
3721 //if(view->TestPoint(m_object_pivot))
3722 if ( !nothingSelected() ) {
3723 renderer.Highlight( Renderer::ePrimitive, false );
3724 renderer.Highlight( Renderer::eFace, false );
3726 renderer.SetState( m_state, Renderer::eWireframeOnly );
3727 renderer.SetState( m_state, Renderer::eFullMaterials );
3729 m_manipulator->render( renderer, volume, GetPivot2World() );
3732 #if defined( DEBUG_SELECTION )
3733 renderer.SetState( g_state_clipped, Renderer::eWireframeOnly );
3734 renderer.SetState( g_state_clipped, Renderer::eFullMaterials );
3735 renderer.addRenderable( g_render_clipped, g_render_clipped.m_world );
3739 #include "preferencesystem.h"
3740 #include "preferences.h"
3742 void SelectionSystem_constructPreferences( PreferencesPage& page ){
3743 page.appendCheckBox( "", "Prefer point entities in 2D", getSelectionSystem().m_bPreferPointEntsIn2D );
3745 void SelectionSystem_constructPage( PreferenceGroup& group ){
3746 PreferencesPage page( group.createPage( "Selection", "Selection System Settings" ) );
3747 SelectionSystem_constructPreferences( page );
3749 void SelectionSystem_registerPreferencesPage(){
3750 PreferencesDialog_addSettingsPage( FreeCaller<void(PreferenceGroup&), SelectionSystem_constructPage>() );
3755 void SelectionSystem_OnBoundsChanged(){
3756 getSelectionSystem().pivotChanged();
3759 SignalHandlerId SelectionSystem_boundsChanged;
3761 void SelectionSystem_Construct(){
3762 RadiantSelectionSystem::constructStatic();
3764 g_RadiantSelectionSystem = new RadiantSelectionSystem;
3766 SelectionSystem_boundsChanged = GlobalSceneGraph().addBoundsChangedCallback( FreeCaller<void(), SelectionSystem_OnBoundsChanged>() );
3768 GlobalShaderCache().attachRenderable( getSelectionSystem() );
3770 GlobalPreferenceSystem().registerPreference( "PreferPointEntsIn2D", make_property_string( getSelectionSystem().m_bPreferPointEntsIn2D ) );
3771 SelectionSystem_registerPreferencesPage();
3774 void SelectionSystem_Destroy(){
3775 GlobalShaderCache().detachRenderable( getSelectionSystem() );
3777 GlobalSceneGraph().removeBoundsChangedCallback( SelectionSystem_boundsChanged );
3779 delete g_RadiantSelectionSystem;
3781 RadiantSelectionSystem::destroyStatic();
3787 inline float screen_normalised( float pos, std::size_t size ){
3788 return ( ( 2.0f * pos ) / size ) - 1.0f;
3791 typedef Vector2 DeviceVector;
3793 inline DeviceVector window_to_normalised_device( WindowVector window, std::size_t width, std::size_t height ){
3794 return DeviceVector( screen_normalised( window.x(), width ), screen_normalised( height - 1 - window.y(), height ) );
3797 inline float device_constrained( float pos ){
3798 return std::min( 1.0f, std::max( -1.0f, pos ) );
3801 inline DeviceVector device_constrained( DeviceVector device ){
3802 return DeviceVector( device_constrained( device.x() ), device_constrained( device.y() ) );
3805 inline float window_constrained( float pos, std::size_t origin, std::size_t size ){
3806 return std::min( static_cast<float>( origin + size ), std::max( static_cast<float>( origin ), pos ) );
3809 inline WindowVector window_constrained( WindowVector window, std::size_t x, std::size_t y, std::size_t width, std::size_t height ){
3810 return WindowVector( window_constrained( window.x(), x, width ), window_constrained( window.y(), y, height ) );
3813 typedef Callback<void(DeviceVector)> MouseEventCallback;
3815 Single<MouseEventCallback> g_mouseMovedCallback;
3816 Single<MouseEventCallback> g_mouseUpCallback;
3819 const ButtonIdentifier c_button_select = c_buttonLeft;
3820 const ButtonIdentifier c_button_select2 = c_buttonRight;
3821 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3822 const ModifierFlags c_modifier_toggle = c_modifierShift;
3823 const ModifierFlags c_modifier_replace = c_modifierShift | c_modifierAlt;
3824 const ModifierFlags c_modifier_face = c_modifierControl;
3826 const ButtonIdentifier c_button_select = c_buttonLeft;
3827 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3828 const ModifierFlags c_modifier_toggle = c_modifierControl;
3829 const ModifierFlags c_modifier_replace = c_modifierNone;
3830 const ModifierFlags c_modifier_face = c_modifierShift;
3832 const ModifierFlags c_modifier_toggle_face = c_modifier_toggle | c_modifier_face;
3833 const ModifierFlags c_modifier_replace_face = c_modifier_replace | c_modifier_face;
3835 const ButtonIdentifier c_button_texture = c_buttonMiddle;
3836 const ModifierFlags c_modifier_apply_texture1 = c_modifierControl | c_modifierShift;
3837 const ModifierFlags c_modifier_apply_texture2 = c_modifierControl;
3838 const ModifierFlags c_modifier_apply_texture3 = c_modifierShift;
3839 const ModifierFlags c_modifier_copy_texture = c_modifierNone;
3843 RadiantSelectionSystem::EModifier modifier_for_state( ModifierFlags state ){
3844 if ( ( state == c_modifier_toggle || state == c_modifier_toggle_face || state == c_modifier_face ) ) {
3846 return RadiantSelectionSystem::eReplace;
3849 return RadiantSelectionSystem::eToggle;
3852 return RadiantSelectionSystem::eManipulator;
3855 rect_t getDeviceArea() const {
3856 DeviceVector delta( m_current - m_start );
3857 if ( selecting() && fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
3858 return SelectionBoxForArea( &m_start[0], &delta[0] );
3862 rect_t default_area = { { 0, 0, }, { 0, 0, }, };
3863 return default_area;
3868 DeviceVector m_start;
3869 DeviceVector m_current;
3870 DeviceVector m_epsilon;
3871 ModifierFlags m_state;
3874 bool m_mouseMovedWhilePressed;
3877 RectangleCallback m_window_update;
3879 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 ){
3883 m_window_update( getDeviceArea() );
3886 void testSelect( DeviceVector position ){
3887 RadiantSelectionSystem::EModifier modifier = modifier_for_state( m_state );
3888 if ( modifier != RadiantSelectionSystem::eManipulator ) {
3889 DeviceVector delta( position - m_start );
3890 if ( fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
3891 DeviceVector delta( position - m_start );
3892 //getSelectionSystem().SelectArea( *m_view, &m_start[0], &delta[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
3893 getSelectionSystem().SelectArea( *m_view, &m_start[0], &delta[0], RadiantSelectionSystem::eToggle, ( m_state & c_modifier_face ) != c_modifierNone );
3895 else if( !m_mouseMovedWhilePressed ){
3896 if ( modifier == RadiantSelectionSystem::eReplace && !m_mouseMoved ) {
3897 modifier = RadiantSelectionSystem::eCycle;
3899 getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
3903 m_start = m_current = DeviceVector( 0.0f, 0.0f );
3907 void testSelect_simpleM1( DeviceVector position ){
3908 /*RadiantSelectionSystem::EModifier modifier = RadiantSelectionSystem::eReplace;
3909 DeviceVector delta( position - m_start );
3910 if ( fabs( delta.x() ) < m_epsilon.x() && fabs( delta.y() ) < m_epsilon.y() ) {
3911 modifier = RadiantSelectionSystem::eCycle;
3913 getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], modifier, false );*/
3914 getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], m_mouseMoved ? RadiantSelectionSystem::eReplace : RadiantSelectionSystem::eCycle, false );
3915 m_start = m_current = device_constrained( position );
3919 bool selecting() const {
3920 return m_state != c_modifier_manipulator && m_mouse2;
3923 void setState( ModifierFlags state ){
3924 bool was_selecting = selecting();
3926 if ( was_selecting ^ selecting() ) {
3931 ModifierFlags getState() const {
3935 void modifierEnable( ModifierFlags type ){
3936 setState( bitfield_enable( getState(), type ) );
3938 void modifierDisable( ModifierFlags type ){
3939 setState( bitfield_disable( getState(), type ) );
3942 void mouseDown( DeviceVector position ){
3943 m_start = m_current = device_constrained( position );
3944 if( !m_mouse2 && m_state != c_modifierNone ){
3945 m_paintSelect = getSelectionSystem().SelectPoint_InitPaint( *m_view, &position[0], &m_epsilon[0], ( m_state & c_modifier_face ) != c_modifierNone );
3949 void mouseMoved( DeviceVector position ){
3950 m_current = device_constrained( position );
3951 m_mouseMovedWhilePressed = true;
3955 else if( m_state != c_modifier_manipulator ){
3956 getSelectionSystem().SelectPoint( *m_view, &m_current[0], &m_epsilon[0],
3957 m_paintSelect ? RadiantSelectionSystem::eSelect : RadiantSelectionSystem::eDeselect,
3958 ( m_state & c_modifier_face ) != c_modifierNone );
3961 typedef MemberCaller<Selector_, void(DeviceVector), &Selector_::mouseMoved> MouseMovedCaller;
3963 void mouseUp( DeviceVector position ){
3965 testSelect( device_constrained( position ) );
3968 m_start = m_current = DeviceVector( 0.0f, 0.0f );
3971 g_mouseMovedCallback.clear();
3972 g_mouseUpCallback.clear();
3974 typedef MemberCaller<Selector_, void(DeviceVector), &Selector_::mouseUp> MouseUpCaller;
3981 DeviceVector m_epsilon;
3984 bool mouseDown( DeviceVector position ){
3985 return getSelectionSystem().SelectManipulator( *m_view, &position[0], &m_epsilon[0] );
3988 void mouseMoved( DeviceVector position ){
3989 getSelectionSystem().MoveSelected( *m_view, &position[0] );
3991 typedef MemberCaller<Manipulator_, void(DeviceVector), &Manipulator_::mouseMoved> MouseMovedCaller;
3993 void mouseUp( DeviceVector position ){
3994 getSelectionSystem().endMove();
3995 g_mouseMovedCallback.clear();
3996 g_mouseUpCallback.clear();
3998 typedef MemberCaller<Manipulator_, void(DeviceVector), &Manipulator_::mouseUp> MouseUpCaller;
4001 void Scene_copyClosestTexture( SelectionTest& test );
4002 void Scene_applyClosestTexture( SelectionTest& test );
4004 class RadiantWindowObserver : public SelectionSystemWindowObserver
4017 Selector_ m_selector;
4018 Manipulator_ m_manipulator;
4020 RadiantWindowObserver() : m_mouse_down( false ){
4025 void setView( const View& view ){
4026 m_selector.m_view = &view;
4027 m_manipulator.m_view = &view;
4029 void setRectangleDrawCallback( const RectangleCallback& callback ){
4030 m_selector.m_window_update = callback;
4032 void onSizeChanged( int width, int height ){
4035 DeviceVector epsilon( SELECT_EPSILON / static_cast<float>( m_width ), SELECT_EPSILON / static_cast<float>( m_height ) );
4036 m_selector.m_epsilon = m_manipulator.m_epsilon = epsilon;
4038 void onMouseDown( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
4039 if ( button == c_button_select || ( button == c_button_select2 && modifiers != c_modifierNone ) ) {
4040 m_mouse_down = true;
4041 //m_selector.m_mouseMoved = false;
4043 DeviceVector devicePosition( window_to_normalised_device( position, m_width, m_height ) );
4044 if ( modifiers == c_modifier_manipulator && m_manipulator.mouseDown( devicePosition ) ) {
4045 g_mouseMovedCallback.insert( MouseEventCallback( Manipulator_::MouseMovedCaller( m_manipulator ) ) );
4046 g_mouseUpCallback.insert( MouseEventCallback( Manipulator_::MouseUpCaller( m_manipulator ) ) );
4050 if ( button == c_button_select ) {
4051 m_selector.m_mouse2 = false;
4054 m_selector.m_mouse2 = true;
4056 m_selector.mouseDown( devicePosition );
4057 g_mouseMovedCallback.insert( MouseEventCallback( Selector_::MouseMovedCaller( m_selector ) ) );
4058 g_mouseUpCallback.insert( MouseEventCallback( Selector_::MouseUpCaller( m_selector ) ) );
4061 else if ( button == c_button_texture ) {
4062 DeviceVector devicePosition( device_constrained( window_to_normalised_device( position, m_width, m_height ) ) );
4064 View scissored( *m_selector.m_view );
4065 ConstructSelectionTest( scissored, SelectionBoxForPoint( &devicePosition[0], &m_selector.m_epsilon[0] ) );
4066 SelectionVolume volume( scissored );
4068 if ( modifiers == c_modifier_apply_texture1 || modifiers == c_modifier_apply_texture2 || modifiers == c_modifier_apply_texture3 ) {
4069 Scene_applyClosestTexture( volume );
4071 else if ( modifiers == c_modifier_copy_texture ) {
4072 Scene_copyClosestTexture( volume );
4076 void onMouseMotion( const WindowVector& position, ModifierFlags modifiers ){
4077 m_selector.m_mouseMoved = true;
4078 if ( m_mouse_down && !g_mouseMovedCallback.empty() ) {
4079 m_selector.m_mouseMovedWhilePressed = true;
4080 g_mouseMovedCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
4083 void onMouseUp( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
4084 if ( ( button == c_button_select || button == c_button_select2 ) && !g_mouseUpCallback.empty() ) {
4085 m_mouse_down = false;
4087 g_mouseUpCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
4089 //L button w/o scene changed = tunnel selection
4090 if( // !getSelectionSystem().m_undo_begun &&
4091 modifiers == c_modifierNone && button == c_button_select &&
4092 //( !m_selector.m_mouseMoved || !m_mouse_down ) &&
4093 !m_selector.m_mouseMovedWhilePressed &&
4094 ( getSelectionSystem().Mode() != SelectionSystem::eComponent || getSelectionSystem().ManipulatorMode() != SelectionSystem::eDrag ) ){
4095 m_selector.testSelect_simpleM1( device_constrained( window_to_normalised_device( position, m_width, m_height ) ) );
4097 //getSelectionSystem().m_undo_begun = false;
4098 m_selector.m_mouseMoved = false;
4099 m_selector.m_mouseMovedWhilePressed = false;
4101 void onModifierDown( ModifierFlags type ){
4102 m_selector.modifierEnable( type );
4104 void onModifierUp( ModifierFlags type ){
4105 m_selector.modifierDisable( type );
4111 SelectionSystemWindowObserver* NewWindowObserver(){
4112 return new RadiantWindowObserver;
4117 #include "modulesystem/singletonmodule.h"
4118 #include "modulesystem/moduleregistry.h"
4120 class SelectionDependencies :
4121 public GlobalSceneGraphModuleRef,
4122 public GlobalShaderCacheModuleRef,
4123 public GlobalOpenGLModuleRef
4127 class SelectionAPI : public TypeSystemRef
4129 SelectionSystem* m_selection;
4131 typedef SelectionSystem Type;
4132 STRING_CONSTANT( Name, "*" );
4135 SelectionSystem_Construct();
4137 m_selection = &getSelectionSystem();
4140 SelectionSystem_Destroy();
4142 SelectionSystem* getTable(){
4147 typedef SingletonModule<SelectionAPI, SelectionDependencies> SelectionModule;
4148 typedef Static<SelectionModule> StaticSelectionModule;
4149 StaticRegisterModule staticRegisterSelection( StaticSelectionModule::instance() );