Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

Diff Revisions 167 vs 168 for /trunk/assets/shaders/GLSL/terrain.glsl

Diff revisions: vs.
  @@ -1,11 +1,12 @@
1 1 -- Vertex.Depth
2 2 uniform mat4 dvd_ModelMatrix;
3 3 uniform mat4 dvd_ModelViewProjectionMatrix;
4 +
4 5 in vec3 inVertexData;
5 6 out vec4 _vertexM;
6 7
7 8 void main(void){
8 - vec4 dvd_Vertex = vec4(inVertexData,1.0);
9 + vec4 dvd_Vertex = vec4(inVertexData,1.0);
9 10 _vertexM = dvd_ModelMatrix * dvd_Vertex;
10 11 gl_Position = dvd_ModelViewProjectionMatrix * dvd_Vertex;
11 12 }
  @@ -15,17 +16,16 @@
15 16 #include "vboInputData.vert"
16 17
17 18 uniform vec3 bbox_min;
18 - uniform vec3 bbox_max;
19 - out vec4 _vertexM;
19 + uniform vec3 bbox_diff;
20 20
21 21 #include "lightingDefaults.vert"
22 22
23 23 void main(void){
24 - int i = 0;
24 +
25 25 computeData();
26 - vec3 vPositionNormalized = (dvd_Vertex.xyz - bbox_min.xyz) / (bbox_max.xyz - bbox_min.xyz);
27 - _texCoord = vPositionNormalized.xz;
28 - _vertexM = dvd_ModelMatrix * dvd_Vertex;
26 +
27 + _texCoord = vec3((dvd_Vertex.xyz - bbox_min.xyz) / bbox_diff).sp;
28 +
29 29 _vertexMV = dvd_ViewMatrix * _vertexM;
30 30 computeLightVectors();
31 31
  @@ -35,7 +35,7 @@
35 35 // position multiplied by the inverse of the camera matrix
36 36 //pos = dvd_ModelViewMatrixInverse * pos;
37 37 // position multiplied by the light matrix. The vertex's position from the light's perspective
38 - _shadowCoord[0] = dvd_lightProjectionMatrices[i] * dvd_Vertex;
38 + _shadowCoord[0] = dvd_lightProjectionMatrices[0] * dvd_Vertex;
39 39 }
40 40
41 41 gl_Position = dvd_ModelViewProjectionMatrix * dvd_Vertex;
  @@ -46,16 +46,20 @@
46 46 in vec4 _vertexM;
47 47 out vec4 _colorOut;
48 48
49 - uniform float water_height;
49 + uniform float _waterHeight;
50 + #if defined(SKIP_HARDWARE_CLIPPING)
51 + in float dvd_ClipDistance[MAX_CLIP_PLANES];
52 + #else
53 + in float gl_ClipDistance[MAX_CLIP_PLANES];
54 + #endif
50 55
51 56 void main (void)
52 57 {
53 - // Discard the fragments that are underwater when drawing in reflection
54 -
55 - if(_vertexM.y < water_height){
56 - discard;
57 - }
58 -
58 + #if defined(SKIP_HARDWARE_CLIPPING)
59 + if(dvd_ClipDistance[0] < 0) discard;
60 + #else
61 + if(gl_ClipDistance[0] < 0) discard;
62 + #endif
59 63 _colorOut = vec4(1.0,1.0,1.0,1.0);
60 64 }
61 65
  @@ -64,11 +68,18 @@
64 68 //based on: http://yannick.gerometta.free.fr/base.php?id=glsldemo
65 69 in vec3 _lightDirection[MAX_LIGHT_COUNT];
66 70 in vec3 _viewDirection;
67 -
68 71 in vec2 _texCoord;
69 72 in vec3 _normalMV;
70 - in vec4 _vertexM;
71 - in vec4 _vertexMV;
73 + in vec4 _vertexM;
74 + in vec4 _vertexMV;
75 +
76 + #if defined(SKIP_HARDWARE_CLIPPING)
77 + in float dvd_ClipDistance[MAX_CLIP_PLANES];
78 + uniform int dvd_clip_plane_active[MAX_CLIP_PLANES];
79 + #else
80 + in float gl_ClipDistance[MAX_CLIP_PLANES];
81 + #endif
82 +
72 83 out vec4 _colorOut;
73 84
74 85 uniform sampler2D texDiffuseMap;
  @@ -81,14 +92,12 @@
81 92
82 93 uniform int dvd_lightType[MAX_LIGHT_COUNT];
83 94 uniform bool dvd_lightEnabled[MAX_LIGHT_COUNT];
95 + uniform float dvd_time;
96 +
84 97 uniform float LODFactor;
85 98 uniform float detail_scale;
86 99 uniform float diffuse_scale;
87 - uniform float water_height;
88 - uniform float time;
89 - uniform bool water_reflection_rendering;
90 - uniform bool alphaTexture;
91 - uniform vec2 zPlanes;
100 + uniform float _waterHeight;
92 101 uniform mat4 material;
93 102 uniform vec3 bbox_min;
94 103 uniform vec4 dvd_lightAmbient;
  @@ -97,7 +106,8 @@
97 106 #define LIGHT_OMNIDIRECTIONAL 1
98 107 #define LIGHT_SPOT 2
99 108
100 - vec4 NormalMapping(vec2 uv, vec3 pixelToLightTBN, bool underwater);
109 + vec4 NormalMapping(in vec2 uv,in vec3 pixelToLightTBN);
110 + vec4 NormalMappingUnderwater(in vec2 uv,in vec3 pixelToLightTBN);
101 111 vec4 CausticsColor();
102 112
103 113 ///Global NDotL, basically
  @@ -106,108 +116,119 @@
106 116 #include "fog.frag"
107 117 #include "shadowMapping.frag"
108 118
109 - bool isUnderWater(){
110 - return _vertexM.y < water_height;
111 - }
112 119
120 + #if defined(SKIP_HARDWARE_CLIPPING)
121 + bool isUnderWater() { return dvd_ClipDistance[0] < 0; }
122 + #else
123 + bool isUnderWater() { return gl_ClipDistance[0] < 0; }
124 + #endif
125 +
113 126 void main (void)
114 127 {
115 - // Discard the fragments that are underwater when drawing in reflection
116 - bool underwater = isUnderWater();
117 - if(water_reflection_rendering && underwater){
118 - discard;
128 + if(isUnderWater()){
129 + _colorOut = NormalMappingUnderwater(_texCoord, _lightDirection[0]);
130 + }else{
131 + _colorOut = NormalMapping(_texCoord, _lightDirection[0]);
119 132 }
120 -
121 - vec4 color = NormalMapping(_texCoord, _lightDirection[0], underwater);
122 -
123 - if(underwater) {
124 - float alpha = (water_height - _vertexM.y) / (2*(water_height - bbox_min.y));
125 - color = (1-alpha) * color + alpha * CausticsColor();
126 - }
127 - applyFog(color);
128 -
129 - _colorOut = color;
133 + applyFog(_colorOut);
130 134 }
131 135
132 136 vec4 CausticsColor()
133 137 {
134 - float time2 = time * 0.000001;
135 - vec2 uv0 = _texCoord*100.0;
138 + float time2 = dvd_time * 0.0001;
139 + vec2 noiseUV = _texCoord*100.0;
140 + vec2 uv0 = noiseUV;
141 + vec2 uv1 = noiseUV + time2;
142 + uv0.t = uv0.t;
136 143 uv0.s -= time2;
137 - uv0.t += time2;
138 - vec4 color0 = texture(texWaterCaustics, uv0);
139 144
140 - vec2 uv1 = _texCoord*100.0;
141 - uv1.s += time2;
142 - uv1.t += time2;
143 - vec4 color1 = texture(texWaterCaustics, uv1);
144 -
145 - return (color0 + color1) /2;
145 + return (texture(texWaterCaustics, uv1) + texture(texWaterCaustics, uv0)) * 0.5;
146 146 }
147 147
148 - vec4 NormalMapping(vec2 uv, vec3 pixelToLightTBN, bool underwater)
148 + const float shadowMaxDistance = 200.0;
149 + vec4 NormalMapping(in vec2 uv, in vec3 pixelToLightTBN)
149 150 {
150 151 vec3 lightVecTBN = normalize(pixelToLightTBN);
151 - vec3 viewVecTBN = normalize(_viewDirection);
152 - //float z = 1.0 - (zDepth) / zPlanes.y;
153 - vec2 uv_detail = uv * detail_scale;
154 - vec2 uv_diffuse = uv * diffuse_scale;
152 + vec3 viewVecTBN = normalize(_viewDirection);
153 + vec2 uv_detail = uv * detail_scale;
154 + vec2 uv_diffuse = uv * diffuse_scale;
155 155
156 - vec3 normalTBN = texture(texNormalHeightMap, uv_detail).rgb * 2.0 - 1.0;
157 - normalTBN = normalize(normalTBN);
158 - iDiffuse = max(dot(lightVecTBN.xyz, normalTBN), 0.0); // diffuse intensity. NDotL
159 -
160 - vec4 tBase[4];
156 + vec3 normalTBN = normalize(texture(texNormalHeightMap, uv_detail).rgb * 2.0 - 1.0);
161 157
158 + vec4 tBase[3];
162 159 tBase[0] = texture(texBlend0, uv_diffuse);
163 160 tBase[1] = texture(texBlend1, uv_diffuse);
164 161 tBase[2] = texture(texBlend2, uv_diffuse);
165 - tBase[3] = texture(texBlend3, uv_diffuse);
166 -
162 +
167 163 vec4 DiffuseMap = texture(texDiffuseMap, uv);
168 -
169 - vec4 cBase;
170 164
171 - if(_vertexM.y < water_height)
172 - cBase = tBase[0];
173 - else {
174 - if(alphaTexture){
175 - cBase = mix(mix(mix(tBase[1], tBase[0], DiffuseMap.r), tBase[2], DiffuseMap.g), tBase[3], DiffuseMap.a);
176 - }else{
177 - cBase = mix(mix(tBase[1], tBase[0], DiffuseMap.r), tBase[2], DiffuseMap.g);
178 - }
179 - }
165 + vec4 cBase = mix(mix(tBase[1], tBase[0], DiffuseMap.r), tBase[2], DiffuseMap.g);
166 +
167 + #if defined(USE_ALPHA_TEXTURE)
168 + vec4 tBase3 = texture(texBlend3, uv_diffuse);
169 + cBase = mix(cBase, tBase3, DiffuseMap.b);
170 + #endif
171 +
172 + iDiffuse = max(dot(lightVecTBN.xyz, normalTBN), 0.0); // diffuse intensity. NDotL
173 +
174 + vec4 cAmbient = dvd_lightAmbient * material[0];
175 + vec4 cDiffuse = material[1] * iDiffuse;
176 +
177 + if(dvd_lightEnabled[0]){
178 + cAmbient += gl_LightSource[0].ambient * material[0];
179 + cDiffuse *= gl_LightSource[0].diffuse;
180 + }
180 181
181 182 // SHADOW MAPS
182 - float distance_max = 200.0;
183 183 float shadow = 1.0;
184 184 float distance = length(_viewDirection);
185 -
186 - if(distance < distance_max) {
185 + if(distance < shadowMaxDistance) {
187 186 applyShadowDirectional(iDiffuse, 0, shadow);
188 - shadow = 1.0 - (1.0-shadow) * (distance_max-distance) / distance_max;
187 + shadow = 0.2 + 0.8 * (1.0 - (1.0-shadow) * (shadowMaxDistance-distance) / shadowMaxDistance);
189 188 }
190 189
190 + return (cAmbient * cBase + cDiffuse * cBase) * shadow;
191 + }
192 +
193 + vec4 NormalMappingUnderwater(in vec2 uv, in vec3 pixelToLightTBN)
194 + {
195 + if(dvd_clip_plane_active[0] == 1) discard;
196 +
197 + vec3 lightVecTBN = normalize(pixelToLightTBN);
198 + vec3 viewVecTBN = normalize(_viewDirection);
199 + vec2 uv_detail = uv * detail_scale;
200 + vec2 uv_diffuse = uv * diffuse_scale;
201 +
202 + vec3 normalTBN = normalize(texture(texNormalHeightMap, uv_detail).rgb * 2.0 - 1.0);
203 +
204 + vec4 cBase = texture(texBlend0, uv_diffuse);
205 +
206 + iDiffuse = max(dot(lightVecTBN.xyz, normalTBN), 0.0); // diffuse intensity. NDotL
207 + float iSpecular = clamp(pow(max(dot(reflect(-lightVecTBN.xyz, normalTBN), viewVecTBN), 0.0), material[3].x ), 0.0, 1.0);
208 +
191 209 vec4 cAmbient = dvd_lightAmbient * material[0];
192 - vec4 cDiffuse = material[1] * iDiffuse * shadow;
210 + vec4 cDiffuse = material[1] * iDiffuse;
211 + vec4 cSpecular = vec4(0);
212 +
213 + float alpha = 0.0f;
214 + vec4 caustic = cSpecular;
193 215
194 216 if(dvd_lightEnabled[0]){
195 -
196 217 cAmbient += gl_LightSource[0].ambient * material[0];
197 218 cDiffuse *= gl_LightSource[0].diffuse;
219 + // Add specular intensity
220 + cSpecular = gl_LightSource[0].specular * material[2] * iSpecular;
221 + alpha = (_waterHeight - _vertexM.y) / (2*(_waterHeight - bbox_min.y));
222 + caustic = alpha * CausticsColor();
198 223 }
199 224
200 - vec4 cSpecular = material[2];
201 - if(underwater){
202 - ///Add specular intensity
203 -
204 - if(dvd_lightEnabled[0]){
205 - cSpecular = gl_LightSource[0].specular * material[2];
206 - cSpecular *= pow(clamp(dot(reflect(-lightVecTBN.xyz, normalTBN), viewVecTBN), 0.0, 1.0), material[3].x )/2.0;
207 - }
208 - cSpecular *= shadow;
225 + // SHADOW MAPS
226 + float shadow = 1.0;
227 + float distance = length(_viewDirection);
228 + if(distance < shadowMaxDistance) {
229 + applyShadowDirectional(iDiffuse, 0, shadow);
230 + shadow = 0.2 + 0.8 * (1.0 - (1.0-shadow) * (shadowMaxDistance-distance) / shadowMaxDistance);
209 231 }
210 232
211 - //return cAmbient * cBase + cDiffuse * cBase + cSpecular;
212 - return cAmbient * cBase + cDiffuse * cBase;
233 + return ((1-alpha) * (cAmbient * cBase + cDiffuse * cBase + cSpecular) + caustic) * shadow;
213 234 }