1 /*
2 Copyright (c) 2018 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.lightpass;
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.image.color;
38 
39 import dagon.core.libs;
40 import dagon.core.ownership;
41 import dagon.graphics.rc;
42 import dagon.graphics.shader;
43 import dagon.graphics.gbuffer;
44 import dagon.graphics.light;
45 
46 class LightPassShader: Shader
47 {
48     string vs = import("LightPass.vs");    
49     string fs = import("LightPass.fs");
50     
51     GBuffer gbuffer;
52     LightSource light;
53     
54     this(GBuffer gbuffer, Owner o)
55     {    
56         auto myProgram = New!ShaderProgram(vs, fs, this);
57         super(myProgram, o);
58         this.gbuffer = gbuffer;
59     }
60     
61     void bind(RenderingContext* rc2d, RenderingContext* rc3d)
62     {
63         setParameter("projectionMatrix", rc3d.projectionMatrix);
64         setParameter("viewSize", Vector2f(gbuffer.width, gbuffer.height));
65         
66         // Texture 0 - color buffer
67         glActiveTexture(GL_TEXTURE0);
68         glBindTexture(GL_TEXTURE_2D, gbuffer.colorTexture);
69         setParameter("colorBuffer", 0);
70         
71         // Texture 1 - roughness-metallic-specularity buffer
72         glActiveTexture(GL_TEXTURE1);
73         glBindTexture(GL_TEXTURE_2D, gbuffer.rmsTexture);
74         setParameter("rmsBuffer", 1);
75         
76         // Texture 2 - position buffer
77         glActiveTexture(GL_TEXTURE2);
78         glBindTexture(GL_TEXTURE_2D, gbuffer.positionTexture);
79         setParameter("positionBuffer", 2);
80         
81         // Texture 3 - normal buffer
82         glActiveTexture(GL_TEXTURE3);
83         glBindTexture(GL_TEXTURE_2D, gbuffer.normalTexture);
84         setParameter("normalBuffer", 3);
85         
86         glActiveTexture(GL_TEXTURE0);
87         
88         if (light)
89         {
90             Matrix4x4f modelViewMatrix = 
91                 rc3d.viewMatrix *
92                 translationMatrix(light.position) * 
93                 scaleMatrix(Vector3f(light.radius, light.radius, light.radius));
94             
95             Vector3f lightPositionEye = light.position * rc3d.viewMatrix;
96             
97             setParameter("modelViewMatrix", modelViewMatrix);
98             setParameter("lightPosition", lightPositionEye);
99             setParameter("lightRadius", light.radius);
100             setParameter("lightAreaRadius", light.areaRadius);
101             setParameter("lightColor", light.color);
102             setParameter("lightEnergy", light.energy);
103         }
104     
105         super.bind(rc3d);
106     }
107     
108     void unbind(RenderingContext* rc2d, RenderingContext* rc3d)
109     {
110         super.unbind(rc3d);
111         
112         glActiveTexture(GL_TEXTURE0);
113         glBindTexture(GL_TEXTURE_2D, 0);
114         
115         glActiveTexture(GL_TEXTURE1);
116         glBindTexture(GL_TEXTURE_2D, 0);
117         
118         glActiveTexture(GL_TEXTURE2);
119         glBindTexture(GL_TEXTURE_2D, 0);
120         
121         glActiveTexture(GL_TEXTURE3);
122         glBindTexture(GL_TEXTURE_2D, 0);
123         
124         glActiveTexture(GL_TEXTURE0);
125     }
126 }