Small fixes to opengl rendering

This commit is contained in:
Michaël Lemaire 2013-12-24 15:00:32 +01:00 committed by Michael Lemaire
parent 806ab74244
commit 742b93d3d9
14 changed files with 61 additions and 56 deletions

View file

@ -4,7 +4,7 @@
ColorProfile::ColorProfile() ColorProfile::ColorProfile()
{ {
setToneMapping(TONE_MAPPING_UNCHARTED, 2.0); setToneMapping(TONE_MAPPING_UNCHARTED, 1.6);
} }
ColorProfile::ColorProfile(ToneMappingOperator tonemapper, double exposure) ColorProfile::ColorProfile(ToneMappingOperator tonemapper, double exposure)

View file

@ -200,7 +200,7 @@ void Scenery::checkCameraAboveGround()
{ {
Vector3 camera_location = camera->getLocation(); Vector3 camera_location = camera->getLocation();
double terrain_height = terrain->getInterpolatedHeight(camera_location.x, camera_location.z, 1, 1) + 2.0; double terrain_height = terrain->getInterpolatedHeight(camera_location.x, camera_location.z, 1, 1) + 2.0;
double water_height = terrain->getWaterHeight() + 1.0; double water_height = terrain->getWaterHeight() + 1.5;
if (camera_location.y < water_height || camera_location.y < terrain_height) if (camera_location.y < water_height || camera_location.y < terrain_height)
{ {
double diff = ((water_height > terrain_height) ? water_height : terrain_height) - camera_location.y; double diff = ((water_height > terrain_height) ? water_height : terrain_height) - camera_location.y;

View file

@ -15,7 +15,8 @@ OpenGLRenderer::OpenGLRenderer(Scenery* scenery):
functions = new OpenGLFunctions(); functions = new OpenGLFunctions();
shared_state = new OpenGLSharedState(); shared_state = new OpenGLSharedState();
shared_state->set("viewDistance", 20.0); shared_state->set("viewDistance", 300.0);
shared_state->set("exposure", 1.6);
skybox = new OpenGLSkybox(this); skybox = new OpenGLSkybox(this);
water = new OpenGLWater(this); water = new OpenGLWater(this);
@ -60,6 +61,8 @@ void OpenGLRenderer::initialize()
prepare(); prepare();
disableClouds();
skybox->initialize(); skybox->initialize();
skybox->updateScenery(); skybox->updateScenery();

View file

@ -54,6 +54,7 @@ void OpenGLTerrain::initialize()
program->addVertexSource("terrain"); program->addVertexSource("terrain");
program->addFragmentSource("bruneton"); program->addFragmentSource("bruneton");
program->addFragmentSource("tonemapping"); program->addFragmentSource("tonemapping");
program->addFragmentSource("fadeout");
program->addFragmentSource("terrain"); program->addFragmentSource("terrain");
// Add terrain chunks // Add terrain chunks

View file

@ -25,6 +25,7 @@ void OpenGLWater::initialize()
program->addVertexSource("water"); program->addVertexSource("water");
program->addFragmentSource("bruneton"); program->addFragmentSource("bruneton");
program->addFragmentSource("tonemapping"); program->addFragmentSource("tonemapping");
program->addFragmentSource("fadeout");
program->addFragmentSource("water"); program->addFragmentSource("water");
setVertex(0, -1.0f, 0.0f, -1.0f); setVertex(0, -1.0f, 0.0f, -1.0f);

View file

@ -84,4 +84,5 @@ OTHER_FILES += \
shaders/bruneton.frag \ shaders/bruneton.frag \
shaders/tonemapping.frag \ shaders/tonemapping.frag \
shaders/terrain.frag \ shaders/terrain.frag \
shaders/terrain.vert shaders/terrain.vert \
shaders/fadeout.frag

View file

@ -2,7 +2,6 @@ const float GROUND_OFFSET = 0.5;
const float Rg = 6360.0; const float Rg = 6360.0;
const float Rt = 6420.0; const float Rt = 6420.0;
const float RL = 6421.0; const float RL = 6421.0;
const float exposure = 0.4;
const float ISun = 100.0; const float ISun = 100.0;
const float AVERAGE_GROUND_REFLECTANCE = 0.1; const float AVERAGE_GROUND_REFLECTANCE = 0.1;
const float HR = 8.0; const float HR = 8.0;
@ -90,7 +89,7 @@ vec4 _sunTransmittance(vec3 v, vec3 s, float r, float mu, float radius)
{ {
vec4 transmittance = r <= Rt ? _transmittanceWithShadow(r, mu) : vec4(1.0); /* T(x,xo) */ vec4 transmittance = r <= Rt ? _transmittanceWithShadow(r, mu) : vec4(1.0); /* T(x,xo) */
float d = _limit(r, mu); float d = _limit(r, mu);
radius *= (1.0 + 10.0 * d / Rt); /* Inflating due to lens effect near horizon */ radius *= (1.0 + 25.0 * d / Rt); /* Inflating due to lens effect near horizon */
float isun = step(cos(radius * M_PI / 180.0), dot(v, s)) * ISun; /* Lsun */ float isun = step(cos(radius * M_PI / 180.0), dot(v, s)) * ISun; /* Lsun */
transmittance.r *= isun; transmittance.r *= isun;
transmittance.g *= isun; transmittance.g *= isun;
@ -186,3 +185,27 @@ vec3 _getInscatterColor(inout vec3 x, inout float t, vec3 v, vec3 s, out float r
} }
return result * ISun; return result * ISun;
} }
vec4 applyAerialPerspective(vec4 base)
{
float yoffset = GROUND_OFFSET - waterHeight;
vec3 camera = vec3(cameraLocation.x, max(cameraLocation.y + yoffset, 0.0), cameraLocation.z);
vec3 location = vec3(unprojected.x, max(unprojected.y + yoffset, 0.0), unprojected.z);
vec3 x = vec3(0.0, Rg + camera.y * WORLD_SCALING, 0.0);
vec3 v = normalize(location - camera);
vec3 s = normalize(sunDirection * SUN_DISTANCE_SCALED - x);
if (v.y == 0.0)
{
v.y = -0.000001;
}
float r = length(x);
float mu = dot(x, v) / r;
float t = length(location - camera) * WORLD_SCALING;
vec3 attenuation;
vec3 inscattering = _getInscatterColor(x, t, v, s, r, mu, attenuation);
return base * vec4(attenuation, 0.0) + vec4(inscattering, 0.0);
}

View file

@ -0,0 +1,8 @@
uniform float viewDistance;
float distanceFadeout()
{
vec3 camera = vec3(cameraLocation.x, 0.0, cameraLocation.z);
vec3 location = vec3(unprojected.x, 0.0, unprojected.z);
return mix(1.0, 0.0, clamp((length(location - camera) - viewDistance * 0.8) / (viewDistance * 0.2), 0.0, 1.0));
}

View file

@ -8,5 +8,6 @@
<file>tonemapping.frag</file> <file>tonemapping.frag</file>
<file>terrain.frag</file> <file>terrain.frag</file>
<file>terrain.vert</file> <file>terrain.vert</file>
<file>fadeout.frag</file>
</qresource> </qresource>
</RCC> </RCC>

View file

@ -15,5 +15,5 @@ void main(void)
vec3 attenuation; vec3 attenuation;
vec3 inscattering = _getInscatterColor(x, t, v, s, r, mu, attenuation); vec3 inscattering = _getInscatterColor(x, t, v, s, r, mu, attenuation);
gl_FragColor = _toneMappingUncharted(sunTransmittance + vec4(inscattering, 0.0), 2.0); gl_FragColor = applyToneMapping(sunTransmittance + vec4(inscattering, 0.0));
} }

View file

@ -1,33 +1,13 @@
uniform sampler2D groundTexture; uniform sampler2D groundTexture;
varying vec2 texcoord; varying vec2 texcoord;
uniform float viewDistance;
void main(void) void main(void)
{ {
gl_FragColor = texture2D(groundTexture, texcoord); gl_FragColor = texture2D(groundTexture, texcoord);
float yoffset = GROUND_OFFSET - waterHeight; gl_FragColor = applyAerialPerspective(gl_FragColor);
vec3 camera = vec3(cameraLocation.x, max(cameraLocation.y + yoffset, 0.0), cameraLocation.z);
vec3 location = vec3(unprojected.x, max(unprojected.y + yoffset, 0.0), unprojected.z);
vec3 x = vec3(0.0, Rg + camera.y * WORLD_SCALING, 0.0);
vec3 v = normalize(location - camera);
vec3 s = normalize(sunDirection * SUN_DISTANCE_SCALED - x);
if (v.y == 0.0) gl_FragColor = applyToneMapping(gl_FragColor);
{
v.y = -0.000001;
}
float r = length(x); gl_FragColor.a = distanceFadeout();
float mu = dot(x, v) / r;
float t = length(location - camera) * WORLD_SCALING;
vec3 attenuation;
vec3 inscattering = _getInscatterColor(x, t, v, s, r, mu, attenuation);
gl_FragColor = gl_FragColor * vec4(attenuation, 0.0) + vec4(inscattering, 0.0);
gl_FragColor = _toneMappingUncharted(gl_FragColor, 2.0);
gl_FragColor.a = mix(1.0, 0.0, clamp((t - viewDistance * 0.8) / (viewDistance * 0.2), 0.0, 1.0));
} }

View file

@ -1,3 +1,5 @@
uniform float exposure;
float _uncharted2Tonemap(float x) float _uncharted2Tonemap(float x)
{ {
float A = 0.15; float A = 0.15;
@ -10,7 +12,12 @@ float _uncharted2Tonemap(float x)
return ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F; return ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F;
} }
vec4 _toneMappingUncharted(vec4 color, float exposure) vec4 applyToneMapping(vec4 color)
{
return vec4(((color * exposure) / (1.0 + color * exposure)).rgb, 1.0);
}
/*vec4 applyToneMapping(vec4 color)
{ {
float W = 11.2; float W = 11.2;
float white_scale = 1.0 / _uncharted2Tonemap(W); float white_scale = 1.0 / _uncharted2Tonemap(W);
@ -22,4 +29,4 @@ vec4 _toneMappingUncharted(vec4 color, float exposure)
result.a = 1.0; result.a = 1.0;
return result; return result;
} }*/

View file

@ -1,32 +1,12 @@
uniform vec4 waterColor; uniform vec4 waterColor;
uniform float viewDistance;
void main(void) void main(void)
{ {
gl_FragColor = waterColor; gl_FragColor = waterColor;
float yoffset = GROUND_OFFSET - waterHeight; gl_FragColor = applyAerialPerspective(gl_FragColor);
vec3 camera = vec3(cameraLocation.x, max(cameraLocation.y + yoffset, 0.0), cameraLocation.z);
vec3 location = vec3(unprojected.x, max(unprojected.y + yoffset, 0.0), unprojected.z);
vec3 x = vec3(0.0, Rg + camera.y * WORLD_SCALING, 0.0);
vec3 v = normalize(location - camera);
vec3 s = normalize(sunDirection * SUN_DISTANCE_SCALED - x);
if (v.y == 0.0) gl_FragColor = applyToneMapping(gl_FragColor);
{
v.y = -0.000001;
}
float r = length(x); gl_FragColor.a = distanceFadeout();
float mu = dot(x, v) / r;
float t = length(location - camera) * WORLD_SCALING;
vec3 attenuation;
vec3 inscattering = _getInscatterColor(x, t, v, s, r, mu, attenuation);
gl_FragColor = gl_FragColor * vec4(attenuation, 0.0) + vec4(inscattering, 0.0);
gl_FragColor = _toneMappingUncharted(gl_FragColor, 2.0);
gl_FragColor.a = mix(1.0, 0.0, clamp((t - viewDistance * 0.8) / (viewDistance * 0.2), 0.0, 1.0));
} }

View file

@ -934,7 +934,7 @@ static Color _sunColor(Vector3 v, Vector3 s, double r, double mu, double radius)
{ {
Color transmittance = r <= Rt ? _transmittanceWithShadow(r, mu) : COLOR_WHITE; /* T(x,xo) */ Color transmittance = r <= Rt ? _transmittanceWithShadow(r, mu) : COLOR_WHITE; /* T(x,xo) */
double d = _limit(r, mu); double d = _limit(r, mu);
radius *= (1.0 + 10.0 * d / Rt); /* Inflating due to lens effect near horizon */ radius *= (1.0 + 25.0 * d / Rt); /* Inflating due to lens effect near horizon */
double isun = step(cos(radius * M_PI / 180.0), v.dotProduct(s)) * ISun; /* Lsun */ double isun = step(cos(radius * M_PI / 180.0), v.dotProduct(s)) * ISun; /* Lsun */
transmittance.r *= isun; transmittance.r *= isun;
transmittance.g *= isun; transmittance.g *= isun;