source: GTP/trunk/App/Demos/Illum/pathmap/Sphere.cpp @ 2197

Revision 2197, 1.6 KB checked in by szirmay, 18 years ago (diff)
Line 
1#include "dxstdafx.h"
2#include "Sphere.hpp"
3
4bool Sphere::intersectBackSide (const Ray& ray, float& depth, float rayMin, float rayMax)
5{
6        lastTestedRayId = ray.id;
7        lastTestedRayResult.isIntersect = false;
8        Vector diff = (ray.origin - centre);
9        float b = 2.0f * (ray.dir * diff);
10        float c = diff * diff - radius * radius;
11
12        float discriminant = b * b - 4.0f * c;
13        if(discriminant < 0.0f)
14                return false;
15        else
16        {
17                depth = (-b + sqrtf(discriminant)) * 0.5f;
18                lastTestedRayResult.isIntersect = true;
19                lastTestedRayResult.depth               = depth;
20                lastTestedRayResult.material    = this->material;
21                if(ray.isShadowRay) return true;
22                lastTestedRayResult.point = ray.origin + ray.dir * depth;
23                lastTestedRayResult.normal.setDifference(lastTestedRayResult.point, centre);
24                lastTestedRayResult.normal *= 1.0f / radius;
25                lastTestedRayResult.object = this;
26                return true;
27        }
28}
29
30bool Sphere::intersect (const Ray& ray, float& depth, float rayMin, float rayMax)
31{
32        lastTestedRayId = ray.id;
33        lastTestedRayResult.isIntersect = false;
34        Vector diff = (ray.origin - centre);
35        float b = 2.0f * (ray.dir * diff);
36        float c = diff * diff - radius * radius;
37
38        float discriminant = b * b - 4.0f * c;
39        if(discriminant < 0.0f)
40                return false;
41        else
42        {
43                depth = (-b - sqrtf(discriminant)) * 0.5f;
44                lastTestedRayResult.isIntersect = true;
45                lastTestedRayResult.depth               = depth;
46                lastTestedRayResult.material    = this->material;
47                if(ray.isShadowRay) return true;
48                lastTestedRayResult.point = ray.origin + ray.dir * depth;
49                lastTestedRayResult.normal.setDifference(lastTestedRayResult.point, centre);
50                lastTestedRayResult.normal *= 1.0f / radius;
51                lastTestedRayResult.object = this;
52                return true;
53        }
54}
Note: See TracBrowser for help on using the repository browser.