src/scene.cc
branchpyrit
changeset 28 ffe83ca074f3
parent 25 b8232edee786
child 31 b4e09433934a
equal deleted inserted replaced
27:e9bb83c2b8b9 28:ffe83ca074f3
   163 	}
   163 	}
   164 	else
   164 	else
   165 		return false;
   165 		return false;
   166 }
   166 }
   167 
   167 
   168 Vector3 Box::normal(Vector3 &P) const
   168 const Vector3 Box::normal(const Vector3 &P) const
   169 {
   169 {
   170 	Vector3 N;
   170 	Vector3 N;
   171 	for (int i = 0; i < 3; i++)
   171 	for (int i = 0; i < 3; i++)
   172 	{
   172 	{
   173 		if (P.cell[i] >= L.cell[i]-Eps && P.cell[i] <= L.cell[i]+Eps)
   173 		if (P.cell[i] >= L.cell[i]-Eps && P.cell[i] <= L.cell[i]+Eps)
   201 {
   201 {
   202     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];
   202     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];
   203 }
   203 }
   204 #endif
   204 #endif
   205 
   205 
   206 Triangle::Triangle(const Vector3 &aA, const Vector3 &aB, const Vector3 &aC, Material *amaterial)
   206 Triangle::Triangle(Vertex *aA, Vertex *aB, Vertex *aC, Material *amaterial)
   207 	: A(aA), B(aB), C(aC)
   207 	: smooth(false), A(aA), B(aB), C(aC)
   208 {
   208 {
   209 	material = amaterial;
   209 	material = amaterial;
   210 	material->reflection = 0;
   210 	material->reflection = 0;
   211 
   211 
   212 	const Vector3 c = B - A;
   212 	const Vector3 c = B->P - A->P;
   213 	const Vector3 b = C - A;
   213 	const Vector3 b = C->P - A->P;
   214 
   214 
   215 	N = -cross(c, b);
   215 	N = cross(c, b);
   216 	N.normalize();
   216 	N.normalize();
   217 
   217 
   218 #ifdef TRI_PLUCKER
   218 #ifdef TRI_PLUCKER
   219 	Plucker(B,C,pla);
   219 	Plucker(B->P,C->P,pla);
   220 	Plucker(C,A,plb);
   220 	Plucker(C->P,A->P,plb);
   221 	Plucker(A,B,plc);
   221 	Plucker(A->P,B->P,plc);
   222 #endif
   222 #endif
   223 
   223 
   224 #if defined(TRI_BARI) || defined(TRI_BARI_PRE)
   224 #if defined(TRI_BARI) || defined(TRI_BARI_PRE)
   225 	if (fabsf(N.x) > fabsf(N.y))
   225 	if (fabsf(N.x) > fabsf(N.y))
   226 	{
   226 	{
   242 	int v = (k + 2) % 3;
   242 	int v = (k + 2) % 3;
   243 
   243 
   244 	Float krec = 1.0 / N[k];
   244 	Float krec = 1.0 / N[k];
   245 	nu = N[u] * krec;
   245 	nu = N[u] * krec;
   246 	nv = N[v] * krec;
   246 	nv = N[v] * krec;
   247 	nd = dot(N, A) * krec;
   247 	nd = dot(N, A->P) * krec;
   248 
   248 
   249 	// first line equation
   249 	// first line equation
   250 	Float reci = 1.0f / (b[u] * c[v] - b[v] * c[u]);
   250 	Float reci = 1.0f / (b[u] * c[v] - b[v] * c[u]);
   251 	bnu = b[u] * reci;
   251 	bnu = b[u] * reci;
   252 	bnv = -b[v] * reci;
   252 	bnv = -b[v] * reci;
   267 	if (side0 != side1)
   267 	if (side0 != side1)
   268 		return false;
   268 		return false;
   269 	const bool side2 = Side(plr, plc) >= 0.0;
   269 	const bool side2 = Side(plr, plc) >= 0.0;
   270 	if (side0 != side2)
   270 	if (side0 != side2)
   271 		return false;
   271 		return false;
   272 	const Float t = - dot( (ray.o-A), N) / dot(ray.dir,N);
   272 	const Float t = - dot( (ray.o - A->P), N) / dot(ray.dir,N);
   273 	if(t <= Eps || t >= dist)
   273 	if(t <= Eps || t >= dist)
   274 		return false;
   274 		return false;
   275 	dist = t;
   275 	dist = t;
   276 	return true;
   276 	return true;
   277 #endif
   277 #endif
   287 	const Float t = (nd - O[k] - nu * O[u] - nv * O[v]) / (D[k] + nu * D[u] + nv * D[v]);
   287 	const Float t = (nd - O[k] - nu * O[u] - nv * O[v]) / (D[k] + nu * D[u] + nv * D[v]);
   288 
   288 
   289 	if (t >= dist || t < Eps)
   289 	if (t >= dist || t < Eps)
   290 		return false;
   290 		return false;
   291 
   291 
   292 	const Float hu = O[u] + t * D[u] - A[u];
   292 	const Float hu = O[u] + t * D[u] - A->P[u];
   293 	const Float hv = O[v] + t * D[v] - A[v];
   293 	const Float hv = O[v] + t * D[v] - A->P[v];
   294 	const Float beta = hv * bnu + hu * bnv;
   294 	const Float beta = hv * bnu + hu * bnv;
   295 
   295 
   296 	if (beta < 0.)
   296 	if (beta < 0.)
   297 		return false;
   297 		return false;
   298 
   298 
   299 	const Float gamma = hu * cnv + hv * cnu;
   299 	const Float gamma = hu * cnv + hv * cnu;
   300 	if (gamma < 0. || beta + gamma > 1)
   300 	if (gamma < 0. || beta + gamma > 1.)
   301 		return false;
   301 		return false;
   302 
   302 
   303 	dist = t;
   303 	dist = t;
   304 	return true;
   304 	return true;
   305 #endif
   305 #endif
   331 }
   331 }
   332 
   332 
   333 BBox Triangle::get_bbox() const
   333 BBox Triangle::get_bbox() const
   334 {
   334 {
   335 	BBox bbox = BBox();
   335 	BBox bbox = BBox();
   336 	bbox.L = A;
   336 	bbox.L = A->P;
   337 	if (B.x < bbox.L.x)  bbox.L.x = B.x;
   337 	if (B->P.x < bbox.L.x)  bbox.L.x = B->P.x;
   338 	if (C.x < bbox.L.x)  bbox.L.x = C.x;
   338 	if (C->P.x < bbox.L.x)  bbox.L.x = C->P.x;
   339 	if (B.y < bbox.L.y)  bbox.L.y = B.y;
   339 	if (B->P.y < bbox.L.y)  bbox.L.y = B->P.y;
   340 	if (C.y < bbox.L.y)  bbox.L.y = C.y;
   340 	if (C->P.y < bbox.L.y)  bbox.L.y = C->P.y;
   341 	if (B.z < bbox.L.z)  bbox.L.z = B.z;
   341 	if (B->P.z < bbox.L.z)  bbox.L.z = B->P.z;
   342 	if (C.z < bbox.L.z)  bbox.L.z = C.z;
   342 	if (C->P.z < bbox.L.z)  bbox.L.z = C->P.z;
   343 	bbox.H = A;
   343 	bbox.H = A->P;
   344 	if (B.x > bbox.H.x)  bbox.H.x = B.x;
   344 	if (B->P.x > bbox.H.x)  bbox.H.x = B->P.x;
   345 	if (C.x > bbox.H.x)  bbox.H.x = C.x;
   345 	if (C->P.x > bbox.H.x)  bbox.H.x = C->P.x;
   346 	if (B.y > bbox.H.y)  bbox.H.y = B.y;
   346 	if (B->P.y > bbox.H.y)  bbox.H.y = B->P.y;
   347 	if (C.y > bbox.H.y)  bbox.H.y = C.y;
   347 	if (C->P.y > bbox.H.y)  bbox.H.y = C->P.y;
   348 	if (B.z > bbox.H.z)  bbox.H.z = B.z;
   348 	if (B->P.z > bbox.H.z)  bbox.H.z = B->P.z;
   349 	if (C.z > bbox.H.z)  bbox.H.z = C.z;
   349 	if (C->P.z > bbox.H.z)  bbox.H.z = C->P.z;
   350 	return bbox;
   350 	return bbox;
   351 };
   351 };