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.particle;
29 
30 import std.stdio;
31 import std.math;
32 import std.conv;
33 
34 import dlib.core.memory;
35 import dlib.math.vector;
36 import dlib.math.matrix;
37 import dlib.math.transformation;
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 
46 class ParticleShader: Shader
47 {
48     string vs = import("Particle.vs");
49     string fs = import("Particle.fs");
50 
51     GBuffer gbuffer;
52 
53     this(GBuffer gbuffer, Owner o)
54     {
55         auto myProgram = New!ShaderProgram(vs, fs, this);
56         super(myProgram, o);
57 
58         this.gbuffer = gbuffer;
59     }
60 
61     override void bind(RenderingContext* rc)
62     {
63         auto idiffuse = "diffuse" in rc.material.inputs;
64         auto inormal = "normal" in rc.material.inputs;
65         auto ienergy = "energy" in rc.material.inputs;
66         auto itransparency = "transparency" in rc.material.inputs;
67         auto iparticleColor = "particleColor" in rc.material.inputs;
68         auto iparticleSphericalNormal = "particleSphericalNormal" in rc.material.inputs;
69         auto ishadeless = "shadeless" in rc.material.inputs;
70 
71         // Matrices
72         setParameter("modelViewMatrix", rc.modelViewMatrix);
73         setParameter("projectionMatrix", rc.projectionMatrix);
74         setParameter("normalMatrix", rc.normalMatrix);
75         setParameter("viewMatrix", rc.viewMatrix);
76         setParameter("invViewMatrix", rc.invViewMatrix);
77 
78         setParameter("prevModelViewProjMatrix", rc.prevModelViewProjMatrix);
79         setParameter("blurModelViewProjMatrix", rc.blurModelViewProjMatrix);
80 
81         setParameter("viewSize", Vector2f(gbuffer.width, gbuffer.height));
82 
83         Color4f particleColor = Color4f(1.0f, 1.0f, 1.0f, 1.0f);
84         if (iparticleColor)
85             particleColor = Color4f(iparticleColor.asVector4f);
86 
87         float alpha = 1.0f;
88         if (itransparency)
89             alpha = itransparency.asFloat;
90 
91         float energy = ienergy.asFloat;
92 
93         setParameter("particleColor", particleColor);
94         setParameter("alpha", alpha);
95         setParameter("energy", energy);
96         setParameter("alphaCutout", rc.shadowPass);
97         setParameter("alphaCutoutThreshold", 0.25f); // TODO: store in material properties
98         setParameter("particlePosition", rc.modelViewMatrix.translation);
99 
100         bool shaded = true;
101         if (ishadeless)
102             shaded = !(ishadeless.asBool);
103         setParameter("shaded", shaded);
104 
105         // Texture 0 - diffuse texture
106         if (idiffuse.texture is null)
107         {
108             setParameter("diffuseVector", idiffuse.asVector4f);
109             setParameterSubroutine("diffuse", ShaderType.Fragment, "diffuseColorValue");
110         }
111         else
112         {
113             glActiveTexture(GL_TEXTURE0);
114             idiffuse.texture.bind();
115             setParameter("diffuseTexture", 0);
116             setParameterSubroutine("diffuse", ShaderType.Fragment, "diffuseColorTexture");
117         }
118 
119         // Texture 1 - position texture (for soft particles)
120         glActiveTexture(GL_TEXTURE1);
121         glBindTexture(GL_TEXTURE_2D, gbuffer.positionTexture);
122         setParameter("positionTexture", 1);
123 
124         // Texture 2 - normal map
125         if (inormal.texture is null)
126         {
127             bool sphericalNormal = false;
128             if (iparticleSphericalNormal)
129                 sphericalNormal = iparticleSphericalNormal.asBool;
130             if (sphericalNormal)
131             {
132                 setParameterSubroutine("normal", ShaderType.Fragment, "normalFunctionHemisphere");
133             }
134             else
135             {
136                 Vector3f nVec = inormal.asVector3f;
137                 setParameter("normalVector", nVec);
138                 setParameterSubroutine("normal", ShaderType.Fragment, "normalValue");
139             }
140         }
141         else
142         {
143             glActiveTexture(GL_TEXTURE2);
144             inormal.texture.bind();
145             setParameter("normalTexture", 2);
146             setParameterSubroutine("normal", ShaderType.Fragment, "normalMap");
147         }
148 
149         // Environment
150         setParameter("sunDirection", rc.environment.sunDirectionEye(rc.viewMatrix));
151         setParameter("sunColor", rc.environment.sunColor);
152         setParameter("sunEnergy", rc.environment.sunEnergy);
153 
154         if (rc.environment.environmentMap)
155         {
156             glActiveTexture(GL_TEXTURE3);
157             rc.environment.environmentMap.bind();
158             setParameter("envTexture", 3);
159             setParameterSubroutine("environment", ShaderType.Fragment, "environmentTexture");
160         }
161         else
162         {
163             setParameter("skyZenithColor", rc.environment.skyZenithColor);
164             setParameter("skyHorizonColor", rc.environment.skyHorizonColor);
165             setParameter("groundColor", rc.environment.groundColor);
166             setParameter("skyEnergy", rc.environment.skyEnergy);
167             setParameter("groundEnergy", rc.environment.groundEnergy);
168             setParameterSubroutine("environment", ShaderType.Fragment, "environmentSky");
169         }
170         
171         setParameter("environmentBrightness", rc.environment.environmentBrightness);
172 
173         glActiveTexture(GL_TEXTURE0);
174 
175         super.bind(rc);
176     }
177 
178     override void unbind(RenderingContext* rc)
179     {
180         super.unbind(rc);
181 
182         glActiveTexture(GL_TEXTURE0);
183         glBindTexture(GL_TEXTURE_2D, 0);
184 
185         glActiveTexture(GL_TEXTURE1);
186         glBindTexture(GL_TEXTURE_2D, 0);
187 
188         glActiveTexture(GL_TEXTURE2);
189         glBindTexture(GL_TEXTURE_2D, 0);
190 
191         glActiveTexture(GL_TEXTURE3);
192         glBindTexture(GL_TEXTURE_2D, 0);
193 
194         glActiveTexture(GL_TEXTURE0);
195     }
196 }