| 1 |
#version 400 core |
| 2 |
|
| 3 |
layout( triangles ) in; |
| 4 |
layout( triangle_strip, max_vertices = 3 ) out; |
| 5 |
|
| 6 |
in EyeSpaceVertex { |
| 7 |
vec3 position; |
| 8 |
vec3 normal; |
| 9 |
} gs_in[]; |
| 10 |
|
| 11 |
out WireframeVertex { |
| 12 |
vec3 position; |
| 13 |
vec3 normal; |
| 14 |
noperspective vec4 edgeA; |
| 15 |
noperspective vec4 edgeB; |
| 16 |
flat int configuration; |
| 17 |
} gs_out; |
| 18 |
|
| 19 |
uniform mat4 viewportMatrix; |
| 20 |
|
| 21 |
const int infoA[] = int[]( 0, 0, 0, 0, 1, 1, 2 ); |
| 22 |
const int infoB[] = int[]( 1, 1, 2, 0, 2, 1, 2 ); |
| 23 |
const int infoAd[] = int[]( 2, 2, 1, 1, 0, 0, 0 ); |
| 24 |
const int infoBd[] = int[]( 2, 2, 1, 2, 0, 2, 1 ); |
| 25 |
|
| 26 |
vec2 transformToViewport( const in vec4 p ) |
| 27 |
{ |
| 28 |
return vec2( viewportMatrix * ( p / p.w ) ); |
| 29 |
} |
| 30 |
|
| 31 |
void main() |
| 32 |
{ |
| 33 |
gs_out.configuration = int(gl_in[0].gl_Position.z < 0) * int(4) |
| 34 |
+ int(gl_in[1].gl_Position.z < 0) * int(2) |
| 35 |
+ int(gl_in[2].gl_Position.z < 0); |
| 36 |
|
| 37 |
// If all vertices are behind us, cull the primitive |
| 38 |
if (gs_out.configuration == 7) |
| 39 |
return; |
| 40 |
|
| 41 |
// Transform each vertex into viewport space |
| 42 |
vec2 p[3]; |
| 43 |
p[0] = transformToViewport( gl_in[0].gl_Position ); |
| 44 |
p[1] = transformToViewport( gl_in[1].gl_Position ); |
| 45 |
p[2] = transformToViewport( gl_in[2].gl_Position ); |
| 46 |
|
| 47 |
if (gs_out.configuration == 0) |
| 48 |
{ |
| 49 |
// Common configuration where all vertices are within the viewport |
| 50 |
gs_out.edgeA = vec4(0.0); |
| 51 |
gs_out.edgeB = vec4(0.0); |
| 52 |
|
| 53 |
// Calculate lengths of 3 edges of triangle |
| 54 |
float a = length( p[1] - p[2] ); |
| 55 |
float b = length( p[2] - p[0] ); |
| 56 |
float c = length( p[1] - p[0] ); |
| 57 |
|
| 58 |
// Calculate internal angles using the cosine rule |
| 59 |
float alpha = acos( ( b * b + c * c - a * a ) / ( 2.0 * b * c ) ); |
| 60 |
float beta = acos( ( a * a + c * c - b * b ) / ( 2.0 * a * c ) ); |
| 61 |
|
| 62 |
// Calculate the perpendicular distance of each vertex from the opposing edge |
| 63 |
float ha = abs( c * sin( beta ) ); |
| 64 |
float hb = abs( c * sin( alpha ) ); |
| 65 |
float hc = abs( b * sin( alpha ) ); |
| 66 |
|
| 67 |
// Now add this perpendicular distance as a per-vertex property in addition to |
| 68 |
// the position and normal calculated in the vertex shader. |
| 69 |
|
| 70 |
// Vertex 0 (a) |
| 71 |
gs_out.edgeA = vec4( ha, 0.0, 0.0, 0.0 ); |
| 72 |
gs_out.normal = gs_in[0].normal; |
| 73 |
gs_out.position = gs_in[0].position; |
| 74 |
gl_Position = gl_in[0].gl_Position; |
| 75 |
EmitVertex(); |
| 76 |
|
| 77 |
// Vertex 1 (b) |
| 78 |
gs_out.edgeA = vec4( 0.0, hb, 0.0, 0.0 ); |
| 79 |
gs_out.normal = gs_in[1].normal; |
| 80 |
gs_out.position = gs_in[1].position; |
| 81 |
gl_Position = gl_in[1].gl_Position; |
| 82 |
EmitVertex(); |
| 83 |
|
| 84 |
// Vertex 2 (c) |
| 85 |
gs_out.edgeA = vec4( 0.0, 0.0, hc, 0.0 ); |
| 86 |
gs_out.normal = gs_in[2].normal; |
| 87 |
gs_out.position = gs_in[2].position; |
| 88 |
gl_Position = gl_in[2].gl_Position; |
| 89 |
EmitVertex(); |
| 90 |
|
| 91 |
// Finish the primitive off |
| 92 |
EndPrimitive(); |
| 93 |
} |
| 94 |
else |
| 95 |
{ |
| 96 |
// Viewport projection breaks down for one or two vertices. |
| 97 |
// Caclulate what we can here and defer rest to fragment shader. |
| 98 |
// Since this is coherent for the entire primitive the conditional |
| 99 |
// in the fragment shader is still cheap as all concurrent |
| 100 |
// fragment shader invocations will take the same code path. |
| 101 |
|
| 102 |
// Copy across the viewport-space points for the (up to) two vertices |
| 103 |
// in the viewport |
| 104 |
gs_out.edgeA.xy = p[infoA[gs_out.configuration]]; |
| 105 |
gs_out.edgeB.xy = p[infoB[gs_out.configuration]]; |
| 106 |
|
| 107 |
// Copy across the viewport-space edge vectors for the (up to) two vertices |
| 108 |
// in the viewport |
| 109 |
gs_out.edgeA.zw = normalize( gs_out.edgeA.xy - p[ infoAd[gs_out.configuration] ] ); |
| 110 |
gs_out.edgeB.zw = normalize( gs_out.edgeB.xy - p[ infoBd[gs_out.configuration] ] ); |
| 111 |
|
| 112 |
// Pass through the other vertex attributes |
| 113 |
gs_out.normal = gs_in[0].normal; |
| 114 |
gs_out.position = gs_in[0].position; |
| 115 |
gl_Position = gl_in[0].gl_Position; |
| 116 |
EmitVertex(); |
| 117 |
|
| 118 |
gs_out.normal = gs_in[1].normal; |
| 119 |
gs_out.position = gs_in[1].position; |
| 120 |
gl_Position = gl_in[1].gl_Position; |
| 121 |
EmitVertex(); |
| 122 |
|
| 123 |
gs_out.normal = gs_in[2].normal; |
| 124 |
gs_out.position = gs_in[2].position; |
| 125 |
gl_Position = gl_in[2].gl_Position; |
| 126 |
EmitVertex(); |
| 127 |
|
| 128 |
// Finish the primitive off |
| 129 |
EndPrimitive(); |
| 130 |
} |
| 131 |
} |