Subversion Repository Public Repository

Divide-Framework

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#ifndef _BUMP_MAPPING_FRAG_
#define _BUMP_MAPPING_FRAG_

uniform int bumpMapLightID = 0;

mat3 _privateTBNMatrix;

void bumpInit() {
    _privateTBNMatrix = mat3(VAR._tangentWV, VAR._bitangentWV, VAR._normalWV);
}

mat3 getTBNMatrix() {
    return _privateTBNMatrix;
}

vec3 getBump(in vec2 uv) {
    return normalize(2.0 * texture(texNormalMap, uv).rgb - 1.0);
}

float ReliefMapping_RayIntersection(in vec2 A, in vec2 AB){
    const int num_steps_lin = 10;
    const int num_steps_bin = 15;
    float linear_step = 1.0 / (float(num_steps_lin));
    //Current depth position
    float depth = 0.0; 
    //Best match found (starts with last position 1.0)
    float best_depth = 1.0;
    float step = linear_step;
    //Search from front to back for first point inside the object
    for(int i=0; i<num_steps_lin-1; i++){
        depth += step;
        if (depth >= 1.0 - texture(texNormalMap, A+AB*depth).a) {
            best_depth = depth; //Store best depth
            i = num_steps_lin-1;
        }
    }
    //The point of intersection is found between (depth) and (depth-step)
    //so start from (depth - step/2)
    step = linear_step * 0.5;
    depth = best_depth - step;
    // binary search
    for(int i = 0; i < num_steps_bin; ++i){
        step *= 0.5;
        best_depth = depth;
        if (depth >= 1.0 - texture(texNormalMap, A + AB * depth).a) {
            depth -= step;
        }else {
            depth += step;
        }
    }
    return best_depth;
}

vec4 ParallaxMapping(in int bumpMapLightID, in vec2 uv){
    vec3 lightVecTBN = vec3(0.0);
    switch (dvd_LightSource[bumpMapLightID]._options.x){
        case LIGHT_DIRECTIONAL      : 
            lightVecTBN = -normalize(dvd_LightSource[bumpMapLightID]._positionWV.xyz);
            break;
        case LIGHT_OMNIDIRECTIONAL  : 
        case LIGHT_SPOT             : 
            lightVecTBN = normalize(-VAR._vertexWV.xyz + dvd_LightSource[bumpMapLightID]._positionWV.xyz);
            break;
    };

    vec3 viewVecTBN = normalize(-VAR._vertexWV.xyz);
    
    //Offset, scale and bias
    vec2 vTexCoord = uv + 
                    ((texture(texNormalMap, uv).a - 0.5) * 
                     dvd_parallaxFactor * 
                     (vec2(viewVecTBN.x, -viewVecTBN.y) / 
                     viewVecTBN.z));

    return getPixelColour(vTexCoord, getTBNMatrix() * getBump(vTexCoord));
}

vec4 ReliefMapping(in int _light, in vec2 uv){
    vec3 viewVecTBN = normalize(-VAR._vertexWV.xyz);
    //Size and search starting position in texture space
    vec2 AB = dvd_reliefFactor * vec2(-viewVecTBN.x, viewVecTBN.y)/viewVecTBN.z;

    float h = ReliefMapping_RayIntersection(uv, AB);
    
    vec2 uv_offset = h * AB;
    
    vec3 p = VAR._vertexWV.xyz;
    vec3 v = normalize(p);
    //Compute light direction
    p += v*h*viewVecTBN.z;    
    
    vec2 planes;
    planes.x = -dvd_ZPlanesCombined.y / (dvd_ZPlanesCombined.y - dvd_ZPlanesCombined.x);
    planes.y = -dvd_ZPlanesCombined.y * dvd_ZPlanesCombined.x / (dvd_ZPlanesCombined.y - dvd_ZPlanesCombined.x);

    gl_FragDepth =((planes.x * p.z + planes.y) / -p.z);
    
    return getPixelColour(uv + uv_offset, getTBNMatrix() * getBump(uv + uv_offset));
}

// http://www.thetenthplanet.de/archives/1180
mat3 cotangent_frame(vec3 N, vec3 p, vec2 uv)
{
    // get edge vectors of the pixel triangle
    vec3 dp1 = dFdx(p);
    vec3 dp2 = dFdy(p);
    vec2 duv1 = dFdx(uv);
    vec2 duv2 = dFdy(uv);

    // solve the linear system
    vec3 dp2perp = cross(dp2, N);
    vec3 dp1perp = cross(N, dp1);
    vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
    vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;

    // construct a scale-invariant frame 
    float invmax = inversesqrt(max(dot(T, T), dot(B, B)));
    return mat3(T * invmax, B * invmax, N);
}

vec3 perturb_normal(vec3 texNorm, vec3 N, vec3 V, vec2 texcoord)
{
    // assume N, the interpolated vertex normal and 
    // V, the view vector (vertex to eye)
    texNorm = texNorm * 255. / 127. - 128. / 127.;
    mat3 TBN = cotangent_frame(N, -V, texcoord);
    return normalize(TBN * texNorm);
}


void aproximateTBN(in vec3 normalWV, out vec3 tangent, out vec3 bitangent) {
    vec3 c1 = cross(normalWV, vec3(0.0, 0.0, 1.0));
    vec3 c2 = cross(normalWV, vec3(0.0, 1.0, 0.0));
    if (length(c1) > length(c2)) {
        tangent = c1;
    } else {
        tangent = c2;
    }
    tangent = normalize(tangent);
    bitangent = normalize(cross(normalWV, tangent));
}
/*---------------- Normal blending -----------------*/
//ref: http://blog.selfshadow.com/sandbox/normals.html

vec3 normalLinearBlend(in vec3 n1, in vec3 n2) {
    return normalize(n1 + n2);
}

vec3 normalOverlayBlend(in vec3 n1, in vec3 n2) {
    /*n1 = texture2D(base_map,   uv).xyz;
    n2 = texture2D(detail_map, uv).xyz;

    vec3 n(overlay(n1.x, n2.x),
           overlay(n1.y, n2.y),
           overlay(n1.z, n2.z));

    return normalize(n*2.0 - 1.0);*/

    return normalLinearBlend(n1, n2);
}

vec3 normalPartialDerivativesBlend(in vec3 n1, in vec3 n2) {
    return normalize(vec3(n1.xy * n2.z + n2.xy * n1.z,
                         n1.z * n2.z));
}

vec3 normalWhiteoutBlend(in vec3 n1, in vec3 n2) {
    return normalize(vec3(n1.xy + n2.xy, n1.z*n2.z));
}

vec3 normalUDNBlend(in vec3 n1, in vec3 n2) {
    return normalize(vec3(n1.xy + n2.xy, n1.z));
}

vec3 normalRNMBlend(in vec3 n1, in vec3 n2) {
    vec3 n1_2 = (n1 + 1) * 0.5;
    vec3 n2_2 = (n2 + 1) * 0.5;

    n1_2 = n1_2 * vec3( 2,  2, 2) + vec3(-1, -1,  0);
    n2_2 = n2_2 * vec3(-2, -2, 2) + vec3( 1,  1, -1);
    return n1_2 * dot(n1_2, n2_2) / n1_2.z - n2_2;
}

vec3 normalUnityBlend(in vec3 n1, in vec3 n2) {
    mat3 nBasis = mat3(
        vec3(n1.z, n1.y, -n1.x), // +90 degree rotation around y axis
        vec3(n1.x, n1.z, -n1.y), // -90 degree rotation around x axis
        vec3(n1.x, n1.y,  n1.z));
    return normalize(n2.x*nBasis[0] + n2.y*nBasis[1] + n2.z*nBasis[2]);
}

#endif //_BUMP_MAPPING_FRAG_

Commits for Divide-Framework/trunk/assets/shaders/GLSL/fragmentAtoms/bumpMapping.frag

Diff revisions: vs.
Revision Author Commited Message
887 Diff Diff IonutCava picture IonutCava Mon 03 Apr, 2017 15:42:51 +0000

[IonutCava]
- Attempt to add a new bumpmapping technique to terrain
- Attempt to smooth terrain by averaging nearby heights

884 Diff Diff IonutCava picture IonutCava Mon 27 Mar, 2017 15:32:26 +0000

[Ionut]
- More terrain work

811 Diff Diff IonutCava picture IonutCava Wed 11 Jan, 2017 17:26:49 +0000

[IonutCava]
- Fix some threadpool cleanup bugs
- Fix terrain rendering (except underwater caustics)
- Fix terrain loading
- Fix terrain unloading

806 Diff Diff IonutCava picture IonutCava Sun 08 Jan, 2017 22:00:48 +0000

[IonutCava]
- Finish implementing per-fragment velocity computation using a compute shader
— Further tuning still needed
- Add VS2017 build targets

805 Diff Diff IonutCava picture IonutCava Fri 02 Dec, 2016 16:05:59 +0000

[IonutCava]
- Singleton elimination update Part I.I: Correct Part I
— Small corrections to previous commit: Fix all asserts, memory leaks and errors that appeared during the following test: Start app -> Load War Scene -> Return to Default Scene -> Quit

800 Diff Diff IonutCava picture IonutCava Fri 25 Nov, 2016 17:04:02 +0000

[IonutCava]
- More profile-guided optimizations
- Added GLSL normal map blending methods from: http://blog.selfshadow.com/sandbox/normals.html
- Added more profile timers

721 Diff Diff IonutCava picture IonutCava Wed 08 Jun, 2016 15:47:49 +0000

[IonutCava]
- Spelling: change all references from our code of “color” to the PROPER British version of “colour” because it sounds 100x better

673 Diff Diff IonutCava picture IonutCava Sun 10 Apr, 2016 19:26:18 +0000

[IonutCava]
- Use stl_allocator with hashmap
- Various memory allocation tweaks and bug fixes
- Initial Toksvig specular-AA shader code (from http://www.selfshadow.com/sandbox/gloss.html)

587 Diff Diff IonutCava picture IonutCava Fri 27 Nov, 2015 17:00:25 +0000

[IonutCava]
- Finished multi-light rendering:
— Proper position/distance transform (homogeneous trans for positions / non-homogeneous trans for directions)
-— Point lights with range based attenuation
-— Spot lights with inner and outer cones for smooth falloff
-— Directional lights same as before
- Added a basic Flashlight system: spot light at the camera’s position facing forward (toggled with L key)
- WarScene updated:
— 16 point lights moving around the scene with random colours

586 IonutCava picture IonutCava Thu 26 Nov, 2015 17:11:27 +0000

[IonutCava]
- Point and Directional light updates