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