src/scene.cc
branchpyrit
changeset 21 79b516a3803d
parent 20 f22952603f29
child 22 76b7bd51d64a
equal deleted inserted replaced
20:f22952603f29 21:79b516a3803d
    10 #include "common.h"
    10 #include "common.h"
    11 #include "scene.h"
    11 #include "scene.h"
    12 
    12 
    13 void Camera::rotate(const Quaternion &q)
    13 void Camera::rotate(const Quaternion &q)
    14 {
    14 {
       
    15 	/*
       
    16 	//non-optimized
    15 	Quaternion res;
    17 	Quaternion res;
    16 	res = q * Quaternion(u) * conjugate(q);
    18 	res = q * Quaternion(u) * conjugate(q);
    17 	u = res.toVector();
    19 	u = res.toVector();
    18 	res = q * Quaternion(v) * conjugate(q);
    20 	res = q * Quaternion(v) * conjugate(q);
    19 	v = res.toVector();
    21 	v = res.toVector();
    20 	res = q * Quaternion(p) * conjugate(q);
    22 	res = q * Quaternion(p) * conjugate(q);
    21 	p = res.toVector();
    23 	p = res.toVector();
    22 	p.normalize();
    24 	*/
    23 	u.normalize();
    25 
    24 	v.normalize();
    26 	// optimized
    25 /*  // optimized version
       
    26 	float t2 =   q.a*q.b;
    27 	float t2 =   q.a*q.b;
    27 	float t3 =   q.a*q.c;
    28 	float t3 =   q.a*q.c;
    28 	float t4 =   q.a*q.d;
    29 	float t4 =   q.a*q.d;
    29 	float t5 =  -q.b*q.b;
    30 	float t5 =  -q.b*q.b;
    30 	float t6 =   q.b*q.c;
    31 	float t6 =   q.b*q.c;
    42 	z = 2*( (t7 -  t3)*u.x + (t2 +  t9)*u.y + (t5 + t8)*u.z ) + u.z;
    43 	z = 2*( (t7 -  t3)*u.x + (t2 +  t9)*u.y + (t5 + t8)*u.z ) + u.z;
    43 	u = Vector3(x,y,z);
    44 	u = Vector3(x,y,z);
    44 	x = 2*( (t8 + t10)*v.x + (t6 -  t4)*v.y + (t3 + t7)*v.z ) + v.x;
    45 	x = 2*( (t8 + t10)*v.x + (t6 -  t4)*v.y + (t3 + t7)*v.z ) + v.x;
    45 	y = 2*( (t4 +  t6)*v.x + (t5 + t10)*v.y + (t9 - t2)*v.z ) + v.y;
    46 	y = 2*( (t4 +  t6)*v.x + (t5 + t10)*v.y + (t9 - t2)*v.z ) + v.y;
    46 	z = 2*( (t7 -  t3)*v.x + (t2 +  t9)*v.y + (t5 + t8)*v.z ) + v.z;
    47 	z = 2*( (t7 -  t3)*v.x + (t2 +  t9)*v.y + (t5 + t8)*v.z ) + v.z;
    47 	v = Vector3(x,y,z);*/
    48 	v = Vector3(x,y,z);
       
    49 	p.normalize();
       
    50 	u.normalize();
       
    51 	v.normalize();
    48 }
    52 }
    49 
    53 
    50 void Camera::move(const float fw, const float left, const float up)
    54 void Camera::move(const float fw, const float left, const float up)
    51 {
    55 {
    52 	eye = eye + fw*p + left*u + up*v;
    56 	eye = eye + fw*p + left*u + up*v;
    90 	return true;
    94 	return true;
    91 }
    95 }
    92 
    96 
    93 bool Sphere::intersect(const Ray &ray, float &dist)
    97 bool Sphere::intersect(const Ray &ray, float &dist)
    94 {
    98 {
    95 	Vector3 V = ((Ray)ray).o - center;
    99 	Vector3 V = ray.o - center;
    96 
   100 	register float d = -dot(V, ray.dir);
    97 	float Vd = - dot(V, ray.dir);
   101 	register float Det = d * d - (dot(V,V) - sqr_radius);
    98 	float Det = Vd * Vd - (dot(V,V) - sqr_radius);
       
    99 
       
   100 	if (Det > 0) {
   102 	if (Det > 0) {
   101 		Det = sqrtf(Det);
   103 		d -= sqrtf(Det);
   102 		float t1 = Vd - Det;
   104 		if (d > 0 && d < dist)
   103 		if (t1 > 0)
   105 		{
   104 		{
   106 			dist = d;
   105 			if (t1 < dist) {
   107 			return true;
   106 				dist = t1;
       
   107 				return true;
       
   108 			}
       
   109 		} else {
       
   110 			float t2 = Vd + Det;
       
   111 			if (t2 > 0)
       
   112 			{
       
   113 				// ray from inside of the sphere
       
   114 				dist = t2;
       
   115 				return true;
       
   116 			}
       
   117 		}
   108 		}
   118 	}
   109 	}
   119 	return false;
   110 	return false;
   120 }
   111 }
   121 
   112 
   161 	return bbox;
   152 	return bbox;
   162 }
   153 }
   163 
   154 
   164 bool Box::intersect(const Ray &ray, float &dist)
   155 bool Box::intersect(const Ray &ray, float &dist)
   165 {
   156 {
   166 	float b;
   157 	float a,b;
   167 	return get_bbox().intersect(ray, dist, b);
   158 	bool res = get_bbox().intersect(ray, a, b);
       
   159 	if (res && a < dist)
       
   160 	{
       
   161 		dist = a;
       
   162 		return true;
       
   163 	}
       
   164 	else
       
   165 		return false;
   168 }
   166 }
   169 
   167 
   170 Vector3 Box::normal(Vector3 &P)
   168 Vector3 Box::normal(Vector3 &P)
   171 {
   169 {
   172 	Vector3 N;
   170 	Vector3 N;