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.environment;
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.image.color;
40 import dlib.text.str;
41 
42 import dagon.core.bindings;
43 import dagon.graphics.shader;
44 import dagon.graphics.cubemap;
45 import dagon.graphics.state;
46 
47 class EnvironmentShader: Shader
48 {
49     String vs, fs;
50 
51     this(Owner owner)
52     {
53         vs = Shader.load("data/__internal/shaders/Environment/Environment.vert.glsl");
54         fs = Shader.load("data/__internal/shaders/Environment/Environment.frag.glsl");
55 
56         auto myProgram = New!ShaderProgram(vs, fs, this);
57         super(myProgram, owner);
58     }
59 
60     ~this()
61     {
62         vs.free();
63         fs.free();
64     }
65 
66     override void bindParameters(GraphicsState* state)
67     {
68         setParameter("viewMatrix", state.viewMatrix);
69         setParameter("invViewMatrix", state.invViewMatrix);
70         setParameter("projectionMatrix", state.projectionMatrix);
71         setParameter("invProjectionMatrix", state.invProjectionMatrix);
72         setParameter("resolution", state.resolution);
73         setParameter("zNear", state.zNear);
74         setParameter("zFar", state.zFar);
75 
76         // Texture 0 - color buffer
77         glActiveTexture(GL_TEXTURE0);
78         glBindTexture(GL_TEXTURE_2D, state.colorTexture);
79         setParameter("colorBuffer", 0);
80 
81         // Texture 1 - depth buffer
82         glActiveTexture(GL_TEXTURE1);
83         glBindTexture(GL_TEXTURE_2D, state.depthTexture);
84         setParameter("depthBuffer", 1);
85 
86         // Texture 2 - normal buffer
87         glActiveTexture(GL_TEXTURE2);
88         glBindTexture(GL_TEXTURE_2D, state.normalTexture);
89         setParameter("normalBuffer", 2);
90 
91         // Texture 3 - pbr buffer
92         glActiveTexture(GL_TEXTURE3);
93         glBindTexture(GL_TEXTURE_2D, state.pbrTexture);
94         setParameter("pbrBuffer", 3);
95 
96         // Texture 4 - environment
97         if (state.environment)
98         {
99             setParameter("fogColor", state.environment.fogColor);
100             setParameter("fogStart", state.environment.fogStart);
101             setParameter("fogEnd", state.environment.fogEnd);
102             setParameter("ambientEnergy", state.environment.ambientEnergy);
103 
104             if (state.environment.ambientMap)
105             {
106                 glActiveTexture(GL_TEXTURE4);
107                 state.environment.ambientMap.bind();
108                 if (cast(Cubemap)state.environment.ambientMap)
109                 {
110                     setParameter("ambientTextureCube", 4);
111                     setParameterSubroutine("ambient", ShaderType.Fragment, "ambientCubemap");
112                 }
113                 else
114                 {
115                     setParameter("ambientTexture", 4);
116                     setParameterSubroutine("ambient", ShaderType.Fragment, "ambientEquirectangularMap");
117                 }
118             }
119             else
120             {
121                 setParameter("ambientVector", state.environment.ambientColor);
122                 setParameterSubroutine("ambient", ShaderType.Fragment, "ambientColor");
123             }
124         }
125         else
126         {
127             setParameter("fogColor", Color4f(0.5f, 0.5f, 0.5f, 1.0f));
128             setParameter("fogStart", 0.0f);
129             setParameter("fogEnd", 1000.0f);
130             setParameter("ambientEnergy", 1.0f);
131             setParameter("ambientVector", Color4f(0.5f, 0.5f, 0.5f, 1.0f));
132             setParameterSubroutine("ambient", ShaderType.Fragment, "ambientColor");
133         }
134 
135         // Texture 5 - occlusion buffer
136         if (glIsTexture(state.occlusionTexture))
137         {
138             glActiveTexture(GL_TEXTURE5);
139             glBindTexture(GL_TEXTURE_2D, state.occlusionTexture);
140             setParameter("occlusionBuffer", 5);
141             setParameter("haveOcclusionBuffer", true);
142         }
143         else
144         {
145             setParameter("haveOcclusionBuffer", false);
146         }
147 
148         glActiveTexture(GL_TEXTURE0);
149 
150         super.bindParameters(state);
151     }
152 
153     override void unbindParameters(GraphicsState* state)
154     {
155         super.unbindParameters(state);
156 
157         glActiveTexture(GL_TEXTURE0);
158         glBindTexture(GL_TEXTURE_2D, 0);
159 
160         glActiveTexture(GL_TEXTURE1);
161         glBindTexture(GL_TEXTURE_2D, 0);
162 
163         glActiveTexture(GL_TEXTURE2);
164         glBindTexture(GL_TEXTURE_2D, 0);
165 
166         glActiveTexture(GL_TEXTURE3);
167         glBindTexture(GL_TEXTURE_2D, 0);
168 
169         glActiveTexture(GL_TEXTURE4);
170         glBindTexture(GL_TEXTURE_2D, 0);
171         glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
172 
173         glActiveTexture(GL_TEXTURE5);
174         glBindTexture(GL_TEXTURE_2D, 0);
175 
176         glActiveTexture(GL_TEXTURE0);
177     }
178 }