diff --git a/viewer/glow-extras/viewer/renderables/PointRenderable.cc b/viewer/glow-extras/viewer/renderables/PointRenderable.cc index d010ef04d0ead380a6494fdd34e1464f010eb13b..c9b3e286225c971138b03b181bab8525dedad3a9 100644 --- a/viewer/glow-extras/viewer/renderables/PointRenderable.cc +++ b/viewer/glow-extras/viewer/renderables/PointRenderable.cc @@ -317,12 +317,13 @@ void PointRenderable::init() sb.addPassthrough("vec2", "QuadPos"); sb.addPassthrough("vec3", "Normal"); + sb.addPassthrough("vec3", "PointPositionWorldSpace"); + for (auto const& attr : getAttributes()) attr->buildShader(sb); if (mRenderMode == RenderMode::Sphere) { - sb.addPassthrough("vec3", "SphereCenter"); sb.addPassthrough("float", "SphereRadius"); sb.addPassthrough("vec3", "fragPosWS"); @@ -331,7 +332,7 @@ void PointRenderable::init() // world space size spheres sb.addVertexShaderCode(R"( vOut.SphereRadius = aPointSize; - vOut.SphereCenter = vec3(uModel * vec4(aPosition, 1.0)); + vOut.PointPositionWorldSpace = vec3(uModel * vec4(aPosition, 1.0)); vec4 vpos = uView * uModel * vec4(aPosition, 1.0); vec4 vposCS = uProj * vpos; @@ -368,7 +369,7 @@ void PointRenderable::init() float l = 2 * distance(pos, uCamPos) * uTanFov2; float s = l * aPointSize / uScreenHeight; vOut.SphereRadius = s; - vOut.SphereCenter = pos; + vOut.PointPositionWorldSpace = pos; vec4 vpos = uView * uModel * vec4(aPosition, 1.0); vec3 fwd = normalize(vpos.xyz); @@ -392,7 +393,8 @@ void PointRenderable::init() // camera facing screen space size sb.addPassthrough("vec3", "fragPosWS"); // Needed for clipping sb.addVertexShaderCode(R"( - vec4 spos = uProj * uView * uModel * vec4(aPosition, 1.0); + vec4 wspos = uModel * vec4(aPosition, 1.0); + vec4 spos = uProj * uView * wspos; spos /= spos.w; vec2 qp = aQuadPos * 2 - 1; @@ -404,6 +406,7 @@ void PointRenderable::init() vOut.Normal = vec3(0,0,0); vOut.fragPosWS = vec3(uModel * vec4(spos.xyz, 1.0)); + vOut.PointPositionWorldSpace = wspos.xyz; gl_Position = uProj * uView * uModel * vec4(spos.xyz, 1.0); )"); } @@ -412,7 +415,8 @@ void PointRenderable::init() // camera facing world space size sb.addPassthrough("vec3", "fragPosWS"); // Needed for clipping sb.addVertexShaderCode(R"( - vec4 vpos = uView * uModel * vec4(aPosition, 1.0); + vec4 wspos = uModel * vec4(aPosition, 1.0); + vec4 vpos = uView * wspos; vec2 qp = aQuadPos * 2 - 1; vpos.xy += qp * aPointSize; @@ -420,6 +424,7 @@ void PointRenderable::init() vOut.Normal = vec3(0,0,0); vOut.fragPosWS = vec3(uModel * vec4(vpos.xyz, 1.0)); + vOut.PointPositionWorldSpace = wspos.xyz; gl_Position = uProj * uView * uModel * vec4(vpos.xyz, 1.0); )"); } @@ -436,36 +441,37 @@ void PointRenderable::init() vec2 qp = aQuadPos * 2 - 1; vOut.fragPosWS = pos + aPointSize * (left * qp.x + up * qp.y); + vOut.PointPositionWorldSpace = pos; vOut.Normal = dir; gl_Position = uProj * uView * vec4(vOut.fragPosWS, 1.0); )"); } + // per-primitive clipping sb.addGeometryShaderDecl(R"( - layout(triangles) in; - layout(triangle_strip, max_vertices = 3) out; - )"); + layout(triangles) in; + layout(triangle_strip, max_vertices = 3) out; + )"); sb.addGeometryShaderCode(R"( - vec3 p0 = vIn[0].fragPosWS; - vec3 p1 = vIn[1].fragPosWS; - vec3 p2 = vIn[2].fragPosWS; - - if (dot(uPrimitiveClipPlane.xyz, p0) > uPrimitiveClipPlane.w || dot(uPrimitiveClipPlane.xyz, p1) > uPrimitiveClipPlane.w || dot(uPrimitiveClipPlane.xyz, p2) > uPrimitiveClipPlane.w) - return; - - PASSTHROUGH(0); - PASSTHROUGH(1); - PASSTHROUGH(2); - - gl_Position = gl_in[0].gl_Position; - EmitVertex(); - gl_Position = gl_in[1].gl_Position; - EmitVertex(); - gl_Position = gl_in[2].gl_Position; - EmitVertex(); - - EndPrimitive(); - )"); + vec3 center = vIn[0].PointPositionWorldSpace; + + if (dot(uPrimitiveClipPlane.xyz, center) > uPrimitiveClipPlane.w) + return; + + PASSTHROUGH(0); + gl_Position = gl_in[0].gl_Position; + EmitVertex(); + PASSTHROUGH(1); + gl_Position = gl_in[1].gl_Position; + EmitVertex(); + PASSTHROUGH(2); + gl_Position = gl_in[2].gl_Position; + EmitVertex(); + + EndPrimitive(); + )"); + + }; @@ -482,9 +488,7 @@ void PointRenderable::init() sbForward.addUniform("uint", "uSeed"); sbForward.addPassthrough("float", "VertexID"); - sbForward.addVertexShaderCode(R"( - vOut.VertexID = float(gl_VertexID); - )"); + sbForward.addVertexShaderCode(R"(vOut.VertexID = float(gl_VertexID);)"); // colored mesh if (aColor) @@ -525,12 +529,12 @@ void PointRenderable::init() vec4 rayTargetVS = (uView * vec4(vIn.fragPosWS, 1)); vec3 rayDirVS = normalize(rayTargetVS.xyz - rayOriginVS.xyz); - vec3 sphereCenterVS = (uView * vec4(vSphereCenter, 1)).xyz; + vec3 PointPositionWorldSpaceVS = (uView * vec4(vPointPositionWorldSpace, 1)).xyz; - vec3 closestP = rayOriginVS.xyz + rayDirVS * dot(rayDirVS, sphereCenterVS - rayOriginVS.xyz); - float sphereDis2 = distance2(closestP, sphereCenterVS); + vec3 closestP = rayOriginVS.xyz + rayDirVS * dot(rayDirVS, PointPositionWorldSpaceVS - rayOriginVS.xyz); + float sphereDis2 = distance2(closestP, PointPositionWorldSpaceVS); - if (dot(uFragmentClipPlane.xyz, vSphereCenter) > uFragmentClipPlane.w) + if (dot(uFragmentClipPlane.xyz, vPointPositionWorldSpace) > uFragmentClipPlane.w) discard; if (sphereDis2 > vSphereRadius * vSphereRadius) @@ -543,7 +547,7 @@ void PointRenderable::init() float depthNDC = spherePosCS.z; vec3 spherePosWS = (uInvView * vec4(spherePosVS, 1)).xyz; - vec3 sphereWS_N = normalize(spherePosWS - vSphereCenter); + vec3 sphereWS_N = normalize(spherePosWS - vPointPositionWorldSpace); if(uIsReverseZEnabled) gl_FragDepth = (depthNDC - gl_DepthRange.near) / gl_DepthRange.diff; @@ -629,8 +633,8 @@ void PointRenderable::init() vec3 rayOrigin = uCamPos; vec3 rayDir = normalize(vIn.fragPosWS - uCamPos); - vec3 closestP = rayOrigin + rayDir * dot(rayDir, vSphereCenter - rayOrigin); - float sphereDis2 = distance2(closestP, vSphereCenter); + vec3 closestP = rayOrigin + rayDir * dot(rayDir, vPointPositionWorldSpace - rayOrigin); + float sphereDis2 = distance2(closestP, vPointPositionWorldSpace); if (sphereDis2 > vSphereRadius * vSphereRadius) discard; @@ -679,10 +683,10 @@ void PointRenderable::init() vec3 rayOrigin = uCamPos; vec3 rayDir = normalize(vIn.fragPosWS - uCamPos); - vec3 closestP = rayOrigin + rayDir * dot(rayDir, vSphereCenter - rayOrigin); - float sphereDis2 = distance2(closestP, vSphereCenter); + vec3 closestP = rayOrigin + rayDir * dot(rayDir, vPointPositionWorldSpace - rayOrigin); + float sphereDis2 = distance2(closestP, vPointPositionWorldSpace); - if (dot(uFragmentClipPlane.xyz, vSphereCenter) > uFragmentClipPlane.w) + if (dot(uFragmentClipPlane.xyz, vPointPositionWorldSpace) > uFragmentClipPlane.w) discard; if (sphereDis2 > vSphereRadius * vSphereRadius)