normalize rendered light color for display consistency (compiler normalizes it anyway)

lower 3d light sphere brightness to reduce occlusion
#63
This commit is contained in:
Garux 2022-12-17 02:15:38 +06:00
parent d41adc5e93
commit 88a03b07be
5 changed files with 35 additions and 17 deletions

View File

@ -587,6 +587,16 @@ inline std::size_t vector3_min_abs_component_index( const BasicVector3<Element>&
return ( fabs( self[2] ) < fabs( self[mini] ) )? 2 : mini; return ( fabs( self[2] ) < fabs( self[mini] ) )? 2 : mini;
} }
template<typename Element>
inline Element vector3_max_component( const BasicVector3<Element>& v ){
return ( v[0] > v[1] ) ? ( ( v[0] > v[2] ) ? v[0] : v[2] ) : ( ( v[1] > v[2] ) ? v[1] : v[2] );
}
template<typename Element>
inline Element vector3_min_component( const BasicVector3<Element>& v ){
return ( v[0] < v[1] ) ? ( ( v[0] < v[2] ) ? v[0] : v[2] ) : ( ( v[1] < v[2] ) ? v[1] : v[2] );
}

View File

@ -37,6 +37,18 @@ inline void read_colour( Vector3& colour, const char* value ){
default_colour( colour ); 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 ){ inline void write_colour( const Vector3& colour, Entity* entity ){
char value[64]; char value[64];
@ -52,19 +64,21 @@ class Colour
void capture_state(){ void capture_state(){
m_state = colour_capture_state_fill( m_colour ); 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(){ void release_state(){
colour_release_state_fill( m_colour ); colour_release_state_fill( m_colour );
colour_release_state_add( m_colour ); colour_release_state_add( m_colour_add );
} }
Vector3 m_colour_add;
public: public:
Vector3 m_colour; Vector3 m_colour;
Colour( const Callback& colourChanged ) Colour( const Callback& colourChanged )
: m_colourChanged( colourChanged ){ : m_colourChanged( colourChanged ){
default_colour( m_colour ); default_colour( m_colour );
m_colour_add = m_colour / 4;
capture_state(); capture_state();
} }
~Colour(){ ~Colour(){
@ -73,7 +87,8 @@ public:
void colourChanged( const char* value ){ void colourChanged( const char* value ){
release_state(); release_state();
read_colour( m_colour, value ); read_colour_normalized( m_colour, value );
m_colour_add = m_colour / 4;
capture_state(); capture_state();
m_colourChanged(); m_colourChanged();

View File

@ -509,14 +509,12 @@ void Entity_ungroupSelectedPrimitives(){
/* scale color so that at least one component is at 1.0F */ /* scale color so that at least one component is at 1.0F */
void NormalizeColor( Vector3& color ){ void NormalizeColor( Vector3& color ){
const std::size_t maxi = vector3_max_abs_component_index( color ); const auto max = vector3_max_component( color );
if ( color[maxi] == 0.f ) if ( max == 0 )
color = Vector3( 1, 1, 1 ); color = Vector3( 1, 1, 1 );
else{ else
const float max = color[maxi];
color /= max; color /= max;
} }
}
void Entity_normalizeColor(){ void Entity_normalizeColor(){
if ( GlobalSelectionSystem().countSelected() != 0 ) { if ( GlobalSelectionSystem().countSelected() != 0 ) {

View File

@ -162,11 +162,6 @@ inline bool VectorCompare( const Vector3& v1, const Vector3& v2 ){
return vector3_equal_epsilon( v1, v2, EQUAL_EPSILON ); return vector3_equal_epsilon( v1, v2, EQUAL_EPSILON );
} }
template<typename T>
T VectorMax( const BasicVector3<T>& 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 ){ inline bool VectorIsOnAxis( const Vector3& v ){
int zeroComponentCount = 0; int zeroComponentCount = 0;
for ( int i = 0; i < 3; ++i ) for ( int i = 0; i < 3; ++i )
@ -349,7 +344,7 @@ inline EPlaneType PlaneTypeForNormal( const Vector3& normal ) {
inline void ColorNormalize( Vector3& color ) { inline void ColorNormalize( Vector3& color ) {
const float max = VectorMax( color ); const float max = vector3_max_component( color );
if ( max == 0 ) { if ( max == 0 ) {
color.set( 1 ); color.set( 1 );

View File

@ -88,7 +88,7 @@ Vector3b ColorToBytes( const Vector3& color, float scale ){
if ( lightmapExposure == 0 ) { if ( lightmapExposure == 0 ) {
/* clamp with color normalization */ /* clamp with color normalization */
max = VectorMax( sample ); max = vector3_max_component( sample );
if ( max > maxLight ) { if ( max > maxLight ) {
sample *= ( maxLight / max ); sample *= ( maxLight / max );
} }
@ -98,7 +98,7 @@ Vector3b ColorToBytes( const Vector3& color, float scale ){
inv = 1.f / lightmapExposure; inv = 1.f / lightmapExposure;
//Exposure //Exposure
max = VectorMax( sample ); max = vector3_max_component( sample );
dif = ( 1 - exp( -max * inv ) ) * 255; 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 ); sample[i] = std::max( 0.f, lightmapContrast * ( sample[i] - 128 ) + 128 );
} }
/* clamp with color normalization */ /* clamp with color normalization */
max = VectorMax( sample ); max = vector3_max_component( sample );
if ( max > 255.0f ) { if ( max > 255.0f ) {
sample *= ( 255.0f / max ); sample *= ( 255.0f / max );
} }