vbsdf r -fresnel Constant 1.0
vsetlocation r 0.5 0.65 0.1
+vclipplane create pln
+vclipplane change pln equation 0 0 -1 0.1
+vclipplane set pln object s
+
vrenderparams -ray -gi -rayDepth 8
}
//! Pass clip planes to the associated graphic driver structure
- void SetClipPlanes (const Graphic3d_SequenceOfHClipPlane& thePlanes) { myClipPlanes = thePlanes; }
+ virtual void SetClipPlanes (const Graphic3d_SequenceOfHClipPlane& thePlanes) { myClipPlanes = thePlanes; }
//! @return bounding box of this presentation
const Graphic3d_BndBox4f& BoundingBox() const
return Standard_True;
}
+// =======================================================================
+// function : OpenGl_RaytraceClipPlanes
+// purpose : Creates new set of clipping planes
+// =======================================================================
+OpenGl_RaytraceClipPlanes::OpenGl_RaytraceClipPlanes()
+{
+ for (Standard_Integer aPlaneIdx = 0; aPlaneIdx < MAX_PLANE_NUMBER; ++aPlaneIdx)
+ {
+ myClipPlanes[aPlaneIdx * 2] = BVH_Vec4f (-1.f, -1.f, -1.f, -1.f);
+ }
+}
+
namespace OpenGl_Raytrace
{
// =======================================================================
};
+//! Set of clipping planes specific for OpenGL primitive array.
+class OpenGl_RaytraceClipPlanes
+{
+public:
+
+ //! Maximum number of clipping planes used in ray-tracing for each
+ //! OpenGL primitive array. This is not implementation restriction,
+ //! but it is reasonable to limit max number of planes in order to
+ //! simplify GLSL data representation.
+ static const Standard_Integer MAX_PLANE_NUMBER = 8;
+
+ //! State of clipping plane.
+ enum ClipPlaneState {
+
+ CLIP_PLANE_OFF = -1, //!< plane is deactivated
+ CLIP_PLANE_VIEW = 0, //!< plane is in view space
+ CLIP_PLANE_WORLD = 1 //!< plane is in world space
+ };
+
+ //! Wrapper for clipping plane configuration.
+ class ClipPlane
+ {
+ public:
+
+ //! Creates new clipping plane wrapper.
+ ClipPlane (BVH_Vec4f& theSettings,
+ BVH_Vec4f& theEquation) : mySettings (theSettings),
+ myEquation (theEquation) {}
+
+ //! Sets 4D equation vector for clipping plane.
+ void SetEquation (const BVH_Vec4d& theEquation, const ClipPlaneState theState = CLIP_PLANE_WORLD)
+ {
+ for (Standard_Integer anIndex = 0; anIndex < 4; ++anIndex)
+ {
+ myEquation[anIndex] = static_cast<Standard_ShortReal> (theEquation[anIndex]);
+ }
+
+ SetState (theState);
+ }
+
+ //! Returns state of clipping plane.
+ ClipPlaneState State()
+ {
+ return static_cast<ClipPlaneState> (mySettings.x());
+ }
+
+ //! Sets state of clipping plane.
+ void SetState (const ClipPlaneState theState)
+ {
+ mySettings.x() = static_cast<Standard_ShortReal> (theState);
+ }
+
+ private:
+
+ //! Settings of clipping plane.
+ BVH_Vec4f& mySettings;
+
+ //! 4D equation vector of clipping plane.
+ BVH_Vec4f& myEquation;
+ };
+
+public:
+
+ //! Creates new set of clipping planes.
+ OpenGl_RaytraceClipPlanes();
+
+ //! Returns clipping plane for the given index.
+ ClipPlane operator[] (const Standard_Integer theIndex)
+ {
+ return ClipPlane (myClipPlanes[theIndex * 2 + 0],
+ myClipPlanes[theIndex * 2 + 1]);
+ }
+
+ //! Returns packed (serialized) representation of clipping planes set.
+ const Standard_ShortReal* Packed()
+ {
+ return reinterpret_cast<Standard_ShortReal*> (this);
+ }
+
+private:
+
+ //! Serialized clipping planes storage.
+ BVH_Vec4f myClipPlanes[MAX_PLANE_NUMBER * 2];
+
+};
+
+//! Stores transform properties of ray-tracing object.
+class OpenGl_RaytraceTransform : public BVH_Transform<Standard_ShortReal, 4>
+{
+public:
+
+ //! Value of invalid clipping plane set.
+ static const Standard_Integer NO_CLIPPING = -1;
+
+public:
+
+ //! Creates new identity transformation.
+ OpenGl_RaytraceTransform() : BVH_Transform<Standard_ShortReal, 4>()
+ {
+ myClipSetID = NO_CLIPPING; // no clipping by default
+ }
+
+ //! Creates new transformation with specified matrix.
+ OpenGl_RaytraceTransform (const BVH_Mat4f& theTransform) : BVH_Transform<Standard_ShortReal, 4> (theTransform)
+ {
+ myClipSetID = NO_CLIPPING; // no clipping by default
+ }
+
+ //! Returns ID of associated set of clipping planes.
+ Standard_Integer ClipSetID() const
+ {
+ return myClipSetID;
+ }
+
+ //! Sets ID of associated set of clipping planes.
+ void SetClipSetID (const Standard_Integer theClipSetID)
+ {
+ myClipSetID = theClipSetID;
+ }
+
+protected:
+
+ //! ID of associated set of clipping planes.
+ Standard_Integer myClipSetID;
+
+};
+
//! Stores geometry of ray-tracing scene.
class OpenGl_RaytraceGeometry : public BVH_Geometry<Standard_ShortReal, 3>
{
std::vector<OpenGl_RaytraceMaterial,
NCollection_StdAllocator<OpenGl_RaytraceMaterial> > Materials;
+ //! Array of sets of clipping plane parameters.
+ std::vector<OpenGl_RaytraceClipPlanes,
+ NCollection_StdAllocator<OpenGl_RaytraceClipPlanes> > ClipPlanes;
+
//! Global ambient from all light sources.
BVH_Vec4f Ambient;
myAspectText->SetAspect (theAspect);
}
+// =======================================================================
+// function : SetClipPlanes
+// purpose :
+// =======================================================================
+void OpenGl_Structure::SetClipPlanes (const Graphic3d_SequenceOfHClipPlane &thePlanes)
+{
+ Graphic3d_CStructure::SetClipPlanes (thePlanes);
+
+ if (IsRaytracable())
+ {
+ ++myModificationState;
+ }
+}
+
// =======================================================================
// function : clearHighlightBox
// purpose :
Standard_EXPORT void Clear (const Handle(OpenGl_Context)& theGlCtx);
+ //! Pass clip planes to the associated graphic driver structure
+ void SetClipPlanes (const Graphic3d_SequenceOfHClipPlane& thePlanes);
+
//! Renders groups of structure without applying any attributes (i.e. transform, material etc).
//! @param theWorkspace current workspace
//! @param theHasClosed flag will be set to TRUE if structure contains at least one group of closed primitives
OpenGl_RT_RaytraceMaterialTexture = 9,
OpenGl_RT_RaytraceLightSrcTexture = 10,
+ OpenGl_RT_RaytraceClippingTexture = 11,
- OpenGl_RT_FsaaInputTexture = 11,
- OpenGl_RT_PrevAccumTexture = 12,
- OpenGl_RT_DepthTexture = 13,
+ OpenGl_RT_FsaaInputTexture = 12,
+ OpenGl_RT_PrevAccumTexture = 13,
+ OpenGl_RT_DepthTexture = 14,
- OpenGl_RT_OpenGlColorTexture = 14,
- OpenGl_RT_OpenGlDepthTexture = 15
+ OpenGl_RT_OpenGlColorTexture = 15,
+ OpenGl_RT_OpenGlDepthTexture = 16
};
//! Tool class for management of shader sources.
Handle(OpenGl_TextureBufferArb) myRaytraceMaterialTexture;
//! Texture buffer of light source properties.
Handle(OpenGl_TextureBufferArb) myRaytraceLightSrcTexture;
+ //! Texture buffer of clipping planes parameters.
+ Handle(OpenGl_TextureBufferArb) myRaytraceClippingTexture;
//! 1st framebuffer (FBO) to perform adaptive FSAA.
Handle(OpenGl_FrameBuffer) myRaytraceFBO1[2];
return Standard_True;
}
- // Get structure material
+ // Get material of the structure
OpenGl_RaytraceMaterial aStructMaterial;
if (theStructure->AspectFace() != NULL)
const Graphic3d_Mat4* theTransform,
const Handle(OpenGl_Context)& theGlContext)
{
+ // ID of clipping plane set
+ Standard_Integer aClipSetID = -1;
+
+ // Collect clipping planes of structure scope
+ if (!theStructure->ClipPlanes().IsEmpty())
+ {
+ NCollection_Handle<Graphic3d_SequenceOfHClipPlane> aUserPlanes;
+
+ for (Graphic3d_SequenceOfHClipPlane::Iterator aClipIter (theStructure->ClipPlanes()); aClipIter.More(); aClipIter.Next())
+ {
+ const Handle(Graphic3d_ClipPlane)& aClipPlane = aClipIter.Value();
+
+ if (aClipPlane->IsOn())
+ {
+ if (aUserPlanes.IsNull())
+ {
+ aUserPlanes = new Graphic3d_SequenceOfHClipPlane();
+ }
+
+ aUserPlanes->Append (aClipPlane);
+ }
+ }
+
+ if (!aUserPlanes.IsNull())
+ {
+ myRaytraceGeometry.ClipPlanes.push_back (OpenGl_RaytraceClipPlanes());
+
+ for (Standard_Integer aPlaneIdx = 0; aPlaneIdx < aUserPlanes->Size(); ++aPlaneIdx)
+ {
+ if (aPlaneIdx < OpenGl_RaytraceClipPlanes::MAX_PLANE_NUMBER)
+ {
+ myRaytraceGeometry.ClipPlanes.back()[aPlaneIdx].SetEquation (aUserPlanes->operator ()(aPlaneIdx + 1)->GetEquation());
+ }
+ }
+
+ aClipSetID = static_cast<Standard_Integer> (myRaytraceGeometry.ClipPlanes.size() - 1);
+ }
+ }
+
for (OpenGl_Structure::GroupIterator aGroupIter (theStructure->DrawGroups()); aGroupIter.More(); aGroupIter.Next())
{
// Get group material
{
OpenGl_TriangleSet* aSet = aSetIter->second;
- BVH_Transform<Standard_ShortReal, 4>* aTransform = new BVH_Transform<Standard_ShortReal, 4>();
+ OpenGl_RaytraceTransform* aTransform = new OpenGl_RaytraceTransform;
if (theTransform != NULL)
{
aTransform->SetTransform (*theTransform);
}
+ if (aClipSetID != OpenGl_RaytraceTransform::NO_CLIPPING)
+ {
+ aTransform->SetClipSetID (aClipSetID);
+ }
+
aSet->SetProperties (aTransform);
if (aSet->MaterialIndex() != OpenGl_TriangleSet::INVALID_MATERIAL && aSet->MaterialIndex() != aMatID)
}
else
{
- NCollection_Handle<BVH_Object<Standard_ShortReal, 3> > aSet =
- addRaytracePrimitiveArray (aPrimArray, aMatID, 0);
+ NCollection_Handle<BVH_Object<Standard_ShortReal, 3> > aSet = addRaytracePrimitiveArray (aPrimArray, aMatID, 0);
if (!aSet.IsNull())
{
- BVH_Transform<Standard_ShortReal, 4>* aTransform = new BVH_Transform<Standard_ShortReal, 4>;
+ OpenGl_RaytraceTransform* aTransform = new OpenGl_RaytraceTransform;
if (theTransform != NULL)
{
aTransform->SetTransform (*theTransform);
}
+ if (aClipSetID != OpenGl_RaytraceTransform::NO_CLIPPING)
+ {
+ aTransform->SetClipSetID (aClipSetID);
+ }
+
aSet->SetProperties (aTransform);
myRaytraceGeometry.Objects().Append (aSet);
"uRaytraceMaterialTexture", OpenGl_RT_RaytraceMaterialTexture);
aShaderProgram->SetSampler (theGlContext,
"uRaytraceLightSrcTexture", OpenGl_RT_RaytraceLightSrcTexture);
+ aShaderProgram->SetSampler (theGlContext,
+ "uRaytraceClippingTexture", OpenGl_RT_RaytraceClippingTexture);
aShaderProgram->SetSampler (theGlContext,
"uOpenGlColorTexture", OpenGl_RT_OpenGlColorTexture);
nullifyResource (theGlContext, myRaytraceLightSrcTexture);
nullifyResource (theGlContext, myRaytraceMaterialTexture);
+ nullifyResource (theGlContext, myRaytraceClippingTexture);
myRaytraceGeometry.ReleaseResources (theGlContext);
}
/////////////////////////////////////////////////////////////////////////////
- // Create OpenGL BVH buffers
+ // Create BVH buffers
if (mySceneNodeInfoTexture.IsNull()) // create scene BVH buffers
{
}
}
+ /////////////////////////////////////////////////////////////////////////////
+ // Create material buffer
+
if (myRaytraceMaterialTexture.IsNull()) // create material buffer
{
myRaytraceMaterialTexture = new OpenGl_TextureBufferArb;
if (!myRaytraceMaterialTexture->Create (theGlContext))
{
#ifdef RAY_TRACE_PRINT_INFO
- std::cout << "Error: Failed to create buffers for material data" << std::endl;
+ std::cout << "Error: Failed to create buffer for material data" << std::endl;
+#endif
+ return Standard_False;
+ }
+ }
+
+ /////////////////////////////////////////////////////////////////////////////
+ // Create clip planes buffer
+
+ if (myRaytraceClippingTexture.IsNull()) // create clipping planes buffer
+ {
+ myRaytraceClippingTexture = new OpenGl_TextureBufferArb;
+
+ if (!myRaytraceClippingTexture->Create (theGlContext))
+ {
+#ifdef RAY_TRACE_PRINT_INFO
+ std::cout << "Error: Failed to create buffer for clip plane data" << std::endl;
#endif
return Standard_False;
}
OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->());
- const BVH_Transform<Standard_ShortReal, 4>* aTransform =
- dynamic_cast<const BVH_Transform<Standard_ShortReal, 4>* > (aTriangleSet->Properties().operator->());
+ const OpenGl_RaytraceTransform* aTransform =
+ dynamic_cast<const OpenGl_RaytraceTransform*> (aTriangleSet->Properties().operator->());
Standard_ASSERT_RETURN (aTransform != NULL,
"OpenGl_TriangleSet does not contain transform", Standard_False);
aNodeTransforms[anElemIndex] = aTransform->Inversed();
+
+ // Note: write clip plane ID in last matrix component, because
+ // the last matrix row is not used for transformation purposes
+ aNodeTransforms[anElemIndex].SetValue (3, 3, aTransform->ClipSetID());
}
aResult &= mySceneTransformTexture->Init (theGlContext, 4,
}
}
+ /////////////////////////////////////////////////////////////////////////////
+ // Write clip planes buffer
+
+ if (myRaytraceGeometry.ClipPlanes.size() != 0)
+ {
+ aResult &= myRaytraceClippingTexture->Init (theGlContext, 4,
+ GLsizei (myRaytraceGeometry.ClipPlanes.size() * 16), myRaytraceGeometry.ClipPlanes.front().Packed());
+
+ if (!aResult)
+ {
+#ifdef RAY_TRACE_PRINT_INFO
+ std::cout << "Error: Failed to upload clipping planes buffer" << std::endl;
+#endif
+ return Standard_False;
+ }
+ }
+
myIsRaytraceDataValid = myRaytraceGeometry.Objects().Size() != 0;
#ifdef RAY_TRACE_PRINT_INFO
mySceneTransformTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneTransformTexture);
myRaytraceMaterialTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceMaterialTexture);
myRaytraceLightSrcTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceLightSrcTexture);
+ myRaytraceClippingTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceClippingTexture);
if (!myOpenGlFBO.IsNull())
{
mySceneTransformTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneTransformTexture);
myRaytraceMaterialTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceMaterialTexture);
myRaytraceLightSrcTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceLightSrcTexture);
+ myRaytraceClippingTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceClippingTexture);
if (!myOpenGlFBO.IsNull())
{
uniform samplerBuffer uRaytraceMaterialTexture;
//! Texture buffer of light source properties.
uniform samplerBuffer uRaytraceLightSrcTexture;
+//! Texture buffer of clipping planes properties.
+uniform samplerBuffer uRaytraceClippingTexture;
+
//! Environment map texture.
uniform sampler2D uEnvironmentMapTexture;
ivec4 SubData;
};
+#define EMPTY_ROOT ivec4(0)
+
#define TRS_OFFSET(treelet) treelet.SubData.x
#define BVH_OFFSET(treelet) treelet.SubData.y
#define VRT_OFFSET(treelet) treelet.SubData.z
#define TRG_OFFSET(treelet) treelet.SubData.w
-#define EMPTY_ROOT ivec4(0)
+#define MAX_CLIP_PLANES 1
+
+#define IS_PLANE_ACTIVE(params) (params.x != -1.F)
+#define IS_PLANE_HIDDEN(params) (params.x == -1.F)
+
+#define PLANE_SETTINGS(trsf, index) (16 * int(trsf.w) + 2 * index + 0)
+#define PLANE_EQUATION(trsf, index) (16 * int(trsf.w) + 2 * index + 1)
// =======================================================================
// function : SceneNearestHit
SSubTree aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);
+ float aClipMax = MAXFLOAT;
+ float aClipMin = -MAXFLOAT;
+
for (bool toContinue = true; toContinue;)
{
ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);
float aTime = IntersectTriangle (aSubTree.TrsfRay,
aPoint0, aPoint1, aPoint2, aParams, aNormal);
- if (aTime < theHit.Time)
+ if (aTime > aClipMin && aTime < aClipMax && aTime < theHit.Time)
{
aTriIndex = aTriangle;
}
else if (aData.x > 0) // switch node
{
- aSubTree.SubData = ivec4 (4 * aData.x - 4, aData.yzw); // store BVH sub-root
+ aClipMax = MAXFLOAT;
+ aClipMin = -MAXFLOAT;
+
+ aSubTree.SubData = ivec4 (aData.x * 4 - 4, aData.yzw); // store BVH sub-root
vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 0);
vec4 aInvTransf1 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 1);
vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 2);
vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 3);
+ for (int aPlaneIdx = 0; aInvTransf3.w >= 0.0 && aPlaneIdx < MAX_CLIP_PLANES; ++aPlaneIdx)
+ {
+ vec4 aSettings = texelFetch (uRaytraceClippingTexture, PLANE_SETTINGS (aInvTransf3, aPlaneIdx));
+ vec4 aEquation = texelFetch (uRaytraceClippingTexture, PLANE_EQUATION (aInvTransf3, aPlaneIdx));
+
+ float aNdotD = -dot (aEquation.xyz, theRay.Direct);
+
+ if (IS_PLANE_ACTIVE (aSettings))
+ {
+ float aPlaneTime = (dot (aEquation.xyz, theRay.Origin) + aEquation.w) / aNdotD;
+
+ if (aNdotD < 0.0)
+ aClipMin = max (aClipMin, aPlaneTime);
+ else
+ aClipMax = min (aClipMax, aPlaneTime);
+ }
+ }
+
aSubTree.TrsfRay.Direct = MatrixColMultiplyDir (theRay.Direct,
aInvTransf0,
aInvTransf1,
SSubTree aSubTree = SSubTree (theRay, theInverse, EMPTY_ROOT);
+ float aClipMax = MAXFLOAT;
+ float aClipMin = -MAXFLOAT;
+
for (bool toContinue = true; toContinue;)
{
ivec4 aData = texelFetch (uSceneNodeInfoTexture, aNode);
aPoint0, aPoint1, aPoint2, aParams, aNormal);
#ifdef TRANSPARENT_SHADOWS
- if (aTime < theDistance)
+ if (aTime > aClipMin && aTime < aClipMax && aTime < theDistance)
{
aFactor *= 1.f - texelFetch (uRaytraceMaterialTexture, MATERIAL_TRAN (aTriangle.w)).x;
}
#else
- if (aTime < theDistance)
+ if (aTime > aClipMin && aTime < aClipMax && aTime < theDistance)
{
aFactor = 0.f;
}
}
else if (aData.x > 0) // switch node
{
+ aClipMax = MAXFLOAT;
+ aClipMin = -MAXFLOAT;
+
aSubTree.SubData = ivec4 (4 * aData.x - 4, aData.yzw); // store BVH sub-root
vec4 aInvTransf0 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 0);
vec4 aInvTransf2 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 2);
vec4 aInvTransf3 = texelFetch (uSceneTransformTexture, TRS_OFFSET (aSubTree) + 3);
+ for (int aPlaneIdx = 0; aInvTransf3.w >= 0.0 && aPlaneIdx < MAX_CLIP_PLANES; ++aPlaneIdx)
+ {
+ vec4 aSettings = texelFetch (uRaytraceClippingTexture, PLANE_SETTINGS (aInvTransf3, aPlaneIdx));
+ vec4 aEquation = texelFetch (uRaytraceClippingTexture, PLANE_EQUATION (aInvTransf3, aPlaneIdx));
+
+ float aNdotD = -dot (aEquation.xyz, theRay.Direct);
+
+ if (IS_PLANE_ACTIVE (aSettings))
+ {
+ float aPlaneTime = (dot (aEquation.xyz, theRay.Origin) + aEquation.w) / aNdotD;
+
+ if (aNdotD < 0.0)
+ aClipMin = max (aClipMin, aPlaneTime);
+ else
+ aClipMax = min (aClipMax, aPlaneTime);
+ }
+ }
+
aSubTree.TrsfRay.Direct = MatrixColMultiplyDir (theRay.Direct,
aInvTransf0,
aInvTransf1,