TinyRaytracer 0.1
A simple C++ raytracer
Loading...
Searching...
No Matches
object.hpp
Go to the documentation of this file.
1
11#ifndef OBJECT_H
12#define OBJECT_H
13#include "struct.hpp"
14
15#include <algorithm>
16#include <memory>
17
18
24public:
27 RGB trans;
28 double ior = 1.458;
29 double roughness = 0;
44};
45
51public:
52 bool isHit = false;
53 double distance;
61};
62
66class Object{
67public:
69 virtual ObjectInfo checkObject(Ray& ray) = 0;
70 virtual void setProperties(RGB,RGB,double,double){}
71 virtual AABB getBox() = 0;
72};
73
79class Sphere : public Object{
80public:
82 double r;
84 Image texture;
88 Sphere(): r(0.0) {
89 c = point3(0.0,0.0,0.0);
90 mat.color = {1.0f,1.0f,1.0f};
91 }
95 Sphere(double x,double y,double z,double r,RGB rgb): r(r) {
96 c = point3(x,y,z);
97 mat.color = rgb;
98 auto rvec = vec3(r,r,r);
99 bbox = AABB(c - rvec,c + rvec);
100 }
104 Sphere(double x,double y,double z,double r,std::string text): r(r) {
105 c = point3(x,y,z);
106 //texture = load_image(text.c_str());
107 texture = Image((text).c_str());
108 if (texture.empty()) texture = Image(("input/"+text).c_str());
109 auto rvec = vec3(r,r,r);
110 bbox = AABB(c - rvec,c + rvec);
111 }
112 ObjectInfo checkObject(Ray& ray) override;
117 AABB getBox() override {return bbox;}
118 std::tuple<double,double> sphereUV(const point3& point) const;
119 RGB getColor(const point3& point);
123 void setProperties(RGB shine,RGB tran,double ior,double roughness) override {
124 mat.shininess = shine;mat.trans = tran;mat.ior = ior;mat.roughness = roughness;
125 }
126};
127
131class Plane{
132public:
133 double a,b,c,d;
137 Plane(): a(0.0),b(0.0),c(0.0),d(0.0),nor(vec3()),point(point3()) {mat.color = {1.0f,1.0f,1.0f};}
138 Plane(double a,double b,double c,double d,RGB rgb): a(a),b(b),c(c),d(d) {
139 nor = vec3(a,b,c).normalize();
140 point = (-d * vec3(a,b,c)) / (pow(a,2) + pow(b,2) + pow(c,2));
141 mat.color = rgb;
142 }
143 void setProperties(RGB shine,RGB tran,double ior,double roughness){
144 mat.shininess = shine;mat.trans = tran;mat.ior = ior;mat.roughness = roughness;
145 }
146};
147
151class Vertex{
152public:
154 Texcoord tex; //<! The texture coordinate, if provided.
155 Vertex(): p(point3()) {}
156 Vertex(double x,double y,double z): p(point3(x,y,z)) {}
157 Vertex(double x,double y,double z,Texcoord tex): p(point3(x,y,z)),tex(tex){}
158};
159
163class Triangle : public Object{
164public:
165 point3 p0,p1,p2;
166 Texcoord tex0,tex1,tex2;
169 Image texture;
171 Triangle(): p0(point3()),p1(point3()),p2(point3()),
172 nor(vec3()),e1(point3()),e2(point3()){mat.color = {1.0f,1.0f,1.0f};}
173 Triangle(Vertex a,Vertex b,Vertex c,RGB rgb) {
174 p0 = a.p;p1 = b.p;p2 = c.p;
175 tex0 = a.tex;tex1 = b.tex;tex2 = c.tex;
176 mat.color = rgb;
177 nor = cross(p1-p0,p2-p0).normalize();
178 vec3 a1 = cross(p2-p0,nor);
179 vec3 a2 = cross(p1-p0,nor);
180 e1 = (1/(dot(a1,p1-p0))) * a1;
181 e2 = (1/(dot(a2,p2-p0))) * a2;
182 bbox = AABB(a.p,b.p,c.p);
183 }
184 Triangle(Vertex a,Vertex b,Vertex c,std::string text) {
185 p0 = a.p;p1 = b.p;p2 = c.p;
186 tex0 = a.tex;tex1 = b.tex;tex2 = c.tex;
187 nor = cross(p1-p0,p2-p0).normalize();
188 vec3 a1 = cross(p2-p0,nor);
189 vec3 a2 = cross(p1-p0,nor);
190 e1 = (1/(dot(a1,p1-p0))) * a1;
191 e2 = (1/(dot(a2,p2-p0))) * a2;
192 texture = Image((text).c_str());
193 if (texture.empty()) texture = Image(("input/"+text).c_str());
194 bbox = AABB(a.p,b.p,c.p);
195 }
196 ObjectInfo checkObject(Ray& ray) override;
197 AABB getBox() override {return bbox;}
198 RGB getColor(double b0,double b1,double b2);
199 void setProperties(RGB shine,RGB tran,double ior,double roughness) override {
200 mat.shininess = shine;mat.trans = tran;mat.ior = ior;mat.roughness = roughness;
201 }
202};
203
204class Sun{
205public:
206 vec3 dir;
207 RGB color;
208 Sun() {
209 dir = vec3(0.0,0.0,0.0);
210 color = {1.0f,1.0f,1.0f};
211 }
212 Sun(double x,double y,double z,RGB rgb) {
213 dir = vec3(x,y,z);
214 color = rgb;
215 }
216};
217
218class Bulb{
219public:
220 point3 point;
221 RGB color;
222 Bulb() {
223 point = point3(0.0,0.0,0.0);
224 color = {1.0f,1.0f,1.0f};
225 }
226 Bulb(double x,double y,double z,RGB rgb) {
227 point = point3(x,y,z);
228 color = rgb;
229 }
230};
231
232
233class BVH : public Object{
234private:
235 shared_ptr<Object> left;
236 shared_ptr<Object> right;
237public:
238 BVH(std::vector<shared_ptr<Object>>& objs,int start,int end,int axis){
239 auto comp = (axis == 0) ? box_x_compare
240 : (axis == 1) ? box_y_compare
241 : box_z_compare;
242
243 int span = end - start;
244
245 if(span == 0){
246 return;
247 }else if(span == 1){
248 left = right = objs[start];
249 }else if(span == 2){
250 left = objs[start];
251 right = objs[start+1];
252 }else{
253 std::sort(std::begin(objs) + start,std::begin(objs) + end,comp);
254 int mid = start + span/2;
255 left = std::make_shared<BVH>(objs,start,mid,(axis+1)%3);
256 right = std::make_shared<BVH>(objs,mid,end,(axis+1)%3);
257 }
258
259 bbox = AABB(left->getBox(), right->getBox());
260 }
261
262
263 ObjectInfo checkObject(Ray& ray) override{
264 //hit nothing, return nothing
265 if (!bbox.hit(ray))
266 return ObjectInfo();
267
268 ObjectInfo leftInfo = left->checkObject(ray);
269 ObjectInfo rightInfo = right->checkObject(ray);
270
271 if(leftInfo.isHit && !rightInfo.isHit) return leftInfo;
272 else if(!leftInfo.isHit && rightInfo.isHit) return rightInfo;
273 else if(!leftInfo.isHit && !rightInfo.isHit) return ObjectInfo();
274 else{
275 if(leftInfo.distance < rightInfo.distance) return leftInfo;
276 else return rightInfo;
277 }
278 }
279
280 static bool box_compare(
281 const shared_ptr<Object> a, const shared_ptr<Object> b, int axis_index
282 ) {
283 auto a_axis_interval = a->getBox().getAxis(axis_index);
284 auto b_axis_interval = b->getBox().getAxis(axis_index);
285 return a_axis_interval.min < b_axis_interval.min;
286 }
287
288 static bool box_x_compare (const shared_ptr<Object> a, const shared_ptr<Object> b) {
289 return box_compare(a, b, 0);
290 }
291
292 static bool box_y_compare (const shared_ptr<Object> a, const shared_ptr<Object> b) {
293 return box_compare(a, b, 1);
294 }
295
296 static bool box_z_compare (const shared_ptr<Object> a, const shared_ptr<Object> b) {
297 return box_compare(a, b, 2);
298 }
299 AABB getBox() override {return bbox;}
300
301};
302
303
304#endif
Definition: struct.hpp:79
Definition: object.hpp:233
Definition: object.hpp:218
Material related variables for objects in the scene.
Definition: object.hpp:23
Materials()
Construct a new Materials object, with every member as default.
Definition: object.hpp:33
RGB color
The color, in RGB format, of the material.
Definition: object.hpp:25
double roughness
Roughness of the object, Default to zero(none).
Definition: object.hpp:29
double ior
< The transparency of the object, which is related to refraction.
Definition: object.hpp:28
RGB shininess
The shininess of the object, which is related to reflection.
Definition: object.hpp:26
Materials(RGB color, RGB shininess, RGB trans, double ior, double roughness)
Construct a new Materials object, with inputs.
Definition: object.hpp:42
The object class, both BVH nodes and objects in the scene.
Definition: object.hpp:66
AABB bbox
Axis-aligned bounding box for the Object class. For BVH traversal.
Definition: object.hpp:68
ObjectInfo is a class that passes the parameters of a ray hit, and the details of the object that was...
Definition: object.hpp:50
bool isHit
Signals if there was a hit. Defaults to false.
Definition: object.hpp:52
vec3 normal
The normal at the point of intersection.
Definition: object.hpp:55
Materials mat
Material properties.
Definition: object.hpp:56
double distance
The distance from the ray origin to the object.
Definition: object.hpp:53
point3 i_point
The intersection point.
Definition: object.hpp:54
A plane defined by ax + by + cz + d = 0.
Definition: object.hpp:131
double d
a,b,c,d in ax + by + cz + d = 0.
Definition: object.hpp:133
Materials mat
Material properties.
Definition: object.hpp:136
vec3 nor
Normal of the plane.
Definition: object.hpp:134
point3 point
A point on the plane, for calculation purposes.
Definition: object.hpp:135
Definition: struct.hpp:20
Class Ray, consists of a eye and direction.
Definition: struct.hpp:68
Class sphere, with one center point and a radius, together with a rgb color value.
Definition: object.hpp:79
double r
Radius of the sphere.
Definition: object.hpp:82
Sphere(double x, double y, double z, double r, RGB rgb)
Construct a new Sphere object, with color inputs.
Definition: object.hpp:95
Sphere(double x, double y, double z, double r, std::string text)
Construct a new Sphere object, with texture inputs.
Definition: object.hpp:104
void setProperties(RGB shine, RGB tran, double ior, double roughness) override
Set the Material properties.
Definition: object.hpp:123
Image texture
The texture, as a image.
Definition: object.hpp:84
Sphere()
Construct a new Sphere object with no inputs.
Definition: object.hpp:88
point3 c
Center point of the sphere.
Definition: object.hpp:81
Materials mat
Material properties.
Definition: object.hpp:83
AABB getBox() override
Get the Axis-aligned bounding box.
Definition: object.hpp:117
Definition: object.hpp:204
Class Triangle, with 3 vertices that made it up.
Definition: object.hpp:163
point3 p2
Three vertices of the triangle.
Definition: object.hpp:165
point3 e2
The e1,e2 coordinates, precomputed for Barycentric calculation.
Definition: object.hpp:168
Materials mat
Material properties.
Definition: object.hpp:170
Texcoord tex2
texture coordinates for the vertices,if provided.
Definition: object.hpp:166
Image texture
Texture, if provided.
Definition: object.hpp:169
vec3 nor
Normal of the triangle.
Definition: object.hpp:167
A Vertex class, used only in input parsing.
Definition: object.hpp:151
point3 p
The point for the vertex.
Definition: object.hpp:153
Definition: vec3.hpp:18
Definition: struct.hpp:60