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.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 else 136 { 137 //lightPos = Vector3f(0.0f, 0.0f, 0.0f); 138 //lightColor = Color4f(1.0f, 1.0f, 1.0f, 1.0f); 139 setParameterSubroutine("lightRadiance", ShaderType.Fragment, "lightRadianceFallback"); 140 } 141 142 // Texture 0 - color buffer 143 glActiveTexture(GL_TEXTURE0); 144 glBindTexture(GL_TEXTURE_2D, state.colorTexture); 145 setParameter("colorBuffer", 0); 146 147 // Texture 1 - depth buffer 148 glActiveTexture(GL_TEXTURE1); 149 glBindTexture(GL_TEXTURE_2D, state.depthTexture); 150 setParameter("depthBuffer", 1); 151 152 // Texture 2 - normal buffer 153 glActiveTexture(GL_TEXTURE2); 154 glBindTexture(GL_TEXTURE_2D, state.normalTexture); 155 setParameter("normalBuffer", 2); 156 157 // Texture 3 - pbr buffer 158 glActiveTexture(GL_TEXTURE3); 159 glBindTexture(GL_TEXTURE_2D, state.pbrTexture); 160 setParameter("pbrBuffer", 3); 161 162 // Texture 5 - occlusion buffer 163 if (glIsTexture(state.occlusionTexture)) 164 { 165 glActiveTexture(GL_TEXTURE5); 166 glBindTexture(GL_TEXTURE_2D, state.occlusionTexture); 167 setParameter("occlusionBuffer", 5); 168 setParameter("haveOcclusionBuffer", true); 169 } 170 else 171 { 172 setParameter("haveOcclusionBuffer", false); 173 } 174 175 glActiveTexture(GL_TEXTURE0); 176 177 super.bindParameters(state); 178 } 179 180 override void unbindParameters(GraphicsState* state) 181 { 182 super.unbindParameters(state); 183 184 glActiveTexture(GL_TEXTURE0); 185 glBindTexture(GL_TEXTURE_2D, 0); 186 187 glActiveTexture(GL_TEXTURE1); 188 glBindTexture(GL_TEXTURE_2D, 0); 189 190 glActiveTexture(GL_TEXTURE2); 191 glBindTexture(GL_TEXTURE_2D, 0); 192 193 glActiveTexture(GL_TEXTURE3); 194 glBindTexture(GL_TEXTURE_2D, 0); 195 196 glActiveTexture(GL_TEXTURE5); 197 glBindTexture(GL_TEXTURE_2D, 0); 198 199 glActiveTexture(GL_TEXTURE0); 200 } 201 }