erebus

diff liberebus/src/geomobj.cc @ 18:09028848f276

- implemented Box object intersection - implemented interactive camera manipulation
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 26 May 2014 23:34:12 +0300
parents e9da2916bc79
children 2e817711d0f6
line diff
     1.1 --- a/liberebus/src/geomobj.cc	Mon May 26 05:41:28 2014 +0300
     1.2 +++ b/liberebus/src/geomobj.cc	Mon May 26 23:34:12 2014 +0300
     1.3 @@ -102,7 +102,71 @@
     1.4  
     1.5  bool Box::intersect(const Ray &ray, RayHit *hit) const
     1.6  {
     1.7 +	Vector3 param[2] = {Vector3{-0.5, -0.5, -0.5}, Vector3{0.5, 0.5, 0.5}};
     1.8 +	Vector3 inv_dir{1.0f / ray.dir.x, 1.0f / ray.dir.y, 1.0f / ray.dir.z};
     1.9 +	int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
    1.10 +
    1.11 +	float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
    1.12 +	float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
    1.13 +	float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
    1.14 +	float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
    1.15 +
    1.16 +	if(tmin > tymax || tymin > tmax) {
    1.17 +		return false;
    1.18 +	}
    1.19 +	if(tymin > tmin) {
    1.20 +		tmin = tymin;
    1.21 +	}
    1.22 +	if(tymax < tmax) {
    1.23 +		tmax = tymax;
    1.24 +	}
    1.25 +
    1.26 +	float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
    1.27 +	float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
    1.28 +
    1.29 +	if(tmin > tzmax || tzmin > tmax) {
    1.30 +		return false;
    1.31 +	}
    1.32 +	if(tzmin > tmin) {
    1.33 +		tmin = tzmin;
    1.34 +	}
    1.35 +	if(tzmax < tmax) {
    1.36 +		tmax = tzmax;
    1.37 +	}
    1.38 +
    1.39 +	float t = tmin < 1e-4 ? tmax : tmin;
    1.40 +	if(t >= 1e-4) {
    1.41 +		if(hit) {
    1.42 +			hit->obj = this;
    1.43 +			hit->dist = t;
    1.44 +		}
    1.45 +		return true;
    1.46 +	}
    1.47  	return false;
    1.48 +
    1.49 +}
    1.50 +
    1.51 +#define BOX_EXT		0.499999
    1.52 +Vector3 Box::calc_normal(const RayHit &hit) const
    1.53 +{
    1.54 +	Vector3 pt = hit.local_ray.origin + hit.local_ray.dir * hit.dist;
    1.55 +	if(pt.x > BOX_EXT) return Vector3(1, 0, 0);
    1.56 +	if(pt.x < -BOX_EXT) return Vector3(-1, 0, 0);
    1.57 +	if(pt.y > BOX_EXT) return Vector3(0, 1, 0);
    1.58 +	if(pt.y < -BOX_EXT) return Vector3(0, -1, 0);
    1.59 +	if(pt.z > BOX_EXT) return Vector3(0, 0, 1);
    1.60 +	if(pt.z < -BOX_EXT) return Vector3(0, 0, -1);
    1.61 +	return Vector3(0, 0, 0);	// shouldn't happen unless numerical precision is fucked
    1.62 +}
    1.63 +
    1.64 +Vector3 Box::calc_tangent(const RayHit &hit) const
    1.65 +{
    1.66 +	return Vector3(1, 0, 0);	// TODO
    1.67 +}
    1.68 +
    1.69 +Vector2 Box::calc_texcoords(const RayHit &hit) const
    1.70 +{
    1.71 +	return Vector2(0, 0);	// TODO
    1.72  }
    1.73  
    1.74  // --- class Triangle ---