Blame view

tube_shaders.py 5.94 KB
9f9f1788   Pavel Govyadinov   clead up version ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #!/usr/bin/env python3
  # -*- coding: utf-8 -*-
  """
  Created on Mon Aug  5 15:58:30 2019
  
  @author: pavel
  """
  
  #vertex shader for the tube drawing
  VERT_SHADER = """
  // Uniforms
  // ------------------------------------
  uniform vec4 u_bb[26];
  uniform vec3 u_LightPost;
  uniform mat4 u_model;
  //uniform mat4 u_view;
  uniform mat4 u_projection;
2282da38   Pavel Govyadinov   added path select...
18
  uniform float u_selection;
9f9f1788   Pavel Govyadinov   clead up version ...
19
20
21
22
23
24
25
26
27
28
  
  uniform vec3 u_eye;
  uniform vec3 u_up;
  uniform vec3 u_target;
  
  // Attributes
  // ------------------------------------
  attribute vec3 a_position;
  attribute vec3 a_normal;
  attribute vec4 a_fg_color;
2282da38   Pavel Govyadinov   added path select...
29
  attribute float a_selection;
9f9f1788   Pavel Govyadinov   clead up version ...
30
31
32
33
34
35
36
37
  
  // Varyings
  // ------------------------------------
  varying vec3 v_normal;
  varying vec4 v_fg_color;
  varying vec3 v_position;
  varying mat4 u_view;
  varying vec3 vt_position;
2282da38   Pavel Govyadinov   added path select...
38
  varying float v_selection;
9f9f1788   Pavel Govyadinov   clead up version ...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  
  
  // Functions
  // ------------------------------------
  
  mat4 make_view(vec3 eye, vec3 target, vec3 up);
  
  void main (void) {
      // Calculate position
      mat4 u_view = make_view(u_eye, u_target, u_up);
  
      mat4 MVP = u_projection * u_view * u_model;
      mat4 MV = u_view * u_model;
      v_normal = vec3(MV * vec4(a_normal, 0.0));
      v_position = vec3(MV*vec4(a_position, 1.0));
      vt_position = vec3(a_position);
      v_fg_color = a_fg_color;
2282da38   Pavel Govyadinov   added path select...
56
      v_selection = a_selection;
9f9f1788   Pavel Govyadinov   clead up version ...
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
      gl_Position = MVP * vec4(a_position, 1.0);
  }
  
  mat4 make_view(vec3 eye, vec3 target, vec3 up)
  {
       vec3 zaxis = normalize(eye - target);
       vec3 xaxis = normalize(cross(up, zaxis));
       vec3 yaxis = cross(zaxis,  xaxis);
  
       mat4 viewMatrix = mat4(
          vec4(      xaxis.x,            yaxis.x,            zaxis.x,       0 ),
          vec4(      xaxis.y,            yaxis.y,            zaxis.y,       0 ),
          vec4(      xaxis.z,            yaxis.z,            zaxis.z,       0 ),
          vec4(-dot( xaxis, eye ), -dot( yaxis, eye ), -dot( zaxis, eye ),  1 )
      );
  
  //     mat4 viewMatrix = mat4(
  //        vec4(      xaxis.x,            xaxis.y,            xaxis.z,       -dot( xaxis, eye ) ),
  //        vec4(      yaxis.x,            yaxis.y,            yaxis.z,       -dot( yaxis, eye ) ),
  //        vec4(      zaxis.x,            zaxis.y,            zaxis.z,       -dot( zaxis, eye ) ),
  //        vec4(         0,                  0,                   0,                   1)
  //    );
  
  
       return viewMatrix;
  }
  """
  
  #Fragment shader for the tube drawing.
  FRAG_SHADER = """
  // Uniforms
  // ------------------------------------
  uniform vec3 u_bb[26];
  uniform vec3 u_LightPos;
  uniform mat4 u_model;
  //uniform mat4 u_view;
  uniform mat4 u_projection;
2282da38   Pavel Govyadinov   added path select...
94
  uniform float u_selection;
9f9f1788   Pavel Govyadinov   clead up version ...
95
96
97
98
99
100
101
102
103
104
105
106
  
  uniform vec3 u_eye;
  uniform vec3 u_up;
  uniform vec3 u_target;
  
  // Varyings
  // ------------------------------------
  varying vec3 v_normal;
  varying vec4 v_fg_color;
  varying vec3 v_position;
  varying mat4 u_view;
  varying vec3 vt_position;
2282da38   Pavel Govyadinov   added path select...
107
  varying float v_selection;
9f9f1788   Pavel Govyadinov   clead up version ...
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
  
  float new_alpha();
  float set_view();
  float bin_alpha(float alpha);
  
  void main()
  {
      //Ambient Lighting
      float ambientStrength = 0.5;
      vec4 ambient_color = ambientStrength*v_fg_color;
  
  
      mat4 MV = u_projection * u_view * u_model;
      //vec3 u_LightP = vec3(0., 0., 0.);
      vec3 u_LightP = vec3(u_eye);
      //mat4 inv = inverse(u_view);
  
      //vec3 u_LightP = vec3(u_view[0][3], u_view[1][3], u_view[2][3]);
  
      //Diffuse Lighting
      //float distance = length(u_LightP - v_position);
      vec3 norm = normalize(v_normal);
      vec3 lightVector = normalize(u_LightP - v_position);
      float diffuse = max(dot(norm, lightVector), 0.0);
  
      //diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance * distance)));
  
      vec4 color = (ambient_color + diffuse)*v_fg_color;
      float alpha = new_alpha();
2282da38   Pavel Govyadinov   added path select...
137
138
139
      if(u_selection > 0.0)
          alpha = max(alpha*v_selection, 0.00);
      //alpha = max(alpha * v_selection, 0.05);
9f9f1788   Pavel Govyadinov   clead up version ...
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
      //if(alpha == 1.0)
      //    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
      //    gl_FragColor = vec4(color.rgb, alpha);
      //else
          gl_FragColor = vec4(color.rgb, alpha);
      //    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
      //gl_FragColor = v_fg_color;
  }
  
  float new_alpha()
  {
       float alpha = 0.0;
  
       //find the min and the max
       float mx = -100000.0;
       float mn = 100000.0;
       vec3 u_light = vec3(u_eye);
       for(int i = 0; i < 26; i++)
       {
            //vec3 rot_bb = vec3(u_projection * u_view * u_model * vec4(u_bb[i], 1.0));
            vec3 rot_bb = u_bb[i];
            if(length(rot_bb - u_light) < mn)
            {
                mn = length(rot_bb - u_light);
            }
            if(length(rot_bb - u_light) > mx)
            {
                mx = length(rot_bb - u_light);
            }
       }
  
       float l = length(u_light - vt_position);
       alpha = (l-mn)/(mx-mn);
       return bin_alpha(alpha);
  //     if(alpha > 0.9)
  //         return 1.0;
  //     else if(alpha > 0.8)
  //         return 0.9;
  //     else if(alpha > 0.7)
  //         return 0.8;
  //     else if (alpha > 0.6)
  //         return 0.7;
  //     else if (alpha > 0.5)
  //         return 0.6;
  //     else if (alpha > 0.4)
  //         return 0.5;
  //     else if (alpha > 0.3)
  //         return 0.4;
  //     else if (alpha > 0.2)
  //         return 0.3;
  //     else if (alpha > 0.1)
  //         return 0.2;
  //    else if (alpha > 0.0)
  //         return 0.0;
  //     else
  //         return alpha;
  }
  
  float bin_alpha(float alpha)
  {
  
   
       if(alpha > 0.8)
           return 0.0;
       else if(alpha > 0.6)
           return 0.1;
       else if (alpha > 0.4)
           return 0.4;
       else if (alpha > 0.2)
           return 0.8;
       else if (alpha > 0.0)
           return 1.0;
       else
           return 1.0;
       
  //     if(alpha > 0.9)
  //         return 0.0;
  //     else if(alpha > 0.8)
  //         return 0.1;
  //     else if(alpha > 0.7)
  //         return 0.2;
  //     else if (alpha > 0.6)
  //         return 0.3;
  //     else if (alpha > 0.5)
  //         return 0.4;
  //     else if (alpha > 0.4)
  //         return 0.5;
  //     else if (alpha > 0.3)
  //         return 0.6;
  //     else if (alpha > 0.2)
  //         return 0.7;
  //     else if (alpha > 0.1)
  //         return 0.8;
  //     else if (alpha > 0.0)
  //         return 0.9;
  //     else
  //         return 1.0;
  }
  
  """