Skip to content

Commit 4c73ed8

Browse files
authored
Merge pull request #1 from clayjohn/windows
Windows version
2 parents da0db27 + 72bba1f commit 4c73ed8

File tree

11 files changed

+505
-524
lines changed

11 files changed

+505
-524
lines changed

assets/shaders/atmo.frag

+55-143
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,61 @@
11
#version 330 core
2-
in vec3 ourColor;
3-
in vec3 TexCoords;
42

5-
//uniform vec2 resolution;
6-
const vec2 resolution = vec2(512.0);
3+
uniform vec2 resolution;
74
uniform float time;
85
uniform mat4 MVPM;
9-
uniform int check;
10-
out vec4 color;
116

12-
// ----------------------------------------------------------------------------
13-
// Rayleigh and Mie scattering atmosphere system
14-
//
15-
// implementation of the techniques described here:
16-
// http://www.scratchapixel.com/old/lessons/3d-advanced-lessons/simulating-the-colors-of-the-sky/atmospheric-scattering/
17-
// ----------------------------------------------------------------------------
7+
out vec4 color;
188

19-
#define _in(T) const in T
20-
#define _inout(T) inout T
21-
#define _out(T) out T
22-
#define _begin(type) type (
23-
#define _end )
24-
#define mul(a, b) (a) * (b)
9+
/*
10+
Atmospheric scattering based off of:
11+
*/
2512

2613
#define PI 3.14159265359
14+
#define BIAS 1e-4 // small offset to avoid self-intersections
15+
16+
// scattering coefficients at sea level (m)
17+
const vec3 betaR = vec3(5.5e-6, 13.0e-6, 22.4e-6); // Rayleigh
18+
const vec3 betaM = vec3(21e-6); // Mie
19+
20+
// scale height (m)
21+
// thickness of the atmosphere if its density were uniform
22+
const float hR = 7994.0; // Rayleigh
23+
const float hM = 1200.0; // Mie
24+
25+
const float earth_radius = 6360e3; // (m)
26+
const float atmosphere_radius = 6420e3; // (m)
27+
28+
vec3 sun_dir = vec3(0, 1, 0);
29+
const float sun_power = 30.0;
30+
31+
32+
const int num_samples = 16;
33+
const int num_samples_light = 8;
34+
2735

28-
// Shadertoy specific uniforms
29-
#define u_res resolution
30-
#define u_time time
3136

3237
struct ray_t {
3338
vec3 origin;
3439
vec3 direction;
3540
};
36-
#define BIAS 1e-4 // small offset to avoid self-intersections
3741

3842
struct sphere_t {
3943
vec3 origin;
4044
float radius;
41-
int material;
4245
};
4346

44-
struct plane_t {
45-
vec3 direction;
46-
float distance;
47-
int material;
48-
};
47+
const sphere_t atmosphere = sphere_t(vec3(0, 0, 0), atmosphere_radius);
4948

50-
int check_pos(vec2 x, float size) {
51-
return int(mod(floor(x.x), size) + mod(floor(x.y), size)*size);
52-
}
5349

54-
mat3 rotate_around_x(_in(float) angle_degrees)
50+
mat3 rotate_around_x(const in float angle_degrees)
5551
{
5652
float angle = radians(angle_degrees);
5753
float _sin = sin(angle);
5854
float _cos = cos(angle);
5955
return mat3(1, 0, 0, 0, _cos, -_sin, 0, _sin, _cos);
6056
}
6157

62-
63-
ray_t get_primary_ray(
64-
_in(vec3) cam_local_point,
65-
_inout(vec3) cam_origin,
66-
_inout(vec3) cam_look_at
67-
){
68-
vec3 fwd = normalize(cam_look_at - cam_origin);
69-
vec3 up = vec3(0, 1, 0);
70-
vec3 right = cross(up, fwd);
71-
up = cross(fwd, right);
72-
73-
ray_t r = _begin(ray_t)
74-
cam_origin,
75-
normalize(fwd + up * cam_local_point.y + right * cam_local_point.x)
76-
_end;
77-
return r;
78-
}
79-
80-
bool isect_sphere(_in(ray_t) ray, _in(sphere_t) sphere, _inout(float) t0, _inout(float) t1)
58+
bool isect_sphere(const in ray_t ray, const in sphere_t sphere, inout float t0, inout float t1)
8159
{
8260
vec3 rc = sphere.origin - ray.origin;
8361
float radius2 = sphere.radius * sphere.radius;
@@ -91,15 +69,6 @@ bool isect_sphere(_in(ray_t) ray, _in(sphere_t) sphere, _inout(float) t0, _inout
9169
return true;
9270
}
9371

94-
// scattering coefficients at sea level (m)
95-
const vec3 betaR = vec3(5.5e-6, 13.0e-6, 22.4e-6); // Rayleigh
96-
const vec3 betaM = vec3(21e-6); // Mie
97-
98-
// scale height (m)
99-
// thickness of the atmosphere if its density were uniform
100-
const float hR = 7994.0; // Rayleigh
101-
const float hM = 1200.0; // Mie
102-
10372
float rayleigh_phase_func(float mu)
10473
{
10574
return
@@ -112,43 +81,19 @@ float rayleigh_phase_func(float mu)
11281
// represents the average cosine of the scattered directions
11382
// 0 is isotropic scattering
11483
// > 1 is forward scattering, < 1 is backwards
115-
const float g = 0.76;
11684
float henyey_greenstein_phase_func(float mu)
11785
{
86+
const float g = 0.76;
11887
return
11988
(1. - g*g)
12089
/ //---------------------------------------------
12190
((4. + PI) * pow(1. + g*g - 2.*g*mu, 1.5));
12291
}
12392

124-
// Schlick Phase Function factor
125-
// Pharr and Humphreys [2004] equivalence to g above
126-
const float k = 1.55*g - 0.55 * (g*g*g);
127-
float schlick_phase_func(float mu)
128-
{
129-
return
130-
(1. - k*k)
131-
/ //-------------------------------------------
132-
(4. * PI * (1. + k*mu) * (1. + k*mu));
133-
}
134-
135-
const float earth_radius = 6360e3; // (m)
136-
const float atmosphere_radius = 6420e3; // (m)
137-
138-
vec3 sun_dir = vec3(0, 1, 0);
139-
const float sun_power = 20.0;
140-
141-
const sphere_t atmosphere = _begin(sphere_t)
142-
vec3(0, 0, 0), atmosphere_radius, 0
143-
_end;
144-
145-
const int num_samples = 16;
146-
const int num_samples_light = 8;
147-
14893
bool get_sun_light(
149-
_in(ray_t) ray,
150-
_inout(float) optical_depthR,
151-
_inout(float) optical_depthM
94+
const in ray_t ray,
95+
inout float optical_depthR,
96+
inout float optical_depthM
15297
){
15398
float t0 = 0.0;
15499
float t1 = 0.0;
@@ -174,7 +119,7 @@ bool get_sun_light(
174119
return true;
175120
}
176121

177-
vec3 get_incident_light(_in(ray_t) ray)
122+
vec3 get_incident_light(const in ray_t ray)
178123
{
179124
// "pierce" the atmosphere with the viewing ray
180125
float t0 = 0.0;
@@ -197,11 +142,7 @@ vec3 get_incident_light(_in(ray_t) ray)
197142
// * integrates to 1 over the entire sphere of directions
198143
float phaseR = rayleigh_phase_func(mu);
199144
float phaseM =
200-
#if 1
201-
henyey_greenstein_phase_func(mu);
202-
#else
203-
schlick_phase_func(mu);
204-
#endif
145+
henyey_greenstein_phase_func(mu);
205146

206147
// optical depth (or "average density")
207148
// represents the accumulated extinction coefficients
@@ -226,10 +167,7 @@ vec3 get_incident_light(_in(ray_t) ray)
226167
optical_depthM += hm;
227168

228169
// gather the sunlight
229-
ray_t light_ray = _begin(ray_t)
230-
s,
231-
sun_dir
232-
_end;
170+
ray_t light_ray = ray_t(s, sun_dir);
233171
float optical_depth_lightR = 0.;
234172
float optical_depth_lightM = 0.;
235173
bool overground = get_sun_light(
@@ -256,64 +194,38 @@ vec3 get_incident_light(_in(ray_t) ray)
256194
sumM * phaseM * betaM);
257195
}
258196

259-
vec3 ACESFilm( vec3 x )
260-
{
261-
float a = 2.51f;
262-
float b = 0.03f;
263-
float c = 2.43f;
264-
float d = 0.59f;
265-
float e = 0.14f;
266-
return clamp((x*(a*x+b))/(x*(c*x+d)+e), 0.0, 1.0);
197+
vec3 U2Tone(vec3 x) {
198+
const float A = 0.15;
199+
const float B = 0.50;
200+
const float C = 0.10;
201+
const float D = 0.20;
202+
const float E = 0.02;
203+
const float F = 0.30;
204+
205+
return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;
267206
}
268207

269208
void main()
270209
{
271-
vec2 aspect_ratio = vec2(u_res.x / u_res.y, 1);
272-
float fov = tan(radians(45.0));
273-
vec2 point_ndc = gl_FragCoord.xy / u_res.xy;
274-
vec3 point_cam = vec3((2.0 * point_ndc - 1.0) * aspect_ratio * fov, 1.0);
210+
vec2 aspect_ratio = vec2(resolution.x / resolution.y, 1);
211+
vec2 point_ndc = gl_FragCoord.xy / resolution.xy;
212+
vec3 point_cam = vec3((2.0 * point_ndc - 1.0) * aspect_ratio, 1.0);
275213

276214
vec4 worldPos = (inverse((MVPM))*vec4(point_cam, 1.0));
277215
worldPos.xyz /= worldPos.w;
278216
point_cam = normalize(worldPos.xyz);
279217

280-
281-
if (check_pos(gl_FragCoord.xy/4.0, 4.0)!=check){
282-
//reprojection from http://john-chapman-graphics.blogspot.ca/2013/01/what-is-motion-blur-motion-pictures-are.html
283-
//look into running all this on cpu
284-
discard;
285-
/*
286-
vec4 current = vec4((2.0 * point_ndc - 1.0) * aspect_ratio * fov, 1.0, 1.0);
287-
current = inverse(MVPM) * current;
288-
vec4 previous = LFMVPM * current;
289-
previous.xyz /= previous.w;
290-
previous.xy = previous.xy * 0.5 + 0.5;
291-
vec2 blurVec = previous.xy - TexCoords.xy;
292-
vec2 lookup = TexCoords.xy+blurVec;
293-
float mip = 0.0;
294-
if (lookup.x<0.0||lookup.x>1.0||lookup.y<0.0||lookup.y>1.0) {
295-
lookup = clamp(lookup, 0.0, 1.0);
296-
lookup = TexCoords.xy;
297-
mip = 1.0;
298-
}
299-
color = texture(lastFrame, lookup, mip);
300-
*/
301-
} else {
302-
303-
vec3 col = vec3(0);
304-
if (point_cam.y>0.0) {
305-
// sun
306-
mat3 rot = rotate_around_x(-abs(sin(u_time / 20.)) * 90.);
218+
vec3 col = vec3(0.4);
219+
if (point_cam.y>-0.05) {
220+
// sun
221+
mat3 rot = rotate_around_x(-abs(sin(time / 20.)) * 90.);
307222
sun_dir *= rot;
308-
309-
vec3 eye = vec3 (0, earth_radius + 1., 0);
310-
vec3 look_at = vec3 (0, earth_radius + 1.0, -1);
311223

312-
//ray_t ray = get_primary_ray(point_cam, eye, look_at);
313224
ray_t ray = ray_t(vec3(0.0, earth_radius+1.0, 0.0), point_cam);
314225
col = get_incident_light(ray);
315226
}
316-
col = ACESFilm(col);
227+
col = U2Tone(col);
228+
col /= U2Tone(vec3(2.5));
229+
col = sqrt(col);
317230
color = vec4(col, 1);
318-
}
319231
}

assets/shaders/atmo.vert

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
#version 330 core
2-
in vec3 position;
3-
in vec3 color;
4-
in vec3 texCoords;
5-
6-
out vec3 ourColor;
7-
out vec3 TexCoords;
8-
out vec4 fragPos;
9-
10-
uniform mat4 MVPM;
2+
layout (location = 0) in vec2 position;
113

124
void main()
135
{
14-
15-
gl_Position = vec4(position.xy, 0.0, 1.0);
16-
ourColor = color;
17-
TexCoords = texCoords;
18-
fragPos = gl_Position;
6+
gl_Position = vec4(position, 0.0, 1.0);
197
}

assets/shaders/basic.vert

-17
This file was deleted.

0 commit comments

Comments
 (0)