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             state.environment = pipeline.environment;
87             
88             glScissor(0, 0, outputBuffer.width, outputBuffer.height);
89             glViewport(0, 0, outputBuffer.width, outputBuffer.height);
90 
91             glEnable(GL_BLEND);
92             glBlendFunc(GL_ONE, GL_ONE);
93 
94             sunLightShader.bind();
95             foreach(entity; groupSunLights)
96             {
97                 Light light = cast(Light)entity;
98                 if (light)
99                 {
100                     if (light.shining)
101                     {
102                         state.light = light;
103                         sunLightShader.bindParameters(&state);
104                         screenSurface.render(&state);
105                         sunLightShader.unbindParameters(&state);
106                     }
107                 }
108             }
109             sunLightShader.unbind();
110 
111             glDisable(GL_DEPTH_TEST);
112             glDepthMask(GL_FALSE);
113 
114             glEnable(GL_CULL_FACE);
115             glCullFace(GL_FRONT);
116 
117             areaLightShader.bind();
118             foreach(entity; groupAreaLights)
119             {
120                 Light light = cast(Light)entity;
121                 if (light)
122                 {
123                     if (light.shining)
124                     {
125                         state.light = light;
126 
127                         state.modelMatrix =
128                             translationMatrix(light.positionAbsolute) *
129                             scaleMatrix(Vector3f(light.volumeRadius, light.volumeRadius, light.volumeRadius));
130 
131                         state.modelViewMatrix = state.viewMatrix * state.modelMatrix;
132 
133                         state.normalMatrix = state.modelViewMatrix.inverse.transposed;
134 
135                         areaLightShader.bindParameters(&state);
136                         lightVolume.render(&state);
137                         areaLightShader.unbindParameters(&state);
138                     }
139                 }
140             }
141             areaLightShader.unbind();
142 
143             glCullFace(GL_BACK);
144             glDisable(GL_CULL_FACE);
145 
146             glDepthMask(GL_TRUE);
147             glEnable(GL_DEPTH_TEST);
148 
149             glDisable(GL_BLEND);
150 
151             outputBuffer.unbind();
152         }
153     }
154 }