1 /*
2 Copyright (c) 2019-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.render.deferred.lightpass;
29 
30 import std.stdio;
31 
32 import dlib.core.memory;
33 import dlib.core.ownership;
34 import dlib.math.vector;
35 import dlib.math.matrix;
36 import dlib.math.transformation;
37 
38 import dagon.core.bindings;
39 import dagon.graphics.screensurface;
40 import dagon.graphics.entity;
41 import dagon.graphics.light;
42 import dagon.graphics.shapes;
43 import dagon.render.pipeline;
44 import dagon.render.pass;
45 import dagon.render.framebuffer;
46 import dagon.render.gbuffer;
47 import dagon.render.shaders.sunlight;
48 import dagon.render.shaders.arealight;
49 
50 class DeferredLightPass: RenderPass
51 {
52     GBuffer gbuffer;
53     ScreenSurface screenSurface;
54     ShapeSphere lightVolume;
55     SunLightShader sunLightShader;
56     AreaLightShader areaLightShader;
57     Framebuffer outputBuffer;
58     Framebuffer occlusionBuffer;
59     EntityGroup groupSunLights;
60     EntityGroup groupAreaLights;
61 
62     this(RenderPipeline pipeline, GBuffer gbuffer)
63     {
64         super(pipeline);
65         this.gbuffer = gbuffer;
66         screenSurface = New!ScreenSurface(this);
67         lightVolume = New!ShapeSphere(1.0f, 8, 4, false, this);
68         sunLightShader = New!SunLightShader(this);
69         areaLightShader = New!AreaLightShader(this);
70     }
71 
72     override void render()
73     {
74         if (groupSunLights && groupAreaLights && outputBuffer && gbuffer)
75         {
76             outputBuffer.bind();
77 
78             state.colorTexture = gbuffer.colorTexture;
79             state.depthTexture = gbuffer.depthTexture;
80             state.normalTexture = gbuffer.normalTexture;
81             state.pbrTexture = gbuffer.pbrTexture;
82             if (occlusionBuffer)
83                 state.occlusionTexture = occlusionBuffer.colorTexture;
84             else
85                 state.occlusionTexture = 0;
86 
87             glScissor(0, 0, outputBuffer.width, outputBuffer.height);
88             glViewport(0, 0, outputBuffer.width, outputBuffer.height);
89 
90             glEnable(GL_BLEND);
91             glBlendFunc(GL_ONE, GL_ONE);
92 
93             sunLightShader.bind();
94             foreach(entity; groupSunLights)
95             {
96                 Light light = cast(Light)entity;
97                 if (light)
98                 {
99                     if (light.shining)
100                     {
101                         state.light = light;
102                         sunLightShader.bindParameters(&state);
103                         screenSurface.render(&state);
104                         sunLightShader.unbindParameters(&state);
105                     }
106                 }
107             }
108             sunLightShader.unbind();
109 
110             glDisable(GL_DEPTH_TEST);
111             glDepthMask(GL_FALSE);
112 
113             glEnable(GL_CULL_FACE);
114             glCullFace(GL_FRONT);
115 
116             areaLightShader.bind();
117             foreach(entity; groupAreaLights)
118             {
119                 Light light = cast(Light)entity;
120                 if (light)
121                 {
122                     if (light.shining)
123                     {
124                         state.light = light;
125 
126                         state.modelMatrix =
127                             translationMatrix(light.positionAbsolute) *
128                             scaleMatrix(Vector3f(light.volumeRadius, light.volumeRadius, light.volumeRadius));
129 
130                         state.modelViewMatrix = state.viewMatrix * state.modelMatrix;
131 
132                         state.normalMatrix = state.modelViewMatrix.inverse.transposed;
133 
134                         areaLightShader.bindParameters(&state);
135                         lightVolume.render(&state);
136                         areaLightShader.unbindParameters(&state);
137                     }
138                 }
139             }
140             areaLightShader.unbind();
141 
142             glCullFace(GL_BACK);
143             glDisable(GL_CULL_FACE);
144 
145             glDepthMask(GL_TRUE);
146             glEnable(GL_DEPTH_TEST);
147 
148             glDisable(GL_BLEND);
149 
150             outputBuffer.unbind();
151         }
152     }
153 }