Ray-Box (AABB) is slower than without

at the moment im trying my best to write my own raytracer (and its quite fun actually). So the last days I tried implementing a bounding box algorithm to it. But im getting a much slower framerate with the bounding boxes turned on :(. I think it has something to do with checking the box with every ray but I dont know how I could change that. Here is my code:

the Intersection algorythm

bool Intersect(Ray r, float3 lb, float3 rt) {     float3 dir_inv = 1 / r.direction;          double t1 = (lb[0] - r.origin[0]) * dir_inv[0];     double t2 = (rt[0] - r.origin[0]) * dir_inv[0];      double tmin = min(t1, t2);     double tmax = max(t1, t2);      for (int i = 1; i < 3; ++i)     {         t1 = (lb[i] - r.origin[i]) * dir_inv[i];         t2 = (rt[i] - r.origin[i]) * dir_inv[i];          tmin = max(tmin, min(t1, t2));         tmax = min(tmax, max(t1, t2));     }      return tmax > max(tmin, 0.0); } 

My trace function:

RayHit Trace(Ray ray) {     RayHit bestHit = CreateRayHit();     uint count, stride, i;      // Trace ground plane     IntersectGroundPlane(ray, bestHit);      // Trace spheres     _Spheres.GetDimensions(count, stride);     for (i = 0; i < count; i++)     {         if (Intersect(ray, _Spheres[i].position - (_Spheres[i].radius), _Spheres[i].position + (_Spheres[i].radius)))             IntersectSphere(ray, bestHit, _Spheres[i]);     }          // Trace mesh objects     _MeshObjects.GetDimensions(count, stride);     for (i = 0; i < count; i++)     {         //if (Intersect(ray, float3(0.0f, 0.0f, 0.0f), float3(10.0f, 10.0f, 10.0f)))             //IntersectMeshObject(ray, bestHit, _MeshObjects[i]);     }      return bestHit; } 

thanks in advance

3D line segment AABB collision, with hit normal?

I’m embarrassed I can’t find this, but I’m wanting to detect intersection with a 3D line segment (not an infinite ray) with a 3D AABB, the AABB being defined as two Vec3f’s which represent the Min and Max extents. So the AABB can be arbitrarily-sized. I also need the surface normal of the AABB, if there was a hit.

From looking at similar algorithms I at least know it seems good to calculate the inverse direction of the line beforehand, at least, if you’re needing to check against multiple AABBs per frame.

I have

struct AABB {     VEC3F min;     VEC3F max; };  // a and b representing start/end points of the line segment // returns true if intersects, also fills out "normal" if true bool LineIntersectsAABB(const VEC3F& a, const VEC3F& b,     const VEC3F& inv_dir, const AABB& aabb,     VEC3F* normal); 

The implementations I’ve found either do not find the hit normal, and/or they’re intended for boxes/cubes where the three dimensions of the box are always equal length, which doesn’t work for me. Implementations seem to vary greatly, which is confusing for me (who is trying to understand it), and, considering that I need the hit normal, I’d imagine that that may rule out certain implementations.