Skip to content

Commit

Permalink
Added controls for tone mapping and light source color temperature
Browse files Browse the repository at this point in the history
  • Loading branch information
fo76utils committed Mar 31, 2024
1 parent ce42fcb commit 97c230b
Show file tree
Hide file tree
Showing 21 changed files with 122 additions and 105 deletions.
Binary file removed res/icon/hue.png
Binary file not shown.
Binary file added res/icon/lightcolor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed res/icon/saturation.png
Binary file not shown.
Binary file added res/icon/tonemap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions res/nifskope.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
<file alias="sun">icon/sun.png</file>
<file alias="cloud">icon/cloud.png</file>
<file alias="light">icon/light.png</file>
<file alias="hue">icon/hue.png</file>
<file alias="saturation">icon/saturation.png</file>
<file alias="lightcolor">icon/lightcolor.png</file>
<file alias="tonemap">icon/tonemap.png</file>
<file alias="cubemap">icon/cubemap.png</file>
<file alias="hidden">icon/hidden.png</file>
<file alias="hiddenDisabled">icon/hidden-disabled.png</file>
Expand Down
22 changes: 12 additions & 10 deletions res/shaders/f76_default.frag
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,18 @@ float OrenNayarFull(vec3 L, vec3 V, vec3 N, float roughness, float NdotL0)
return L1 + L2;
}

vec3 tonemap(vec3 x)
vec3 tonemap(vec3 x, float y)
{
float _A = 0.15;
float _B = 0.50;
float _C = 0.10;
float _D = 0.20;
float _E = 0.02;
float _F = 0.30;

return ((x*(_A*x+_C*_B)+_D*_E)/(x*(_A*x+_B)+_D*_F))-_E/_F;
float a = 0.15;
float b = 0.50;
float c = 0.10;
float d = 0.20;
float e = 0.02;
float f = 0.30;

vec3 z = x * (y * 4.22978723);
z = (z * (a * z + b * c) + d * e) / (z * (a * z + b) + d * f) - e / f;
return z / (y * 0.93333333);
}

float srgbCompress(float x)
Expand Down Expand Up @@ -293,7 +295,7 @@ void main(void)
// Emissive
color.rgb += emissive;

color.rgb = tonemap(color.rgb * D.a) / tonemap(vec3(1.0));
color.rgb = tonemap(color.rgb * D.a, A.a);
color.a = C.a * baseMap.a;

gl_FragColor = color;
Expand Down
3 changes: 1 addition & 2 deletions res/shaders/f76_default.vert
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ void main( void )
ViewDir = -v.xyz;
LightDir = gl_LightSource[0].position.xyz;

A = gl_LightSource[0].ambient;
A.rgb *= 1.2;
A = vec4(gl_LightSource[0].ambient.rgb * 1.2, gl_LightSource[0].ambient.a);
C = gl_Color;
D = gl_LightSource[0].diffuse;
}
22 changes: 12 additions & 10 deletions res/shaders/fo4_default.frag
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,18 @@ vec3 TorranceSparrow(float NdotL, float NdotH, float NdotV, float VdotH, vec3 co
return color * spec * M_PI;
}

vec3 tonemap(vec3 x)
vec3 tonemap(vec3 x, float y)
{
float _A = 0.15;
float _B = 0.50;
float _C = 0.10;
float _D = 0.20;
float _E = 0.02;
float _F = 0.30;

return ((x*(_A*x+_C*_B)+_D*_E)/(x*(_A*x+_B)+_D*_F))-_E/_F;
float a = 0.15;
float b = 0.50;
float c = 0.10;
float d = 0.20;
float e = 0.02;
float f = 0.30;

vec3 z = x * (y * 4.22978723);
z = (z * (a * z + b * c) + d * e) / (z * (a * z + b) + d * f) - e / f;
return z / (y * 0.93333333);
}

vec4 colorLookup( float x, float y ) {
Expand Down Expand Up @@ -328,7 +330,7 @@ void main( void )
// Emissive
color.rgb += emissive;

color.rgb = tonemap( color.rgb * D.a ) / tonemap( vec3(1.0) );
color.rgb = tonemap( color.rgb * D.a, A.a );
color.a = C.a * baseMap.a;

gl_FragColor = color;
Expand Down
2 changes: 1 addition & 1 deletion res/shaders/fo4_default.vert
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void main( void )
ViewDir = -v.xyz;
LightDir = gl_LightSource[0].position.xyz;

A = sqrt(gl_LightSource[0].ambient) * 0.375;
A = vec4(sqrt(gl_LightSource[0].ambient.rgb) * 0.375, gl_LightSource[0].ambient.a);
C = gl_Color;
D = sqrt(gl_LightSource[0].diffuse);
}
2 changes: 1 addition & 1 deletion res/shaders/fo4_effectshader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void main( void )
ViewDir = tbnMatrix * -v.xyz;
LightDir = tbnMatrix * gl_LightSource[0].position.xyz;

A = sqrt(gl_LightSource[0].ambient) * 0.375;
A = vec4(sqrt(gl_LightSource[0].ambient.rgb) * 0.375, gl_LightSource[0].ambient.a);
C = gl_Color;
D = sqrt(gl_LightSource[0].diffuse);
}
22 changes: 12 additions & 10 deletions res/shaders/sk_default.frag
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,18 @@ in vec4 D;
in mat3 tbnMatrix;


vec3 tonemap(vec3 x)
vec3 tonemap(vec3 x, float y)
{
float _A = 0.15;
float _B = 0.50;
float _C = 0.10;
float _D = 0.20;
float _E = 0.02;
float _F = 0.30;

return ((x*(_A*x+_C*_B)+_D*_E)/(x*(_A*x+_B)+_D*_F))-_E/_F;
float a = 0.15;
float b = 0.50;
float c = 0.10;
float d = 0.20;
float e = 0.02;
float f = 0.30;

vec3 z = x * (y * 4.22978723);
z = (z * (a * z + b * c) + d * e) / (z * (a * z + b) + d * f) - e / f;
return z / (y * 0.93333333);
}

vec3 toGrayscale(vec3 color)
Expand Down Expand Up @@ -168,7 +170,7 @@ void main( void )
}

color.rgb = albedo * (diffuse + emissive) + spec;
color.rgb = tonemap( color.rgb * D.a ) / tonemap( vec3(1.0) );
color.rgb = tonemap( color.rgb * D.a, A.a );
color.a = C.a * baseMap.a;

gl_FragColor = color;
Expand Down
2 changes: 1 addition & 1 deletion res/shaders/sk_default.vert
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void main( void )
ViewDir = -v.xyz;
LightDir = gl_LightSource[0].position.xyz;

A = sqrt(gl_LightSource[0].ambient) * 0.375;
A = vec4(sqrt(gl_LightSource[0].ambient.rgb) * 0.375, gl_LightSource[0].ambient.a);
C = gl_Color;
D = sqrt(gl_LightSource[0].diffuse);
}
22 changes: 12 additions & 10 deletions res/shaders/sk_msn.frag
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,18 @@ varying vec4 C;
varying vec4 D;


vec3 tonemap(vec3 x)
vec3 tonemap(vec3 x, float y)
{
float _A = 0.15;
float _B = 0.50;
float _C = 0.10;
float _D = 0.20;
float _E = 0.02;
float _F = 0.30;

return ((x*(_A*x+_C*_B)+_D*_E)/(x*(_A*x+_B)+_D*_F))-_E/_F;
float a = 0.15;
float b = 0.50;
float c = 0.10;
float d = 0.20;
float e = 0.02;
float f = 0.30;

vec3 z = x * (y * 4.22978723);
z = (z * (a * z + b * c) + d * e) / (z * (a * z + b) + d * f) - e / f;
return z / (y * 0.93333333);
}

vec3 toGrayscale(vec3 color)
Expand Down Expand Up @@ -186,7 +188,7 @@ void main( void )
}

color.rgb = albedo * (diffuse + emissive) + spec;
color.rgb = tonemap( color.rgb * D.a ) / tonemap( vec3(1.0) );
color.rgb = tonemap( color.rgb * D.a, A.a );
color.a = C.a * baseMap.a;

gl_FragColor = color;
Expand Down
2 changes: 1 addition & 1 deletion res/shaders/sk_msn.vert
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void main( void )
ViewDir = -v.xyz;
LightDir = gl_LightSource[0].position.xyz;

A = sqrt(gl_LightSource[0].ambient) * 0.375;
A = vec4(sqrt(gl_LightSource[0].ambient.rgb) * 0.375, gl_LightSource[0].ambient.a);
C = gl_Color;
D = sqrt(gl_LightSource[0].diffuse);
}
22 changes: 12 additions & 10 deletions res/shaders/sk_multilayer.frag
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,18 @@ varying vec4 D;
varying mat3 tbnMatrix;


vec3 tonemap(vec3 x)
vec3 tonemap(vec3 x, float y)
{
float _A = 0.15;
float _B = 0.50;
float _C = 0.10;
float _D = 0.20;
float _E = 0.02;
float _F = 0.30;

return ((x*(_A*x+_C*_B)+_D*_E)/(x*(_A*x+_B)+_D*_F))-_E/_F;
float a = 0.15;
float b = 0.50;
float c = 0.10;
float d = 0.20;
float e = 0.02;
float f = 0.30;

vec3 z = x * (y * 4.22978723);
z = (z * (a * z + b * c) + d * e) / (z * (a * z + b) + d * f) - e / f;
return z / (y * 0.93333333);
}

vec3 toGrayscale(vec3 color)
Expand Down Expand Up @@ -204,7 +206,7 @@ void main( void )
}

color.rgb = albedo * (diffuse + emissive) + spec;
color.rgb = tonemap( color.rgb * D.a ) / tonemap( vec3(1.0) );
color.rgb = tonemap( color.rgb * D.a, A.a );
color.a = C.a * baseMap.a;

gl_FragColor = color;
Expand Down
22 changes: 12 additions & 10 deletions res/shaders/stf_default.frag
Original file line number Diff line number Diff line change
Expand Up @@ -342,16 +342,18 @@ float OrenNayarFull(vec3 L, vec3 V, vec3 N, float roughness, float NdotL0)
return L1 + L2;
}

vec3 tonemap(vec3 x)
vec3 tonemap(vec3 x, float y)
{
float _A = 0.15;
float _B = 0.50;
float _C = 0.10;
float _D = 0.20;
float _E = 0.02;
float _F = 0.30;

return ((x*(_A*x+_C*_B)+_D*_E)/(x*(_A*x+_B)+_D*_F))-_E/_F;
float a = 0.15;
float b = 0.50;
float c = 0.10;
float d = 0.20;
float e = 0.02;
float f = 0.30;

vec3 z = x * (y * 4.22978723);
z = (z * (a * z + b * c) + d * e) / (z * (a * z + b) + d * f) - e / f;
return z / (y * 0.93333333);
}

vec2 getTexCoord(in UVStream uvStream)
Expand Down Expand Up @@ -626,7 +628,7 @@ void main(void)
// Emissive
color.rgb += emissive;

color.rgb = tonemap(color.rgb * D.a) / tonemap(vec3(1.0));
color.rgb = tonemap(color.rgb * D.a, A.a);
color.a = baseMap.a * alpha;

fragColor = color;
Expand Down
36 changes: 21 additions & 15 deletions src/glview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "ui/settingsdialog.h"
#include "ui/widgets/fileselect.h"
#include "gamemanager.h"
#include "libfo76utils/src/fp32vec4.hpp"

#include <QApplication>
#include <QActionGroup>
Expand Down Expand Up @@ -613,15 +614,21 @@ void GLView::paintGL()
if ( scene->hasVisMode(Scene::VisNormalsOnly) && scene->hasOption(Scene::DoTexturing) && !scene->hasOption(Scene::DisableShaders) ) {
amb = 0.1f;
}

GLfloat mat_amb[] = { amb, amb, amb, 1.0f };
float brightnessR = float( std::cos((brightnessH < 0.5f ? brightnessH : brightnessH - 1.0f) * 4.71238898f) );
float brightnessG = float( std::cos((brightnessH - 0.33333333f) * 4.71238898f) );
float brightnessB = float( std::cos((brightnessH - 0.66666667f) * 4.71238898f) );
brightnessR = ( (std::max(brightnessR, 0.0f) * brightnessR - 1.0f) * brightnessS + 1.0f ) * brightnessL;
brightnessG = ( (std::max(brightnessG, 0.0f) * brightnessG - 1.0f) * brightnessS + 1.0f ) * brightnessL;
brightnessB = ( (std::max(brightnessB, 0.0f) * brightnessB - 1.0f) * brightnessS + 1.0f ) * brightnessL;
GLfloat mat_diff[] = { brightnessR, brightnessG, brightnessB, brightnessScale };
GLfloat mat_amb[] = { amb, amb, amb, toneMapping };

const FloatVector4 a6( 0.02760699f, -0.02760716f, -0.89546080f, 0.0f );
const FloatVector4 a5( 0.17419180f, -0.17419179f, 1.39674540f, 0.0f );
const FloatVector4 a4( -0.31251446f, 0.31251526f, 0.88845913f, 0.0f );
const FloatVector4 a3( -0.19438881f, 0.19438865f, -2.36482405f, 0.0f );
const FloatVector4 a2( 0.68779535f, -0.68779591f, -0.04841438f, 0.0f );
const FloatVector4 a1( -0.57691451f, 0.57691471f, 2.42385767f, 0.0f );
const FloatVector4 a0( 1.0f );
FloatVector4 c( lightColor );
c = ( ( ( ( (c * a6 + a5) * c + a4 ) * c + a3 ) * c + a2 ) * c + a1 ) * c + a0;
c = c / std::max( c[0], std::max(c[1], c[2]) );
c.maxValues( FloatVector4(0.0f) ).minValues( FloatVector4(1.0f) );
c *= brightnessL;
GLfloat mat_diff[] = { c[0], c[1], c[2], brightnessScale };


glShadeModel( GL_SMOOTH );
Expand Down Expand Up @@ -816,17 +823,16 @@ void GLView::setLightLevel( int value )
update();
}

void GLView::setLightHue( int value )
void GLView::setLightColor( int value )
{
brightnessH = float( value ) / 1440.0f;
brightnessH = brightnessH - float( std::floor(brightnessH) );
// color temperature on a logarithmic scale
lightColor = float( value ) / 720.0f - 1.0f;
update();
}

void GLView::setLightSaturation( int value )
void GLView::setToneMapping( int value )
{
brightnessS = std::min( std::max(float(value / 1440.0f), 0.0f), 1.0f );
brightnessS = brightnessS * ( 2.0f - brightnessS );
toneMapping = float( std::pow( 4.22978723f, float( value - 1440 ) / 720.0f ) );
update();
}

Expand Down
8 changes: 4 additions & 4 deletions src/glview.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ class GLView final : public QGLWidget
float brightnessScale = 1.0f; // overall brightness
float ambient = 1.0f; // environment map / ambient light level
float brightnessL = 1.0f; // directional light intensity,
float brightnessH = 0.0f; // hue (0.0 to 1.0) and saturation
float brightnessS = 0.0f;
float lightColor = 0.0f; // and color temperature (-1.0 to 1.0)
float toneMapping = 0.23641851f; // 0.05 to 1.0
float declination = 0.0f;
float planarAngle = 0.0f;
bool frontalLight = true;
Expand Down Expand Up @@ -167,8 +167,8 @@ public slots:
void loadUserView();
void setBrightness( int );
void setLightLevel( int );
void setLightHue( int );
void setLightSaturation( int );
void setLightColor( int );
void setToneMapping( int );
void setAmbient( int );
void setDeclination( int );
void setPlanarAngle( int );
Expand Down
12 changes: 6 additions & 6 deletions src/ui/widgets/lightingwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ LightingWidget::LightingWidget( GLView * ogl, QWidget * parent ) : QWidget(paren
// Inform ogl of changes
connect( ui->sldDirectional, &QSlider::valueChanged, ogl, &GLView::setBrightness );
connect( ui->sldLightLevel, &QSlider::valueChanged, ogl, &GLView::setLightLevel );
connect( ui->sldLightHue, &QSlider::valueChanged, ogl, &GLView::setLightHue );
connect( ui->sldLightSaturation, &QSlider::valueChanged, ogl, &GLView::setLightSaturation );
connect( ui->sldLightColor, &QSlider::valueChanged, ogl, &GLView::setLightColor );
connect( ui->sldToneMapping, &QSlider::valueChanged, ogl, &GLView::setToneMapping );
connect( ui->sldAmbient, &QSlider::valueChanged, ogl, &GLView::setAmbient );
connect( ui->sldDeclination, &QSlider::valueChanged, ogl, &GLView::setDeclination );
connect( ui->sldPlanarAngle, &QSlider::valueChanged, ogl, &GLView::setPlanarAngle );
Expand All @@ -63,10 +63,10 @@ void LightingWidget::setDefaults()
sld( ui->sldDeclination, DeclinationMin, DeclinationMax, DeclinationDefault );
sld( ui->sldPlanarAngle, PlanarAngleMin, PlanarAngleMax, PlanarAngleDefault );
sld( ui->sldLightLevel, LightLevelMin, LightLevelMax, LightLevelDefault );
sld( ui->sldLightHue, LightHueMin, LightHueMax, LightHueDefault );
ui->sldLightHue->setSingleStep( LightHueMax / 12 );
ui->sldLightHue->setTickInterval( LightHueMax / 3 );
sld( ui->sldLightSaturation, LightSaturationMin, LightSaturationMax, LightSaturationDefault );
sld( ui->sldLightColor, LightColorMin, LightColorMax, LightColorDefault );
ui->sldLightColor->setSingleStep( LightColorMax / 16 );
ui->sldLightColor->setTickInterval( LightColorMax / 8 );
sld( ui->sldToneMapping, ToneMappingMin, ToneMappingMax, ToneMappingDefault );
}

void LightingWidget::setActions( QVector<QAction *> atns )
Expand Down
Loading

0 comments on commit 97c230b

Please sign in to comment.