1 /*
2 Copyright (c) 2019-2022 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.render.shaders.arealight;
29 
30 import std.stdio;
31 import std.math;
32 
33 import dlib.core.memory;
34 import dlib.core.ownership;
35 import dlib.math.vector;
36 import dlib.math.matrix;
37 import dlib.math.transformation;
38 import dlib.math.interpolation;
39 import dlib.math.utils;
40 import dlib.image.color;
41 import dlib.text.str;
42 
43 import dagon.core.bindings;
44 import dagon.graphics.shader;
45 import dagon.graphics.state;
46 import dagon.graphics.light;
47 
48 class AreaLightShader: Shader
49 {
50     String vs, fs;
51 
52     this(Owner owner)
53     {
54         vs = Shader.load("data/__internal/shaders/AreaLight/AreaLight.vert.glsl");
55         fs = Shader.load("data/__internal/shaders/AreaLight/AreaLight.frag.glsl");
56 
57         auto myProgram = New!ShaderProgram(vs.toString, fs.toString, this);
58         super(myProgram, owner);
59     }
60 
61     ~this()
62     {
63         vs.free();
64         fs.free();
65     }
66 
67     override void bindParameters(GraphicsState* state)
68     {
69         setParameter("viewMatrix", state.viewMatrix);
70         setParameter("invViewMatrix", state.invViewMatrix);
71         setParameter("modelViewMatrix", state.modelViewMatrix);
72         setParameter("projectionMatrix", state.projectionMatrix);
73         setParameter("invProjectionMatrix", state.invProjectionMatrix);
74         setParameter("resolution", state.resolution);
75         setParameter("zNear", state.zNear);
76         setParameter("zFar", state.zFar);
77 
78         // Environment
79         if (state.environment)
80         {
81             setParameter("fogColor", state.environment.fogColor);
82             setParameter("fogStart", state.environment.fogStart);
83             setParameter("fogEnd", state.environment.fogEnd);
84         }
85         else
86         {
87             setParameter("fogColor", Color4f(0.5f, 0.5f, 0.5f, 1.0f));
88             setParameter("fogStart", 0.0f);
89             setParameter("fogEnd", 1000.0f);
90         }
91 
92         // Light
93         Vector3f lightPos;
94         Color4f lightColor;
95         float lightEnergy = 1.0f;
96         if (state.light)
97         {
98             auto light = state.light;
99 
100             lightPos = light.positionAbsolute * state.viewMatrix;
101             lightColor = light.color;
102             lightEnergy = light.energy;
103 
104             if (light.type == LightType.AreaSphere)
105             {
106                 setParameterSubroutine("lightRadiance", ShaderType.Fragment, "lightRadianceAreaSphere");
107             }
108             else if (light.type == LightType.AreaTube)
109             {
110                 Vector3f lightPosition2Eye = (light.positionAbsolute + light.directionAbsolute * light.length) * state.viewMatrix;
111                 setParameter("lightPosition2", lightPosition2Eye);
112                 setParameterSubroutine("lightRadiance", ShaderType.Fragment, "lightRadianceAreaTube");
113             }
114             else if (light.type == LightType.Spot)
115             {
116                 setParameter("lightSpotCosCutoff", cos(degtorad(light.spotOuterCutoff)));
117                 setParameter("lightSpotCosInnerCutoff", cos(degtorad(light.spotInnerCutoff)));
118                 Vector4f lightDirHg = Vector4f(light.directionAbsolute);
119                 lightDirHg.w = 0.0;
120                 Vector3f spotDirection = (lightDirHg * state.viewMatrix).xyz;
121                 setParameter("lightSpotDirection", spotDirection);
122                 setParameterSubroutine("lightRadiance", ShaderType.Fragment, "lightRadianceSpot");
123             }
124             else // unsupported light type
125             {
126                 setParameterSubroutine("lightRadiance", ShaderType.Fragment, "lightRadianceFallback");
127             }
128 
129             setParameter("lightPosition", lightPos);
130             setParameter("lightColor", lightColor);
131             setParameter("lightEnergy", lightEnergy);
132             setParameter("lightRadius", light.volumeRadius);
133             setParameter("lightAreaRadius", light.radius);
134             
135             setParameter("lightDiffuse", light.diffuse);
136             setParameter("lightSpecular", light.specular);
137         }
138         else
139         {
140             setParameterSubroutine("lightRadiance", ShaderType.Fragment, "lightRadianceFallback");
141         }
142 
143         // Texture 0 - color buffer
144         glActiveTexture(GL_TEXTURE0);
145         glBindTexture(GL_TEXTURE_2D, state.colorTexture);
146         setParameter("colorBuffer", 0);
147 
148         // Texture 1 - depth buffer
149         glActiveTexture(GL_TEXTURE1);
150         glBindTexture(GL_TEXTURE_2D, state.depthTexture);
151         setParameter("depthBuffer", 1);
152 
153         // Texture 2 - normal buffer
154         glActiveTexture(GL_TEXTURE2);
155         glBindTexture(GL_TEXTURE_2D, state.normalTexture);
156         setParameter("normalBuffer", 2);
157 
158         // Texture 3 - pbr buffer
159         glActiveTexture(GL_TEXTURE3);
160         glBindTexture(GL_TEXTURE_2D, state.pbrTexture);
161         setParameter("pbrBuffer", 3);
162 
163         // Texture 5 - occlusion buffer
164         if (glIsTexture(state.occlusionTexture))
165         {
166             glActiveTexture(GL_TEXTURE5);
167             glBindTexture(GL_TEXTURE_2D, state.occlusionTexture);
168             setParameter("occlusionBuffer", 5);
169             setParameter("haveOcclusionBuffer", true);
170         }
171         else
172         {
173             setParameter("haveOcclusionBuffer", false);
174         }
175 
176         glActiveTexture(GL_TEXTURE0);
177 
178         super.bindParameters(state);
179     }
180 
181     override void unbindParameters(GraphicsState* state)
182     {
183         super.unbindParameters(state);
184 
185         glActiveTexture(GL_TEXTURE0);
186         glBindTexture(GL_TEXTURE_2D, 0);
187 
188         glActiveTexture(GL_TEXTURE1);
189         glBindTexture(GL_TEXTURE_2D, 0);
190 
191         glActiveTexture(GL_TEXTURE2);
192         glBindTexture(GL_TEXTURE_2D, 0);
193 
194         glActiveTexture(GL_TEXTURE3);
195         glBindTexture(GL_TEXTURE_2D, 0);
196 
197         glActiveTexture(GL_TEXTURE5);
198         glBindTexture(GL_TEXTURE_2D, 0);
199 
200         glActiveTexture(GL_TEXTURE0);
201     }
202 }