Debian Codegraph @

tests/manual/tessellation-modes/shaders/phongwireframe.frag

raw
1 #version 400 core
2
3 uniform struct LightInfo {
4 vec4 position;
5 vec3 intensity;
6 } light;
7
8 uniform struct LineInfo {
9 float width;
10 vec4 color;
11 } line;
12
13 uniform vec3 ka; // Ambient reflectivity
14 uniform vec3 kd; // Diffuse reflectivity
15 uniform vec3 ks; // Specular reflectivity
16 uniform float shininess; // Specular shininess factor
17
18 in WireframeVertex {
19 vec3 position;
20 vec3 normal;
21 noperspective vec4 edgeA;
22 noperspective vec4 edgeB;
23 flat int configuration;
24 } fs_in;
25
26 out vec4 fragColor;
27
28 vec3 adsModel( const in vec3 pos, const in vec3 n )
29 {
30 // Calculate the vector from the light to the fragment
31 vec3 s = normalize( vec3( light.position ) - pos );
32
33 // Calculate the vector from the fragment to the eye position (the
34 // origin since this is in "eye" or "camera" space
35 vec3 v = normalize( -pos );
36
37 // Refleft the light beam using the normal at this fragment
38 vec3 r = reflect( -s, n );
39
40 // Calculate the diffus component
41 vec3 diffuse = vec3( max( dot( s, n ), 0.0 ) );
42
43 // Calculate the specular component
44 vec3 specular = vec3( pow( max( dot( r, v ), 0.0 ), shininess ) );
45
46 // Combine the ambient, diffuse and specular contributions
47 return light.intensity * ( ka + kd * diffuse + ks * specular );
48 }
49
50 vec4 shadeLine( const in vec4 color )
51 {
52 // Find the smallest distance between the fragment and a triangle edge
53 float d;
54 if ( fs_in.configuration == 0 )
55 {
56 // Common configuration
57 d = min( fs_in.edgeA.x, fs_in.edgeA.y );
58 d = min( d, fs_in.edgeA.z );
59 }
60 else
61 {
62 // Handle configuration where screen space projection breaks down
63 // Compute and compare the squared distances
64 vec2 AF = gl_FragCoord.xy - fs_in.edgeA.xy;
65 float sqAF = dot( AF, AF );
66 float AFcosA = dot( AF, fs_in.edgeA.zw );
67 d = abs( sqAF - AFcosA * AFcosA );
68
69 vec2 BF = gl_FragCoord.xy - fs_in.edgeB.xy;
70 float sqBF = dot( BF, BF );
71 float BFcosB = dot( BF, fs_in.edgeB.zw );
72 d = min( d, abs( sqBF - BFcosB * BFcosB ) );
73
74 // Only need to care about the 3rd edge for some configurations.
75 if ( fs_in.configuration == 1 || fs_in.configuration == 2 || fs_in.configuration == 4 )
76 {
77 float AFcosA0 = dot( AF, normalize( fs_in.edgeB.xy - fs_in.edgeA.xy ) );
78 d = min( d, abs( sqAF - AFcosA0 * AFcosA0 ) );
79 }
80
81 d = sqrt( d );
82 }
83
84 // Blend between line color and phong color
85 float mixVal;
86 if ( d < line.width - 1.0 )
87 {
88 mixVal = 1.0;
89 }
90 else if ( d > line.width + 1.0 )
91 {
92 mixVal = 0.0;
93 }
94 else
95 {
96 float x = d - ( line.width - 1.0 );
97 mixVal = exp2( -2.0 * ( x * x ) );
98 }
99
100 return mix( color, line.color, mixVal );
101 }
102
103 void main()
104 {
105 // Calculate the color from the phong model
106 vec4 color = vec4( adsModel( fs_in.position, normalize( fs_in.normal ) ), 1.0 );
107 fragColor = shadeLine( color );
108 }