diff --git a/data/scene/atmosphereearth.scene b/data/scene/atmosphereearth.scene index 35c8f307e3..5da8f30f5e 100644 --- a/data/scene/atmosphereearth.scene +++ b/data/scene/atmosphereearth.scene @@ -45,7 +45,8 @@ function postInitialization() graphical settings for the renderables. ]]-- openspace.printInfo("Setting default values") - openspace.setPropertyValue("Sun.renderable.enabled", false) + openspace.setPropertyValue("Sun.renderable.enabled", true) + openspace.setPropertyValue("SunGlare.renderable.enabled", false) openspace.setPropertyValue("SunMarker.renderable.enabled", false) openspace.setPropertyValue("EarthMarker.renderable.enabled", false) openspace.setPropertyValue("Constellation Bounds.renderable.enabled", false) @@ -88,8 +89,8 @@ return { --"atmospheremars", --"moon", - --"lodglobes/earth", - "lodglobes/mars", + "lodglobes/earth", + --"lodglobes/mars", --"lodglobes/moon", --"toyvolume", diff --git a/data/scene/lodglobes/earth/earth.mod b/data/scene/lodglobes/earth/earth.mod index 096d2a2b79..75ddfccdee 100644 --- a/data/scene/lodglobes/earth/earth.mod +++ b/data/scene/lodglobes/earth/earth.mod @@ -58,7 +58,8 @@ return { Atmosphere = { -- Atmosphere radius in Km --AtmoshereRadius = 6450, - AtmoshereRadius = 6420.0, + --AtmoshereRadius = 6420.0, + AtmoshereRadius = 6447.0, --PlanetRadius = 6378.137, PlanetRadius = 6377.0, --PlanetRadius = 6360.0, diff --git a/data/scene/lodglobes/mars/mars.mod b/data/scene/lodglobes/mars/mars.mod index ea9ab42fb7..eaf871d725 100644 --- a/data/scene/lodglobes/mars/mars.mod +++ b/data/scene/lodglobes/mars/mars.mod @@ -38,9 +38,9 @@ return { InteractionDepthBelowEllipsoid = 10000, -- Useful when having negative height map values Atmosphere = { -- Atmosphere radius in Km - AtmoshereRadius = 3436.0, + AtmoshereRadius = 3476.0, --PlanetRadius = 3396.19, - PlanetRadius = 3395.0, + PlanetRadius = 3394.0, PlanetAverageGroundReflectance = 0.1, Rayleigh = { Coefficients = { diff --git a/modules/atmosphere/shaders/deferred_test_fs.glsl b/modules/atmosphere/shaders/deferred_test_fs.glsl index 38f03e3686..84553b0104 100644 --- a/modules/atmosphere/shaders/deferred_test_fs.glsl +++ b/modules/atmosphere/shaders/deferred_test_fs.glsl @@ -345,9 +345,10 @@ void dCalculateRayRenderablePlanet(out dRay ray, out dvec4 planetPositionObjectC * attenuation := out of transmittance T(x,x0). This will be used later when * calculating the reflectance R[L]. */ -vec3 inscatterNoTestRadiance(inout vec3 x, inout float t, const vec3 v, const vec3 s, - out float r, out float mu, out vec3 attenuation, const vec3 fragPosObj, - const double maxLength, const double pixelDepth ) { +vec3 inscatterRadiance(inout vec3 x, inout float t, inout float irradianceFactor, + const vec3 v, const vec3 s, out float r, out float mu, + out vec3 attenuation, const vec3 fragPosObj, + const double maxLength, const double pixelDepth ) { vec3 radiance; r = length(x); @@ -390,6 +391,10 @@ vec3 inscatterNoTestRadiance(inout vec3 x, inout float t, const vec3 v, const ve // the unsused contribution to the final radiance. vec4 inscatterFromSurface = texture4D(inscatterTexture, r0, mu0, muSun0, nu); inscatterRadiance = max(inscatterRadiance - attenuation.rgbr * inscatterFromSurface, 0.0); + + // We set the irradianceFactor to 1.0 so the reflected irradiance will be considered + // when calculating the reflected light on the ground. + irradianceFactor = 1.0; } else { attenuation = analyticTransmittance(r, mu, t); } @@ -458,192 +463,6 @@ vec3 inscatterNoTestRadiance(inout vec3 x, inout float t, const vec3 v, const ve } - - -/* - * Calculates the light scattering in the view direction comming from other - * light rays scattered in the atmosphere. - * Following the paper: S[L]|x - T(x,xs) * S[L]|xs - * The view direction here is the ray: x + tv, s is the sun direction, - * r and mu the position and zenith cosine angle as in the paper. - * Arguments: - * x := camera position - * t := ray displacement variable after calculating the intersection with the - * atmosphere. It is the distance from the camera to the last intersection with - * the atmosphere. If the ray hits the ground, t is updated to the correct value - * v := view direction (ray's direction) (normalized) - * s := Sun direction (normalized) - * r := out of ||x|| inside atmosphere (or top of atmosphere) - * mu := out of cosine of the zenith view angle - * attenuation := out of transmittance T(x,x0). This will be used later when - * calculating the reflectance R[L]. - */ -vec3 inscatterRadiance(inout vec3 x, inout float t, const vec3 v, const vec3 s, - out float r, out float mu, out vec3 attenuation) { - vec3 radiance; - - r = length(x); - mu = dot(x, v) / r; - - float mu2 = mu * mu; - float r2 = r * r; - float Rt2 = Rt * Rt; - float Rg2 = Rg * Rg; - - // Dist stores the distance from the camera position - // to the first (the only one in some cases) intersection of the - // light ray and the top of atmosphere. - - // From the cosine law for x0 at top of atmosphere: - // Rt^2 = r^2 + dist^2 - 2*r*dist*cos(PI - theta) - // Pay attentation to the -sqrt, it means we are - // considering the distance from observer to the - // first intersection with the atmosphere. - float dist = -r * mu - sqrt(r2 * (mu2 - 1.0f) + Rt2); - - // Are we at space? - if (dist > 0.0f) { - // Because we are at space, we must obtain the vector x, - // the correct cosine of between x and v and the right height r, - // with the x in top of atmosphere. - // What we do is to move from the starting point x (camera position) - // to the point on the atmosphere. So, because we have a new x, - // we must also calculate the new cosine between x and v. s is the - // same because we consider the Sun as a parallel ray light source. - t -= dist; - x += dist * v; - // mu(x0 and v) - // cos(theta') = (x0 dot v)/(||x0||*||v||) = ((x + dist*v) dot v)/(Rt * 1) - // cos(theta') = mu' = (r*mu + dist)/Rt - mu = (r * mu + dist) / Rt; - mu2 = mu * mu; - r = Rt; - r2 = r * r; - } - - // Intersects atmosphere? - if (r <= Rt) { - float nu = dot(v, s); - float muSun = dot(x, s) / r; - float rayleighPhase = rayleighPhaseFunction(nu); - float miePhase = miePhaseFunction(nu); - //return vec3(1.0, 0.0, 1.0); - // S[L](x,s,v) - vec4 inscatterRadiance = max(texture4D(inscatterTexture, r, mu, muSun, nu), 0.0); - //return vec3(1.0, 0.0, 0.0); - // After removing the initial path from camera pos to top of atmosphere or the - // current camera position if inside atmosphere, t > 0 - if (t > 0.0) { - // Here we must test if we are hitting the ground: - bool insideATM = false; - double offset = 0.0; - double maxLength = 0.0; - dRay ray; - ray.direction = vec4(v, 0.0); - ray.origin = vec4(x, 1.0); - bool hitGround = dAtmosphereIntersection(vec3(0.0), ray, Rg+1, - insideATM, offset, maxLength); - if (hitGround) { - t = float(offset); - } - // Calculate the zenith angles for x0 and v, s: - vec3 x0 = x + t * v; - float r0 = length(x0); - float mu0 = dot(x0, v) / r0; - float muSun0 = dot(x0, s) / r0; - - // Transmittance from point r, direction mu, distance t - // By Analytical calculation - attenuation = analyticTransmittance(r, mu, t); - - // By Texture Access - //attenuation = transmittance(r, mu, v, x0); - - //The following Code is generating surface acne on atmosphere. JCC - // We need a better acne avoidance constant (0.01). Done!! Adaptive from distance to x - //if (r0 > Rg + (0.1f * r)) { - // It r0 > Rg it means the ray hits something inside the atmosphere. So we need to - // remove the inScattering contribution from the main ray from the hitting point - // to the end of the ray. - - if (r0 > Rg + (0.01f)) { - // Here we use the idea of S[L](a->b) = S[L](b->a), and get the S[L](x0, v, s) - // Then we calculate S[L] = S[L]|x - T(x, x0)*S[L]|x0 - inscatterRadiance = max(inscatterRadiance - attenuation.rgbr * texture4D(inscatterTexture, r0, mu0, muSun0, nu), 0.0); - - // cos(PI-thetaH) = dist/r - // cos(thetaH) = - dist/r - // muHorizon = -sqrt(r^2-Rg^2)/r = -sqrt(1-(Rg/r)^2) - float muHorizon = -sqrt(1.0f - (Rg2 / r2)); - - // In order to avoid imprecision problems near horizon, - // we interpolate between two points: above and below horizon - const float INTERPOLATION_EPS = 0.004f; // precision const from Brunetton - if (abs(mu - muHorizon) < INTERPOLATION_EPS) { - // We want an interpolation value close to 1/2, so the - // contribution of each radiance value is almost the same - // or it has a havey weight if from above or below horizon - float interpolationValue = ((mu - muHorizon) + INTERPOLATION_EPS) / (2.0f * INTERPOLATION_EPS); - - float t2 = t * t; - - // Above Horizon - mu = muHorizon - INTERPOLATION_EPS; - //r0 = sqrt(r * r + t * t + 2.0f * r * t * mu); - // From cosine law where t = distance between x and x0 - // r0^2 = r^2 + t^2 - 2 * r * t * cos(PI-theta) - r0 = sqrt(r2 + t2 + 2.0f * r * t * mu); - // From the dot product: cos(theta0) = (x0 dot v)/(||ro||*||v||) - // mu0 = ((x + t) dot v) / r0 - // mu0 = (x dot v + t dot v) / r0 - // mu0 = (r*mu + t) / r0 - mu0 = (r * mu + t) / r0; - vec4 inScatterAboveX = texture4D(inscatterTexture, r, mu, muSun, nu); - vec4 inScatterAboveXs = texture4D(inscatterTexture, r0, mu0, muSun0, nu); - // Attention for the attenuation.r value applied to the S_Mie - vec4 inScatterAbove = max(inScatterAboveX - attenuation.rgbr * inScatterAboveXs, 0.0f); - - // Below Horizon - mu = muHorizon + INTERPOLATION_EPS; - r0 = sqrt(r2 + t2 + 2.0f * r * t * mu); - mu0 = (r * mu + t) / r0; - vec4 inScatterBelowX = texture4D(inscatterTexture, r, mu, muSun, nu); - vec4 inScatterBelowXs = texture4D(inscatterTexture, r0, mu0, muSun0, nu); - // Attention for the attenuation.r value applied to the S_Mie - vec4 inScatterBelow = max(inScatterBelowX - attenuation.rgbr * inScatterBelowXs, 0.0); - - // Interpolate between above and below inScattering radiance - inscatterRadiance = mix(inScatterAbove, inScatterBelow, interpolationValue); - } - } - } - - // The w component of inscatterRadiance has stored the Cm,r value (Cm = Sm[L0]) - // So, we must reintroduce the Mie inscatter by the proximity rule as described in the - // paper by Bruneton and Neyret in "Angular precision" paragraph: - - // Hermite interpolation between two values - // This step is done because imprecision problems happen when the Sun is slightly below - // the horizon. When this happen, we avoid the Mie scattering contribution. - inscatterRadiance.w *= smoothstep(0.0f, 0.02f, muSun); - vec3 inscatterMie = inscatterRadiance.rgb * inscatterRadiance.a / max(inscatterRadiance.r, 1e-4) * - (betaRayleigh.r / betaRayleigh); - - radiance = max(inscatterRadiance.rgb * rayleighPhase + inscatterMie * miePhase, 0.0f); - - } else { - // No intersection with atmosphere - // The ray is traveling on space - radiance = vec3(1.0, 1.0, 0.0f); - } - - - // Finally we add the Lsun (all calculations are done with no Lsun so - // we can change it on the fly with no precomputations) - return radiance * sunRadiance; - -} - /* * Calculates the light reflected in the view direction comming from other * light rays integrated over the hemispehre plus the direct light (L0) from Sun. @@ -665,79 +484,47 @@ vec3 inscatterRadiance(inout vec3 x, inout float t, const vec3 v, const vec3 s, */ vec3 groundColor(const vec3 x, const float t, const vec3 v, const vec3 s, const float r, const float mu, const vec3 attenuationXtoX0, const vec4 groundColor, - const vec4 normalReflectance) + const vec4 normalReflectance, const float irradianceFactor) { vec3 reflectedRadiance = vec3(0.0f); - //float d = length(x + t * v); - //float x_0 = sqrt(r * r + d * d - 2 * r * d * mu); - - // Ray hits planet's surface - // if (t > 0.0f) { - //if (x_0 >= Rg) { - // First we obtain the ray's end point on the surface - vec3 x0 = x + t * v; - float r0 = length(x0); - // Normal of intersection point. - vec3 n = normalReflectance.xyz; + // First we obtain the ray's end point on the surface + vec3 x0 = x + t * v; + float r0 = length(x0); + // Normal of intersection point. + vec3 n = normalReflectance.xyz; + vec4 reflectance = groundColor * vec4(0.4); + //reflectance.w = 1.0; + + // L0 is not included in the irradiance texture. + // We first calculate the light attenuation from the top of the atmosphere + // to x0. + float muSun = max(dot(n, s), 0.0); + // Is direct Sun light arriving at x0? If not, there is no direct light from Sun (shadowed) + vec3 transmittanceL0 = muSun < -sqrt(1.0f - ((Rg * Rg) / (r0 * r0))) ? vec3(0.0f) : transmittanceLUT(r0, muSun); - // Old deferred: - //vec2 coords = vec2(atan(n.y, n.x), acos(n.z)) * vec2(0.5, 1.0) / M_PI + vec2(0.5, 0.0); - //vec2 coords = vec2(0.5 + (atan(n.z, n.x))/(2*M_PI), 0.5 - asin(n.y)/(M_PI)); - // TODO: Chango to G-Buffer. - //vec4 reflectance = texture2D(reflectanceTexture, coords) * vec4(0.2, 0.2, 0.2, 1.0); - vec4 reflectance = groundColor; - //vec4 reflectance = groundColor; - //reflectance.w = 1.0; - - // The following code is generating surface acne in ground. - // It is only necessary inside atmosphere rendering. JCC - // If r0 > Rg + EPS (we are not intersecting the ground), - // we get a constant initial ground radiance - // if (r0 > Rg + 0.01) { - // reflectance = vec4(0.0, 0.0, 0.0, 0.0); - // } + // E[L*] at x0 + vec3 irradianceReflected = irradiance(irradianceTexture, r0, muSun) * irradianceFactor; - // L0 is not included in the irradiance texture. - // We first calculate the light attenuation from the top of the atmosphere - // to x0. - float muSun = dot(n, s); - // Is direct Sun light arriving at x0? If not, there is no direct light from Sun (shadowed) - vec3 transmittanceL0 = muSun < -sqrt(1.0f - ((Rg * Rg) / (r0 * r0))) ? vec3(0.0f) : transmittanceLUT(r0, muSun); - - // E[L*] at x0 - vec3 irradianceReflected = irradiance(irradianceTexture, r0, muSun); + // R[L0] + R[L*] + vec3 groundRadiance = reflectance.rgb * (muSun * transmittanceL0 + irradianceReflected) + * sunRadiance / M_PI; - // Adding clouds texture - //vec4 clouds = vec4(0.85)*texture(cloudsTexture, vs_st); + // Yellowish specular reflection from sun on oceans and rivers + if (reflectance.w > 0.0) { + vec3 h = normalize(s - v); + // Fresnell Schlick's approximation + float fresnel = 0.02f + 0.98f * pow(1.0f - dot(-v, h), 5.0f); + // Walter BRDF approximation + float waterBrdf = fresnel * pow(max(dot(h, n), 0.0f), 150.0f); + // Adding Fresnell and Water BRDFs approximation to the final surface color + // (After adding the sunRadiance and the attenuation of the Sun through atmosphere) + groundRadiance += reflectance.w * max(waterBrdf, 0.0) * transmittanceL0 * sunRadiance; + } - //vec3 groundRadiance = (reflectance.rgb + clouds.rgb) * - // (max(muSun, 0.0) * transmittanceL0 + irradianceReflected) * sunRadiance / M_PI; - - // R[L0] + R[L*] - vec3 groundRadiance = reflectance.rgb * - (max(muSun, 0.0) * transmittanceL0 + irradianceReflected) * sunRadiance / M_PI; + // Finally, we attenuate the surface Radiance from the the point x0 to the camera location. + reflectedRadiance = attenuationXtoX0 * groundRadiance; - // Yellowish specular reflection from sun on oceans and rivers - if (reflectance.w > 0.0) { - vec3 h = normalize(s - v); - // Fresnell Schlick's approximation - float fresnel = 0.02f + 0.98f * pow(1.0f - dot(-v, h), 5.0f); - // Walter BRDF approximation - float waterBrdf = fresnel * pow(max(dot(h, n), 0.0f), 150.0f); - // Adding Fresnell and Water BRDFs approximation to the final surface color - // (After adding the sunRadiance and the attenuation of the Sun through atmosphere) - groundRadiance += reflectance.w * max(waterBrdf, 0.0) * transmittanceL0 * sunRadiance; - } - - // Finally, we attenuate the surface Radiance from the the point x0 to the camera location. - reflectedRadiance = attenuationXtoX0 * groundRadiance; - - // } else { // ray looking at the sky - // //reflectedRadiance = vec3(0.0f); - // reflectedRadiance = vec3(1.0f, 0.0, 0.0); - // } - // Returns reflectedRadiance = 0.0 if the ray doesn't hit the ground. return reflectedRadiance; } @@ -782,7 +569,7 @@ void main() { meanPosition /= nAaSamples; // geoDepth /= nAaSamples; - meanNormal.xyz = normalize(meanNormal.xyz); + //meanNormal.xyz = normalize(meanNormal.xyz); // Ray in object space dRay ray; @@ -835,9 +622,14 @@ void main() { // next comparison with the planet's ground make sense: pixelDepth -= offset; - vec3 inscatterColor = inscatterNoTestRadiance(x, tF, v, s, r, mu, attenuation, - vec3(fragObjectCoords.xyz), maxLength, pixelDepth); - vec3 groundColor = groundColor(x, tF, v, s, r, mu, attenuation, meanColor, meanNormal); + float irradianceFactor = 0.0; + + vec3 inscatterColor = inscatterRadiance(x, tF, irradianceFactor, v, + s, r, mu, attenuation, + vec3(fragObjectCoords.xyz), + maxLength, pixelDepth); + vec3 groundColor = groundColor(x, tF, v, s, r, mu, attenuation, + meanColor, meanNormal, irradianceFactor); vec3 sunColor = sunColor(x, tF, v, s, r, mu); vec4 finalRadiance = vec4(HDR(inscatterColor + groundColor + sunColor), 1.0); @@ -891,12 +683,15 @@ void main() { // OS Eye to World coords dvec4 tmpRInvPos = dInverseCamRotTransform * meanPosition; dvec4 fragWorldCoords = dvec4(dvec3(tmpRInvPos) + dCampos, 1.0); - dvec4 tmpRInvNormal = dInverseCamRotTransform * meanNormal; - dvec4 fragNormalWorldCoords = dvec4(dvec3(tmpRInvNormal) + dCampos, 1.0); + //dvec4 tmpRInvNormal = dInverseCamRotTransform * meanNormal; + //dvec4 fragNormalWorldCoords = dvec4(dvec3(tmpRInvNormal) + dCampos, 1.0); // World to Object (Normal and Position in meters) dvec4 fragObjectCoords = dInverseTransformMatrix * fragWorldCoords; - dvec4 fragNormalObjectCoords = dInverseTransformMatrix * fragNormalWorldCoords; + //dvec4 fragNormalObjectCoords = dInverseTransformMatrix * fragNormalWorldCoords; + + // Normal in Object Space already (changed 05/26/2017). + dvec4 fragNormalObjectCoords = dvec4(normalize(meanNormal.xyz), 1.0); // Distance of the pixel in the gBuffer to the observer double pixelDepth = distance(cameraPositionInObject.xyz, fragObjectCoords.xyz); @@ -906,7 +701,8 @@ void main() { fragObjectCoords.xyz /= 1000.0; if (meanPosition.xyz != vec3(0.0) && (pixelDepth < offset)) { - renderTarget = meanColor; + //renderTarget = meanColor; + renderTarget = vec4(0.0); } else { // Following paper nomenclature double t = offset; @@ -927,9 +723,14 @@ void main() { // next comparison with the planet's ground make sense: pixelDepth -= offset; - vec3 inscatterColor = inscatterNoTestRadiance(x, tF, v, s, r, mu, attenuation, - vec3(fragObjectCoords.xyz), maxLength, pixelDepth); - vec3 groundColor = groundColor(x, tF, v, s, r, mu, attenuation, meanColor, meanNormal); + float irradianceFactor = 0.0; + + vec3 inscatterColor = inscatterRadiance(x, tF, irradianceFactor, v, + s, r, mu, attenuation, + vec3(fragObjectCoords.xyz), + maxLength, pixelDepth); + vec3 groundColor = groundColor(x, tF, v, s, r, mu, attenuation, + meanColor, meanNormal, irradianceFactor); vec3 sunColor = sunColor(x, tF, v, s, r, mu); //vec4 finalRadiance = vec4(HDR(inscatterColor + sunColor), 1.0); @@ -940,10 +741,10 @@ void main() { //vec4 finalRadiance = vec4(HDR(inscatterColor + meanColor.xyz), meanColor.w); //vec4 finalRadiance = vec4(HDR(sunColor), 1.0); //vec4 finalRadiance = vec4(sunColor, 1.0); - //vec4 finalRadiance = vec4(HDR(inscatterColor + groundColor + sunColor), 1.0); + //vec4 finalRadiance = vec4(HDR(inscatterColor + groundColor), 1.0); // The meanColor is temporary here - vec4 finalRadiance = vec4(HDR(inscatterColor + groundColor + sunColor + meanColor.xyz), 1.0); + vec4 finalRadiance = vec4(HDR(inscatterColor + groundColor + sunColor), 1.0); renderTarget = finalRadiance; //renderTarget = vec4(vec3(pixelDepth/100000),1.0); diff --git a/modules/globebrowsing/globes/renderableglobe.cpp b/modules/globebrowsing/globes/renderableglobe.cpp index eae107b059..e2e451e196 100644 --- a/modules/globebrowsing/globes/renderableglobe.cpp +++ b/modules/globebrowsing/globes/renderableglobe.cpp @@ -93,7 +93,7 @@ RenderableGlobe::RenderableGlobe(const ghoul::Dictionary& dictionary) , _texturePropertyOwner("Textures") #ifdef OPENSPACE_MODULE_ATMOSPHERE_ENABLED , _atmosphereProperties({ - FloatProperty("atmmosphereHeight", "Atmosphere Height (KM)", 60.0f, 0.1f, 1000.0f), + FloatProperty("atmmosphereHeight", "Atmosphere Height (KM)", 60.0f, 0.1f, 99.0f), FloatProperty("averageGroundReflectance", "Average Ground Reflectance (%)", 0.1f, 0.0f, 1.0f), FloatProperty("rayleighHeightScale", "Rayleigh Height Scale (KM)", 8.0f, 0.1f, 20.0f), FloatProperty("rayleighScatteringCoeffX", "Rayleigh Scattering Coeff X (x10e-3)", 1.0f, 0.01f, 100.0f), diff --git a/modules/globebrowsing/rendering/chunkrenderer.cpp b/modules/globebrowsing/rendering/chunkrenderer.cpp index 10cd20767c..04cf5ef85a 100644 --- a/modules/globebrowsing/rendering/chunkrenderer.cpp +++ b/modules/globebrowsing/rendering/chunkrenderer.cpp @@ -255,14 +255,17 @@ void ChunkRenderer::renderChunkLocally(const Chunk& chunk, const RenderData& dat std::vector cornerNames = { "p01", "p11", "p00", "p10" }; std::vector cornersCameraSpace(4); + std::vector cornersModelSpace(4); for (int i = 0; i < 4; ++i) { Quad q = (Quad)i; Geodetic2 corner = chunk.surfacePatch().getCorner(q); glm::dvec3 cornerModelSpace = ellipsoid.cartesianSurfacePosition(corner); + cornersModelSpace.push_back(cornerModelSpace); glm::dvec3 cornerCameraSpace = glm::dvec3(dmat4(modelViewTransform) * glm::dvec4(cornerModelSpace, 1)); cornersCameraSpace[i] = cornerCameraSpace; programObject->setUniform(cornerNames[i], vec3(cornerCameraSpace)); + } // TODO: Patch normal can be calculated for all corners and then linearly @@ -273,7 +276,18 @@ void ChunkRenderer::renderChunkLocally(const Chunk& chunk, const RenderData& dat cornersCameraSpace[Quad::NORTH_EAST] - cornersCameraSpace[Quad::SOUTH_WEST])); + // In order to improve performance, lets use the normal in object space (model space) + // for deferred rendering. + vec3 patchNormalModelSpace = normalize( + cross(cornersModelSpace[Quad::SOUTH_EAST] - + cornersModelSpace[Quad::SOUTH_WEST], + cornersModelSpace[Quad::NORTH_EAST] - + cornersModelSpace[Quad::SOUTH_WEST])); + + programObject->setUniform("patchNormalCameraSpace", patchNormalCameraSpace); + // TODO (JCC): Enable the right normal for displaced points in the patch + //programObject->setUniform("patchNormalModelSpace", patchNormalModelSpace); programObject->setUniform("projectionTransform", data.camera.projectionMatrix()); if (_layerManager->layerGroup( diff --git a/modules/globebrowsing/shaders/globalchunkedlodpatch_fs.glsl b/modules/globebrowsing/shaders/globalchunkedlodpatch_fs.glsl index 0c8f24a77c..1e76ed0a9c 100644 --- a/modules/globebrowsing/shaders/globalchunkedlodpatch_fs.glsl +++ b/modules/globebrowsing/shaders/globalchunkedlodpatch_fs.glsl @@ -36,7 +36,8 @@ Fragment getFragment() { // TODO: Change the color for the new deferred system (JCC) frag.gColor = frag.color; // Normal is written in Camera Rig (OS Eye) Space - frag.gNormalReflectance = vec4(ellipsoidNormalCameraSpace, 1.0); // adding 1.0 to reflectance by now + //frag.gNormalReflectance = vec4(ellipsoidNormalCameraSpace, 1.0); // adding 1.0 to reflectance by now + frag.gNormalReflectance = vec4(fs_normal, 1.0); // adding 1.0 to reflectance by now frag.gPosition = vec4(positionCameraSpace, 1.0); // in Camera Rig Space frag.depth = fs_position.w; diff --git a/modules/globebrowsing/shaders/globalchunkedlodpatch_vs.glsl b/modules/globebrowsing/shaders/globalchunkedlodpatch_vs.glsl index 86904a5db8..8bb579876f 100644 --- a/modules/globebrowsing/shaders/globalchunkedlodpatch_vs.glsl +++ b/modules/globebrowsing/shaders/globalchunkedlodpatch_vs.glsl @@ -43,6 +43,7 @@ layout(location = 1) in vec2 in_uv; out vec2 fs_uv; out vec4 fs_position; +out vec3 fs_normal; out vec3 ellipsoidNormalCameraSpace; out LevelWeights levelWeights; out vec3 positionCameraSpace; @@ -84,5 +85,6 @@ void main() { fs_position = z_normalization(positionClippingSpace); gl_Position = fs_position; ellipsoidNormalCameraSpace = mat3(modelViewTransform) * pair.normal; + fs_normal = pair.normal; positionCameraSpace = vec3(modelViewTransform * vec4(pair.position, 1)); } diff --git a/modules/globebrowsing/shaders/localchunkedlodpatch_fs.glsl b/modules/globebrowsing/shaders/localchunkedlodpatch_fs.glsl index 293417f9ae..416c7bfaf2 100644 --- a/modules/globebrowsing/shaders/localchunkedlodpatch_fs.glsl +++ b/modules/globebrowsing/shaders/localchunkedlodpatch_fs.glsl @@ -38,6 +38,7 @@ Fragment getFragment() { frag.gColor = frag.color; // Normal is written in Camera Rig (OS Eye) Space frag.gNormalReflectance = vec4(ellipsoidNormalCameraSpace, 1.0); // adding 1.0 to reflectance by now + //frag.gNormalReflectance = vec4(fs_normal, 1.0); // adding 1.0 to reflectance by now frag.gPosition = vec4(positionCameraSpace, 1.0); // in Camera Rig Space frag.depth = fs_position.w; diff --git a/modules/globebrowsing/shaders/localchunkedlodpatch_vs.glsl b/modules/globebrowsing/shaders/localchunkedlodpatch_vs.glsl index 8759bbdc17..98ee3c886a 100644 --- a/modules/globebrowsing/shaders/localchunkedlodpatch_vs.glsl +++ b/modules/globebrowsing/shaders/localchunkedlodpatch_vs.glsl @@ -39,12 +39,14 @@ uniform vec3 p10; uniform vec3 p01; uniform vec3 p11; uniform vec3 patchNormalCameraSpace; +uniform vec3 patchNormalModelSpace; uniform float chunkMinHeight; layout(location = 1) in vec2 in_uv; out vec2 fs_uv; out vec4 fs_position; +out vec3 fs_normal; out vec3 ellipsoidNormalCameraSpace; out LevelWeights levelWeights; out vec3 positionCameraSpace; @@ -93,5 +95,6 @@ void main() { fs_position = z_normalization(positionClippingSpace); gl_Position = fs_position; ellipsoidNormalCameraSpace = patchNormalCameraSpace; + fs_normal = patchNormalModelSpace; positionCameraSpace = p; } diff --git a/modules/globebrowsing/shaders/tilefragcolor.hglsl b/modules/globebrowsing/shaders/tilefragcolor.hglsl index 2324643096..d3f88b2c4a 100644 --- a/modules/globebrowsing/shaders/tilefragcolor.hglsl +++ b/modules/globebrowsing/shaders/tilefragcolor.hglsl @@ -70,6 +70,7 @@ uniform vec2 vertexResolution; #endif in vec4 fs_position; +in vec3 fs_normal; in vec2 fs_uv; in vec3 ellipsoidNormalCameraSpace; in vec3 positionCameraSpace;