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.shaders.arealight; 29 30 import std.stdio; 31 import std.math; 32 33 import dlib.core.memory; 34 import dlib.core.ownership; 35 import dlib.math.vector; 36 import dlib.math.matrix; 37 import dlib.math.transformation; 38 import dlib.math.interpolation; 39 import dlib.math.utils; 40 import dlib.image.color; 41 import dlib.text.str; 42 43 import dagon.core.bindings; 44 import dagon.graphics.shader; 45 import dagon.graphics.state; 46 import dagon.graphics.light; 47 48 class AreaLightShader: Shader 49 { 50 String vs, fs; 51 52 this(Owner owner) 53 { 54 vs = Shader.load("data/__internal/shaders/AreaLight/AreaLight.vert.glsl"); 55 fs = Shader.load("data/__internal/shaders/AreaLight/AreaLight.frag.glsl"); 56 57 auto myProgram = New!ShaderProgram(vs.toString, fs.toString, this); 58 super(myProgram, owner); 59 } 60 61 ~this() 62 { 63 vs.free(); 64 fs.free(); 65 } 66 67 override void bindParameters(GraphicsState* state) 68 { 69 setParameter("viewMatrix", state.viewMatrix); 70 setParameter("invViewMatrix", state.invViewMatrix); 71 setParameter("modelViewMatrix", state.modelViewMatrix); 72 setParameter("projectionMatrix", state.projectionMatrix); 73 setParameter("invProjectionMatrix", state.invProjectionMatrix); 74 setParameter("resolution", state.resolution); 75 setParameter("zNear", state.zNear); 76 setParameter("zFar", state.zFar); 77 78 // Environment 79 if (state.environment) 80 { 81 setParameter("fogColor", state.environment.fogColor); 82 setParameter("fogStart", state.environment.fogStart); 83 setParameter("fogEnd", state.environment.fogEnd); 84 } 85 else 86 { 87 setParameter("fogColor", Color4f(0.5f, 0.5f, 0.5f, 1.0f)); 88 setParameter("fogStart", 0.0f); 89 setParameter("fogEnd", 1000.0f); 90 } 91 92 // Light 93 Vector3f lightPos; 94 Color4f lightColor; 95 float lightEnergy = 1.0f; 96 if (state.light) 97 { 98 auto light = state.light; 99 100 lightPos = light.positionAbsolute * state.viewMatrix; 101 lightColor = light.color; 102 lightEnergy = light.energy; 103 104 if (light.type == LightType.AreaSphere) 105 { 106 setParameterSubroutine("lightRadiance", ShaderType.Fragment, "lightRadianceAreaSphere"); 107 } 108 else if (light.type == LightType.AreaTube) 109 { 110 Vector3f lightPosition2Eye = (light.positionAbsolute + light.directionAbsolute * light.length) * state.viewMatrix; 111 setParameter("lightPosition2", lightPosition2Eye); 112 setParameterSubroutine("lightRadiance", ShaderType.Fragment, "lightRadianceAreaTube"); 113 } 114 else if (light.type == LightType.Spot) 115 { 116 setParameter("lightSpotCosCutoff", cos(degtorad(light.spotOuterCutoff))); 117 setParameter("lightSpotCosInnerCutoff", cos(degtorad(light.spotInnerCutoff))); 118 Vector4f lightDirHg = Vector4f(light.directionAbsolute); 119 lightDirHg.w = 0.0; 120 Vector3f spotDirection = (lightDirHg * state.viewMatrix).xyz; 121 setParameter("lightSpotDirection", spotDirection); 122 setParameterSubroutine("lightRadiance", ShaderType.Fragment, "lightRadianceSpot"); 123 } 124 else // unsupported light type 125 { 126 setParameterSubroutine("lightRadiance", ShaderType.Fragment, "lightRadianceFallback"); 127 } 128 129 setParameter("lightPosition", lightPos); 130 setParameter("lightColor", lightColor); 131 setParameter("lightEnergy", lightEnergy); 132 setParameter("lightRadius", light.volumeRadius); 133 setParameter("lightAreaRadius", light.radius); 134 135 setParameter("lightDiffuse", light.diffuse); 136 setParameter("lightSpecular", light.specular); 137 } 138 else 139 { 140 //lightPos = Vector3f(0.0f, 0.0f, 0.0f); 141 //lightColor = Color4f(1.0f, 1.0f, 1.0f, 1.0f); 142 setParameterSubroutine("lightRadiance", ShaderType.Fragment, "lightRadianceFallback"); 143 } 144 145 // Texture 0 - color buffer 146 glActiveTexture(GL_TEXTURE0); 147 glBindTexture(GL_TEXTURE_2D, state.colorTexture); 148 setParameter("colorBuffer", 0); 149 150 // Texture 1 - depth buffer 151 glActiveTexture(GL_TEXTURE1); 152 glBindTexture(GL_TEXTURE_2D, state.depthTexture); 153 setParameter("depthBuffer", 1); 154 155 // Texture 2 - normal buffer 156 glActiveTexture(GL_TEXTURE2); 157 glBindTexture(GL_TEXTURE_2D, state.normalTexture); 158 setParameter("normalBuffer", 2); 159 160 // Texture 3 - pbr buffer 161 glActiveTexture(GL_TEXTURE3); 162 glBindTexture(GL_TEXTURE_2D, state.pbrTexture); 163 setParameter("pbrBuffer", 3); 164 165 // Texture 5 - occlusion buffer 166 if (glIsTexture(state.occlusionTexture)) 167 { 168 glActiveTexture(GL_TEXTURE5); 169 glBindTexture(GL_TEXTURE_2D, state.occlusionTexture); 170 setParameter("occlusionBuffer", 5); 171 setParameter("haveOcclusionBuffer", true); 172 } 173 else 174 { 175 setParameter("haveOcclusionBuffer", false); 176 } 177 178 glActiveTexture(GL_TEXTURE0); 179 180 super.bindParameters(state); 181 } 182 183 override void unbindParameters(GraphicsState* state) 184 { 185 super.unbindParameters(state); 186 187 glActiveTexture(GL_TEXTURE0); 188 glBindTexture(GL_TEXTURE_2D, 0); 189 190 glActiveTexture(GL_TEXTURE1); 191 glBindTexture(GL_TEXTURE_2D, 0); 192 193 glActiveTexture(GL_TEXTURE2); 194 glBindTexture(GL_TEXTURE_2D, 0); 195 196 glActiveTexture(GL_TEXTURE3); 197 glBindTexture(GL_TEXTURE_2D, 0); 198 199 glActiveTexture(GL_TEXTURE5); 200 glBindTexture(GL_TEXTURE_2D, 0); 201 202 glActiveTexture(GL_TEXTURE0); 203 } 204 }