diff --git a/libs/entitylib.h b/libs/entitylib.h index b0c300f9..8575d65e 100644 --- a/libs/entitylib.h +++ b/libs/entitylib.h @@ -675,9 +675,14 @@ namespace std } } -inline bool aabb_fits_view( const AABB& aabb, const Matrix4& viewport, int ratio ){ +inline bool aabb_fits_view( const AABB& aabb, const Matrix4& modelview, const Matrix4& viewport, int ratio ){ + const AABB transformed_bounds = aabb_for_oriented_aabb( + AABB( aabb.origin, Vector3( std::max( aabb.extents[0], 8.f ), std::max( aabb.extents[1], 8.f ), std::max( aabb.extents[2], 8.f ) ) ), + modelview + ); + //return ( aabb.extents[0] / viewport[0] ) > 0.25f || ( aabb.extents[1] / viewport[5] ) > 0.25f; - return ( viewport[0] + viewport[5] ) / ( aabb.extents[0] + aabb.extents[1] ) < ratio; + return ( viewport[0] + viewport[5] ) / ( transformed_bounds.extents[0] + transformed_bounds.extents[1] ) < ratio; } #endif diff --git a/plugins/entity/angle.h b/plugins/entity/angle.h index 3e2da58b..e3a1a38d 100644 --- a/plugins/entity/angle.h +++ b/plugins/entity/angle.h @@ -52,7 +52,7 @@ inline void write_angle( float angle, Entity* entity ){ else { char value[64]; - sprintf( value, "%f", angle ); + sprintf( value, "%g", angle ); entity->setKeyValue( "angle", value ); } } @@ -79,13 +79,19 @@ void write( Entity* entity ) const { } }; +inline float float_snapped_to_zero( float value ){ + return fabs( value ) < 1e-6 ? 0.f : value; +} + inline float angle_rotated( float angle, const Quaternion& rotation ){ - return matrix4_get_rotation_euler_xyz_degrees( - matrix4_multiplied_by_matrix4( - matrix4_rotation_for_z_degrees( angle ), - matrix4_rotation_for_quaternion_quantised( rotation ) - ) - ).z(); + return float_snapped_to_zero( + matrix4_get_rotation_euler_xyz_degrees( + matrix4_multiplied_by_matrix4( + matrix4_rotation_for_quaternion_quantised( rotation ), + matrix4_rotation_for_z_degrees( angle ) + ) + ).z() + ); } #endif diff --git a/plugins/entity/angles.h b/plugins/entity/angles.h index 1ace81ea..ed594bc1 100644 --- a/plugins/entity/angles.h +++ b/plugins/entity/angles.h @@ -70,8 +70,6 @@ inline void write_angles( const Vector3& angles, Entity* entity ){ } else { - char value[64]; - if ( angles[0] == 0 && angles[1] == 0 ) { float yaw = angles[2]; entity->setKeyValue( "angles", "" ); @@ -79,20 +77,55 @@ inline void write_angles( const Vector3& angles, Entity* entity ){ } else { - sprintf( value, "%f %f %f", angles[1], angles[2], angles[0] ); + char value[64]; + sprintf( value, "%g %g %g", angles[1], angles[2], angles[0] ); entity->setKeyValue( "angle", "" ); entity->setKeyValue( "angles", value ); } } } +inline Matrix4 matrix4_rotation_for_euler_xyz_degrees_quantised( const Vector3& angles ){ + if( angles[0] == 0.f && angles[1] == 0.f ){ + return matrix4_rotation_for_z_degrees( angles[2] ); + } + else if( angles[0] == 0.f && angles[2] == 0.f ){ + return matrix4_rotation_for_y_degrees( angles[1] ); + } + else if( angles[1] == 0.f && angles[2] == 0.f ){ + return matrix4_rotation_for_x_degrees( angles[0] ); + } + return matrix4_rotation_for_euler_xyz_degrees( angles ); +} + +inline Vector3 angles_snapped_to_zero( const Vector3& angles ){ + float epsilon = ( fabs( angles[0] ) > 0.001f || fabs( angles[1] ) > 0.001f || fabs( angles[2] ) > 0.001f ) ? 5e-5 : 1e-6; + return Vector3( fabs( angles[0] ) < epsilon ? 0.f : angles[0], + fabs( angles[1] ) < epsilon ? 0.f : angles[1], + fabs( angles[2] ) < epsilon ? 0.f : angles[2] + ); +} + inline Vector3 angles_rotated( const Vector3& angles, const Quaternion& rotation ){ - return matrix4_get_rotation_euler_xyz_degrees( - matrix4_multiplied_by_matrix4( - matrix4_rotation_for_euler_xyz_degrees( angles ), - matrix4_rotation_for_quaternion_quantised( rotation ) - ) - ); + return angles_snapped_to_zero( + matrix4_get_rotation_euler_xyz_degrees( + matrix4_multiplied_by_matrix4( + matrix4_rotation_for_quaternion_quantised( rotation ), + matrix4_rotation_for_euler_xyz_degrees_quantised( angles ) + ) + ) + ); +} + +inline Vector3 angles_rotated_for_rotated_pivot( const Vector3& angles, const Quaternion& rotation ){ + return angles_snapped_to_zero( + matrix4_get_rotation_euler_xyz_degrees( + matrix4_multiplied_by_matrix4( + matrix4_rotation_for_euler_xyz_degrees_quantised( angles ), + matrix4_rotation_for_quaternion_quantised( rotation ) + ) + ) + ); } class AnglesKey diff --git a/plugins/entity/doom3group.cpp b/plugins/entity/doom3group.cpp index a7fcbaaa..099c7d4c 100644 --- a/plugins/entity/doom3group.cpp +++ b/plugins/entity/doom3group.cpp @@ -363,7 +363,7 @@ void renderSolid( Renderer& renderer, const VolumeTest& volume, const Matrix4& l renderer.addRenderable( m_curveCatmullRom.m_renderCurve, localToWorld ); } - if ( selected || childSelected || ( g_showNames && ( volume.fill() || aabb_fits_view( aabb_for_oriented_aabb( childBounds, volume.GetModelview() ), volume.GetViewport(), g_showNamesRatio ) ) ) ) { + if ( selected || childSelected || ( g_showNames && ( volume.fill() || aabb_fits_view( childBounds, volume.GetModelview(), volume.GetViewport(), g_showNamesRatio ) ) ) ) { // draw models as usual if ( !isModel() ) { // don't draw the name for worldspawn diff --git a/plugins/entity/eclassmodel.cpp b/plugins/entity/eclassmodel.cpp index 6ee57e64..385cc5e2 100644 --- a/plugins/entity/eclassmodel.cpp +++ b/plugins/entity/eclassmodel.cpp @@ -254,7 +254,7 @@ void renderSolid( Renderer& renderer, const VolumeTest& volume, const Matrix4& l renderer.PopState(); } renderer.SetState( m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly ); - if ( selected || ( g_showNames && ( volume.fill() || aabb_fits_view( aabb_for_oriented_aabb( m_aabb_local, volume.GetModelview() ), volume.GetViewport(), g_showNamesRatio ) ) ) ) { + if ( selected || ( g_showNames && ( volume.fill() || aabb_fits_view( m_aabb_local, volume.GetModelview(), volume.GetViewport(), g_showNamesRatio ) ) ) ) { m_renderName.render( renderer, volume, localToWorld, selected ); } } @@ -295,7 +295,7 @@ void freezeTransform(){ rotation_assign( m_rotationKey.m_rotation, m_rotation ); m_rotationKey.write( &m_entity ); } - else + else if( m_angleKey.m_angle != m_angle ) { m_angleKey.m_angle = m_angle; m_angleKey.write( &m_entity ); @@ -355,7 +355,10 @@ void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const { void evaluateTransform(){ if ( getType() == TRANSFORM_PRIMITIVE ) { m_contained.translate( getTranslation() ); - m_contained.rotate( getRotation() ); + if( getRotation() != c_quaternion_identity ){ + m_contained.rotate( getRotation() ); + } + } } void applyTransform(){ diff --git a/plugins/entity/generic.cpp b/plugins/entity/generic.cpp index 2c22d708..e6d98021 100644 --- a/plugins/entity/generic.cpp +++ b/plugins/entity/generic.cpp @@ -227,7 +227,7 @@ void renderWireframe( Renderer& renderer, const VolumeTest& volume, const Matrix renderer.SetState( m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly ); renderer.addRenderable( m_aabb_wire, localToWorld ); renderArrow( renderer, volume, localToWorld ); - if ( selected || ( g_showNames && aabb_fits_view( aabb_for_oriented_aabb( m_aabb_local, volume.GetModelview() ), volume.GetViewport(), g_showNamesRatio ) ) ) { + if ( selected || ( g_showNames && aabb_fits_view( m_aabb_local, volume.GetModelview(), volume.GetViewport(), g_showNamesRatio ) ) ) { m_renderName.render( renderer, volume, localToWorld, selected ); } } @@ -260,8 +260,10 @@ void revertTransform(){ void freezeTransform(){ m_originKey.m_origin = m_origin; m_originKey.write( &m_entity ); - m_anglesKey.m_angles = m_angles; - m_anglesKey.write( &m_entity ); + if( m_anglesKey.m_angles != m_angles ){ + m_anglesKey.m_angles = m_angles; + m_anglesKey.write( &m_entity ); + } } void transformChanged(){ revertTransform(); @@ -338,7 +340,9 @@ void testSelect( Selector& selector, SelectionTest& test ){ void evaluateTransform(){ if ( getType() == TRANSFORM_PRIMITIVE ) { m_contained.translate( getTranslation() ); - m_contained.rotate( getRotation() ); + if( getRotation() != c_quaternion_identity ){ + m_contained.rotate( getRotation() ); + } } } void applyTransform(){ diff --git a/plugins/entity/group.cpp b/plugins/entity/group.cpp index 97c823b1..78d507a2 100644 --- a/plugins/entity/group.cpp +++ b/plugins/entity/group.cpp @@ -150,7 +150,7 @@ void detach( scene::Traversable::Observer* observer ){ void renderSolid( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected, bool childSelected, const AABB& childBounds ) const { renderer.SetState( m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly ); - if ( selected || childSelected || ( g_showNames && ( volume.fill() || aabb_fits_view( aabb_for_oriented_aabb( childBounds, volume.GetModelview() ), volume.GetViewport(), g_showNamesRatio ) ) ) ) { + if ( selected || childSelected || ( g_showNames && ( volume.fill() || aabb_fits_view( childBounds, volume.GetModelview(), volume.GetViewport(), g_showNamesRatio ) ) ) ) { // don't draw the name for worldspawn if ( !strcmp( m_entity.getEntityClass().name(), "worldspawn" ) ) { return; diff --git a/plugins/entity/light.cpp b/plugins/entity/light.cpp index 988b20fa..a34d92b1 100644 --- a/plugins/entity/light.cpp +++ b/plugins/entity/light.cpp @@ -1460,7 +1460,7 @@ void renderSolid( Renderer& renderer, const VolumeTest& volume, const Matrix4& l } } - if ( ( selected || ( g_showNames && ( volume.fill() || aabb_fits_view( aabb_for_oriented_aabb( m_aabb_light, volume.GetModelview() ), volume.GetViewport(), g_showNamesRatio ) ) ) ) && !string_equal( m_named.name(), "light" ) ) { + if ( ( selected || ( g_showNames && ( volume.fill() || aabb_fits_view( m_aabb_light, volume.GetModelview(), volume.GetViewport(), g_showNamesRatio ) ) ) ) && !string_equal( m_named.name(), "light" ) ) { m_renderName.render( renderer, volume, localToWorld, selected ); } } diff --git a/plugins/entity/miscmodel.cpp b/plugins/entity/miscmodel.cpp index 06949bab..79ad58a2 100644 --- a/plugins/entity/miscmodel.cpp +++ b/plugins/entity/miscmodel.cpp @@ -195,7 +195,7 @@ void renderSolid( Renderer& renderer, const VolumeTest& volume, const Matrix4& l m_renderOrigin.render( renderer, volume, localToWorld ); } renderer.SetState( m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly ); - if ( ( selected || ( g_showNames && ( volume.fill() || aabb_fits_view( aabb_for_oriented_aabb( AABB( Vector3( 0, 0, 0 ), Vector3( 32, 32, 32 ) ), volume.GetModelview() ), volume.GetViewport(), g_showNamesRatio ) ) ) ) && !string_equal( m_named.name(), "misc_model" ) ) { + if ( ( selected || ( g_showNames && ( volume.fill() || aabb_fits_view( AABB( Vector3( 0, 0, 0 ), Vector3( 32, 32, 32 ) ), volume.GetModelview(), volume.GetViewport(), g_showNamesRatio ) ) ) ) && !string_equal( m_named.name(), "misc_model" ) ) { m_renderName.render( renderer, volume, localToWorld, selected ); } } @@ -207,7 +207,7 @@ void translate( const Vector3& translation ){ m_origin = origin_translated( m_origin, translation ); } void rotate( const Quaternion& rotation ){ - m_angles = angles_rotated( m_angles, rotation ); + m_angles = angles_rotated_for_rotated_pivot( m_angles, rotation ); } void scale( const Vector3& scaling ){ m_scale = scale_scaled( m_scale, scaling ); @@ -224,8 +224,10 @@ void revertTransform(){ void freezeTransform(){ m_originKey.m_origin = m_origin; m_originKey.write( &m_entity ); - m_anglesKey.m_angles = m_angles; - m_anglesKey.write( &m_entity ); + if( m_anglesKey.m_angles != m_angles ){ + m_anglesKey.m_angles = m_angles; + m_anglesKey.write( &m_entity ); + } m_scaleKey.m_scale = m_scale; m_scaleKey.write( &m_entity ); } @@ -280,7 +282,9 @@ void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const { void evaluateTransform(){ if ( getType() == TRANSFORM_PRIMITIVE ) { m_contained.translate( getTranslation() ); - m_contained.rotate( getRotation() ); + if( getRotation() != c_quaternion_identity ){ + m_contained.rotate( getRotation() ); + } m_contained.scale( getScale() ); } } diff --git a/plugins/entity/origin.h b/plugins/entity/origin.h index 8628c3cf..18efbf15 100644 --- a/plugins/entity/origin.h +++ b/plugins/entity/origin.h @@ -40,17 +40,20 @@ inline void read_origin( Vector3& origin, const char* value ){ } inline void write_origin( const Vector3& origin, Entity* entity, const char* key ){ char value[64]; - sprintf( value, "%f %f %f", origin[0], origin[1], origin[2] ); + sprintf( value, "%g %g %g", origin[0], origin[1], origin[2] ); entity->setKeyValue( key, value ); } inline Vector3 origin_translated( const Vector3& origin, const Vector3& translation ){ - return matrix4_get_translation_vec3( - matrix4_multiplied_by_matrix4( - matrix4_translation_for_vec3( origin ), - matrix4_translation_for_vec3( translation ) - ) - ); + return vector3_snapped( + matrix4_get_translation_vec3( + matrix4_multiplied_by_matrix4( + matrix4_translation_for_vec3( origin ), + matrix4_translation_for_vec3( translation ) + ) + ), + 1e-4 + ); } inline Vector3 origin_snapped( const Vector3& origin, float snap ){ diff --git a/plugins/entity/scale.h b/plugins/entity/scale.h index f4cb726c..9d2759c1 100644 --- a/plugins/entity/scale.h +++ b/plugins/entity/scale.h @@ -62,13 +62,13 @@ inline void write_scale( const Vector3& scale, Entity* entity ){ char value[64]; if ( scale[0] == scale[1] && scale[0] == scale[2] ) { - sprintf( value, "%f", scale[0] ); + sprintf( value, "%g", scale[0] ); entity->setKeyValue( "modelscale_vec", "" ); entity->setKeyValue( "modelscale", value ); } else { - sprintf( value, "%f %f %f", scale[0], scale[1], scale[2] ); + sprintf( value, "%g %g %g", scale[0], scale[1], scale[2] ); entity->setKeyValue( "modelscale", "" ); entity->setKeyValue( "modelscale_vec", value ); } diff --git a/plugins/mapq3/write.cpp b/plugins/mapq3/write.cpp index f7becfee..6c427160 100644 --- a/plugins/mapq3/write.cpp +++ b/plugins/mapq3/write.cpp @@ -70,7 +70,8 @@ bool pre( scene::Node& node ) const { Entity* entity = Node_getEntity( node ); if ( entity != 0 ) { - if( entity->isContainer() && Node_getTraversable( node )->empty() && !string_equal( entity->getKeyValue( "classname" ), "worldspawn" ) ){ + if( entity->isContainer() && Node_getTraversable( node )->empty() && !string_equal( entity->getKeyValue( "classname" ), "worldspawn" ) + && string_empty( entity->getKeyValue( "origin" ) ) ){ globalErrorStream() << "discarding empty group entity: # = " << g_count_entities << "; classname = " << entity->getKeyValue( "classname" ) << "\n"; return false; } diff --git a/radiant/entity.cpp b/radiant/entity.cpp index f1e4e471..75abd75b 100644 --- a/radiant/entity.cpp +++ b/radiant/entity.cpp @@ -93,7 +93,18 @@ void post( const scene::Path& path, scene::Instance& instance ) const { Entity* entity = Node_getEntity( path.top() ); if ( entity != 0 && ( instance.childSelected() || Instance_getSelectable( instance )->isSelected() ) ) { - NodeSmartReference node( GlobalEntityCreator().createEntity( GlobalEntityClassManager().findOrInsert( m_classname, node_is_group( path.top() ) ) ) ); + if( string_equal_nocase( entity->getKeyValue( "classname" ), "worldspawn" ) ){ + globalErrorStream() << "do not want to convert worldspawn entity\n"; + return; + } + + EntityClass* eclass = GlobalEntityClassManager().findOrInsert( m_classname, node_is_group( path.top() ) ); + if( !eclass->fixedsize && !entity->isContainer() ){ + globalErrorStream() << "can't convert point to group entity\n"; + return; + } + //NodeSmartReference node( GlobalEntityCreator().createEntity( GlobalEntityClassManager().findOrInsert( m_classname, node_is_group( path.top() ) ) ) ); + NodeSmartReference node( GlobalEntityCreator().createEntity( eclass ) ); EntityCopyingVisitor visitor( *Node_getEntity( node ) );