1 /*
2 Copyright (c) 2017-2018 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 */
27 
28 module dagon.graphics.environment;
29 
30 import dlib.core.memory;
31 import dlib.image.image;
32 import dlib.image.unmanaged;
33 import dlib.image.color;
34 import dlib.image.render.shapes;
35 import dlib.image.hsv;
36 import dlib.math.utils;
37 import dlib.math.vector;
38 import dlib.math.matrix;
39 import dlib.math.quaternion;
40 import dlib.math.interpolation;
41 
42 import dagon.core.libs;
43 import dagon.core.ownership;
44 import dagon.graphics.texture;
45 
46 class Environment: Owner
47 {
48     // TODO: change/interpolate parameters based on object position?
49 
50     bool useSkyColors = false;
51     bool atmosphericFog = false;
52 
53     Color4f backgroundColor = Color4f(0.5f, 0.5f, 0.5f, 1.0f);
54     Color4f ambientConstant = Color4f(0.5f, 0.5f, 0.5f, 1.0f);
55 
56     Texture environmentMap;
57     Texture skyMap;
58 
59     Color4f sunZenithColor = Color4f(1.0, 0.9, 0.8, 1.0);
60     Color4f sunHorizonColor = Color4f(1.0, 0.3, 0.0, 1.0);
61     Quaternionf sunRotation;
62     float sunEnergy = 100.0f;
63 
64     Color4f skyZenithColorAtMidday = Color4f(0.223, 0.572, 0.752, 1.0); //Color4f(0.1, 0.2, 1.0, 1.0);
65     Color4f skyZenithColorAtSunset = Color4f(0.1, 0.2, 0.4, 1.0);
66     Color4f skyZenithColorAtMidnight = Color4f(0.01, 0.01, 0.05, 1.0);
67 
68     Color4f skyHorizonColorAtMidday = Color4f(0.9, 1.0, 1.0, 1.0); //Color4f(0.5, 0.6, 0.65, 1.0);
69     Color4f skyHorizonColorAtSunset = Color4f(0.87, 0.44, 0.1, 1.0);
70     Color4f skyHorizonColorAtMidnight = Color4f(0.1, 0.1, 0.1, 1.0);
71 
72     float skyEnergyAtMidday = 5.0;
73     float skyEnergyAtSunset = 2.0;
74     float skyEnergyAtMidnight = 0.01;
75 
76     Color4f groundColor = Color4f(0.5, 0.4, 0.4, 1.0);
77 
78     float groundEnergyAtMidday = 1.0;
79     float groundEnergyAtSunset = 0.01;
80     float groundEnergyAtMidnight = 0.0;
81 
82     Color4f fogColor = Color4f(0.1f, 0.1f, 0.1f, 1.0f);
83     float fogStart = 0.0f;
84     float fogEnd = 10000.0f;
85 
86     Color4f skyZenithColor = Color4f(0.223, 0.572, 0.752, 1.0);
87     Color4f skyHorizonColor = Color4f(0.9, 1.0, 1.0, 1.0);
88     float skyEnergy = 1.0f;
89     float groundEnergy = 1.0f;
90     
91     float skyBrightness = 1.0f;
92     float environmentBrightness = 1.0f;
93 
94     bool showSun = true;
95     bool showSunHalo = true;
96     float sunSize = 1.0f;
97     float sunScattering = 0.05f;
98 
99     this(Owner o)
100     {
101         super(o);
102 
103         setDayTime(9, 0, 0);
104     }
105 
106     void update(double dt)
107     {
108         skyZenithColor = lerpColorsBySunAngle(skyZenithColorAtMidday, skyZenithColorAtSunset, skyZenithColorAtMidnight);
109         skyHorizonColor = lerpColorsBySunAngle(skyHorizonColorAtMidday, skyHorizonColorAtSunset, skyHorizonColorAtMidnight);
110 
111         Vector3f sunDir = sunDirection();
112 
113         float s1 = clamp(dot(sunDir, Vector3f(0.0, 1.0, 0.0)), 0.0, 1.0);
114         float s2 = clamp(dot(sunDir, Vector3f(0.0, -1.0, 0.0)), 0.0, 1.0);
115 
116         ambientConstant = (skyZenithColor + skyHorizonColor + Color4f(0.06f, 0.05f, 0.05f)) * 0.3f * lerp(lerp(0.01f, 0.001f, s2), 2.0f, s1);
117 
118         skyEnergy = lerp(lerp(skyEnergyAtSunset, skyEnergyAtMidnight, s2), skyEnergyAtMidday, s1);
119         groundEnergy = lerp(lerp(groundEnergyAtSunset, groundEnergyAtMidnight, s2), groundEnergyAtMidday, s1);
120 
121         if (atmosphericFog)
122             fogColor = skyZenithColor * skyEnergy * 0.5f;
123         else
124             fogColor = backgroundColor;
125     }
126 
127     void setDayTime(uint h, uint m, float s)
128     {
129         if (h >= 24) h = h % 24;
130         if (m >= 60) m = m % 60;
131         if (s >= 60) s = s % 60;
132 
133         float t = (h * 3600.0f + m * 60.0f + s) / (3600.0f * 24.0f);
134         while (t > 1.0f) t -= 1.0f;
135         sunRotation = rotationQuaternion(Axis.x, degtorad(-(t * 360.0f - 90.0f)));
136     }
137 
138     Vector3f sunDirection()
139     {
140         return sunRotation.rotate(Vector3f(0, 0, 1));
141     }
142 
143     Vector3f sunDirectionEye(Matrix4x4f viewMatrix)
144     {
145         Vector4f sunHGVector = Vector4f(sunRotation.rotate(Vector3f(0, 0, 1)));
146         sunHGVector.w = 0.0;
147         return (sunHGVector * viewMatrix).xyz;
148     }
149 
150     Color4f sunColor()
151     {
152         return lerpColorsBySunAngle(sunZenithColor, sunHorizonColor, Color4f(0.0f, 0.0f, 0.0f, 1.0f));
153     }
154 
155     Color4f lerpColorsBySunAngle(Color4f atZenith, Color4f atHorizon, Color4f atNightSide)
156     {
157         float s = dot(sunDirection, Vector3f(0.0, 1.0, 0.0));
158         Vector3f sunColor;
159         if (s < 0.01f)
160             sunColor = atNightSide;
161         else if (s < 0.08f)
162         {
163             sunColor = lerp(atHorizon, atZenith, s);
164             sunColor = lerp(sunColor, Vector3f(atNightSide), (0.07f - (s - 0.01f)) / 0.07f);
165         }
166         else
167             sunColor = lerp(Vector3f(atHorizon), Vector3f(atZenith), s);
168         return Color4f(sunColor.x, sunColor.y, sunColor.z, 1.0f);
169     }
170 }