Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;

            // load mesh
            worker.ReportProgress(0, "Loading mesh ...");
            var mesh = new WavefrontObjMesh(e.Argument as string);

            worker.ReportProgress(100, "Mesh loaded");

            // create voxel octree from mesh
            worker.ReportProgress(0, "Creating voxel octree ...");
            mesh.GenerateOctree(8, .1f);
            worker.ReportProgress(100, "Voxel octree created");

            e.Result = mesh;
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        internal void Parse()
        {
            var configDoc = new XmlDocument();

            configDoc.LoadXml(configXmlEditBox.Text);

            #region Log

            var logNode = configDoc.SelectSingleNode("/AthenaConfig/Log");
            if (logNode != null)
            {
                Log.Instance.MinLogLevel = logNode.Attributes["minLogLevel"] != null ? (LogLevel)Enum.Parse(typeof(LogLevel), logNode.Attributes["minLogLevel"].Value) : LogLevel.Anything;

                if (logNode.SelectSingleNode("Filename") != null && !string.IsNullOrEmpty(logNode.SelectSingleNode("Filename").InnerText))
                {
                    Log.Instance.Filename = logNode.SelectSingleNode("Filename").InnerText;
                }
            }

            #endregion

            #region Output

            var vsyncNode = configDoc.SelectSingleNode("/AthenaConfig/Output").Attributes["vSync"];
            if (vsyncNode != null)
            {
                VSync = (VSyncMode)Enum.Parse(typeof(VSyncMode), vsyncNode.Value);
            }

            var saveAfterRenderingNode = configDoc.SelectSingleNode("/AthenaConfig/Output").Attributes["saveAfterRendering"];
            if (saveAfterRenderingNode != null)
            {
                SaveOutputAfterRendering = bool.Parse(saveAfterRenderingNode.Value);
            }

            Fullscreen = false;
            var fullscreenNode = configDoc.SelectSingleNode("/AthenaConfig/Output").Attributes["fullscreen"];
            if (fullscreenNode != null)
            {
                Fullscreen = bool.Parse(fullscreenNode.Value);
            }

            OutputSize = new Vector2i(Int32.Parse(configDoc.SelectSingleNode("/AthenaConfig/Output/Resolution").Attributes["width"].Value), Int32.Parse(configDoc.SelectSingleNode("/AthenaConfig/Output/Resolution").Attributes["height"].Value));

            OutputImageFilename = null;
            var outputImageFilenameNode = configDoc.SelectSingleNode("/AthenaConfig/Output/Filename");
            if (outputImageFilenameNode != null)
            {
                OutputImageFilename = outputImageFilenameNode.Value;
            }

            #endregion

            #region Rendering

            NumberOfThreads = null;
            if (configDoc.SelectSingleNode("/AthenaConfig/Rendering").Attributes["threads"] != null)
            {
                NumberOfThreads = Int32.Parse(configDoc.SelectSingleNode("/AthenaConfig/Rendering").Attributes["threads"].Value);
            }

            NumberOfJobs = new Vector2i(Int32.Parse(configDoc.SelectSingleNode("/AthenaConfig/Rendering/JobCount").Attributes["width"].Value), Int32.Parse(configDoc.SelectSingleNode("/AthenaConfig/Rendering/JobCount").Attributes["height"].Value));
            //JobSize = new Vector2i(Int32.Parse(configDoc.SelectSingleNode("/AthenaConfig/Rendering/JobSize").Attributes["width"].Value), Int32.Parse(configDoc.SelectSingleNode("/AthenaConfig/Rendering/JobSize").Attributes["height"].Value));

            FramesToRender = null;
            var framesToRenderNode = configDoc.SelectSingleNode("/AthenaConfig/Rendering").Attributes["framesToRender"];
            if (framesToRenderNode != null)
            {
                FramesToRender = Int32.Parse(framesToRenderNode.Value);
            }

            if (FramesToRender == 0)
            {
                FramesToRender = null;
            }

            WaitForOutputRedraw = false;
            var waitForOutputRedrawNode = configDoc.SelectSingleNode("/AthenaConfig/Rendering").Attributes["waitForOutputRedraw"];
            if (waitForOutputRedrawNode != null)
            {
                WaitForOutputRedraw = bool.Parse(waitForOutputRedrawNode.Value);
            }

            RayTracer.BackgroundColor = null;

            var backgroundNode = configDoc.SelectSingleNode("/AthenaConfig/Output/Background");
            if (backgroundNode != null)
            {
                if (backgroundNode.Attributes["useRayDirectionVector"] != null && bool.Parse(backgroundNode.Attributes["useRayDirectionVector"].Value))
                {
                    RayTracer.BackgroundColor = null;
                }
                else if (backgroundNode.SelectSingleNode("Color") != null)
                {
                    RayTracer.BackgroundColor = ParseColorRGBNode(backgroundNode.SelectSingleNode("Color"));
                }
                else
                {
                    RayTracer.BackgroundColor = ColorRGB.Black;
                }
            }

            #region RayTracer

            RayTracer.MaxOctreeDepth = 8;

            var rayTracerNode = configDoc.SelectSingleNode("/AthenaConfig/Rendering/RayTracer");
            if (rayTracerNode != null)
            {
                if (rayTracerNode.Attributes["maxOctreeDepth"] != null)
                {
                    RayTracer.MaxOctreeDepth = Int32.Parse(rayTracerNode.Attributes["maxOctreeDepth"].Value);
                }
            }

            #endregion

            #endregion

            #region Scene

            Scene.Current = null;

            var sceneNode = configDoc.SelectSingleNode("/AthenaConfig/Scene");
            if (sceneNode == null)
            {
                throw new Exception("No scene found in configuration");
            }

            Scene.Current = new Scene(ParseName(sceneNode));

            var sceneMaxDepth           = 8;
            var sceneVoxelSizeThreshold = 1f;
            ParseVoxelOctreeNode(sceneNode.SelectSingleNode("VoxelOctree"), ref sceneMaxDepth, ref sceneVoxelSizeThreshold);

            VoxelOctree.GenerateOnMultipleThreads = false;
            var voxelOctreeNode = sceneNode.SelectSingleNode("VoxelOctree");
            if (voxelOctreeNode != null && voxelOctreeNode.Attributes["generateOnMultipleThreads"] != null)
            {
                VoxelOctree.GenerateOnMultipleThreads = bool.Parse(voxelOctreeNode.Attributes["generateOnMultipleThreads"].Value);
            }

            #region Camera

            var cameraNode = configDoc.SelectSingleNode("/AthenaConfig/Scene/Camera");
            if (cameraNode != null)
            {
                Scene.Current.Camera = new Camera(ParseVector3Node(cameraNode.SelectSingleNode("Position")), ParseVector3Node(cameraNode.SelectSingleNode("Target")));

                if (cameraNode.Attributes["fov"] != null)
                {
                    Scene.Current.Camera.FieldOfVision = float.Parse(cameraNode.Attributes["fov"].Value);
                }

                Scene.Current.Camera.MovementSpeed = cameraNode.SelectSingleNode("MovementSpeed") != null?ParseVector3Node(cameraNode.SelectSingleNode("MovementSpeed")) : new Athena.Core.DataTypes.Vector3(1);

                Scene.Current.Camera.RotationSpeed = cameraNode.SelectSingleNode("RotationSpeed") != null?ParseVector3Node(cameraNode.SelectSingleNode("RotationSpeed")) : new Athena.Core.DataTypes.Vector3(1);
            }

            #endregion

            #region Objects

            foreach (XmlNode objNode in sceneNode.SelectSingleNode("Objects").ChildNodes)
            {
                if (objNode is XmlComment)
                {
                    continue;
                }

                var position = ParseVector3Node(objNode.SelectSingleNode("Position"));
                var name     = ParseName(objNode);

                if (objNode.Name == "WavefrontObjMesh")
                {
                    #region Mesh

                    var mesh = new WavefrontObjMesh(objNode.SelectSingleNode("Filename").InnerText)
                    {
                        Position = position, Name = name
                    };

                    if (objNode.Attributes["voxelize"] == null || bool.Parse(objNode.Attributes["voxelize"].Value))
                    {
                        var maxDepth           = sceneMaxDepth;
                        var voxelSizeThreshold = sceneVoxelSizeThreshold;
                        ParseVoxelOctreeNode(objNode.SelectSingleNode("VoxelOctree"), ref maxDepth, ref voxelSizeThreshold);

                        mesh.GenerateOctree(maxDepth, voxelSizeThreshold);
                    }

                    #endregion

                    Scene.Current.AddObject(mesh);
                }

                if (objNode.Name == "Terrain")
                {
                    #region Terrain

                    var heightMap = null as float[];
                    if (objNode.Attributes["generationMethod"].Value == "MidPointDisplacement")
                    {
                        var midPointDisplacementNode = objNode.SelectSingleNode("MidPointDisplacement");
                        var heightMapSize            = int.Parse(midPointDisplacementNode.Attributes["size"].Value);
                        var roughness = float.Parse(midPointDisplacementNode.Attributes["roughness"].Value);

                        int?seed = null;
                        if (midPointDisplacementNode.Attributes["seed"] != null)
                        {
                            seed = int.Parse(midPointDisplacementNode.Attributes["seed"].Value);
                        }

                        heightMap = HeightMapGenerator.GenerateWithMidPointDisplacement(heightMapSize, roughness, seed);
                    }

                    var size      = float.Parse(objNode.Attributes["size"].Value);
                    var maxHeight = float.Parse(objNode.Attributes["maxHeight"].Value);

                    var terrain = new Terrain(size, maxHeight, heightMap)
                    {
                        Position = position, Name = name
                    };

                    if (objNode.Attributes["voxelize"] == null || bool.Parse(objNode.Attributes["voxelize"].Value))
                    {
                        var maxDepth           = sceneMaxDepth;
                        var voxelSizeThreshold = sceneVoxelSizeThreshold;
                        ParseVoxelOctreeNode(objNode.SelectSingleNode("VoxelOctree"), ref maxDepth, ref voxelSizeThreshold);

                        terrain.GenerateOctree(maxDepth, voxelSizeThreshold);
                    }

                    #endregion

                    Scene.Current.AddObject(terrain);
                }

                else if (objNode.Name == "Sphere")
                {
                    #region Sphere

                    var sphere = new Sphere()
                    {
                        Position = position, Name = name
                    };

                    sphere.Radius = float.Parse(objNode.Attributes["radius"].Value);

                    if (objNode.Attributes["voxelize"] == null || bool.Parse(objNode.Attributes["voxelize"].Value))
                    {
                        var maxDepth           = sceneMaxDepth;
                        var voxelSizeThreshold = sceneVoxelSizeThreshold;
                        ParseVoxelOctreeNode(objNode.SelectSingleNode("VoxelOctree"), ref maxDepth, ref voxelSizeThreshold);

                        sphere.Octree = VoxelOctree.Create(sphere, maxDepth, voxelSizeThreshold);
                    }

                    #endregion

                    Scene.Current.AddObject(sphere);
                }

                else if (objNode.Name == "OmniLight")
                {
                    #region OmniLight

                    var omniLight = new OmniLight(float.Parse(objNode.Attributes["radius"].Value))
                    {
                        Position = position, Name = name
                    };

                    omniLight.Color = ParseColorRGBNode(objNode.SelectSingleNode("Color"));

                    if (objNode.Attributes["intensity"] != null)
                    {
                        omniLight.Intensity = float.Parse(objNode.Attributes["intensity"].Value);
                    }

                    #endregion

                    Scene.Current.AddObject(omniLight);
                }
            }

            #endregion

            #endregion
        }