/** * SetIsectPt * * @param isectpt */ protected void SetIsectPt(IntersectPt isectpt) { t = isectpt.GetT(); Enter = isectpt.GetEnter(); IntersectObj = isectpt.GetIntersectObj(); OriginalObjID = isectpt.GetOriginal(); Intersection.Set(isectpt.GetIntersection().GetX(), isectpt.GetIntersection().GetY(), isectpt.GetIntersection().GetZ()); }
/** * Intersect * * @param ray * @param pt * @return boolean */ public override bool Intersect(Ray ray, IntersectPt pt) { double vd = Normal.Dot(ray.GetDirection()); double vo, t; Vector origVec = new Vector(ray.GetOrigin().GetX(), ray.GetOrigin().GetY(), ray.GetOrigin().GetZ()); if (vd == 0.0f) { return(false); } vo = -Normal.Dot(origVec) - D; t = vo / vd; if (t < pt.GetThreshold()) { return(false); } pt.GetIntersection().Combine(ray.GetOrigin(), ray.GetDirection(), 1.0f, t); if (!Check(ray, pt)) { return(false); } pt.SetT(t); pt.SetIntersectObj(this); if (GetObjID() == pt.GetOriginal()) { pt.SetEnter(false); } else { pt.SetEnter(true); } GetCachePt().Set(ray.GetID(), pt); return(true); }
/** * Check * * @param ray * @param pt * @return boolean */ protected override bool Check (Ray ray, IntersectPt pt) { Vector intersectVec = new Vector (pt.GetIntersection ().GetX (), pt.GetIntersection ().GetY (), pt.GetIntersection ().GetZ ()); double check = S1.Dot (intersectVec); if (check < 0.0f || check > 1.0f) { return (false); } check = S2.Dot (intersectVec); if (check < 0.0f || check > 1.0f) { return (false); } check = S3.Dot (intersectVec); if (check < 0.0f || check > 1.0f) { return (false); } return (true); }
/** * Intersect * * @param ray * @param pt * @return boolean */ public override bool Intersect(Ray ray, IntersectPt pt) { Vector OC = new Vector(); double l2OC, tCA, t2HC; OC.Sub(Origin, ray.GetOrigin()); l2OC = OC.SquaredLength(); tCA = OC.Dot(ray.GetDirection()); if (l2OC >= RadiusSquare && tCA <= 0) { return(false); } t2HC = RadiusSquare - l2OC + tCA * tCA; if (t2HC < 0) { return(false); } if (l2OC <= RadiusSquare) { pt.SetT(tCA + (double)System.Math.Sqrt(t2HC)); if (pt.GetT() < pt.GetThreshold()) { return(false); } pt.SetEnter(false); pt.GetIntersection().Combine(ray.GetOrigin(), ray.GetDirection(), 1.0f, pt.GetT()); } else { pt.SetT(tCA - (double)System.Math.Sqrt(t2HC)); pt.SetEnter(true); if (pt.GetT() < pt.GetThreshold()) { pt.SetT(tCA + (double)System.Math.Sqrt(t2HC)); pt.SetEnter(false); } pt.GetIntersection().Combine(ray.GetOrigin(), ray.GetDirection(), 1.0f, pt.GetT()); } pt.SetIntersectObj(this); GetCachePt().Set(ray.GetID(), pt); return(true); }
/** * Check * * @param ray * @param pt * @return boolean */ protected override bool Check(Ray ray, IntersectPt pt) { Vector intersectVec = new Vector(pt.GetIntersection().GetX(), pt.GetIntersection().GetY(), pt.GetIntersection().GetZ()); double check = S1.Dot(intersectVec); if (check < 0.0f || check > 1.0f) { return(false); } check = S2.Dot(intersectVec); if (check < 0.0f || check > 1.0f) { return(false); } check = S3.Dot(intersectVec); if (check < 0.0f || check > 1.0f) { return(false); } return(true); }
/** * Intersect * * @param ray * @param pt * @return boolean */ public override bool Intersect(Ray ray, IntersectPt pt) { double vd = Normal.Dot(ray.GetDirection()); double vo, t; Vector origVec = new Vector(ray.GetOrigin().GetX(), ray.GetOrigin().GetY(), ray.GetOrigin().GetZ()); if(vd == 0.0f) { return (false); } vo = -Normal.Dot(origVec) - D; t = vo / vd; if(t < pt.GetThreshold()) { return (false); } pt.GetIntersection().Combine(ray.GetOrigin(), ray.GetDirection(), 1.0f, t); if(!Check(ray, pt)) { return (false); } pt.SetT(t); pt.SetIntersectObj(this); if(GetObjID() == pt.GetOriginal()) { pt.SetEnter(false); } else { pt.SetEnter(true); } GetCachePt().Set(ray.GetID(), pt); return (true); }
/** * Intersect * * @param ray * @param pt * @return boolean */ public override bool Intersect(Ray ray, IntersectPt pt) { Vector OC = new Vector(); double l2OC, tCA, t2HC; OC.Sub(Origin, ray.GetOrigin()); l2OC = OC.SquaredLength(); tCA = OC.Dot(ray.GetDirection()); if(l2OC >= RadiusSquare && tCA <= 0) { return (false); } t2HC = RadiusSquare - l2OC + tCA * tCA; if(t2HC < 0) { return (false); } if(l2OC <= RadiusSquare) { pt.SetT(tCA + (double)System.Math.Sqrt(t2HC)); if(pt.GetT() < pt.GetThreshold()) { return (false); } pt.SetEnter(false); pt.GetIntersection().Combine(ray.GetOrigin(), ray.GetDirection(), 1.0f, pt.GetT()); } else { pt.SetT(tCA - (double)System.Math.Sqrt(t2HC)); pt.SetEnter(true); if(pt.GetT() < pt.GetThreshold()) { pt.SetT(tCA + (double)System.Math.Sqrt(t2HC)); pt.SetEnter(false); } pt.GetIntersection().Combine(ray.GetOrigin(), ray.GetDirection(), 1.0f, pt.GetT()); } pt.SetIntersectObj(this); GetCachePt().Set(ray.GetID(), pt); return (true); }
/** * Check * * @param ray * @param pt * @return boolean */ protected override bool Check(Ray ray, IntersectPt pt) { return(InsidePolygon(GetVerts(), GetNumVerts(), pt.GetIntersection(), ray)); }
/** * FindNearestIsect * * @param octree * @param ray * @param originID * @param level * @param isectnode * @return boolean */ public bool FindNearestIsect(OctNode octree, Ray ray, int originID, int level, OctNode isectnode) { Point testpt = new Point(ray.GetOrigin()); OctNode current; ObjNode currentnode; CacheIntersectPt isectptr; IntersectPt test = new IntersectPt(); if(level == 0) { testpt.Combine(ray.GetOrigin(), ray.GetDirection(), 1.0f, Threshold); } current = octree.FindTreeNode(testpt); IntersectObj = null; while(current != null) { currentnode = current.GetList(); while(currentnode != null) { bool found = false; if(currentnode.GetObj().GetCachePt().GetID() == ray.GetID()) { isectptr = currentnode.GetObj().GetCachePt(); if(current == current.FindTreeNode(isectptr.GetIntersection())) { if(IntersectObj == null) { SetIsectPt(isectptr); isectnode.Copy(current); } else { if(isectptr.GetT() < t) { SetIsectPt(isectptr); isectnode.Copy(current); } } found = true; } } if(!found) { test.SetOrigID(originID); if(currentnode.GetObj().Intersect(ray, test)) { if(current == current.FindTreeNode(test.GetIntersection())) { if(IntersectObj == null) { SetIsectPt(test); isectnode.Copy(current); } else { if(test.GetT() < t) { SetIsectPt(test); isectnode.Copy(current); } } } } } currentnode = currentnode.Next(); } if(IntersectObj == null) { OctNode adjacent = current.Intersect(ray, testpt, Threshold); if(adjacent == null) { current = null; } else { current = adjacent.FindTreeNode(testpt); } } else { current = null; } } if(IntersectObj == null) { return (false); } else { return (true); } }
/** * FindNearestIsect * * @param octree * @param ray * @param originID * @param level * @param isectnode * @return boolean */ public bool FindNearestIsect(OctNode octree, Ray ray, int originID, int level, OctNode isectnode) { Point testpt = new Point(ray.GetOrigin()); OctNode current; ObjNode currentnode; CacheIntersectPt isectptr; IntersectPt test = new IntersectPt(); if (level == 0) { testpt.Combine(ray.GetOrigin(), ray.GetDirection(), 1.0f, Threshold); } current = octree.FindTreeNode(testpt); IntersectObj = null; while (current != null) { currentnode = current.GetList(); while (currentnode != null) { bool found = false; if (currentnode.GetObj().GetCachePt().GetID() == ray.GetID()) { isectptr = currentnode.GetObj().GetCachePt(); if (current == current.FindTreeNode(isectptr.GetIntersection())) { if (IntersectObj == null) { SetIsectPt(isectptr); isectnode.Copy(current); } else { if (isectptr.GetT() < t) { SetIsectPt(isectptr); isectnode.Copy(current); } } found = true; } } if (!found) { test.SetOrigID(originID); if (currentnode.GetObj().Intersect(ray, test)) { if (current == current.FindTreeNode(test.GetIntersection())) { if (IntersectObj == null) { SetIsectPt(test); isectnode.Copy(current); } else { if (test.GetT() < t) { SetIsectPt(test); isectnode.Copy(current); } } } } } currentnode = currentnode.Next(); } if (IntersectObj == null) { OctNode adjacent = current.Intersect(ray, testpt, Threshold); if (adjacent == null) { current = null; } else { current = adjacent.FindTreeNode(testpt); } } else { current = null; } } if (IntersectObj == null) { return(false); } else { return(true); } }
/** * Check * * @param ray * @param pt * @return boolean */ protected override bool Check(Ray ray, IntersectPt pt) { return (InsidePolygon(GetVerts(), GetNumVerts(), pt.GetIntersection(), ray)); }
/** * Shade * * @param tree * @param eyeRay * @param color * @param factor * @param level * @param originID */ private void Shade(OctNode tree, Ray eyeRay, Color color, double factor, int level, int originID) { Color lightColor = new Color(0.0f, 0.0f, 0.0f); Color reflectColor = new Color(0.0f, 0.0f, 0.0f); Color refractColor = new Color(0.0f, 0.0f, 0.0f); IntersectPt intersect = new IntersectPt(); OctNode baseoctn = new OctNode(); Vector normal = new Vector(); Ray reflect = new Ray(); Ray refract = new Ray(); double mu; int current; if (intersect.FindNearestIsect(tree, eyeRay, originID, level, baseoctn)) { intersect.GetIntersectObj().FindNormal(intersect.GetIntersection(), normal); GetLightColor(baseoctn, intersect.GetIntersection(), normal, intersect.GetIntersectObj(), lightColor); if (level < MaxLevel) { double check = factor * (1.0f - intersect.GetIntersectObj().GetMaterial().GetKTran()) * intersect.GetIntersectObj().GetMaterial().GetShininess(); if (check > MinFactor) { reflect.SetOrigin(intersect.GetIntersection()); reflect.GetDirection().Combine(eyeRay.GetDirection(), normal, 1.0f, -2.0f * normal.Dot(eyeRay.GetDirection())); reflect.SetID(RayID); this.RayID = this.RayID + 1; Shade(baseoctn, reflect, reflectColor, check, level + 1, originID); reflectColor.Scale((1.0f - intersect.GetIntersectObj().GetMaterial().GetKTran()) * intersect.GetIntersectObj().GetMaterial().GetShininess(), intersect.GetIntersectObj().GetMaterial().GetSpecColor()); } check = factor * intersect.GetIntersectObj().GetMaterial().GetKTran(); if (check > MinFactor) { if (intersect.GetEnter()) { mu = 1.0f / intersect.GetIntersectObj().GetMaterial().GetRefIndex(); current = intersect.GetIntersectObj().GetObjID(); } else { mu = intersect.GetIntersectObj().GetMaterial().GetRefIndex(); normal.Negate(); current = 0; } double IdotN = normal.Dot(eyeRay.GetDirection()); double TotIntReflect = 1.0f - mu * mu * (1.0f - IdotN * IdotN); if (TotIntReflect >= 0.0) { double gamma = -mu * IdotN - (double)Math.Sqrt(TotIntReflect); refract.SetOrigin(intersect.GetIntersection()); refract.GetDirection().Combine(eyeRay.GetDirection(), normal, mu, gamma); refract.SetID(RayID); this.RayID = RayID + 1; Shade(baseoctn, refract, refractColor, check, level + 1, current); refractColor.Scale(intersect.GetIntersectObj().GetMaterial().GetKTran(), intersect.GetIntersectObj().GetMaterial().GetSpecColor()); } } } color.Combine(intersect.GetIntersectObj().GetMaterial().GetEmissColor(), intersect.GetIntersectObj().GetMaterial().GetAmbColor(), AmbLightIntensity, lightColor, reflectColor, refractColor); } }
/** * Shade * * @param tree * @param eyeRay * @param color * @param factor * @param level * @param originID */ private void Shade(OctNode tree, Ray eyeRay, Color color, double factor, int level, int originID) { Color lightColor = new Color(0.0f, 0.0f, 0.0f); Color reflectColor = new Color(0.0f, 0.0f, 0.0f); Color refractColor = new Color(0.0f, 0.0f, 0.0f); IntersectPt intersect = new IntersectPt(); OctNode baseoctn = new OctNode(); Vector normal = new Vector(); Ray reflect = new Ray(); Ray refract = new Ray(); double mu; int current; if(intersect.FindNearestIsect(tree, eyeRay, originID, level, baseoctn)) { intersect.GetIntersectObj().FindNormal(intersect.GetIntersection(), normal); GetLightColor(baseoctn, intersect.GetIntersection(), normal, intersect.GetIntersectObj(), lightColor); if(level < MaxLevel) { double check = factor * (1.0f - intersect.GetIntersectObj().GetMaterial().GetKTran()) * intersect.GetIntersectObj().GetMaterial().GetShininess(); if(check > MinFactor) { reflect.SetOrigin(intersect.GetIntersection()); reflect.GetDirection().Combine(eyeRay.GetDirection(), normal, 1.0f, -2.0f * normal.Dot(eyeRay.GetDirection())); reflect.SetID(RayID); this.RayID = this.RayID + 1; Shade(baseoctn, reflect, reflectColor, check, level + 1, originID); reflectColor.Scale((1.0f - intersect.GetIntersectObj().GetMaterial().GetKTran()) * intersect.GetIntersectObj().GetMaterial().GetShininess(), intersect.GetIntersectObj().GetMaterial().GetSpecColor()); } check = factor * intersect.GetIntersectObj().GetMaterial().GetKTran(); if(check > MinFactor) { if(intersect.GetEnter()) { mu = 1.0f / intersect.GetIntersectObj().GetMaterial().GetRefIndex(); current = intersect.GetIntersectObj().GetObjID(); } else { mu = intersect.GetIntersectObj().GetMaterial().GetRefIndex(); normal.Negate(); current = 0; } double IdotN = normal.Dot(eyeRay.GetDirection()); double TotIntReflect = 1.0f - mu * mu * (1.0f - IdotN * IdotN); if(TotIntReflect >= 0.0) { double gamma = -mu * IdotN - (double)Math.Sqrt(TotIntReflect); refract.SetOrigin(intersect.GetIntersection()); refract.GetDirection().Combine(eyeRay.GetDirection(), normal, mu, gamma); refract.SetID(RayID); this.RayID = RayID + 1; Shade(baseoctn, refract, refractColor, check, level + 1, current); refractColor.Scale(intersect.GetIntersectObj().GetMaterial().GetKTran(), intersect.GetIntersectObj().GetMaterial().GetSpecColor()); } } } color.Combine(intersect.GetIntersectObj().GetMaterial().GetEmissColor(), intersect.GetIntersectObj().GetMaterial().GetAmbColor(), AmbLightIntensity, lightColor, reflectColor, refractColor); } }