#!/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; 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; // Varyings // ------------------------------------ varying vec3 v_normal; varying vec4 v_fg_color; varying vec3 v_position; varying mat4 u_view; varying vec3 vt_position; // 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; 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; 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; 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(); //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; } """