public static GeometricObject CreateObjectFromElement(this RElement ele, Dictionary<string, Texture> txts, ref bool instance) { string n = ele.Name; string name = getName(ele); bool shad = getShadow(ele); instance = !getInstance(ele); Material m = getMaterial(ele, txts); if (n == P.Obj.Sph) { Sphere sph = new Sphere(ele.Attributes[P.Pos].ToVector3(), ele.Attributes[P.Rad].ToFloat(), name); sph.SetShadows(shad); sph.SetMaterial(m); return sph; } else if (n == P.Obj.Box) { Box box = new Box(ele.Attributes[P.Min].ToVector3(), ele.Attributes[P.Max].ToVector3(), name); box.SetShadows(shad); box.SetMaterial(m); return box; } else if (n == P.Obj.Plane) { Plane p = new Plane(ele.Attributes[P.Pos].ToVector3(), ele.Attributes[P.Dir].ToVector3(), name); p.SetShadows(shad); p.SetMaterial(m); return p; } else if (n == P.Obj.Disk) { Disk d = new Disk(ele.Attributes[P.Pos].ToVector3(), ele.Attributes[P.Dir].ToVector3(), ele.Attributes[P.Rad].ToFloat(), name); d.SetShadows(shad); d.SetMaterial(m); return d; } else if (n == P.Obj.Annulus) { Annulus d = new Annulus(ele.Attributes[P.Pos].ToVector3(), ele.Attributes[P.Dir].ToVector3(), float.Parse(ele.Attributes[P.IRad].Value), float.Parse(ele.Attributes[P.ORad].Value), name); d.SetShadows(shad); d.SetMaterial(m); return d; } else if (n == P.Obj.Tri) { Triangle t = new Triangle(ele.Attributes["P1"].ToVector3(), ele.Attributes["P2"].ToVector3(), ele.Attributes["P3"].ToVector3(), name); t.SetShadows(shad); t.SetMaterial(m); return t; } else if (n == P.Obj.STri) { SmoothTriangle t = new SmoothTriangle(ele.Attributes["P1"].ToVector3(), ele.Attributes["P2"].ToVector3(), ele.Attributes["P3"].ToVector3(), name); t.SetShadows(shad); t.SetMaterial(m); return t; } else if (n == P.Obj.Rect) { Rectangle r = new Rectangle(ele.Attributes["P"].ToVector3(), GetVector(ele.Attributes["A"]), GetVector(ele.Attributes["B"]), name); r.SetShadows(shad); r.SetMaterial(m); return r; } else if (n == P.Obj.OCyl) { OpenCylinder oc = new OpenCylinder(float.Parse(ele.Attributes[P.Bottom].Value), float.Parse(ele.Attributes[P.Top].Value), float.Parse(ele.Attributes[P.Rad].Value), name); oc.SetShadows(shad); oc.SetMaterial(m); return oc; } else if (n == P.Obj.Cyl) { SolidCylinder oc = new SolidCylinder(float.Parse(ele.Attributes[P.Bottom].Value), float.Parse(ele.Attributes[P.Top].Value), float.Parse(ele.Attributes[P.Rad].Value), name); oc.SetShadows(shad); oc.SetMaterial(m); return oc; } else if (n == P.Obj.OC) { OpenCone oc = new OpenCone(float.Parse(ele.Attributes[P.Rad].Value), float.Parse(ele.Attributes[P.Height].Value), name); oc.SetShadows(shad); oc.SetMaterial(m); return oc; } else if (n == P.Obj.COCyl) { ConvexOpenCylinder oc = new ConvexOpenCylinder(float.Parse(ele.Attributes[P.Bottom].Value), float.Parse(ele.Attributes[P.Top].Value), float.Parse(ele.Attributes[P.Rad].Value), name); oc.SetShadows(shad); oc.SetMaterial(m); return oc; } else if (n == P.Obj.Ins) { Instance ins = new Instance(GeometricObject.GetObject(ele.Attributes["ObjectRef"].Value), name); ins.SetShadows(shad); transform(ins, ele); if (m != null) ins.SetMaterial(m); return ins; } else if (n == P.Obj.Tor) { Torus tor = new Torus(ele.Attributes[P.Srad].ToFloat(), ele.Attributes[P.Trad].ToFloat(), name); tor.SetShadows(shad); if (m != null) tor.SetMaterial(m); return tor; } else if (n == P.Obj.ConSph) { ConcaveSphere conc = new ConcaveSphere(ele.Attributes[P.Pos].ToVector3(), ele.Attributes[P.Rad].ToFloat(), name); conc.SetShadows(shad); if (m != null) conc.SetMaterial(m); return conc; } else if (n == P.Obj.ConHsph) { ConcaveHemisphere conc = new ConcaveHemisphere(ele.Attributes[P.Pos].ToVector3(), ele.Attributes[P.Rad].ToFloat(), name); conc.SetShadows(shad); if (m != null) conc.SetMaterial(m); return conc; } else if (n == P.Obj.Mesh) { Mesh mesh = new Mesh(name); mesh.SetShadows(shad); getVertices(mesh, ele); getNormals(mesh, ele); getFaces(mesh, ele); if (m != null) mesh.SetMaterial(m); return mesh; } else { return getCObject(ele); } }
void build11() { int ns = 100; world.ViewPlane.HRes = 600; world.ViewPlane.VRes = 600; world.ViewPlane.SetSampler(new Regular(ns)); world.ViewPlane.MaxDepth = 10; world.Tracer = new Whitted(world); AmbientOccluder occ = new AmbientOccluder(); occ.RadianceScale = 1.0f; occ.MinAmount = 0f; occ.Shadows = true; occ.Color = Vector3.One; occ.SetSampler(new Regular(ns)); world.AmbientLight = occ; Pinhole cam = new Pinhole(); cam.Position = new Vector3(-6, 6, 45); cam.Target = new Vector3(0, 0, 0); cam.Distance = 5000; cam.Zoom = 1.7f; world.Camera = cam; Matte plane_material = new Matte(0.9f, 0.9f, Vector3.One); XRectangle pl = new XRectangle(new Vector3(-2.5f, 0, -2.5f), new Vector3(0, 0, 5), new Vector3(5, 0, 0), Vector3.Up); //Plane pl = new Plane(new Vector3(0), Vector3.Up); pl.SetMaterial(plane_material); world.Objects.Add(pl); PointLight light = new PointLight(); light.Color = Vector3.One; light.Position = new Vector3(-3, 3, 0); light.RadianceScale = 2.0f; float refl = 0.18f; float spec = 0.8f; float amb = 0.5f; float diff = 1f; float exp = 100; Group tree = new Group(); world.Lights.Add(light); { ImageTexture im = new ImageTexture(); Disque.Raytracer.Textures.Image imm = Extensions.CreateFromBitmap(@"C:\Users\Belal\Downloads\Earth_Diffuse_2.jpg"); im.SetImage(imm); im.SetMapping(new SphericalMap()); SV_Phong sphere_m = new SV_Phong(); sphere_m.SetCD(im); sphere_m.SetSpecularColor(im); sphere_m.SetExp(40); sphere_m.SetSpecularRC(0.2f); sphere_m.SetDiffuseRC(0.6f); sphere_m.SetAmbientRC(0.25f); Sphere sphere = new Sphere(new Vector3(0, 0f, 0), 1f); Instance ins = new Instance(sphere); ins.SetMaterial(sphere_m); ins.RotateY(MathHelper.ToRadians(90)); ins.Scale(new Vector3(0.4f)); ins.Translate(new Vector3(0, 0.4f, 0.5f)); world.Objects.Add(ins); } { Reflective smatte2 = new Reflective(); smatte2.SetAmbientRC(amb); smatte2.SetDiffuseRC(diff); smatte2.SetCD(new Vector3(1, 0.77f, 0.77f)); smatte2.SetRColor(new Vector3(1, 0.77f, 0.77f)); smatte2.SetSpecularRC(spec); smatte2.SetExp(exp); smatte2.SetReflectiveRC(refl); Sphere s = new Sphere(new Vector3(-0.8f, 0.45f, 0.1f), 0.45f); s.SetMaterial(smatte2); tree.AddObject(s); //world.Objects.Add(s); } { Reflective smatte2 = new Reflective(); smatte2.SetAmbientRC(amb); smatte2.SetDiffuseRC(diff); smatte2.SetCD(new Vector3(0.92f, 0.74f, 1)); smatte2.SetRColor(new Vector3(0.92f, 0.74f, 1)); smatte2.SetSpecularRC(spec); smatte2.SetExp(exp); smatte2.SetReflectiveRC(refl); Sphere s = new Sphere(new Vector3(0.7f, 0.4f, 1.5f), 0.4f); s.SetMaterial(smatte2); tree.AddObject(s); //world.Objects.Add(s); } { Reflective smatte2 = new Reflective(); smatte2.SetAmbientRC(amb); smatte2.SetDiffuseRC(diff); smatte2.SetCD(new Vector3(0.62f, 0f, 0.7f)); smatte2.SetRColor(new Vector3(0.62f, 0f, 0.7f)); smatte2.SetSpecularRC(spec); smatte2.SetExp(exp); smatte2.SetReflectiveRC(refl); Sphere s = new Sphere(new Vector3(-0.7f, 0.2f, 1.5f), 0.2f); s.SetMaterial(smatte2); tree.AddObject(s); //world.Objects.Add(s); } world.Objects.Add(tree); }
static GeometricObject getPObject(XElement ele, ref bool instance) { string typ = ele.Name.LocalName; if (ele.Attribute("Instance") != null) instance = bool.Parse(getAttribute("Instance", ele)); else instance = false; if (typ == "Sphere") { Sphere sph = new Sphere(getVector(getAttribute("Position", ele)), float.Parse(getAttribute("Radius", ele)), getAttribute("Name", ele)); sph.SetShadows(bool.Parse(getAttribute("Shadows", ele))); sph.SetMaterial(getMaterial(ele)); return sph; } else if (typ == "Box") { Box b = new Box(getVector(getAttribute("Min", ele)), getVector(getAttribute("Max", ele)), getAttribute("Name", ele)); b.SetShadows(bool.Parse(getAttribute("Shadows", ele))); b.SetMaterial(getMaterial(ele)); return b; } else if (typ == "Plane") { Plane p = new Plane(getVector(getAttribute("Position", ele)), getVector(getAttribute("Direction", ele)), getAttribute("Name", ele)); p.SetShadows(bool.Parse(getAttribute("Shadows", ele))); p.SetMaterial(getMaterial(ele)); return p; } else if (typ == "Disk") { Disk d = new Disk(getVector(getAttribute("Center", ele)), getVector(getAttribute("Direction", ele)), float.Parse(getAttribute("Radius", ele)), getAttribute("Name", ele)); d.SetShadows(bool.Parse(getAttribute("Shadows", ele))); d.SetMaterial(getMaterial(ele)); return d; } else if (typ == "Annulus") { Annulus d = new Annulus(getVector(getAttribute("Center", ele)), getVector(getAttribute("Direction", ele)), float.Parse(getAttribute("InnerRadius", ele)), float.Parse(getAttribute("OuterRadius", ele)), getAttribute("Name", ele)); d.SetShadows(bool.Parse(getAttribute("Shadows", ele))); d.SetMaterial(getMaterial(ele)); return d; } else if (typ == "Triangle") { Triangle t = new Triangle(getVector(getAttribute("P1", ele)), getVector(getAttribute("P2", ele)), getVector(getAttribute("P3", ele)), getAttribute("Name", ele)); t.SetShadows(bool.Parse(getAttribute("Shadows", ele))); t.SetMaterial(getMaterial(ele)); return t; } else if (typ == "SmoothTriangle") { SmoothTriangle t = new SmoothTriangle(getVector(getAttribute("P1", ele)), getVector(getAttribute("P2", ele)), getVector(getAttribute("P3", ele)), getAttribute("Name", ele)); t.SetShadows(bool.Parse(getAttribute("Shadows", ele))); t.SetMaterial(getMaterial(ele)); return t; } else if (typ == "Rectangle") { Rectangle r = new Rectangle(getVector(getAttribute("P", ele)), getVector(getAttribute("A", ele)), getVector(getAttribute("B", ele)), getAttribute("Name", ele)); r.SetShadows(bool.Parse(getAttribute("Shadows", ele))); r.SetMaterial(getMaterial(ele)); return r; } else if (typ == "OpenCylinder") { OpenCylinder oc = new OpenCylinder(float.Parse(getAttribute("Bottom", ele)), float.Parse(getAttribute("Top", ele)), float.Parse(getAttribute("Radius", ele)), getAttribute("Name", ele)); oc.SetShadows(bool.Parse(getAttribute("Shadows", ele))); oc.SetMaterial(getMaterial(ele)); return oc; } else if (typ == "OpenCone") { OpenCone oc = new OpenCone(float.Parse(getAttribute("Radius", ele)), float.Parse(getAttribute("Height", ele)), getAttribute("Name", ele)); oc.SetShadows(bool.Parse(getAttribute("Shadows", ele))); oc.SetMaterial(getMaterial(ele)); return oc; } else if (typ == "ConvexOpenCylinder") { ConvexOpenCylinder oc = new ConvexOpenCylinder(float.Parse(getAttribute("Bottom", ele)), float.Parse(getAttribute("Top", ele)), float.Parse(getAttribute("Radius", ele)), getAttribute("Name", ele)); oc.SetShadows(bool.Parse(getAttribute("Shadows", ele))); oc.SetMaterial(getMaterial(ele)); return oc; } else if (typ == "Instance") { Instance ins = new Instance(GeometricObject.GetObject(getAttribute("ObjectRef", ele)), getAttribute("Name", ele)); ins.SetShadows(bool.Parse(getAttribute("Shadows", ele))); return ins; } else { return getCObject(ele); } }