Commit c1dc9b62 authored by Sylvain Thery's avatar Sylvain Thery

resolve pb of clipping with line Shaders

parent 4107949a
......@@ -4,14 +4,10 @@ PRECISION;
VARYING_FRAG vec3 fragColor;
uniform float alpha;
uniform vec4 planeClip;
VARYING_FRAG vec3 fragClip;
FRAG_OUT_DEF;
void main()
{
if (dot(planeClip,vec4(fragClip,1.0))>0.0)
discard;
FRAG_OUT = vec4(fragColor,alpha);
}
// ShaderBold3DColorLines::geometryShaderText
uniform float lineWidth;
uniform mat4 ModelViewMatrix;
uniform mat4 ProjectionMatrix;
uniform float lineWidth;
uniform vec4 planeClip;
VARYING_IN vec3 vcolor[];
VARYING_OUT vec3 fragColor;
VARYING_IN vec3 posClip[];
VARYING_OUT vec3 fragClip;
void main()
{
vec4 Ap = (ProjectionMatrix*POSITION_IN(0));
vec4 Bp = (ProjectionMatrix*POSITION_IN(1));
/*
float v0 = dot(planeClip,POSITION_IN(0));
float v1 = dot(planeClip,POSITION_IN(1));
vec4 A = vec4(0.0,0.0,0.0,0.0);
vec4 B = vec4(0.0,0.0,0.0,0.0);
if (v0 <= 0.0) //A not clipped
{
A = ModelViewMatrix *POSITION_IN(0);
if (v1 <= 0.0)
B = ModelViewMatrix *POSITION_IN(1);
else
B = ModelViewMatrix * vec4(POSITION_IN(0).xyz * v1/(v1-v0) - POSITION_IN(1).xyz * v0/(v1-v0) ,1.0);
}
else
{
if (v1 <= 0.0) //B not clipped
{
B = ModelViewMatrix *POSITION_IN(1);
A = ModelViewMatrix * vec4(POSITION_IN(1).xyz * v0/(v0-v1) - POSITION_IN(0).xyz * v1/(v0-v1) ,1.0);
}
}
*/
vec4 A = ModelViewMatrix*POSITION_IN(0);
vec4 B = ModelViewMatrix*POSITION_IN(1);
vec4 Ap = ProjectionMatrix*A;
vec4 Bp = ProjectionMatrix*B;
vec3 U3 = normalize((Bp.xyz/Bp.w - Ap.xyz/Ap.w));
vec3 U = vec3(lineWidth*U3);
vec3 V = vec3(lineWidth*normalize(vec2(U[1],-U[0])), 0.0);
fragClip = posClip[0];
fragColor = vcolor[0];
gl_Position = ProjectionMatrix*vec4(POSITION_IN(0).xyz-U, 1.0);
gl_Position = ProjectionMatrix*vec4(A.xyz-U, 1.0);
EmitVertex();
gl_Position = ProjectionMatrix*vec4(POSITION_IN(0).xyz+V, 1.0);
gl_Position = ProjectionMatrix*vec4(A.xyz+V, 1.0);
EmitVertex();
gl_Position = ProjectionMatrix*vec4(POSITION_IN(0).xyz-V, 1.0);
gl_Position = ProjectionMatrix*vec4(A.xyz-V, 1.0);
EmitVertex();
fragClip = posClip[1];
fragColor = vcolor[1];
gl_Position = ProjectionMatrix*vec4(POSITION_IN(1).xyz+V, 1.0);
gl_Position = ProjectionMatrix*vec4(B.xyz+V, 1.0);
EmitVertex();
gl_Position = ProjectionMatrix*vec4(POSITION_IN(1).xyz-V, 1.0);
gl_Position = ProjectionMatrix*vec4(B.xyz-V, 1.0);
EmitVertex();
gl_Position = ProjectionMatrix*vec4(POSITION_IN(1).xyz+U, 1.0);
gl_Position = ProjectionMatrix*vec4(B.xyz+U, 1.0);
EmitVertex();
EndPrimitive();
}
......@@ -2,14 +2,12 @@
ATTRIBUTE vec3 VertexPosition;
ATTRIBUTE vec3 VertexColor;
uniform mat4 ModelViewMatrix;
VARYING_VERT vec3 vcolor;
VARYING_VERT vec3 posClip;
INVARIANT_POS;
void main ()
{
posClip = VertexPosition;
vcolor = VertexColor;
gl_Position = ModelViewMatrix * vec4 (VertexPosition, 1.0);
gl_Position = vec4 (VertexPosition, 1.0);
}
// ShaderBold3DLines::fragmentShaderText
PRECISION;
uniform vec4 lineColor;
uniform vec4 planeClip;
VARYING_FRAG vec3 fragClip;
FRAG_OUT_DEF;
void main()
{
if (dot(planeClip,vec4(fragClip,1.0))>0.0)
discard;
FRAG_OUT = lineColor;
}
// ShaderBold3DLines::geometryShaderText
uniform mat4 ModelViewMatrix;
uniform mat4 ProjectionMatrix;
uniform float lineWidth;
VARYING_IN vec3 posClip[];
VARYING_OUT vec3 fragClip;
uniform vec4 planeClip;
void main()
{
vec4 Ap = (ProjectionMatrix*POSITION_IN(0));
vec4 Bp = (ProjectionMatrix*POSITION_IN(1));
float v0 = dot(planeClip,POSITION_IN(0));
float v1 = dot(planeClip,POSITION_IN(1));
vec4 A = vec4(0.0,0.0,0.0,0.0);
vec4 B = vec4(0.0,0.0,0.0,0.0);
if (v0 <= 0.0) //A not clipped
{
A = ModelViewMatrix *POSITION_IN(0);
if (v1 <= 0.0)
B = ModelViewMatrix *POSITION_IN(1);
else
B = ModelViewMatrix * vec4(POSITION_IN(0).xyz * v1/(v1-v0) - POSITION_IN(1).xyz * v0/(v1-v0) ,1.0);
}
else
{
if (v1 <= 0.0) //B not clipped
{
B = ModelViewMatrix *POSITION_IN(1);
A = ModelViewMatrix * vec4(POSITION_IN(1).xyz * v0/(v0-v1) - POSITION_IN(0).xyz * v1/(v0-v1) ,1.0);
}
}
vec4 Ap = (ProjectionMatrix*A);
vec4 Bp = (ProjectionMatrix*B);
vec3 U3 = normalize((Bp.xyz/Bp.w - Ap.xyz/Ap.w));
vec3 U = vec3(lineWidth*U3);
vec3 V = vec3(lineWidth*normalize(vec2(U[1],-U[0])), 0.0);
fragClip = posClip[0];
gl_Position = ProjectionMatrix*vec4(POSITION_IN(0).xyz-U, 1.0);
gl_Position = ProjectionMatrix*vec4(A.xyz-U, 1.0);
EmitVertex();
gl_Position = ProjectionMatrix*vec4(POSITION_IN(0).xyz+V, 1.0);
gl_Position = ProjectionMatrix*vec4(A.xyz+V, 1.0);
EmitVertex();
gl_Position = ProjectionMatrix*vec4(POSITION_IN(0).xyz-V, 1.0);
gl_Position = ProjectionMatrix*vec4(A.xyz-V, 1.0);
EmitVertex();
fragClip = posClip[1];
gl_Position = ProjectionMatrix*vec4(POSITION_IN(1).xyz+V, 1.0);
gl_Position = ProjectionMatrix*vec4(B.xyz+V, 1.0);
EmitVertex();
gl_Position = ProjectionMatrix*vec4(POSITION_IN(1).xyz-V, 1.0);
gl_Position = ProjectionMatrix*vec4(B.xyz-V, 1.0);
EmitVertex();
gl_Position = ProjectionMatrix*vec4(POSITION_IN(1).xyz+U, 1.0);
gl_Position = ProjectionMatrix*vec4(B.xyz+U, 1.0);
EmitVertex();
EndPrimitive();
}
// ShaderBold3DLines::vertexShaderText
ATTRIBUTE vec3 VertexPosition;
uniform mat4 ModelViewMatrix;
VARYING_VERT vec3 posClip;
INVARIANT_POS;
void main ()
{
posClip = VertexPosition;
gl_Position = ModelViewMatrix * vec4 (VertexPosition, 1.0);
gl_Position = vec4 (VertexPosition, 1.0);
}
......@@ -4,14 +4,10 @@ PRECISION;
VARYING_FRAG vec3 fragColor;
uniform float alpha;
uniform vec4 planeClip;
VARYING_FRAG vec3 fragClip;
FRAG_OUT_DEF;
void main()
{
if (dot(planeClip,vec4(fragClip,1.0))>0.0)
discard;
FRAG_OUT = vec4(fragColor,alpha);
}
// ShaderBoldColorLines::geometryShaderText
uniform vec2 lineWidths;
uniform mat4 ModelViewMatrix;
uniform mat4 ProjectionMatrix;
uniform vec2 lineWidths;
uniform vec4 planeClip;
VARYING_IN vec3 vcolor[];
VARYING_OUT vec3 fragColor;
VARYING_IN vec3 posClip[];
VARYING_OUT vec3 fragClip;
void main()
{
vec4 A = POSITION_IN(0);
vec4 B = POSITION_IN(1);
float v0 = dot(planeClip,POSITION_IN(0));
float v1 = dot(planeClip,POSITION_IN(1));
vec4 A = vec4(0.0,0.0,0.0,0.0);
vec4 B = vec4(0.0,0.0,0.0,0.0);
if (v0 <= 0.0) //A not clipped
{
A = ModelViewMatrix *POSITION_IN(0);
if (v1 <= 0.0)
B = ModelViewMatrix *POSITION_IN(1);
else
B = ModelViewMatrix * vec4(POSITION_IN(0).xyz * v1/(v1-v0) - POSITION_IN(1).xyz * v0/(v1-v0) ,1.0);
}
else
{
if (v1 <= 0.0) //B not clipped
{
B = ModelViewMatrix *POSITION_IN(1);
A = ModelViewMatrix * vec4(POSITION_IN(1).xyz * v0/(v0-v1) - POSITION_IN(0).xyz * v1/(v0-v1) ,1.0);
}
}
float nearZ = 1.0;
if (ProjectionMatrix[2][2] != 1.0)
nearZ = - ProjectionMatrix[3][2] / (ProjectionMatrix[2][2] - 1.0);
if ((A.z < nearZ) || (B.z < nearZ))
{
if (A.z >= nearZ)
......@@ -28,16 +46,15 @@ void main()
A = ProjectionMatrix*A;
B = ProjectionMatrix*B;
A = A/A.w;
B = B/B.w;
vec2 U2 = normalize(vec2(lineWidths[1],lineWidths[0])*(B.xy - A.xy));
vec2 LWCorr =lineWidths * max(abs(U2.x),abs(U2.y));
vec3 U = vec3(LWCorr*U2,0.0);
vec3 V = vec3(LWCorr*vec2(U2[1], -U2[0]), 0.0);
fragClip = posClip[0];
fragColor = vcolor[0];
gl_Position = vec4(A.xyz-U, 1.0);
EmitVertex();
......@@ -46,7 +63,6 @@ void main()
gl_Position = vec4(A.xyz-V, 1.0);
EmitVertex();
fragClip = posClip[1];
fragColor = vcolor[1];
gl_Position = vec4(B.xyz+V, 1.0);
EmitVertex();
......@@ -54,7 +70,7 @@ void main()
EmitVertex();
gl_Position = vec4(B.xyz+U, 1.0);
EmitVertex();
EndPrimitive();
}
}
......@@ -2,14 +2,11 @@
ATTRIBUTE vec3 VertexPosition;
ATTRIBUTE vec3 VertexColor;
uniform mat4 ModelViewMatrix;
VARYING_VERT vec3 vcolor;
VARYING_VERT vec3 posClip;
INVARIANT_POS;
void main ()
{
posClip = VertexPosition;
vcolor = VertexColor;
gl_Position = ModelViewMatrix * vec4 (VertexPosition, 1.0);
gl_Position = vec4(VertexPosition, 1.0);
}
......@@ -2,13 +2,9 @@
PRECISION;
uniform vec4 lineColor;
uniform vec4 planeClip;
VARYING_FRAG vec3 fragClip;
FRAG_OUT_DEF;
void main()
{
if (dot(planeClip,vec4(fragClip,1.0))>0.0)
discard;
FRAG_OUT = lineColor;
}
// ShaderBoldLines::geometryShaderText
uniform mat4 ModelViewMatrix;
uniform mat4 ProjectionMatrix;
uniform vec2 lineWidths;
VARYING_IN vec3 posClip[];
VARYING_OUT vec3 fragClip;
uniform vec4 planeClip;
void main()
{
vec4 A = POSITION_IN(0);
vec4 B = POSITION_IN(1);
float v0 = dot(planeClip,POSITION_IN(0));
float v1 = dot(planeClip,POSITION_IN(1));
vec4 A = vec4(0.0,0.0,0.0,0.0);
vec4 B = vec4(0.0,0.0,0.0,0.0);
if (v0 <= 0.0) //A not clipped
{
A = ModelViewMatrix *POSITION_IN(0);
if (v1 <= 0.0)
B = ModelViewMatrix *POSITION_IN(1);
else
B = ModelViewMatrix * vec4(POSITION_IN(0).xyz * v1/(v1-v0) - POSITION_IN(1).xyz * v0/(v1-v0) ,1.0);
}
else
{
if (v1 <= 0.0) //B not clipped
{
B = ModelViewMatrix *POSITION_IN(1);
A = ModelViewMatrix * vec4(POSITION_IN(1).xyz * v0/(v0-v1) - POSITION_IN(0).xyz * v1/(v0-v1) ,1.0);
}
}
float nearZ = 1.0;
if (ProjectionMatrix[2][2] != 1.0)
......@@ -31,7 +51,6 @@ void main()
vec3 U = vec3(LWCorr*U2,0.0);
vec3 V = vec3(LWCorr*vec2(U2[1], -U2[0]), 0.0);
fragClip = posClip[0];
gl_Position = vec4(A.xyz-U, 1.0);
EmitVertex();
gl_Position = vec4(A.xyz+V, 1.0);
......@@ -39,7 +58,6 @@ void main()
gl_Position = vec4(A.xyz-V, 1.0);
EmitVertex();
fragClip = posClip[1];
gl_Position = vec4(B.xyz+V, 1.0);
EmitVertex();
gl_Position = vec4(B.xyz-V, 1.0);
......
// ShaderBoldLines::vertexShaderText
ATTRIBUTE vec3 VertexPosition;
uniform mat4 ModelViewMatrix;
VARYING_VERT vec3 posClip;
INVARIANT_POS;
void main ()
{
posClip = VertexPosition;
gl_Position = ModelViewMatrix * vec4 (VertexPosition, 1.0);
gl_Position = vec4 (VertexPosition, 1.0);
}
......@@ -4,14 +4,10 @@ PRECISION;
VARYING_FRAG vec3 fragColor;
uniform float alpha;
uniform vec4 planeClip;
VARYING_FRAG vec3 fragClip;
FRAG_OUT_DEF;
void main()
{
if (dot(planeClip,vec4(fragClip,1.0))>0.0)
discard;
FRAG_OUT = vec4(fragColor,alpha);
}
// ShaderColorDarts::geometryShaderText
uniform mat4 ModelViewMatrix;
uniform mat4 ProjectionMatrix;
uniform vec2 lineWidths;
uniform vec4 planeClip;
VARYING_IN vec3 vcolor[];
VARYING_OUT vec3 fragColor;
VARYING_IN vec3 posClip[];
VARYING_OUT vec3 fragClip;
void main()
{
vec4 A = POSITION_IN(0);
vec4 B = POSITION_IN(1);
float v0 = dot(planeClip,POSITION_IN(0));
float v1 = dot(planeClip,POSITION_IN(1));
float scalePoint = 3.0;
vec4 A = vec4(0.0,0.0,0.0,0.0);
vec4 B = vec4(0.0,0.0,0.0,0.0);
if (v0 <= 0.0) //A not clipped
{
A = ModelViewMatrix *POSITION_IN(0);
if (v1 <= 0.0)
B = ModelViewMatrix *POSITION_IN(1);
else
B = ModelViewMatrix * vec4(POSITION_IN(0).xyz * v1/(v1-v0) - POSITION_IN(1).xyz * v0/(v1-v0) ,1.0);
}
else
{
if (v1 <= 0.0) //B not clipped
{
B = ModelViewMatrix *POSITION_IN(1);
A = ModelViewMatrix * vec4(POSITION_IN(1).xyz * v0/(v0-v1) - POSITION_IN(0).xyz * v1/(v0-v1) ,1.0);
scalePoint = 1.0;
}
}
float nearZ = 1.0;
if (ProjectionMatrix[2][2] != 1.0)
......@@ -33,29 +53,27 @@ void main()
B = B/B.w;
vec2 U2 = normalize(vec2(lineWidths[1],lineWidths[0])*(B.xy - A.xy));
vec2 LWCorr =lineWidths * max(abs(U2.x),abs(U2.y));
vec3 U = vec3(LWCorr*U2,0.0) * 3.0;
vec3 V = vec3(LWCorr*vec2(U2[1], -U2[0]), 0.0)*3.0;
vec3 U = vec3(lineWidths*U2,0.0)*scalePoint;
vec3 V = vec3(lineWidths*vec2(U2[1], -U2[0]), 0.0) * scalePoint;
fragClip = posClip[0];
fragColor = vcolor[0];
gl_Position = vec4(A.xyz-U+V, 1.0);
EmitVertex();
gl_Position = vec4(A.xyz-U-V, 1.0);
EmitVertex();
gl_Position = vec4(A.xyz+U+V, 1.0);
EmitVertex();
gl_Position = vec4(A.xyz+U-V, 1.0);
EmitVertex();
V /= 3.0;
V /= scalePoint;
gl_Position = vec4(A.xyz+V, 1.0);
EmitVertex();
gl_Position = vec4(A.xyz-V, 1.0);
EmitVertex();
fragClip = posClip[1];
fragColor = vcolor[1];
gl_Position = vec4(B.xyz+V, 1.0);
EmitVertex();
......
......@@ -2,14 +2,11 @@
ATTRIBUTE vec3 VertexPosition;
ATTRIBUTE vec3 VertexColor;
uniform mat4 ModelViewMatrix;
VARYING_VERT vec3 vcolor;
VARYING_VERT vec3 posClip;
INVARIANT_POS;
void main ()
{
posClip = VertexPosition;
vcolor = VertexColor;
gl_Position = ModelViewMatrix * vec4 (VertexPosition, 1.0);
gl_Position = vec4 (VertexPosition, 1.0);
}
......@@ -2,14 +2,10 @@
PRECISION;
uniform vec4 lineColor;
uniform vec4 planeClip;
VARYING_FRAG vec3 fragClip;
FRAG_OUT_DEF;
void main()
{
if (dot(planeClip,vec4(fragClip,1.0))>0.0)
discard;
FRAG_OUT = lineColor;
}
// ShaderDarts::geometryShaderText
uniform mat4 ModelViewMatrix;
uniform mat4 ProjectionMatrix;
uniform vec2 lineWidths;
VARYING_IN vec3 posClip[];
VARYING_OUT vec3 fragClip;
uniform vec4 planeClip;
void main()
{
vec4 A = POSITION_IN(0);
vec4 B = POSITION_IN(1);
float v0 = dot(planeClip,POSITION_IN(0));
float v1 = dot(planeClip,POSITION_IN(1));
float scalePoint = 3.0;
vec4 A = vec4(0.0,0.0,0.0,0.0);
vec4 B = vec4(0.0,0.0,0.0,0.0);
if (v0 <= 0.0) //A not clipped
{
A = ModelViewMatrix *POSITION_IN(0);
if (v1 <= 0.0)
B = ModelViewMatrix *POSITION_IN(1);
else
B = ModelViewMatrix * vec4(POSITION_IN(0).xyz * v1/(v1-v0) - POSITION_IN(1).xyz * v0/(v1-v0) ,1.0);
}
else
{
if (v1 <= 0.0) //B not clipped
{
B = ModelViewMatrix *POSITION_IN(1);
A = ModelViewMatrix * vec4(POSITION_IN(1).xyz * v0/(v0-v1) - POSITION_IN(0).xyz * v1/(v0-v1) ,1.0);
scalePoint = 1.0;
}
}
float nearZ = 1.0;
if (ProjectionMatrix[2][2] != 1.0)
......@@ -28,12 +49,8 @@ void main()
B = B/B.w;
vec2 U2 = normalize(vec2(lineWidths[1],lineWidths[0])*(B.xy - A.xy));
vec2 LWCorr =lineWidths * max(abs(U2.x),abs(U2.y));
vec3 U = vec3(LWCorr*U2,0.0)*3.0;
vec3 V = vec3(LWCorr*vec2(U2[1], -U2[0]), 0.0)*3.0;
fragClip = posClip[0];
vec3 U = vec3(lineWidths*U2,0.0)*scalePoint;
vec3 V = vec3(lineWidths*vec2(U2[1], -U2[0]), 0.0) * scalePoint;
gl_Position = vec4(A.xyz-U+V, 1.0);
EmitVertex();
......@@ -44,14 +61,13 @@ void main()
gl_Position = vec4(A.xyz+U-V, 1.0);
EmitVertex();
V /= 3.0;
V /= scalePoint;
gl_Position = vec4(A.xyz+V, 1.0);
EmitVertex();
gl_Position = vec4(A.xyz-V, 1.0);
EmitVertex();
fragClip = posClip[1];
gl_Position = vec4(B.xyz+V, 1.0);
EmitVertex();
gl_Position = vec4(B.xyz-V, 1.0);
......
// ShaderDarts::vertexShaderText
ATTRIBUTE vec3 VertexPosition;
uniform mat4 ModelViewMatrix;
VARYING_VERT vec3 posClip;
INVARIANT_POS;
void main ()
{
posClip = VertexPosition;
gl_Position = ModelViewMatrix * vec4 (VertexPosition, 1.0);
gl_Position = vec4 (VertexPosition, 1.0);
}
......@@ -77,10 +77,7 @@ void ShaderBold3DColorLines::sendParams()
bind();
glUniform1f(*m_uniform_lineWidth, m_lineWidth);
glUniform1f (*m_unif_alpha, m_opacity);
if (*m_unif_planeClip > 0)
glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());
glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());
unbind();
}
......@@ -141,9 +138,13 @@ void ShaderBold3DColorLines::setClippingPlane(const Geom::Vec4f& plane)
{
if (*m_unif_planeClip > 0)
{
m_planeClip = plane;
double Nn = sqrt(plane[0] * plane[0] + plane[1] * plane[1] + plane[2] * plane[2]);
if ((fabs(Nn - 1.0) > 0.0000001) && (fabs(Nn)>0.000001))
m_planeClip = plane / Nn;
else
m_planeClip = plane;
bind();
glUniform4fv(*m_unif_planeClip, 1, plane.data());
glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());
unbind();
}
}
......
......@@ -74,10 +74,7 @@ void ShaderBold3DLines::sendParams()
bind();
glUniform1f(*m_uniform_lineWidth, m_lineWidth);
glUniform4fv(*m_uniform_color, 1, m_color.data());
if (*m_unif_planeClip > 0)
glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());
glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());
unbind();
}
......@@ -127,9 +124,13 @@ void ShaderBold3DLines::setClippingPlane(const Geom::Vec4f& plane)
{
if (*m_unif_planeClip > 0)
{
m_planeClip = plane;
double Nn = sqrt(plane[0] * plane[0] + plane[1] * plane[1] + plane[2] * plane[2]);
if ((fabs(Nn - 1.0) > 0.0000001) && (fabs(Nn)>0.000001))
m_planeClip = plane / Nn;
else
m_planeClip = plane;
bind();
glUniform4fv(*m_unif_planeClip, 1, plane.data());
glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());
unbind();
}
}
......
......@@ -77,9 +77,7 @@ void ShaderBoldColorLines::sendParams()
bind();
glUniform2fv(*m_uniform_lineWidth, 1, m_lineWidth.data());
glUniform1f (*m_unif_alpha, m_opacity);
if (*m_unif_planeClip > 0)
glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());
glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());