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.pass;
29 
30 import dlib.core.memory;
31 import dlib.core.ownership;
32 import dlib.math.vector;
33 import dlib.math.matrix;
34 import dlib.geometry.frustum;
35 import dlib.image.color;
36 
37 import dagon.core.event;
38 import dagon.core.bindings;
39 import dagon.core.time;
40 import dagon.graphics.entity;
41 import dagon.graphics.material;
42 import dagon.graphics.shader;
43 import dagon.graphics.state;
44 import dagon.render.pipeline;
45 import dagon.render.view;
46 import dagon.render.shaders.fallback;
47 
48 class RenderPass: EventListener
49 {
50     RenderPipeline pipeline;
51     RenderView view;
52     EntityGroup group;
53     GraphicsState state;
54     Material defaultMaterial;
55     FallbackShader defaultShader;
56     bool active = true;
57     bool clear = true;
58     Matrix4x4f prevViewMatrix;
59 
60     this(RenderPipeline pipeline, EntityGroup group = null)
61     {
62         super(pipeline.eventManager, pipeline);
63         this.pipeline = pipeline;
64         this.group = group;
65         pipeline.addPass(this);
66         state.reset();
67         defaultShader = New!FallbackShader(this);
68         defaultMaterial = New!Material(this);
69         prevViewMatrix = Matrix4x4f.identity;
70     }
71 
72     void update(Time t)
73     {
74         processEvents();
75 
76         state.time = t;
77         state.localTime += t.delta;
78         if (state.localTime >= 1.0f)
79             state.localTime = 0.0f;
80 
81         if (view)
82         {
83             state.viewMatrix = view.viewMatrix();
84             state.invViewMatrix = view.invViewMatrix();
85 
86             state.invViewRotationMatrix = matrix3x3to4x4(matrix4x4to3x3(state.invViewMatrix));
87 
88             state.prevViewMatrix = prevViewMatrix;
89             prevViewMatrix = state.viewMatrix;
90 
91             state.projectionMatrix = view.projectionMatrix();
92             state.invProjectionMatrix = state.projectionMatrix.inverse;
93             
94             state.frustum = Frustum(state.projectionMatrix * state.viewMatrix);
95 
96             state.resolution = Vector2f(view.width, view.height);
97             state.zNear = view.zNear;
98             state.zFar = view.zFar;
99 
100             state.cameraPosition = view.cameraPosition;
101         }
102     }
103 
104     void renderEntity(Entity entity, Shader shader)
105     {
106         state.layer = entity.layer;
107         state.blurMask = entity.blurMask;
108         state.gbufferMask = entity.gbufferMask;
109         state.modelMatrix = entity.absoluteTransformation;
110         state.invModelMatrix = entity.invAbsoluteTransformation;
111         state.modelViewMatrix = state.viewMatrix * state.modelMatrix;
112         state.normalMatrix = state.modelViewMatrix.inverse.transposed;
113         state.prevModelViewMatrix = state.prevViewMatrix * entity.prevAbsoluteTransformation;
114         state.shader = shader;
115         state.opacity = entity.opacity;
116 
117         if (entity.material)
118             entity.material.bind(&state);
119         else
120             defaultMaterial.bind(&state);
121 
122         shader.bindParameters(&state);
123 
124         entity.drawable.render(&state);
125 
126         shader.unbindParameters(&state);
127 
128         if (entity.material)
129             entity.material.unbind(&state);
130         else
131             defaultMaterial.unbind(&state);
132     }
133 
134     void render()
135     {
136         /*
137         if (view && group)
138         {
139             glScissor(view.x, view.y, view.width, view.height);
140             glViewport(view.x, view.y, view.width, view.height);
141 
142             if (clear)
143             {
144                 Color4f backgroundColor = Color4f(0.0f, 0.0f, 0.0f, 1.0f);
145                 state.environment = pipeline.environment;
146                 if (state.environment)
147                     backgroundColor = state.environment.backgroundColor;
148                 
149                 glClearColor(
150                     backgroundColor.r,
151                     backgroundColor.g,
152                     backgroundColor.b,
153                     1.0f);
154                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
155             }
156 
157             defaultShader.bind();
158             foreach(entity; group)
159             if (entity.visible && entity.drawable)
160             {
161                 renderEntity(entity, defaultShader);
162             }
163             defaultShader.unbind();
164         }
165         */
166     }
167 }