src/scene.cc
branchpyrit
changeset 15 a0a3e334744f
parent 14 fc18ac4833f2
child 20 f22952603f29
equal deleted inserted replaced
14:fc18ac4833f2 15:a0a3e334744f
    19 
    19 
    20 	for (int i = 0; i < 3; i++)
    20 	for (int i = 0; i < 3; i++)
    21 	{
    21 	{
    22 		if (ray.dir.cell[i] == 0) {
    22 		if (ray.dir.cell[i] == 0) {
    23 			/* ray is parallel to these planes */
    23 			/* ray is parallel to these planes */
    24 			if (ray.a.cell[i] < L.cell[i] || ray.a.cell[i] > H.cell[i])
    24 			if (ray.o.cell[i] < L.cell[i] || ray.o.cell[i] > H.cell[i])
    25 				return false;
    25 				return false;
    26 		} else
    26 		} else
    27 		{
    27 		{
    28 			/* compute the intersection distance of the planes */
    28 			/* compute the intersection distance of the planes */
    29 			t1 = (L.cell[i] - ray.a.cell[i]) / ray.dir.cell[i];
    29 			t1 = (L.cell[i] - ray.o.cell[i]) / ray.dir.cell[i];
    30 			t2 = (H.cell[i] - ray.a.cell[i]) / ray.dir.cell[i];
    30 			t2 = (H.cell[i] - ray.o.cell[i]) / ray.dir.cell[i];
    31 
    31 
    32 			if (t1 > t2)
    32 			if (t1 > t2)
    33 				swap(t1, t2);
    33 				swap(t1, t2);
    34 
    34 
    35 			if (t1 > tnear)
    35 			if (t1 > tnear)
    48 	return true;
    48 	return true;
    49 }
    49 }
    50 
    50 
    51 bool Sphere::intersect(const Ray &ray, float &dist)
    51 bool Sphere::intersect(const Ray &ray, float &dist)
    52 {
    52 {
    53 	Vector3 V = ((Ray)ray).a - center;
    53 	Vector3 V = ((Ray)ray).o - center;
    54 
    54 
    55 	float Vd = - dot(V, ray.dir);
    55 	float Vd = - dot(V, ray.dir);
    56 	float Det = Vd * Vd - (dot(V,V) - sqr_radius);
    56 	float Det = Vd * Vd - (dot(V,V) - sqr_radius);
    57 
    57 
    58 	if (Det > 0) {
    58 	if (Det > 0) {
    79 
    79 
    80 bool Sphere::intersect_all(const Ray &ray, float dist, vector<float> &allts)
    80 bool Sphere::intersect_all(const Ray &ray, float dist, vector<float> &allts)
    81 {
    81 {
    82 	//allts = new vector<float>();
    82 	//allts = new vector<float>();
    83 
    83 
    84 	Vector3 V = ((Ray)ray).a - center;
    84 	Vector3 V = ((Ray)ray).o - center;
    85 	float Vd = - dot(V, ray.dir);
    85 	float Vd = - dot(V, ray.dir);
    86 	float Det = Vd * Vd - (dot(V,V) - sqr_radius);
    86 	float Det = Vd * Vd - (dot(V,V) - sqr_radius);
    87 
    87 
    88 	if (Det > 0) {
    88 	if (Det > 0) {
    89 		Det = sqrtf(Det);
    89 		Det = sqrtf(Det);
   113 
   113 
   114 BBox Sphere::get_bbox()
   114 BBox Sphere::get_bbox()
   115 {
   115 {
   116 	BBox bbox = BBox();
   116 	BBox bbox = BBox();
   117 	bbox.L = center - radius;
   117 	bbox.L = center - radius;
   118 	//bbox.L.y = center.y - radius;
       
   119 	//bbox.L.z = center.z - radius;
       
   120 	bbox.H = center + radius;
   118 	bbox.H = center + radius;
   121 	//bbox.H.y = center.y + radius;
       
   122 	//bbox.H.z = center.z + radius;
       
   123 	return bbox;
   119 	return bbox;
   124 }
   120 }
   125 
   121 
   126 bool Box::intersect(const Ray &ray, float &dist)
   122 bool Box::intersect(const Ray &ray, float &dist)
   127 {
   123 {
   129 	return get_bbox().intersect(ray, dist, b);
   125 	return get_bbox().intersect(ray, dist, b);
   130 }
   126 }
   131 
   127 
   132 Vector3 Box::normal(Vector3 &P)
   128 Vector3 Box::normal(Vector3 &P)
   133 {
   129 {
   134 	Vector3 N(0,1,0);
   130 	Vector3 N;
   135 	/*for (int i = 0; i < 3; i++)
   131 	for (int i = 0; i < 3; i++)
   136 	{
   132 	{
   137 		if (P.cell[i] >= L.cell[i]-Eps && P.cell[i] <= L.cell[i]+Eps)
   133 		if (P.cell[i] >= L.cell[i]-Eps && P.cell[i] <= L.cell[i]+Eps)
   138 		//if (P.cell[i] == L.cell[i])
   134 		//if (P.cell[i] == L.cell[i])
   139 		{
   135 		{
   140 			N.cell[i] = -1.0;
   136 			N.cell[i] = -1.0;
   144 		//if (P.cell[i] == H.cell[i])
   140 		//if (P.cell[i] == H.cell[i])
   145 		{
   141 		{
   146 			N.cell[i] = +1.0;
   142 			N.cell[i] = +1.0;
   147 			break;
   143 			break;
   148 		}
   144 		}
   149 	}*/
   145 	}
   150 	return N;
   146 	return N;
   151 }
   147 }
   152 
   148 
   153 // this initialization and following intersection methods implements
   149 // this initialization and following intersection methods implements
   154 // Fast Triangle Intersection algorithm from
   150 // Fast Triangle Intersection algorithm from
   199 }
   195 }
   200 
   196 
   201 // see comment for previous method
   197 // see comment for previous method
   202 bool Triangle::intersect(const Ray &ray, float &dist)
   198 bool Triangle::intersect(const Ray &ray, float &dist)
   203 {
   199 {
   204 	Vector3 O = ray.a;
   200 	Vector3 O = ray.o;
   205 	Vector3 D = ray.dir;
   201 	Vector3 D = ray.dir;
   206 
   202 
   207 	const int modulo3[5] = {0,1,2,0,1};
   203 	const int modulo3[5] = {0,1,2,0,1};
   208 	const int ku = modulo3[k+1];
   204 	const int ku = modulo3[k+1];
   209 	const int kv = modulo3[k+2];
   205 	const int kv = modulo3[k+2];