1 /*
2 Copyright (c) 2017-2020 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.light;
29 
30 import std.stdio;
31 import std.math;
32 import std.conv;
33 import std.random;
34 
35 import dlib.core.memory;
36 import dlib.core.ownership;
37 import dlib.math.vector;
38 import dlib.math.matrix;
39 import dlib.math.transformation;
40 import dlib.math.quaternion;
41 import dlib.container.array;
42 import dlib.image.color;
43 
44 import dagon.core.bindings;
45 import dagon.graphics.state;
46 import dagon.graphics.entity;
47 import dagon.graphics.shadowmap;
48 import dagon.graphics.csm;
49 
50 enum LightType
51 {
52     AreaSphere = 1,
53     AreaTube = 2,
54     Sun = 3,
55     Spot = 4
56 }
57 
58 class Light: Entity
59 {
60     bool shining;
61     Color4f color;
62     float volumeRadius; // max light attenuation radius
63     float radius;
64     float length;
65     float energy;
66     float spotOuterCutoff;
67     float spotInnerCutoff;
68     LightType type;
69     bool shadowEnabled;
70     bool scatteringEnabled;
71     float scattering;
72     float mediumDensity;
73     uint scatteringSamples;
74     float scatteringMaxRandomStepOffset;
75     bool scatteringUseShadow;
76     ShadowMap _shadowMap;
77     float diffuse;
78     float specular;
79 
80     this(EntityManager manager)
81     {
82         super(manager);
83         visible = false;
84         castShadow = false;
85         shining = true;
86         length = 1.0f;
87         color = Color4f(1.0f, 1.0f, 1.0f, 1.0f);
88         volumeRadius = 1.0f;
89         radius = 0.0f;
90         energy = 1.0f;
91         spotOuterCutoff = 30.0f;
92         spotInnerCutoff = 15.0f;
93         type = LightType.AreaSphere;
94         shadowEnabled = false;
95         scatteringEnabled = false;
96         scattering = 0.05f;
97         mediumDensity = 0.5f;
98         scatteringSamples = 20;
99         scatteringMaxRandomStepOffset = 0.2f;
100         scatteringUseShadow = false;
101         diffuse = 1.0f;
102         specular = 1.0f;
103     }
104     
105     ShadowMap shadowMap()
106     {
107         if (_shadowMap is null && shadowEnabled)
108         {
109             if (type == LightType.Sun)
110                 _shadowMap = New!CascadedShadowMap(this, this);
111         }
112         
113         return _shadowMap;
114     }
115 }