ogl_beamforming

Ultrasound Beamforming Implemented with OpenGL
git clone anongit@rnpnr.xyz:ogl_beamforming.git
Log | Files | Refs | Feed | Submodules | README | LICENSE

render_3d.frag.glsl (2649B)


      1 /* See LICENSE for license details. */
      2 
      3 /* input:  h [0,360] | s,v [0, 1] *
      4  * output: rgb [0,1]              */
      5 vec3 hsv2rgb(vec3 hsv)
      6 {
      7 	vec3 k = mod(vec3(5, 3, 1) + hsv.x / 60, 6);
      8 	k = max(min(min(k, 4 - k), 1), 0);
      9 	return hsv.z - hsv.z * hsv.y * k;
     10 }
     11 
     12 /* NOTE(rnp): adapted from: https://iquilezles.org/articles/distfunctions */
     13 float sdf_wire_box_outside(vec3 p, vec3 b, float e)
     14 {
     15 	p = abs(p) - b;
     16 	vec3 q = abs(p + e) - e;
     17 	float result = min(min(length(max(vec3(p.x, q.y, q.z), 0.0)),
     18 	                       length(max(vec3(q.x, p.y, q.z), 0.0))),
     19 	                       length(max(vec3(q.x, q.y, p.z), 0.0)));
     20 	return result;
     21 }
     22 
     23 int texture_dimension(ivec3 points)
     24 {
     25 	points = ivec3(greaterThan(points, ivec3(1)));
     26 	return points.x + points.y + points.z;
     27 }
     28 
     29 
     30 float sample_value(vec3 p)
     31 {
     32 	float result = length(texture(u_texture, p).xy);
     33 	float threshold_val = pow(10.0f, u_threshold / 20.0f);
     34 	result = clamp(result, 0.0f, threshold_val);
     35 	result = result / threshold_val;
     36 	result = pow(result, u_gamma);
     37 
     38 	if (u_log_scale) {
     39 		result = 20 * log(result) / log(10);
     40 		result = clamp(result, -u_db_cutoff, 0) / -u_db_cutoff;
     41 		result = 1 - result;
     42 	}
     43 
     44 	return result;
     45 }
     46 
     47 float grad(float x)
     48 {
     49 	float h  = length(fwidth(texture_coordinate.xy));
     50 	float s1 = sample_value(vec3(x + h, 0, 0));
     51 	float s2 = sample_value(vec3(x - h, 0, 0));
     52 	return (s1 - s2) / (2.0f * h);
     53 }
     54 
     55 void main(void)
     56 {
     57 	int dimension = texture_dimension(textureSize(u_texture, 0));
     58 
     59 	if (dimension == 3) {
     60 		// TODO(rnp): add slice offset passed in as a uniform
     61 	}
     62 
     63 	float smp = sample_value(texture_coordinate);
     64 	//float t = test_texture_coordinate.y;
     65 	//smp = smp * smoothstep(-0.4, 1.1, t) * u_gain;
     66 
     67 	vec3  p = 2.0f * test_texture_coordinate - 1.0f;
     68 
     69 	switch (dimension) {
     70 	case 1:{
     71 
     72 		float df = mix(grad(texture_coordinate.x), dFdx(smp),
     73 		               smoothstep(0.0f, 0.55f, abs(texture_coordinate.x - 0.5f)));
     74 		float de = abs(smp - texture_coordinate.y) / sqrt(1.0f + df * df);
     75 
     76 		float eps       = length(fwidth(texture_coordinate.xy));
     77 		float thickness = 4.f;
     78 
     79 		float alpha = smoothstep((0.5f * thickness + 2.0f) * eps, (0.5f * thickness + 0.0f) * eps, de);
     80 		out_colour = vec4(u_bb_colour.xyz, alpha);
     81 	}break;
     82 
     83 	case 2:
     84 	case 3:
     85 	{
     86 		float t = clamp(sdf_wire_box_outside(p, vec3(1.0f), u_bb_fraction) / u_bb_fraction, 0, 1);
     87 
     88 		out_colour = vec4(t * vec3(smp) + (1 - t) * u_bb_colour.xyz, 1);
     89 		if (u_solid_bb) out_colour = u_bb_colour;
     90 	}break;
     91 	}
     92 
     93 	//out_colour = vec4(textureQueryLod(u_texture, texture_coordinate).y, 0, 0, 1);
     94 	//out_colour = vec4(abs(normal), 1);
     95 	//out_colour = vec4(1, 1, 1, smp);
     96 	//out_colour = vec4(smp * abs(normal), 1);
     97 }