diff --git a/library/PolyVoxCore/include/PolyVoxCore/Raycast.h b/library/PolyVoxCore/include/PolyVoxCore/Raycast.h
index 860f4efb..544c078d 100644
--- a/library/PolyVoxCore/include/PolyVoxCore/Raycast.h
+++ b/library/PolyVoxCore/include/PolyVoxCore/Raycast.h
@@ -59,6 +59,14 @@ namespace PolyVox
/// of the RaycastResult structure and the intersectionFound flag is set to true, otherwise
/// the intersectionFound flag is set to false.
///
+ /// Important Note: These has been confusion in the past with people not realising
+ /// that the length of the direction vector is important. Most graphics API can provide
+ /// a camera position and view direction for picking purposes, but the view direction is
+ /// usually normalised (i.e. of length one). If you use this view direction directly you
+ /// will only iterate over a single voxel and won't find what you are looking for. Instead
+ /// you must scale the direction vector so that it's length represents the maximum distance
+ /// over which you want the ray to be cast.
+ ///
/// The following code snippet shows how the class is used:
/// \code
/// Vector3DFloat start(rayOrigin.x(), rayOrigin.y(), rayOrigin.z());
@@ -88,12 +96,12 @@ namespace PolyVox
{
public:
///Constructor
- Raycast(VolumeType* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirection, RaycastResult& result);
+ Raycast(VolumeType* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirectionAndLength, RaycastResult& result);
///Sets the start position for the ray.
void setStart(const Vector3DFloat& v3dStart);
///Set the direction for the ray.
- void setDirection(const Vector3DFloat& v3dDirection);
+ void setDirection(const Vector3DFloat& v3dDirectionAndLength);
///Performs the raycast.
void execute();
@@ -107,7 +115,7 @@ namespace PolyVox
typename VolumeType::Sampler m_sampVolume;
Vector3DFloat m_v3dStart;
- Vector3DFloat m_v3dDirection;
+ Vector3DFloat m_v3dDirectionAndLength;
float m_fMaxDistance;
};
}
diff --git a/library/PolyVoxCore/include/PolyVoxCore/Raycast.inl b/library/PolyVoxCore/include/PolyVoxCore/Raycast.inl
index 38e84e46..3fb32a2c 100644
--- a/library/PolyVoxCore/include/PolyVoxCore/Raycast.inl
+++ b/library/PolyVoxCore/include/PolyVoxCore/Raycast.inl
@@ -27,17 +27,17 @@ namespace PolyVox
/// Builds a Raycast object.
/// \param volData A pointer to the volume through which the ray will be cast.
/// \param v3dStart The starting position of the ray.
- /// \param v3dDirection The direction of the ray. The length of this vector also
+ /// \param v3dDirectionAndLength The direction of the ray. The length of this vector also
/// represents the length of the ray.
/// \param result An instance of RaycastResult in which the result will be stored.
////////////////////////////////////////////////////////////////////////////////
template< template class VolumeType, typename VoxelType>
- Raycast::Raycast(VolumeType* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirection, RaycastResult& result)
+ Raycast::Raycast(VolumeType* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirectionAndLength, RaycastResult& result)
:m_result(result)
,m_volData(volData)
,m_sampVolume(volData)
,m_v3dStart(v3dStart)
- ,m_v3dDirection(v3dDirection)
+ ,m_v3dDirectionAndLength(v3dDirectionAndLength)
{
}
@@ -51,13 +51,14 @@ namespace PolyVox
}
////////////////////////////////////////////////////////////////////////////////
- /// \param v3dDirection The direction of the ray. The length of this vector also
+ /// \param v3dDirectionAndLength The direction of the ray. The length of this vector also
/// represents the length of the ray.
////////////////////////////////////////////////////////////////////////////////
template< template class VolumeType, typename VoxelType>
- void Raycast::setDirection(const Vector3DFloat& v3dDirection)
+ void Raycast::setDirection(const Vector3DFloat& v3dDirectionAndLength)
{
- m_v3dDirection = v3dDirection;
+ //FIXME: We should add a warning when the ray direction is of length one, as this seems to be a common mistake.
+ m_v3dDirectionAndLength = v3dDirectionAndLength;
}
////////////////////////////////////////////////////////////////////////////////
@@ -72,7 +73,7 @@ namespace PolyVox
Vector3DFloat v3dStart = m_v3dStart + Vector3DFloat(0.5f, 0.5f, 0.5f);
//Compute the end point
- Vector3DFloat v3dEnd = v3dStart + m_v3dDirection;
+ Vector3DFloat v3dEnd = v3dStart + m_v3dDirectionAndLength;
//Do the raycast
doRaycast(v3dStart.getX(), v3dStart.getY(), v3dStart.getZ(), v3dEnd.getX(), v3dEnd.getY(), v3dEnd.getZ());
diff --git a/library/PolyVoxCore/include/PolyVoxCore/RaycastWithCallback.h b/library/PolyVoxCore/include/PolyVoxCore/RaycastWithCallback.h
index bb53812f..972d0a4f 100644
--- a/library/PolyVoxCore/include/PolyVoxCore/RaycastWithCallback.h
+++ b/library/PolyVoxCore/include/PolyVoxCore/RaycastWithCallback.h
@@ -33,12 +33,12 @@ namespace PolyVox
{
public:
///Constructor
- RaycastWithCallback(VolumeType* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirection, polyvox_function funcCallback);
+ RaycastWithCallback(VolumeType* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirectionAndLength, polyvox_function funcCallback);
///Sets the start position for the ray.
void setStart(const Vector3DFloat& v3dStart);
///Set the direction for the ray.
- void setDirection(const Vector3DFloat& v3dDirection);
+ void setDirection(const Vector3DFloat& v3dDirectionAndLength);
///Performs the raycast.
void execute();
@@ -52,7 +52,7 @@ namespace PolyVox
typename VolumeType::Sampler m_sampVolume;
Vector3DFloat m_v3dStart;
- Vector3DFloat m_v3dDirection;
+ Vector3DFloat m_v3dDirectionAndLength;
float m_fMaxDistance;
};
}
diff --git a/library/PolyVoxCore/include/PolyVoxCore/RaycastWithCallback.inl b/library/PolyVoxCore/include/PolyVoxCore/RaycastWithCallback.inl
index 2cd269e3..c3da4620 100644
--- a/library/PolyVoxCore/include/PolyVoxCore/RaycastWithCallback.inl
+++ b/library/PolyVoxCore/include/PolyVoxCore/RaycastWithCallback.inl
@@ -24,11 +24,11 @@ freely, subject to the following restrictions:
namespace PolyVox
{
template< template class VolumeType, typename VoxelType>
- RaycastWithCallback::RaycastWithCallback(VolumeType* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirection, polyvox_function funcCallback)
+ RaycastWithCallback::RaycastWithCallback(VolumeType* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirectionAndLength, polyvox_function funcCallback)
:m_volData(volData)
,m_sampVolume(volData)
,m_v3dStart(v3dStart)
- ,m_v3dDirection(v3dDirection)
+ ,m_v3dDirectionAndLength(v3dDirectionAndLength)
,m_funcCallback(funcCallback)
{
//Check the user provided a callback, because it
@@ -43,9 +43,9 @@ namespace PolyVox
}
template< template class VolumeType, typename VoxelType>
- void RaycastWithCallback::setDirection(const Vector3DFloat& v3dDirection)
+ void RaycastWithCallback::setDirection(const Vector3DFloat& v3dDirectionAndLength)
{
- m_v3dDirection = v3dDirection;
+ m_v3dDirectionAndLength = v3dDirectionAndLength;
}
template< template class VolumeType, typename VoxelType>
@@ -57,7 +57,7 @@ namespace PolyVox
Vector3DFloat v3dStart = m_v3dStart + Vector3DFloat(0.5f, 0.5f, 0.5f);
//Compute the end point
- Vector3DFloat v3dEnd = v3dStart + m_v3dDirection;
+ Vector3DFloat v3dEnd = v3dStart + m_v3dDirectionAndLength;
//Do the raycast
doRaycast(v3dStart.getX(), v3dStart.getY(), v3dStart.getZ(), v3dEnd.getX(), v3dEnd.getY(), v3dEnd.getZ());