Exemplo n.º 1
0
        public static Scene CreateMarblesAxisScene()
        {
            var background = new Background(new ColorVector(), 0.0);

            var redMaterial   = new SolidMaterial(0.5, 0.2, 0.0, 0.6, 0.0, 0.0, 1.0, new ColorVector(0.9, 0.0, 0.0));
            var greenMaterial = new SolidMaterial(0.5, 0.2, 0.0, 0.6, 0.0, 0.0, 1.0, new ColorVector(0.0, 0.9, 0.0));
            var blueMaterial  = new SolidMaterial(0.5, 0.2, 0.0, 0.6, 0.0, 0.0, 1.0, new ColorVector(0.0, 0.0, 0.9));

            double sphereDistanceIncrement = 5.0;
            int    numSpheresPerAxis       = 10;
            double sphereRadius            = 2.0;

            double maxAxis = sphereDistanceIncrement * Convert.ToDouble(numSpheresPerAxis);

            var shapes = new List <Shape>();

            double x = 0.0;

            while (x <= maxAxis)
            {
                shapes.Add(new SphereShape(new PosVector(x, 0.0, 0.0), sphereRadius, redMaterial));
                x += sphereDistanceIncrement;
            }

            double y = 0.0;

            while (y <= maxAxis)
            {
                shapes.Add(new SphereShape(new PosVector(0.0, y, 0.0), sphereRadius, greenMaterial));
                y += sphereDistanceIncrement;
            }

            double z = 0.0;

            while (z <= maxAxis)
            {
                shapes.Add(new SphereShape(new PosVector(0.0, 0.0, z), sphereRadius, blueMaterial));
                z += sphereDistanceIncrement;
            }

            var chessMaterial = new ChessboardMaterial(0.5, 0.2, 0.0, 0.2, 0.0, 0.0, 2.0, new ColorVector(0.8, 0.8, 0.8), new ColorVector(), 15.0);

            shapes.Add(new PlaneShape(new PosVector(0.0, 0.0, 1.0), 0.0, chessMaterial));

            var lights = new List <Light>();

            lights.Add(new PointLight(new PosVector(5.0, 10.0, 10.0), new ColorVector(0.9, 0.9, 0.9)));

            return(Scene.Create(background, shapes, lights));
        }
Exemplo n.º 2
0
        public static NffParserResult ParseFile(string path, int numThreads, int rayTraceDepth, int resolutionX,
                                                int resolutionY)
        {
            var background = new Background(new ColorVector(0.0, 0.0, 0.0), 0.0);
            var lights     = new List <Light>();
            var shapes     = new List <Shape>();
            var cameraAt   = PosVector.NewDefault();
            var cameraFrom = PosVector.NewDefault();
            var cameraUp   = PosVector.NewDefault();

            var lookingFor = LookingFor.Instruction;

            var currentMaterial = new SolidMaterial(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, new ColorVector(0.0, 0.0, 0.0));

            var polyVectors        = new List <PosVector>();
            var currentItemCounter = 0;

            var lines = File.ReadAllLines(path);

            foreach (var line in lines)
            {
                var split = line.Split(' ', '\t');

                switch (lookingFor)
                {
                case LookingFor.Instruction:
                {
                    var instruction = split[0];

                    if (instruction == "b")
                    {
                        // background color
                        background =
                            new Background(new ColorVector(double.Parse(split[1]), double.Parse(split[2]), double.Parse(split[3])),
                                           0.0);
                    }
                    else if (instruction == "v")
                    {
                        // viewpoint location
                        lookingFor = LookingFor.ViewpointFrom;
                    }
                    else if (instruction == "l")
                    {
                        // positional light
                        var colorVector = split.Length == 7
                ? new ColorVector(double.Parse(split[4]), double.Parse(split[5]), double.Parse(split[6]))
                : new ColorVector(1.0, 1.0, 1.0);
                        lights.Add(
                            new PointLight(new PosVector(double.Parse(split[1]), double.Parse(split[2]), double.Parse(split[3])),
                                           colorVector));
                    }
                    else if (instruction == "f")
                    {
                        // println!("reading f: {}", num);
                        // object material properties
                        // "f" red green blue Kd Ks Shine T index_of_refraction
                        // Kd Diffuse component
                        // Ks Specular
                        // Shine Phong cosine power for highlights
                        // T Transmittance (fraction of contribution of the transmitting ray).
                        // Usually, 0 <= Kd <= 1 and 0 <= Ks <= 1, though it is not required that Kd + Ks = 1. Note that transmitting objects (T > 0) are considered to have two sides for algorithms that need these (normally, objects have one side).
                        // todo: i don't think i'm assigning the correct values into my solidmaterial yet
                        currentMaterial = new SolidMaterial(
                            0.0,                    // kAmbient
                            double.Parse(split[4]), // kDiffuse
                            double.Parse(split[5]), // kSpecular
                            double.Parse(split[7]), // kReflection
                            double.Parse(split[8]), // kTransparent
                            double.Parse(split[8]), // refraction    -- todo: which is which here?
                            double.Parse(split[6]), // gloss
                            new ColorVector(double.Parse(split[1]), double.Parse(split[2]), double.Parse(split[3]))
                            );
                    }
                    else if (instruction == "c")
                    {
                        // cone or cylinder
                    }
                    else if (instruction == "s")
                    {
                        // sphere
                        shapes.Add(new SphereShape(
                                       new PosVector(double.Parse(split[1]), double.Parse(split[2]), double.Parse(split[3])),
                                       double.Parse(split[4]),
                                       currentMaterial
                                       ));
                    }
                    else if (instruction == "p")
                    {
                        // polygon
                        currentItemCounter = int.Parse(split[1]);
                        polyVectors        = new List <PosVector>();
                        lookingFor         = LookingFor.Polygon;
                    }
                    else if (instruction == "pp")
                    {
                        // polygon patch
                    }
                    else if (instruction == "#")
                    {
                        // comment
                    }
                }
                break;

                case LookingFor.Polygon:
                {
                    if (currentItemCounter > 0)
                    {
                        currentItemCounter--;
                        polyVectors.Add(new PosVector(double.Parse(split[0]), double.Parse(split[1]), double.Parse(split[2])));
                    }

                    if (currentItemCounter == 0)
                    {
                        if (polyVectors.Count >= 3)
                        {
                            var firstVert = polyVectors[0];
                            var prevVert  = polyVectors[1];
                            var thisVert  = polyVectors[2];
                            shapes.Add(new TriangleShape(firstVert, prevVert, thisVert, currentMaterial, currentMaterial));

                            for (var i = 3; i < polyVectors.Count; i++)
                            {
                                prevVert = thisVert;
                                thisVert = polyVectors[i];
                                shapes.Add(new TriangleShape(firstVert, prevVert, thisVert, currentMaterial, currentMaterial));
                            }
                        }

                        lookingFor = LookingFor.Instruction;
                    }
                }
                break;

                case LookingFor.ViewpointFrom:
                {
                    cameraFrom = new PosVector(double.Parse(split[1]), double.Parse(split[2]), double.Parse(split[3]));
                    lookingFor = LookingFor.ViewpointAt;
                }
                break;

                case LookingFor.ViewpointAt:
                {
                    cameraAt   = new PosVector(double.Parse(split[1]), double.Parse(split[2]), double.Parse(split[3]));
                    lookingFor = LookingFor.ViewpointUp;
                }
                break;

                case LookingFor.ViewpointUp:
                {
                    cameraUp   = new PosVector(double.Parse(split[1]), double.Parse(split[2]), double.Parse(split[3]));
                    lookingFor = LookingFor.ViewpointAngle;
                }
                break;

                case LookingFor.ViewpointAngle:
                {
                    // todo: implement
                    lookingFor = LookingFor.ViewpointHither;
                }
                break;

                case LookingFor.ViewpointHither:
                {
                    // todo: implement
                    lookingFor = LookingFor.ViewpointResolution;
                }
                break;

                case LookingFor.ViewpointResolution:
                {
                    //resolutionX = int.Parse(split[1]);
                    //resolutionY = int.Parse(split[2]);

                    lookingFor = LookingFor.Instruction;
                }
                break;
                }
            }

            return(new NffParserResult(Scene.Create(background, shapes, lights),
                                       new RenderData(resolutionX, resolutionY, rayTraceDepth, numThreads, true),
                                       new Camera(cameraFrom, cameraAt, cameraUp, 50.0)));
        }