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.water;
29 
30 import std.stdio;
31 import std.math;
32 import std.conv;
33 
34 import dlib.core.memory;
35 import dlib.core.stream;
36 import dlib.math.vector;
37 import dlib.math.matrix;
38 import dlib.math.transformation;
39 import dlib.image.color;
40 import dlib.image.io.png;
41 
42 import dagon.core.libs;
43 import dagon.core.ownership;
44 import dagon.graphics.rc;
45 import dagon.graphics.shader;
46 import dagon.graphics.gbuffer;
47 import dagon.graphics.texture;
48 import dagon.graphics.cubemap;
49 import dagon.resource.asset;
50 import dagon.resource.textureasset;
51 
52 // TODO: move to dlib.math.utils
53 real frac(real v)
54 {
55     real intpart;
56     return modf(v, intpart);
57 }
58 
59 class WaterShader: Shader
60 {
61     string vs = import("Water.vs");
62     string fs = import("Water.fs");
63     string rippleTextureFilename = "data/__internal/ripples.png";
64 
65     GBuffer gbuffer;
66     Texture rippleTexture;
67 
68     this(GBuffer gbuffer, AssetManager assetManager, Owner o)
69     {
70         auto myProgram = New!ShaderProgram(vs, fs, this);
71         super(myProgram, o);
72 
73         this.gbuffer = gbuffer;
74 
75         TextureAsset rippleTextureAsset = textureAsset(assetManager, rippleTextureFilename);
76         rippleTexture = rippleTextureAsset.texture;
77     }
78 
79     override void bind(RenderingContext* rc)
80     {
81         // Matrices
82         setParameter("modelViewMatrix", rc.modelViewMatrix);
83         setParameter("projectionMatrix", rc.projectionMatrix);
84         setParameter("normalMatrix", rc.normalMatrix);
85         setParameter("viewMatrix", rc.viewMatrix);
86         setParameter("invViewMatrix", rc.invViewMatrix);
87         setParameter("prevModelViewProjMatrix", rc.prevModelViewProjMatrix);
88         setParameter("blurModelViewProjMatrix", rc.blurModelViewProjMatrix);
89 
90         setParameter("viewSize", Vector2f(gbuffer.width, gbuffer.height));
91 
92         // Texture 1 - position texture (for smooth coast transparency)
93         glActiveTexture(GL_TEXTURE1);
94         glBindTexture(GL_TEXTURE_2D, gbuffer.positionTexture);
95         setParameter("positionTexture", 1);
96 
97         // Environment
98         setParameter("sunDirection", rc.environment.sunDirectionEye(rc.viewMatrix));
99 
100         if (rc.environment.environmentMap)
101         {
102             glActiveTexture(GL_TEXTURE3);
103             rc.environment.environmentMap.bind();
104 
105             if (cast(Cubemap)rc.environment.environmentMap)
106             {
107                 setParameter("envTextureCube", 3);
108                 setParameterSubroutine("environment", ShaderType.Fragment, "environmentCubemap");
109             }
110             else
111             {
112                 setParameter("envTexture", 3);
113                 setParameterSubroutine("environment", ShaderType.Fragment, "environmentTexture");
114             }
115         }
116         else
117         {
118             setParameter("skyZenithColor", rc.environment.skyZenithColor);
119             setParameter("skyHorizonColor", rc.environment.skyHorizonColor);
120             setParameter("groundColor", rc.environment.groundColor);
121             setParameter("skyEnergy", rc.environment.skyEnergy);
122             setParameter("groundEnergy", rc.environment.groundEnergy);
123             setParameterSubroutine("environment", ShaderType.Fragment, "environmentSky");
124         }
125         
126         setParameter("environmentBrightness", rc.environment.environmentBrightness);
127 
128         // Ripple parameters
129         glActiveTexture(GL_TEXTURE2);
130         rippleTexture.bind();
131         setParameter("rippleTexture", 2);
132 
133         float rippleTimesX = frac((rc.time) * 1.6);
134         float rippleTimesY = frac((rc.time * 0.85 + 0.2) * 1.6);
135         float rippleTimesZ = frac((rc.time * 0.93 + 0.45) * 1.6);
136         float rippleTimesW = frac((rc.time * 1.13 + 0.7) * 1.6);
137         setParameter("rippleTimes", Vector4f(rippleTimesX, rippleTimesY, rippleTimesZ, rippleTimesW));
138 
139         glActiveTexture(GL_TEXTURE0);
140 
141         super.bind(rc);
142     }
143 
144     override void unbind(RenderingContext* rc)
145     {
146         super.unbind(rc);
147 
148         glActiveTexture(GL_TEXTURE1);
149         glBindTexture(GL_TEXTURE_2D, 0);
150 
151         glActiveTexture(GL_TEXTURE2);
152         glBindTexture(GL_TEXTURE_2D, 0);
153 
154         glActiveTexture(GL_TEXTURE3);
155         glBindTexture(GL_TEXTURE_2D, 0);
156         glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
157 
158         glActiveTexture(GL_TEXTURE0);
159     }
160 }