# Atmospheric Scattering with SkyDome, shader implementation

This topic is 892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I'm trying to implement an atmospheric scattering in openGL. I'm using this "paper" as tutorial:
http://developer.amd.com/wordpress/media/2012/10/GDC_02_HoffmanPreetham.pdf

However I have some difficulties to understand certain points and to figure out some constants.

Basically I've to implement these formulas:
[attachment=31800:Screenshot from 2016-05-08 13-41-18.png]

Firstly i don't know if s is the distance from eye to the dome or the distance from eye to the light source (here sun) position.
Same for the angle theta I can't figure out if it's the angle from ground to sun or to the dome position the eye is looking at.

Secondly in this slide:
[attachment=31801:Screenshot from 2016-05-08 13-46-07.png]

It tells me the blue color of the sky will appears. I know it's cause of rayleigh scattering but there is something i can't understand. All the calculation in the formulas above give me a scalar: so how a white light of the sun wich is basically a vec3(1,1,1), will become blue when I multiply it by scalars, it will only get in gray scale because I will have for result for example vec3(0.8,0.8,0.8). I mean , if some different sky color appears, I must multiply the sun light with a vec3 to change the RGB value differently.

##### Share on other sites

The ?R parameter is an RGB vector.

In my code, it's {0.0000055, 0.000013, 0.0000224}

##### Share on other sites

Thank you :D

Could you give me some other of your parameter, because i have trouble to find it on web. I mean all I get isn't as an RGB vector.

If you could give me your Esun and Bm parameter ?

##### Share on other sites

A person called M?rti?š Up?tis made a Preetham implementation in Blender Game Engine. I could try to find the file. However, he found out that using Hosek-Wilkie is way better idea. Currently he's been working on the Hosek-Wilkie shader and has had some great progress.

##### Share on other sites

Could you give me some other of your parameter, because i have trouble to find it on web. I mean all I get isn't as an RGB vector.

If you could give me your Esun and Bm parameter ?

My sky is based off this article, which has all the numbers in it's accompanying source code: http://www.scratchapixel.com/old/lessons/3d-advanced-lessons/simulating-the-colors-of-the-sky/atmospheric-scattering

?M is 0.000021

Esun seems arbitrary; they use 20 in this article. I guess you can search for "irradiance of sun on earth" if you're using real units in your renderer.

##### Share on other sites

Thank you a lot :)
I think it will really help me ;)

##### Share on other sites

Hi, I encountered some difficulties to implement my shader.

Here is the code for the sky shader:

#version 330

in vec3 vpoint;
in vec2 vtexcoord;

out vec2 uv;

out vec3 atmos;

uniform mat4 M;
uniform mat4 V;
uniform mat4 P;

mat4 MVP = P*V*M;

//uniform vec3 lpos;
vec3 lpos = vec3(100,0,0);
uniform vec3 cpos;

vec3 br = vec3(5.5e-6, 13.0e-6, 22.4e-6);
vec3 bm = vec3(21e-6);

float g = -0.75f;

vec3 Esun = vec3(2000,2000,2000);

vec3 Br(float theta){
return 3/(16*3.14) * br * (1+cos(theta)*cos(theta));
}

vec3 Bm(float theta){
return 1/(4*3.14) * bm * ((1 - g)*(1 - g))/(pow(1+g*g-2*g*cos(theta),3/2));
}

vec3 atmospheric(float theta, float s){
return (Br(theta)*Bm(theta))/(br+bm) * Esun * (1- exp( -(br+bm)*s ));
}

void main() {

gl_Position = MVP * vec4(vpoint, 1.0);
uv = vtexcoord;

vec3 domePos = vec3(M*vec4(vpoint,1.0));

vec3 ldir = lpos - domePos;

float s = length(domePos-cpos);
float theta = acos(dot(normalize(ldir-domePos),normalize(domePos-cpos)*vec3(1,1,0)));

atmos = atmospheric(theta,s)*1000000*5;
}


I don't get what I'm expected, here is what I get:

http://imgur.com/q6uEzE6

I only have the blue, and no redish sunset, yet the sun is low and according to the different tutorials I have seen, i should see some redish color appear when the sun goes low.

##### Share on other sites

Maybe you've got too high exprossure or something like that? I'm not sure about it.

##### Share on other sites
e.g. reinhard:
atmos = pow( clamp(atmos / (atmos+1.0),0.0,1.0), vec3(1.0/2.2) );
or logarithmic:
atmos = pow( clamp(smoothstep(0.0, 12.0, log2(1.0+atmos)),0.0,1.0), vec3(1.0/2.2) );

##### Share on other sites

For the exposure the problem is that is I reduce the exposure, the scene get dark quite quickly.

I implemented the tone mapping you gave me, it gives a much homogeneous scene :) I didn't know this technic.

http://imgur.com/8TTtvGx

However I can't find why I don't get the effect of the scattering. Maybe the mie scattering isn't good, because rayleigh seems "working" since the blue color is present.

1. 1
Rutin
31
2. 2
3. 3
4. 4
5. 5

• 13
• 16
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632962
• Total Posts
3009505
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!