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, const bool snap ) = 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, const bool snap ){
228 point_on_sphere( current, device2manip, x, y );
231 Vector3 axis( 0, 0, 0 );
232 for( std::size_t i = 0; i < 3; ++i ){
233 if( current[i] == 0.0f ){
238 if( vector3_length_squared( axis ) != 0 ){
239 constrain_to_axis( current, axis );
240 m_rotatable.rotate( quaternion_for_axisangle( axis, float_snapped( angle_for_axis( m_start, current, axis ), static_cast<float>( c_pi / 12.0 ) ) ) );
245 vector3_normalise( current );
246 m_rotatable.rotate( quaternion_for_unit_vectors( m_start, current ) );
250 class RotateAxis : public Manipulatable
254 Rotatable& m_rotatable;
256 RotateAxis( Rotatable& rotatable )
257 : m_rotatable( rotatable ){
259 void Construct( const Matrix4& device2manip, const float x, const float y ){
260 point_on_sphere( m_start, device2manip, x, y );
261 constrain_to_axis( m_start, m_axis );
263 /// \brief Converts current position to a normalised vector orthogonal to axis.
264 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap ){
266 point_on_sphere( current, device2manip, x, y );
267 constrain_to_axis( current, m_axis );
270 m_rotatable.rotate( quaternion_for_axisangle( m_axis, float_snapped( angle_for_axis( m_start, current, m_axis ), static_cast<float>( c_pi / 12.0 ) ) ) );
273 m_rotatable.rotate( quaternion_for_axisangle( m_axis, angle_for_axis( m_start, current, m_axis ) ) );
277 void SetAxis( const Vector3& axis ){
282 void translation_local2object( Vector3& object, const Vector3& local, const Matrix4& local2object ){
283 object = matrix4_get_translation_vec3(
284 matrix4_multiplied_by_matrix4(
285 matrix4_translated_by_vec3( local2object, local ),
286 matrix4_full_inverse( local2object )
294 virtual ~Translatable() = default;
295 virtual void translate( const Vector3& translation ) = 0;
298 class TranslateAxis : public Manipulatable
302 Translatable& m_translatable;
304 TranslateAxis( Translatable& translatable )
305 : m_translatable( translatable ){
307 void Construct( const Matrix4& device2manip, const float x, const float y ){
308 point_on_axis( m_start, m_axis, device2manip, x, y );
310 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap ){
312 point_on_axis( current, m_axis, device2manip, x, y );
313 current = vector3_scaled( m_axis, distance_for_axis( m_start, current, m_axis ) );
315 translation_local2object( current, current, manip2object );
316 vector3_snap( current, GetSnapGridSize() );
318 m_translatable.translate( current );
321 void SetAxis( const Vector3& axis ){
326 class TranslateFree : public Manipulatable
330 Translatable& m_translatable;
332 TranslateFree( Translatable& translatable )
333 : m_translatable( translatable ){
335 void Construct( const Matrix4& device2manip, const float x, const float y ){
336 point_on_plane( m_start, device2manip, x, y );
338 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap ){
340 point_on_plane( current, device2manip, x, y );
341 current = vector3_subtracted( current, m_start );
344 for ( std::size_t i = 0; i < 3 ; ++i ){
345 if( fabs( current[i] ) >= fabs( current[(i + 1) % 3] ) ){
346 current[(i + 1) % 3] = 0.0f;
354 translation_local2object( current, current, manip2object );
355 vector3_snap( current, GetSnapGridSize() );
357 m_translatable.translate( current );
361 void GetSelectionAABB( AABB& bounds );
362 const Matrix4& SelectionSystem_GetPivot2World();
367 virtual ~Scalable() = default;
368 virtual void scale( const Vector3& scaling ) = 0;
372 class ScaleAxis : public Manipulatable
377 Scalable& m_scalable;
379 Vector3 m_choosen_extent;
382 ScaleAxis( Scalable& scalable )
383 : m_scalable( scalable ){
385 void Construct( const Matrix4& device2manip, const float x, const float y ){
386 point_on_axis( m_start, m_axis, device2manip, x, y );
389 GetSelectionAABB( aabb );
390 Vector3 transform_origin = vector4_to_vector3( SelectionSystem_GetPivot2World().t() );
391 m_choosen_extent = Vector3(
392 std::max( aabb.origin[0] + aabb.extents[0] - transform_origin[0], - aabb.origin[0] + aabb.extents[0] + transform_origin[0] ),
393 std::max( aabb.origin[1] + aabb.extents[1] - transform_origin[1], - aabb.origin[1] + aabb.extents[1] + transform_origin[1] ),
394 std::max( aabb.origin[2] + aabb.extents[2] - transform_origin[2], - aabb.origin[2] + aabb.extents[2] + transform_origin[2] )
398 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap ){
399 //globalOutputStream() << "manip2object: " << manip2object << " device2manip: " << device2manip << " x: " << x << " y:" << y <<"\n";
401 point_on_axis( current, m_axis, device2manip, x, y );
402 Vector3 delta = vector3_subtracted( current, m_start );
404 translation_local2object( delta, delta, manip2object );
405 vector3_snap( delta, GetSnapGridSize() );
407 Vector3 start( vector3_snapped( m_start, GetSnapGridSize() != 0.0f ? GetSnapGridSize() : 0.001f ) );
408 for ( std::size_t i = 0; i < 3 ; ++i ){ //prevent snapping to 0 with big gridsize
409 if( float_snapped( m_start[i], 0.001f ) != 0.0f && start[i] == 0.0f ){
410 start[i] = GetSnapGridSize();
413 //globalOutputStream() << "m_start: " << m_start << " start: " << start << " delta: " << delta <<"\n";
415 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
416 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
417 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
420 for( std::size_t i = 0; i < 3; i++ ){
421 if( m_choosen_extent[i] > 0.0625f ){ //epsilon to prevent super high scale for set of models, having really small extent, formed by origins
422 scale[i] = ( m_choosen_extent[i] + delta[i] ) / m_choosen_extent[i];
426 for( std::size_t i = 0; i < 3; i++ ){
427 if( scale[i] == 1.0f ){
428 scale[i] = vector3_dot( scale, m_axis );
432 //globalOutputStream() << "scale: " << scale <<"\n";
433 m_scalable.scale( scale );
436 void SetAxis( const Vector3& axis ){
441 class ScaleFree : public Manipulatable
445 Scalable& m_scalable;
447 Vector3 m_choosen_extent;
450 ScaleFree( Scalable& scalable )
451 : m_scalable( scalable ){
453 void Construct( const Matrix4& device2manip, const float x, const float y ){
454 point_on_plane( m_start, device2manip, x, y );
457 GetSelectionAABB( aabb );
458 Vector3 transform_origin = vector4_to_vector3( SelectionSystem_GetPivot2World().t() );
459 m_choosen_extent = Vector3(
460 std::max( aabb.origin[0] + aabb.extents[0] - transform_origin[0], - aabb.origin[0] + aabb.extents[0] + transform_origin[0] ),
461 std::max( aabb.origin[1] + aabb.extents[1] - transform_origin[1], - aabb.origin[1] + aabb.extents[1] + transform_origin[1] ),
462 std::max( aabb.origin[2] + aabb.extents[2] - transform_origin[2], - aabb.origin[2] + aabb.extents[2] + transform_origin[2] )
465 void Transform( const Matrix4& manip2object, const Matrix4& device2manip, const float x, const float y, const bool snap ){
467 point_on_plane( current, device2manip, x, y );
468 Vector3 delta = vector3_subtracted( current, m_start );
470 translation_local2object( delta, delta, manip2object );
471 vector3_snap( delta, GetSnapGridSize() );
473 Vector3 start( vector3_snapped( m_start, GetSnapGridSize() != 0.0f ? GetSnapGridSize() : 0.001f ) );
474 for ( std::size_t i = 0; i < 3 ; ++i ){ //prevent snapping to 0 with big gridsize
475 if( float_snapped( m_start[i], 0.001f ) != 0.0f && start[i] == 0.0f ){
476 start[i] = GetSnapGridSize();
480 start[0] == 0 ? 1 : 1 + delta[0] / start[0],
481 start[1] == 0 ? 1 : 1 + delta[1] / start[1],
482 start[2] == 0 ? 1 : 1 + delta[2] / start[2]
485 //globalOutputStream() << "m_start: " << m_start << " start: " << start << " delta: " << delta <<"\n";
486 for( std::size_t i = 0; i < 3; i++ ){
487 if( m_choosen_extent[i] > 0.0625f ){
488 scale[i] = ( m_choosen_extent[i] + delta[i] ) / m_choosen_extent[i];
491 //globalOutputStream() << "pre snap scale: " << scale <<"\n";
493 float bestscale = scale[0];
494 for( std::size_t i = 1; i < 3; i++ ){
495 //if( fabs( 1.0f - fabs( scale[i] ) ) > fabs( 1.0f - fabs( bestscale ) ) ){
496 if( fabs( scale[i] ) > fabs( bestscale ) && scale[i] != 1.0f ){ //harder to scale down with this, but glitchier with upper one
497 bestscale = scale[i];
499 //globalOutputStream() << "bestscale: " << bestscale <<"\n";
501 for( std::size_t i = 0; i < 3; i++ ){
502 if( start[i] != 0.0f ){ // !!!!check grid == 0 case
503 scale[i] = ( scale[i] < 0.0f ) ? -fabs( bestscale ) : fabs( bestscale );
507 //globalOutputStream() << "scale: " << scale <<"\n";
508 m_scalable.scale( scale );
521 class RenderableClippedPrimitive : public OpenGLRenderable
525 PointVertex m_points[9];
529 std::vector<primitive_t> m_primitives;
533 void render( RenderStateFlags state ) const {
534 for ( std::size_t i = 0; i < m_primitives.size(); ++i )
536 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_primitives[i].m_points[0].colour );
537 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_primitives[i].m_points[0].vertex );
538 switch ( m_primitives[i].m_count )
541 case 2: glDrawArrays( GL_LINES, 0, GLsizei( m_primitives[i].m_count ) ); break;
542 default: glDrawArrays( GL_POLYGON, 0, GLsizei( m_primitives[i].m_count ) ); break;
547 void construct( const Matrix4& world2device ){
548 m_inverse = matrix4_full_inverse( world2device );
549 m_world = g_matrix4_identity;
552 void insert( const Vector4 clipped[9], std::size_t count ){
555 m_primitives.back().m_count = count;
556 for ( std::size_t i = 0; i < count; ++i )
558 Vector3 world_point( vector4_projected( matrix4_transformed_vector4( m_inverse, clipped[i] ) ) );
559 m_primitives.back().m_points[i].vertex = vertex3f_for_vector3( world_point );
564 m_primitives.clear();
568 m_primitives.push_back( primitive_t() );
570 const Colour4b colour_clipped( 255, 127, 0, 255 );
572 for ( std::size_t i = 0; i < 9; ++i )
573 m_primitives.back().m_points[i].colour = colour_clipped;
578 #define DEBUG_SELECTION
581 #if defined( DEBUG_SELECTION )
582 Shader* g_state_clipped;
583 RenderableClippedPrimitive g_render_clipped;
588 // dist_Point_to_Line(): get the distance of a point to a line.
589 // Input: a Point P and a Line L (in any dimension)
590 // Return: the shortest distance from P to L
592 dist_Point_to_Line( Point P, Line L ){
593 Vector v = L.P1 - L.P0;
596 double c1 = dot( w,v );
597 double c2 = dot( v,v );
600 Point Pb = L.P0 + b * v;
607 typedef Vector3 point_type;
609 Segment3D( const point_type& _p0, const point_type& _p1 )
610 : p0( _p0 ), p1( _p1 ){
616 typedef Vector3 Point3D;
618 inline double vector3_distance_squared( const Point3D& a, const Point3D& b ){
619 return vector3_length_squared( b - a );
622 // get the distance of a point to a segment.
623 Point3D segment_closest_point_to_point( const Segment3D& segment, const Point3D& point ){
624 Vector3 v = segment.p1 - segment.p0;
625 Vector3 w = point - segment.p0;
627 double c1 = vector3_dot( w,v );
632 double c2 = vector3_dot( v,v );
637 return Point3D( segment.p0 + v * ( c1 / c2 ) );
640 double segment_dist_to_point_3d( const Segment3D& segment, const Point3D& point ){
641 return vector3_distance_squared( point, segment_closest_point_to_point( segment, point ) );
644 typedef Vector3 point_t;
645 typedef const Vector3* point_iterator_t;
647 // crossing number test for a point in a polygon
648 // This code is patterned after [Franklin, 2000]
649 bool point_test_polygon_2d( const point_t& P, point_iterator_t start, point_iterator_t finish ){
650 std::size_t crossings = 0;
652 // loop through all edges of the polygon
653 for ( point_iterator_t prev = finish - 1, cur = start; cur != finish; prev = cur, ++cur )
654 { // edge from (*prev) to (*cur)
655 if ( ( ( ( *prev )[1] <= P[1] ) && ( ( *cur )[1] > P[1] ) ) // an upward crossing
656 || ( ( ( *prev )[1] > P[1] ) && ( ( *cur )[1] <= P[1] ) ) ) { // a downward crossing
657 // compute the actual edge-ray intersect x-coordinate
658 float vt = (float)( P[1] - ( *prev )[1] ) / ( ( *cur )[1] - ( *prev )[1] );
659 if ( P[0] < ( *prev )[0] + vt * ( ( *cur )[0] - ( *prev )[0] ) ) { // P[0] < intersect
660 ++crossings; // a valid crossing of y=P[1] right of P[0]
664 return ( crossings & 0x1 ) != 0; // 0 if even (out), and 1 if odd (in)
667 inline double triangle_signed_area_XY( const Vector3& p0, const Vector3& p1, const Vector3& p2 ){
668 return ( ( p1[0] - p0[0] ) * ( p2[1] - p0[1] ) ) - ( ( p2[0] - p0[0] ) * ( p1[1] - p0[1] ) );
679 inline SelectionIntersection select_point_from_clipped( Vector4& clipped ){
680 return SelectionIntersection( clipped[2] / clipped[3], static_cast<float>( vector3_length_squared( Vector3( clipped[0] / clipped[3], clipped[1] / clipped[3], 0 ) ) ) );
683 void BestPoint( std::size_t count, Vector4 clipped[9], SelectionIntersection& best, clipcull_t cull ){
684 Vector3 normalised[9];
687 for ( std::size_t i = 0; i < count; ++i )
689 normalised[i][0] = clipped[i][0] / clipped[i][3];
690 normalised[i][1] = clipped[i][1] / clipped[i][3];
691 normalised[i][2] = clipped[i][2] / clipped[i][3];
695 if ( cull != eClipCullNone && count > 2 ) {
696 double signed_area = triangle_signed_area_XY( normalised[0], normalised[1], normalised[2] );
698 if ( ( cull == eClipCullCW && signed_area > 0 )
699 || ( cull == eClipCullCCW && signed_area < 0 ) ) {
705 Segment3D segment( normalised[0], normalised[1] );
706 Point3D point = segment_closest_point_to_point( segment, Vector3( 0, 0, 0 ) );
707 assign_if_closer( best, SelectionIntersection( point.z(), 0 ) );
709 else if ( count > 2 && !point_test_polygon_2d( Vector3( 0, 0, 0 ), normalised, normalised + count ) ) {
710 point_iterator_t end = normalised + count;
711 for ( point_iterator_t previous = end - 1, current = normalised; current != end; previous = current, ++current )
713 Segment3D segment( *previous, *current );
714 Point3D point = segment_closest_point_to_point( segment, Vector3( 0, 0, 0 ) );
715 float depth = point.z();
717 float distance = static_cast<float>( vector3_length_squared( point ) );
719 assign_if_closer( best, SelectionIntersection( depth, distance ) );
722 else if ( count > 2 ) {
725 SelectionIntersection(
726 static_cast<float>( ray_distance_to_plane(
727 Ray( Vector3( 0, 0, 0 ), Vector3( 0, 0, 1 ) ),
728 plane3_for_points( normalised[0], normalised[1], normalised[2] )
735 #if defined( DEBUG_SELECTION )
737 g_render_clipped.insert( clipped, count );
742 void LineStrip_BestPoint( const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
744 for ( std::size_t i = 0; ( i + 1 ) < size; ++i )
746 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[i + 1].vertex ), clipped );
747 BestPoint( count, clipped, best, eClipCullNone );
751 void LineLoop_BestPoint( const Matrix4& local2view, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
753 for ( std::size_t i = 0; i < size; ++i )
755 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[i].vertex ), vertex3f_to_vector3( vertices[( i + 1 ) % size].vertex ), clipped );
756 BestPoint( count, clipped, best, eClipCullNone );
760 void Line_BestPoint( const Matrix4& local2view, const PointVertex vertices[2], SelectionIntersection& best ){
762 const std::size_t count = matrix4_clip_line( local2view, vertex3f_to_vector3( vertices[0].vertex ), vertex3f_to_vector3( vertices[1].vertex ), clipped );
763 BestPoint( count, clipped, best, eClipCullNone );
766 void Circle_BestPoint( const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, const std::size_t size, SelectionIntersection& best ){
768 for ( std::size_t i = 0; i < size; ++i )
770 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 );
771 BestPoint( count, clipped, best, cull );
775 void Quad_BestPoint( const Matrix4& local2view, clipcull_t cull, const PointVertex* vertices, SelectionIntersection& best ){
778 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 );
779 BestPoint( count, clipped, best, cull );
782 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 );
783 BestPoint( count, clipped, best, cull );
787 struct FlatShadedVertex
798 typedef FlatShadedVertex* FlatShadedVertexIterator;
799 void Triangles_BestPoint( const Matrix4& local2view, clipcull_t cull, FlatShadedVertexIterator first, FlatShadedVertexIterator last, SelectionIntersection& best ){
800 for ( FlatShadedVertexIterator x( first ), y( first + 1 ), z( first + 2 ); x != last; x += 3, y += 3, z += 3 )
804 matrix4_clip_triangle(
806 reinterpret_cast<const Vector3&>( ( *x ).vertex ),
807 reinterpret_cast<const Vector3&>( ( *y ).vertex ),
808 reinterpret_cast<const Vector3&>( ( *z ).vertex ),
819 typedef std::multimap<SelectionIntersection, Selectable*> SelectableSortedSet;
821 class SelectionPool : public Selector
823 SelectableSortedSet m_pool;
824 SelectionIntersection m_intersection;
825 Selectable* m_selectable;
828 void pushSelectable( Selectable& selectable ){
829 m_intersection = SelectionIntersection();
830 m_selectable = &selectable;
832 void popSelectable(){
833 addSelectable( m_intersection, m_selectable );
834 m_intersection = SelectionIntersection();
836 void addIntersection( const SelectionIntersection& intersection ){
837 assign_if_closer( m_intersection, intersection );
839 void addSelectable( const SelectionIntersection& intersection, Selectable* selectable ){
840 if ( intersection.valid() ) {
841 m_pool.insert( SelectableSortedSet::value_type( intersection, selectable ) );
845 typedef SelectableSortedSet::iterator iterator;
848 return m_pool.begin();
855 return m_pool.empty();
860 const Colour4b g_colour_sphere( 0, 0, 0, 255 );
861 const Colour4b g_colour_screen( 0, 255, 255, 255 );
862 const Colour4b g_colour_selected( 255, 255, 0, 255 );
864 inline const Colour4b& colourSelected( const Colour4b& colour, bool selected ){
865 return ( selected ) ? g_colour_selected : colour;
868 template<typename remap_policy>
869 inline void draw_semicircle( const std::size_t segments, const float radius, PointVertex* vertices, remap_policy remap ){
870 const double increment = c_pi / double(segments << 2);
872 std::size_t count = 0;
875 remap_policy::set( vertices[segments << 2].vertex, -radius, 0, 0 );
876 while ( count < segments )
878 PointVertex* i = vertices + count;
879 PointVertex* j = vertices + ( ( segments << 1 ) - ( count + 1 ) );
881 PointVertex* k = i + ( segments << 1 );
882 PointVertex* l = j + ( segments << 1 );
885 PointVertex* m = i + ( segments << 2 );
886 PointVertex* n = j + ( segments << 2 );
887 PointVertex* o = k + ( segments << 2 );
888 PointVertex* p = l + ( segments << 2 );
891 remap_policy::set( i->vertex, x,-y, 0 );
892 remap_policy::set( k->vertex,-y,-x, 0 );
894 remap_policy::set( m->vertex,-x, y, 0 );
895 remap_policy::set( o->vertex, y, x, 0 );
901 const double theta = increment * count;
902 x = static_cast<float>( radius * cos( theta ) );
903 y = static_cast<float>( radius * sin( theta ) );
906 remap_policy::set( j->vertex, y,-x, 0 );
907 remap_policy::set( l->vertex,-x,-y, 0 );
909 remap_policy::set( n->vertex,-y, x, 0 );
910 remap_policy::set( p->vertex, x, y, 0 );
918 virtual Manipulatable* GetManipulatable() = 0;
919 virtual void testSelect( const View& view, const Matrix4& pivot2world ){
921 virtual void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
923 virtual void setSelected( bool select ) = 0;
924 virtual bool isSelected() const = 0;
928 inline Vector3 normalised_safe( const Vector3& self ){
929 if ( vector3_equal( self, g_vector3_identity ) ) {
930 return g_vector3_identity;
932 return vector3_normalised( self );
936 class RotateManipulator : public Manipulator
938 struct RenderableCircle : public OpenGLRenderable
940 Array<PointVertex> m_vertices;
942 RenderableCircle( std::size_t size ) : m_vertices( size ){
944 void render( RenderStateFlags state ) const {
945 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
946 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
947 glDrawArrays( GL_LINE_LOOP, 0, GLsizei( m_vertices.size() ) );
949 void setColour( const Colour4b& colour ){
950 for ( Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
952 ( *i ).colour = colour;
957 struct RenderableSemiCircle : public OpenGLRenderable
959 Array<PointVertex> m_vertices;
961 RenderableSemiCircle( std::size_t size ) : m_vertices( size ){
963 void render( RenderStateFlags state ) const {
964 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_vertices.data()->colour );
965 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_vertices.data()->vertex );
966 glDrawArrays( GL_LINE_STRIP, 0, GLsizei( m_vertices.size() ) );
968 void setColour( const Colour4b& colour ){
969 for ( Array<PointVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
971 ( *i ).colour = colour;
978 Vector3 m_axis_screen;
979 RenderableSemiCircle m_circle_x;
980 RenderableSemiCircle m_circle_y;
981 RenderableSemiCircle m_circle_z;
982 RenderableCircle m_circle_screen;
983 RenderableCircle m_circle_sphere;
984 SelectableBool m_selectable_x;
985 SelectableBool m_selectable_y;
986 SelectableBool m_selectable_z;
987 SelectableBool m_selectable_screen;
988 SelectableBool m_selectable_sphere;
990 Matrix4 m_local2world_x;
991 Matrix4 m_local2world_y;
992 Matrix4 m_local2world_z;
993 bool m_circle_x_visible;
994 bool m_circle_y_visible;
995 bool m_circle_z_visible;
997 static Shader* m_state_outer;
999 RotateManipulator( Rotatable& rotatable, std::size_t segments, float radius ) :
1000 m_free( rotatable ),
1001 m_axis( rotatable ),
1002 m_circle_x( ( segments << 2 ) + 1 ),
1003 m_circle_y( ( segments << 2 ) + 1 ),
1004 m_circle_z( ( segments << 2 ) + 1 ),
1005 m_circle_screen( segments << 3 ),
1006 m_circle_sphere( segments << 3 ){
1007 draw_semicircle( segments, radius, m_circle_x.m_vertices.data(), RemapYZX() );
1008 draw_semicircle( segments, radius, m_circle_y.m_vertices.data(), RemapZXY() );
1009 draw_semicircle( segments, radius, m_circle_z.m_vertices.data(), RemapXYZ() );
1011 draw_circle( segments, radius * 1.15f, m_circle_screen.m_vertices.data(), RemapXYZ() );
1012 draw_circle( segments, radius, m_circle_sphere.m_vertices.data(), RemapXYZ() );
1014 m_selectable_sphere.setSelected( true );
1018 void UpdateColours(){
1019 m_circle_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1020 m_circle_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1021 m_circle_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1022 m_circle_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1023 m_circle_sphere.setColour( colourSelected( g_colour_sphere, false ) );
1026 void updateCircleTransforms(){
1027 Vector3 localViewpoint( matrix4_transformed_direction( matrix4_transposed( m_pivot.m_worldSpace ), vector4_to_vector3( m_pivot.m_viewpointSpace.z() ) ) );
1029 m_circle_x_visible = !vector3_equal_epsilon( g_vector3_axis_x, localViewpoint, 1e-6f );
1030 if ( m_circle_x_visible ) {
1031 m_local2world_x = g_matrix4_identity;
1032 vector4_to_vector3( m_local2world_x.y() ) = normalised_safe(
1033 vector3_cross( g_vector3_axis_x, localViewpoint )
1035 vector4_to_vector3( m_local2world_x.z() ) = normalised_safe(
1036 vector3_cross( vector4_to_vector3( m_local2world_x.x() ), vector4_to_vector3( m_local2world_x.y() ) )
1038 matrix4_premultiply_by_matrix4( m_local2world_x, m_pivot.m_worldSpace );
1041 m_circle_y_visible = !vector3_equal_epsilon( g_vector3_axis_y, localViewpoint, 1e-6f );
1042 if ( m_circle_y_visible ) {
1043 m_local2world_y = g_matrix4_identity;
1044 vector4_to_vector3( m_local2world_y.z() ) = normalised_safe(
1045 vector3_cross( g_vector3_axis_y, localViewpoint )
1047 vector4_to_vector3( m_local2world_y.x() ) = normalised_safe(
1048 vector3_cross( vector4_to_vector3( m_local2world_y.y() ), vector4_to_vector3( m_local2world_y.z() ) )
1050 matrix4_premultiply_by_matrix4( m_local2world_y, m_pivot.m_worldSpace );
1053 m_circle_z_visible = !vector3_equal_epsilon( g_vector3_axis_z, localViewpoint, 1e-6f );
1054 if ( m_circle_z_visible ) {
1055 m_local2world_z = g_matrix4_identity;
1056 vector4_to_vector3( m_local2world_z.x() ) = normalised_safe(
1057 vector3_cross( g_vector3_axis_z, localViewpoint )
1059 vector4_to_vector3( m_local2world_z.y() ) = normalised_safe(
1060 vector3_cross( vector4_to_vector3( m_local2world_z.z() ), vector4_to_vector3( m_local2world_z.x() ) )
1062 matrix4_premultiply_by_matrix4( m_local2world_z, m_pivot.m_worldSpace );
1066 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1067 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1068 updateCircleTransforms();
1073 renderer.SetState( m_state_outer, Renderer::eWireframeOnly );
1074 renderer.SetState( m_state_outer, Renderer::eFullMaterials );
1076 renderer.addRenderable( m_circle_screen, m_pivot.m_viewpointSpace );
1077 renderer.addRenderable( m_circle_sphere, m_pivot.m_viewpointSpace );
1079 if ( m_circle_x_visible ) {
1080 renderer.addRenderable( m_circle_x, m_local2world_x );
1082 if ( m_circle_y_visible ) {
1083 renderer.addRenderable( m_circle_y, m_local2world_y );
1085 if ( m_circle_z_visible ) {
1086 renderer.addRenderable( m_circle_z, m_local2world_z );
1089 void testSelect( const View& view, const Matrix4& pivot2world ){
1090 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1091 updateCircleTransforms();
1093 SelectionPool selector;
1097 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_x ) );
1099 #if defined( DEBUG_SELECTION )
1100 g_render_clipped.construct( view.GetViewMatrix() );
1103 SelectionIntersection best;
1104 LineStrip_BestPoint( local2view, m_circle_x.m_vertices.data(), m_circle_x.m_vertices.size(), best );
1105 selector.addSelectable( best, &m_selectable_x );
1109 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_y ) );
1111 #if defined( DEBUG_SELECTION )
1112 g_render_clipped.construct( view.GetViewMatrix() );
1115 SelectionIntersection best;
1116 LineStrip_BestPoint( local2view, m_circle_y.m_vertices.data(), m_circle_y.m_vertices.size(), best );
1117 selector.addSelectable( best, &m_selectable_y );
1121 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_local2world_z ) );
1123 #if defined( DEBUG_SELECTION )
1124 g_render_clipped.construct( view.GetViewMatrix() );
1127 SelectionIntersection best;
1128 LineStrip_BestPoint( local2view, m_circle_z.m_vertices.data(), m_circle_z.m_vertices.size(), best );
1129 selector.addSelectable( best, &m_selectable_z );
1134 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1137 SelectionIntersection best;
1138 LineLoop_BestPoint( local2view, m_circle_screen.m_vertices.data(), m_circle_screen.m_vertices.size(), best );
1139 selector.addSelectable( best, &m_selectable_screen );
1143 SelectionIntersection best;
1144 Circle_BestPoint( local2view, eClipCullCW, m_circle_sphere.m_vertices.data(), m_circle_sphere.m_vertices.size(), best );
1145 selector.addSelectable( best, &m_selectable_sphere );
1149 m_axis_screen = m_pivot.m_axis_screen;
1151 if ( !selector.failed() ) {
1152 ( *selector.begin() ).second->setSelected( true );
1156 Manipulatable* GetManipulatable(){
1157 if ( m_selectable_x.isSelected() ) {
1158 m_axis.SetAxis( g_vector3_axis_x );
1161 else if ( m_selectable_y.isSelected() ) {
1162 m_axis.SetAxis( g_vector3_axis_y );
1165 else if ( m_selectable_z.isSelected() ) {
1166 m_axis.SetAxis( g_vector3_axis_z );
1169 else if ( m_selectable_screen.isSelected() ) {
1170 m_axis.SetAxis( m_axis_screen );
1178 void setSelected( bool select ){
1179 m_selectable_x.setSelected( select );
1180 m_selectable_y.setSelected( select );
1181 m_selectable_z.setSelected( select );
1182 m_selectable_screen.setSelected( select );
1184 bool isSelected() const {
1185 return m_selectable_x.isSelected()
1186 | m_selectable_y.isSelected()
1187 | m_selectable_z.isSelected()
1188 | m_selectable_screen.isSelected()
1189 | m_selectable_sphere.isSelected();
1193 Shader* RotateManipulator::m_state_outer;
1196 const float arrowhead_length = 16;
1197 const float arrowhead_radius = 4;
1199 inline void draw_arrowline( const float length, PointVertex* line, const std::size_t axis ){
1200 ( *line++ ).vertex = vertex3f_identity;
1201 ( *line ).vertex = vertex3f_identity;
1202 vertex3f_to_array( ( *line ).vertex )[axis] = length - arrowhead_length;
1205 template<typename VertexRemap, typename NormalRemap>
1206 inline void draw_arrowhead( const std::size_t segments, const float length, FlatShadedVertex* vertices, VertexRemap, NormalRemap ){
1207 std::size_t head_tris = ( segments << 3 );
1208 const double head_segment = c_2pi / head_tris;
1209 for ( std::size_t i = 0; i < head_tris; ++i )
1212 FlatShadedVertex& point = vertices[i * 6 + 0];
1213 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1214 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( i * head_segment ) );
1215 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( i * head_segment ) );
1216 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1217 NormalRemap::y( point.normal ) = static_cast<float>( cos( i * head_segment ) );
1218 NormalRemap::z( point.normal ) = static_cast<float>( sin( i * head_segment ) );
1221 FlatShadedVertex& point = vertices[i * 6 + 1];
1222 VertexRemap::x( point.vertex ) = length;
1223 VertexRemap::y( point.vertex ) = 0;
1224 VertexRemap::z( point.vertex ) = 0;
1225 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1226 NormalRemap::y( point.normal ) = static_cast<float>( cos( ( i + 0.5 ) * head_segment ) );
1227 NormalRemap::z( point.normal ) = static_cast<float>( sin( ( i + 0.5 ) * head_segment ) );
1230 FlatShadedVertex& point = vertices[i * 6 + 2];
1231 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1232 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1233 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1234 NormalRemap::x( point.normal ) = arrowhead_radius / arrowhead_length;
1235 NormalRemap::y( point.normal ) = static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1236 NormalRemap::z( point.normal ) = static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1240 FlatShadedVertex& point = vertices[i * 6 + 3];
1241 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1242 VertexRemap::y( point.vertex ) = 0;
1243 VertexRemap::z( point.vertex ) = 0;
1244 NormalRemap::x( point.normal ) = -1;
1245 NormalRemap::y( point.normal ) = 0;
1246 NormalRemap::z( point.normal ) = 0;
1249 FlatShadedVertex& point = vertices[i * 6 + 4];
1250 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1251 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( i * head_segment ) );
1252 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( i * head_segment ) );
1253 NormalRemap::x( point.normal ) = -1;
1254 NormalRemap::y( point.normal ) = 0;
1255 NormalRemap::z( point.normal ) = 0;
1258 FlatShadedVertex& point = vertices[i * 6 + 5];
1259 VertexRemap::x( point.vertex ) = length - arrowhead_length;
1260 VertexRemap::y( point.vertex ) = arrowhead_radius * static_cast<float>( cos( ( i + 1 ) * head_segment ) );
1261 VertexRemap::z( point.vertex ) = arrowhead_radius * static_cast<float>( sin( ( i + 1 ) * head_segment ) );
1262 NormalRemap::x( point.normal ) = -1;
1263 NormalRemap::y( point.normal ) = 0;
1264 NormalRemap::z( point.normal ) = 0;
1269 template<typename Triple>
1270 class TripleRemapXYZ
1273 static float& x( Triple& triple ){
1276 static float& y( Triple& triple ){
1279 static float& z( Triple& triple ){
1284 template<typename Triple>
1285 class TripleRemapYZX
1288 static float& x( Triple& triple ){
1291 static float& y( Triple& triple ){
1294 static float& z( Triple& triple ){
1299 template<typename Triple>
1300 class TripleRemapZXY
1303 static float& x( Triple& triple ){
1306 static float& y( Triple& triple ){
1309 static float& z( Triple& triple ){
1314 void vector3_print( const Vector3& v ){
1315 globalOutputStream() << "( " << v.x() << " " << v.y() << " " << v.z() << " )";
1318 class TranslateManipulator : public Manipulator
1320 struct RenderableArrowLine : public OpenGLRenderable
1322 PointVertex m_line[2];
1324 RenderableArrowLine(){
1326 void render( RenderStateFlags state ) const {
1327 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_line[0].colour );
1328 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_line[0].vertex );
1329 glDrawArrays( GL_LINES, 0, 2 );
1331 void setColour( const Colour4b& colour ){
1332 m_line[0].colour = colour;
1333 m_line[1].colour = colour;
1336 struct RenderableArrowHead : public OpenGLRenderable
1338 Array<FlatShadedVertex> m_vertices;
1340 RenderableArrowHead( std::size_t size )
1341 : m_vertices( size ){
1343 void render( RenderStateFlags state ) const {
1344 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( FlatShadedVertex ), &m_vertices.data()->colour );
1345 glVertexPointer( 3, GL_FLOAT, sizeof( FlatShadedVertex ), &m_vertices.data()->vertex );
1346 glNormalPointer( GL_FLOAT, sizeof( FlatShadedVertex ), &m_vertices.data()->normal );
1347 glDrawArrays( GL_TRIANGLES, 0, GLsizei( m_vertices.size() ) );
1349 void setColour( const Colour4b& colour ){
1350 for ( Array<FlatShadedVertex>::iterator i = m_vertices.begin(); i != m_vertices.end(); ++i )
1352 ( *i ).colour = colour;
1356 struct RenderableQuad : public OpenGLRenderable
1358 PointVertex m_quad[4];
1359 void render( RenderStateFlags state ) const {
1360 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_quad[0].colour );
1361 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_quad[0].vertex );
1362 glDrawArrays( GL_LINE_LOOP, 0, 4 );
1364 void setColour( const Colour4b& colour ){
1365 m_quad[0].colour = colour;
1366 m_quad[1].colour = colour;
1367 m_quad[2].colour = colour;
1368 m_quad[3].colour = colour;
1372 TranslateFree m_free;
1373 TranslateAxis m_axis;
1374 RenderableArrowLine m_arrow_x;
1375 RenderableArrowLine m_arrow_y;
1376 RenderableArrowLine m_arrow_z;
1377 RenderableArrowHead m_arrow_head_x;
1378 RenderableArrowHead m_arrow_head_y;
1379 RenderableArrowHead m_arrow_head_z;
1380 RenderableQuad m_quad_screen;
1381 SelectableBool m_selectable_x;
1382 SelectableBool m_selectable_y;
1383 SelectableBool m_selectable_z;
1384 SelectableBool m_selectable_screen;
1385 Pivot2World m_pivot;
1387 static Shader* m_state_wire;
1388 static Shader* m_state_fill;
1390 TranslateManipulator( Translatable& translatable, std::size_t segments, float length ) :
1391 m_free( translatable ),
1392 m_axis( translatable ),
1393 m_arrow_head_x( 3 * 2 * ( segments << 3 ) ),
1394 m_arrow_head_y( 3 * 2 * ( segments << 3 ) ),
1395 m_arrow_head_z( 3 * 2 * ( segments << 3 ) ){
1396 draw_arrowline( length, m_arrow_x.m_line, 0 );
1397 draw_arrowhead( segments, length, m_arrow_head_x.m_vertices.data(), TripleRemapXYZ<Vertex3f>(), TripleRemapXYZ<Normal3f>() );
1398 draw_arrowline( length, m_arrow_y.m_line, 1 );
1399 draw_arrowhead( segments, length, m_arrow_head_y.m_vertices.data(), TripleRemapYZX<Vertex3f>(), TripleRemapYZX<Normal3f>() );
1400 draw_arrowline( length, m_arrow_z.m_line, 2 );
1401 draw_arrowhead( segments, length, m_arrow_head_z.m_vertices.data(), TripleRemapZXY<Vertex3f>(), TripleRemapZXY<Normal3f>() );
1403 draw_quad( 16, m_quad_screen.m_quad );
1406 void UpdateColours(){
1407 m_arrow_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1408 m_arrow_head_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1409 m_arrow_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1410 m_arrow_head_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1411 m_arrow_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1412 m_arrow_head_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1413 m_quad_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1416 bool manipulator_show_axis( const Pivot2World& pivot, const Vector3& axis ){
1417 return fabs( vector3_dot( pivot.m_axis_screen, axis ) ) < 0.95;
1420 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1421 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1426 Vector3 x = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.x() ) );
1427 bool show_x = manipulator_show_axis( m_pivot, x );
1429 Vector3 y = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.y() ) );
1430 bool show_y = manipulator_show_axis( m_pivot, y );
1432 Vector3 z = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.z() ) );
1433 bool show_z = manipulator_show_axis( m_pivot, z );
1435 renderer.SetState( m_state_wire, Renderer::eWireframeOnly );
1436 renderer.SetState( m_state_wire, Renderer::eFullMaterials );
1439 renderer.addRenderable( m_arrow_x, m_pivot.m_worldSpace );
1442 renderer.addRenderable( m_arrow_y, m_pivot.m_worldSpace );
1445 renderer.addRenderable( m_arrow_z, m_pivot.m_worldSpace );
1448 renderer.addRenderable( m_quad_screen, m_pivot.m_viewplaneSpace );
1450 renderer.SetState( m_state_fill, Renderer::eWireframeOnly );
1451 renderer.SetState( m_state_fill, Renderer::eFullMaterials );
1454 renderer.addRenderable( m_arrow_head_x, m_pivot.m_worldSpace );
1457 renderer.addRenderable( m_arrow_head_y, m_pivot.m_worldSpace );
1460 renderer.addRenderable( m_arrow_head_z, m_pivot.m_worldSpace );
1463 void testSelect( const View& view, const Matrix4& pivot2world ){
1464 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1466 SelectionPool selector;
1468 Vector3 x = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.x() ) );
1469 bool show_x = manipulator_show_axis( m_pivot, x );
1471 Vector3 y = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.y() ) );
1472 bool show_y = manipulator_show_axis( m_pivot, y );
1474 Vector3 z = vector3_normalised( vector4_to_vector3( m_pivot.m_worldSpace.z() ) );
1475 bool show_z = manipulator_show_axis( m_pivot, z );
1478 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1481 SelectionIntersection best;
1482 Quad_BestPoint( local2view, eClipCullCW, m_quad_screen.m_quad, best );
1483 if ( best.valid() ) {
1484 best = SelectionIntersection( 0, 0 );
1485 selector.addSelectable( best, &m_selectable_screen );
1491 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_worldSpace ) );
1493 #if defined( DEBUG_SELECTION )
1494 g_render_clipped.construct( view.GetViewMatrix() );
1498 SelectionIntersection best;
1499 Line_BestPoint( local2view, m_arrow_x.m_line, best );
1500 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_x.m_vertices.begin(), m_arrow_head_x.m_vertices.end(), best );
1501 selector.addSelectable( best, &m_selectable_x );
1505 SelectionIntersection best;
1506 Line_BestPoint( local2view, m_arrow_y.m_line, best );
1507 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_y.m_vertices.begin(), m_arrow_head_y.m_vertices.end(), best );
1508 selector.addSelectable( best, &m_selectable_y );
1512 SelectionIntersection best;
1513 Line_BestPoint( local2view, m_arrow_z.m_line, best );
1514 Triangles_BestPoint( local2view, eClipCullCW, m_arrow_head_z.m_vertices.begin(), m_arrow_head_z.m_vertices.end(), best );
1515 selector.addSelectable( best, &m_selectable_z );
1519 if ( !selector.failed() ) {
1520 ( *selector.begin() ).second->setSelected( true );
1524 Manipulatable* GetManipulatable(){
1525 if ( m_selectable_x.isSelected() ) {
1526 m_axis.SetAxis( g_vector3_axis_x );
1529 else if ( m_selectable_y.isSelected() ) {
1530 m_axis.SetAxis( g_vector3_axis_y );
1533 else if ( m_selectable_z.isSelected() ) {
1534 m_axis.SetAxis( g_vector3_axis_z );
1543 void setSelected( bool select ){
1544 m_selectable_x.setSelected( select );
1545 m_selectable_y.setSelected( select );
1546 m_selectable_z.setSelected( select );
1547 m_selectable_screen.setSelected( select );
1549 bool isSelected() const {
1550 return m_selectable_x.isSelected()
1551 | m_selectable_y.isSelected()
1552 | m_selectable_z.isSelected()
1553 | m_selectable_screen.isSelected();
1557 Shader* TranslateManipulator::m_state_wire;
1558 Shader* TranslateManipulator::m_state_fill;
1560 class ScaleManipulator : public Manipulator
1562 struct RenderableArrow : public OpenGLRenderable
1564 PointVertex m_line[2];
1566 void render( RenderStateFlags state ) const {
1567 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_line[0].colour );
1568 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_line[0].vertex );
1569 glDrawArrays( GL_LINES, 0, 2 );
1571 void setColour( const Colour4b& colour ){
1572 m_line[0].colour = colour;
1573 m_line[1].colour = colour;
1576 struct RenderableQuad : public OpenGLRenderable
1578 PointVertex m_quad[4];
1579 void render( RenderStateFlags state ) const {
1580 glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( PointVertex ), &m_quad[0].colour );
1581 glVertexPointer( 3, GL_FLOAT, sizeof( PointVertex ), &m_quad[0].vertex );
1582 glDrawArrays( GL_QUADS, 0, 4 );
1584 void setColour( const Colour4b& colour ){
1585 m_quad[0].colour = colour;
1586 m_quad[1].colour = colour;
1587 m_quad[2].colour = colour;
1588 m_quad[3].colour = colour;
1594 RenderableArrow m_arrow_x;
1595 RenderableArrow m_arrow_y;
1596 RenderableArrow m_arrow_z;
1597 RenderableQuad m_quad_screen;
1598 SelectableBool m_selectable_x;
1599 SelectableBool m_selectable_y;
1600 SelectableBool m_selectable_z;
1601 SelectableBool m_selectable_screen;
1602 Pivot2World m_pivot;
1604 ScaleManipulator( Scalable& scalable, std::size_t segments, float length ) :
1607 draw_arrowline( length, m_arrow_x.m_line, 0 );
1608 draw_arrowline( length, m_arrow_y.m_line, 1 );
1609 draw_arrowline( length, m_arrow_z.m_line, 2 );
1611 draw_quad( 16, m_quad_screen.m_quad );
1614 Pivot2World& getPivot(){
1618 void UpdateColours(){
1619 m_arrow_x.setColour( colourSelected( g_colour_x, m_selectable_x.isSelected() ) );
1620 m_arrow_y.setColour( colourSelected( g_colour_y, m_selectable_y.isSelected() ) );
1621 m_arrow_z.setColour( colourSelected( g_colour_z, m_selectable_z.isSelected() ) );
1622 m_quad_screen.setColour( colourSelected( g_colour_screen, m_selectable_screen.isSelected() ) );
1625 void render( Renderer& renderer, const VolumeTest& volume, const Matrix4& pivot2world ){
1626 m_pivot.update( pivot2world, volume.GetModelview(), volume.GetProjection(), volume.GetViewport() );
1631 renderer.addRenderable( m_arrow_x, m_pivot.m_worldSpace );
1632 renderer.addRenderable( m_arrow_y, m_pivot.m_worldSpace );
1633 renderer.addRenderable( m_arrow_z, m_pivot.m_worldSpace );
1635 renderer.addRenderable( m_quad_screen, m_pivot.m_viewpointSpace );
1637 void testSelect( const View& view, const Matrix4& pivot2world ){
1638 m_pivot.update( pivot2world, view.GetModelview(), view.GetProjection(), view.GetViewport() );
1640 SelectionPool selector;
1643 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_worldSpace ) );
1645 #if defined( DEBUG_SELECTION )
1646 g_render_clipped.construct( view.GetViewMatrix() );
1650 SelectionIntersection best;
1651 Line_BestPoint( local2view, m_arrow_x.m_line, best );
1652 selector.addSelectable( best, &m_selectable_x );
1656 SelectionIntersection best;
1657 Line_BestPoint( local2view, m_arrow_y.m_line, best );
1658 selector.addSelectable( best, &m_selectable_y );
1662 SelectionIntersection best;
1663 Line_BestPoint( local2view, m_arrow_z.m_line, best );
1664 selector.addSelectable( best, &m_selectable_z );
1669 Matrix4 local2view( matrix4_multiplied_by_matrix4( view.GetViewMatrix(), m_pivot.m_viewpointSpace ) );
1672 SelectionIntersection best;
1673 Quad_BestPoint( local2view, eClipCullCW, m_quad_screen.m_quad, best );
1674 selector.addSelectable( best, &m_selectable_screen );
1678 if ( !selector.failed() ) {
1679 ( *selector.begin() ).second->setSelected( true );
1683 Manipulatable* GetManipulatable(){
1684 if ( m_selectable_x.isSelected() ) {
1685 m_axis.SetAxis( g_vector3_axis_x );
1688 else if ( m_selectable_y.isSelected() ) {
1689 m_axis.SetAxis( g_vector3_axis_y );
1692 else if ( m_selectable_z.isSelected() ) {
1693 m_axis.SetAxis( g_vector3_axis_z );
1701 void setSelected( bool select ){
1702 m_selectable_x.setSelected( select );
1703 m_selectable_y.setSelected( select );
1704 m_selectable_z.setSelected( select );
1705 m_selectable_screen.setSelected( select );
1707 bool isSelected() const {
1708 return m_selectable_x.isSelected()
1709 | m_selectable_y.isSelected()
1710 | m_selectable_z.isSelected()
1711 | m_selectable_screen.isSelected();
1716 inline PlaneSelectable* Instance_getPlaneSelectable( scene::Instance& instance ){
1717 return InstanceTypeCast<PlaneSelectable>::cast( instance );
1720 class PlaneSelectableSelectPlanes : public scene::Graph::Walker
1722 Selector& m_selector;
1723 SelectionTest& m_test;
1724 PlaneCallback m_selectedPlaneCallback;
1726 PlaneSelectableSelectPlanes( Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback )
1727 : m_selector( selector ), m_test( test ), m_selectedPlaneCallback( selectedPlaneCallback ){
1729 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1730 if ( path.top().get().visible() ) {
1731 Selectable* selectable = Instance_getSelectable( instance );
1732 if ( selectable != 0 && selectable->isSelected() ) {
1733 PlaneSelectable* planeSelectable = Instance_getPlaneSelectable( instance );
1734 if ( planeSelectable != 0 ) {
1735 planeSelectable->selectPlanes( m_selector, m_test, m_selectedPlaneCallback );
1743 class PlaneSelectableSelectReversedPlanes : public scene::Graph::Walker
1745 Selector& m_selector;
1746 const SelectedPlanes& m_selectedPlanes;
1748 PlaneSelectableSelectReversedPlanes( Selector& selector, const SelectedPlanes& selectedPlanes )
1749 : m_selector( selector ), m_selectedPlanes( selectedPlanes ){
1751 bool pre( const scene::Path& path, scene::Instance& instance ) const {
1752 if ( path.top().get().visible() ) {
1753 Selectable* selectable = Instance_getSelectable( instance );
1754 if ( selectable != 0 && selectable->isSelected() ) {
1755 PlaneSelectable* planeSelectable = Instance_getPlaneSelectable( instance );
1756 if ( planeSelectable != 0 ) {
1757 planeSelectable->selectReversedPlanes( m_selector, m_selectedPlanes );
1765 void Scene_forEachPlaneSelectable_selectPlanes( scene::Graph& graph, Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback ){
1766 graph.traverse( PlaneSelectableSelectPlanes( selector, test, selectedPlaneCallback ) );
1769 void Scene_forEachPlaneSelectable_selectReversedPlanes( scene::Graph& graph, Selector& selector, const SelectedPlanes& selectedPlanes ){
1770 graph.traverse( PlaneSelectableSelectReversedPlanes( selector, selectedPlanes ) );
1777 bool operator()( const Plane3& plane, const Plane3& other ) const {
1778 if ( plane.a < other.a ) {
1781 if ( other.a < plane.a ) {
1785 if ( plane.b < other.b ) {
1788 if ( other.b < plane.b ) {
1792 if ( plane.c < other.c ) {
1795 if ( other.c < plane.c ) {
1799 if ( plane.d < other.d ) {
1802 if ( other.d < plane.d ) {
1810 typedef std::set<Plane3, PlaneLess> PlaneSet;
1812 inline void PlaneSet_insert( PlaneSet& self, const Plane3& plane ){
1813 self.insert( plane );
1816 inline bool PlaneSet_contains( const PlaneSet& self, const Plane3& plane ){
1817 return self.find( plane ) != self.end();
1821 class SelectedPlaneSet : public SelectedPlanes
1823 PlaneSet m_selectedPlanes;
1825 bool empty() const {
1826 return m_selectedPlanes.empty();
1829 void insert( const Plane3& plane ){
1830 PlaneSet_insert( m_selectedPlanes, plane );
1832 bool contains( const Plane3& plane ) const {
1833 return PlaneSet_contains( m_selectedPlanes, plane );
1835 typedef MemberCaller<SelectedPlaneSet, void(const Plane3&), &SelectedPlaneSet::insert> InsertCaller;
1839 bool Scene_forEachPlaneSelectable_selectPlanes( scene::Graph& graph, Selector& selector, SelectionTest& test ){
1840 SelectedPlaneSet selectedPlanes;
1842 Scene_forEachPlaneSelectable_selectPlanes( graph, selector, test, SelectedPlaneSet::InsertCaller( selectedPlanes ) );
1843 Scene_forEachPlaneSelectable_selectReversedPlanes( graph, selector, selectedPlanes );
1845 return !selectedPlanes.empty();
1848 void Scene_Translate_Component_Selected( scene::Graph& graph, const Vector3& translation );
1849 void Scene_Translate_Selected( scene::Graph& graph, const Vector3& translation );
1850 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume );
1851 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode );
1852 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode );
1853 void Scene_SelectAll_Component( bool select, SelectionSystem::EComponentMode componentMode );
1855 class ResizeTranslatable : public Translatable
1857 void translate( const Vector3& translation ){
1858 Scene_Translate_Component_Selected( GlobalSceneGraph(), translation );
1862 class DragTranslatable : public Translatable
1864 void translate( const Vector3& translation ){
1865 if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent ) {
1866 Scene_Translate_Component_Selected( GlobalSceneGraph(), translation );
1870 Scene_Translate_Selected( GlobalSceneGraph(), translation );
1875 class SelectionVolume : public SelectionTest
1877 Matrix4 m_local2view;
1883 SelectionVolume( const View& view )
1887 const VolumeTest& getVolume() const {
1891 const Vector3& getNear() const {
1894 const Vector3& getFar() const {
1898 void BeginMesh( const Matrix4& localToWorld, bool twoSided ){
1899 m_local2view = matrix4_multiplied_by_matrix4( m_view.GetViewMatrix(), localToWorld );
1901 // Cull back-facing polygons based on winding being clockwise or counter-clockwise.
1902 // Don't cull if the view is wireframe and the polygons are two-sided.
1903 m_cull = twoSided && !m_view.fill() ? eClipCullNone : ( matrix4_handedness( localToWorld ) == MATRIX4_RIGHTHANDED ) ? eClipCullCW : eClipCullCCW;
1906 Matrix4 screen2world( matrix4_full_inverse( m_local2view ) );
1908 m_near = vector4_projected(
1909 matrix4_transformed_vector4(
1911 Vector4( 0, 0, -1, 1 )
1915 m_far = vector4_projected(
1916 matrix4_transformed_vector4(
1918 Vector4( 0, 0, 1, 1 )
1923 #if defined( DEBUG_SELECTION )
1924 g_render_clipped.construct( m_view.GetViewMatrix() );
1927 void TestPoint( const Vector3& point, SelectionIntersection& best ){
1929 if ( matrix4_clip_point( m_local2view, point, clipped ) == c_CLIP_PASS ) {
1930 best = select_point_from_clipped( clipped );
1933 void TestPolygon( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1935 for ( std::size_t i = 0; i + 2 < count; ++i )
1938 matrix4_clip_triangle(
1940 reinterpret_cast<const Vector3&>( vertices[0] ),
1941 reinterpret_cast<const Vector3&>( vertices[i + 1] ),
1942 reinterpret_cast<const Vector3&>( vertices[i + 2] ),
1951 void TestLineLoop( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1956 for ( VertexPointer::iterator i = vertices.begin(), end = i + count, prev = i + ( count - 1 ); i != end; prev = i, ++i )
1961 reinterpret_cast<const Vector3&>( ( *prev ) ),
1962 reinterpret_cast<const Vector3&>( ( *i ) ),
1971 void TestLineStrip( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1976 for ( VertexPointer::iterator i = vertices.begin(), end = i + count, next = i + 1; next != end; i = next, ++next )
1981 reinterpret_cast<const Vector3&>( ( *i ) ),
1982 reinterpret_cast<const Vector3&>( ( *next ) ),
1991 void TestLines( const VertexPointer& vertices, std::size_t count, SelectionIntersection& best ){
1996 for ( VertexPointer::iterator i = vertices.begin(), end = i + count; i != end; i += 2 )
2001 reinterpret_cast<const Vector3&>( ( *i ) ),
2002 reinterpret_cast<const Vector3&>( ( *( i + 1 ) ) ),
2011 void TestTriangles( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
2013 for ( IndexPointer::iterator i( indices.begin() ); i != indices.end(); i += 3 )
2016 matrix4_clip_triangle(
2018 reinterpret_cast<const Vector3&>( vertices[*i] ),
2019 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2020 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2029 void TestQuads( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
2031 for ( IndexPointer::iterator i( indices.begin() ); i != indices.end(); i += 4 )
2034 matrix4_clip_triangle(
2036 reinterpret_cast<const Vector3&>( vertices[*i] ),
2037 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2038 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
2046 matrix4_clip_triangle(
2048 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2049 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2050 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
2059 void TestQuadStrip( const VertexPointer& vertices, const IndexPointer& indices, SelectionIntersection& best ){
2061 for ( IndexPointer::iterator i( indices.begin() ); i + 2 != indices.end(); i += 2 )
2064 matrix4_clip_triangle(
2066 reinterpret_cast<const Vector3&>( vertices[*i] ),
2067 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2068 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2076 matrix4_clip_triangle(
2078 reinterpret_cast<const Vector3&>( vertices[*( i + 2 )] ),
2079 reinterpret_cast<const Vector3&>( vertices[*( i + 1 )] ),
2080 reinterpret_cast<const Vector3&>( vertices[*( i + 3 )] ),
2091 class SelectionCounter
2094 using func = void(const Selectable &);
2096 SelectionCounter( const SelectionChangeCallback& onchanged )
2097 : m_count( 0 ), m_onchanged( onchanged ){
2099 void operator()( const Selectable& selectable ){
2100 if ( selectable.isSelected() ) {
2105 ASSERT_MESSAGE( m_count != 0, "selection counter underflow" );
2109 m_onchanged( selectable );
2111 bool empty() const {
2112 return m_count == 0;
2114 std::size_t size() const {
2118 std::size_t m_count;
2119 SelectionChangeCallback m_onchanged;
2122 inline void ConstructSelectionTest( View& view, const rect_t selection_box ){
2123 view.EnableScissor( selection_box.min[0], selection_box.max[0], selection_box.min[1], selection_box.max[1] );
2126 inline const rect_t SelectionBoxForPoint( const float device_point[2], const float device_epsilon[2] ){
2127 rect_t selection_box;
2128 selection_box.min[0] = device_point[0] - device_epsilon[0];
2129 selection_box.min[1] = device_point[1] - device_epsilon[1];
2130 selection_box.max[0] = device_point[0] + device_epsilon[0];
2131 selection_box.max[1] = device_point[1] + device_epsilon[1];
2132 return selection_box;
2135 inline const rect_t SelectionBoxForArea( const float device_point[2], const float device_delta[2] ){
2136 rect_t selection_box;
2137 selection_box.min[0] = ( device_delta[0] < 0 ) ? ( device_point[0] + device_delta[0] ) : ( device_point[0] );
2138 selection_box.min[1] = ( device_delta[1] < 0 ) ? ( device_point[1] + device_delta[1] ) : ( device_point[1] );
2139 selection_box.max[0] = ( device_delta[0] > 0 ) ? ( device_point[0] + device_delta[0] ) : ( device_point[0] );
2140 selection_box.max[1] = ( device_delta[1] > 0 ) ? ( device_point[1] + device_delta[1] ) : ( device_point[1] );
2141 return selection_box;
2144 Quaternion construct_local_rotation( const Quaternion& world, const Quaternion& localToWorld ){
2145 return quaternion_normalised( quaternion_multiplied_by_quaternion(
2146 quaternion_normalised( quaternion_multiplied_by_quaternion(
2147 quaternion_inverse( localToWorld ),
2154 inline void matrix4_assign_rotation( Matrix4& matrix, const Matrix4& other ){
2155 matrix[0] = other[0];
2156 matrix[1] = other[1];
2157 matrix[2] = other[2];
2158 matrix[4] = other[4];
2159 matrix[5] = other[5];
2160 matrix[6] = other[6];
2161 matrix[8] = other[8];
2162 matrix[9] = other[9];
2163 matrix[10] = other[10];
2166 void matrix4_assign_rotation_for_pivot( Matrix4& matrix, scene::Instance& instance ){
2167 Editable* editable = Node_getEditable( instance.path().top() );
2168 if ( editable != 0 ) {
2169 matrix4_assign_rotation( matrix, matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ) );
2173 matrix4_assign_rotation( matrix, instance.localToWorld() );
2177 inline bool Instance_isSelectedComponents( scene::Instance& instance ){
2178 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
2179 return componentSelectionTestable != 0
2180 && componentSelectionTestable->isSelectedComponents();
2183 class TranslateSelected : public SelectionSystem::Visitor
2185 const Vector3& m_translate;
2187 TranslateSelected( const Vector3& translate )
2188 : m_translate( translate ){
2190 void visit( scene::Instance& instance ) const {
2191 Transformable* transform = Instance_getTransformable( instance );
2192 if ( transform != 0 ) {
2193 transform->setType( TRANSFORM_PRIMITIVE );
2194 transform->setTranslation( m_translate );
2199 void Scene_Translate_Selected( scene::Graph& graph, const Vector3& translation ){
2200 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2201 GlobalSelectionSystem().foreachSelected( TranslateSelected( translation ) );
2205 Vector3 get_local_pivot( const Vector3& world_pivot, const Matrix4& localToWorld ){
2207 matrix4_transformed_point(
2208 matrix4_full_inverse( localToWorld ),
2214 void translation_for_pivoted_matrix_transform( Vector3& parent_translation, const Matrix4& local_transform, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2215 // we need a translation inside the parent system to move the origin of this object to the right place
2217 // mathematically, it must fulfill:
2219 // local_translation local_transform local_pivot = local_pivot
2220 // local_translation = local_pivot - local_transform local_pivot
2223 // local_transform local_translation local_pivot = local_pivot
2224 // local_translation local_pivot = local_transform^-1 local_pivot
2225 // local_translation + local_pivot = local_transform^-1 local_pivot
2226 // local_translation = local_transform^-1 local_pivot - local_pivot
2228 Vector3 local_pivot( get_local_pivot( world_pivot, localToWorld ) );
2230 Vector3 local_translation(
2233 matrix4_transformed_point(
2238 matrix4_transformed_point(
2239 matrix4_full_inverse(local_transform),
2247 translation_local2object( parent_translation, local_translation, localToParent );
2251 globalOutputStream() << "World pivot is at " << world_pivot << "\n";
2252 globalOutputStream() << "Local pivot is at " << local_pivot << "\n";
2253 globalOutputStream() << "Transformation " << local_transform << " moves it to: " << matrix4_transformed_point(local_transform, local_pivot) << "\n";
2254 globalOutputStream() << "Must move by " << local_translation << " in the local system" << "\n";
2255 globalOutputStream() << "Must move by " << parent_translation << " in the parent system" << "\n";
2259 void translation_for_pivoted_rotation( Vector3& parent_translation, const Quaternion& local_rotation, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2260 translation_for_pivoted_matrix_transform( parent_translation, matrix4_rotation_for_quaternion_quantised( local_rotation ), world_pivot, localToWorld, localToParent );
2263 void translation_for_pivoted_scale( Vector3& parent_translation, const Vector3& world_scale, const Vector3& world_pivot, const Matrix4& localToWorld, const Matrix4& localToParent ){
2264 Matrix4 local_transform(
2265 matrix4_multiplied_by_matrix4(
2266 matrix4_full_inverse( localToWorld ),
2267 matrix4_multiplied_by_matrix4(
2268 matrix4_scale_for_vec3( world_scale ),
2273 local_transform.tx() = local_transform.ty() = local_transform.tz() = 0; // cancel translation parts
2274 translation_for_pivoted_matrix_transform( parent_translation, local_transform, world_pivot, localToWorld, localToParent );
2277 class rotate_selected : public SelectionSystem::Visitor
2279 const Quaternion& m_rotate;
2280 const Vector3& m_world_pivot;
2282 rotate_selected( const Quaternion& rotation, const Vector3& world_pivot )
2283 : m_rotate( rotation ), m_world_pivot( world_pivot ){
2285 void visit( scene::Instance& instance ) const {
2286 TransformNode* transformNode = Node_getTransformNode( instance.path().top() );
2287 if ( transformNode != 0 ) {
2288 Transformable* transform = Instance_getTransformable( instance );
2289 if ( transform != 0 ) {
2290 transform->setType( TRANSFORM_PRIMITIVE );
2291 transform->setScale( c_scale_identity );
2292 transform->setTranslation( c_translation_identity );
2294 transform->setType( TRANSFORM_PRIMITIVE );
2295 transform->setRotation( m_rotate );
2298 Editable* editable = Node_getEditable( instance.path().top() );
2299 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2301 Vector3 parent_translation;
2302 translation_for_pivoted_rotation(
2306 matrix4_multiplied_by_matrix4( instance.localToWorld(), localPivot ),
2307 matrix4_multiplied_by_matrix4( transformNode->localToParent(), localPivot )
2310 transform->setTranslation( parent_translation );
2317 void Scene_Rotate_Selected( scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot ){
2318 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2319 GlobalSelectionSystem().foreachSelected( rotate_selected( rotation, world_pivot ) );
2323 class scale_selected : public SelectionSystem::Visitor
2325 const Vector3& m_scale;
2326 const Vector3& m_world_pivot;
2328 scale_selected( const Vector3& scaling, const Vector3& world_pivot )
2329 : m_scale( scaling ), m_world_pivot( world_pivot ){
2331 void visit( scene::Instance& instance ) const {
2332 TransformNode* transformNode = Node_getTransformNode( instance.path().top() );
2333 if ( transformNode != 0 ) {
2334 Transformable* transform = Instance_getTransformable( instance );
2335 if ( transform != 0 ) {
2336 transform->setType( TRANSFORM_PRIMITIVE );
2337 transform->setScale( c_scale_identity );
2338 transform->setTranslation( c_translation_identity );
2340 transform->setType( TRANSFORM_PRIMITIVE );
2341 transform->setScale( m_scale );
2343 Editable* editable = Node_getEditable( instance.path().top() );
2344 const Matrix4& localPivot = editable != 0 ? editable->getLocalPivot() : g_matrix4_identity;
2346 Vector3 parent_translation;
2347 translation_for_pivoted_scale(
2351 matrix4_multiplied_by_matrix4( instance.localToWorld(), localPivot ),
2352 matrix4_multiplied_by_matrix4( transformNode->localToParent(), localPivot )
2355 transform->setTranslation( parent_translation );
2362 void Scene_Scale_Selected( scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot ){
2363 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2364 GlobalSelectionSystem().foreachSelected( scale_selected( scaling, world_pivot ) );
2369 class translate_component_selected : public SelectionSystem::Visitor
2371 const Vector3& m_translate;
2373 translate_component_selected( const Vector3& translate )
2374 : m_translate( translate ){
2376 void visit( scene::Instance& instance ) const {
2377 Transformable* transform = Instance_getTransformable( instance );
2378 if ( transform != 0 ) {
2379 transform->setType( TRANSFORM_COMPONENT );
2380 transform->setTranslation( m_translate );
2385 void Scene_Translate_Component_Selected( scene::Graph& graph, const Vector3& translation ){
2386 if ( GlobalSelectionSystem().countSelected() != 0 ) {
2387 GlobalSelectionSystem().foreachSelectedComponent( translate_component_selected( translation ) );
2391 class rotate_component_selected : public SelectionSystem::Visitor
2393 const Quaternion& m_rotate;
2394 const Vector3& m_world_pivot;
2396 rotate_component_selected( const Quaternion& rotation, const Vector3& world_pivot )
2397 : m_rotate( rotation ), m_world_pivot( world_pivot ){
2399 void visit( scene::Instance& instance ) const {
2400 Transformable* transform = Instance_getTransformable( instance );
2401 if ( transform != 0 ) {
2402 Vector3 parent_translation;
2403 translation_for_pivoted_rotation( parent_translation, m_rotate, m_world_pivot, instance.localToWorld(), Node_getTransformNode( instance.path().top() )->localToParent() );
2405 transform->setType( TRANSFORM_COMPONENT );
2406 transform->setRotation( m_rotate );
2407 transform->setTranslation( parent_translation );
2412 void Scene_Rotate_Component_Selected( scene::Graph& graph, const Quaternion& rotation, const Vector3& world_pivot ){
2413 if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
2414 GlobalSelectionSystem().foreachSelectedComponent( rotate_component_selected( rotation, world_pivot ) );
2418 class scale_component_selected : public SelectionSystem::Visitor
2420 const Vector3& m_scale;
2421 const Vector3& m_world_pivot;
2423 scale_component_selected( const Vector3& scaling, const Vector3& world_pivot )
2424 : m_scale( scaling ), m_world_pivot( world_pivot ){
2426 void visit( scene::Instance& instance ) const {
2427 Transformable* transform = Instance_getTransformable( instance );
2428 if ( transform != 0 ) {
2429 Vector3 parent_translation;
2430 translation_for_pivoted_scale( parent_translation, m_scale, m_world_pivot, instance.localToWorld(), Node_getTransformNode( instance.path().top() )->localToParent() );
2432 transform->setType( TRANSFORM_COMPONENT );
2433 transform->setScale( m_scale );
2434 transform->setTranslation( parent_translation );
2439 void Scene_Scale_Component_Selected( scene::Graph& graph, const Vector3& scaling, const Vector3& world_pivot ){
2440 if ( GlobalSelectionSystem().countSelectedComponents() != 0 ) {
2441 GlobalSelectionSystem().foreachSelectedComponent( scale_component_selected( scaling, world_pivot ) );
2446 class BooleanSelector : public Selector
2449 SelectionIntersection m_intersection;
2450 Selectable* m_selectable;
2452 BooleanSelector() : m_selected( false ){
2455 void pushSelectable( Selectable& selectable ){
2456 m_intersection = SelectionIntersection();
2457 m_selectable = &selectable;
2459 void popSelectable(){
2460 if ( m_intersection.valid() ) {
2463 m_intersection = SelectionIntersection();
2465 void addIntersection( const SelectionIntersection& intersection ){
2466 if ( m_selectable->isSelected() ) {
2467 assign_if_closer( m_intersection, intersection );
2476 class BestSelector : public Selector
2478 SelectionIntersection m_intersection;
2479 Selectable* m_selectable;
2480 SelectionIntersection m_bestIntersection;
2481 std::list<Selectable*> m_bestSelectable;
2483 BestSelector() : m_bestIntersection( SelectionIntersection() ), m_bestSelectable( 0 ){
2486 void pushSelectable( Selectable& selectable ){
2487 m_intersection = SelectionIntersection();
2488 m_selectable = &selectable;
2490 void popSelectable(){
2491 if ( m_intersection.equalEpsilon( m_bestIntersection, 0.25f, 0.001f ) ) {
2492 m_bestSelectable.push_back( m_selectable );
2493 m_bestIntersection = m_intersection;
2495 else if ( m_intersection < m_bestIntersection ) {
2496 m_bestSelectable.clear();
2497 m_bestSelectable.push_back( m_selectable );
2498 m_bestIntersection = m_intersection;
2500 m_intersection = SelectionIntersection();
2502 void addIntersection( const SelectionIntersection& intersection ){
2503 assign_if_closer( m_intersection, intersection );
2506 std::list<Selectable*>& best(){
2507 return m_bestSelectable;
2511 class DragManipulator : public Manipulator
2513 TranslateFree m_freeResize;
2514 TranslateFree m_freeDrag;
2515 ResizeTranslatable m_resize;
2516 DragTranslatable m_drag;
2517 SelectableBool m_dragSelectable;
2522 DragManipulator() : m_freeResize( m_resize ), m_freeDrag( m_drag ), m_selected( false ){
2525 Manipulatable* GetManipulatable(){
2526 return m_dragSelectable.isSelected() ? &m_freeDrag : &m_freeResize;
2529 void testSelect( const View& view, const Matrix4& pivot2world ){
2530 SelectionPool selector;
2532 SelectionVolume test( view );
2534 if ( GlobalSelectionSystem().Mode() == SelectionSystem::ePrimitive ) {
2535 BooleanSelector booleanSelector;
2537 Scene_TestSelect_Primitive( booleanSelector, test, view );
2539 if ( booleanSelector.isSelected() ) {
2540 selector.addSelectable( SelectionIntersection( 0, 0 ), &m_dragSelectable );
2545 m_selected = Scene_forEachPlaneSelectable_selectPlanes( GlobalSceneGraph(), selector, test );
2550 BestSelector bestSelector;
2551 Scene_TestSelect_Component_Selected( bestSelector, test, view, GlobalSelectionSystem().ComponentMode() );
2552 for ( std::list<Selectable*>::iterator i = bestSelector.best().begin(); i != bestSelector.best().end(); ++i )
2554 if ( !( *i )->isSelected() ) {
2555 GlobalSelectionSystem().setSelectedAllComponents( false );
2558 selector.addSelectable( SelectionIntersection( 0, 0 ), ( *i ) );
2559 m_dragSelectable.setSelected( true );
2563 for ( SelectionPool::iterator i = selector.begin(); i != selector.end(); ++i )
2565 ( *i ).second->setSelected( true );
2569 void setSelected( bool select ){
2570 m_selected = select;
2571 m_dragSelectable.setSelected( select );
2573 bool isSelected() const {
2574 return m_selected || m_dragSelectable.isSelected();
2578 class ClipManipulator : public Manipulator
2582 Manipulatable* GetManipulatable(){
2583 ERROR_MESSAGE( "clipper is not manipulatable" );
2587 void setSelected( bool select ){
2589 bool isSelected() const {
2594 class select_all : public scene::Graph::Walker
2598 select_all( bool select )
2599 : m_select( select ){
2601 bool pre( const scene::Path& path, scene::Instance& instance ) const {
2602 Selectable* selectable = Instance_getSelectable( instance );
2603 if ( selectable != 0 ) {
2604 selectable->setSelected( m_select );
2610 class select_all_component : public scene::Graph::Walker
2613 SelectionSystem::EComponentMode m_mode;
2615 select_all_component( bool select, SelectionSystem::EComponentMode mode )
2616 : m_select( select ), m_mode( mode ){
2618 bool pre( const scene::Path& path, scene::Instance& instance ) const {
2619 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
2620 if ( componentSelectionTestable ) {
2621 componentSelectionTestable->setSelectedComponents( m_select, m_mode );
2627 void Scene_SelectAll_Component( bool select, SelectionSystem::EComponentMode componentMode ){
2628 GlobalSceneGraph().traverse( select_all_component( select, componentMode ) );
2632 // RadiantSelectionSystem
2633 class RadiantSelectionSystem :
2634 public SelectionSystem,
2635 public Translatable,
2640 mutable Matrix4 m_pivot2world;
2641 Matrix4 m_pivot2world_start;
2642 Matrix4 m_manip2pivot_start;
2643 Translation m_translation;
2644 Rotation m_rotation;
2647 static Shader* m_state;
2648 bool m_bPreferPointEntsIn2D;
2650 EManipulatorMode m_manipulator_mode;
2651 Manipulator* m_manipulator;
2656 EComponentMode m_componentmode;
2658 SelectionCounter m_count_primitive;
2659 SelectionCounter m_count_component;
2661 TranslateManipulator m_translate_manipulator;
2662 RotateManipulator m_rotate_manipulator;
2663 ScaleManipulator m_scale_manipulator;
2664 DragManipulator m_drag_manipulator;
2665 ClipManipulator m_clip_manipulator;
2667 typedef SelectionList<scene::Instance> selection_t;
2668 selection_t m_selection;
2669 selection_t m_component_selection;
2671 Signal1<const Selectable&> m_selectionChanged_callbacks;
2673 void ConstructPivot() const;
2674 void setCustomPivotOrigin( Vector3& point ) const;
2676 void getSelectionAABB( AABB& bounds ) const;
2678 mutable bool m_pivotChanged;
2679 bool m_pivot_moving;
2680 mutable bool m_pivotIsCustom;
2682 void Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode );
2684 bool nothingSelected() const {
2685 return ( Mode() == eComponent && m_count_component.empty() )
2686 || ( Mode() == ePrimitive && m_count_primitive.empty() );
2701 RadiantSelectionSystem() :
2702 m_bPreferPointEntsIn2D( true ),
2703 m_undo_begun( false ),
2704 m_mode( ePrimitive ),
2705 m_componentmode( eDefault ),
2706 m_count_primitive( SelectionChangedCaller( *this ) ),
2707 m_count_component( SelectionChangedCaller( *this ) ),
2708 m_translate_manipulator( *this, 2, 64 ),
2709 m_rotate_manipulator( *this, 8, 64 ),
2710 m_scale_manipulator( *this, 0, 64 ),
2711 m_pivotChanged( false ),
2712 m_pivot_moving( false ),
2713 m_pivotIsCustom( false ){
2714 SetManipulatorMode( eTranslate );
2716 addSelectionChangeCallback( PivotChangedSelectionCaller( *this ) );
2717 AddGridChangeCallback( PivotChangedCaller( *this ) );
2719 void pivotChanged() const {
2720 m_pivotChanged = true;
2721 SceneChangeNotify();
2723 typedef ConstMemberCaller<RadiantSelectionSystem, void(), &RadiantSelectionSystem::pivotChanged> PivotChangedCaller;
2724 void pivotChangedSelection( const Selectable& selectable ){
2727 typedef MemberCaller<RadiantSelectionSystem, void(const Selectable&), &RadiantSelectionSystem::pivotChangedSelection> PivotChangedSelectionCaller;
2729 void SetMode( EMode mode ){
2730 if ( m_mode != mode ) {
2735 EMode Mode() const {
2738 void SetComponentMode( EComponentMode mode ){
2739 m_componentmode = mode;
2741 EComponentMode ComponentMode() const {
2742 return m_componentmode;
2744 void SetManipulatorMode( EManipulatorMode mode ){
2745 m_pivotIsCustom = false;
2746 m_manipulator_mode = mode;
2747 switch ( m_manipulator_mode )
2749 case eTranslate: m_manipulator = &m_translate_manipulator; break;
2750 case eRotate: m_manipulator = &m_rotate_manipulator; break;
2751 case eScale: m_manipulator = &m_scale_manipulator; break;
2752 case eDrag: m_manipulator = &m_drag_manipulator; break;
2753 case eClip: m_manipulator = &m_clip_manipulator; break;
2757 EManipulatorMode ManipulatorMode() const {
2758 return m_manipulator_mode;
2761 SelectionChangeCallback getObserver( EMode mode ){
2762 if ( mode == ePrimitive ) {
2763 return makeCallback( m_count_primitive );
2767 return makeCallback( m_count_component );
2770 std::size_t countSelected() const {
2771 return m_count_primitive.size();
2773 std::size_t countSelectedComponents() const {
2774 return m_count_component.size();
2776 void onSelectedChanged( scene::Instance& instance, const Selectable& selectable ){
2777 if ( selectable.isSelected() ) {
2778 m_selection.append( instance );
2782 m_selection.erase( instance );
2785 ASSERT_MESSAGE( m_selection.size() == m_count_primitive.size(), "selection-tracking error" );
2787 void onComponentSelection( scene::Instance& instance, const Selectable& selectable ){
2788 if ( selectable.isSelected() ) {
2789 m_component_selection.append( instance );
2793 m_component_selection.erase( instance );
2796 ASSERT_MESSAGE( m_component_selection.size() == m_count_component.size(), "selection-tracking error" );
2798 scene::Instance& ultimateSelected() const {
2799 ASSERT_MESSAGE( m_selection.size() > 0, "no instance selected" );
2800 return m_selection.back();
2802 scene::Instance& penultimateSelected() const {
2803 ASSERT_MESSAGE( m_selection.size() > 1, "only one instance selected" );
2804 return *( *( --( --m_selection.end() ) ) );
2806 void setSelectedAll( bool selected ){
2807 GlobalSceneGraph().traverse( select_all( selected ) );
2809 m_manipulator->setSelected( selected );
2811 void setSelectedAllComponents( bool selected ){
2812 Scene_SelectAll_Component( selected, SelectionSystem::eVertex );
2813 Scene_SelectAll_Component( selected, SelectionSystem::eEdge );
2814 Scene_SelectAll_Component( selected, SelectionSystem::eFace );
2816 m_manipulator->setSelected( selected );
2819 void foreachSelected( const Visitor& visitor ) const {
2820 selection_t::const_iterator i = m_selection.begin();
2821 while ( i != m_selection.end() )
2823 visitor.visit( *( *( i++ ) ) );
2826 void foreachSelectedComponent( const Visitor& visitor ) const {
2827 selection_t::const_iterator i = m_component_selection.begin();
2828 while ( i != m_component_selection.end() )
2830 visitor.visit( *( *( i++ ) ) );
2834 void addSelectionChangeCallback( const SelectionChangeHandler& handler ){
2835 m_selectionChanged_callbacks.connectLast( handler );
2837 void selectionChanged( const Selectable& selectable ){
2838 m_selectionChanged_callbacks( selectable );
2840 typedef MemberCaller<RadiantSelectionSystem, void(const Selectable&), &RadiantSelectionSystem::selectionChanged> SelectionChangedCaller;
2844 m_pivot2world_start = GetPivot2World();
2847 bool SelectManipulator( const View& view, const float device_point[2], const float device_epsilon[2] ){
2848 if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
2849 #if defined ( DEBUG_SELECTION )
2850 g_render_clipped.destroy();
2853 m_manipulator->setSelected( false );
2855 if ( !nothingSelected() || ( ManipulatorMode() == eDrag && Mode() == eComponent ) ) {
2856 View scissored( view );
2857 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2858 m_manipulator->testSelect( scissored, GetPivot2World() );
2863 m_pivot_moving = m_manipulator->isSelected();
2865 if ( m_pivot_moving ) {
2867 pivot.update( GetPivot2World(), view.GetModelview(), view.GetProjection(), view.GetViewport() );
2869 m_manip2pivot_start = matrix4_multiplied_by_matrix4( matrix4_full_inverse( m_pivot2world_start ), pivot.m_worldSpace );
2871 Matrix4 device2manip;
2872 ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
2873 m_manipulator->GetManipulatable()->Construct( device2manip, device_point[0], device_point[1] );
2875 m_undo_begun = false;
2878 SceneChangeNotify();
2881 return m_pivot_moving;
2885 if ( Mode() == eComponent ) {
2886 setSelectedAllComponents( false );
2890 setSelectedAll( false );
2894 void deselectComponentsOrAll( bool components ){
2896 setSelectedAllComponents( false );
2904 void SelectPoint( const View& view, const float device_point[2], const float device_epsilon[2], RadiantSelectionSystem::EModifier modifier, bool face ){
2905 //globalOutputStream() << device_point[0] << " " << device_point[1] << "\n";
2906 ASSERT_MESSAGE( fabs( device_point[0] ) <= 1.0f && fabs( device_point[1] ) <= 1.0f, "point-selection error" );
2908 if ( modifier == eReplace ) {
2909 deselectComponentsOrAll( face );
2912 //nothingSelected() doesn't consider faces, selected in non-component mode, m
2913 if ( modifier == eCycle && nothingSelected() ){
2914 modifier = eReplace;
2917 #if defined ( DEBUG_SELECTION )
2918 g_render_clipped.destroy();
2922 View scissored( view );
2923 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
2925 SelectionVolume volume( scissored );
2926 SelectionPool selector;
2927 SelectionPool selector_point_ents;
2928 const bool prefer_point_ents = m_bPreferPointEntsIn2D && Mode() == ePrimitive && !view.fill() && !face
2929 && ( modifier == RadiantSelectionSystem::eReplace || modifier == RadiantSelectionSystem::eSelect || modifier == RadiantSelectionSystem::eDeselect );
2931 if( prefer_point_ents ){
2932 Scene_TestSelect( selector_point_ents, volume, scissored, eEntity, ComponentMode() );
2934 if( prefer_point_ents && !selector_point_ents.failed() ){
2937 // if cycle mode not enabled, enable it
2938 case RadiantSelectionSystem::eReplace:
2941 ( *selector_point_ents.begin() ).second->setSelected( true );
2944 case RadiantSelectionSystem::eSelect:
2946 SelectionPool::iterator best = selector_point_ents.begin();
2947 if( !( *best ).second->isSelected() ){
2948 ( *best ).second->setSelected( true );
2950 SelectionPool::iterator i = best;
2952 while ( i != selector_point_ents.end() )
2954 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
2955 if( !( *i ).second->isSelected() ){
2956 ( *i ).second->setSelected( true );
2966 case RadiantSelectionSystem::eDeselect:
2968 SelectionPool::iterator best = selector_point_ents.begin();
2969 if( ( *best ).second->isSelected() ){
2970 ( *best ).second->setSelected( false );
2972 SelectionPool::iterator i = best;
2974 while ( i != selector_point_ents.end() )
2976 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
2977 if( ( *i ).second->isSelected() ){
2978 ( *i ).second->setSelected( false );
2994 Scene_TestSelect_Component( selector, volume, scissored, eFace );
2997 Scene_TestSelect( selector, volume, scissored, Mode(), ComponentMode() );
3000 if ( !selector.failed() ) {
3003 case RadiantSelectionSystem::eToggle:
3005 SelectableSortedSet::iterator best = selector.begin();
3006 // toggle selection of the object with least depth
3007 if ( ( *best ).second->isSelected() ) {
3008 ( *best ).second->setSelected( false );
3011 ( *best ).second->setSelected( true );
3015 // if cycle mode not enabled, enable it
3016 case RadiantSelectionSystem::eReplace:
3019 ( *selector.begin() ).second->setSelected( true );
3022 // select the next object in the list from the one already selected
3023 case RadiantSelectionSystem::eCycle:
3025 bool CycleSelectionOccured = false;
3026 SelectionPool::iterator i = selector.begin();
3027 while ( i != selector.end() )
3029 if ( ( *i ).second->isSelected() ) {
3030 deselectComponentsOrAll( face );
3032 if ( i != selector.end() ) {
3033 i->second->setSelected( true );
3037 selector.begin()->second->setSelected( true );
3039 CycleSelectionOccured = true;
3044 if( !CycleSelectionOccured ){
3045 deselectComponentsOrAll( face );
3046 ( *selector.begin() ).second->setSelected( true );
3050 case RadiantSelectionSystem::eSelect:
3052 SelectionPool::iterator best = selector.begin();
3053 if( !( *best ).second->isSelected() ){
3054 ( *best ).second->setSelected( true );
3056 SelectionPool::iterator i = best;
3058 while ( i != selector.end() )
3060 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3061 if( !( *i ).second->isSelected() ){
3062 ( *i ).second->setSelected( true );
3072 case RadiantSelectionSystem::eDeselect:
3074 SelectionPool::iterator best = selector.begin();
3075 if( ( *best ).second->isSelected() ){
3076 ( *best ).second->setSelected( false );
3078 SelectionPool::iterator i = best;
3080 while ( i != selector.end() )
3082 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3083 if( ( *i ).second->isSelected() ){
3084 ( *i ).second->setSelected( false );
3098 else if( modifier == eCycle ){
3099 deselectComponentsOrAll( face );
3105 bool SelectPoint_InitPaint( const View& view, const float device_point[2], const float device_epsilon[2], bool face ){
3106 ASSERT_MESSAGE( fabs( device_point[0] ) <= 1.0f && fabs( device_point[1] ) <= 1.0f, "point-selection error" );
3107 #if defined ( DEBUG_SELECTION )
3108 g_render_clipped.destroy();
3112 View scissored( view );
3113 ConstructSelectionTest( scissored, SelectionBoxForPoint( device_point, device_epsilon ) );
3115 SelectionVolume volume( scissored );
3116 SelectionPool selector;
3117 SelectionPool selector_point_ents;
3118 const bool prefer_point_ents = m_bPreferPointEntsIn2D && Mode() == ePrimitive && !view.fill() && !face;
3120 if( prefer_point_ents ){
3121 Scene_TestSelect( selector_point_ents, volume, scissored, eEntity, ComponentMode() );
3123 if( prefer_point_ents && !selector_point_ents.failed() ){
3124 SelectableSortedSet::iterator best = selector_point_ents.begin();
3125 const bool wasSelected = ( *best ).second->isSelected();
3126 ( *best ).second->setSelected( !wasSelected );
3127 SelectableSortedSet::iterator i = best;
3129 while ( i != selector_point_ents.end() )
3131 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3132 ( *i ).second->setSelected( !wasSelected );
3139 return !wasSelected;
3141 else{//do primitives, if ents failed
3143 Scene_TestSelect_Component( selector, volume, scissored, eFace );
3146 Scene_TestSelect( selector, volume, scissored, Mode(), ComponentMode() );
3148 if ( !selector.failed() ){
3149 SelectableSortedSet::iterator best = selector.begin();
3150 const bool wasSelected = ( *best ).second->isSelected();
3151 ( *best ).second->setSelected( !wasSelected );
3152 SelectableSortedSet::iterator i = best;
3154 while ( i != selector.end() )
3156 if( ( *i ).first.equalEpsilon( ( *best ).first, 0.25f, 0.000001f ) ){
3157 ( *i ).second->setSelected( !wasSelected );
3164 return !wasSelected;
3173 void SelectArea( const View& view, const float device_point[2], const float device_delta[2], RadiantSelectionSystem::EModifier modifier, bool face ){
3174 if ( modifier == eReplace ) {
3175 deselectComponentsOrAll( face );
3178 #if defined ( DEBUG_SELECTION )
3179 g_render_clipped.destroy();
3183 View scissored( view );
3184 ConstructSelectionTest( scissored, SelectionBoxForArea( device_point, device_delta ) );
3186 SelectionVolume volume( scissored );
3189 Scene_TestSelect_Component( pool, volume, scissored, eFace );
3193 Scene_TestSelect( pool, volume, scissored, Mode(), ComponentMode() );
3196 for ( SelectionPool::iterator i = pool.begin(); i != pool.end(); ++i )
3198 ( *i ).second->setSelected( !( modifier == RadiantSelectionSystem::eToggle && ( *i ).second->isSelected() ) );
3204 void translate( const Vector3& translation ){
3205 if ( !nothingSelected() ) {
3206 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
3208 m_translation = translation;
3210 m_pivot2world = m_pivot2world_start;
3211 matrix4_translate_by_vec3( m_pivot2world, translation );
3213 if ( Mode() == eComponent ) {
3214 Scene_Translate_Component_Selected( GlobalSceneGraph(), m_translation );
3218 Scene_Translate_Selected( GlobalSceneGraph(), m_translation );
3221 SceneChangeNotify();
3224 void outputTranslation( TextOutputStream& ostream ){
3225 ostream << " -xyz " << m_translation.x() << " " << m_translation.y() << " " << m_translation.z();
3227 void rotate( const Quaternion& rotation ){
3228 if ( !nothingSelected() ) {
3229 //ASSERT_MESSAGE(!m_pivotChanged, "pivot is invalid");
3231 m_rotation = rotation;
3233 if ( Mode() == eComponent ) {
3234 Scene_Rotate_Component_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
3236 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3240 Scene_Rotate_Selected( GlobalSceneGraph(), m_rotation, vector4_to_vector3( m_pivot2world.t() ) );
3242 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3245 SceneChangeNotify();
3248 void outputRotation( TextOutputStream& ostream ){
3249 ostream << " -eulerXYZ " << m_rotation.x() << " " << m_rotation.y() << " " << m_rotation.z();
3251 void scale( const Vector3& scaling ){
3252 if ( !nothingSelected() ) {
3255 if ( Mode() == eComponent ) {
3256 Scene_Scale_Component_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
3260 Scene_Scale_Selected( GlobalSceneGraph(), m_scale, vector4_to_vector3( m_pivot2world.t() ) );
3263 SceneChangeNotify();
3266 void outputScale( TextOutputStream& ostream ){
3267 ostream << " -scale " << m_scale.x() << " " << m_scale.y() << " " << m_scale.z();
3270 void rotateSelected( const Quaternion& rotation, bool snapOrigin ){
3271 if( snapOrigin && !m_pivotIsCustom ){
3272 m_pivot2world.tx() = float_snapped( m_pivot2world.tx(), GetSnapGridSize() );
3273 m_pivot2world.ty() = float_snapped( m_pivot2world.ty(), GetSnapGridSize() );
3274 m_pivot2world.tz() = float_snapped( m_pivot2world.tz(), GetSnapGridSize() );
3280 void translateSelected( const Vector3& translation ){
3282 translate( translation );
3285 void scaleSelected( const Vector3& scaling ){
3291 void MoveSelected( const View& view, const float device_point[2], bool snap ){
3292 if ( m_manipulator->isSelected() ) {
3293 if ( !m_undo_begun ) {
3294 m_undo_begun = true;
3295 GlobalUndoSystem().start();
3298 Matrix4 device2manip;
3299 ConstructDevice2Manip( device2manip, m_pivot2world_start, view.GetModelview(), view.GetProjection(), view.GetViewport() );
3300 m_manipulator->GetManipulatable()->Transform( m_manip2pivot_start, device2manip, device_point[0], device_point[1], snap );
3304 /// \todo Support view-dependent nudge.
3305 void NudgeManipulator( const Vector3& nudge, const Vector3& view ){
3306 if ( ManipulatorMode() == eTranslate || ManipulatorMode() == eDrag ) {
3307 translateSelected( nudge );
3312 void freezeTransforms();
3314 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const;
3315 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
3316 renderSolid( renderer, volume );
3319 const Matrix4& GetPivot2World() const {
3321 return m_pivot2world;
3324 static void constructStatic(){
3325 m_state = GlobalShaderCache().capture( "$POINT" );
3326 #if defined( DEBUG_SELECTION )
3327 g_state_clipped = GlobalShaderCache().capture( "$DEBUG_CLIPPED" );
3329 TranslateManipulator::m_state_wire = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3330 TranslateManipulator::m_state_fill = GlobalShaderCache().capture( "$FLATSHADE_OVERLAY" );
3331 RotateManipulator::m_state_outer = GlobalShaderCache().capture( "$WIRE_OVERLAY" );
3334 static void destroyStatic(){
3335 #if defined( DEBUG_SELECTION )
3336 GlobalShaderCache().release( "$DEBUG_CLIPPED" );
3338 GlobalShaderCache().release( "$WIRE_OVERLAY" );
3339 GlobalShaderCache().release( "$FLATSHADE_OVERLAY" );
3340 GlobalShaderCache().release( "$WIRE_OVERLAY" );
3341 GlobalShaderCache().release( "$POINT" );
3345 Shader* RadiantSelectionSystem::m_state = 0;
3350 RadiantSelectionSystem* g_RadiantSelectionSystem;
3352 inline RadiantSelectionSystem& getSelectionSystem(){
3353 return *g_RadiantSelectionSystem;
3359 class testselect_entity_visible : public scene::Graph::Walker
3361 Selector& m_selector;
3362 SelectionTest& m_test;
3364 testselect_entity_visible( Selector& selector, SelectionTest& test )
3365 : m_selector( selector ), m_test( test ){
3367 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3368 if( path.top().get_pointer() == Map_GetWorldspawn( g_map ) ||
3369 node_is_group( path.top().get() ) ){
3372 Selectable* selectable = Instance_getSelectable( instance );
3373 if ( selectable != 0
3374 && Node_isEntity( path.top() ) ) {
3375 m_selector.pushSelectable( *selectable );
3378 SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3379 if ( selectionTestable ) {
3380 selectionTestable->testSelect( m_selector, m_test );
3385 void post( const scene::Path& path, scene::Instance& instance ) const {
3386 Selectable* selectable = Instance_getSelectable( instance );
3387 if ( selectable != 0
3388 && Node_isEntity( path.top() ) ) {
3389 m_selector.popSelectable();
3394 class testselect_primitive_visible : public scene::Graph::Walker
3396 Selector& m_selector;
3397 SelectionTest& m_test;
3399 testselect_primitive_visible( Selector& selector, SelectionTest& test )
3400 : m_selector( selector ), m_test( test ){
3402 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3403 Selectable* selectable = Instance_getSelectable( instance );
3404 if ( selectable != 0 ) {
3405 m_selector.pushSelectable( *selectable );
3408 SelectionTestable* selectionTestable = Instance_getSelectionTestable( instance );
3409 if ( selectionTestable ) {
3410 selectionTestable->testSelect( m_selector, m_test );
3415 void post( const scene::Path& path, scene::Instance& instance ) const {
3416 Selectable* selectable = Instance_getSelectable( instance );
3417 if ( selectable != 0 ) {
3418 m_selector.popSelectable();
3423 class testselect_component_visible : public scene::Graph::Walker
3425 Selector& m_selector;
3426 SelectionTest& m_test;
3427 SelectionSystem::EComponentMode m_mode;
3429 testselect_component_visible( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3430 : m_selector( selector ), m_test( test ), m_mode( mode ){
3432 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3433 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3434 if ( componentSelectionTestable ) {
3435 componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3443 class testselect_component_visible_selected : public scene::Graph::Walker
3445 Selector& m_selector;
3446 SelectionTest& m_test;
3447 SelectionSystem::EComponentMode m_mode;
3449 testselect_component_visible_selected( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode )
3450 : m_selector( selector ), m_test( test ), m_mode( mode ){
3452 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3453 Selectable* selectable = Instance_getSelectable( instance );
3454 if ( selectable != 0 && selectable->isSelected() ) {
3455 ComponentSelectionTestable* componentSelectionTestable = Instance_getComponentSelectionTestable( instance );
3456 if ( componentSelectionTestable ) {
3457 componentSelectionTestable->testSelectComponents( m_selector, m_test, m_mode );
3465 void Scene_TestSelect_Primitive( Selector& selector, SelectionTest& test, const VolumeTest& volume ){
3466 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_primitive_visible( selector, test ) );
3469 void Scene_TestSelect_Component_Selected( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3470 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible_selected( selector, test, componentMode ) );
3473 void Scene_TestSelect_Component( Selector& selector, SelectionTest& test, const VolumeTest& volume, SelectionSystem::EComponentMode componentMode ){
3474 Scene_forEachVisible( GlobalSceneGraph(), volume, testselect_component_visible( selector, test, componentMode ) );
3477 void RadiantSelectionSystem::Scene_TestSelect( Selector& selector, SelectionTest& test, const View& view, SelectionSystem::EMode mode, SelectionSystem::EComponentMode componentMode ){
3482 Scene_forEachVisible( GlobalSceneGraph(), view, testselect_entity_visible( selector, test ) );
3486 Scene_TestSelect_Primitive( selector, test, view );
3489 Scene_TestSelect_Component_Selected( selector, test, view, componentMode );
3494 class FreezeTransforms : public scene::Graph::Walker
3497 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3498 TransformNode* transformNode = Node_getTransformNode( path.top() );
3499 if ( transformNode != 0 ) {
3500 Transformable* transform = Instance_getTransformable( instance );
3501 if ( transform != 0 ) {
3502 transform->freezeTransform();
3509 void RadiantSelectionSystem::freezeTransforms(){
3510 GlobalSceneGraph().traverse( FreezeTransforms() );
3514 void RadiantSelectionSystem::endMove(){
3517 if ( Mode() == ePrimitive ) {
3518 if ( ManipulatorMode() == eDrag ) {
3519 Scene_SelectAll_Component( false, SelectionSystem::eFace );
3523 m_pivot_moving = false;
3526 SceneChangeNotify();
3528 if ( m_undo_begun ) {
3529 StringOutputStream command;
3531 if ( ManipulatorMode() == eTranslate ) {
3532 command << "translateTool";
3533 outputTranslation( command );
3535 else if ( ManipulatorMode() == eRotate ) {
3536 command << "rotateTool";
3537 outputRotation( command );
3539 else if ( ManipulatorMode() == eScale ) {
3540 command << "scaleTool";
3541 outputScale( command );
3543 else if ( ManipulatorMode() == eDrag ) {
3544 command << "dragTool";
3547 GlobalUndoSystem().finish( command.c_str() );
3552 inline AABB Instance_getPivotBounds( scene::Instance& instance ){
3553 Entity* entity = Node_getEntity( instance.path().top() );
3555 && ( entity->getEntityClass().fixedsize
3556 || !node_is_group( instance.path().top() ) ) ) {
3557 Editable* editable = Node_getEditable( instance.path().top() );
3558 if ( editable != 0 ) {
3559 return AABB( vector4_to_vector3( matrix4_multiplied_by_matrix4( instance.localToWorld(), editable->getLocalPivot() ).t() ), Vector3( 0, 0, 0 ) );
3563 return AABB( vector4_to_vector3( instance.localToWorld().t() ), Vector3( 0, 0, 0 ) );
3567 return instance.worldAABB();
3570 class bounds_selected : public scene::Graph::Walker
3574 bounds_selected( AABB& bounds )
3575 : m_bounds( bounds ){
3578 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3579 Selectable* selectable = Instance_getSelectable( instance );
3580 if ( selectable != 0
3581 && selectable->isSelected() ) {
3582 aabb_extend_by_aabb_safe( m_bounds, Instance_getPivotBounds( instance ) );
3588 class bounds_selected_component : public scene::Graph::Walker
3592 bounds_selected_component( AABB& bounds )
3593 : m_bounds( bounds ){
3596 bool pre( const scene::Path& path, scene::Instance& instance ) const {
3597 Selectable* selectable = Instance_getSelectable( instance );
3598 if ( selectable != 0
3599 && selectable->isSelected() ) {
3600 ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3601 if ( componentEditable ) {
3602 aabb_extend_by_aabb_safe( m_bounds, aabb_for_oriented_aabb_safe( componentEditable->getSelectedComponentsBounds(), instance.localToWorld() ) );
3609 void Scene_BoundsSelected( scene::Graph& graph, AABB& bounds ){
3610 graph.traverse( bounds_selected( bounds ) );
3613 void Scene_BoundsSelectedComponent( scene::Graph& graph, AABB& bounds ){
3614 graph.traverse( bounds_selected_component( bounds ) );
3618 inline void pivot_for_node( Matrix4& pivot, scene::Node& node, scene::Instance& instance ){
3619 ComponentEditable* componentEditable = Instance_getComponentEditable( instance );
3620 if ( GlobalSelectionSystem().Mode() == SelectionSystem::eComponent
3621 && componentEditable != 0 ) {
3622 pivot = matrix4_translation_for_vec3( componentEditable->getSelectedComponentsBounds().origin );
3626 Bounded* bounded = Instance_getBounded( instance );
3627 if ( bounded != 0 ) {
3628 pivot = matrix4_translation_for_vec3( bounded->localAABB().origin );
3632 pivot = g_matrix4_identity;
3638 void RadiantSelectionSystem::ConstructPivot() const {
3639 if ( !m_pivotChanged || m_pivot_moving || m_pivotIsCustom ) {
3642 m_pivotChanged = false;
3644 Vector3 m_object_pivot;
3646 if ( !nothingSelected() ) {
3649 if ( Mode() == eComponent ) {
3650 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3654 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3656 m_object_pivot = bounds.origin;
3659 //vector3_snap( m_object_pivot, GetSnapGridSize() );
3660 //globalOutputStream() << m_object_pivot << "\n";
3661 m_pivot2world = matrix4_translation_for_vec3( m_object_pivot );
3663 switch ( m_manipulator_mode )
3668 if ( Mode() == eComponent ) {
3669 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3673 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3677 if ( Mode() == eComponent ) {
3678 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3682 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3691 void RadiantSelectionSystem::setCustomPivotOrigin( Vector3& point ) const {
3692 if ( !nothingSelected() && ( m_manipulator_mode == eTranslate || m_manipulator_mode == eRotate || m_manipulator_mode == eScale ) ) {
3694 if ( Mode() == eComponent ) {
3695 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3699 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3701 //globalOutputStream() << point << "\n";
3702 for( std::size_t i = 0; i < 3; i++ ){
3703 if( point[i] < 900000.0f ){
3704 float bestsnapDist = fabs( bounds.origin[i] - point[i] );
3705 float bestsnapTo = bounds.origin[i];
3706 float othersnapDist = fabs( bounds.origin[i] + bounds.extents[i] - point[i] );
3707 if( othersnapDist < bestsnapDist ){
3708 bestsnapDist = othersnapDist;
3709 bestsnapTo = bounds.origin[i] + bounds.extents[i];
3711 othersnapDist = fabs( bounds.origin[i] - bounds.extents[i] - point[i] );
3712 if( othersnapDist < bestsnapDist ){
3713 bestsnapDist = othersnapDist;
3714 bestsnapTo = bounds.origin[i] - bounds.extents[i];
3716 othersnapDist = fabs( float_snapped( point[i], GetSnapGridSize() ) - point[i] );
3717 if( othersnapDist < bestsnapDist ){
3718 bestsnapDist = othersnapDist;
3719 bestsnapTo = float_snapped( point[i], GetSnapGridSize() );
3721 point[i] = bestsnapTo;
3723 m_pivot2world[i + 12] = point[i]; //m_pivot2world.tx() .ty() .tz()
3727 switch ( m_manipulator_mode )
3732 if ( Mode() == eComponent ) {
3733 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3737 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3741 if ( Mode() == eComponent ) {
3742 matrix4_assign_rotation_for_pivot( m_pivot2world, m_component_selection.back() );
3746 matrix4_assign_rotation_for_pivot( m_pivot2world, m_selection.back() );
3753 m_pivotIsCustom = true;
3757 void RadiantSelectionSystem::getSelectionAABB( AABB& bounds ) const {
3758 if ( !nothingSelected() ) {
3759 if ( Mode() == eComponent ) {
3760 Scene_BoundsSelectedComponent( GlobalSceneGraph(), bounds );
3764 Scene_BoundsSelected( GlobalSceneGraph(), bounds );
3769 void GetSelectionAABB( AABB& bounds ){
3770 getSelectionSystem().getSelectionAABB( bounds );
3773 const Matrix4& SelectionSystem_GetPivot2World(){
3774 return getSelectionSystem().GetPivot2World();
3777 void RadiantSelectionSystem::renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
3778 //if(view->TestPoint(m_object_pivot))
3779 if ( !nothingSelected() ) {
3780 renderer.Highlight( Renderer::ePrimitive, false );
3781 renderer.Highlight( Renderer::eFace, false );
3783 renderer.SetState( m_state, Renderer::eWireframeOnly );
3784 renderer.SetState( m_state, Renderer::eFullMaterials );
3786 m_manipulator->render( renderer, volume, GetPivot2World() );
3789 #if defined( DEBUG_SELECTION )
3790 renderer.SetState( g_state_clipped, Renderer::eWireframeOnly );
3791 renderer.SetState( g_state_clipped, Renderer::eFullMaterials );
3792 renderer.addRenderable( g_render_clipped, g_render_clipped.m_world );
3796 #include "preferencesystem.h"
3797 #include "preferences.h"
3799 void SelectionSystem_constructPreferences( PreferencesPage& page ){
3800 page.appendCheckBox( "", "Prefer point entities in 2D", getSelectionSystem().m_bPreferPointEntsIn2D );
3802 void SelectionSystem_constructPage( PreferenceGroup& group ){
3803 PreferencesPage page( group.createPage( "Selection", "Selection System Settings" ) );
3804 SelectionSystem_constructPreferences( page );
3806 void SelectionSystem_registerPreferencesPage(){
3807 PreferencesDialog_addSettingsPage( FreeCaller<void(PreferenceGroup&), SelectionSystem_constructPage>() );
3812 void SelectionSystem_OnBoundsChanged(){
3813 getSelectionSystem().pivotChanged();
3816 SignalHandlerId SelectionSystem_boundsChanged;
3818 void SelectionSystem_Construct(){
3819 RadiantSelectionSystem::constructStatic();
3821 g_RadiantSelectionSystem = new RadiantSelectionSystem;
3823 SelectionSystem_boundsChanged = GlobalSceneGraph().addBoundsChangedCallback( FreeCaller<void(), SelectionSystem_OnBoundsChanged>() );
3825 GlobalShaderCache().attachRenderable( getSelectionSystem() );
3827 GlobalPreferenceSystem().registerPreference( "PreferPointEntsIn2D", make_property_string( getSelectionSystem().m_bPreferPointEntsIn2D ) );
3828 SelectionSystem_registerPreferencesPage();
3831 void SelectionSystem_Destroy(){
3832 GlobalShaderCache().detachRenderable( getSelectionSystem() );
3834 GlobalSceneGraph().removeBoundsChangedCallback( SelectionSystem_boundsChanged );
3836 delete g_RadiantSelectionSystem;
3838 RadiantSelectionSystem::destroyStatic();
3844 inline float screen_normalised( float pos, std::size_t size ){
3845 return ( ( 2.0f * pos ) / size ) - 1.0f;
3848 typedef Vector2 DeviceVector;
3850 inline DeviceVector window_to_normalised_device( WindowVector window, std::size_t width, std::size_t height ){
3851 return DeviceVector( screen_normalised( window.x(), width ), screen_normalised( height - 1 - window.y(), height ) );
3854 inline float device_constrained( float pos ){
3855 return std::min( 1.0f, std::max( -1.0f, pos ) );
3858 inline DeviceVector device_constrained( DeviceVector device ){
3859 return DeviceVector( device_constrained( device.x() ), device_constrained( device.y() ) );
3862 inline float window_constrained( float pos, std::size_t origin, std::size_t size ){
3863 return std::min( static_cast<float>( origin + size ), std::max( static_cast<float>( origin ), pos ) );
3866 inline WindowVector window_constrained( WindowVector window, std::size_t x, std::size_t y, std::size_t width, std::size_t height ){
3867 return WindowVector( window_constrained( window.x(), x, width ), window_constrained( window.y(), y, height ) );
3870 typedef Callback<void(DeviceVector)> MouseEventCallback;
3872 Single<MouseEventCallback> g_mouseMovedCallback;
3873 Single<MouseEventCallback> g_mouseUpCallback;
3876 const ButtonIdentifier c_button_select = c_buttonLeft;
3877 const ButtonIdentifier c_button_select2 = c_buttonRight;
3878 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3879 const ModifierFlags c_modifier_toggle = c_modifierShift;
3880 const ModifierFlags c_modifier_replace = c_modifierShift | c_modifierAlt;
3881 const ModifierFlags c_modifier_face = c_modifierControl;
3883 const ButtonIdentifier c_button_select = c_buttonLeft;
3884 const ModifierFlags c_modifier_manipulator = c_modifierNone;
3885 const ModifierFlags c_modifier_toggle = c_modifierControl;
3886 const ModifierFlags c_modifier_replace = c_modifierNone;
3887 const ModifierFlags c_modifier_face = c_modifierShift;
3889 const ModifierFlags c_modifier_toggle_face = c_modifier_toggle | c_modifier_face;
3890 const ModifierFlags c_modifier_replace_face = c_modifier_replace | c_modifier_face;
3892 const ButtonIdentifier c_button_texture = c_buttonMiddle;
3893 const ModifierFlags c_modifier_apply_texture1 = c_modifierControl | c_modifierShift;
3894 const ModifierFlags c_modifier_apply_texture2 = c_modifierControl;
3895 const ModifierFlags c_modifier_apply_texture3 = c_modifierShift;
3896 const ModifierFlags c_modifier_copy_texture = c_modifierNone;
3900 RadiantSelectionSystem::EModifier modifier_for_state( ModifierFlags state ){
3901 if ( ( state == c_modifier_toggle || state == c_modifier_toggle_face || state == c_modifier_face ) ) {
3903 return RadiantSelectionSystem::eReplace;
3906 return RadiantSelectionSystem::eToggle;
3909 return RadiantSelectionSystem::eManipulator;
3912 rect_t getDeviceArea() const {
3913 DeviceVector delta( m_current - m_start );
3914 if ( selecting() && fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
3915 return SelectionBoxForArea( &m_start[0], &delta[0] );
3919 rect_t default_area = { { 0, 0, }, { 0, 0, }, };
3920 return default_area;
3925 DeviceVector m_start;
3926 DeviceVector m_current;
3927 DeviceVector m_epsilon;
3928 ModifierFlags m_state;
3931 bool m_mouseMovedWhilePressed;
3934 RectangleCallback m_window_update;
3936 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 ){
3940 m_window_update( getDeviceArea() );
3943 void testSelect( DeviceVector position ){
3944 RadiantSelectionSystem::EModifier modifier = modifier_for_state( m_state );
3945 if ( modifier != RadiantSelectionSystem::eManipulator ) {
3946 DeviceVector delta( position - m_start );
3947 if ( fabs( delta.x() ) > m_epsilon.x() && fabs( delta.y() ) > m_epsilon.y() ) {
3948 DeviceVector delta( position - m_start );
3949 //getSelectionSystem().SelectArea( *m_view, &m_start[0], &delta[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
3950 getSelectionSystem().SelectArea( *m_view, &m_start[0], &delta[0], RadiantSelectionSystem::eToggle, ( m_state & c_modifier_face ) != c_modifierNone );
3952 else if( !m_mouseMovedWhilePressed ){
3953 if ( modifier == RadiantSelectionSystem::eReplace && !m_mouseMoved ) {
3954 modifier = RadiantSelectionSystem::eCycle;
3956 getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], modifier, ( m_state & c_modifier_face ) != c_modifierNone );
3960 m_start = m_current = DeviceVector( 0.0f, 0.0f );
3964 void testSelect_simpleM1( DeviceVector position ){
3965 /*RadiantSelectionSystem::EModifier modifier = RadiantSelectionSystem::eReplace;
3966 DeviceVector delta( position - m_start );
3967 if ( fabs( delta.x() ) < m_epsilon.x() && fabs( delta.y() ) < m_epsilon.y() ) {
3968 modifier = RadiantSelectionSystem::eCycle;
3970 getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], modifier, false );*/
3971 getSelectionSystem().SelectPoint( *m_view, &position[0], &m_epsilon[0], m_mouseMoved ? RadiantSelectionSystem::eReplace : RadiantSelectionSystem::eCycle, false );
3972 m_start = m_current = device_constrained( position );
3976 bool selecting() const {
3977 return m_state != c_modifier_manipulator && m_mouse2;
3980 void setState( ModifierFlags state ){
3981 bool was_selecting = selecting();
3983 if ( was_selecting ^ selecting() ) {
3988 ModifierFlags getState() const {
3992 void modifierEnable( ModifierFlags type ){
3993 setState( bitfield_enable( getState(), type ) );
3995 void modifierDisable( ModifierFlags type ){
3996 setState( bitfield_disable( getState(), type ) );
3999 void mouseDown( DeviceVector position ){
4000 m_start = m_current = device_constrained( position );
4001 if( !m_mouse2 && m_state != c_modifierNone ){
4002 m_paintSelect = getSelectionSystem().SelectPoint_InitPaint( *m_view, &position[0], &m_epsilon[0], ( m_state & c_modifier_face ) != c_modifierNone );
4006 void mouseMoved( DeviceVector position ){
4007 m_current = device_constrained( position );
4008 m_mouseMovedWhilePressed = true;
4012 else if( m_state != c_modifier_manipulator ){
4013 getSelectionSystem().SelectPoint( *m_view, &m_current[0], &m_epsilon[0],
4014 m_paintSelect ? RadiantSelectionSystem::eSelect : RadiantSelectionSystem::eDeselect,
4015 ( m_state & c_modifier_face ) != c_modifierNone );
4018 typedef MemberCaller<Selector_, void(DeviceVector), &Selector_::mouseMoved> MouseMovedCaller;
4020 void mouseUp( DeviceVector position ){
4022 testSelect( device_constrained( position ) );
4025 m_start = m_current = DeviceVector( 0.0f, 0.0f );
4028 g_mouseMovedCallback.clear();
4029 g_mouseUpCallback.clear();
4031 typedef MemberCaller<Selector_, void(DeviceVector), &Selector_::mouseUp> MouseUpCaller;
4038 DeviceVector m_epsilon;
4040 ModifierFlags m_state;
4042 Manipulator_() : m_state( c_modifierNone ){
4045 bool mouseDown( DeviceVector position ){
4046 return getSelectionSystem().SelectManipulator( *m_view, &position[0], &m_epsilon[0] );
4049 void mouseMoved( DeviceVector position ){
4050 getSelectionSystem().MoveSelected( *m_view, &position[0], ( m_state & c_modifierShift ) == c_modifierShift );
4052 typedef MemberCaller<Manipulator_, void(DeviceVector), &Manipulator_::mouseMoved> MouseMovedCaller;
4054 void mouseUp( DeviceVector position ){
4055 getSelectionSystem().endMove();
4056 g_mouseMovedCallback.clear();
4057 g_mouseUpCallback.clear();
4059 typedef MemberCaller<Manipulator_, void(DeviceVector), &Manipulator_::mouseUp> MouseUpCaller;
4061 void setState( ModifierFlags state ){
4065 ModifierFlags getState() const {
4069 void modifierEnable( ModifierFlags type ){
4070 setState( bitfield_enable( getState(), type ) );
4072 void modifierDisable( ModifierFlags type ){
4073 setState( bitfield_disable( getState(), type ) );
4077 void Scene_copyClosestTexture( SelectionTest& test );
4078 void Scene_applyClosestTexture( SelectionTest& test );
4080 class RadiantWindowObserver : public SelectionSystemWindowObserver
4093 Selector_ m_selector;
4094 Manipulator_ m_manipulator;
4096 RadiantWindowObserver() : m_mouse_down( false ){
4101 void setView( const View& view ){
4102 m_selector.m_view = &view;
4103 m_manipulator.m_view = &view;
4105 void setRectangleDrawCallback( const RectangleCallback& callback ){
4106 m_selector.m_window_update = callback;
4108 void onSizeChanged( int width, int height ){
4111 DeviceVector epsilon( SELECT_EPSILON / static_cast<float>( m_width ), SELECT_EPSILON / static_cast<float>( m_height ) );
4112 m_selector.m_epsilon = m_manipulator.m_epsilon = epsilon;
4114 void onMouseDown( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
4115 if ( button == c_button_select || ( button == c_button_select2 && modifiers != c_modifierNone ) ) {
4116 m_mouse_down = true;
4117 //m_selector.m_mouseMoved = false;
4119 DeviceVector devicePosition( window_to_normalised_device( position, m_width, m_height ) );
4120 if ( modifiers == c_modifier_manipulator && m_manipulator.mouseDown( devicePosition ) ) {
4121 g_mouseMovedCallback.insert( MouseEventCallback( Manipulator_::MouseMovedCaller( m_manipulator ) ) );
4122 g_mouseUpCallback.insert( MouseEventCallback( Manipulator_::MouseUpCaller( m_manipulator ) ) );
4126 if ( button == c_button_select ) {
4127 m_selector.m_mouse2 = false;
4130 m_selector.m_mouse2 = true;
4132 m_selector.mouseDown( devicePosition );
4133 g_mouseMovedCallback.insert( MouseEventCallback( Selector_::MouseMovedCaller( m_selector ) ) );
4134 g_mouseUpCallback.insert( MouseEventCallback( Selector_::MouseUpCaller( m_selector ) ) );
4137 else if ( button == c_button_texture ) {
4138 DeviceVector devicePosition( device_constrained( window_to_normalised_device( position, m_width, m_height ) ) );
4140 View scissored( *m_selector.m_view );
4141 ConstructSelectionTest( scissored, SelectionBoxForPoint( &devicePosition[0], &m_selector.m_epsilon[0] ) );
4142 SelectionVolume volume( scissored );
4144 if ( modifiers == c_modifier_apply_texture1 || modifiers == c_modifier_apply_texture2 || modifiers == c_modifier_apply_texture3 ) {
4145 Scene_applyClosestTexture( volume );
4147 else if ( modifiers == c_modifier_copy_texture ) {
4148 Scene_copyClosestTexture( volume );
4152 void onMouseMotion( const WindowVector& position, ModifierFlags modifiers ){
4153 m_selector.m_mouseMoved = true;
4154 if ( m_mouse_down && !g_mouseMovedCallback.empty() ) {
4155 m_selector.m_mouseMovedWhilePressed = true;
4156 g_mouseMovedCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
4159 void onMouseUp( const WindowVector& position, ButtonIdentifier button, ModifierFlags modifiers ){
4160 if ( ( button == c_button_select || button == c_button_select2 ) && !g_mouseUpCallback.empty() ) {
4161 m_mouse_down = false;
4163 g_mouseUpCallback.get() ( window_to_normalised_device( position, m_width, m_height ) );
4165 //L button w/o scene changed = tunnel selection
4166 if( // !getSelectionSystem().m_undo_begun &&
4167 modifiers == c_modifierNone && button == c_button_select &&
4168 //( !m_selector.m_mouseMoved || !m_mouse_down ) &&
4169 !m_selector.m_mouseMovedWhilePressed &&
4170 ( getSelectionSystem().Mode() != SelectionSystem::eComponent || getSelectionSystem().ManipulatorMode() != SelectionSystem::eDrag ) ){
4171 m_selector.testSelect_simpleM1( device_constrained( window_to_normalised_device( position, m_width, m_height ) ) );
4173 //getSelectionSystem().m_undo_begun = false;
4174 m_selector.m_mouseMoved = false;
4175 m_selector.m_mouseMovedWhilePressed = false;
4177 void onModifierDown( ModifierFlags type ){
4178 m_selector.modifierEnable( type );
4179 m_manipulator.modifierEnable( type );
4181 void onModifierUp( ModifierFlags type ){
4182 m_selector.modifierDisable( type );
4183 m_manipulator.modifierDisable( type );
4189 SelectionSystemWindowObserver* NewWindowObserver(){
4190 return new RadiantWindowObserver;
4195 #include "modulesystem/singletonmodule.h"
4196 #include "modulesystem/moduleregistry.h"
4198 class SelectionDependencies :
4199 public GlobalSceneGraphModuleRef,
4200 public GlobalShaderCacheModuleRef,
4201 public GlobalOpenGLModuleRef
4205 class SelectionAPI : public TypeSystemRef
4207 SelectionSystem* m_selection;
4209 typedef SelectionSystem Type;
4210 STRING_CONSTANT( Name, "*" );
4213 SelectionSystem_Construct();
4215 m_selection = &getSelectionSystem();
4218 SelectionSystem_Destroy();
4220 SelectionSystem* getTable(){
4225 typedef SingletonModule<SelectionAPI, SelectionDependencies> SelectionModule;
4226 typedef Static<SelectionModule> StaticSelectionModule;
4227 StaticRegisterModule staticRegisterSelection( StaticSelectionModule::instance() );