intersection - RayCasting - sphere, plane and OBB -
i have problem understanding how excatly intersection test when testing ray against plane, sphere , obb. have presentation of assignment , need able explain how code have written works. dont understand it, though have searched on internet , read through lot of sites telling how works. still dont understand it, im hoping can give me dummy explaination of easy understand. i'll provide code in question down below, appreciated. wanna understand how stuff works fully.
void giantsphere::test(ray & ray, hitdata & hit) { float b = ray.d.dot(ray.o - this->center); float j = (ray.o - this->center).dot(ray.o - this->center) - pow(this->radius,2); float intersectcalc = b*b - j; if (intersectcalc >= 0) { float t = -b - sqrt(intersectcalc); if (hit.t == -1 || hit.t > t) { hit.t = t; hit.lastshape = this; hit.color = this->c; hit.lastnormal = this->normal(ray.o+ray.d*t); } } } vec giantsphere::normal(vec & point) { vec temp = point - this->center; temp.normalize(); return temp; } giantsphere::giantsphere(vec _center, float _radius, color _color) { this->center = _center; this->radius = _radius; this->c = _color; } void giantplane::test(ray & ray, hitdata & hit) { float d = this->n.dot(this->point); float t = (d - this->n.dot(ray.o)) / this->n.dot(ray.d); if (t > 0.0001f) { if (hit.t == -1 || hit.t > t) { hit.t = t; hit.lastshape = this; hit.color = this->c; hit.lastnormal = this->n; } } } vec giantplane::normal(vec & point) { point = n; return point; } giantplane::giantplane(vec normal, float _d, color color) { this->n = normal; //this->d = _d; this->point = this->n*_d; this->c = color; } void giantobb::test(ray & ray, hitdata & hit) { float tmin = -infinity; float tmax = +infinity; vec p = bcenter - ray.o; // bcenter center of obb. ray.o origin of ray. vec arr[3] = { bu,bv,bw }; // direction/base vectors. (1,0,0), (0,1,0), (0,0,1) (auto& : arr) { float e = i.dot(p); float f = i.dot(ray.d); float q = -e - halfbu; // distance between center , each side. 100. float w = -e + halfbu; if (abs(f) > 1e-20f) { float t1 = ((e + halfbu) / f); float t2 = ((e - halfbu) / f); if (t1 > t2) { std::swap(t1, t2); } if (t1 > tmin) { tmin = t1; } if (t2 < tmax) { tmax = t2; } if (tmin > tmax) { return; } if (tmax < 0) { return; } } else if (q > 0 || w < 0) { return; } } if (tmin > 0) { if (hit.t == -1) { hit.t = tmin; hit.lastshape = this; hit.color = c; } else if (tmin < hit.t) { hit.t = tmin; hit.lastshape = this; hit.color = c; } } else if (tmax <= 0) { if (hit.t == -1) { hit.t = tmax; hit.lastshape = this; hit.color = c; } else if (tmax < hit.t) { hit.t = tmax; hit.lastshape = this; hit.color = c; } } } vec giantobb::normal(vec & point) { vec arr[3] = { this->bu,this->bv,this->bw }; float halfarr[3] = { this->halfbu,this->halfbv,this->halfbw }; vec returnvalue = vec(); (int = 0; < 3; i++) { vec splus = this->bcenter + (arr[i] * halfarr[i]); vec sminus = this->bcenter - (arr[i] * halfarr[i]); if ((point - splus).dot(arr[i]) < 0.0001f && (point - splus).dot(arr[i]) > -0.0001f) { float dot = (point - splus).dot(arr[i]); returnvalue = arr[i]; } else if ((point - sminus).dot(arr[i] * -1) < 0.0001f && (point - sminus).dot(arr[i] * -1) > -0.0001f) { float dot2 = (point - sminus).dot(arr[i]); returnvalue = arr[i] * -1; } } return returnvalue; } giantobb::giantobb(vec b, vec b1, vec b2, vec b3, float hu, float hv, float hw, color _color) { this->bcenter = b; this->bu = b1; this->bv = b2; this->bw = b3; this->c = _color; this->halfbu = hu; this->halfbv = hv; this->halfbw = hw; } giantobb::giantobb(vec b, float hu, float hv, float hw, color _color) { this->bcenter = b; this->halfbu = hu; this->halfbv = hv; this->halfbw = hw; this->c = _color; }
Comments
Post a Comment