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