/** * FindLightBlock * * @param tree * @param ray * @param maxt * @return boolean */ private bool FindLightBlock(OctNode tree, Ray ray, double maxt) { OctNode current = tree.FindTreeNode(ray.GetOrigin()); IntersectPt test = new IntersectPt(); Point testpt = new Point(); while (current != null) { ObjNode currentnode = current.GetList(); while (currentnode != null) { bool found = false; if (currentnode.GetObj().GetCachePt().GetID() == ray.GetID()) { found = true; } if (!found) { test.SetOrigID(0); if (currentnode.GetObj().Intersect(ray, test)) { if (test.GetT() < maxt) { return(true); } } } currentnode = currentnode.Next(); } OctNode adjacent = current.Intersect(ray, testpt, test.GetThreshold()); if (adjacent == null) { current = null; } else { current = adjacent.FindTreeNode(testpt); } } return(false); }
/// <summary> /// Merges vertices /// </summary> public void MergeVertices(float tolerance) { OctNode root = null; List <MeshVertex> oldVertices = new List <MeshVertex>(Vertices); Vertices.Clear(); for (int i = 0; i < Triangles.Count; i++) { MeshVertex v0 = oldVertices[Triangles[i].Index0]; MeshVertex v1 = oldVertices[Triangles[i].Index1]; MeshVertex v2 = oldVertices[Triangles[i].Index2]; int i0 = InsertVertex(ref root, v0, tolerance); int i1 = InsertVertex(ref root, v1, tolerance); int i2 = InsertVertex(ref root, v2, tolerance); int mtrl = Triangles[i].MaterialIndex; Triangles[i] = new MeshTriangle(i0, i1, i2, mtrl); } }
/** * Intersect * * @param ray * @param intersect * @param Threshold * @return OctNode */ public OctNode Intersect(Ray ray, Point intersect, double Threshold) { Vector delta = new Vector(0.0f, 0.0f, 0.0f); double current = 0.0f; double t; int[] facehits = new int[3]; facehits [0] = -1; facehits [1] = -1; facehits [2] = -1; OctNode adjacent = null; Face[] OFaces = this.OctFaces; Face MAXXF = OFaces [MAXX]; Face MAXYF = OFaces [MAXY]; Face MAXZF = OFaces [MAXZ]; Face MINXF = OFaces [MINX]; Face MINYF = OFaces [MINY]; Face MINZF = OFaces [MINZ]; if (ray.GetDirection().GetX() != 0.0) { t = -(ray.GetOrigin().GetX() - OctFaces [MAXX].GetVert(0).GetX()) / ray.GetDirection().GetX(); if (t > Threshold && t > current) { intersect.Combine(ray.GetOrigin(), ray.GetDirection(), 1.0f, t); if ((intersect.GetY() <= MAXYF.GetVert(0).GetY()) && (intersect.GetY() >= MINYF.GetVert(0).GetY()) && (intersect.GetZ() <= MAXZF.GetVert(0).GetZ()) && (intersect.GetZ() >= MINZF.GetVert(0).GetZ())) { current = t; facehits [0] = MAXX; delta.SetX(Threshold); } } t = -(ray.GetOrigin().GetX() - OctFaces [MINX].GetVert(0).GetX()) / ray.GetDirection().GetX(); if (t > Threshold && t > current) { intersect.Combine(ray.GetOrigin(), ray.GetDirection(), 1.0f, t); if ((intersect.GetY() <= MAXYF.GetVert(0).GetY()) && (intersect.GetY() >= MINYF.GetVert(0).GetY()) && (intersect.GetZ() <= MAXZF.GetVert(0).GetZ()) && (intersect.GetZ() >= MINZF.GetVert(0).GetZ())) { current = t; facehits [0] = MINX; delta.SetX(-Threshold); } } } if (ray.GetDirection().GetY() != 0.0) { t = -(ray.GetOrigin().GetY() - OctFaces [MAXY].GetVert(0).GetY()) / ray.GetDirection().GetY(); if (t > Threshold) { if (t > current) { intersect.Combine(ray.GetOrigin(), ray.GetDirection(), 1.0f, t); if ((intersect.GetX() <= MAXXF.GetVert(0).GetX()) && (intersect.GetX() >= MINXF.GetVert(0).GetX()) && (intersect.GetZ() <= MAXZF.GetVert(0).GetZ()) && (intersect.GetZ() >= MINZF.GetVert(0).GetZ())) { current = t; facehits [0] = MAXY; delta.Set(0.0f, Threshold, 0.0f); } } else if (t == current) { facehits [1] = MAXY; delta.SetY(Threshold); } } t = -(ray.GetOrigin().GetY() - OctFaces [MINY].GetVert(0).GetY()) / ray.GetDirection().GetY(); if (t > Threshold) { if (t > current) { intersect.Combine(ray.GetOrigin(), ray.GetDirection(), 1.0f, t); if ((intersect.GetX() <= MAXXF.GetVert(0).GetX()) && (intersect.GetX() >= MINXF.GetVert(0).GetX()) && (intersect.GetZ() <= MAXZF.GetVert(0).GetZ()) && (intersect.GetZ() >= MINZF.GetVert(0).GetZ())) { current = t; facehits [0] = MINY; delta.Set(0.0f, -Threshold, 0.0f); } } else if (t == current) { facehits [1] = MINY; delta.SetY(-Threshold); } } } if (ray.GetDirection().GetZ() != 0.0) { t = -(ray.GetOrigin().GetZ() - OctFaces [MAXZ].GetVert(0).GetZ()) / ray.GetDirection().GetZ(); if (t > Threshold) { if (t > current) { intersect.Combine(ray.GetOrigin(), ray.GetDirection(), 1.0f, t); if ((intersect.GetX() <= MAXXF.GetVert(0).GetX()) && (intersect.GetX() >= MINXF.GetVert(0).GetX()) && (intersect.GetY() <= MAXYF.GetVert(0).GetY()) && (intersect.GetY() >= MINYF.GetVert(0).GetY())) { current = t; facehits [0] = MAXZ; delta.Set(0.0f, 0.0f, Threshold); } } else if (t == current) { if (facehits [1] < 0) { facehits [1] = MAXZ; } else { facehits [2] = MAXZ; } delta.SetZ(Threshold); } } t = -(ray.GetOrigin().GetZ() - OctFaces [MINZ].GetVert(0).GetZ()) / ray.GetDirection().GetZ(); if (t > Threshold) { if (t > current) { intersect.Combine(ray.GetOrigin(), ray.GetDirection(), 1.0f, t); if ((intersect.GetX() <= MAXXF.GetVert(0).GetX()) && (intersect.GetX() >= MINXF.GetVert(0).GetX()) && (intersect.GetY() <= MAXYF.GetVert(0).GetY()) && (intersect.GetY() >= MINYF.GetVert(0).GetY())) { current = t; facehits [0] = MINZ; delta.Set(0.0f, 0.0f, -Threshold); } } else if (t == current) { if (facehits [1] < 0) { facehits [1] = MINZ; } else { facehits [2] = MINZ; } delta.SetZ(-Threshold); } } } if (facehits [0] >= MAXX) { intersect.Combine(ray.GetOrigin(), ray.GetDirection(), 1.0f, current); intersect.Add(delta); adjacent = Adjacent [facehits [0]]; if (facehits [1] >= MAXX) { if (adjacent != null) { adjacent = adjacent.GetAdjacent(facehits [1]); if (facehits [2] >= MAXX) { if (adjacent != null) { adjacent = adjacent.GetAdjacent(facehits [2]); } else { adjacent = null; } } } else { adjacent = null; } } } return(adjacent); }
/** * FormAdjacent * * @param maxX * @param maxY * @param maxZ * @param minX * @param minY * @param minZ */ private void FormAdjacent(OctNode maxX, OctNode maxY, OctNode maxZ, OctNode minX, OctNode minY, OctNode minZ) { Adjacent [0] = maxX; Adjacent [1] = maxY; Adjacent [2] = maxZ; Adjacent [3] = minX; Adjacent [4] = minY; Adjacent [5] = minZ; }
/** * CreateChildren * * @param objects * @param depth */ private void CreateChildren(ObjNode objects, int depth) { double maxX = OctFaces [MAXX].GetVert(0).GetX(); double minX = OctFaces [MINX].GetVert(0).GetX(); double maxY = OctFaces [MAXY].GetVert(0).GetY(); double minY = OctFaces [MINY].GetVert(0).GetY(); double maxZ = OctFaces [MAXZ].GetVert(0).GetZ(); double minZ = OctFaces [MINZ].GetVert(0).GetZ(); Point midpt = new Point((maxX + minX) / 2.0f, (maxY + minY) / 2.0f, (maxZ + minZ) / 2.0f); Point max = new Point(); Point min = new Point(); ObjNode currentnode; int i; max.Set(maxX, maxY, maxZ); min.Set(midpt.GetX(), midpt.GetY(), midpt.GetZ()); Child [0] = new OctNode(max, min); max.Set(maxX, midpt.GetY(), maxZ); min.Set(midpt.GetX(), minY, midpt.GetZ()); Child [1] = new OctNode(max, min); max.Set(maxX, midpt.GetY(), midpt.GetZ()); min.Set(midpt.GetX(), minY, minZ); Child [2] = new OctNode(max, min); max.Set(maxX, maxY, midpt.GetZ()); min.Set(midpt.GetX(), midpt.GetY(), minZ); Child [3] = new OctNode(max, min); max.Set(midpt.GetX(), maxY, maxZ); min.Set(minX, midpt.GetY(), midpt.GetZ()); Child [4] = new OctNode(max, min); max.Set(midpt.GetX(), midpt.GetY(), maxZ); min.Set(minX, minY, midpt.GetZ()); Child [5] = new OctNode(max, min); max.Set(midpt.GetX(), midpt.GetY(), midpt.GetZ()); min.Set(minX, minY, minZ); Child [6] = new OctNode(max, min); max.Set(midpt.GetX(), maxY, midpt.GetZ()); min.Set(minX, midpt.GetY(), minZ); Child [7] = new OctNode(max, min); OctNode[] adj = this.Adjacent; OctNode[] chld = this.Child; OctNode adj0 = adj [0]; OctNode adj1 = adj [1]; OctNode adj2 = adj [2]; OctNode adj3 = adj [3]; OctNode adj4 = adj [4]; OctNode adj5 = adj [5]; OctNode chld0 = chld [0]; OctNode chld1 = chld [1]; OctNode chld2 = chld [2]; OctNode chld3 = chld [3]; OctNode chld4 = chld [4]; OctNode chld5 = chld [5]; OctNode chld6 = chld [6]; OctNode chld7 = chld [7]; Child [0].FormAdjacent(adj0, adj1, adj2, chld4, chld1, chld3); Child [1].FormAdjacent(adj0, chld0, adj2, chld5, adj4, chld2); Child [2].FormAdjacent(adj0, chld3, chld1, chld6, adj4, adj5); Child [3].FormAdjacent(adj0, adj1, chld0, chld7, chld2, adj5); Child [4].FormAdjacent(chld0, adj1, adj2, adj3, chld5, chld7); Child [5].FormAdjacent(chld1, chld4, adj2, adj3, adj4, chld6); Child [6].FormAdjacent(chld2, chld7, chld5, adj3, adj4, adj5); Child [7].FormAdjacent(chld3, adj1, chld4, adj3, chld6, adj5); if (objects != null) { currentnode = objects; } else { currentnode = ObjList; } while (currentnode != null) { ObjectType currentobj = currentnode.GetObj(); for (i = 0; i < 8; i++) { OctNode cc = chld [i]; max = cc.GetFace(0).GetVert(0); min = cc.GetFace(5).GetVert(3); if (!((currentobj.GetMin().GetX() > max.GetX()) || (currentobj.GetMax().GetX() < min.GetX()))) { if (!((currentobj.GetMin().GetY() > max.GetY()) || (currentobj.GetMax().GetY() < min.GetY()))) { if (!((currentobj.GetMin().GetZ() > max.GetZ()) || (currentobj.GetMax().GetZ() < min.GetZ()))) { ObjNode newnode = new ObjNode(currentobj, Child [i].GetList()); cc.SetList(newnode); cc.IncNumObj(); } } } } currentnode = currentnode.Next(); } if (objects == null) { NumObj = 0; ObjList = null; } if (depth < MaxDepth) { for (i = 0; i < 8; i++) { if (Child [i].GetNumObj() > MaxObj) { Child [i].CreateChildren(null, depth + 1); } } } }
/** * 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); } }
/** * 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); } }
/** * FindLightBlock * * @param tree * @param ray * @param maxt * @return boolean */ private bool FindLightBlock (OctNode tree, Ray ray, double maxt) { OctNode current = tree.FindTreeNode (ray.GetOrigin ()); IntersectPt test = new IntersectPt (); Point testpt = new Point (); while (current != null) { ObjNode currentnode = current.GetList (); while (currentnode != null) { bool found = false; if (currentnode.GetObj ().GetCachePt ().GetID () == ray.GetID ()) { found = true; } if (!found) { test.SetOrigID (0); if (currentnode.GetObj ().Intersect (ray, test)) { if (test.GetT () < maxt) { return (true); } } } currentnode = currentnode.Next (); } OctNode adjacent = current.Intersect (ray, testpt, test.GetThreshold ()); if (adjacent == null) { current = null; } else { current = adjacent.FindTreeNode (testpt); } } return (false); }
/** * Copy * * @param original */ public void Copy(OctNode original) { int i; for(i = 0; i < 6; i++) { Adjacent[i] = original.GetAdjacent(i); OctFaces[i] = original.GetFace(i); } for(i = 0; i < 8; i++) { Child[i] = original.GetChild(i); } ObjList = original.GetList(); NumObj = original.GetNumObj(); }
/** * FormAdjacent * * @param maxX * @param maxY * @param maxZ * @param minX * @param minY * @param minZ */ private void FormAdjacent(OctNode maxX, OctNode maxY, OctNode maxZ, OctNode minX, OctNode minY, OctNode minZ) { Adjacent[0] = maxX; Adjacent[1] = maxY; Adjacent[2] = maxZ; Adjacent[3] = minX; Adjacent[4] = minY; Adjacent[5] = minZ; }
/** * CreateChildren * * @param objects * @param depth */ private void CreateChildren(ObjNode objects, int depth) { double maxX = OctFaces[MAXX].GetVert(0).GetX(); double minX = OctFaces[MINX].GetVert(0).GetX(); double maxY = OctFaces[MAXY].GetVert(0).GetY(); double minY = OctFaces[MINY].GetVert(0).GetY(); double maxZ = OctFaces[MAXZ].GetVert(0).GetZ(); double minZ = OctFaces[MINZ].GetVert(0).GetZ(); Point midpt = new Point((maxX + minX) / 2.0f, (maxY + minY) / 2.0f, (maxZ + minZ) / 2.0f); Point max = new Point(); Point min = new Point(); ObjNode currentnode; int i; max.Set(maxX, maxY, maxZ); min.Set(midpt.GetX(), midpt.GetY(), midpt.GetZ()); Child[0] = new OctNode(max, min); max.Set(maxX, midpt.GetY(), maxZ); min.Set(midpt.GetX(), minY, midpt.GetZ()); Child[1] = new OctNode(max, min); max.Set(maxX, midpt.GetY(), midpt.GetZ()); min.Set(midpt.GetX(), minY, minZ); Child[2] = new OctNode(max, min); max.Set(maxX, maxY, midpt.GetZ()); min.Set(midpt.GetX(), midpt.GetY(), minZ); Child[3] = new OctNode(max, min); max.Set(midpt.GetX(), maxY, maxZ); min.Set(minX, midpt.GetY(), midpt.GetZ()); Child[4] = new OctNode(max, min); max.Set(midpt.GetX(), midpt.GetY(), maxZ); min.Set(minX, minY, midpt.GetZ()); Child[5] = new OctNode(max, min); max.Set(midpt.GetX(), midpt.GetY(), midpt.GetZ()); min.Set(minX, minY, minZ); Child[6] = new OctNode(max, min); max.Set(midpt.GetX(), maxY, midpt.GetZ()); min.Set(minX, midpt.GetY(), minZ); Child[7] = new OctNode(max, min); OctNode[] adj = this.Adjacent; OctNode[] chld = this.Child; OctNode adj0 = adj[0]; OctNode adj1 = adj[1]; OctNode adj2 = adj[2]; OctNode adj3 = adj[3]; OctNode adj4 = adj[4]; OctNode adj5 = adj[5]; OctNode chld0 = chld[0]; OctNode chld1 = chld[1]; OctNode chld2 = chld[2]; OctNode chld3 = chld[3]; OctNode chld4 = chld[4]; OctNode chld5 = chld[5]; OctNode chld6 = chld[6]; OctNode chld7 = chld[7]; Child[0].FormAdjacent(adj0, adj1, adj2, chld4, chld1, chld3); Child[1].FormAdjacent(adj0, chld0, adj2, chld5, adj4, chld2); Child[2].FormAdjacent(adj0, chld3, chld1, chld6, adj4, adj5); Child[3].FormAdjacent(adj0, adj1, chld0, chld7, chld2, adj5); Child[4].FormAdjacent(chld0, adj1, adj2, adj3, chld5, chld7); Child[5].FormAdjacent(chld1, chld4, adj2, adj3, adj4, chld6); Child[6].FormAdjacent(chld2, chld7, chld5, adj3, adj4, adj5); Child[7].FormAdjacent(chld3, adj1, chld4, adj3, chld6, adj5); if(objects != null) { currentnode = objects; } else { currentnode = ObjList; } while(currentnode != null) { ObjectType currentobj = currentnode.GetObj(); for(i = 0; i < 8; i++) { OctNode cc = chld[i]; max = cc.GetFace(0).GetVert(0); min = cc.GetFace(5).GetVert(3); if(!((currentobj.GetMin().GetX() > max.GetX()) || (currentobj.GetMax().GetX() < min.GetX()))) { if(!((currentobj.GetMin().GetY() > max.GetY()) || (currentobj.GetMax().GetY() < min.GetY()))) { if(!((currentobj.GetMin().GetZ() > max.GetZ()) || (currentobj.GetMax().GetZ() < min.GetZ()))) { ObjNode newnode = new ObjNode(currentobj, Child[i].GetList()); cc.SetList(newnode); cc.IncNumObj(); } } } } currentnode = currentnode.Next(); } if(objects == null) { NumObj = 0; ObjList = null; } if(depth < MaxDepth) { for(i = 0; i < 8; i++) { if(Child[i].GetNumObj() > MaxObj) { Child[i].CreateChildren(null, depth + 1); } } } }
void OnDisable() { OctNode.Shutdown(); }
/** * Scene * * @param width * @param height * @param filename */ public Scene(String filename) { int numObj = LoadScene(filename); octree = new OctNode(this, numObj); }
/** * 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); } }
public Octree(int sizeIn, Vector3 position) { size = sizeIn; root = new OctNode(size, position, 0); root.createChildren(); }
/** * GetLightColor * * @param tree * @param point * @param normal * @param currentObj * @param color */ private void GetLightColor (OctNode tree, Point point, Vector normal, ObjectType currentObj, Color color) { Ray shadow = new Ray (); LightNode current = lights; double maxt; while (current != null) { shadow.SetOrigin (point); shadow.GetDirection ().Sub (current.GetLight ().GetPosition (), point); maxt = shadow.GetDirection ().Length (); shadow.GetDirection ().Normalize (); shadow.SetID (RayID); this.RayID = this.RayID + 1; if (!FindLightBlock (tree, shadow, maxt)) { double factor = Math.Max (0.0f, normal.Dot (shadow.GetDirection ())); if (factor != 0.0) { color.Mix (factor, current.GetLight ().GetColor (), currentObj.GetMaterial ().GetDiffColor ()); } } current = current.Next (); } }
/** * 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); } }
/** * Scene * * @param width * @param height * @param filename */ public Scene () { int numObj = LoadScene (); octree = new OctNode (this, numObj); }