diff --git a/devices/gl/src/VisGLCameraOrthographicObject.cpp b/devices/gl/src/VisGLCameraOrthographicObject.cpp index 4fe71d221..1db05bf8b 100644 --- a/devices/gl/src/VisGLCameraOrthographicObject.cpp +++ b/devices/gl/src/VisGLCameraOrthographicObject.cpp @@ -34,6 +34,7 @@ #include "math_util.h" #include +#include #include #include @@ -59,6 +60,31 @@ void Object::commit() current.height.get(ANARI_FLOAT32, &height); normalize3(direction); + + // clip distances, same rule as VisRTX : unset means no clipping, invalid + // values are ignored + nearClip = 0.0f; + farClip = FLT_MAX; + float nearValue = nearClip; + float farValue = farClip; + const bool hasNear = current.near.get(ANARI_FLOAT32, &nearValue); + const bool hasFar = current.far.get(ANARI_FLOAT32, &farValue); + if (hasNear || hasFar) { + if ((hasNear && !(nearValue >= 0.0f)) || !(nearValue < farValue)) { + anariReportStatus(device, + handle, + ANARI_CAMERA, + ANARI_SEVERITY_WARNING, + ANARI_STATUS_INVALID_ARGUMENT, + "camera 'near' (%f) and 'far' (%f) must satisfy 0 <= near < far; " + "ignoring both", + nearValue, + farValue); + } else { + nearClip = nearValue; + farClip = farValue; + } + } } void Object::updateAt(size_t index, float *bounds) const @@ -66,8 +92,16 @@ void Object::updateAt(size_t index, float *bounds) const float range[2]; projectBoundingBox(range, bounds, position, direction); float depth = range[1]-range[0]; - float far = range[1] + 0.001f*depth; - float near = fast_maxf(0.0f, range[0] - 0.001f*depth); + // clip range enclosing the world, narrowed to the camera near and far when + // they are set + float far = fast_minf(farClip, range[1] + 0.001f * depth); + float near = fast_maxf(nearClip, range[0] - 0.001f * depth); + if (!(far > near) && (nearClip > 0.0f || farClip < FLT_MAX)) { + // the world is outside the camera clip range, nothing is visible : use that + // range itself + near = nearClip; + far = farClip < FLT_MAX ? farClip : near + 1.0f; + } float width = height * aspect; diff --git a/devices/gl/src/VisGLCameraOrthographicObject.h b/devices/gl/src/VisGLCameraOrthographicObject.h index 4df6a2c41..84c4ce1e9 100644 --- a/devices/gl/src/VisGLCameraOrthographicObject.h +++ b/devices/gl/src/VisGLCameraOrthographicObject.h @@ -47,6 +47,9 @@ class Object float region[4]; float aspect; float height; + // "near" and "far" parameters, 0 and FLT_MAX when not set + float nearClip; + float farClip; void calculateMatrices(float near, float far); diff --git a/devices/gl/src/VisGLCameraPerspectiveObject.cpp b/devices/gl/src/VisGLCameraPerspectiveObject.cpp index 2a5a1eb53..3f8fc5e6c 100644 --- a/devices/gl/src/VisGLCameraPerspectiveObject.cpp +++ b/devices/gl/src/VisGLCameraPerspectiveObject.cpp @@ -34,6 +34,7 @@ #include "math_util.h" #include +#include #include #include @@ -59,14 +60,46 @@ void Object::commit() current.aspect.get(ANARI_FLOAT32, &aspect); normalize3(direction); + + // clip distances, same rule as VisRTX : unset means no clipping, invalid values are ignored + nearClip = 0.0f; + farClip = FLT_MAX; + float nearValue = nearClip; + float farValue = farClip; + const bool hasNear = current.near.get(ANARI_FLOAT32, &nearValue); + const bool hasFar = current.far.get(ANARI_FLOAT32, &farValue); + if (hasNear || hasFar) { + if ((hasNear && !(nearValue > 0.0f)) || !(nearValue < farValue)) { + anariReportStatus(device, + handle, + ANARI_CAMERA, + ANARI_SEVERITY_WARNING, + ANARI_STATUS_INVALID_ARGUMENT, + "camera 'near' (%f) and 'far' (%f) must satisfy 0 < near < far; ignoring both", + nearValue, + farValue); + } else { + nearClip = nearValue; + farClip = farValue; + } + } } void Object::updateAt(size_t index, float *bounds) const { float range[2]; projectBoundingBox(range, bounds, position, direction); - float far = range[1] * 1.0001f; - float near = fast_maxf(far * 1.0e-3, range[0] * 0.9999f); + // clip range enclosing the world, narrowed to the camera near and far when they are set. + // Without a near distance, the near plane is pushed to far/1000 to keep the depth precision : + // it clips the foreground when the world is much larger than the view (a globe seen from low altitude). + float far = fast_minf(farClip, range[1] * 1.0001f); + float near = nearClip > 0.0f ? fast_maxf(nearClip, range[0] * 0.9999f) + : fast_maxf(far * 1.0e-3f, range[0] * 0.9999f); + if (!(far > near) && (nearClip > 0.0f || farClip < FLT_MAX)) { + // the world is outside the camera clip range, nothing is visible : use that range itself + near = nearClip > 0.0f ? nearClip : farClip * 1.0e-3f; + far = farClip < FLT_MAX ? farClip : 2.0f * near; + } float height = near * tanf(fovy * 0.5f); float width = height * aspect; diff --git a/devices/gl/src/VisGLCameraPerspectiveObject.h b/devices/gl/src/VisGLCameraPerspectiveObject.h index 594097f16..ec922621c 100644 --- a/devices/gl/src/VisGLCameraPerspectiveObject.h +++ b/devices/gl/src/VisGLCameraPerspectiveObject.h @@ -48,6 +48,9 @@ class Object float region[4]; float fovy; float aspect; + // "near" and "far" parameters, 0 and FLT_MAX when not set + float nearClip; + float farClip; void calculateMatrices(float near, float far);