1 /*
2 Copyright (c) 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.renderer;
29 
30 import dlib.core.memory;
31 
32 import dagon.core.libs;
33 import dagon.core.ownership;
34 import dagon.core.event;
35 import dagon.graphics.gbuffer;
36 import dagon.graphics.framebuffer;
37 import dagon.graphics.deferred;
38 import dagon.graphics.shadow;
39 import dagon.graphics.rc;
40 import dagon.resource.scene;
41 
42 class Renderer: Owner
43 {
44     Scene scene;
45     EventManager eventManager;
46 
47     GBuffer gbuffer;
48     Framebuffer sceneFramebuffer;
49 
50     DeferredEnvironmentPass deferredEnvPass;
51     DeferredLightPass deferredLightPass;
52 
53     CascadedShadowMap shadowMap;
54 
55     this(Scene scene, Owner o)
56     {
57         super(o);
58 
59         this.scene = scene;
60         this.eventManager = scene.eventManager;
61 
62         gbuffer = New!GBuffer(eventManager.windowWidth, eventManager.windowHeight, this);
63         sceneFramebuffer = New!Framebuffer(gbuffer, eventManager.windowWidth, eventManager.windowHeight, true, true, this);
64         shadowMap = New!CascadedShadowMap(1024, 10, 30, 200, -100, 100, this);
65 
66         deferredEnvPass = New!DeferredEnvironmentPass(gbuffer, shadowMap, this);
67         deferredLightPass = New!DeferredLightPass(gbuffer, this);
68     }
69 
70     void render(RenderingContext *rc)
71     {
72         renderPreStep(gbuffer, rc);
73         renderToTarget(sceneFramebuffer, gbuffer, rc);
74         sceneFramebuffer.swapColorTextureAttachments();
75     }
76 
77     void renderPreStep(GBuffer gbuf, RenderingContext *rc)
78     {
79         shadowMap.render(scene, rc);
80         gbuf.render(scene, rc);
81     }
82 
83     void renderToTarget(RenderTarget rt, GBuffer gbuf, RenderingContext *rc)
84     {
85         rt.bind();
86 
87         RenderingContext rcDeferred;
88         rcDeferred.initOrtho(eventManager, scene.environment, gbuf.width, gbuf.height, 0.0f, 100.0f);
89         prepareViewport(rt);
90         rt.clear(scene.environment.backgroundColor);
91 
92         glBindFramebuffer(GL_READ_FRAMEBUFFER, gbuf.fbo);
93         glBlitFramebuffer(0, 0, gbuf.width, gbuf.height, 0, 0, gbuf.width, gbuf.height, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
94         glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
95 
96         deferredEnvPass.gbuffer = gbuf;
97         deferredLightPass.gbuffer = gbuf;
98 
99         scene.renderBackgroundEntities3D(rc);
100         deferredEnvPass.render(&rcDeferred, rc);
101         deferredLightPass.render(scene, &rcDeferred, rc);
102         scene.renderTransparentEntities3D(rc);
103         scene.particleSystem.render(rc);
104 
105         rt.unbind();
106     }
107 
108     void prepareViewport(RenderTarget rt = null)
109     {
110         glEnable(GL_SCISSOR_TEST);
111         if (rt)
112         {
113             glScissor(0, 0, rt.width, rt.height);
114             glViewport(0, 0, rt.width, rt.height);
115         }
116         else
117         {
118             glScissor(0, 0, eventManager.windowWidth, eventManager.windowHeight);
119             glViewport(0, 0, eventManager.windowWidth, eventManager.windowHeight);
120         }
121         glClearColor(
122             scene.environment.backgroundColor.r,
123             scene.environment.backgroundColor.g,
124             scene.environment.backgroundColor.b, 0.0f);
125     }
126 }