1 /* 2 Copyright (c) 2018-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.graphics.shaders.arealight; 29 30 import std.stdio; 31 import std.math; 32 33 import dlib.core.memory; 34 import dlib.math.vector; 35 import dlib.math.matrix; 36 import dlib.math.transformation; 37 import dlib.math.utils; 38 import dlib.image.color; 39 40 import dagon.core.libs; 41 import dagon.core.ownership; 42 import dagon.graphics.rc; 43 import dagon.graphics.shader; 44 import dagon.graphics.gbuffer; 45 import dagon.graphics.light; 46 47 class AreaLightShader: Shader 48 { 49 string vs = import("AreaLight.vs"); 50 string fs = import("AreaLight.fs"); 51 52 GBuffer gbuffer; 53 LightSource light; 54 55 this(GBuffer gbuffer, Owner o) 56 { 57 auto myProgram = New!ShaderProgram(vs, fs, this); 58 super(myProgram, o); 59 this.gbuffer = gbuffer; 60 } 61 62 void bind(RenderingContext* rc2d, RenderingContext* rc3d) 63 { 64 setParameter("projectionMatrix", rc3d.projectionMatrix); 65 setParameter("viewSize", Vector2f(gbuffer.width, gbuffer.height)); 66 67 // Texture 0 - color buffer 68 glActiveTexture(GL_TEXTURE0); 69 glBindTexture(GL_TEXTURE_2D, gbuffer.colorTexture); 70 setParameter("colorBuffer", 0); 71 72 // Texture 1 - roughness-metallic-specularity buffer 73 glActiveTexture(GL_TEXTURE1); 74 glBindTexture(GL_TEXTURE_2D, gbuffer.rmsTexture); 75 setParameter("rmsBuffer", 1); 76 77 // Texture 2 - position buffer 78 glActiveTexture(GL_TEXTURE2); 79 glBindTexture(GL_TEXTURE_2D, gbuffer.positionTexture); 80 setParameter("positionBuffer", 2); 81 82 // Texture 3 - normal buffer 83 glActiveTexture(GL_TEXTURE3); 84 glBindTexture(GL_TEXTURE_2D, gbuffer.normalTexture); 85 setParameter("normalBuffer", 3); 86 87 glActiveTexture(GL_TEXTURE0); 88 89 if (light) 90 { 91 Matrix4x4f modelViewMatrix = 92 rc3d.viewMatrix * 93 translationMatrix(light.position) * 94 scaleMatrix(Vector3f(light.radius, light.radius, light.radius)); 95 96 Vector3f lightPositionEye = light.position * rc3d.viewMatrix; 97 98 setParameter("modelViewMatrix", modelViewMatrix); 99 setParameter("lightPosition", lightPositionEye); 100 setParameter("lightRadius", light.radius); 101 setParameter("lightAreaRadius", light.areaRadius); 102 setParameter("lightColor", light.color); 103 setParameter("lightEnergy", light.energy); 104 105 if (light.type == LightType.AreaSphere) 106 { 107 setParameterSubroutine("lightRadiance", ShaderType.Fragment, "lightRadianceAreaSphere"); 108 } 109 else if (light.type == LightType.AreaTube) 110 { 111 Vector3f lightPosition2Eye = (light.position + light.direction * light.tubeLength) * rc3d.viewMatrix; 112 setParameter("lightPosition2", lightPosition2Eye); 113 setParameterSubroutine("lightRadiance", ShaderType.Fragment, "lightRadianceAreaTube"); 114 } 115 else if (light.type == LightType.Spot) 116 { 117 //setParameter("lightSpotCutoff", light.spotCutoff); 118 setParameter("lightSpotCosCutoff", cos(degtorad(light.spotOuterCutoff))); 119 setParameter("lightSpotCosInnerCutoff", cos(degtorad(light.spotInnerCutoff))); 120 Vector3f spotDirection = light.directionEye(rc3d.viewMatrix); 121 setParameter("lightSpotDirection", spotDirection); 122 //setParameter("lightSpotExponent", light.spotExponent); 123 setParameterSubroutine("lightRadiance", ShaderType.Fragment, "lightRadianceSpot"); 124 } 125 else // unsupported light type 126 { 127 setParameterSubroutine("lightRadiance", ShaderType.Fragment, "lightRadianceFallback"); 128 } 129 } 130 else 131 { 132 setParameterSubroutine("lightRadiance", ShaderType.Fragment, "lightRadianceFallback"); 133 } 134 135 super.bind(rc3d); 136 } 137 138 void unbind(RenderingContext* rc2d, RenderingContext* rc3d) 139 { 140 super.unbind(rc3d); 141 142 glActiveTexture(GL_TEXTURE0); 143 glBindTexture(GL_TEXTURE_2D, 0); 144 145 glActiveTexture(GL_TEXTURE1); 146 glBindTexture(GL_TEXTURE_2D, 0); 147 148 glActiveTexture(GL_TEXTURE2); 149 glBindTexture(GL_TEXTURE_2D, 0); 150 151 glActiveTexture(GL_TEXTURE3); 152 glBindTexture(GL_TEXTURE_2D, 0); 153 154 glActiveTexture(GL_TEXTURE0); 155 } 156 }