1
0
Fork 0

HDR: Render the Moon, stars and planets

Moon textures have been adapted from the following webpage:

https://svs.gsfc.nasa.gov/4720
NASA's Scientific Visualization Studio
This commit is contained in:
Fernando García Liñán 2023-11-06 14:51:53 +01:00
parent 228ac3d168
commit 746532f098
11 changed files with 277 additions and 7 deletions

View file

@ -161,7 +161,4 @@
</technique>
<technique n="109">
<scheme>hdr-geometry</scheme>
</technique>
</PropertyList>

122
Effects/moon.eff Normal file
View file

@ -0,0 +1,122 @@
<?xml version="1.0" encoding="utf-8"?>
<PropertyList>
<name>Effects/moon</name>
<parameters>
<texture n="1">
<image>Textures/Sky/moon_color.png</image>
<type>2d</type>
<filter>linear-mipmap-linear</filter>
<mag-filter>linear-mipmap-linear</mag-filter>
<wrap-s>clamp-to-edge</wrap-s>
<wrap-t>clamp-to-edge</wrap-t>
<internal-format>normalized</internal-format>
</texture>
<texture n="2">
<image>Textures/Sky/moon_n.png</image>
<type>2d</type>
<filter>linear-mipmap-linear</filter>
<mag-filter>linear-mipmap-linear</mag-filter>
<wrap-s>clamp-to-edge</wrap-s>
<wrap-t>clamp-to-edge</wrap-t>
<internal-format>normalized</internal-format>
</texture>
<!-- exposure.glsl -->
<exposure-compensation>
<use>/sim/rendering/hdr/exposure-compensation</use>
</exposure-compensation>
</parameters>
<!-- Fixed-pipeline fallback -->
<technique n="11">
<pass>
<!--
An empty pass should be fine as we are filling the stateset manually
in simgear/scene/sky/moon.cxx
-->
</pass>
</technique>
<technique n="129">
<scheme>hdr-forward</scheme>
<pass>
<!-- Disable depth testing -->
<depth>
<enabled>false</enabled>
</depth>
<!-- Only render the moon on top of the skydome -->
<stencil>
<function>equal</function>
<value>0</value>
</stencil>
<!-- Make sure we render after the skydome -->
<render-bin>
<bin-number>-5</bin-number>
<bin-name>RenderBin</bin-name>
</render-bin>
<!-- Color map -->
<texture-unit>
<unit>1</unit>
<type><use>texture[1]/type</use></type>
<image><use>texture[1]/image</use></image>
<filter><use>texture[1]/filter</use></filter>
<mag-filter><use>texture[1]/mag-filter</use></mag-filter>
<wrap-s><use>texture[1]/wrap-s</use></wrap-s>
<wrap-t><use>texture[1]/wrap-t</use></wrap-t>
</texture-unit>
<!-- Normal map -->
<texture-unit>
<unit>2</unit>
<type><use>texture[2]/type</use></type>
<image><use>texture[2]/image</use></image>
<filter><use>texture[2]/filter</use></filter>
<mag-filter><use>texture[2]/mag-filter</use></mag-filter>
<wrap-s><use>texture[2]/wrap-s</use></wrap-s>
<wrap-t><use>texture[2]/wrap-t</use></wrap-t>
</texture-unit>
<cull-face>back</cull-face>
<!-- Additive blending to add the sky in-scattering from the skydome -->
<blend>
<active>true</active>
<source>one</source>
<destination>one</destination>
</blend>
<program>
<vertex-shader>Shaders/HDR/moon.vert</vertex-shader>
<fragment-shader>Shaders/HDR/moon.frag</fragment-shader>
<fragment-shader>Shaders/HDR/math.glsl</fragment-shader>
<fragment-shader>Shaders/HDR/color.glsl</fragment-shader>
<fragment-shader>Shaders/HDR/normalmap.glsl</fragment-shader>
<fragment-shader>Shaders/HDR/atmos_spectral.glsl</fragment-shader>
<fragment-shader>Shaders/HDR/exposure.glsl</fragment-shader>
</program>
<uniform>
<name>color_tex</name>
<type>sampler-2d</type>
<value type="int">1</value>
</uniform>
<uniform>
<name>normal_tex</name>
<type>sampler-2d</type>
<value type="int">2</value>
</uniform>
<uniform>
<name>transmittance_tex</name>
<type>sampler-2d</type>
<value type="int">12</value>
</uniform>
<!-- exposure.glsl -->
<uniform>
<name>lum_tex</name>
<type>sampler-2d</type>
<value type="int">14</value>
</uniform>
<uniform>
<name>exposure_compensation</name>
<type>float</type>
<value><use>exposure-compensation</use></value>
</uniform>
</pass>
</technique>
</PropertyList>

View file

@ -3,6 +3,10 @@
<PropertyList>
<name>Effects/stars</name>
<parameters>
<max-radiance type="float">2.0</max-radiance>
</parameters>
<!-- Fixed-pipeline fallback -->
<technique n="11">
<pass>
@ -13,7 +17,32 @@
</pass>
</technique>
<technique n="109">
<scheme>hdr-geometry</scheme>
<technique n="129">
<scheme>hdr-forward</scheme>
<pass>
<depth>
<enabled>false</enabled>
</depth>
<!-- Only render the stars on top of the skydome -->
<stencil>
<function>equal</function>
<value>0</value>
</stencil>
<cull-face>off</cull-face>
<blend>
<active>true</active>
<source>src-alpha</source>
<destination>one-minus-src-alpha</destination>
</blend>
<program>
<vertex-shader>Shaders/HDR/stars.vert</vertex-shader>
<fragment-shader>Shaders/HDR/stars.frag</fragment-shader>
</program>
<uniform>
<name>max_radiance</name>
<type>float</type>
<value><use>max-radiance</use></value>
</uniform>
</pass>
</technique>
</PropertyList>

View file

@ -77,8 +77,6 @@ void main()
kw4 * group4;
downsample /= kw_sum;
} else {
// Apply weighted distribution:
// 0.5 + 0.125 + 0.125 + 0.125 + 0.125 = 1
downsample = e*0.125;
downsample += (a+c+g+i)*0.03125;
downsample += (b+d+f+h)*0.0625;

69
Shaders/HDR/moon.frag Normal file
View file

@ -0,0 +1,69 @@
#version 330 core
layout(location = 0) out vec4 fragColor;
in VS_OUT {
vec2 texcoord;
vec3 vertex_normal;
vec3 view_vector;
} fs_in;
uniform sampler2D color_tex;
uniform sampler2D transmittance_tex;
uniform vec3 fg_SunDirection;
uniform float fg_CameraDistanceToEarthCenter;
uniform float fg_EarthRadius;
uniform vec3 fg_CameraViewUp;
const float ATMOSPHERE_RADIUS = 6471e3;
// math.glsl
float M_1_PI();
// color.glsl
vec3 eotf_inverse_sRGB(vec3 srgb);
// normalmap.glsl
vec3 perturb_normal(vec3 N, vec3 V, vec2 texcoord);
// atmos_spectral.glsl
vec4 get_sun_spectral_irradiance();
vec3 linear_srgb_from_spectral_samples(vec4 L);
// exposure.glsl
vec3 apply_exposure(vec3 color);
void main()
{
vec3 albedo = eotf_inverse_sRGB(texture(color_tex, fs_in.texcoord).rgb);
vec3 N = normalize(fs_in.vertex_normal);
N = perturb_normal(N, fs_in.view_vector, fs_in.texcoord);
float NdotL = max(dot(N, fg_SunDirection), 0.0);
vec3 V = normalize(fs_in.view_vector);
// Simple Lambertian BRDF
vec3 color = albedo * M_1_PI() * NdotL
* linear_srgb_from_spectral_samples(get_sun_spectral_irradiance());
// Apply aerial perspective
float normalized_altitude =
(fg_CameraDistanceToEarthCenter - fg_EarthRadius)
/ (ATMOSPHERE_RADIUS - fg_EarthRadius);
float cos_theta = dot(-V, fg_CameraViewUp);
vec2 uv = vec2(cos_theta * 0.5 + 0.5, clamp(normalized_altitude, 0.0, 1.0));
vec4 transmittance = texture(transmittance_tex, uv);
// The proper thing would be to have spectral albedo data for the moon,
// but that's too much work for little return. Just approximate the
// transmittance by taking the average of the four spectral samples.
color *= dot(transmittance, vec4(0.25));
// Pre-expose
color = apply_exposure(color);
// Final color = transmittance * moon color + sky inscattering
// In this frag shader we output the multiplication part, and the sky
// in-scattering is added by doing additive blending on top of the skydome.
fragColor = vec4(color, 1.0);
}

24
Shaders/HDR/moon.vert Normal file
View file

@ -0,0 +1,24 @@
#version 330 core
layout(location = 0) in vec4 pos;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec4 vertex_color;
layout(location = 3) in vec4 multitexcoord0;
out VS_OUT {
vec2 texcoord;
vec3 vertex_normal;
vec3 view_vector;
} vs_out;
uniform mat4 osg_ModelViewProjectionMatrix;
uniform mat4 osg_ModelViewMatrix;
uniform mat3 osg_NormalMatrix;
void main()
{
gl_Position = osg_ModelViewProjectionMatrix * pos;
vs_out.texcoord = multitexcoord0.st;
vs_out.vertex_normal = osg_NormalMatrix * normal;
vs_out.view_vector = (osg_ModelViewMatrix * pos).xyz;
}

View file

@ -20,6 +20,7 @@ vec3 eval_ibl(vec3 base_color, float metallic, float roughness, vec3 f0,
float occlusion, vec3 ws_N, vec3 ws_refl, float NdotV);
// aerial_perspective.glsl
vec3 add_aerial_perspective(vec3 color, vec2 coord, float depth);
// sun.glsl
vec3 get_sun_radiance(vec3 p);
// clustered.glsl
vec3 eval_scene_lights(vec3 base_color, float metallic, float roughness, vec3 f0,

14
Shaders/HDR/stars.frag Normal file
View file

@ -0,0 +1,14 @@
#version 330 core
layout(location = 0) out vec4 fragColor;
in VS_OUT {
vec4 color;
} fs_in;
uniform float max_radiance;
void main()
{
fragColor = vec4(fs_in.color.rgb * max_radiance, fs_in.color.a);
}

16
Shaders/HDR/stars.vert Normal file
View file

@ -0,0 +1,16 @@
#version 330 core
layout(location = 0) in vec4 pos;
layout(location = 2) in vec4 vertex_color;
out VS_OUT {
vec4 color;
} vs_out;
uniform mat4 osg_ModelViewProjectionMatrix;
void main()
{
gl_Position = osg_ModelViewProjectionMatrix * pos;
vs_out.color = vertex_color;
}

BIN
Textures/Sky/moon_color.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3 MiB

BIN
Textures/Sky/moon_n.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 MiB