I have a problem with my raycasting. I have 8 cubes, that define the first level of an octree (Eg. they create bigger cube).
I need to raycast them and "render" each cube. If I have only one cube, the raycasting works fine. If I use 8 cubes, I have a problem. If I move my camera, gaps between cubes start to appear. Cubes are moving apart.
The whole octree bounding box min is (0,0,0), max is (64, 64, 64). Each box has size 32x32x32 and all boxes are set in [0,0,0] and move to their right position with transform matrix tmp
for (int i = 0; i < 8; i++)
{
MyMath::Vector3 centerVec = this->streams[i].center; //left-bottom corner of box
MyMath::Matrix4x4 tmp = MyMath::Matrix4x4::Translation(centerVec);
tmp = tmp * *(camera->GetViewMatrix());
tmp = MyMath::Matrix4x4::Invert(tmp);
CastRays(tmp, camera);
}
CastRays goes through every screen pixel and calculate ray from pixel position and camera coordinates.
foreach pixel
{
Vector3 vec = Map2DTo3D(x, y, width, height, &cameraCoordSystem);
Vector3 dir = vec - cameraPos;
Ray ray;
ray.dir = dir;
ray.origin = cameraPos;
ray.dir = TransformCoordinate(ray.dir, worldInv);
ray.origin = TransformCoordinate(ray.origin, worldInv);
ray.dir = Normalize(ray.dir);
int value = TraverseRay(ray);
.....
}
Traverse ray uses "An Efficient Parametric Algorithm for Octree Traversal" algorithm
int TraverseRay(ray)
{
if (ray.dir.x < 0.0f)
{
ray.origin.x = boxSize - ray.origin.x;
ray.dir.x *= -1;
}
if (ray.dir.y < 0.0f)
{
ray.origin.y = boxSize - ray.origin.y;
ray.dir.y *= -1;
}
if (ray.dir.z < 0.0f)
{
ray.origin.z = boxSize - ray.origin.z;
ray.dir.z *= -1;
}
float tx0 = (0 - ray.origin.x) / ray.dir.x;
float tx1 = (boxSize - ray.origin.x) / ray.dir.x;
float ty0 = (0 - ray.origin.y) / ray.dir.y;
float ty1 = (boxSize - ray.origin.y) / ray.dir.y;
float tz0 = 0 - ray.origin.z) / ray.dir.z;
float tz1 = (boxSize - ray.origin.z) / ray.dir.z;
//infinity test - not included... have no effect on bug
if (Max(tx0, ty0, tz0) < Min(tx1, ty1, tz1))
{
return 1;
//there is procsubtree().. but again.. no effect on error... error occurs
//even with return 1
}
else
{
return -1;
}
}
The space between boxes is changing its size with respect to the camera. There should be no space between boxes: it should be one single square (if I rotate this, there are other 4 boxes from octree behind those)
Image: