erebus
view liberebus/src/geomobj.cc @ 42:b9294cd6b9dc
console input history with up/down
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 10 Jun 2014 16:15:08 +0300 |
parents | 09028848f276 |
children | ed18af9da8f7 |
line source
1 #include <math.h>
2 #include <float.h>
3 #include <vmath/vmath.h>
4 #include "geomobj.h"
5 #include "erebus_impl.h"
7 static LambertRefl lambert;
9 GeomObject::GeomObject()
10 {
11 brdf = &lambert;
12 }
14 ObjType GeomObject::get_type() const
15 {
16 return ObjType::geom;
17 }
19 bool GeomObject::intersect(const Ray &ray, RayHit *hit) const
20 {
21 return false;
22 }
24 Vector3 GeomObject::gen_surface_point() const
25 {
26 return Vector3(0, 0, 0);
27 }
29 Vector3 GeomObject::calc_normal(const RayHit &hit) const
30 {
31 // when you look at singularities, the singularities always look back at you :)
32 return -(hit.local_ray.dir).normalized();
33 }
35 Vector3 GeomObject::calc_tangent(const RayHit &hit) const
36 {
37 return Vector3(1, 0, 0); // whatever...
38 }
40 Vector2 GeomObject::calc_texcoords(const RayHit &hit) const
41 {
42 return Vector2();
43 }
45 // --- class Sphere ---
47 bool Sphere::intersect(const Ray &ray, RayHit *hit) const
48 {
49 // assumes center is the origin and radius is 1
50 float a = dot_product(ray.dir, ray.dir);
51 float b = 2.0 * dot_product(ray.dir, ray.origin);
52 float c = dot_product(ray.origin, ray.origin) - 1.0;
54 float d = b * b - 4.0 * a * c;
55 if(d < 1e-6) return false;
57 float sqrt_d = sqrt(d);
58 float t0 = (-b + sqrt_d) / (2.0 * a);
59 float t1 = (-b - sqrt_d) / (2.0 * a);
61 if(t0 < 1e-6) t0 = t1;
62 if(t1 < 1e-6) t1 = t0;
63 float t = t0 < t1 ? t0 : t1;
64 if(t < 1e-6) return false;
66 if(hit) {
67 hit->dist = t;
68 hit->obj = this;
69 hit->subobj = 0;
70 }
71 return true;
72 }
74 Vector3 Sphere::gen_surface_point() const
75 {
76 return sphrand(1.0);
77 }
79 Vector3 Sphere::calc_normal(const RayHit &hit) const
80 {
81 Vector3 pt = hit.local_ray.origin + hit.local_ray.dir * hit.dist;
82 return pt.normalized();
83 }
85 static inline Vector3 sphvec(float u, float v)
86 {
87 float theta = u * M_PI * 2.0;
88 float phi = v * M_PI;
90 return Vector3(sin(theta) * sin(phi), cos(phi), cos(theta) * sin(phi));
91 }
93 Vector3 Sphere::calc_tangent(const RayHit &hit) const
94 {
95 Vector2 uv = calc_texcoords(hit);
96 Vector3 pnext = sphvec(uv.x + 0.05, 0.5);
97 Vector3 pprev = sphvec(uv.y - 0.05, 0.5);
98 return (pnext - pprev).normalized();
99 }
101 Vector2 Sphere::calc_texcoords(const RayHit &hit) const
102 {
103 Vector3 pt = hit.local_ray.origin + hit.local_ray.dir * hit.dist;
104 pt.normalize();
106 float theta = atan2(pt.z, pt.x);
107 float phi = acos(pt.y);
109 return Vector2(theta / M_PI + 0.5, phi / M_PI);
110 }
113 // --- class Box ---
115 bool Box::intersect(const Ray &ray, RayHit *hit) const
116 {
117 Vector3 param[2] = {Vector3{-0.5, -0.5, -0.5}, Vector3{0.5, 0.5, 0.5}};
118 Vector3 inv_dir{1.0f / ray.dir.x, 1.0f / ray.dir.y, 1.0f / ray.dir.z};
119 int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
121 float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
122 float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
123 float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
124 float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
126 if(tmin > tymax || tymin > tmax) {
127 return false;
128 }
129 if(tymin > tmin) {
130 tmin = tymin;
131 }
132 if(tymax < tmax) {
133 tmax = tymax;
134 }
136 float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
137 float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
139 if(tmin > tzmax || tzmin > tmax) {
140 return false;
141 }
142 if(tzmin > tmin) {
143 tmin = tzmin;
144 }
145 if(tzmax < tmax) {
146 tmax = tzmax;
147 }
149 float t = tmin < 1e-4 ? tmax : tmin;
150 if(t >= 1e-4) {
151 if(hit) {
152 hit->obj = this;
153 hit->dist = t;
154 }
155 return true;
156 }
157 return false;
158 }
160 #define SIGN(x) (x >= 0.0 ? 1.0 : -1.0)
162 Vector3 Box::gen_surface_point() const
163 {
164 Vector3 rnd{randf(-1, 1), randf(-1, 1), randf(-1, 1)};
165 float absrnd[3];
167 absrnd[0] = fabs(rnd.x);
168 absrnd[1] = fabs(rnd.y);
169 absrnd[2] = fabs(rnd.z);
171 int major = 0;
172 for(int i=1; i<3; i++) {
173 if(absrnd[i] > absrnd[major]) {
174 major = i;
175 }
176 }
177 rnd[major] = SIGN(rnd[major]);
178 return rnd;
179 }
181 #define BOX_EXT 0.499999
182 Vector3 Box::calc_normal(const RayHit &hit) const
183 {
184 Vector3 pt = hit.local_ray.origin + hit.local_ray.dir * hit.dist;
185 if(pt.x > BOX_EXT) return Vector3(1, 0, 0);
186 if(pt.x < -BOX_EXT) return Vector3(-1, 0, 0);
187 if(pt.y > BOX_EXT) return Vector3(0, 1, 0);
188 if(pt.y < -BOX_EXT) return Vector3(0, -1, 0);
189 if(pt.z > BOX_EXT) return Vector3(0, 0, 1);
190 if(pt.z < -BOX_EXT) return Vector3(0, 0, -1);
191 return Vector3(0, 0, 0); // shouldn't happen unless numerical precision is fucked
192 }
194 Vector3 Box::calc_tangent(const RayHit &hit) const
195 {
196 return Vector3(1, 0, 0); // TODO
197 }
199 Vector2 Box::calc_texcoords(const RayHit &hit) const
200 {
201 return Vector2(0, 0); // TODO
202 }
204 // --- class Triangle ---
206 bool Triangle::intersect(const Ray &ray, RayHit *hit) const
207 {
208 return false;
209 }
211 Vector3 Triangle::gen_surface_point() const
212 {
213 float bu = randf();
214 float bv = randf();
215 float bw = 1.0 - (bu + bv);
217 return v[0] * bu + v[1] * bv + v[2] * bw;
218 }
220 // --- class Mesh ---
222 bool Mesh::intersect(const Ray &ray, RayHit *hit) const
223 {
224 RayHit nearest;
225 nearest.dist = FLT_MAX;
227 for(size_t i=0; i<faces.size(); i++) {
228 if(faces[i].intersect(ray, hit)) {
229 if(!hit) return true;
230 if(hit->dist < nearest.dist) {
231 nearest = *hit;
232 }
233 }
234 }
236 if(nearest.dist < FLT_MAX) {
237 *hit = nearest;
238 hit->subobj = hit->obj;
239 hit->obj = this;
240 return true;
241 }
242 return false;
243 }
245 Vector3 Mesh::gen_surface_point() const
246 {
247 // this needs some precalculation...
248 return Vector3(0, 0, 0); // TODO
249 }