File: momentum.glsl

package info (click to toggle)
python-moderngl-window 2.4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 69,260 kB
  • sloc: python: 11,225; makefile: 20
file content (81 lines) | stat: -rw-r--r-- 1,936 bytes parent folder | download | duplicates (2)
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
#version 330

#if defined VERTEX_SHADER

in vec3 in_position;
in vec2 in_texcoord_0;
out vec2 uv0;

void main() {
    gl_Position = vec4(in_position, 1);
    uv0 = in_texcoord_0;
}

#elif defined FRAGMENT_SHADER

out float fragColor;
in vec2 uv0;

uniform sampler2D momentum_texture;
uniform sampler2D pressure_texture;
uniform sampler2D walls_texture;
uniform float rho;
uniform float damping;
uniform float external_flow;

const ivec2 kpos[9] = ivec2[9](
    ivec2(-1,  1),  ivec2(0,  1),  ivec2(1,  1),
    ivec2(-1,  0),  ivec2(0,  0),  ivec2(1,  0),
    ivec2(-1, -1),  ivec2(0, -1),  ivec2(1, -1)
);

const float diff_kernel[9] = float[9](
    .025,  .1, .025,
      .1,  .5,   .1,
    .025,  .1, .025
);

const float con_kernel[9] = float[9](
      0.0, .25,   0.0,
     .25,  -1.0, .25,
      0.0, .25,   0.0
);

float diffusion(ivec2 uv, sampler2D source) {
    float value = 0;
    for (int i = 0; i < 9; i++) {
        value += texelFetch(source, uv + kpos[i], 0).r * diff_kernel[i];
    }
    return value;
}

const float viscosity = .018;

float convection(ivec2 uv, sampler2D source) {
    float value = 0;
    for (int i = 0; i < 9; i++) {
        value += texelFetch(source, uv + kpos[i], 0).r * con_kernel[i];
    }
    return value;
}

void main() {
    ivec2 uv = ivec2(gl_FragCoord.xy);

    float momentum = texelFetch(momentum_texture, uv, 0).r;
    float wall = texelFetch(walls_texture, uv, 0).r;
    
    float diff_momentum = diffusion(uv, momentum_texture);
    float con_momentum = convection(uv, momentum_texture);
    float con_pressure = convection(uv, pressure_texture);

    if (wall > 0) {
        fragColor = -external_flow;
    } else {
        fragColor = (diff_momentum - 
                    viscosity * momentum *
                    con_momentum +
                    con_pressure * rho) * damping;
    }
}
#endif