1 /*
2 Copyright (c) 2015 Timur Gafarov 
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module dgl.graphics.bumpshader;
30 
31 import dlib.core.memory;
32 import dgl.core.event;
33 import dgl.graphics.shader;
34 import dgl.graphics.glslshader;
35 
36 // TODO: shadow support
37 
38 private string _bumpVertexShader = q{
39     varying vec3 position;
40     varying vec3 n, t, b;
41     varying vec3 eyeVec;
42 		
43     void main(void)
44     {
45         gl_TexCoord[0] = gl_MultiTexCoord0;
46         gl_TexCoord[1] = gl_MultiTexCoord1;
47 
48         n = normalize(gl_NormalMatrix * gl_Normal);
49         t = normalize(gl_NormalMatrix * gl_Color.xyz);
50         b = cross(n, t);
51 	    vec4 pos = gl_ModelViewMatrix * gl_Vertex;
52 	    position = pos.xyz;
53         
54         eyeVec.x = dot(position, t);
55         eyeVec.y = dot(position, b);
56         eyeVec.z = dot(position, n);
57         eyeVec = -normalize(eyeVec);
58         
59 	    gl_Position = ftransform();
60     }
61 };
62 
63 private string _bumpFragmentShader = q{
64 
65     varying vec3 position;
66     varying vec3 n, t, b;
67     varying vec3 eyeVec;
68 		
69     uniform sampler2D dgl_Texture0;
70     uniform sampler2D dgl_Texture1;
71     uniform sampler2D dgl_Texture2;
72     
73     uniform bool parallaxEnabled;
74     const float scale = 0.05;
75     const float bias = -0.03;
76 
77     void main (void) 
78     {
79         vec2 texCoords = gl_TexCoord[0].st;
80         if (parallaxEnabled)
81         {
82             vec2 E = vec2(eyeVec.x, -eyeVec.y);
83             float height = texture2D(dgl_Texture1, texCoords).a; 
84             height = height * scale + bias;
85             texCoords = texCoords + (height * E);
86         }
87         
88         vec3 normal = 2.0 * texture2D(dgl_Texture1, texCoords).rgb - 1.0;
89         normal = normalize(normal);
90 	
91 	    float Csh = 32.0;
92 
93         vec3 lightDirection;
94         float attenuation; 
95         vec3 L_tan;
96         const float lightRadiusSqr = 20.0;
97 
98         vec4 tex = texture2D(dgl_Texture0, texCoords);
99         vec4 emit = vec4(0.0, 0.0, 0.0, 1.0);
100         if (gl_FrontMaterial.emission.w > 0.0)
101             emit = texture2D(dgl_Texture2, texCoords) * gl_FrontMaterial.emission.w;
102             
103         vec4 col = vec4(0.0, 0.0, 0.0, 1.0);
104 
105         vec3 halfVector;
106         float distance;
107         float diffuse;
108         float specular;
109         
110         const float roughness = 0.9;
111 
112         for (int i = 0; i < 4; i++)
113 	    {
114 	        if (gl_LightSource[i].position.w < 2.0)
115 	        {
116 	            vec4 Ca = gl_FrontMaterial.ambient * gl_LightSource[i].ambient; 
117 	            vec4 Cd = gl_FrontMaterial.diffuse * gl_LightSource[i].diffuse; 
118 	            vec4 Cs = gl_FrontMaterial.specular * gl_LightSource[i].specular;  
119             
120 	            vec3 positionToLightSource = vec3(gl_LightSource[i].position.xyz - position);
121 	
122 	            distance = length(positionToLightSource);
123             
124                 lightDirection = normalize(positionToLightSource);
125             
126                 attenuation = clamp(1.0 - distance/lightRadiusSqr, 0.0, 1.0);
127 
128                 L_tan.x = dot(lightDirection, t);
129                 L_tan.y = dot(lightDirection, b);
130                 L_tan.z = dot(lightDirection, n);
131 
132 	            diffuse = clamp(dot(normal, L_tan), 0.0, 1.0);
133                 
134                 // Blinn-Phong
135                 halfVector = normalize(L_tan + eyeVec);
136 			    float NDotH = dot(normal, halfVector);
137 			    specular = max(pow(NDotH, Csh), 0.0);
138                 
139 	            col += (Ca + (Cd*diffuse) + (Cs*specular)) * attenuation; 
140 	        }
141 	    }
142 
143 	    gl_FragColor = tex * col + emit;
144 	    gl_FragColor.a = 1.0;
145     }
146 };
147 
148 GLSLShader bumpShader(EventManager emgr)
149 {
150     auto shader = New!GLSLShader(emgr, _bumpVertexShader, _bumpFragmentShader);
151     shader.setParamBool("parallaxEnabled", false);
152     return shader;
153 }