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 58 Color4f sunZenithColor = Color4f(1.0, 0.9, 0.8, 1.0); 59 Color4f sunHorizonColor = Color4f(1.0, 0.3, 0.0, 1.0); 60 Quaternionf sunRotation; 61 float sunEnergy = 100.0f; 62 63 Color4f skyZenithColorAtMidday = Color4f(0.223, 0.572, 0.752, 1.0); //Color4f(0.1, 0.2, 1.0, 1.0); 64 Color4f skyZenithColorAtSunset = Color4f(0.1, 0.2, 0.4, 1.0); 65 Color4f skyZenithColorAtMidnight = Color4f(0.01, 0.01, 0.05, 1.0); 66 67 Color4f skyHorizonColorAtMidday = Color4f(0.9, 1.0, 1.0, 1.0); //Color4f(0.5, 0.6, 0.65, 1.0); 68 Color4f skyHorizonColorAtSunset = Color4f(0.87, 0.44, 0.1, 1.0); 69 Color4f skyHorizonColorAtMidnight = Color4f(0.1, 0.1, 0.1, 1.0); 70 71 float skyEnergyAtMidday = 5.0; 72 float skyEnergyAtSunset = 1.0; 73 float skyEnergyAtMidnight = 0.001; 74 75 Color4f groundColor = Color4f(0.5, 0.4, 0.4, 1.0); 76 77 float groundEnergyAtMidday = 1.0; 78 float groundEnergyAtSunset = 0.01; 79 float groundEnergyAtMidnight = 0.0; 80 81 Color4f fogColor = Color4f(0.1f, 0.1f, 0.1f, 1.0f); 82 float fogStart = 0.0f; 83 float fogEnd = 10000.0f; 84 //float fogEnergy = 0.1f; 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 bool showSun = true; 92 bool showSunHalo = true; 93 float sunSize = 1.0f; 94 float sunScattering = 0.05f; 95 96 this(Owner o) 97 { 98 super(o); 99 100 setDayTime(9, 0, 0); 101 } 102 103 void update(double dt) 104 { 105 skyZenithColor = lerpColorsBySunAngle(skyZenithColorAtMidday, skyZenithColorAtSunset, skyZenithColorAtMidnight); 106 skyHorizonColor = lerpColorsBySunAngle(skyHorizonColorAtMidday, skyHorizonColorAtSunset, skyHorizonColorAtMidnight); 107 //backgroundColor = skyZenithColor; 108 109 Vector3f sunDir = sunDirection(); 110 111 float s1 = clamp(dot(sunDir, Vector3f(0.0, 1.0, 0.0)), 0.0, 1.0); 112 float s2 = clamp(dot(sunDir, Vector3f(0.0, -1.0, 0.0)), 0.0, 1.0); 113 114 ambientConstant = (skyZenithColor + skyHorizonColor + Color4f(0.06f, 0.05f, 0.05f)) * 0.3f * lerp(lerp(0.01f, 0.001f, s2), 2.0f, s1); 115 116 skyEnergy = lerp(lerp(skyEnergyAtSunset, skyEnergyAtMidnight, s2), skyEnergyAtMidday, s1); 117 groundEnergy = lerp(lerp(groundEnergyAtSunset, groundEnergyAtMidnight, s2), groundEnergyAtMidday, s1); 118 119 if (atmosphericFog) 120 fogColor = skyHorizonColor * skyEnergy; //lerpColorsBySunAngle(skyZenithColor, skyHorizonColor, Color4f(0, 0, 0, 0)) * fogEnergy; 121 else 122 fogColor = backgroundColor; 123 } 124 125 void setDayTime(uint h, uint m, float s) 126 { 127 if (h >= 24) h = h % 24; 128 if (m >= 60) m = m % 60; 129 if (s >= 60) s = s % 60; 130 131 float t = (h * 3600.0f + m * 60.0f + s) / (3600.0f * 24.0f); 132 while (t > 1.0f) t -= 1.0f; 133 sunRotation = rotationQuaternion(Axis.x, degtorad(-(t * 360.0f - 90.0f))); 134 } 135 136 Vector3f sunDirection() 137 { 138 return sunRotation.rotate(Vector3f(0, 0, 1)); 139 } 140 141 Vector3f sunDirectionEye(Matrix4x4f viewMatrix) 142 { 143 Vector4f sunHGVector = Vector4f(sunRotation.rotate(Vector3f(0, 0, 1))); 144 sunHGVector.w = 0.0; 145 return (sunHGVector * viewMatrix).xyz; 146 } 147 148 Color4f sunColor() 149 { 150 return lerpColorsBySunAngle(sunZenithColor, sunHorizonColor, Color4f(0.0f, 0.0f, 0.0f, 1.0f)); 151 } 152 153 Color4f lerpColorsBySunAngle(Color4f atZenith, Color4f atHorizon, Color4f atNightSide) 154 { 155 float s = dot(sunDirection, Vector3f(0.0, 1.0, 0.0)); 156 Vector3f sunColor; 157 if (s < 0.01f) 158 sunColor = atNightSide; 159 else if (s < 0.08f) 160 { 161 sunColor = lerp(atHorizon, atZenith, s); 162 sunColor = lerp(sunColor, Vector3f(atNightSide), (0.07f - (s - 0.01f)) / 0.07f); 163 } 164 else 165 sunColor = lerp(Vector3f(atHorizon), Vector3f(atZenith), s); 166 return Color4f(sunColor.x, sunColor.y, sunColor.z, 1.0f); 167 } 168 }