diff --git a/libs/math/vector.h b/libs/math/vector.h index d5272821..e42b76ce 100644 --- a/libs/math/vector.h +++ b/libs/math/vector.h @@ -587,6 +587,16 @@ inline std::size_t vector3_min_abs_component_index( const BasicVector3& return ( fabs( self[2] ) < fabs( self[mini] ) )? 2 : mini; } +template +inline Element vector3_max_component( const BasicVector3& v ){ + return ( v[0] > v[1] ) ? ( ( v[0] > v[2] ) ? v[0] : v[2] ) : ( ( v[1] > v[2] ) ? v[1] : v[2] ); +} + +template +inline Element vector3_min_component( const BasicVector3& v ){ + return ( v[0] < v[1] ) ? ( ( v[0] < v[2] ) ? v[0] : v[2] ) : ( ( v[1] < v[2] ) ? v[1] : v[2] ); +} + diff --git a/plugins/entity/colour.h b/plugins/entity/colour.h index bc7b381a..8b52ad5e 100644 --- a/plugins/entity/colour.h +++ b/plugins/entity/colour.h @@ -37,6 +37,18 @@ inline void read_colour( Vector3& colour, const char* value ){ default_colour( colour ); } } +inline void read_colour_normalized( Vector3& colour, const char* value ){ + if ( !string_parse_vector3( value, colour ) ) { + default_colour( colour ); + } + else{ + const auto max = vector3_max_component( colour ); + if ( max == 0 ) + default_colour( colour ); + else + colour /= max; + } +} inline void write_colour( const Vector3& colour, Entity* entity ){ char value[64]; @@ -52,19 +64,21 @@ class Colour void capture_state(){ m_state = colour_capture_state_fill( m_colour ); - m_state_additive = colour_capture_state_add( m_colour ); + m_state_additive = colour_capture_state_add( m_colour_add ); } void release_state(){ colour_release_state_fill( m_colour ); - colour_release_state_add( m_colour ); + colour_release_state_add( m_colour_add ); } + Vector3 m_colour_add; public: Vector3 m_colour; Colour( const Callback& colourChanged ) : m_colourChanged( colourChanged ){ default_colour( m_colour ); + m_colour_add = m_colour / 4; capture_state(); } ~Colour(){ @@ -73,7 +87,8 @@ public: void colourChanged( const char* value ){ release_state(); - read_colour( m_colour, value ); + read_colour_normalized( m_colour, value ); + m_colour_add = m_colour / 4; capture_state(); m_colourChanged(); diff --git a/radiant/entity.cpp b/radiant/entity.cpp index 40be6e26..d9509d27 100644 --- a/radiant/entity.cpp +++ b/radiant/entity.cpp @@ -509,13 +509,11 @@ void Entity_ungroupSelectedPrimitives(){ /* scale color so that at least one component is at 1.0F */ void NormalizeColor( Vector3& color ){ - const std::size_t maxi = vector3_max_abs_component_index( color ); - if ( color[maxi] == 0.f ) + const auto max = vector3_max_component( color ); + if ( max == 0 ) color = Vector3( 1, 1, 1 ); - else{ - const float max = color[maxi]; + else color /= max; - } } void Entity_normalizeColor(){ diff --git a/tools/quake3/common/qmath.h b/tools/quake3/common/qmath.h index 1df21138..4a5c2983 100644 --- a/tools/quake3/common/qmath.h +++ b/tools/quake3/common/qmath.h @@ -162,11 +162,6 @@ inline bool VectorCompare( const Vector3& v1, const Vector3& v2 ){ return vector3_equal_epsilon( v1, v2, EQUAL_EPSILON ); } -template -T VectorMax( const BasicVector3& v ){ - return ( v[0] > v[1] ) ? ( ( v[0] > v[2] ) ? v[0] : v[2] ) : ( ( v[1] > v[2] ) ? v[1] : v[2] ); -} - inline bool VectorIsOnAxis( const Vector3& v ){ int zeroComponentCount = 0; for ( int i = 0; i < 3; ++i ) @@ -349,7 +344,7 @@ inline EPlaneType PlaneTypeForNormal( const Vector3& normal ) { inline void ColorNormalize( Vector3& color ) { - const float max = VectorMax( color ); + const float max = vector3_max_component( color ); if ( max == 0 ) { color.set( 1 ); diff --git a/tools/quake3/q3map2/light_ydnar.cpp b/tools/quake3/q3map2/light_ydnar.cpp index f21c4138..de799ae6 100644 --- a/tools/quake3/q3map2/light_ydnar.cpp +++ b/tools/quake3/q3map2/light_ydnar.cpp @@ -88,7 +88,7 @@ Vector3b ColorToBytes( const Vector3& color, float scale ){ if ( lightmapExposure == 0 ) { /* clamp with color normalization */ - max = VectorMax( sample ); + max = vector3_max_component( sample ); if ( max > maxLight ) { sample *= ( maxLight / max ); } @@ -98,7 +98,7 @@ Vector3b ColorToBytes( const Vector3& color, float scale ){ inv = 1.f / lightmapExposure; //Exposure - max = VectorMax( sample ); + max = vector3_max_component( sample ); dif = ( 1 - exp( -max * inv ) ) * 255; @@ -123,7 +123,7 @@ Vector3b ColorToBytes( const Vector3& color, float scale ){ sample[i] = std::max( 0.f, lightmapContrast * ( sample[i] - 128 ) + 128 ); } /* clamp with color normalization */ - max = VectorMax( sample ); + max = vector3_max_component( sample ); if ( max > 255.0f ) { sample *= ( 255.0f / max ); }