1 /* 2 Copyright (c) 2019-2022 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 abstract 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 updateState(); 82 } 83 84 void updateState() 85 { 86 if (view) 87 { 88 state.viewMatrix = view.viewMatrix(); 89 state.invViewMatrix = view.invViewMatrix(); 90 91 state.invViewRotationMatrix = matrix3x3to4x4(matrix4x4to3x3(state.invViewMatrix)); 92 93 state.prevViewMatrix = prevViewMatrix; 94 prevViewMatrix = state.viewMatrix; 95 96 state.projectionMatrix = view.projectionMatrix(); 97 state.invProjectionMatrix = state.projectionMatrix.inverse; 98 99 state.frustum = Frustum(state.projectionMatrix * state.viewMatrix); 100 101 state.resolution = Vector2f(view.width, view.height); 102 state.zNear = view.zNear; 103 state.zFar = view.zFar; 104 105 state.cameraPosition = view.cameraPosition; 106 } 107 } 108 109 void renderEntity(Entity entity, Shader shader) 110 { 111 state.layer = entity.layer; 112 state.blurMask = entity.blurMask; 113 state.gbufferMask = entity.gbufferMask; 114 state.modelMatrix = entity.absoluteTransformation; 115 state.invModelMatrix = entity.invAbsoluteTransformation; 116 state.modelViewMatrix = state.viewMatrix * state.modelMatrix; 117 state.normalMatrix = state.modelViewMatrix.inverse.transposed; 118 state.prevModelViewMatrix = state.prevViewMatrix * entity.prevAbsoluteTransformation; 119 state.shader = shader; 120 state.opacity = entity.opacity; 121 122 if (entity.material) 123 entity.material.bind(&state); 124 else 125 defaultMaterial.bind(&state); 126 127 shader.bindParameters(&state); 128 129 entity.drawable.render(&state); 130 131 shader.unbindParameters(&state); 132 133 if (entity.material) 134 entity.material.unbind(&state); 135 else 136 defaultMaterial.unbind(&state); 137 } 138 139 void render() 140 { 141 // 142 } 143 }