diff options
Diffstat (limited to 'examples/qt3d/tessellation-modes/shaders')
10 files changed, 469 insertions, 0 deletions
diff --git a/examples/qt3d/tessellation-modes/shaders/flat.frag b/examples/qt3d/tessellation-modes/shaders/flat.frag new file mode 100644 index 000000000..6e596d4d6 --- /dev/null +++ b/examples/qt3d/tessellation-modes/shaders/flat.frag @@ -0,0 +1,12 @@ +#version 400 core + +in Vertex { + vec3 color; +} fs_in; + +out vec4 fragColor; + +void main() +{ + fragColor = vec4( fs_in.color, 1.0 ); +} diff --git a/examples/qt3d/tessellation-modes/shaders/isolines.tcs b/examples/qt3d/tessellation-modes/shaders/isolines.tcs new file mode 100644 index 000000000..624af51cc --- /dev/null +++ b/examples/qt3d/tessellation-modes/shaders/isolines.tcs @@ -0,0 +1,15 @@ +#version 400 core + +layout( vertices = 4 ) out; + +uniform float outer[4] = float[]( 1.0, 1.0, 1.0, 1.0 ); + +void main() +{ + // Pass along the vertex position unmodified + gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; + + // Set the tessellation levels + gl_TessLevelOuter[0] = outer[0]; // number of isolines + gl_TessLevelOuter[1] = outer[1]; // divisions along isoline +} diff --git a/examples/qt3d/tessellation-modes/shaders/isolines.tes b/examples/qt3d/tessellation-modes/shaders/isolines.tes new file mode 100644 index 000000000..f6b984bc8 --- /dev/null +++ b/examples/qt3d/tessellation-modes/shaders/isolines.tes @@ -0,0 +1,74 @@ +#version 400 core + +layout( isolines, fractional_even_spacing, ccw ) in; + +out Vertex { + vec3 color; +} te_out; + +uniform mat4 modelViewMatrix; +uniform mat3 normalMatrix; +uniform mat4 projectionMatrix; +uniform mat4 mvp; + +// Calculate RGB triplet from HSV +vec3 hsvToRGB( float h, float s, float v ) +{ + if ( s <= 0.0 ) + return vec3( v ); + + h = h * 6.0; + float c = v * s; + float x = ( 1.0 - abs( ( mod( h, 2 ) - 1 ) ) ) * c; + float m = v - c; + float r = 0.0; + float g = 0.0; + float b = 0.0; + + if ( h < 1.0 ) { r = c; g = x; b = 0.0;} + else if ( h < 2.0 ) { r = x; g = c; b = 0.0; } + else if ( h < 3.0 ) { r = 0.0; g = c; b = x; } + else if ( h < 4.0 ) { r = 0.0; g = x; b = c; } + else if ( h < 5.0 ) { r = x; g = 0.0; b = c; } + else { r = c; g = 0.0; b = x; } + + return vec3( r + m, g + m, b + m ); +} + +void main() +{ + float u = gl_TessCoord.x; + float v = gl_TessCoord.y; + + vec4 a = gl_in[0].gl_Position; + vec4 b = gl_in[1].gl_Position; + vec4 c = gl_in[2].gl_Position; + vec4 d = gl_in[3].gl_Position; + + // Use the (u,v) parametric coords to calculate the vertex. + // u is the position along an isoline + // v is isoline number + + // Let's make a sinusoidal curve as a function of u that varies + // in the v direction which we will define as orthogonal to u + + // Interpolate in u along top and bottom edges of the patch + vec4 q0 = mix( a, b, u ); + vec4 q1 = mix( d, c, u ); + + // Interpolate in v between the above positions. This gives the + // nominal position for our vertex + vec4 p = mix( q0, q1, v ); + + // Find "vertical" direction + vec4 vBasis = normalize( q1 - q0 ); + + // Offset vertex in this direction using sinusoid + vec4 pos = p + 0.2 * vBasis * sin( 20.0 * u ); + + // Use a hue value based on v + te_out.color = hsvToRGB( v, 1.0, 1.0 ); + + // Transform to clip-space + gl_Position = mvp * pos; +} diff --git a/examples/qt3d/tessellation-modes/shaders/passthru.vert b/examples/qt3d/tessellation-modes/shaders/passthru.vert new file mode 100644 index 000000000..71875a55f --- /dev/null +++ b/examples/qt3d/tessellation-modes/shaders/passthru.vert @@ -0,0 +1,10 @@ +#version 400 core + +in vec3 vertexPosition; + +void main() +{ + // We do the transformations later in the + // tessellation evaluation shader + gl_Position = vec4( vertexPosition, 1.0 ); +} diff --git a/examples/qt3d/tessellation-modes/shaders/phongwireframe.frag b/examples/qt3d/tessellation-modes/shaders/phongwireframe.frag new file mode 100644 index 000000000..4fd62007d --- /dev/null +++ b/examples/qt3d/tessellation-modes/shaders/phongwireframe.frag @@ -0,0 +1,108 @@ +#version 400 core + +uniform struct LightInfo { + vec4 position; + vec3 intensity; +} light; + +uniform struct LineInfo { + float width; + vec4 color; +} line; + +uniform vec3 ka; // Ambient reflectivity +uniform vec3 kd; // Diffuse reflectivity +uniform vec3 ks; // Specular reflectivity +uniform float shininess; // Specular shininess factor + +in WireframeVertex { + vec3 position; + vec3 normal; + noperspective vec4 edgeA; + noperspective vec4 edgeB; + flat int configuration; +} fs_in; + +out vec4 fragColor; + +vec3 adsModel( const in vec3 pos, const in vec3 n ) +{ + // Calculate the vector from the light to the fragment + vec3 s = normalize( vec3( light.position ) - pos ); + + // Calculate the vector from the fragment to the eye position (the + // origin since this is in "eye" or "camera" space + vec3 v = normalize( -pos ); + + // Refleft the light beam using the normal at this fragment + vec3 r = reflect( -s, n ); + + // Calculate the diffus component + vec3 diffuse = vec3( max( dot( s, n ), 0.0 ) ); + + // Calculate the specular component + vec3 specular = vec3( pow( max( dot( r, v ), 0.0 ), shininess ) ); + + // Combine the ambient, diffuse and specular contributions + return light.intensity * ( ka + kd * diffuse + ks * specular ); +} + +vec4 shadeLine( const in vec4 color ) +{ + // Find the smallest distance between the fragment and a triangle edge + float d; + if ( fs_in.configuration == 0 ) + { + // Common configuration + d = min( fs_in.edgeA.x, fs_in.edgeA.y ); + d = min( d, fs_in.edgeA.z ); + } + else + { + // Handle configuration where screen space projection breaks down + // Compute and compare the squared distances + vec2 AF = gl_FragCoord.xy - fs_in.edgeA.xy; + float sqAF = dot( AF, AF ); + float AFcosA = dot( AF, fs_in.edgeA.zw ); + d = abs( sqAF - AFcosA * AFcosA ); + + vec2 BF = gl_FragCoord.xy - fs_in.edgeB.xy; + float sqBF = dot( BF, BF ); + float BFcosB = dot( BF, fs_in.edgeB.zw ); + d = min( d, abs( sqBF - BFcosB * BFcosB ) ); + + // Only need to care about the 3rd edge for some configurations. + if ( fs_in.configuration == 1 || fs_in.configuration == 2 || fs_in.configuration == 4 ) + { + float AFcosA0 = dot( AF, normalize( fs_in.edgeB.xy - fs_in.edgeA.xy ) ); + d = min( d, abs( sqAF - AFcosA0 * AFcosA0 ) ); + } + + d = sqrt( d ); + } + + // Blend between line color and phong color + float mixVal; + if ( d < line.width - 1.0 ) + { + mixVal = 1.0; + } + else if ( d > line.width + 1.0 ) + { + mixVal = 0.0; + } + else + { + float x = d - ( line.width - 1.0 ); + mixVal = exp2( -2.0 * ( x * x ) ); + } + + return mix( color, line.color, mixVal ); +} + +void main() +{ + // Calculate the color from the phong model + vec4 color = vec4( adsModel( fs_in.position, normalize( fs_in.normal ) ), 1.0 ); + fragColor = shadeLine( color ); +} diff --git a/examples/qt3d/tessellation-modes/shaders/quads.tcs b/examples/qt3d/tessellation-modes/shaders/quads.tcs new file mode 100644 index 000000000..367195522 --- /dev/null +++ b/examples/qt3d/tessellation-modes/shaders/quads.tcs @@ -0,0 +1,21 @@ +#version 400 core + +layout( vertices = 4 ) out; + +uniform float inner[2] = float[]( 10.0, 10.0 ); +uniform float outer[4] = float[]( 1.0, 1.0, 1.0, 1.0 ); + +void main() +{ + // Pass along the vertex position unmodified + gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; + + // Set the tessellation levels from the uniforms + gl_TessLevelOuter[0] = outer[0]; + gl_TessLevelOuter[1] = outer[1]; + gl_TessLevelOuter[2] = outer[2]; + gl_TessLevelOuter[3] = outer[3]; + + gl_TessLevelInner[0] = inner[0]; + gl_TessLevelInner[1] = inner[1]; +} diff --git a/examples/qt3d/tessellation-modes/shaders/quads.tes b/examples/qt3d/tessellation-modes/shaders/quads.tes new file mode 100644 index 000000000..a3fd48441 --- /dev/null +++ b/examples/qt3d/tessellation-modes/shaders/quads.tes @@ -0,0 +1,40 @@ +#version 400 core + +layout( quads, fractional_even_spacing, ccw ) in; + +out EyeSpaceVertex { + vec3 position; + vec3 normal; +} te_out; + +uniform mat4 modelView; +uniform mat3 modelViewNormal; +uniform mat4 projectionMatrix; +uniform mat4 mvp; + +void main() +{ + float u = gl_TessCoord.x; + float v = gl_TessCoord.y; + + vec4 p00 = gl_in[0].gl_Position; + vec4 p10 = gl_in[1].gl_Position; + vec4 p11 = gl_in[2].gl_Position; + vec4 p01 = gl_in[3].gl_Position; + + // Linearly interpolate to the vertex position using the + // (u,v) barycentric coords + vec4 pos = p00 * ( 1.0 - u ) * ( 1.0 - v ) + + p10 * u * ( 1.0 - v ) + + p01 * v * ( 1.0 - u ) + + p11 * u * v; + + // Transform to eye space (for lighting calcs) + te_out.position = vec3( modelView * pos ); + + // Assume normal points along z + te_out.normal = normalize( modelViewNormal * vec3( 0.0, 0.0, 1.0 ) ); + + // Transform to clip-space + gl_Position = mvp * pos; +} diff --git a/examples/qt3d/tessellation-modes/shaders/robustwireframe.geom b/examples/qt3d/tessellation-modes/shaders/robustwireframe.geom new file mode 100644 index 000000000..02eaaf398 --- /dev/null +++ b/examples/qt3d/tessellation-modes/shaders/robustwireframe.geom @@ -0,0 +1,131 @@ +#version 400 core + +layout( triangles ) in; +layout( triangle_strip, max_vertices = 3 ) out; + +in EyeSpaceVertex { + vec3 position; + vec3 normal; +} gs_in[]; + +out WireframeVertex { + vec3 position; + vec3 normal; + noperspective vec4 edgeA; + noperspective vec4 edgeB; + flat int configuration; +} gs_out; + +uniform mat4 viewportMatrix; + +const int infoA[] = int[]( 0, 0, 0, 0, 1, 1, 2 ); +const int infoB[] = int[]( 1, 1, 2, 0, 2, 1, 2 ); +const int infoAd[] = int[]( 2, 2, 1, 1, 0, 0, 0 ); +const int infoBd[] = int[]( 2, 2, 1, 2, 0, 2, 1 ); + +vec2 transformToViewport( const in vec4 p ) +{ + return vec2( viewportMatrix * ( p / p.w ) ); +} + +void main() +{ + gs_out.configuration = int(gl_in[0].gl_Position.z < 0) * int(4) + + int(gl_in[1].gl_Position.z < 0) * int(2) + + int(gl_in[2].gl_Position.z < 0); + + // If all vertices are behind us, cull the primitive + if (gs_out.configuration == 7) + return; + + // Transform each vertex into viewport space + vec2 p[3]; + p[0] = transformToViewport( gl_in[0].gl_Position ); + p[1] = transformToViewport( gl_in[1].gl_Position ); + p[2] = transformToViewport( gl_in[2].gl_Position ); + + if (gs_out.configuration == 0) + { + // Common configuration where all vertices are within the viewport + gs_out.edgeA = vec4(0.0); + gs_out.edgeB = vec4(0.0); + + // Calculate lengths of 3 edges of triangle + float a = length( p[1] - p[2] ); + float b = length( p[2] - p[0] ); + float c = length( p[1] - p[0] ); + + // Calculate internal angles using the cosine rule + float alpha = acos( ( b * b + c * c - a * a ) / ( 2.0 * b * c ) ); + float beta = acos( ( a * a + c * c - b * b ) / ( 2.0 * a * c ) ); + + // Calculate the perpendicular distance of each vertex from the opposing edge + float ha = abs( c * sin( beta ) ); + float hb = abs( c * sin( alpha ) ); + float hc = abs( b * sin( alpha ) ); + + // Now add this perpendicular distance as a per-vertex property in addition to + // the position and normal calculated in the vertex shader. + + // Vertex 0 (a) + gs_out.edgeA = vec4( ha, 0.0, 0.0, 0.0 ); + gs_out.normal = gs_in[0].normal; + gs_out.position = gs_in[0].position; + gl_Position = gl_in[0].gl_Position; + EmitVertex(); + + // Vertex 1 (b) + gs_out.edgeA = vec4( 0.0, hb, 0.0, 0.0 ); + gs_out.normal = gs_in[1].normal; + gs_out.position = gs_in[1].position; + gl_Position = gl_in[1].gl_Position; + EmitVertex(); + + // Vertex 2 (c) + gs_out.edgeA = vec4( 0.0, 0.0, hc, 0.0 ); + gs_out.normal = gs_in[2].normal; + gs_out.position = gs_in[2].position; + gl_Position = gl_in[2].gl_Position; + EmitVertex(); + + // Finish the primitive off + EndPrimitive(); + } + else + { + // Viewport projection breaks down for one or two vertices. + // Caclulate what we can here and defer rest to fragment shader. + // Since this is coherent for the entire primitive the conditional + // in the fragment shader is still cheap as all concurrent + // fragment shader invocations will take the same code path. + + // Copy across the viewport-space points for the (up to) two vertices + // in the viewport + gs_out.edgeA.xy = p[infoA[gs_out.configuration]]; + gs_out.edgeB.xy = p[infoB[gs_out.configuration]]; + + // Copy across the viewport-space edge vectors for the (up to) two vertices + // in the viewport + gs_out.edgeA.zw = normalize( gs_out.edgeA.xy - p[ infoAd[gs_out.configuration] ] ); + gs_out.edgeB.zw = normalize( gs_out.edgeB.xy - p[ infoBd[gs_out.configuration] ] ); + + // Pass through the other vertex attributes + gs_out.normal = gs_in[0].normal; + gs_out.position = gs_in[0].position; + gl_Position = gl_in[0].gl_Position; + EmitVertex(); + + gs_out.normal = gs_in[1].normal; + gs_out.position = gs_in[1].position; + gl_Position = gl_in[1].gl_Position; + EmitVertex(); + + gs_out.normal = gs_in[2].normal; + gs_out.position = gs_in[2].position; + gl_Position = gl_in[2].gl_Position; + EmitVertex(); + + // Finish the primitive off + EndPrimitive(); + } +} diff --git a/examples/qt3d/tessellation-modes/shaders/triangles.tcs b/examples/qt3d/tessellation-modes/shaders/triangles.tcs new file mode 100644 index 000000000..c311796fd --- /dev/null +++ b/examples/qt3d/tessellation-modes/shaders/triangles.tcs @@ -0,0 +1,19 @@ +#version 400 core + +layout( vertices = 3 ) out; + +uniform float inner[2] = float[]( 1.0, 1.0 ); +uniform float outer[4] = float[]( 1.0, 1.0, 1.0, 1.0 ); + +void main() +{ + // Pass along the vertex position unmodified + gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position; + + // Set the tessellation levels from the uniforms + gl_TessLevelOuter[0] = outer[0]; + gl_TessLevelOuter[1] = outer[1]; + gl_TessLevelOuter[2] = outer[2]; + + gl_TessLevelInner[0] = inner[0]; +} diff --git a/examples/qt3d/tessellation-modes/shaders/triangles.tes b/examples/qt3d/tessellation-modes/shaders/triangles.tes new file mode 100644 index 000000000..c833e9587 --- /dev/null +++ b/examples/qt3d/tessellation-modes/shaders/triangles.tes @@ -0,0 +1,39 @@ +#version 400 core + +layout( triangles, fractional_even_spacing, ccw ) in; + +out EyeSpaceVertex { + vec3 position; + vec3 normal; +} te_out; + +uniform mat4 modelViewMatrix; +uniform mat3 normalMatrix; +uniform mat4 projectionMatrix; +uniform mat4 mvp; + +void main() +{ + float u = gl_TessCoord.x; + float v = gl_TessCoord.y; + float w = gl_TessCoord.z; + + vec4 p0 = gl_in[0].gl_Position; + vec4 p1 = gl_in[1].gl_Position; + vec4 p2 = gl_in[2].gl_Position; + + // Linearly interpolate to the vertex position using the + // (u,v) barycentric coords + vec4 pos = (u * p0) + + (v * p1) + + (w * p2); + + // Transform to eye space (for lighting calcs) + te_out.position = vec3( modelViewMatrix * pos ); + + // Assume normal points along z + te_out.normal = normalize( normalMatrix * vec3( 0.0, 0.0, 1.0 ) ); + + // Transform to clip-space + gl_Position = mvp * pos; +} |