/** * 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 (); } }
/** * RenderScene */ public void RenderScene (Canvas canvas, int width, int section, int nsections) { Vector view = camera.GetViewDir (); Vector up = camera.GetOrthoUp (); Vector plane = new Vector (); Vector horIncr = new Vector (); Vector vertIncr = new Vector (); double ylen = camera.GetFocalDist () * (double)Math.Tan (0.5f * camera.GetFOV ()); double xlen = ylen * canvas.GetWidth () / canvas.GetHeight (); Point upleft = new Point (); Point upright = new Point (); Point lowleft = new Point (); Point basepoint = new Point (); Point current; Ray eyeRay = new Ray (); int ypixel, xpixel; RayID = 1; plane.Cross (view, up); view.Scale (camera.GetFocalDist ()); up.Scale (ylen); plane.Scale (-xlen); upleft.FindCorner (view, up, plane, camera.GetPosition ()); plane.Negate (); upright.FindCorner (view, up, plane, camera.GetPosition ()); up.Negate (); plane.Negate (); lowleft.FindCorner (view, up, plane, camera.GetPosition ()); horIncr.Sub (upright, upleft); horIncr.Scale (horIncr.Length () / ((double)canvas.GetWidth ())); vertIncr.Sub (lowleft, upleft); vertIncr.Scale (vertIncr.Length () / ((double)canvas.GetHeight ())); basepoint.Set (upleft.GetX () + 0.5f * (horIncr.GetX () + vertIncr.GetX ()), upleft.GetY () + 0.5f * (horIncr.GetY () + vertIncr.GetY ()), upleft.GetZ () + 0.5f * (horIncr.GetZ () + vertIncr.GetZ ())); eyeRay.SetOrigin (camera.GetPosition ()); int xstart = section * width / nsections; int xend = xstart + width / nsections; MainCL.logger.InfoFormat ("+" + xstart + " to " + (xend - 1) + " by " + canvas.GetHeight ()); for (ypixel = 0; ypixel < canvas.GetHeight (); ypixel++) { current = new Point (basepoint); for (xpixel = 0; xpixel < canvas.GetWidth (); xpixel++) { if (xpixel >= xstart && xpixel < xend) { Color color = new Color (0.0f, 0.0f, 0.0f); eyeRay.GetDirection ().Sub (current, eyeRay.GetOrigin ()); eyeRay.GetDirection ().Normalize (); eyeRay.SetID (RayID); this.RayID = this.RayID + 1; Shade (octree, eyeRay, color, 1.0f, 0, 0); canvas.Write (Brightness, xpixel, ypixel, color); } current.Add (horIncr); } basepoint.Add (vertIncr); } MainCL.logger.InfoFormat ("-" + xstart + " to " + (xend - 1) + " by " + canvas.GetHeight ()); }
/** * 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); } }