src/scene.cc
branchpyrit
changeset 78 9569e9f35374
parent 69 303583d2fb97
child 86 ce6abe0aeeae
equal deleted inserted replaced
77:dbe8438d5dca 78:9569e9f35374
    22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    24  * THE SOFTWARE.
    24  * THE SOFTWARE.
    25  */
    25  */
    26 
    26 
    27 #include <math.h>
       
    28 
       
    29 #include "common.h"
       
    30 #include "scene.h"
    27 #include "scene.h"
    31 
    28 
    32 void Camera::rotate(const Quaternion &q)
    29 void Camera::rotate(const Quaternion &q)
    33 {
    30 {
    34 	/*
    31 	/*
   108 
   105 
   109 	a = tnear;
   106 	a = tnear;
   110 	b = tfar;
   107 	b = tfar;
   111 	return true;
   108 	return true;
   112 }
   109 }
   113 
       
   114 bool Sphere::intersect(const Ray &ray, Float &dist) const
       
   115 {
       
   116 	Vector3 V = ray.o - center;
       
   117 	register Float d = -dot(V, ray.dir);
       
   118 	register Float Det = d * d - (dot(V,V) - sqr_radius);
       
   119 	register Float t1,t2;
       
   120 	if (Det > 0) {
       
   121 		Det = sqrtf(Det);
       
   122 		t1 = d - Det;
       
   123 		t2 = d + Det;
       
   124 		if (t1 > 0)
       
   125 		{
       
   126 			if (t1 < dist)
       
   127 			{
       
   128 				dist = t1;
       
   129 				return true;
       
   130 			}
       
   131 		}
       
   132 		else if (t2 > 0 && t2 < dist)
       
   133 		{
       
   134 			dist = t2;
       
   135 			return true;
       
   136 		}
       
   137 	}
       
   138 	return false;
       
   139 }
       
   140 
       
   141 /* if there should be CSG sometimes, this may be needed... */
       
   142 bool Sphere::intersect_all(const Ray &ray, Float dist, vector<Float> &allts) const
       
   143 {
       
   144 	//allts = new vector<Float>();
       
   145 
       
   146 	Vector3 V = ((Ray)ray).o - center;
       
   147 	Float Vd = - dot(V, ray.dir);
       
   148 	Float Det = Vd * Vd - (dot(V,V) - sqr_radius);
       
   149 
       
   150 	if (Det > 0) {
       
   151 		Det = sqrtf(Det);
       
   152 		Float t1 = Vd - Det;
       
   153 		Float t2 = Vd + Det;
       
   154 		if (t1 < 0)
       
   155 		{
       
   156 			if (t2 > 0)
       
   157 			{
       
   158 				// ray from inside of the sphere
       
   159 				allts.push_back(0.0);
       
   160 				allts.push_back(t2);
       
   161 				return true;
       
   162 			}
       
   163 			else
       
   164 				return false;
       
   165 		}
       
   166 		else
       
   167 		{
       
   168 			allts.push_back(t1);
       
   169 			allts.push_back(t2);
       
   170 			return true;
       
   171 		}
       
   172 	}
       
   173 	return false;
       
   174 }
       
   175 
       
   176 bool Sphere::intersect_bbox(const BBox &bbox) const
       
   177 {
       
   178 	register float dmin = 0;
       
   179 	for (int i = 0; i < 3; i++)
       
   180 	{
       
   181 		if (center[i] < bbox.L[i])
       
   182 			dmin += (center[i] - bbox.L[i])*(center[i] - bbox.L[i]);
       
   183 		else
       
   184 		if (center[i] > bbox.H[i])
       
   185 			dmin += (center[i] - bbox.H[i])*(center[i] - bbox.H[i]);
       
   186 	}
       
   187 	if (dmin <= sqr_radius)
       
   188 		return true;
       
   189 	return false;
       
   190 };
       
   191 
       
   192 BBox Sphere::get_bbox() const
       
   193 {
       
   194 	return BBox(center - radius, center + radius);
       
   195 }
       
   196 
       
   197 bool Box::intersect(const Ray &ray, Float &dist) const
       
   198 {
       
   199 	register Float tnear = -Inf;
       
   200 	register Float tfar = Inf;
       
   201 	register Float t1, t2;
       
   202 
       
   203 	for (int i = 0; i < 3; i++)
       
   204 	{
       
   205 		if (ray.dir[i] == 0) {
       
   206 			/* ray is parallel to these planes */
       
   207 			if (ray.o[i] < L[i] || ray.o[i] > H[i])
       
   208 				return false;
       
   209 		}
       
   210 		else
       
   211 		{
       
   212 			/* compute the intersection distance of the planes */
       
   213 			t1 = (L[i] - ray.o[i]) / ray.dir[i];
       
   214 			t2 = (H[i] - ray.o[i]) / ray.dir[i];
       
   215 
       
   216 			if (t1 > t2)
       
   217 				swap(t1, t2);
       
   218 
       
   219 			if (t1 > tnear)
       
   220 				tnear = t1; /* want largest Tnear */
       
   221 			if (t2 < tfar)
       
   222 				tfar = t2; /* want smallest Tfar */
       
   223 			if (tnear > tfar || tfar < 0)
       
   224 				return false; /* box missed; box is behind ray */
       
   225 		}
       
   226 	}
       
   227 
       
   228 	if (tnear < dist)
       
   229 	{
       
   230 		dist = tnear;
       
   231 		return true;
       
   232 	}
       
   233 	return false;
       
   234 }
       
   235 
       
   236 bool Box::intersect_bbox(const BBox &bbox) const
       
   237 {
       
   238 	return (
       
   239 	H.x > bbox.L.x && L.x < bbox.H.x &&
       
   240 	H.y > bbox.L.y && L.y < bbox.H.y &&
       
   241 	H.z > bbox.L.z && L.z < bbox.H.z);
       
   242 }
       
   243 
       
   244 const Vector3 Box::normal(const Vector3 &P) const
       
   245 {
       
   246 	register Vector3 l = P - L;
       
   247 	register Vector3 h = H - P;
       
   248 
       
   249 	if (l.x < h.x)
       
   250 		h.x = -1;
       
   251 	else
       
   252 	{
       
   253 		l.x = h.x;
       
   254 		h.x = +1;
       
   255 	}
       
   256 
       
   257 	if (l.y < h.y)
       
   258 		h.y = -1;
       
   259 	else
       
   260 	{
       
   261 		l.y = h.y;
       
   262 		h.y = +1;
       
   263 	}
       
   264 
       
   265 	if (l.z < h.z)
       
   266 		h.z = -1;
       
   267 	else
       
   268 	{
       
   269 		l.z = h.z;
       
   270 		h.z = +1;
       
   271 	}
       
   272 
       
   273 	if (l.x > l.y)
       
   274 	{
       
   275 		h.x = 0;
       
   276 		if (l.y > l.z)
       
   277 			h.y = 0;
       
   278 		else
       
   279 			h.z = 0;
       
   280 	}
       
   281 	else
       
   282 	{
       
   283 		h.y = 0;
       
   284 		if (l.x > l.z)
       
   285 			h.x = 0;
       
   286 		else
       
   287 			h.z = 0;
       
   288 	}
       
   289 	return h;
       
   290 }
       
   291 
       
   292 #ifdef TRI_PLUCKER
       
   293 inline void Plucker(const Vector3 &p, const Vector3 &q, Float* pl)
       
   294 {
       
   295     pl[0] = p.x*q.y - q.x*p.y;
       
   296     pl[1] = p.x*q.z - q.x*p.z;
       
   297     pl[2] = p.x - q.x;
       
   298     pl[3] = p.y*q.z - q.y*p.z;
       
   299     pl[4] = p.z - q.z;
       
   300     pl[5] = q.y - p.y;
       
   301 }
       
   302 
       
   303 inline Float Side(const Float* pla, const Float* plb)
       
   304 {
       
   305     return pla[0]*plb[4] + pla[1]*plb[5] + pla[2]*plb[3] + pla[4]*plb[0] + pla[5]*plb[1] + pla[3]*plb[2];
       
   306 }
       
   307 #endif
       
   308 
       
   309 Triangle::Triangle(Vertex *aA, Vertex *aB, Vertex *aC, Material *amaterial)
       
   310 	: A(aA), B(aB), C(aC)
       
   311 {
       
   312 	material = amaterial;
       
   313 
       
   314 	const Vector3 c = B->P - A->P;
       
   315 	const Vector3 b = C->P - A->P;
       
   316 
       
   317 	N = cross(c, b);
       
   318 	N.normalize();
       
   319 
       
   320 #ifdef TRI_PLUCKER
       
   321 	Plucker(B->P,C->P,pla);
       
   322 	Plucker(C->P,A->P,plb);
       
   323 	Plucker(A->P,B->P,plc);
       
   324 #endif
       
   325 
       
   326 #if defined(TRI_BARI) || defined(TRI_BARI_PRE)
       
   327 	if (fabsf(N.x) > fabsf(N.y))
       
   328 	{
       
   329 		if (fabsf(N.x) > fabsf(N.z))
       
   330 			k = 0;
       
   331 		else
       
   332 			k = 2;
       
   333 	}
       
   334 	else
       
   335 	{
       
   336 		if (fabsf(N.y) > fabsf(N.z))
       
   337 			k = 1;
       
   338 		else
       
   339 			k = 2;
       
   340 	}
       
   341 #endif
       
   342 #ifdef TRI_BARI_PRE
       
   343 	int u = (k + 1) % 3;
       
   344 	int v = (k + 2) % 3;
       
   345 
       
   346 	Float krec = 1.0 / N[k];
       
   347 	nu = N[u] * krec;
       
   348 	nv = N[v] * krec;
       
   349 	nd = dot(N, A->P) * krec;
       
   350 
       
   351 	// first line equation
       
   352 	Float reci = 1.0f / (b[u] * c[v] - b[v] * c[u]);
       
   353 	bnu = b[u] * reci;
       
   354 	bnv = -b[v] * reci;
       
   355 
       
   356 	// second line equation
       
   357 	cnu = -c[u] * reci;
       
   358 	cnv = c[v] * reci;
       
   359 #endif
       
   360 }
       
   361 
       
   362 bool Triangle::intersect(const Ray &ray, Float &dist) const
       
   363 {
       
   364 #ifdef TRI_PLUCKER
       
   365 	Float plr[6];
       
   366 	Plucker(ray.o, ray.o+ray.dir, plr);
       
   367 	const bool side0 = Side(plr, pla) >= 0.0;
       
   368 	const bool side1 = Side(plr, plb) >= 0.0;
       
   369 	if (side0 != side1)
       
   370 		return false;
       
   371 	const bool side2 = Side(plr, plc) >= 0.0;
       
   372 	if (side0 != side2)
       
   373 		return false;
       
   374 	const Float t = - dot( (ray.o - A->P), N) / dot(ray.dir,N);
       
   375 	if(t <= Eps || t >= dist)
       
   376 		return false;
       
   377 	dist = t;
       
   378 	return true;
       
   379 #endif
       
   380 
       
   381 #if defined(TRI_BARI) || defined(TRI_BARI_PRE)
       
   382 	static const int modulo3[5] = {0,1,2,0,1};
       
   383 	const Vector3 &O = ray.o;
       
   384 	const Vector3 &D = ray.dir;
       
   385 	register const int u = modulo3[k+1];
       
   386 	register const int v = modulo3[k+2];
       
   387 #endif
       
   388 #ifdef TRI_BARI_PRE
       
   389 	const Float t = (nd - O[k] - nu * O[u] - nv * O[v]) / (D[k] + nu * D[u] + nv * D[v]);
       
   390 
       
   391 	if (t >= dist || t < Eps)
       
   392 		return false;
       
   393 
       
   394 	const Float hu = O[u] + t * D[u] - A->P[u];
       
   395 	const Float hv = O[v] + t * D[v] - A->P[v];
       
   396 	const Float beta = hv * bnu + hu * bnv;
       
   397 
       
   398 	if (beta < 0.)
       
   399 		return false;
       
   400 
       
   401 	const Float gamma = hu * cnv + hv * cnu;
       
   402 	if (gamma < 0. || beta + gamma > 1.)
       
   403 		return false;
       
   404 
       
   405 	dist = t;
       
   406 	return true;
       
   407 #endif
       
   408 
       
   409 #ifdef TRI_BARI
       
   410 	// original barycentric coordinates based intesection
       
   411 	// not optimized, just for reference
       
   412 	const Vector3 c = B - A;
       
   413 	const Vector3 b = C - A;
       
   414 	// distance test
       
   415 	const Float t = - dot( (O-A), N) / dot(D,N);
       
   416 	if (t < Eps || t > dist)
       
   417 		return false;
       
   418 
       
   419 	// calc hitpoint
       
   420 	const Float Hu = O[u] + t * D[u] - A[u];
       
   421 	const Float Hv = O[v] + t * D[v] - A[v];
       
   422 	const Float beta = (b[u] * Hv - b[v] * Hu) / (b[u] * c[v] - b[v] * c[u]);
       
   423 	if (beta < 0)
       
   424 		return false;
       
   425 	const Float gamma = (c[v] * Hu - c[u] * Hv) / (b[u] * c[v] - b[v] * c[u]);
       
   426 	if (gamma < 0)
       
   427 		return false;
       
   428 	if (beta+gamma > 1)
       
   429 		return false;
       
   430 	dist = t;
       
   431 	return true;
       
   432 #endif
       
   433 }
       
   434 
       
   435 bool Triangle::intersect_bbox(const BBox &bbox) const
       
   436 {
       
   437 	const Vector3 boxcenter = (bbox.L+bbox.H)*0.5;
       
   438 	const Vector3 boxhalfsize = (bbox.H-bbox.L)*0.5;
       
   439 	const Vector3 v0 = A->P - boxcenter;
       
   440 	const Vector3 v1 = B->P - boxcenter;
       
   441 	const Vector3 v2 = C->P - boxcenter;
       
   442 	const Vector3 e0 = v1-v0;
       
   443 	const Vector3 e1 = v2-v1;
       
   444 	const Vector3 e2 = v0-v2;
       
   445 
       
   446 	Float fex = fabsf(e0.x);
       
   447 	Float fey = fabsf(e0.y);
       
   448 	Float fez = fabsf(e0.z);
       
   449 
       
   450 	Float p0,p1,p2,min,max,rad;
       
   451 
       
   452 	p0 = e0.z*v0.y - e0.y*v0.z;
       
   453 	p2 = e0.z*v2.y - e0.y*v2.z;
       
   454 	if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;}
       
   455 	rad = fez * boxhalfsize.y + fey * boxhalfsize.z;
       
   456 	if(min>rad || max<-rad) return false;
       
   457 
       
   458 	p0 = -e0.z*v0.x + e0.x*v0.z;
       
   459 	p2 = -e0.z*v2.x + e0.x*v2.z;
       
   460 	if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;}
       
   461 	rad = fez * boxhalfsize.x + fex * boxhalfsize.z;
       
   462 	if(min>rad || max<-rad) return false;
       
   463 
       
   464 	p1 = e0.y*v1.x - e0.x*v1.y;
       
   465 	p2 = e0.y*v2.x - e0.x*v2.y;
       
   466 	if(p2<p1) {min=p2; max=p1;} else {min=p1; max=p2;}
       
   467 	rad = fey * boxhalfsize.x + fex * boxhalfsize.y;
       
   468 	if(min>rad || max<-rad) return false;
       
   469 
       
   470 	fex = fabsf(e1.x);
       
   471 	fey = fabsf(e1.y);
       
   472 	fez = fabsf(e1.z);
       
   473 
       
   474 	p0 = e1.z*v0.y - e1.y*v0.z;
       
   475 	p2 = e1.z*v2.y - e1.y*v2.z;
       
   476 	if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;}
       
   477 	rad = fez * boxhalfsize.y + fey * boxhalfsize.z;
       
   478 	if(min>rad || max<-rad) return false;
       
   479 
       
   480 	p0 = -e1.z*v0.x + e1.x*v0.z;
       
   481 	p2 = -e1.z*v2.x + e1.x*v2.z;
       
   482 	if(p0<p2) {min=p0; max=p2;} else {min=p2; max=p0;}
       
   483 	rad = fez * boxhalfsize.x + fex * boxhalfsize.z;
       
   484 	if(min>rad || max<-rad) return false;
       
   485 
       
   486 	p0 = e1.y*v0.x - e1.x*v0.y;
       
   487 	p1 = e1.y*v1.x - e1.x*v1.y;
       
   488 	if(p0<p1) {min=p0; max=p1;} else {min=p1; max=p0;}
       
   489 	rad = fey * boxhalfsize.x + fex * boxhalfsize.y;
       
   490 	if(min>rad || max<-rad) return false;
       
   491 
       
   492 	fex = fabsf(e2.x);
       
   493 	fey = fabsf(e2.y);
       
   494 	fez = fabsf(e2.z);
       
   495 
       
   496 	p0 = e2.z*v0.y - e2.y*v0.z;
       
   497 	p1 = e2.z*v1.y - e2.y*v1.z;
       
   498 	if(p0<p1) {min=p0; max=p1;} else {min=p1; max=p0;}
       
   499 	rad = fez * boxhalfsize.y + fey * boxhalfsize.z;
       
   500 	if(min>rad || max<-rad) return false;
       
   501 
       
   502 	p0 = -e2.z*v0.x + e2.x*v0.z;
       
   503 	p1 = -e2.z*v1.x + e2.x*v1.z;
       
   504 	if(p0<p1) {min=p0; max=p1;} else {min=p1; max=p0;}
       
   505 	rad = fez * boxhalfsize.x + fex * boxhalfsize.z;
       
   506 	if(min>rad || max<-rad) return false;
       
   507 
       
   508 	p1 = e2.y*v1.x - e2.x*v1.y;
       
   509 	p2 = e2.y*v2.x - e2.x*v2.y;
       
   510 	if(p2<p1) {min=p2; max=p1;} else {min=p1; max=p2;}
       
   511 	rad = fey * boxhalfsize.x + fex * boxhalfsize.y;
       
   512 	if(min>rad || max<-rad) return false;
       
   513 
       
   514 	/* test overlap in the {x,y,z}-directions */
       
   515 	/* test in X-direction */
       
   516 	min = v0.x;
       
   517 	if (v1.x < min) min = v1.x;
       
   518 	if (v2.x < min) min = v2.x;
       
   519 	max = v0.x;
       
   520 	if (v1.x > max) max = v1.x;
       
   521 	if (v2.x > max) max = v2.x;
       
   522 	if(min>boxhalfsize.x || max<-boxhalfsize.x) return false;
       
   523 
       
   524 	/* test in Y-direction */
       
   525 	min = v0.y;
       
   526 	if (v1.y < min) min = v1.y;
       
   527 	if (v2.y < min) min = v2.y;
       
   528 	max = v0.y;
       
   529 	if (v1.y > max) max = v1.y;
       
   530 	if (v2.y > max) max = v2.y;
       
   531 	if(min>boxhalfsize.y || max<-boxhalfsize.y) return false;
       
   532 
       
   533 	/* test in Z-direction */
       
   534 	min = v0.z;
       
   535 	if (v1.z < min) min = v1.z;
       
   536 	if (v2.z < min) min = v2.z;
       
   537 	max = v0.z;
       
   538 	if (v1.z > max) max = v1.z;
       
   539 	if (v2.z > max) max = v2.z;
       
   540 	if(min>boxhalfsize.z || max<-boxhalfsize.z) return false;
       
   541 
       
   542 	/*  test if the box intersects the plane of the triangle */
       
   543 	Vector3 vmin,vmax;
       
   544 	Float v;
       
   545 	for(int q=0;q<3;q++)
       
   546 	{
       
   547 		v=v0[q];
       
   548 		if(N[q]>0.0f)
       
   549 		{
       
   550 			vmin.cell[q]=-boxhalfsize[q] - v;
       
   551 			vmax.cell[q]= boxhalfsize[q] - v;
       
   552 		}
       
   553 		else
       
   554 		{
       
   555 			vmin.cell[q]= boxhalfsize[q] - v;
       
   556 			vmax.cell[q]=-boxhalfsize[q] - v;
       
   557 		}
       
   558 	}
       
   559 	if(dot(N,vmin)>0.0f) return false;
       
   560 	if(dot(N,vmax)>=0.0f) return true;
       
   561 
       
   562 	return false;
       
   563 }
       
   564 
       
   565 BBox Triangle::get_bbox() const
       
   566 {
       
   567 	BBox bbox = BBox();
       
   568 	bbox.L = A->P;
       
   569 	if (B->P.x < bbox.L.x)  bbox.L.x = B->P.x;
       
   570 	if (C->P.x < bbox.L.x)  bbox.L.x = C->P.x;
       
   571 	if (B->P.y < bbox.L.y)  bbox.L.y = B->P.y;
       
   572 	if (C->P.y < bbox.L.y)  bbox.L.y = C->P.y;
       
   573 	if (B->P.z < bbox.L.z)  bbox.L.z = B->P.z;
       
   574 	if (C->P.z < bbox.L.z)  bbox.L.z = C->P.z;
       
   575 	bbox.H = A->P;
       
   576 	if (B->P.x > bbox.H.x)  bbox.H.x = B->P.x;
       
   577 	if (C->P.x > bbox.H.x)  bbox.H.x = C->P.x;
       
   578 	if (B->P.y > bbox.H.y)  bbox.H.y = B->P.y;
       
   579 	if (C->P.y > bbox.H.y)  bbox.H.y = C->P.y;
       
   580 	if (B->P.z > bbox.H.z)  bbox.H.z = B->P.z;
       
   581 	if (C->P.z > bbox.H.z)  bbox.H.z = C->P.z;
       
   582 	return bbox;
       
   583 };