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.quaternion;
39 import dlib.math.interpolation;
40 import derelict.opengl;
41 import dagon.core.ownership;
42 import dagon.graphics.texture;
43 
44 Color4f saturation(Color4f c, float s)
45 {
46     ColorHSVAf hsv = ColorHSVAf(c);
47     hsv.scaleSaturation(s);
48     return hsv.rgba;
49 }
50 
51 class Environment: Owner
52 {
53     // TODO: change/interpolate parameters based on object position?
54     
55     bool useSkyColors = false;
56     bool atmosphericFog = false;
57     
58     Color4f backgroundColor = Color4f(0.1f, 0.1f, 0.1f, 1.0f);
59     Color4f ambientConstant = Color4f(0.1f, 0.1f, 0.1f, 1.0f);
60     
61     Texture environmentMap;    
62     
63     Color4f sunZenithColor = Color4f(1.0, 0.9, 0.8, 1.0);
64     Color4f sunHorizonColor = Color4f(1.0, 0.2, 0.0, 1.0);
65     Quaternionf sunRotation;
66     float sunEnergy = 50.0f;
67        
68     Color4f skyZenithColorAtMidday = Color4f(0.1, 0.2, 1.0, 1.0);
69     Color4f skyZenithColorAtSunset = Color4f(0.1, 0.2, 0.4, 1.0);
70     Color4f skyZenithColorAtMidnight = Color4f(0.01, 0.01, 0.05, 1.0);
71     
72     Color4f skyHorizonColorAtMidday = Color4f(0.5, 0.6, 0.65, 1.0);
73     Color4f skyHorizonColorAtSunset = Color4f(0.87, 0.44, 0.1, 1.0);
74     Color4f skyHorizonColorAtMidnight = Color4f(0.1, 0.1, 0.1, 1.0);
75     
76     float skyEnergyAtMidday = 5.0;
77     float skyEnergyAtSunset = 0.5;
78     float skyEnergyAtMidnight = 0.001;
79     
80     Color4f groundColor = Color4f(0.06, 0.05, 0.05, 1.0);
81     
82     float groundEnergyAtMidday = 0.001;
83     float groundEnergyAtSunset = 0.0005;
84     float groundEnergyAtMidnight = 0.0;
85     
86     Color4f fogColor = Color4f(0.1f, 0.1f, 0.1f, 1.0f);
87     float fogStart = 0.0f;
88     float fogEnd = 10000.0f;    
89     //float fogEnergy = 0.1f;
90     
91     
92     Color4f skyZenithColor = Color4f(0.223, 0.572, 0.752, 1.0);
93     Color4f skyHorizonColor = Color4f(0.9, 1.0, 1.0, 1.0);
94     float skyEnergy = 1.0f;
95     float groundEnergy = 1.0f;
96 
97     this(Owner o)
98     {
99         super(o);
100         
101         sunRotation = rotationQuaternion(Axis.x, degtorad(-45.0f));
102     }
103     
104     void update(double dt)
105     {
106         if (useSkyColors)
107         {
108             skyZenithColor = lerpColorsBySunAngle(skyZenithColorAtMidday, skyZenithColorAtSunset, skyZenithColorAtMidnight);
109             skyHorizonColor = lerpColorsBySunAngle(skyHorizonColorAtMidday, skyHorizonColorAtSunset, skyHorizonColorAtMidnight);
110             backgroundColor = skyZenithColor;
111             
112             float s1 = clamp(dot(sunDirection, Vector3f(0.0, 1.0, 0.0)), 0.0, 1.0);
113             float s2 = clamp(dot(sunDirection, Vector3f(0.0, -1.0, 0.0)), 0.0, 1.0);
114             
115             ambientConstant = (skyZenithColor + skyHorizonColor + Color4f(0.06f, 0.05f, 0.05f)) * 0.3f * lerp(lerp(0.01f, 0.001f, s2), 2.0f, s1);
116              
117             skyEnergy = lerp(lerp(skyEnergyAtSunset, skyEnergyAtMidnight, s2), skyEnergyAtMidday, s1);
118             groundEnergy = lerp(lerp(groundEnergyAtSunset, groundEnergyAtMidnight, s2), groundEnergyAtMidday, s1);
119             
120             if (atmosphericFog)
121                 fogColor = skyHorizonColor * skyEnergy; //lerpColorsBySunAngle(skyZenithColor, skyHorizonColor, Color4f(0, 0, 0, 0)) * fogEnergy;
122             else
123                 fogColor = backgroundColor;
124         }
125         else
126         {
127             fogColor = backgroundColor;
128         }    
129     }
130     
131     Vector3f sunDirection()
132     {
133         return sunRotation.rotate(Vector3f(0, 0, 1));
134     }
135 
136     Color4f sunColor()
137     {
138         return lerpColorsBySunAngle(sunZenithColor, sunHorizonColor, Color4f(0.0f, 0.0f, 0.0f, 1.0f));
139     }
140     
141     Color4f lerpColorsBySunAngle(Color4f atZenith, Color4f atHorizon, Color4f atNightSide)
142     {
143         float s = dot(sunDirection, Vector3f(0.0, 1.0, 0.0));
144         Vector3f sunColor;
145         if (s < 0.01f)
146             sunColor = atNightSide;
147         else if (s < 0.08f)
148         {
149             sunColor = lerp(atHorizon, atZenith, s);
150             sunColor = lerp(sunColor, Vector3f(atNightSide), (0.07f - (s - 0.01f)) / 0.07f);
151         }
152         else
153             sunColor = lerp(Vector3f(atHorizon), Vector3f(atZenith), s);
154         return Color4f(sunColor.x, sunColor.y, sunColor.z, 1.0f);
155     }
156 }