src/scene.cc
branchpyrit
changeset 14 fc18ac4833f2
parent 12 f4fcabf05785
child 15 a0a3e334744f
equal deleted inserted replaced
13:fbd1d2f7d94e 14:fc18ac4833f2
     4  *
     4  *
     5  * Radek Brich, 2006
     5  * Radek Brich, 2006
     6  */
     6  */
     7 
     7 
     8 #include <math.h>
     8 #include <math.h>
     9 #include <float.h>
     9 
    10 
    10 #include "common.h"
    11 #include "scene.h"
    11 #include "scene.h"
    12 
    12 
    13 /* http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtinter3.htm */
    13 /* http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtinter3.htm */
    14 bool BBox::intersect(const Ray &ray, float &a, float &b)
    14 bool BBox::intersect(const Ray &ray, float &a, float &b)
    15 {
    15 {
    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] > R.cell[i])
    24 			if (ray.a.cell[i] < L.cell[i] || ray.a.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.a.cell[i]) / ray.dir.cell[i];
    30 			t2 = (R.cell[i] - ray.a.cell[i]) / ray.dir.cell[i];
    30 			t2 = (H.cell[i] - ray.a.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)
   112 }
   112 }
   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.x = center.x - radius;
   117 	bbox.L = center - radius;
   118 	bbox.L.y = center.y - radius;
   118 	//bbox.L.y = center.y - radius;
   119 	bbox.L.z = center.z - radius;
   119 	//bbox.L.z = center.z - radius;
   120 	bbox.R.x = center.x + radius;
   120 	bbox.H = center + radius;
   121 	bbox.R.y = center.y + radius;
   121 	//bbox.H.y = center.y + radius;
   122 	bbox.R.z = center.z + radius;
   122 	//bbox.H.z = center.z + radius;
   123 	return bbox;
   123 	return bbox;
   124 }
   124 }
   125 
   125 
   126 bool Plane::intersect(const Ray &ray, float &dist)
   126 bool Box::intersect(const Ray &ray, float &dist)
   127 {
   127 {
   128 	float dir = dot(N, ray.dir);
   128 	float b;
   129 	if (dir != 0)
   129 	return get_bbox().intersect(ray, dist, b);
   130 	{
   130 }
   131 		float newdist = -(dot(N, ray.a) + d) / dir;
   131 
   132 		if (newdist > 0 && newdist < dist) {
   132 Vector3 Box::normal(Vector3 &P)
   133 			dist = newdist;
   133 {
   134 			return true;
   134 	Vector3 N(0,1,0);
   135 		}
   135 	/*for (int i = 0; i < 3; i++)
   136 	}
   136 	{
   137 	return false;
   137 		if (P.cell[i] >= L.cell[i]-Eps && P.cell[i] <= L.cell[i]+Eps)
   138 }
   138 		//if (P.cell[i] == L.cell[i])
   139 
   139 		{
   140 BBox Plane::get_bbox()
   140 			N.cell[i] = -1.0;
   141 {
   141 			break;
   142 	return BBox();
   142 		}
       
   143 		if (P.cell[i] >= H.cell[i]-Eps && P.cell[i] <= H.cell[i]+Eps)
       
   144 		//if (P.cell[i] == H.cell[i])
       
   145 		{
       
   146 			N.cell[i] = +1.0;
       
   147 			break;
       
   148 		}
       
   149 	}*/
       
   150 	return N;
   143 }
   151 }
   144 
   152 
   145 // this initialization and following intersection methods implements
   153 // this initialization and following intersection methods implements
   146 // Fast Triangle Intersection algorithm from
   154 // Fast Triangle Intersection algorithm from
   147 // http://www.mpi-inf.mpg.de/~wald/PhD/
   155 // http://www.mpi-inf.mpg.de/~wald/PhD/
   231 	if (C.x < bbox.L.x)  bbox.L.x = C.x;
   239 	if (C.x < bbox.L.x)  bbox.L.x = C.x;
   232 	if (B.y < bbox.L.y)  bbox.L.y = B.y;
   240 	if (B.y < bbox.L.y)  bbox.L.y = B.y;
   233 	if (C.y < bbox.L.y)  bbox.L.y = C.y;
   241 	if (C.y < bbox.L.y)  bbox.L.y = C.y;
   234 	if (B.z < bbox.L.z)  bbox.L.z = B.z;
   242 	if (B.z < bbox.L.z)  bbox.L.z = B.z;
   235 	if (C.z < bbox.L.z)  bbox.L.z = C.z;
   243 	if (C.z < bbox.L.z)  bbox.L.z = C.z;
   236 	bbox.R = A;
   244 	bbox.H = A;
   237 	if (B.x > bbox.R.x)  bbox.R.x = B.x;
   245 	if (B.x > bbox.H.x)  bbox.H.x = B.x;
   238 	if (C.x > bbox.R.x)  bbox.R.x = C.x;
   246 	if (C.x > bbox.H.x)  bbox.H.x = C.x;
   239 	if (B.y > bbox.R.y)  bbox.R.y = B.y;
   247 	if (B.y > bbox.H.y)  bbox.H.y = B.y;
   240 	if (C.y > bbox.R.y)  bbox.R.y = C.y;
   248 	if (C.y > bbox.H.y)  bbox.H.y = C.y;
   241 	if (B.z > bbox.R.z)  bbox.R.z = B.z;
   249 	if (B.z > bbox.H.z)  bbox.H.z = B.z;
   242 	if (C.z > bbox.R.z)  bbox.R.z = C.z;
   250 	if (C.z > bbox.H.z)  bbox.H.z = C.z;
   243 	return bbox;
   251 	return bbox;
   244 };
   252 };