Hi!
I'm currently looking for a ray->box intersection function (in C#) for my raytracer.
This is my code so far, ported from elias_t's code from the code archives. it works but it doesn't return picked coordinates and normals. can anyone help me on with this?
thanks.
XYZ - Box coordinates
Width, Height, Depth - Box size
I'm currently looking for a ray->box intersection function (in C#) for my raytracer.
This is my code so far, ported from elias_t's code from the code archives. it works but it doesn't return picked coordinates and normals. can anyone help me on with this?
thanks.
XYZ - Box coordinates
Width, Height, Depth - Box size
public bool Pick(double ray_x, double ray_y, double ray_z, double ray_nx, double ray_ny, double ray_nz, bool infinite) { double bminx = X - Width / 2; double bminy = Y - Height / 2; double bminz = Z - Depth / 2; double bmaxx = X + Width / 2; double bmaxy = Y + Height / 2; double bmaxz = Z + Depth / 2; double txmin = 0, txmax = 0, tymin = 0, tymax = 0, tzmin = 0, tzmax = 0; if (ray_nx >= 0) { txmin = (bminx - ray_x) / ray_nx; txmax = (bmaxx - ray_x) / ray_nx; } else { txmin = (bmaxx - ray_x) / ray_nx; txmax = (bminx - ray_x) / ray_nx; } if (ray_ny >= 0) { tymin = (bminy - ray_y) / ray_ny; tymax = (bmaxy - ray_y) / ray_ny; } else { tymin = (bmaxy - ray_y) / ray_ny; tymax = (bminy - ray_y) / ray_ny; } if (txmin > tymax || tymin > txmax) return false; if (tymin > txmin) txmin = tymin; if (tymax < txmax) txmax = tymax; if (ray_nz >= 0) { tzmin = (bminz - ray_z) / ray_nz; tzmax = (bmaxz - ray_z) / ray_nz; } else { tzmin = (bmaxz - ray_z) / ray_nz; tzmax = (bminz - ray_z) / ray_nz; } if (txmin > tzmax || tzmin > txmax) return false; Picker.PickedDis = Math.Sqrt((Picker.PickedX - ray_x) * (Picker.PickedX - ray_x) + (Picker.PickedY - ray_y) * (Picker.PickedY - ray_y) + (Picker.PickedZ - ray_z) * (Picker.PickedZ - ray_z)); return true; }