virtual bool intersect(Ray const & ray, Hitpoint & hit) const { //we want to find the farthest entrace and closest exit to the box //if the exit is closer than the entrance, there is no hit const size_t vecDim = 3; float entrance = 0.0f; float exit = hit.getParameter(); Vector3 normal = Vector3(0,0,0); for(int i=0; i<vecDim; i++) { float slabA = bbMin[i]; float slabB = bbMax[i]; float invDir = 1.0f / ray.getDirection()[i]; float origin = ray.getOrigin()[i]; float closestHit = (slabA - origin) * invDir; float farthestHit = (slabB - origin) * invDir; if(farthestHit < closestHit) std::swap(closestHit, farthestHit); bool tooClose = farthestHit < entrance; bool tooFar = closestHit > exit; if(tooClose || tooFar) return false; bool foundNewEntrance = closestHit > entrance; entrance = foundNewEntrance ? closestHit : entrance; bool foundNewExit = farthestHit < exit; exit = foundNewExit ? farthestHit : exit; if(foundNewEntrance) { normal = Vector3(0,0,0); normal[i] = ray.getDirection()[i] * -1; normal.normalize(); } } hit.setMaterialId( this->getMaterialId() ); hit.setNormal(normal); hit.setParameter(entrance); return true; }