1 /*
2 Copyright (c) 2019 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.modelMatrix = entity.absoluteTransformation;
108         state.invModelMatrix = entity.invAbsoluteTransformation;
109         state.modelViewMatrix = state.viewMatrix * state.modelMatrix;
110         state.normalMatrix = state.modelViewMatrix.inverse.transposed;
111         state.prevModelViewMatrix = state.prevViewMatrix * entity.prevAbsoluteTransformation;
112         state.shader = shader;
113         state.opacity = entity.opacity;
114 
115         if (entity.material)
116             entity.material.bind(&state);
117         else
118             defaultMaterial.bind(&state);
119 
120         shader.bindParameters(&state);
121 
122         entity.drawable.render(&state);
123 
124         shader.unbindParameters(&state);
125 
126         if (entity.material)
127             entity.material.unbind(&state);
128         else
129             defaultMaterial.unbind(&state);
130     }
131 
132     void render()
133     {
134         if (view && group)
135         {
136             glScissor(view.x, view.y, view.width, view.height);
137             glViewport(view.x, view.y, view.width, view.height);
138 
139             if (clear)
140             {
141                 Color4f backgroundColor = Color4f(0.0f, 0.0f, 0.0f, 1.0f);
142                 if (state.environment)
143                     backgroundColor = state.environment.backgroundColor;
144 
145                 glClearColor(
146                     backgroundColor.r,
147                     backgroundColor.g,
148                     backgroundColor.b,
149                     backgroundColor.a);
150                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
151             }
152 
153             defaultShader.bind();
154             foreach(entity; group)
155             if (entity.visible && entity.drawable)
156             {
157                 renderEntity(entity, defaultShader);
158             }
159             defaultShader.unbind();
160         }
161     }
162 }