Skip to content

Commit

Permalink
Visual adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
yvt committed Feb 18, 2017
1 parent 84e1292 commit 6279c9e
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 110 deletions.
15 changes: 9 additions & 6 deletions Resources/Shaders/PostFilters/AutoExposure.fs
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
/*
Copyright (c) 2015 yvt
This file is part of OpenSpades.
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/

// This shader computes the gain of the auto exposure.

uniform sampler2D mainTexture;

uniform float minGain;
uniform float maxGain;

varying vec4 color;

void main() {
Expand All @@ -34,7 +37,7 @@ void main() {
// weaken the effect
// brightness = mix(brightness, 1., 0.05);

gl_FragColor.xyz = vec3(.8 / brightness);
gl_FragColor.xyz = vec3(clamp(.6 / brightness, minGain, maxGain));
gl_FragColor.w = color.w;
}

31 changes: 17 additions & 14 deletions Resources/Shaders/PostFilters/ColorCorrection.fs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/*
Copyright (c) 2013 yvt
This file is part of OpenSpades.
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/


Expand All @@ -27,23 +27,26 @@ uniform float enhancement;
uniform float saturation;
uniform vec3 tint;

vec3 acesToneMapping(vec3 x)
{
return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0);
}

void main() {

// Input is in the device color space
gl_FragColor = texture2D(mainTexture, texCoord);

gl_FragColor.xyz *= tint;

vec3 gray = vec3(dot(gl_FragColor.xyz, vec3(1. / 3.)));
gl_FragColor.xyz = mix(gray, gl_FragColor.xyz, saturation);

#if USE_HDR

vec3 filtered = smoothstep(0., 1., gl_FragColor.xyz);
vec3 temporal = gl_FragColor.xyz - 0.8;
filtered -= exp2(-7. * temporal * temporal) * 0.2;

gl_FragColor.xyz *= gl_FragColor.xyz; // linearize
gl_FragColor.xyz = acesToneMapping(gl_FragColor.xyz * 0.8);
gl_FragColor.xyz = sqrt(gl_FragColor.xyz); // delinearize
gl_FragColor.xyz = mix(gl_FragColor.xyz,
max(filtered, vec3(0.)),
smoothstep(0., 1., gl_FragColor.xyz),
enhancement);
#else
gl_FragColor.xyz = mix(gl_FragColor.xyz,
Expand Down
22 changes: 3 additions & 19 deletions Resources/Shaders/PostFilters/LensDust.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,33 @@


uniform sampler2D blurTexture1;
uniform sampler2D blurTexture2;
uniform sampler2D blurTexture3;
uniform sampler2D blurTexture4;
uniform sampler2D dustTexture;
uniform sampler2D inputTexture;
uniform sampler2D noiseTexture;

varying vec2 texCoord;
varying vec4 dustTexCoord1;
varying vec4 dustTexCoord2;
varying vec2 dustTexCoord;
varying vec4 noiseTexCoord;

void main() {
// dust filter texture
vec3 dust1 = texture2D(dustTexture, dustTexCoord1.xy).xyz;
vec3 dust2 = texture2D(dustTexture, dustTexCoord1.zw).xyz;
vec3 dust3 = texture2D(dustTexture, dustTexCoord2.xy).xyz;
vec3 dust4 = texture2D(dustTexture, dustTexCoord2.zw).xyz;
vec3 dust1 = texture2D(dustTexture, dustTexCoord).xyz;

// linearize
dust1 *= dust1;
dust2 *= dust2;
dust3 *= dust3;
dust4 *= dust4;

// blurred texture (already linearized?)
vec3 blur1 = texture2D(blurTexture1, texCoord).xyz;
vec3 blur2 = texture2D(blurTexture2, texCoord).xyz;
vec3 blur3 = texture2D(blurTexture3, texCoord).xyz;
vec3 blur4 = texture2D(blurTexture4, texCoord).xyz;

vec3 sum = dust1 * blur1;
sum += dust2 * blur2;
sum += dust3 * blur3;
sum += dust4 * blur4;

vec3 final = texture2D(inputTexture, texCoord).xyz;
#if !LINEAR_FRAMEBUFFER
final *= final;
#endif

final *= 0.95;
final += sum * 0.8;
final += sum * 2.0;

// add grain
float grain = texture2D(noiseTexture, noiseTexCoord.xy).x;
Expand Down
33 changes: 12 additions & 21 deletions Resources/Shaders/PostFilters/LensDust.vs
Original file line number Diff line number Diff line change
@@ -1,53 +1,44 @@
/*
Copyright (c) 2013 yvt
This file is part of OpenSpades.
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/



attribute vec2 positionAttribute;

varying vec2 texCoord;
varying vec4 dustTexCoord1;
varying vec4 dustTexCoord2;
varying vec2 dustTexCoord;
varying vec4 noiseTexCoord;

uniform vec4 noiseTexCoordFactor;

void main() {

vec2 pos = positionAttribute;

vec2 scrPos = pos * 2. - 1.;

gl_Position = vec4(scrPos, 0.5, 1.);

texCoord = pos;

vec2 dustCoord = texCoord *
vec2(511. / 1024., 255. / 512.) +
vec2(0.5 / 1024., 0.5 / 512.);
dustCoord.y = 0.5 - dustCoord.y;
dustTexCoord1 = dustCoord.xyxy +
vec4(0.0, 0.0, 0.5, 0.0);
dustTexCoord2 = dustCoord.xyxy +
vec4(0.0, 0.5, 0.5, 0.5);


dustTexCoord = texCoord;
noiseTexCoord = texCoord.xyxy * noiseTexCoordFactor;
}

3 changes: 2 additions & 1 deletion Resources/Shaders/Shadow/MapRadiosity.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ varying vec3 radiosityTextureCoord;
varying vec3 ambientShadowTextureCoord;
varying vec3 normalVarying;
uniform vec3 ambientColor;
uniform vec3 fogColor;

vec3 DecodeRadiosityValue(vec3 val){
// reverse bias
Expand Down Expand Up @@ -98,5 +99,5 @@ vec3 BlurredReflection_Map(float detailAmbientOcclusion, vec3 direction, float s

amb *= .8 - normalVarying.z * .2;

return col + ambientColor * amb;
return col + fogColor * amb;
}
3 changes: 2 additions & 1 deletion Resources/Shaders/Shadow/MapRadiosityLow.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ varying vec3 radiosityTextureCoord;
varying vec3 ambientShadowTextureCoord;
varying vec3 normalVarying;
uniform vec3 ambientColor;
uniform vec3 fogColor;

vec3 DecodeRadiosityValue(vec3 val){
// reverse bias
Expand Down Expand Up @@ -100,5 +101,5 @@ vec3 BlurredReflection_Map(float detailAmbientOcclusion, vec3 direction, float s

amb *= .8 - normalVarying.z * .2;

return col + ambientColor * amb;
return col + fogColor * amb;
}
Binary file added Resources/Textures/LensDustTexture.jpg
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 Resources/Textures/RealLens.jpg
Binary file not shown.
38 changes: 30 additions & 8 deletions Sources/Draw/GLAutoExposureFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "GLQuadRenderer.h"
#include "GLRenderer.h"
#include "IGLDevice.h"
#include "GLSettings.h"

namespace spades {
namespace draw {
Expand Down Expand Up @@ -77,21 +78,23 @@ namespace spades {
dev->DeleteFramebuffer(exposureFramebuffer);
}

#define Level BloomLevel

struct Level {
int w, h;
GLColorBuffer buffer;
};
namespace {
struct Level {
int w, h;
GLColorBuffer buffer;
};
}

GLColorBuffer GLAutoExposureFilter::Filter(GLColorBuffer input) {
GLColorBuffer GLAutoExposureFilter::Filter(GLColorBuffer input, float dt) {
SPADES_MARK_FUNCTION();

std::vector<Level> levels;

IGLDevice *dev = renderer->GetGLDevice();
GLQuadRenderer qr(dev);

GLSettings &settings = renderer->GetSettings();

static GLProgramAttribute thruPosition("positionAttribute");
static GLProgramUniform thruColor("colorUniform");
static GLProgramUniform thruTexture("mainTexture");
Expand All @@ -116,11 +119,15 @@ namespace spades {
static GLProgramUniform computeGainColor("colorUniform");
static GLProgramUniform computeGainTexture("mainTexture");
static GLProgramUniform computeGainTexCoordRange("texCoordRange");
static GLProgramUniform computeGainMinGain("minGain");
static GLProgramUniform computeGainMaxGain("maxGain");

computeGainPosition(computeGain);
computeGainColor(computeGain);
computeGainTexture(computeGain);
computeGainTexCoordRange(computeGain);
computeGainMinGain(computeGain);
computeGainMaxGain(computeGain);

preprocess->Use();
preprocessColor.SetValue(1.f, 1.f, 1.f, 1.f);
Expand Down Expand Up @@ -169,10 +176,25 @@ namespace spades {
dev->Enable(IGLDevice::Blend, true);
dev->BlendFunc(IGLDevice::SrcAlpha, IGLDevice::OneMinusSrcAlpha);

float minExposure = settings.r_hdrAutoExposureMin;
float maxExposure = settings.r_hdrAutoExposureMax;

// safety
minExposure = std::min(std::max(minExposure, -10.0f), 10.0f);
maxExposure = std::min(std::max(maxExposure, minExposure), 10.0f);

// adaption speed control
if ((float)settings.r_hdrAutoExposureSpeed < 0.0f) {
settings.r_hdrAutoExposureSpeed = 0.0f;
}
float rate = 1.0f - std::pow(0.01f, dt * settings.r_hdrAutoExposureSpeed);

computeGain->Use();
computeGainTexCoordRange.SetValue(0.f, 0.f, 1.f, 1.f);
computeGainTexture.SetValue(0);
computeGainColor.SetValue(1.f, 1.f, 1.f, 0.1f);
computeGainColor.SetValue(1.f, 1.f, 1.f, rate);
computeGainMinGain.SetValue(std::pow(2.0f, minExposure));
computeGainMaxGain.SetValue(std::pow(2.0f, maxExposure));
qr.SetCoordAttributeIndex(computeGainPosition());
dev->BindFramebuffer(IGLDevice::Framebuffer, exposureFramebuffer);
dev->BindTexture(IGLDevice::Texture2D, buffer.GetTexture());
Expand Down
2 changes: 1 addition & 1 deletion Sources/Draw/GLAutoExposureFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace spades {
public:
GLAutoExposureFilter(GLRenderer *);
~GLAutoExposureFilter();
GLColorBuffer Filter(GLColorBuffer);
GLColorBuffer Filter(GLColorBuffer, float dt);
};
}
}
24 changes: 18 additions & 6 deletions Sources/Draw/GLColorCorrectionFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,25 @@ namespace spades {

const client::SceneDefinition &def = renderer->GetSceneDef();

if (settings.r_bloom) {
// make image sharper
saturation.SetValue(.85f * def.saturation);
enhancement.SetValue(0.7f);
if (settings.r_hdr) {
// when HDR is enabled ACES tone mapping is applied first, so
// lower enhancement value is required
if (settings.r_bloom) {
saturation.SetValue(0.8f * def.saturation);
enhancement.SetValue(0.1f);
} else {
saturation.SetValue(0.9f * def.saturation);
enhancement.SetValue(0.0f);
}
} else {
saturation.SetValue(1.f * def.saturation);
enhancement.SetValue(0.3f);
if (settings.r_bloom) {
// make image sharper
saturation.SetValue(.85f * def.saturation);
enhancement.SetValue(0.7f);
} else {
saturation.SetValue(1.f * def.saturation);
enhancement.SetValue(0.3f);
}
}

lensTexture.SetValue(0);
Expand Down
Loading

0 comments on commit 6279c9e

Please sign in to comment.