70 virtual void setProperties(
RGB,
RGB,
double,
double){}
71 virtual AABB getBox() = 0;
104 Sphere(
double x,
double y,
double z,
double r,std::string text):
r(
r) {
107 texture = Image((text).c_str());
118 std::tuple<double,double> sphereUV(
const point3& point)
const;
138 Plane(
double a,
double b,
double c,
double d,
RGB rgb): a(a),b(b),c(c),
d(
d) {
140 point = (-
d *
vec3(a,b,c)) / (pow(a,2) + pow(b,2) + pow(c,2));
143 void setProperties(
RGB shine,
RGB tran,
double ior,
double roughness){
174 p0 = a.
p;p1 = b.
p;
p2 = c.
p;
175 tex0 = a.tex;tex1 = b.tex;
tex2 = c.tex;
177 nor = cross(p1-p0,
p2-p0).normalize();
180 e1 = (1/(dot(a1,p1-p0))) * a1;
181 e2 = (1/(dot(a2,
p2-p0))) * a2;
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();
190 e1 = (1/(dot(a1,p1-p0))) * a1;
191 e2 = (1/(dot(a2,
p2-p0))) * a2;
192 texture = Image((text).c_str());
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 {
209 dir =
vec3(0.0,0.0,0.0);
210 color = {1.0f,1.0f,1.0f};
212 Sun(
double x,
double y,
double z,
RGB rgb) {
223 point =
point3(0.0,0.0,0.0);
224 color = {1.0f,1.0f,1.0f};
226 Bulb(
double x,
double y,
double z,
RGB rgb) {
235 shared_ptr<Object> left;
236 shared_ptr<Object> right;
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
243 int span = end - start;
248 left = right = objs[start];
251 right = objs[start+1];
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);
259 bbox =
AABB(left->getBox(), right->getBox());
269 ObjectInfo rightInfo = right->checkObject(ray);
271 if(leftInfo.
isHit && !rightInfo.
isHit)
return leftInfo;
272 else if(!leftInfo.
isHit && rightInfo.
isHit)
return rightInfo;
276 else return rightInfo;
280 static bool box_compare(
281 const shared_ptr<Object> a,
const shared_ptr<Object> b,
int axis_index
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;
288 static bool box_x_compare (
const shared_ptr<Object> a,
const shared_ptr<Object> b) {
289 return box_compare(a, b, 0);
292 static bool box_y_compare (
const shared_ptr<Object> a,
const shared_ptr<Object> b) {
293 return box_compare(a, b, 1);
296 static bool box_z_compare (
const shared_ptr<Object> a,
const shared_ptr<Object> b) {
297 return box_compare(a, b, 2);
299 AABB getBox()
override {
return bbox;}
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: struct.hpp:60