Пример #1
0
 public PhongShader()
 {
     ambientBRDF = new Lambertian();
     diffuseBRDF = new Lambertian();
     specularBRDF = new GlossySpecular();
     PhongExponent = 200;
 }
Пример #2
0
 public PhongShader(Lambertian ambientBRDF, Lambertian diffuseBRDF, GlossySpecular specularBRDF)
 {
     this.ambientBRDF = new Lambertian(ambientBRDF);
     this.diffuseBRDF = new Lambertian(diffuseBRDF);
     this.specularBRDF = new GlossySpecular(specularBRDF);
 }
 public Matte() : base()
 {
     ambient_brdf = new Lambertian();
     diffuse_brdf = new Lambertian();
 }
Пример #4
0
        public void LoadScene(string path)
        {
            Console.WriteLine("Loading Scene");
            var root = Path.GetDirectoryName(path);
            var json = (JObject)JsonConvert.DeserializeObject(File.ReadAllText(path));

            //Camera
            var camera_data = (JObject)JsonConvert.DeserializeObject(json["Camera"].ToString());

            camera = new Camera(
                Vector3.FromList(JsonConvert.DeserializeObject <List <float> >(camera_data["lookFrom"].ToString())),
                Vector3.FromList(JsonConvert.DeserializeObject <List <float> >(camera_data["lookAt"].ToString())),
                new Vector3(0, 1, 0),
                JsonToF(float.Parse(camera_data["FOV"].ToString())),
                width / (float)height,
                JsonToF(float.Parse(camera_data["radius"].ToString())),
                JsonToF(float.Parse(camera_data["focus"].ToString())),
                JsonToF(float.Parse(camera_data["shutter"].ToString())));
            //Sky
            Skybox = json.ContainsKey("Sky");
            if (Skybox)
            {
                var skydata = (JObject)JsonConvert.DeserializeObject(File.ReadAllText(root + "/" + json["Sky"]));
                var datas   = JsonConvert.DeserializeObject <List <string> >(skydata["data"].ToString());
                var r       = Path.GetDirectoryName(root + "/" + json["Sky"]) + "/";
                sky = new CubeMap(r + datas[0], r + datas[1], r + datas[2], r + datas[3], r + datas[4], r + datas[5]);
            }
            //Material
            var shaders = new Dictionary <string, Shader>();

            var material_datas = (JObject)JsonConvert.DeserializeObject(json["Materials"].ToString());

            foreach (var pair in material_datas)
            {
                Shader shader        = null;
                var    materjson     = (JObject)JsonConvert.DeserializeObject(File.ReadAllText(root + "/" + pair.Value));
                var    datas         = JsonConvert.DeserializeObject <List <string> >(materjson["data"].ToString());
                var    material_root = Path.GetDirectoryName(root + "/" + pair.Value);
                switch (materjson["type"].ToString())
                {
                case "standard":
                    Texture reflect = null;
                    if (float.TryParse(datas[2], out var r))
                    {
                        reflect = new GrayTexture(r);
                    }
                    else
                    {
                        reflect = new ImageTexture(material_root + "/" + datas[2]);
                    }
                    shader = new StardardShader(
                        new ImageTexture(material_root + "/" + datas[0]),
                        new ImageTexture(material_root + "/" + datas[1]),
                        reflect, datas[3].ToString() == ""?null:new ImageTexture(material_root + "/" + datas[3])
                        );
                    break;

                case "metal":
                    shader = new Metal(
                        new ImageTexture(material_root + "/" + datas[0]),
                        float.Parse(datas[1]));
                    break;

                case "dielectirc":
                    shader = datas.Count == 1
                            ? new Dielectirc(float.Parse(datas[0]))
                            : new Dielectirc(float.Parse(datas[0]), new Color32(int.Parse(datas[1]) / 255f, int.Parse(datas[2]) / 255f, int.Parse(datas[3]) / 255f));
                    break;

                case "Lambertian":
                    shader = new Lambertian(new ImageTexture(material_root + "/" + datas[0]));
                    break;

                case "light":
                    shader = datas.Count == 2
                            ? new DiffuseLight(new ImageTexture(material_root + "/" + datas[1]), float.Parse(datas[0]))
                            :new DiffuseLight(new ConstantTexture(new Color32(int.Parse(datas[1]) / 255f, int.Parse(datas[2]) / 255f, int.Parse(datas[3]) / 255f)), float.Parse(datas[0]));
                    break;
                }
                shaders.Add(pair.Key.ToString(), shader);
            }

            Shader[] GetModelShaderList(List <string> name)
            {
                var result = new Shader[name.Count];

                for (var i = 0; i < name.Count; i++)
                {
                    result[i] = shaders[name[i]];
                }
                return(result);
            }

            //Objects
            var obj_datas = (JObject)JsonConvert.DeserializeObject(json["Objects"].ToString());
            var objlist   = new List <Hitable>();

            foreach (var pair in obj_datas)
            {
                var     obj = (JObject)JsonConvert.DeserializeObject(pair.Value.ToString());
                Hitable a   = null;

                switch (obj["type"].ToString())
                {
                case "model":
                    var scale = JsonToV3(obj["scale"]);
                    var pos   = JsonToV3(obj["position"]);
                    var rot   = JsonToV3(obj["rotation"]);
                    var m     = new Translate(new RotateY(ByteModel.Load(root + "/" + obj["file"],
                                                                         GetModelShaderList(JsonConvert.DeserializeObject <List <string> >(obj["material"].ToString())),
                                                                         new Vector3(scale.x, scale.y, scale.z)), rot.y), pos);
                    objlist.Add(m);
                    if (bool.Parse(obj["important"].ToString()))
                    {
                        Important.list.Add(a);
                    }
                    break;

                case "Oldmodel":
                    var scale_0 = JsonToV3(obj["scale"]);
                    var pos_0   = JsonToV3(obj["position"]);
                    var rot_0   = JsonToV3(obj["rotation"]);
                    var _m      = new Translate(new RotateY(ByteModel.LoadOld(root + "/" + obj["file"],
                                                                              GetModelShaderList(JsonConvert.DeserializeObject <List <string> >(obj["material"].ToString()))[0],
                                                                              new Vector3(scale_0.x, scale_0.y, scale_0.z)), rot_0.y), pos_0);
                    objlist.Add(_m);
                    if (bool.Parse(obj["important"].ToString()))
                    {
                        Important.list.Add(a);
                    }
                    break;

                case "cube":
                    a = new Translate(
                        new Mesh(Instances.Model.Cube,
                                 shaders[
                                     JsonConvert.DeserializeObject <List <string> >(obj["material"].ToString())[0]
                                 ]).Create(),
                        JsonToV3(obj["position"]));
                    objlist.Add(a);
                    if (bool.Parse(obj["important"].ToString()))
                    {
                        Important.list.Add(a);
                    }
                    break;

                case "sphere":
                    a = new Sphere(JsonToV3(obj["position"]), float.Parse(obj["radius"].ToString()),
                                   shaders[
                                       JsonConvert.DeserializeObject <List <string> >(obj["material"].ToString())[0]
                                   ]);
                    objlist.Add(a);
                    if (bool.Parse(obj["important"].ToString()))
                    {
                        Important.list.Add(a);
                    }
                    break;
                }
            }
            world.list.Add(new RotateY(new BVHNode(objlist.ToArray(), objlist.Count, 0, 1), 0));
            PreviewWindow.main.Loading.Visible = false;
        }
Пример #5
0
 public AbsMaterial(AbsMaterial other)
 {
     ambient = (Lambertian)other.ambient.Clone();
 }
Пример #6
0
 public AbsMaterial(Lambertian ambient)
 {
     this.ambient = ambient;
 }
Пример #7
0
 public AbsMaterial()
 {
     ambient = new Lambertian();
 }
Пример #8
0
 public MatteShader()
 {
     ambientBRDF = new Lambertian();
     diffuseBRDF = new Lambertian();
 }
Пример #9
0
        public void RenderScene()
        {
            int nx = 800;
            int ny = 400;
            int ns = 100;

            var lowerLeftCorner = new Vector3(-2.0, -1.0, -1.0);
            var horizontal      = new Vector3(4.0, 0.0, 0.0);
            var vertical        = new Vector3(0.0, 2.0, 0.0);
            var origin          = new Vector3(0.0, 0.0, 0.0);

            Random rng = new Random();
            UnitSphereUniformSampler sphereSampler = new UnitSphereUniformSampler(rng);
            UnitCircleUniformSampler circleSampler = new UnitCircleUniformSampler(rng);

            IMaterial mat1 = new Lambertian(new Vector3(0.1, 0.2, 0.5), sphereSampler);
            IMaterial mat2 = new Lambertian(new Vector3(0.8, 0.8, 0.0), sphereSampler);
            IMaterial mat3 = new Metal(new Vector3(0.8, 0.6, 0.2), 0.3, sphereSampler);
            IMaterial mat4 = new Dielectric(1.5, rng);

            List <IHitable> objects = new List <IHitable>(4);

            objects.Add(new Sphere(new Vector3(0, 0, -1), 0.5, mat1));
            objects.Add(new Sphere(new Vector3(0, -100.5, -1), 100, mat2));
            objects.Add(new Sphere(new Vector3(1, 0, -1), 0.5, mat3));
            objects.Add(new Sphere(new Vector3(-1, 0, -1), 0.5, mat4));
            objects.Add(new Sphere(new Vector3(-1, 0, -1), -0.45, mat4));

            HitableList world    = new HitableList(objects);
            Vector3     lookFrom = new Vector3(-2, 2, 1);
            Vector3     lookAt   = new Vector3(0, 0, -1);
            Camera      camera   = new Camera(
                circleSampler,
                lookFrom,
                lookAt,
                new Vector3(0, 1, 0),
                90.0 * Math.PI / 180.0,
                (double)nx / (double)ny,
                0.2,
                (lookAt - lookFrom).Length());

            Random random = new Random();

            // Create folder to output frames
            if (!Directory.Exists("ThreeSpheres"))
            {
                Directory.CreateDirectory("ThreeSpheres");
            }

            Sensor sensor   = new Sensor(nx, ny, new SqrtColorSpace(), RGBColor.Black);
            int    frameNum = 0;

            for (int s = 0; s < ns; ++s)
            {
                for (int j = ny - 1; j >= 0; --j)
                {
                    for (int i = 0; i < nx; i++)
                    {
                        double u = (i + random.NextDouble()) / nx;
                        double v = (j + random.NextDouble()) / ny;

                        Ray3    r     = new Ray3(camera.GetRay(u, v));
                        Vector3 color = Program.Color(r, world, 0);
                        sensor.AddSample(i, j, new RGBColor(color[0], color[1], color[2]));
                    }
                }

                if (s % (ns / 10) == 0)
                {
                    // Output 1 frame with current number of samples
                    sensor.WritePPMFile($"ThreeSpheres\\frame_{frameNum}.ppm").Wait();
                    frameNum++;
                }

                Console.Write("\r{0}", s);
            }

            sensor.WritePPMFile("finalOutput.ppm").Wait();
        }