private Vector3 ShadeRec(Vector3 V, RTLight lgt, IntersectionRecord rec) { RTMaterial m = mSceneDatabase.GetMaterial(rec.MaterialIndex); Vector3 r = Vector3.Zero; float percentToUse = 1f; if (mComputeShadow) percentToUse = lgt.PercentVisible(rec.IntersectPosition, rec.GeomIndex, mSceneDatabase); if (percentToUse > 0f) { Vector3 L = lgt.GetNormalizedDirection(rec.IntersectPosition); float NdotL = Vector3.Dot(rec.NormalAtIntersect, L); if (NdotL > 0) { Vector3 diffColor = m.GetDiffuse(mSceneDatabase, rec); Vector3 lightColor = percentToUse * lgt.GetColor(rec.IntersectPosition); r += diffColor * NdotL * lightColor; Vector3 R = 2 * NdotL * rec.NormalAtIntersect - L; float VdotR = Vector3.Dot(V, R); if (VdotR > 0) r += m.GetSpecular(mSceneDatabase, rec) * (float)Math.Pow(VdotR, m.GetN) * lightColor; } } return r; }
private Vector3 ShadeRec(Vector3 V, RTLight lgt, IntersectionRecord rec) { RTMaterial m = mSceneDatabase.GetMaterial(rec.MaterialIndex); Vector3 r = Vector3.Zero; float percentToUse = 1f; if (mComputeShadow) { percentToUse = lgt.PercentVisible(rec.IntersectPosition, rec.GeomIndex, mSceneDatabase); } if (percentToUse > 0f) { Vector3 L = lgt.GetNormalizedDirection(rec.IntersectPosition); float NdotL = Vector3.Dot(rec.NormalAtIntersect, L); if (NdotL > 0) { Vector3 diffColor = m.GetDiffuse(mSceneDatabase, rec); Vector3 lightColor = percentToUse * lgt.GetColor(rec.IntersectPosition); r += diffColor * NdotL * lightColor; Vector3 R = 2 * NdotL * rec.NormalAtIntersect - L; float VdotR = Vector3.Dot(V, R); if (VdotR > 0) { r += m.GetSpecular(mSceneDatabase, rec) * (float)Math.Pow(VdotR, m.GetN) * lightColor; } } } return(r); }
/// <summary> /// Lights: can be access as an array, the collection is a simple array /// </summary> /// <param name="l"></param> public void AddLight(RTLight l) { l.SetResourceIndex(mAllLights.Count); mAllLights.AddResource(l); }
public CommandFileParser(String cmdFile, ContentManager meshLoader, System.Windows.Forms.TextBox statusArea, RTCore rt, SceneDatabase scene) { mStatusArea = statusArea; mFullPath = System.IO.Path.GetFullPath(System.IO.Path.GetDirectoryName(cmdFile)); mParser = new XmlTextReader(cmdFile); mParser.WhitespaceHandling = WhitespaceHandling.None; ParserRead(); while (!IsEndElement("RayTracer_552")) { if (IsElement() && (!IsElement("RayTracer_552"))) { if (IsElement("camera")) { RTCamera c = new RTCamera(this); rt.SetCamera(c); ParserRead(); } else if (IsElement("sphere")) { RTSphere s = new RTSphere(this); scene.AddGeom(s); ParserRead(); } else if (IsElement("rectangle")) { RTRectangle r = new RTRectangle(this); scene.AddGeom(r); ParserRead(); } else if (IsElement("triangle")) { RTTriangle t = new RTTriangle(this); scene.AddGeom(t); ParserRead(); } else if (IsElement("mesh")) { RTTriangle.ParseMeshForTriangles(this, meshLoader, scene); ParserRead(); } else if (IsElement("imagespec")) { ImageSpec s = new ImageSpec(this); rt.SetImageSpec(s); ParserRead(); } else if (IsElement("rtspec")) { rt.Parse(this); ParserRead(); } else if (IsElement("material")) { RTMaterial m = new RTMaterial(this); scene.AddMaterial(m); ParserRead(); } else if (IsElement("light")) { RTLight l = new RTLight(this); scene.AddLight(l); ParserRead(); } else if (IsElement("texture")) { RTTexture t = new RTTexture(this); scene.AddTexture(t); ParserRead(); } else { ParserError("Main Parser:"); } } else { ParserRead(); } } mParser.Close(); if (!mHasError) { mStatusArea.Text = "Parsing Completed!"; } }
public CommandFileParser(String cmdFile, ContentManager meshLoader, System.Windows.Forms.TextBox statusArea, RTCore rt, SceneDatabase scene) { mStatusArea = statusArea; mFullPath = System.IO.Path.GetFullPath(System.IO.Path.GetDirectoryName(cmdFile)); mParser = new XmlTextReader(cmdFile); mParser.WhitespaceHandling = WhitespaceHandling.None; ParserRead(); while (!IsEndElement("RayTracer_552")) { if (IsElement() && (!IsElement("RayTracer_552")) ) { if (IsElement("camera")) { RTCamera c = new RTCamera(this); rt.SetCamera(c); ParserRead(); } else if (IsElement("sphere")) { RTSphere s = new RTSphere(this); scene.AddGeom(s); ParserRead(); } else if (IsElement("rectangle")) { RTRectangle r = new RTRectangle(this); scene.AddGeom(r); ParserRead(); } else if (IsElement("triangle")) { RTTriangle t = new RTTriangle(this); scene.AddGeom(t); ParserRead(); } else if (IsElement("mesh")) { RTTriangle.ParseMeshForTriangles(this, meshLoader, scene); ParserRead(); } else if (IsElement("imagespec")) { ImageSpec s = new ImageSpec(this); rt.SetImageSpec(s); ParserRead(); } else if (IsElement("rtspec")) { rt.Parse(this); ParserRead(); } else if (IsElement("material")) { RTMaterial m = new RTMaterial(this); scene.AddMaterial(m); ParserRead(); } else if (IsElement("light")) { RTLight l = new RTLight(this); scene.AddLight(l); ParserRead(); } else if (IsElement("texture")) { RTTexture t = new RTTexture(this); scene.AddTexture(t); ParserRead(); } else ParserError("Main Parser:"); } else ParserRead(); } mParser.Close(); if (!mHasError) mStatusArea.Text = "Parsing Completed!"; }
/// <summary> /// /// </summary> private Vector3 ComputeShading(IntersectionRecord rec, int generation) { if (rec.GeomIndex == RTCore.kInvalidIndex) { return(mBgColor); } Vector3 resultColor = Vector3.Zero; Vector3 V = -Vector3.Normalize(rec.RayDirection); RTMaterial m = mSceneDatabase.GetMaterial(rec.MaterialIndex); Vector3 useNormal = m.GetNormal(mSceneDatabase, rec); for (int l = 0; l < mSceneDatabase.GetNumLights(); l++) { RTLight lgt = mSceneDatabase.GetLight(l); resultColor += ShadeRec(V, lgt, rec); } resultColor += mSceneDatabase.GetMaterial(rec.MaterialIndex).GetAmbient(mSceneDatabase, rec); int nextGen = generation + 1; // now take care of reflection float reflectivity = m.GetReflectivity(mSceneDatabase, rec); Vector3 reflColor = Vector3.Zero; if (mComputeReflection && (reflectivity > 0f) && (generation < mGeneration)) { IntersectionRecord refRec = new IntersectionRecord(); Vector3 refDir = Vector3.Reflect(rec.RayDirection, useNormal); Ray refRay = Ray.CrateRayFromPtDir(rec.IntersectPosition, refDir); // Now compute new visibility ComputeVisibility(refRay, refRec, rec.GeomIndex); if (refRec.GeomIndex != RTCore.kInvalidIndex) { reflColor = ComputeShading(refRec, nextGen) * reflectivity; } else { reflColor = mBgColor * reflectivity; } } // now do transparency // // The following code: // 1. only supports refractive index > 1 (from less dense e.g.: air, to more dense: e.g., glass) // 2. cannot go from dense material into less dense material (once enter cannot exit) // 3. once entered: do not know how to go from one transparent object into another transparent object // float transparency = m.GetTransparency(mSceneDatabase, rec); Vector3 transColor = Vector3.Zero; if (mComputeReflection && (transparency > 0f) && (generation < mGeneration)) { float cosThetaI = Vector3.Dot(V, useNormal); float invN = 1f / m.GetRefractiveIndex; float cosThetaT = (float)Math.Sqrt(1 - ((invN * invN) * (1 - (cosThetaI * cosThetaI)))); Vector3 transDir = -((invN * V) + ((cosThetaT - (invN * cosThetaI)) * useNormal)); Ray transRay = Ray.CrateRayFromPtDir(rec.IntersectPosition, transDir); // Now compute new visibility IntersectionRecord transRec = new IntersectionRecord(); ComputeVisibility(transRay, transRec, rec.GeomIndex); // here we assume single layer geometries if (transRec.GeomIndex != RTCore.kInvalidIndex) { transColor = ComputeShading(transRec, nextGen) * transparency; } else { transColor = mBgColor * transparency; } } resultColor = (1 - transparency - reflectivity) * resultColor + transColor + reflColor; return(resultColor); }