-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRayTracer.h
125 lines (125 loc) · 6 KB
/
RayTracer.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <vector>
#include <cmath>
#include <iostream>
#include <fstream>
using namespace std;
#pragma once
class RayTracer {
// Class to hold color info about a material
struct ColorPack {
vector<unsigned char> ambientConstant;
vector<unsigned char> diffuseConstant;
vector<unsigned char> specularConstant;
float phongExponent = 1.0f;
bool reflect = false;
ColorPack() = default;
ColorPack(vector<unsigned char> ambientConstant, vector<unsigned char>
diffuseConstant, vector<unsigned char> specularConstant, float phongExponent, bool
reflect = false);
};
// Abstract object class
struct Object {
ColorPack color;
Object(ColorPack color);
virtual ~Object() = default;
// All objects check for intersection and can tell you their normal vector
virtual float intersection(vector<float> p, vector<float> d) = 0;
virtual vector<float> getNormal(const vector<float>& x) = 0;
};
// Sphere Class
struct Sphere : public Object {
vector<float> center;
float radius;
Sphere(vector<float> center, float radius, ColorPack color);
float intersection(vector<float> p, vector<float> d) override;
vector<float> getNormal(const vector<float>& x) override;
};
// Light Object Class
struct LightObj : public Sphere {
LightObj(vector<float> center, float radius, ColorPack color);
};
// Plane Class
struct Plane : public Object {
vector<float> a;
vector<float> b;
vector<float> c;
Plane(vector<float> point1, vector<float> point2, vector<float> point3,
ColorPack color);
float intersection(vector<float> p, vector<float> d) override;
vector<float> getNormal(const vector<float>& x) override;
};
// Triangle Class
struct Triangle : public Object {
vector<float> a;
vector<float> b;
vector<float> c;
Triangle(vector<float> point1, vector<float> point2, vector<float> point3,
ColorPack color);
float intersection(vector<float> p, vector<float> d) override;
vector<float> getNormal(const vector<float> &x) override;
};
// Light Class
struct Light {
vector<float> location;
float intensity;
Light(vector<float> location, float intensity);
};
vector<float> findColor(vector<float> p, vector<float> d, int num, int limit);
public:
// Saves an image to a ppm file (chose ppm because it's easy to write to)
void takePicture(string fileName, unsigned char* image);
// Functions for Vector Math
static vector<float> addVec(const vector<float>& a, const vector<float>&
b);
static vector<float> scalarVec(float scalar, const vector<float>& a);
static float dotVec(const vector<float>& a, const vector<float>& b);
static vector<float> crossVec(const vector<float>& a, const vector<float>&
b);
static float vecMag(const vector<float>& a);
static vector<float> normalizeVec(const vector<float>& a);
static vector<float> multiplyVec(const vector<float>& a, const
vector<float>& b);
static vector<float> scaleColor(const vector<unsigned char>& a);
// For transforming vectors
static vector<float> transformVector(vector<float> vec, float pitch, float
yaw, float roll);
unsigned char* produceImage(vector<float> camera, vector<float> lookAtVec,
vector<float> upVec);
~RayTracer();
unsigned char* image = nullptr;
// Run Time Settings
float distanceAwayConstant = 0.1f;
float ambientIntensity = 0.5f;
bool orthogonal = true;
bool lightVisualization = false;
int imgSizeX = 256;
int imgSizeY = 256;
float projectionDistance = 144.0f;
// Object List
vector<Object*> objects = {new Sphere({125, 50, -150}, 50, {{255, 128,
255}, {255, 128, 255}, {255, 255, 255}, 16}),
new Triangle({175, 5, -75}, {200, 55, -100},
{225, 5, -75}, {{128, 255, 255}, {128, 255, 255}, {255, 255, 255}, 16}),
new Triangle({225, 5, -75}, {200, 55, -100},
{225, 5, -125}, {{128, 255, 255}, {128, 255, 255}, {255, 255, 255}, 16}),
new Triangle({225, 5, -125}, {200, 55, -100},
{175, 5, -125}, {{128, 255, 255}, {128, 255, 255}, {255, 255, 255}, 16}),
new Triangle({175, 5, -125}, {200, 55, -100},
{175, 5, -75}, {{128, 255, 255}, {128, 255, 255}, {255, 255, 255}, 16}),
new Triangle({225, 5, -125}, {175, 5, -125},
{175, 5, -75}, {{128, 255, 255}, {128, 255, 255}, {255, 255, 255}, 16}),
new Triangle({225, 5, -75}, {225, 5, -125},
{175, 5, -75}, {{128, 255, 255}, {128, 255, 255}, {255, 255, 255}, 16}),
new Sphere({128, 41, -62}, 20, {{255, 128, 128},
{255, 128, 128}, {255, 255, 255}, 16}),
new Plane({0, 0, 0}, {1, 0, 0}, {0, 0, 1},
{{255, 255, 0}, {255, 255, 0}, {255, 255, 255}, 16, true}),
new LightObj({100, 100, -50}, 5, {{255, 255, 0},
{255, 255, 0}, {255, 255, 255}, 16}),
new LightObj({231, 63, -127}, 5, {{255, 255, 0},
{255, 255, 0}, {255, 255, 255}, 16})};
vector<unsigned char> backgroundColor = {0, 0, 0};
// Lights
vector<Light*> lights = {new Light({100, 100, -50}, 0.2f),
new Light({231, 63, -127}, 0.2f)};
};