-
Notifications
You must be signed in to change notification settings - Fork 0
/
SphereGlass.h
executable file
·60 lines (56 loc) · 1.68 KB
/
SphereGlass.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef SPHEREGLASS_H
#define SPHEREGLASS_H
#include <vector>
#include "Object.h"
#include "Ray.h"
#include "MyVector.h"
#include "Sphere.h"
#include "Color.h"
#include <iostream>
class SphereGlass : public Sphere
{
public:
float refractionIndex;
SphereGlass(MyVector cent, double rad, Color col, float refractionIndex);
void Rebound(Ray *ray, MyVector hitPosition);
double distance(MyVector position);
};
SphereGlass::SphereGlass(MyVector cent, double rad, Color color, float refractionIndex) : Sphere(cent, rad, color, 0), refractionIndex(refractionIndex)
{
}
bool randomBool()
{
return (rand() % (2)) == 1;
}
void SphereGlass::Rebound(Ray *ray, MyVector hitPosition)
{
MyVector normalVector = this->NormalVector(hitPosition);
float refractionRatio = refractionIndex;
if (dotProduct(ray->direction, normalVector) > 0) // ray inside the sphere
{
normalVector = -1 * normalVector;
refractionRatio = 1.0 / refractionIndex;
}
double cosTheta = dotProduct(-1 * ray->direction, normalVector);
double sinTheta = sqrt(1.0 - cosTheta * cosTheta);
MyVector outPut;
if (refractionRatio * sinTheta > 1.0 && randomBool()) // reflection
{
MyVector v = dotProduct(-1 * (ray->direction), normalVector) * normalVector;
outPut = ray->direction - 2 * v;
}
else // refraction
{
MyVector v = (-1.0 / refractionRatio) * (ray->direction + cosTheta * normalVector);
MyVector u = -1 * (sqrt(1 - v.moduleSq())) * normalVector;
outPut = (u + v);
}
outPut.normalize();
ray->direction = outPut;
ray->position = hitPosition;
}
double SphereGlass::distance(MyVector position)
{
return 0;
}
#endif