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