TinyRaytracer 0.1
A simple C++ raytracer
Loading...
Searching...
No Matches
struct.hpp
Go to the documentation of this file.
1
11#ifndef RAY_H
12#define RAY_H
13#include "helper.hpp"
14#include "libpng.h"
15#include "vec3.hpp"
16#include "interval.hpp"
17#include <memory>
18using std::pow;
19/*r,g,b from 0 to 1*/
20class RGB{
21public:
22 double r = 0.0;
23 double g = 0.0;
24 double b = 0.0;
25 RGB(): r(0.0),g(0.0),b(0.0){}
26 RGB(double r,double g,double b): r(r),g(g),b(b){}
27 bool operator==(const RGB& other) const {
28 return (r == other.r && g == other.g && b == other.b);
29 }
30 RGB operator-(const RGB& other) const {
31 return RGB(r-other.r,g-other.g,b-other.b);
32 }
33 RGB operator*(const RGB& other) const {
34 return RGB(r*other.r,g*other.g,b*other.b);
35 }
36};
37
38/*r,g,b from 0 to 1*/
39class RGBA{
40public:
41 double r = 0.0;
42 double g = 0.0;
43 double b = 0.0;
44 double a = 0.0;
45 RGBA(): r(0.0),g(0.0),b(0.0),a(0.0){}
46 RGBA(double r,double g,double b,double a): r(r),g(g),b(b),a(a){}
47 RGBA operator+(const RGBA& other) const {
48 return RGBA(r+other.r,g+other.g,b+other.b,a+other.a);
49 }
50 friend RGBA operator*(RGB rgb,const RGBA& other){
51 return RGBA(rgb.r*other.r,rgb.g*other.g,rgb.b*other.b,other.a);
52 }
53 RGBA mean(int aa){
54 return RGBA(r/aa,g/aa,b/aa,a/aa);
55 }
56};
57
58
59/*u,v*/
60typedef struct{
61 double u = 0;
62 double v = 0;
64
68class Ray{
69public:
70 point3 eye;
71 vec3 dir;
72 int bounce;
73 Ray() : bounce(0) {}
74 Ray(double x,double y);
75 Ray(point3 eye,vec3 dir,int bounce): eye(eye), dir(dir.normalize()),bounce(bounce){}
76
77};
78
79class AABB{
80public:
81 Interval x,y,z;
82
83 AABB() {}
84
85 AABB(const Interval& x,const Interval& y,const Interval& z):
86 x(x),y(y),z(z) {}
87
88 AABB(const point3& a,const point3& b) {
89 x = (a.x <= b.x) ? Interval(a.x, b.x) : Interval(b.x, a.x);
90 y = (a.y <= b.y) ? Interval(a.y, b.y) : Interval(b.y, a.y);
91 z = (a.z <= b.z) ? Interval(a.z, b.z) : Interval(b.z, a.z);
92 }
93
94 AABB(const point3& a,const point3& b,const point3& c) {
95 float x_min,x_max,y_min,y_max,z_min,z_max;
96 //obtain the min and max value for the three coordinates
97 x_min = std::min({a.x,b.x,c.x});x_max = std::max({a.x,b.x,c.x});
98 y_min = std::min({a.y,b.y,c.y});y_max = std::max({a.y,b.y,c.y});
99 z_min = std::min({a.z,b.z,c.z});z_max = std::max({a.z,b.z,c.z});
100 x = Interval(x_min,x_max);y = Interval(y_min,y_max);z = Interval(z_min,z_max);
101 //expand if the interval is too small
102 if(x.size() < 0.01) x = x.expand(0.01);
103 if(y.size() < 0.01) y = y.expand(0.01);
104 if(z.size() < 0.01) z = z.expand(0.01);
105 }
106
107 AABB(const AABB& a, const AABB& b) {
108 x = Interval(std::min(a.x.min,b.x.min),std::max(a.x.max,b.x.max));
109 y = Interval(std::min(a.y.min,b.y.min),std::max(a.y.max,b.y.max));
110 z = Interval(std::min(a.z.min,b.z.min),std::max(a.z.max,b.z.max));
111 }
112
113 const Interval& getAxis(int n) const {
114 if (n == 0) return x;
115 if (n == 1) return y;
116 return z;
117 }
118
119 int longestAxis() const {
120 if (x.size() > y.size())
121 return x.size() > z.size() ? 0 : 2;
122 else
123 return y.size() > z.size() ? 1 : 2;
124 }
125
126
127 bool hit(const Ray& r) const {
128 const point3& ray_eye = r.eye;
129 const vec3& ray_dir = r.dir;
130
131 double t_min = double(-INFINITY);
132 double t_max = double(INFINITY);
133
134 for (int axis = 0; axis < 3; axis++) {
135 const Interval& ax = getAxis(axis);
136 if (ax.min > ax.max) return false;
137 //const double adinv = (ray_dir[axis] != 0) ? (1.0 / ray_dir[axis]) : INFINITY;
138 const double adinv = 1.0 / ray_dir[axis];
139
140 double t0 = (ax.min - ray_eye[axis]) * adinv;
141 double t1 = (ax.max - ray_eye[axis]) * adinv;
142
143 if (t0 > t1) std::swap(t0, t1);
144
145 t_min = std::max(t_min, t0);
146 t_max = std::min(t_max, t1);
147
148 if (t_max <= t_min)
149 return false;
150 }
151 return true;
152 }
153};
154
155#endif
Definition: struct.hpp:79
Definition: interval.hpp:21
Interval expand(double delta) const
Expand the interval by delta on both ends.
Definition: interval.hpp:55
Definition: struct.hpp:39
Definition: struct.hpp:20
Class Ray, consists of a eye and direction.
Definition: struct.hpp:68
Definition: vec3.hpp:18
Definition: struct.hpp:60