Пример #1
0
        private void readPolygons(StreamTokenizer lex, Scene scene, List<Vector> vertices, Material shapeMaterial, Matrix shapeMatrix)
        {
            int startLevel = lex.BracketLevel;
            while (!lex.EndOfStream)
            {
                double i1 = 0;
                if (lex.TryReadDouble(out i1))
                {
                    // read polyline till -1
                    double iPreLast = lex.ReadDouble();
                    double iLast = lex.ReadDouble();
                    while (iLast != -1)
                    {
                        scene.addObject(new Triangle(shapeMaterial,
                        shapeMatrix * vertices[(int)i1],
                        shapeMatrix * vertices[(int)iPreLast],
                        shapeMatrix * vertices[(int)iLast]));

                        iPreLast = iLast;
                        iLast = lex.ReadDouble();
                    }
                }

                if (lex.BracketLevel <= startLevel)
                    return;
            }
        }
Пример #2
0
 public void AddToSceneFrom(Scene scene, string fileName)
 {
     // deserialize
     ScnFile scnFile = ScnFile.LoadFrom(fileName);
     string modelPath = Path.GetDirectoryName(fileName);
     AddToSceneFrom(scene, scnFile, modelPath);
 }
Пример #3
0
 public void AddToSceneFrom(Scene scene, string fileName)
 {
     using (FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
     {
         StreamTokenizer lex = new StreamTokenizer(stream);
         readWholeFile(lex, scene);
     }
 }
Пример #4
0
        private void readShapeObject(StreamTokenizer lex, Scene scene, Matrix shapeMatrix)
        {
            int startLevel = lex.BracketLevel;
            List<Vector> shapeVertices = null;
            Material shapeMaterial = new Material(Color.Black, 0, 0);
            while (!lex.EndOfStream)
            {
                lex.NextWord();
                if (lex.BracketLevel <= startLevel)
                    return;

                if (lex.Word == "diffuseColor")
                {
                    double r = lex.ReadDouble();
                    double g = lex.ReadDouble();
                    double b = lex.ReadDouble();
                    shapeMaterial.Color = new Color(r, g, b, 1.0);
                }

                if (lex.Word == "transparency")
                {
                    Color shapeColor = shapeMaterial.Color;
                    shapeColor.A = 1 - lex.ReadDouble();
                    shapeMaterial.Color = shapeColor;
                }

                if (lex.Word == "shininess")
                {
                    shapeMaterial.Reflectance = lex.ReadDouble();
                    shapeMaterial.Specular = shapeMaterial.Reflectance;
                }

                if (lex.Word == "point")
                {
                    shapeVertices = readShapeVertices(lex);
                }

                if (lex.Word == "coordIndex")
                {
                    scene.addMaterial(shapeMaterial);
                    readPolygons(lex, scene, shapeVertices, shapeMaterial, shapeMatrix);
                }

                if (lex.BracketLevel <= startLevel)
                    return;
            }
        }
Пример #5
0
        public void AddToSceneFrom(Scene scene, ScnFile scnFile, string modelPath)
        {
            foreach (Primitive primitive in scnFile.Primitives)
            {
                scene.addObject(primitive);
            }
            foreach (Light light in scnFile.Lights)
            {
                scene.addLight(light);
            }
            // use VRML loader to load external models
            Vrml2FormatLoader vrmlLoader = new Vrml2FormatLoader();
            foreach (ModelFileInfo modelFile in scnFile.ModelFiles)
            {
                // assume model in same folder as xml file
                string modelFileName = Path.Combine(modelPath, modelFile.FileName);

                vrmlLoader.TransformMatrix = modelFile.TransformMatrix;
                vrmlLoader.AddToSceneFrom(scene, modelFileName);
            }
        }
Пример #6
0
        private void load_Click(object sender, EventArgs e)
        {
            this.scene = new Scene();

            // fill scene
            ScnFormatLoader loader = new ScnFormatLoader();
            //loader.AddToSceneFrom(scene, @"D:\__prog__\__Cs\Lucid\scenes\test_chair\chair.xml");
            loader.AddToSceneFrom(scene, this.scnFile, AppDomain.CurrentDomain.BaseDirectory);

            scene.Compile();
        }
Пример #7
0
        private unsafe void Render(Scene sc, Bitmap bitmap, Rectangle rect)
        {
            // clip rect to bitmap
            rect = new Rectangle(
                Math.Max(0, rect.Left),
                Math.Max(0, rect.Top),
                Math.Min(bitmap.Width - rect.Left, rect.Width),
                Math.Min(bitmap.Height - rect.Top, rect.Height));

            Lucid.Raytracing.Color[] colorBuffer = new Lucid.Raytracing.Color[rect.Width * rect.Height];
            sc.Render(rect, colorBuffer);

            BitmapData data = bitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            for (int y = 0; y < data.Height; y++)
            {
                int* dataPos = (int*)((int)data.Scan0 + (y * data.Stride));

                for (int x = 0; x < data.Width; x++)
                {
                    *dataPos = colorBuffer[x + rect.Width * y].ToArgb();
                    dataPos++;
                    //bitmap.SetPixel(x, y, col.ToDrawingColor());
                }
            }

            bitmap.UnlockBits(data);
        }
Пример #8
0
        private void readWholeFile(StreamTokenizer lex, Scene scene)
        {
            while (!lex.EndOfStream)
            {
                while (lex.Word == "Transform")
                {
                    readTransformObject(lex, scene);
                }

                lex.NextWord();
            }
        }
Пример #9
0
        private void readTransformObject(StreamTokenizer lex, Scene scene)
        {
            int startLevel = lex.BracketLevel;
            while (!lex.EndOfStream)
            {
                lex.NextWord();
                // check to return immediately after reading word
                if (lex.BracketLevel <= startLevel)
                    return;

                // transform matrix
                Matrix ShapeMatrix = TransformMatrix;
                while (lex.Word == "Transform")
                {
                    // nested transforms
                    readTransformObject(lex, scene);
                }
                if (lex.Word == "translation")
                {
                    double tx = lex.ReadDouble();
                    double ty = lex.ReadDouble();
                    double tz = lex.ReadDouble();
                    ShapeMatrix = TransformMatrix.Translate(tx, ty, tz);
                }
                while (lex.Word == "Shape")
                {
                    readShapeObject(lex, scene, ShapeMatrix);
                }

                // must have chance to return immediately after returning,
                // because the word might be needed several levels higher
                if (lex.BracketLevel <= startLevel)
                    return;
            }
        }