OpenGL: new built-in shaders for mesh edges

This commit is contained in:
2016-10-25 03:19:10 -04:00
parent c5072941c3
commit 94e14a2c43
5 changed files with 178 additions and 1 deletions

View File

@@ -0,0 +1,64 @@
// Draw "fancy" wireframe, displaying front-facing, back-facing and
// silhouette lines differently.
// Mike Erwin, April 2015
uniform bool drawFront = true;
uniform bool drawBack = true;
uniform bool drawSilhouette = true;
uniform vec4 frontColor;
uniform vec4 backColor;
uniform vec4 silhouetteColor;
uniform vec4 eye; // direction we are looking
uniform mat4 ModelViewProjectionMatrix;
#if __VERSION__ == 120
attribute vec3 pos;
// normals of faces this edge joins (object coords)
attribute vec3 N1;
attribute vec3 N2;
flat varying vec4 finalColor;
#else
in vec3 pos;
// normals of faces this edge joins (object coords)
in vec3 N1;
in vec3 N2;
flat out vec4 finalColor;
#endif
// TODO: in float angle; // [-pi .. +pi], + peak, 0 flat, - valley
// to discard an entire line, set both endpoints to nowhere
// and it won't produce any fragments
const vec4 nowhere = vec4(vec3(0.0), 1.0);
void main()
{
bool face_1_front = dot(N1, eye) > 0.0;
bool face_2_front = dot(N2, eye) > 0.0;
vec4 position = ModelViewProjectionMatrix * vec4(pos, 1.0);
if (face_1_front && face_2_front) {
// front-facing edge
gl_Position = drawFront ? position : nowhere;
finalColor = frontColor;
}
else if (face_1_front || face_2_front) {
// exactly one face is front-facing, silhouette edge
gl_Position = drawSilhouette ? position : nowhere;
finalColor = silhouetteColor;
}
else {
// back-facing edge
gl_Position = drawBack ? position : nowhere;
finalColor = backColor;
}
}

View File

@@ -0,0 +1,78 @@
// Draw "fancy" wireframe, displaying front-facing, back-facing and
// silhouette lines differently.
// Mike Erwin, April 2015
// After working with this shader a while, convinced we should make
// separate shaders for perpective & ortho. (Oct 2016)
// This shader is an imperfect stepping stone until all platforms are
// ready for geometry shaders.
// Due to perspective, the line segment's endpoints might disagree on
// whether the adjacent faces are front facing. Need to use a geometry
// shader or pass in an extra position attribute (the other endpoint)
// to do this properly.
uniform bool drawFront = true;
uniform bool drawBack = true;
uniform bool drawSilhouette = true;
uniform vec4 frontColor;
uniform vec4 backColor;
uniform vec4 silhouetteColor;
uniform mat4 ModelViewMatrix;
uniform mat4 ModelViewProjectionMatrix;
uniform mat3 NormalMatrix;
#if __VERSION__ == 120
attribute vec3 pos;
// normals of faces this edge joins (object coords)
attribute vec3 N1;
attribute vec3 N2;
flat varying vec4 finalColor;
#else
in vec3 pos;
// normals of faces this edge joins (object coords)
in vec3 N1;
in vec3 N2;
flat out vec4 finalColor;
#endif
// TODO: in float angle; // [-pi .. +pi], + peak, 0 flat, - valley
// to discard an entire line, set its color to invisible
// (must have GL_BLEND enabled, or discard in fragment shader)
const vec4 invisible = vec4(0.0);
bool front(vec3 N)
{
vec4 xformed = ModelViewMatrix * vec4(pos, 1.0);
return dot(NormalMatrix * N, normalize(-xformed.xyz)) > 0.0;
}
void main()
{
bool face_1_front = front(N1);
bool face_2_front = front(N2);
gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
if (face_1_front && face_2_front) {
// front-facing edge
finalColor = drawFront ? frontColor : invisible;
}
else if (face_1_front || face_2_front) {
// exactly one face is front-facing, silhouette edge
finalColor = drawSilhouette ? silhouetteColor : invisible;
}
else {
// back-facing edge
finalColor = drawBack ? backColor : invisible;
}
}