Exemple #1
0
        public OcclusionCullingScreen(IServiceLocator services)
            : base(services)
        {
            _sceneNodes = new List <SceneNode>();

            // Create new occlusion buffer with default settings.
            OcclusionBuffer = new OcclusionBuffer(GraphicsService);
            OcclusionBuffer.ProgressiveShadowCasterCulling = true;

            EnableCulling = true;

            // Create a second camera for rendering a top-down view of the scene.
            var topDownPerspective = new PerspectiveProjection();

            topDownPerspective.SetFieldOfView(MathHelper.ToRadians(90), 1, 1, 512);
            _topDownCameraNode           = new CameraNode(new Camera(topDownPerspective));
            _topDownCameraNode.PoseWorld = new Pose(new Vector3F(-10, 120, -10));
            _topDownCameraNode.LookAt(new Vector3F(-10, 0, -10), Vector3F.UnitZ);

            _sceneQuery    = new CustomSceneQuery();
            _debugRenderer = new DebugRenderer(GraphicsService, null, null);

            // The DigitalRune Profiler is used to measure execution times.
            Profiler.SetFormat("Occlusion.Render", 1e3f, "[ms]");
            Profiler.SetFormat("Occlusion.Query", 1e3f, "[ms]");
        }
Exemple #2
0
        public WpRagdollSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            GraphicsScreen.ClearBackground = true;
            GraphicsScreen.BackgroundColor = Color.CornflowerBlue;

            // Set a fixed camera.
            var projection = new PerspectiveProjection();

            projection.SetFieldOfView(
                MathHelper.ToRadians(30),
                GraphicsService.GraphicsDevice.Viewport.AspectRatio,
                1f,
                100.0f);
            Vector3F cameraTarget   = new Vector3F(0, 1, 0);
            Vector3F cameraPosition = new Vector3F(0, 12, 0);
            Vector3F cameraUpVector = new Vector3F(0, 0, -1);

            GraphicsScreen.CameraNode = new CameraNode(new Camera(projection))
            {
                View = Matrix44F.CreateLookAt(cameraPosition, cameraTarget, cameraUpVector),
            };

            InitializePhysics();
        }
Exemple #3
0
        public MyGraphicsScreen(IGraphicsService graphicsService)
            : base(graphicsService)
        {
            _meshRenderer = new MeshRenderer();

            var contentManager = ServiceLocator.Current.GetInstance <ContentManager>();
            var spriteFont     = contentManager.Load <SpriteFont>("SpriteFont1");

            _debugRenderer = new DebugRenderer(graphicsService, spriteFont);

            Scene = new Scene();

            // Add a camera with a perspective projection.
            var projection = new PerspectiveProjection();

            projection.SetFieldOfView(
                ConstantsF.PiOver4,
                graphicsService.GraphicsDevice.Viewport.AspectRatio,
                0.1f,
                100.0f);
            CameraNode = new CameraNode(new Camera(projection))
            {
                Name = "CameraPerspective"
            };
            Scene.Children.Add(CameraNode);
        }
        public void SetProjectionFieldOfViewTest()
        {
            PerspectiveProjection projection = new PerspectiveProjection();

            projection.SetFieldOfView(MathHelper.ToRadians(60), 16.0f / 9.0f, 1, 10);

            PerspectiveProjection projection2 = new PerspectiveProjection();

            projection2.SetFieldOfView(MathHelper.ToRadians(60), 16.0f / 9.0f);
            projection2.Near = 1;
            projection2.Far  = 10;

            Projection projection3 = new PerspectiveProjection
            {
                Left   = -2.0528009f / 2.0f,
                Right  = 2.0528009f / 2.0f,
                Bottom = -1.1547005f / 2.0f,
                Top    = 1.1547005f / 2.0f,
                Near   = 1,
                Far    = 10,
            };

            Matrix44F expected = Matrix44F.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60), 16.0f / 9.0f, 1, 10);

            Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, projection));
            Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, projection2));
            Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, projection3.ToMatrix44F()));
        }
Exemple #5
0
        public CharacterControllerSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            SampleFramework.IsMouseVisible = false;
            GraphicsScreen.ClearBackground = true;
            GraphicsScreen.BackgroundColor = Color.CornflowerBlue;

            // Create a camera.
            var projection = new PerspectiveProjection();

            projection.SetFieldOfView(
                ConstantsF.PiOver4,
                GraphicsService.GraphicsDevice.Viewport.AspectRatio,
                0.1f,
                1000.0f);
            _cameraNode = new CameraNode(new Camera(projection));
            GraphicsScreen.CameraNode = _cameraNode;

            // We use one collision domain that computes collision info for all game objects.
            _domain = new CollisionDomain(new CollisionDetection());

            // Create collision objects for a test level.
            CharacterControllerLevel.Load(_domain);

            // Add collision filter:
            // The _domain contains a lot of collision objects for obstacles in the level.
            // We do not need to compute contacts between these static obstacles. To avoid
            // this, the CharacterControllerLevel puts all level collision objects into
            // the collision group 1. We add a broad phase collision filter which filters out
            // collision checks between objects of collision group 1.
            _domain.BroadPhase.Filter = new DelegatePairFilter <CollisionObject>(
                pair =>
            {
                if (pair.First.CollisionGroup == 1 && pair.Second.CollisionGroup == 1)
                {
                    return(false);
                }

                return(true);
            });

            // Create character controller.
            _character          = new CharacterController(_domain);
            _character.Position = new Vector3F(0, 0, 1);

            // Create the trigger volume.
            _triggerVolume = new CollisionObject(
                new GeometricObject(new SphereShape(3), new Pose(new Vector3F(-5, 0, 5))))
            {
                // We do not want to compute detailed contact information (contact points, contact
                // normal vectors, etc.). We are only interested if the object touches another object or not.
                // Therefore, we set the collision object type to "trigger". Trigger objects are better for
                // performance than normal collision objects. Additionally, the character controller should
                // be able to walk through the trigger volume. The character controller treats objects as
                // solids if it finds contact information (contact positions with contact normal vectors).
                Type = CollisionObjectType.Trigger
            };
            _domain.CollisionObjects.Add(_triggerVolume);
        }
Exemple #6
0
        public void GetScreenSizeWithPerspective()
        {
            // Camera
            var projection = new PerspectiveProjection();

            projection.SetFieldOfView(MathHelper.ToRadians(90), 2.0f / 1.0f, 1.0f, 100f);
            var camera     = new Camera(projection);
            var cameraNode = new CameraNode(camera);

            cameraNode.PoseWorld = new Pose(new Vector3(123, 456, -789), Matrix.CreateRotation(new Vector3(1, -2, 3), MathHelper.ToRadians(75)));

            // 2:1 viewport
            var viewport = new Viewport(10, 10, 200, 100);

            // Test object
            var shape           = new SphereShape();
            var geometricObject = new GeometricObject(shape);

            // Empty sphere at camera position.
            shape.Radius         = 0;
            geometricObject.Pose = cameraNode.PoseWorld;
            Vector2F screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject);

            Assert.AreEqual(0, screenSize.X);
            Assert.AreEqual(0, screenSize.Y);

            // Empty sphere centered at near plane.
            shape.Radius         = 0;
            geometricObject.Pose = cameraNode.PoseWorld * new Pose(new Vector3(0.123f, -0.543f, -1));
            screenSize           = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject);
            Assert.AreEqual(0, screenSize.X);
            Assert.AreEqual(0, screenSize.Y);

            // Create sphere which as a bounding sphere of ~1 unit diameter:
            // Since the bounding sphere is based on the AABB, we need to make the
            // actual sphere a bit smaller.
            shape.Radius = 1 / (2 * (float)Math.Sqrt(3)) + Numeric.EpsilonF;

            // Sphere at camera position.
            geometricObject.Pose = cameraNode.PoseWorld;
            screenSize           = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject);
            Assert.Greater(screenSize.X, 200);
            Assert.Greater(screenSize.Y, 100);

            // Sphere at near plane.
            geometricObject.Pose = cameraNode.PoseWorld * new Pose(new Vector3(0.123f, -0.543f, -1));
            screenSize           = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject);
            Assert.IsTrue(Numeric.AreEqual(screenSize.X, 50.0f, 10f));
            Assert.IsTrue(Numeric.AreEqual(screenSize.Y, 50.0f, 10f));

            // Double distance --> half size
            geometricObject.Pose = cameraNode.PoseWorld * new Pose(new Vector3(0.123f, -0.543f, -2));
            screenSize           = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject);
            Assert.IsTrue(Numeric.AreEqual(screenSize.X, 25.0f, 5f));
            Assert.IsTrue(Numeric.AreEqual(screenSize.Y, 25.0f, 5f));
        }
Exemple #7
0
        private void InitializeGraphicsScreen()
        {
            Debug.Assert(GraphicsScreens.Count == 0, "Reset graphics screens before calling InitializeGraphicsScreen().");

            var services        = Document.Editor.Services;
            var graphicsService = services.GetInstance <IGraphicsService>().ThrowIfMissing();

            // Initialize graphics screens.
            var graphicsScreen = UseDeferredLighting
                                 ? (GraphicsScreen) new DeferredGraphicsScreen(services)
                                 : new BasicGraphicsScreen(services);

            GraphicsScreens.Add(graphicsScreen);
            GraphicsScreens.Add(new DebugGraphicsScreen(services));

            // Add default lighting.
            var scene = UseDeferredLighting
                        ? ((DeferredGraphicsScreen)graphicsScreen).Scene
                        : ((BasicGraphicsScreen)graphicsScreen).Scene;

            GameHelper.AddLights(scene);

            // Add a ground plane (useful for orientation and to check model shadows).
            if (_groundModelNode == null)
            {
                var content = services.GetInstance <ContentManager>().ThrowIfMissing();
                _groundModelNode = content.Load <ModelNode>("DigitalRune.Editor.Game/Models/Misc/GroundPlane/GroundPlane").Clone();
            }
            _groundModelNode.IsEnabled = ShowGroundPlane;
            scene.Children.Add(_groundModelNode);

            // Add camera.
            if (CameraNode == null)
            {
                var projection = new PerspectiveProjection();
                projection.SetFieldOfView(
                    ConstantsF.PiOver4,
                    graphicsService.GraphicsDevice.Viewport.AspectRatio,
                    0.1f,
                    10000.0f);
                CameraNode = new CameraNode(new Camera(projection))
                {
                    Name = "CameraPerspective",
                };
            }

            if (UseDeferredLighting)
            {
                ((DeferredGraphicsScreen)graphicsScreen).ActiveCameraNode = CameraNode;
            }
            else
            {
                ((BasicGraphicsScreen)graphicsScreen).CameraNode = CameraNode;
            }
        }
Exemple #8
0
        //--------------------------------------------------------------
        #region Methods
        //--------------------------------------------------------------

        protected override void OnLoad()
        {
            var graphicsService = _services.GetInstance <IGraphicsService>();

            // Define camera projection.
            var projection = new PerspectiveProjection();

            projection.SetFieldOfView(
                ConstantsF.PiOver4,
                graphicsService.GraphicsDevice.Viewport.AspectRatio,
                0.1f,
                1000.0f);

            // Create a camera node.
            CameraNode = new CameraNode(new Camera(projection));
        }
Exemple #9
0
        public WindowsPhoneSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            GraphicsScreen.ClearBackground = true;
            GraphicsScreen.BackgroundColor = Color.CornflowerBlue;

            // Set a fixed camera.
            var projection = new PerspectiveProjection();

            projection.SetFieldOfView(
                MathHelper.ToRadians(30),
                GraphicsService.GraphicsDevice.Viewport.AspectRatio,
                1f,
                1000.0f);
            Vector3F cameraTarget   = new Vector3F(0, 1, 0);
            Vector3F cameraPosition = new Vector3F(0, 12, 0);
            Vector3F cameraUpVector = new Vector3F(0, 0, -1);

            GraphicsScreen.CameraNode = new CameraNode(new Camera(projection))
            {
                View = Matrix44F.CreateLookAt(cameraPosition, cameraTarget, cameraUpVector),
            };

            // We use the accelerometer to control the camera view. The accelerometer registers every
            // little change, but we do not want a shaky camera. We can use a low-pass filter to smooth
            // the sensor signal.
            _lowPassFilter = new LowPassFilter(new Vector3F(0, -1, 0))
            {
                TimeConstant = 0.15f, // Let's try a time constant of 0.15 seconds.
                // When increasing the time constant the camera becomes more stable,
                // but also slower.
            };

            // Enable touch gestures
            _originalEnabledGestures   = TouchPanel.EnabledGestures;
            TouchPanel.EnabledGestures =
                GestureType.Tap      // Tap is used to drop new bodies.
                | GestureType.Flick  // Flick creates an explosion.
                | GestureType.Hold   // Hold to clear the scene.
                | GestureType.Pinch; // Pinch can be used to zoom in or out.

            InitializePhysics();
        }
Exemple #10
0
        public void UnprojectTest()
        {
            Viewport viewport = new Viewport(0, 0, 640, 480);
            PerspectiveProjection projection = new PerspectiveProjection();

            projection.SetFieldOfView(MathHelper.ToRadians(60), viewport.AspectRatio, 10, 1000);
            Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 0), new Vector3(0, 0, -1), Vector3.Up);

            Assert.IsTrue(Vector3.AreNumericallyEqual(new Vector3(0, 0, -10), viewport.Unproject(new Vector3(320, 240, 0), projection, view)));
            Assert.IsTrue(Vector3.AreNumericallyEqual(new Vector3(projection.Left, projection.Top, -10), viewport.Unproject(new Vector3(0, 0, 0), projection, view)));
            Assert.IsTrue(Vector3.AreNumericallyEqual(new Vector3(projection.Right, projection.Top, -10), viewport.Unproject(new Vector3(640, 0, 0), projection, view)));
            Assert.IsTrue(Vector3.AreNumericallyEqual(new Vector3(projection.Left, projection.Bottom, -10), viewport.Unproject(new Vector3(0, 480, 0), projection, view)));
            Assert.IsTrue(Vector3.AreNumericallyEqual(new Vector3(projection.Right, projection.Bottom, -10), viewport.Unproject(new Vector3(640, 480, 0), projection, view)));

            Vector3[] farCorners = new Vector3[4];
            GraphicsHelper.GetFrustumFarCorners(projection, farCorners);
            Assert.IsTrue(Vector3.AreNumericallyEqual((Vector3)farCorners[0], viewport.Unproject(new Vector3(0, 0, 1), projection, view)));
            Assert.IsTrue(Vector3.AreNumericallyEqual((Vector3)farCorners[1], viewport.Unproject(new Vector3(640, 0, 1), projection, view)));
            Assert.IsTrue(Vector3.AreNumericallyEqual((Vector3)farCorners[2], viewport.Unproject(new Vector3(0, 480, 1), projection, view)));
            Assert.IsTrue(Vector3.AreNumericallyEqual((Vector3)farCorners[3], viewport.Unproject(new Vector3(640, 480, 1), projection, view)));
        }
Exemple #11
0
        //--------------------------------------------------------------
        #region Methods
        //--------------------------------------------------------------

        protected override void OnLoad()
        {
            // Get services.
            _inputService    = _services.GetInstance <IInputService>();
            _graphicsService = _services.GetInstance <IGraphicsService>();
            _scene           = _services.GetInstance <IScene>();
            var content           = _services.GetInstance <ContentManager>();
            var gameObjectService = _services.GetInstance <IGameObjectService>();

            // Get camera game object.
            _cameraObject = (CameraObject)gameObjectService.Objects["Camera"];

            // Create SkyNodes.
            InitializeSky(content);

            // Create LightNodes
            InitializeLights();

            // Optionally, the sky is captured into a cube map and the cube map is added
            // to the scene instead of all the sky nodes.
            if (_cacheSky)
            {
                // We use a SceneCaptureNode to create the cube map.
                // The cube map uses RGBM encoding to store HDR values.
                var renderToTexture = new RenderToTexture
                {
                    Texture = new RenderTargetCube(
                        _graphicsService.GraphicsDevice,
#if XBOX
                        512,
#else
                        1024,
#endif
                        true,
                        SurfaceFormat.Color,
                        DepthFormat.None),
                };
                var projection = new PerspectiveProjection();
                projection.SetFieldOfView(ConstantsF.PiOver2, 1, 1, 10);
                _sceneCaptureNode = new SceneCaptureNode(renderToTexture)
                {
                    // Note: The scene is captured at the origin (0, 0, 0).
                    CameraNode = new CameraNode(new Camera(projection)),
                };

                SkyboxNode = new SkyboxNode
                {
                    Encoding = ColorEncoding.Rgbm,
                    Texture  = (TextureCube)renderToTexture.Texture,
                };
                _scene.Children.Add(SkyboxNode);
            }

            // The Ephemeris class computes the positions of the sun and the moon as seen
            // from any place on the earth.
            _ephemeris = new Ephemeris
            {
                // Seattle, Washington
                //Latitude = 47,
                //Longitude = 122,
                //Altitude = 100

                // Vienna
                //Latitude = 48,
                //Longitude = -16,
                //Altitude = 0

                // Equator
                Latitude  = 0,
                Longitude = 0,
                Altitude  = 0
            };

            // Update the positions of sky objects and the lights.
            UpdateSky();

            // Create cube map.
            if (_cacheSky)
            {
                UpdateCubeMap(TimeSpan.Zero);
            }

            // Add GUI controls to the Options window.
            var sampleFramework = _services.GetInstance <SampleFramework>();
            var optionsPanel    = sampleFramework.AddOptions("Game Objects");
            var panel           = SampleHelper.AddGroupBox(optionsPanel, "DynamicSkyObject");

            SampleHelper.AddSlider(
                panel,
                "Time",
                "F2",
                0,
                24,
                (float)Time.TimeOfDay.TotalHours,
                value =>
            {
                var time = Time;
                time     = time.Subtract(time.TimeOfDay).Add(TimeSpan.FromHours(value));
                Time     = time;
            });

            SampleHelper.AddCheckBox(
                panel,
                "Enable ambient light",
                EnableAmbientLight,
                isChecked => EnableAmbientLight = isChecked);

            SampleHelper.AddSlider(
                panel,
                "Fog sample angle",
                "F2",
                0,
                ConstantsF.PiOver2,
                FogSampleAngle,
                value => FogSampleAngle = value);

            SampleHelper.AddSlider(
                panel,
                "Fog saturation",
                "F2",
                0,
                1,
                FogSaturation,
                value => FogSaturation = value);

            //SampleHelper.AddSlider(
            //  panel,
            //  "Fog scattering symmetry R",
            //  "F2",
            //  0,
            //  1,
            //  FogScatteringSymmetry.X,
            //  value => FogScatteringSymmetry = new Vector3F(value, FogScatteringSymmetry.Y, FogScatteringSymmetry.Z));
            //SampleHelper.AddSlider(
            //  panel,
            //  "Fog scattering symmetry G",
            //  "F2",
            //  0,
            //  1,
            //  FogScatteringSymmetry.Y,
            //  value => FogScatteringSymmetry = new Vector3F(FogScatteringSymmetry.X, value, FogScatteringSymmetry.Z));
            //SampleHelper.AddSlider(
            //  panel,
            //  "Fog scattering symmetry B",
            //  "F2",
            //  0,
            //  1,
            //  FogScatteringSymmetry.Z,
            //  value => FogScatteringSymmetry = new Vector3F(FogScatteringSymmetry.X, FogScatteringSymmetry.Y, value));
        }
        /// <summary>
        /// Renders the environment maps for the image-based lights.
        /// </summary>
        /// <remarks>
        /// This method uses the current DeferredGraphicsScreen to render new environment maps at
        /// runtime. The DeferredGraphicsScreen has a SceneCaptureRenderer which we can use to
        /// capture environment maps of the current scene.
        /// To capture new environment maps the flag _updateEnvironmentMaps must be set to true.
        /// When this flag is set, SceneCaptureNodes are added to the scene. When the graphics
        /// screen calls the SceneCaptureRenderer the next time, the new environment maps will be
        /// captured.
        /// The flag _updateEnvironmentMaps remains true until the new environment maps are available.
        /// This method checks the SceneCaptureNode.LastFrame property to check if new environment maps
        /// have been computed. Usually, the environment maps will be available in the next frame.
        /// (However, the XNA Game class can skip graphics rendering if the game is running slowly.
        /// Then we would have to wait more than 1 frame.)
        /// When environment maps are being rendered, the image-based lights are disabled to capture
        /// only the scene with ambient and directional lights. Dynamic objects are also disabled
        /// to capture only the static scene.
        /// </remarks>
        private void UpdateEnvironmentMaps()
        {
            if (!_updateEnvironmentMaps)
            {
                return;
            }

            // One-time initializations:
            if (_sceneCaptureNodes[0] == null)
            {
                // Create cube maps and scene capture nodes.
                // (Note: A cube map size of 256 is enough for surfaces with a specular power
                // in the range [0, 200000].)
                for (int i = 0; i < _sceneCaptureNodes.Length; i++)
                {
                    var renderTargetCube = new RenderTargetCube(
                        GraphicsService.GraphicsDevice,
                        256,
                        true,
                        SurfaceFormat.Color,
                        DepthFormat.None);

                    var renderToTexture = new RenderToTexture {
                        Texture = renderTargetCube
                    };
                    var projection = new PerspectiveProjection();
                    projection.SetFieldOfView(ConstantsF.PiOver2, 1, 1, 100);
                    _sceneCaptureNodes[i] = new SceneCaptureNode(renderToTexture)
                    {
                        CameraNode = new CameraNode(new Camera(projection))
                        {
                            PoseWorld = _lightNodes[i].PoseWorld,
                        },
                    };

                    _imageBasedLights[i].Texture = renderTargetCube;
                }

                // We use a ColorEncoder to encode a HDR image in a normal Color texture.
                _colorEncoder = new ColorEncoder(GraphicsService)
                {
                    SourceEncoding = ColorEncoding.Rgb,
                    TargetEncoding = ColorEncoding.Rgbm,
                };

                // The SceneCaptureRenderer has a render callback which defines what is rendered
                // into the scene capture render targets.
                _graphicsScreen.SceneCaptureRenderer.RenderCallback = context =>
                {
                    var graphicsDevice   = GraphicsService.GraphicsDevice;
                    var renderTargetPool = GraphicsService.RenderTargetPool;

                    // Get scene nodes which are visible by the current camera.
                    CustomSceneQuery sceneQuery = context.Scene.Query <CustomSceneQuery>(context.CameraNode, context);

                    // The final image has to be rendered into this render target.
                    var ldrTarget = context.RenderTarget;

                    // Use an intermediate HDR render target with the same resolution as the final target.
                    var format = new RenderTargetFormat(ldrTarget)
                    {
                        SurfaceFormat      = SurfaceFormat.HdrBlendable,
                        DepthStencilFormat = DepthFormat.Depth24Stencil8
                    };
                    var hdrTarget = renderTargetPool.Obtain2D(format);

                    graphicsDevice.SetRenderTarget(hdrTarget);
                    context.RenderTarget = hdrTarget;

                    // Render scene (without post-processing, without lens flares, no debug rendering, no reticle).
                    _graphicsScreen.RenderScene(sceneQuery, context, false, false, false, false);

                    // Convert the HDR image to RGBM image.
                    context.SourceTexture = hdrTarget;
                    context.RenderTarget  = ldrTarget;
                    _colorEncoder.Process(context);
                    context.SourceTexture = null;

                    // Clean up.
                    renderTargetPool.Recycle(hdrTarget);
                    context.RenderTarget = ldrTarget;
                };
            }

            if (_sceneCaptureNodes[0].Parent == null)
            {
                // Add the scene capture nodes to the scene.
                for (int i = 0; i < _sceneCaptureNodes.Length; i++)
                {
                    _graphicsScreen.Scene.Children.Add(_sceneCaptureNodes[i]);
                }

                // Remember the old time stamp of the nodes.
                _oldEnvironmentMapTimeStamp = _sceneCaptureNodes[0].LastFrame;

                // Disable all lights except ambient and directional lights.
                // We do not capture the image-based lights or any other lights (e.g. point lights)
                // in the cube map.
                foreach (var lightNode in _graphicsScreen.Scene.GetDescendants().OfType <LightNode>())
                {
                    lightNode.IsEnabled = (lightNode.Light is AmbientLight) || (lightNode.Light is DirectionalLight);
                }

                // Disable dynamic objects.
                foreach (var node in _graphicsScreen.Scene.GetDescendants())
                {
                    if (node is MeshNode || node is LodGroupNode)
                    {
                        if (!node.IsStatic)
                        {
                            node.IsEnabled = false;
                        }
                    }
                }
            }
            else
            {
                // The scene capture nodes are part of the scene. Check if they have been
                // updated.
                if (_sceneCaptureNodes[0].LastFrame != _oldEnvironmentMapTimeStamp)
                {
                    // We have new environment maps. Restore the normal scene.
                    for (int i = 0; i < _sceneCaptureNodes.Length; i++)
                    {
                        _graphicsScreen.Scene.Children.Remove(_sceneCaptureNodes[i]);
                    }

                    _updateEnvironmentMaps = false;

                    foreach (var lightNode in _graphicsScreen.Scene.GetDescendants().OfType <LightNode>())
                    {
                        lightNode.IsEnabled = true;
                    }

                    foreach (var node in _graphicsScreen.Scene.GetDescendants())
                    {
                        if (node is MeshNode || node is LodGroupNode)
                        {
                            if (!node.IsStatic)
                            {
                                node.IsEnabled = true;
                            }
                        }
                    }
                }
            }
        }
        public FacialAnimationSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            _graphicsScreen = new DeferredGraphicsScreen(Services)
            {
                DrawReticle = false
            };
            GraphicsService.Screens.Insert(0, _graphicsScreen);
            Services.Register(typeof(DebugRenderer), null, _graphicsScreen.DebugRenderer);
            Services.Register(typeof(IScene), null, _graphicsScreen.Scene);

            // Add a game object which adds some GUI controls for the deferred graphics
            // screen to the Options window.
            GameObjectService.Objects.Add(new DeferredGraphicsOptionsObject(Services));

            // Use a fixed camera.
            var projection = new PerspectiveProjection();

            projection.SetFieldOfView(
                ConstantsF.PiOver4,
                GraphicsService.GraphicsDevice.Viewport.AspectRatio,
                0.1f,
                10);
            var cameraNode = new CameraNode(new Camera(projection));

            cameraNode.LookAt(new Vector3F(0.15f, 0.15f, 0.5f), new Vector3F(0.1f, 0.15f, 0), Vector3F.Up);
            _graphicsScreen.Scene.Children.Add(cameraNode);
            _graphicsScreen.ActiveCameraNode = cameraNode;

            // Lighting setup:
            var keyLight = new LightNode(new Spotlight {
                DiffuseIntensity = 0.6f, SpecularIntensity = 0.4f
            });

            keyLight.LookAt(new Vector3F(-2, 2, 2), new Vector3F(), Vector3F.Up);
            _graphicsScreen.Scene.Children.Add(keyLight);

            var backLight = new LightNode(new Spotlight {
                DiffuseIntensity = 0.3f, SpecularIntensity = 0.3f
            });

            backLight.LookAt(new Vector3F(1, 0.5f, -2), new Vector3F(), Vector3F.Up);
            _graphicsScreen.Scene.Children.Add(backLight);

            var fillLight = new LightNode(new AmbientLight {
                HemisphericAttenuation = 1, Intensity = 0.1f
            });

            _graphicsScreen.Scene.Children.Add(fillLight);

            // The scene does not have a proper background. That's why the exposure is a
            // bit off. --> Reduce the max exposure.
            var hdrFilter = _graphicsScreen.PostProcessors.OfType <HdrFilter>().First();

            hdrFilter.MaxExposure = 6;

            // Load the customized "Sintel" model (original: Durian Open Movie Project - http://www.sintel.org/).
            var model = ContentManager.Load <ModelNode>("Sintel/Sintel-Head").Clone();

            model.PoseWorld = new Pose(new Vector3F(0, 0, 0), Matrix33F.CreateRotationY(MathHelper.ToRadians(10)) * Matrix33F.CreateRotationX(-MathHelper.ToRadians(90)));
            _graphicsScreen.Scene.Children.Add(model);

            // The model consists of a root node and a mesh node.
            //  ModelNode "Sintel-Head"
            //    MeshNode "Sintel"
            _sintel = (MeshNode)model.Children[0];

            // The model contains two skeletal animations:
            // - "MOUTH-open" is just a single frame.
            // - "Test" is a short animation (250 frames).

            // In the Options window, we will add a slider to move the jaw.
            // Slider.Value = 0 ... mouth closed (default)
            _mouthClosedPose = SkeletonPose.Create(_sintel.Mesh.Skeleton);
            // Slider.Value = 1 ... mouth open (copied from the "MOUTH-open" animation)
            SkeletonKeyFrameAnimation mouthOpen = _sintel.Mesh.Animations["MOUTH-open"];

            _mouthOpenPose = SkeletonPose.Create(_sintel.Mesh.Skeleton);
            mouthOpen.GetValue(TimeSpan.Zero, ref _mouthOpenPose, ref _mouthOpenPose, ref _mouthOpenPose);

            // Turn the "Test" animation into an endless loop.
            _skeletalAnimation = new AnimationClip <SkeletonPose>(_sintel.Mesh.Animations["Test"])
            {
                Duration     = TimeSpan.MaxValue,
                LoopBehavior = LoopBehavior.Cycle
            };

            // Mesh has several morph targets for facial animation, which are imported
            // automatically via the content pipeline. Unfortunately, the XNA content
            // pipeline cannot import morph target animations automatically.
            // In this demo, we will create a morph target animation in code.
            _morphingAnimation = CreateMorphingAnimation();

            CreateGuiControls();
        }
        public void GetScreenSizeWithPerspective()
        {
            // Camera
              var projection = new PerspectiveProjection();
              projection.SetFieldOfView(MathHelper.ToRadians(90), 2.0f / 1.0f, 1.0f, 100f);
              var camera = new Camera(projection);
              var cameraNode = new CameraNode(camera);
              cameraNode.PoseWorld = new Pose(new Vector3F(123, 456, -789), Matrix33F.CreateRotation(new Vector3F(1, -2, 3), MathHelper.ToRadians(75)));

              // 2:1 viewport
              var viewport = new Viewport(10, 10, 200, 100);

              // Test object
              var shape = new SphereShape();
              var geometricObject = new GeometricObject(shape);

              // Empty sphere at camera position.
              shape.Radius = 0;
              geometricObject.Pose = cameraNode.PoseWorld;
              Vector2F screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject);
              Assert.AreEqual(0, screenSize.X);
              Assert.AreEqual(0, screenSize.Y);

              // Empty sphere centered at near plane.
              shape.Radius = 0;
              geometricObject.Pose = cameraNode.PoseWorld * new Pose(new Vector3F(0.123f, -0.543f, -1));
              screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject);
              Assert.AreEqual(0, screenSize.X);
              Assert.AreEqual(0, screenSize.Y);

              // Create sphere which as a bounding sphere of ~1 unit diameter:
              // Since the bounding sphere is based on the AABB, we need to make the
              // actual sphere a bit smaller.
              shape.Radius = 1 / (2 * (float)Math.Sqrt(3)) + Numeric.EpsilonF;

              // Sphere at camera position.
              geometricObject.Pose = cameraNode.PoseWorld;
              screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject);
              Assert.Greater(screenSize.X, 200);
              Assert.Greater(screenSize.Y, 100);

              // Sphere at near plane.
              geometricObject.Pose = cameraNode.PoseWorld * new Pose(new Vector3F(0.123f, -0.543f, -1));
              screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject);
              Assert.IsTrue(Numeric.AreEqual(screenSize.X, 50.0f, 10f));
              Assert.IsTrue(Numeric.AreEqual(screenSize.Y, 50.0f, 10f));

              // Double distance --> half size
              geometricObject.Pose = cameraNode.PoseWorld * new Pose(new Vector3F(0.123f, -0.543f, -2));
              screenSize = GraphicsHelper.GetScreenSize(cameraNode, viewport, geometricObject);
              Assert.IsTrue(Numeric.AreEqual(screenSize.X, 25.0f, 5f));
              Assert.IsTrue(Numeric.AreEqual(screenSize.Y, 25.0f, 5f));
        }
        public void UnprojectTest()
        {
            Viewport viewport = new Viewport(0, 0, 640, 480);
              PerspectiveProjection projection = new PerspectiveProjection();
              projection.SetFieldOfView(MathHelper.ToRadians(60), viewport.AspectRatio, 10, 1000);
              Matrix44F view = Matrix44F.CreateLookAt(new Vector3F(0, 0, 0), new Vector3F(0, 0, -1), Vector3F.Up);

              Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(0, 0, -10), viewport.Unproject(new Vector3F(320, 240, 0), projection, view)));
              Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(projection.Left, projection.Top, -10), viewport.Unproject(new Vector3F(0, 0, 0), projection, view)));
              Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(projection.Right, projection.Top, -10), viewport.Unproject(new Vector3F(640, 0, 0), projection, view)));
              Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(projection.Left, projection.Bottom, -10), viewport.Unproject(new Vector3F(0, 480, 0), projection, view)));
              Assert.IsTrue(Vector3F.AreNumericallyEqual(new Vector3F(projection.Right, projection.Bottom, -10), viewport.Unproject(new Vector3F(640, 480, 0), projection, view)));

              Vector3[] farCorners = new Vector3[4];
              GraphicsHelper.GetFrustumFarCorners(projection, farCorners);
              Assert.IsTrue(Vector3F.AreNumericallyEqual((Vector3F)farCorners[0], viewport.Unproject(new Vector3F(0, 0, 1), projection, view)));
              Assert.IsTrue(Vector3F.AreNumericallyEqual((Vector3F)farCorners[1], viewport.Unproject(new Vector3F(640, 0, 1), projection, view)));
              Assert.IsTrue(Vector3F.AreNumericallyEqual((Vector3F)farCorners[2], viewport.Unproject(new Vector3F(0, 480, 1), projection, view)));
              Assert.IsTrue(Vector3F.AreNumericallyEqual((Vector3F)farCorners[3], viewport.Unproject(new Vector3F(640, 480, 1), projection, view)));
        }
Exemple #16
0
        private void Render(RenderContext context)
        {
            var originalRenderTarget = context.RenderTarget;
            var originalViewport     = context.Viewport;

            var graphicsDevice = context.GraphicsService.GraphicsDevice;

            if (_updateCubeMap)
            {
                _updateCubeMap = false;

                _cloudMapRenderer.Render(_skyNodes, context);

                // Create a camera with 45° FOV for a single cube map face.
                var perspectiveProjection = new PerspectiveProjection();
                perspectiveProjection.SetFieldOfView(ConstantsF.PiOver2, 1, 1, 100);
                context.CameraNode = new CameraNode(new Camera(perspectiveProjection));

                var size      = _skybox.Texture.Size;
                var hdrFormat = new RenderTargetFormat(size, size, false, SurfaceFormat.HdrBlendable, DepthFormat.None);
                var hdrTarget = context.GraphicsService.RenderTargetPool.Obtain2D(hdrFormat);
                var ldrFormat = new RenderTargetFormat(size, size, false, SurfaceFormat.Color, DepthFormat.None);
                var ldrTarget = context.GraphicsService.RenderTargetPool.Obtain2D(ldrFormat);

                var spriteBatch = GraphicsService.GetSpriteBatch();
                for (int side = 0; side < 6; side++)
                {
                    // Rotate camera to face the current cube map face.
                    var cubeMapFace = (CubeMapFace)side;
                    context.CameraNode.View = Matrix44F.CreateLookAt(
                        new Vector3F(),
                        GraphicsHelper.GetCubeMapForwardDirection(cubeMapFace),
                        GraphicsHelper.GetCubeMapUpDirection(cubeMapFace));

                    // Render sky into HDR render target.
                    graphicsDevice.SetRenderTarget(hdrTarget);
                    context.RenderTarget = hdrTarget;
                    context.Viewport     = graphicsDevice.Viewport;
                    graphicsDevice.Clear(Color.Black);
                    _skyRenderer.Render(_skyNodes, context);

                    graphicsDevice.BlendState = BlendState.Opaque;

                    // Convert HDR to RGBM.
                    context.SourceTexture = hdrTarget;
                    context.RenderTarget  = ldrTarget;
                    _colorEncoder.Process(context);
                    context.SourceTexture = null;

                    // Copy RGBM texture into cube map face.
                    graphicsDevice.SetRenderTarget((RenderTargetCube)_skybox.Texture, cubeMapFace);
                    spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, null, null, null);
                    spriteBatch.Draw(ldrTarget, new Vector2(0, 0), Color.White);
                    spriteBatch.End();
                }

                context.GraphicsService.RenderTargetPool.Recycle(ldrTarget);
                context.GraphicsService.RenderTargetPool.Recycle(hdrTarget);
            }

            graphicsDevice.BlendState        = BlendState.Opaque;
            graphicsDevice.DepthStencilState = DepthStencilState.Default;
            graphicsDevice.RasterizerState   = RasterizerState.CullCounterClockwise;

            context.CameraNode = _cameraObject.CameraNode;

            var tempFormat = new RenderTargetFormat(originalRenderTarget);

            tempFormat.SurfaceFormat = SurfaceFormat.HdrBlendable;
            var tempTarget = context.GraphicsService.RenderTargetPool.Obtain2D(tempFormat);

            graphicsDevice.SetRenderTarget(tempTarget);
            graphicsDevice.Viewport = originalViewport;
            context.RenderTarget    = tempTarget;
            context.Viewport        = originalViewport;

            _skyRenderer.Render(_skybox, context);

            context.SourceTexture = tempTarget;
            context.RenderTarget  = originalRenderTarget;
            _hdrFilter.Process(context);
            context.SourceTexture = null;

            context.GraphicsService.RenderTargetPool.Recycle(tempTarget);

            RenderDebugInfo(context);

            context.CameraNode = null;
        }
Exemple #17
0
        //--------------------------------------------------------------
        #region Methods
        //--------------------------------------------------------------

        protected override void OnLoad()
        {
            // Get services.
            _inputService    = _services.GetInstance <IInputService>();
            _graphicsService = _services.GetInstance <IGraphicsService>();
            _scene           = _services.GetInstance <IScene>();
            var content           = _services.GetInstance <ContentManager>();
            var gameObjectService = _services.GetInstance <IGameObjectService>();

            // Get camera game object.
            _cameraObject = (CameraObject)gameObjectService.Objects["Camera"];

            // Create SkyNodes.
            InitializeSky(content);

            // Create LightNodes
            InitializeLights();

            // Optionally, the sky is captured into a cube map and the cube map is added
            // to the scene instead of all the sky nodes.
            if (_cacheSky)
            {
                // We use a SceneCaptureNode to create the cube map.
                // The cube map uses RGBM encoding to store HDR values.
                var renderToTexture = new RenderToTexture
                {
                    Texture = new RenderTargetCube(
                        _graphicsService.GraphicsDevice, 1024, false,
                        SurfaceFormat.Color, DepthFormat.None),
                };
                var projection = new PerspectiveProjection();
                projection.SetFieldOfView(ConstantsF.PiOver2, 1, 1, 10);
                _sceneCaptureNode = new SceneCaptureNode(renderToTexture)
                {
                    // Note: The scene is captured at the origin (0, 0, 0).
                    CameraNode = new CameraNode(new Camera(projection)),
                };

                SkyboxNode = new SkyboxNode
                {
                    Encoding = ColorEncoding.Rgbm,
                    Texture  = (TextureCube)renderToTexture.Texture,
                };
                _scene.Children.Add(SkyboxNode);
            }

            // The Ephemeris class computes the positions of the sun and the moon as seen
            // from any place on the earth.
            _ephemeris = new Ephemeris
            {
                // Seattle, Washington
                //Latitude = 47,
                //Longitude = 122,
                //Altitude = 100

                // Vienna
                //Latitude = 48,
                //Longitude = -16,
                //Altitude = 0

                // Equator
                Latitude  = 0,
                Longitude = 90,
                Altitude  = 0
            };

#if XBOX
            _time = DateTime.Now;
#else
            _time = DateTimeOffset.Now;
#endif

            // Update the positions of sky objects and the lights.
            UpdateSky();

            // Create cube map.
            if (_cacheSky)
            {
                UpdateCubeMap(TimeSpan.Zero);
            }
        }
        private bool _cullingEnabled = true; // True to use frustum culling. False to disable frustum culling.


        public FrustumCullingSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            GraphicsScreen.ClearBackground = true;
            GraphicsScreen.BackgroundColor = Color.CornflowerBlue;

            // The top-down camera.
            var orthographicProjection = new OrthographicProjection();

            orthographicProjection.Set(
                LevelSize * 1.1f * GraphicsService.GraphicsDevice.Viewport.AspectRatio,
                LevelSize * 1.1f,
                1,
                10000f);
            var topDownCamera = new Camera(orthographicProjection);

            _topDownCameraNode = new CameraNode(topDownCamera)
            {
                View = Matrix44F.CreateLookAt(new Vector3F(0, 1000, 0), new Vector3F(0, 0, 0), -Vector3F.UnitZ),
            };

            // The perspective camera moving through the scene.
            var perspectiveProjection = new PerspectiveProjection();

            perspectiveProjection.SetFieldOfView(
                MathHelper.ToRadians(45),
                GraphicsService.GraphicsDevice.Viewport.AspectRatio,
                1,
                500);
            var sceneCamera = new Camera(perspectiveProjection);

            _sceneCameraNode = new CameraNode(sceneCamera);

            // Initialize collision detection.
            // We use one collision domain that manages all objects.
            _domain = new CollisionDomain(new CollisionDetection())
            {
                // We exchange the default broad phase with a DualPartition. The DualPartition
                // has special support for frustum culling.
                BroadPhase = new DualPartition <CollisionObject>(),
            };

            // Create a lot of random objects and add them to the collision domain.
            RandomHelper.Random = new Random(12345);
            for (int i = 0; i < NumberOfObjects; i++)
            {
                // A real scene consists of a lot of complex objects such as characters, vehicles,
                // buildings, lights, etc. When doing frustum culling we need to test each objects against
                // the viewing frustum. If it intersects with the viewing frustum, the object is visible
                // from the camera's point of view. However, in practice we do not test the exact object
                // against the viewing frustum. Each objects is approximated by a simpler shape. In our
                // example, we assume that each object is approximated with an oriented bounding box.
                // (We could also use an other shape, such as a bounding sphere.)

                // Create a random box.
                Shape randomShape = new BoxShape(RandomHelper.Random.NextVector3F(1, 10));

                // Create a random position.
                Vector3F randomPosition;
                randomPosition.X = RandomHelper.Random.NextFloat(-LevelSize / 2, LevelSize / 2);
                randomPosition.Y = RandomHelper.Random.NextFloat(0, 2);
                randomPosition.Z = RandomHelper.Random.NextFloat(-LevelSize / 2, LevelSize / 2);

                // Create a random orientation.
                QuaternionF randomOrientation = RandomHelper.Random.NextQuaternionF();

                // Create object and add it to collision domain.
                var geometricObject = new GeometricObject(randomShape, new Pose(randomPosition, randomOrientation));
                var collisionObject = new CollisionObject(geometricObject)
                {
                    CollisionGroup = 0,
                };
                _domain.CollisionObjects.Add(collisionObject);
            }

            // Per default, the collision domain computes collision between all objects.
            // In this sample we do not need this information and disable it with a collision
            // filter.
            // In a real application, we would use this collision information for rendering,
            // for example, to find out which lights overlap with which meshes, etc.
            var filter = new CollisionFilter();

            // Disable collision between objects in collision group 0.
            filter.Set(0, 0, false);
            _domain.CollisionDetection.CollisionFilter = filter;

            // Start with the top-down camera.
            GraphicsScreen.CameraNode = _topDownCameraNode;

            // We will collect a few statistics for debugging.
            Profiler.SetFormat("NoCull", 1000, "Time in ms to submit DebugRenderer draw jobs without frustum culling.");
            Profiler.SetFormat("WithCull", 1000, "Time in ms to submit DebugRenderer draw jobs with frustum culling.");
        }
Exemple #19
0
        public SceneCaptureCubeSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            SampleFramework.IsMouseVisible = false;

            // Create a graphics screen. This screen has to call the SceneCaptureRenderer
            // to handle the SceneCaptureNodes!
            _graphicsScreen = new DeferredGraphicsScreen(Services)
            {
                DrawReticle = true
            };
            GraphicsService.Screens.Insert(0, _graphicsScreen);
            GameObjectService.Objects.Add(new DeferredGraphicsOptionsObject(Services));

            Services.Register(typeof(DebugRenderer), null, _graphicsScreen.DebugRenderer);
            Services.Register(typeof(IScene), null, _graphicsScreen.Scene);

            // Add gravity and damping to the physics Simulation.
            Simulation.ForceEffects.Add(new Gravity());
            Simulation.ForceEffects.Add(new Damping());

            // Add a custom game object which controls the camera.
            var cameraGameObject = new CameraObject(Services);

            GameObjectService.Objects.Add(cameraGameObject);
            _graphicsScreen.ActiveCameraNode = cameraGameObject.CameraNode;

            // More standard objects.
            GameObjectService.Objects.Add(new GrabObject(Services));
            GameObjectService.Objects.Add(new ObjectCreatorObject(Services));
            //GameObjectService.Objects.Add(new StaticSkyObject(Services));
            GameObjectService.Objects.Add(new DynamicSkyObject(Services, true, false, true));
            GameObjectService.Objects.Add(new GroundObject(Services));
            GameObjectService.Objects.Add(new DudeObject(Services));
            GameObjectService.Objects.Add(new DynamicObject(Services, 1));
            GameObjectService.Objects.Add(new DynamicObject(Services, 2));
            GameObjectService.Objects.Add(new DynamicObject(Services, 5));
            GameObjectService.Objects.Add(new DynamicObject(Services, 6));
            GameObjectService.Objects.Add(new DynamicObject(Services, 7));
            GameObjectService.Objects.Add(new FogObject(Services)
            {
                AttachToCamera = true
            });
            GameObjectService.Objects.Add(new LavaBallsObject(Services));

            // Add a few palm trees.
            Random random = new Random(12345);

            for (int i = 0; i < 10; i++)
            {
                Vector3F  position    = new Vector3F(random.NextFloat(-3, -8), 0, random.NextFloat(0, -5));
                Matrix33F orientation = Matrix33F.CreateRotationY(random.NextFloat(0, ConstantsF.TwoPi));
                float     scale       = random.NextFloat(0.5f, 1.2f);
                GameObjectService.Objects.Add(new StaticObject(Services, "PalmTree/palm_tree", scale, new Pose(position, orientation)));
            }

            // Load the "Bubble" mesh and place it at a fixed position in the scene.
            var modelNode = ContentManager.Load <ModelNode>("Bubble/Bubble");
            var meshNode  = modelNode.GetDescendants().OfType <MeshNode>().First().Clone();

            meshNode.PoseWorld = new Pose(new Vector3F(0, 1, 0));
            _graphicsScreen.Scene.Children.Add(meshNode);

            // Surface of the mesh should reflect the scene in real-time. Reflections are
            // created using environment mapping: The scene is rendered into a cube map,
            // which is then applied to the mesh.
            // To render the scene into a cube map, we need to define a CameraNode and a
            // SceneCaptureNode: The CameraNode defines the point from where the scene is
            // captured. The SceneCaptureNode defines where and in which format the captured
            // image is needed.

            // Attach a camera to the center of the mesh.
            var projection = new PerspectiveProjection();

            projection.SetFieldOfView(ConstantsF.PiOver2, 1, 0.1f, 20);
            var captureCameraNode = new CameraNode(new Camera(projection));

            meshNode.Children = new SceneNodeCollection {
                captureCameraNode
            };

            // Attach a SceneCaptureNode with a cube map render target to the mesh.
            var renderToTexture = new RenderToTexture
            {
                Texture = new RenderTargetCube(
                    GraphicsService.GraphicsDevice,
                    256,
                    true,
                    SurfaceFormat.Color,
                    DepthFormat.None),
            };
            var sceneCaptureNode = new SceneCaptureNode(renderToTexture)
            {
                Shape      = meshNode.Shape,
                CameraNode = captureCameraNode,
            };

            meshNode.Children.Add(sceneCaptureNode);

            // The bubble model uses a special effect and is rendered in the "AlphaBlend"
            // render pass. Let's modify the effect parameters to use the created cube map
            // as the reflection map of the bubble.
            var effectBinding = meshNode.Mesh.Materials[0]["AlphaBlend"];

            effectBinding.Set("ReflectionStrength", 0.5f);
            effectBinding.Set("RefractionStrength", 0.0f);
            effectBinding.Set("FresnelBias", 1.0f);
            effectBinding.Set("BlendMode", 1.0f);
            effectBinding.Set("Alpha", 1.0f);
            effectBinding.Set("CustomEnvironmentMap", (TextureCube)renderToTexture.Texture);
        }
Exemple #20
0
        public void RandomizedMassSceneUpdate2()
        {
            const int NumberOfSceneNodes = 10;
              const int NumberOfSteps = 10000;
              const float WorldSize = 1000;

              var random = new Random(123457);
              var scene = new Scene();
              scene.EnableMultithreading = false;

              var nodes = new TestSceneNode[NumberOfSceneNodes];

              // Create random nodes.
              for (int i = 0; i < NumberOfSceneNodes; i++)
              {
            var node = new TestSceneNode();
            nodes[i] = node;

            var position = random.NextVector3F(0, WorldSize);
            var orientation = random.NextQuaternionF();
            node.PoseLocal = new Pose(position, orientation);

            float p = random.NextFloat(0, 100);
            if (p < 0.1f)
              node.Shape = Shape.Empty;
            else if (p < 0.2f)
              node.Shape = Shape.Infinite;
            else if (p < 0.6f)
              node.Shape = new BoxShape(random.NextVector3F(0, WorldSize));
            else
              node.Shape = new SphereShape(random.NextFloat(0, WorldSize));
              }

              var projection = new PerspectiveProjection();
              projection.SetFieldOfView(0.8f, 1, WorldSize / 10000, WorldSize);
              var camera = new Camera(projection);
              var cameraNode = new CameraNode(camera);

              for (int updateIndex = 0; updateIndex < NumberOfSteps; updateIndex++)
              {
            int actionsPerFrame = random.NextInteger(0, 100);
            for (int i = 0; i < actionsPerFrame; i++)
            {
              var node = nodes[random.Next(0, NumberOfSceneNodes)];

              const int numberOfActions = 100;
              int action = random.Next(0, numberOfActions);

              //scene.Validate();

              if (action == 0)
              {
            // Add
            if (node.Parent == null)
            {
              scene.Children.Add(node);
              //scene.Validate();
            }
              }
              else if (action == 1)
              {
            // Remove
            if (node.Parent != null)
            {
              node.Parent.Children.Remove(node);
              //scene.Validate();
            }
              }
              else if (action == 2)
              {
            // Move
            var pose = node.PoseWorld;
            pose.Position = random.NextVector3F(0, WorldSize);
            node.PoseWorld = pose;
            //scene.Validate();
              }
              else if (action == 3)
              {
            // Very small Move
            var pose = node.PoseWorld;
            const float maxDistance = WorldSize / 10000;
            pose.Position += random.NextVector3F(-maxDistance, maxDistance);
            node.PoseWorld = pose;
            //scene.Validate();
              }
              else if (action == 4)
              {
            // Small Move
            var pose = node.PoseWorld;
            const float maxDistance = WorldSize / 100;
            pose.Position += random.NextVector3F(-maxDistance, maxDistance);
            node.PoseWorld = pose;
            //scene.Validate();
              }
              else if (action == 5)
              {
            // Scale
            node.ScaleLocal = random.NextVector3F(0.0f, 10f);
            //scene.Validate();
              }
              else if (action == 6)
              {
            // Rotate
            node.PoseWorld = new Pose(node.PoseWorld.Position, random.NextQuaternionF());
            //scene.Validate();
              }
              else if (action == 7)
              {
            // Query
            var query = scene.Query<CameraFrustumQuery>(cameraNode, null);
            //Debug.WriteLine("Camera queried nodes: " + query.SceneNodes.Count);
            //scene.Validate();
              }
              else if (action == 8)
              {
            // Move camera.
            cameraNode.PoseWorld = new Pose(random.NextVector3F(0, WorldSize), random.NextQuaternionF());
            //scene.Validate();
              }
              else if (action == 9)
              {
            // Change shape.
            int type = random.NextInteger(0, 5);
            if (type == 0)
              node.Shape = new BoxShape(random.NextVector3F(0, WorldSize));
            else if (type == 1)
              node.Shape = new SphereShape(random.NextFloat(0, WorldSize));
            else if (type == 2)
              node.Shape = new BoxShape(new Vector3F(Single.MaxValue));
            else if (type == 3)
              node.Shape = new BoxShape(new Vector3F(0));
            else if (type == 4)
              node.Shape = Shape.Empty;
            //scene.Validate();
              }
              else if (action == 10)
              {
            // Add to random parent.
            if (node.Parent == null)
            {
              var randomParent = nodes[random.NextInteger(0, NumberOfSceneNodes - 1)];

              // Avoid loops:
              bool isLoop = node.GetSubtree().Contains(randomParent);
              if (!isLoop)
              {
                if (randomParent.Children == null)
                  randomParent.Children = new SceneNodeCollection();

                randomParent.Children.Add(node);
              }
            }
              }
              else if (action == 11)
              {
            if (node.Parent != null && node.Parent != scene)
              node.Parent.Children.Remove(node);
              }
              else if (action == 13)
              {
            if (random.NextInteger(0, 100) < 5)
            scene.Children.Clear();
              }
              else if (action == 14)
              {
            if (random.NextInteger(0, 100) < 5)
              scene.Children = new SceneNodeCollection();
              }
            }

            //Debug.WriteLine("Number of nodes in scene: " + scene.GetDescendants().Count());

            //scene.Validate();
            scene.Update(TimeSpan.FromSeconds(0.016666666f));
            //scene.Validate();
              }
        }
Exemple #21
0
        public void RandomizedMassSceneUpdate2()
        {
            const int   NumberOfSceneNodes = 10;
            const int   NumberOfSteps      = 10000;
            const float WorldSize          = 1000;

            var random = new Random(123457);
            var scene  = new Scene();

            scene.EnableMultithreading = false;

            var nodes = new TestSceneNode[NumberOfSceneNodes];

            // Create random nodes.
            for (int i = 0; i < NumberOfSceneNodes; i++)
            {
                var node = new TestSceneNode();
                nodes[i] = node;

                var position    = random.NextVector3F(0, WorldSize);
                var orientation = random.NextQuaternionF();
                node.PoseLocal = new Pose(position, orientation);

                float p = random.NextFloat(0, 100);
                if (p < 0.1f)
                {
                    node.Shape = Shape.Empty;
                }
                else if (p < 0.2f)
                {
                    node.Shape = Shape.Infinite;
                }
                else if (p < 0.6f)
                {
                    node.Shape = new BoxShape(random.NextVector3F(0, WorldSize));
                }
                else
                {
                    node.Shape = new SphereShape(random.NextFloat(0, WorldSize));
                }
            }

            var projection = new PerspectiveProjection();

            projection.SetFieldOfView(0.8f, 1, WorldSize / 10000, WorldSize);
            var camera     = new Camera(projection);
            var cameraNode = new CameraNode(camera);

            for (int updateIndex = 0; updateIndex < NumberOfSteps; updateIndex++)
            {
                int actionsPerFrame = random.NextInteger(0, 100);
                for (int i = 0; i < actionsPerFrame; i++)
                {
                    var node = nodes[random.Next(0, NumberOfSceneNodes)];

                    const int numberOfActions = 100;
                    int       action          = random.Next(0, numberOfActions);

                    //scene.Validate();

                    if (action == 0)
                    {
                        // Add
                        if (node.Parent == null)
                        {
                            scene.Children.Add(node);
                            //scene.Validate();
                        }
                    }
                    else if (action == 1)
                    {
                        // Remove
                        if (node.Parent != null)
                        {
                            node.Parent.Children.Remove(node);
                            //scene.Validate();
                        }
                    }
                    else if (action == 2)
                    {
                        // Move
                        var pose = node.PoseWorld;
                        pose.Position  = random.NextVector3F(0, WorldSize);
                        node.PoseWorld = pose;
                        //scene.Validate();
                    }
                    else if (action == 3)
                    {
                        // Very small Move
                        var         pose        = node.PoseWorld;
                        const float maxDistance = WorldSize / 10000;
                        pose.Position += random.NextVector3F(-maxDistance, maxDistance);
                        node.PoseWorld = pose;
                        //scene.Validate();
                    }
                    else if (action == 4)
                    {
                        // Small Move
                        var         pose        = node.PoseWorld;
                        const float maxDistance = WorldSize / 100;
                        pose.Position += random.NextVector3F(-maxDistance, maxDistance);
                        node.PoseWorld = pose;
                        //scene.Validate();
                    }
                    else if (action == 5)
                    {
                        // Scale
                        node.ScaleLocal = random.NextVector3F(0.0f, 10f);
                        //scene.Validate();
                    }
                    else if (action == 6)
                    {
                        // Rotate
                        node.PoseWorld = new Pose(node.PoseWorld.Position, random.NextQuaternionF());
                        //scene.Validate();
                    }
                    else if (action == 7)
                    {
                        // Query
                        var query = scene.Query <CameraFrustumQuery>(cameraNode, null);
                        //Debug.WriteLine("Camera queried nodes: " + query.SceneNodes.Count);
                        //scene.Validate();
                    }
                    else if (action == 8)
                    {
                        // Move camera.
                        cameraNode.PoseWorld = new Pose(random.NextVector3F(0, WorldSize), random.NextQuaternionF());
                        //scene.Validate();
                    }
                    else if (action == 9)
                    {
                        // Change shape.
                        int type = random.NextInteger(0, 5);
                        if (type == 0)
                        {
                            node.Shape = new BoxShape(random.NextVector3F(0, WorldSize));
                        }
                        else if (type == 1)
                        {
                            node.Shape = new SphereShape(random.NextFloat(0, WorldSize));
                        }
                        else if (type == 2)
                        {
                            node.Shape = new BoxShape(new Vector3F(Single.MaxValue));
                        }
                        else if (type == 3)
                        {
                            node.Shape = new BoxShape(new Vector3F(0));
                        }
                        else if (type == 4)
                        {
                            node.Shape = Shape.Empty;
                        }
                        //scene.Validate();
                    }
                    else if (action == 10)
                    {
                        // Add to random parent.
                        if (node.Parent == null)
                        {
                            var randomParent = nodes[random.NextInteger(0, NumberOfSceneNodes - 1)];

                            // Avoid loops:
                            bool isLoop = node.GetSubtree().Contains(randomParent);
                            if (!isLoop)
                            {
                                if (randomParent.Children == null)
                                {
                                    randomParent.Children = new SceneNodeCollection();
                                }

                                randomParent.Children.Add(node);
                            }
                        }
                    }
                    else if (action == 11)
                    {
                        if (node.Parent != null && node.Parent != scene)
                        {
                            node.Parent.Children.Remove(node);
                        }
                    }
                    else if (action == 13)
                    {
                        if (random.NextInteger(0, 100) < 5)
                        {
                            scene.Children.Clear();
                        }
                    }
                    else if (action == 14)
                    {
                        if (random.NextInteger(0, 100) < 5)
                        {
                            scene.Children = new SceneNodeCollection();
                        }
                    }
                }

                //Debug.WriteLine("Number of nodes in scene: " + scene.GetDescendants().Count());

                //scene.Validate();
                scene.Update(TimeSpan.FromSeconds(0.016666666f));
                //scene.Validate();
            }
        }
        public void SetProjectionFieldOfViewTest()
        {
            PerspectiveProjection projection = new PerspectiveProjection();
              projection.SetFieldOfView(MathHelper.ToRadians(60), 16.0f / 9.0f, 1, 10);

              PerspectiveProjection projection2 = new PerspectiveProjection();
              projection2.SetFieldOfView(MathHelper.ToRadians(60), 16.0f / 9.0f);
              projection2.Near = 1;
              projection2.Far = 10;

              Projection projection3 = new PerspectiveProjection
              {
            Left = -2.0528009f / 2.0f,
            Right = 2.0528009f / 2.0f,
            Bottom = -1.1547005f / 2.0f,
            Top = 1.1547005f / 2.0f,
            Near = 1,
            Far = 10,
              };

              Matrix44F expected = Matrix44F.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60), 16.0f / 9.0f, 1, 10);
              Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, projection));
              Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, projection2));
              Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, projection3.ToMatrix44F()));
        }
Exemple #23
0
        // OnLoad() is called when the GameObject is added to the IGameObjectService.
        protected override void OnLoad()
        {
            var contentManager = _services.GetInstance <ContentManager>();

            if (_type == 1)
            {
                // A simple cube.
                _rigidBody = new RigidBody(new BoxShape(1, 1, 1));
                _modelNode = contentManager.Load <ModelNode>("RustyCube/RustyCube").Clone();
            }
            else if (_type == 2)
            {
                // Another simple cube.
                _rigidBody = new RigidBody(new BoxShape(1, 1, 1));
                _modelNode = contentManager.Load <ModelNode>("MetalGrateBox/MetalGrateBox").Clone();
            }
            else if (_type == 3)
            {
                // A TV-like box.
                _rigidBody = new RigidBody(new BoxShape(1, 0.6f, 0.8f))
                {
                    UserData = "TV"
                };
                _modelNode = contentManager.Load <ModelNode>("TVBox/TVBox");

                if (_modelNode.Children.OfType <LightNode>().Count() == 0)
                {
                    // This is the first time the "TVBox" is loaded.

                    // Add a projector light to the model that projects the TV screen. The
                    // TV screen is the emissive part of the TV mesh.
                    var meshNode = _modelNode.Children.OfType <MeshNode>().First();
                    var material = meshNode.Mesh.Materials.First(m => m.Name == "TestCard");

                    // Get texture from material.
                    // Note: In XNA the effect parameter type is Texture. In MonoGame it is Texture2D.
                    Texture2D texture;
                    EffectParameterBinding parameterBinding = material["Material"].ParameterBindings["EmissiveTexture"];
                    if (parameterBinding is EffectParameterBinding <Texture> )
                    {
                        texture = (Texture2D)((EffectParameterBinding <Texture>)parameterBinding).Value;
                    }
                    else
                    {
                        texture = ((EffectParameterBinding <Texture2D>)parameterBinding).Value;
                    }

                    var projection = new PerspectiveProjection();
                    projection.Near = 0.55f;
                    projection.Far  = 3.0f;
                    projection.SetFieldOfView(MathHelper.ToRadians(60), 0.76f / 0.56f);

                    var projectorLight = new ProjectorLight(texture, projection);
                    projectorLight.Attenuation = 4;
                    var projectorLightNode = new LightNode(projectorLight);
                    projectorLightNode.LookAt(new Vector3F(0, 0.2f, 0), Vector3F.Zero, Vector3F.UnitZ);

                    // Attach the projector light to the model.
                    _modelNode.Children.Add(projectorLightNode);
                }

                _modelNode = _modelNode.Clone();
            }
            else if (_type == 4)
            {
                // A "magic" sphere with a colored point light.
                _rigidBody = new RigidBody(new SphereShape(0.25f));
                _modelNode = contentManager.Load <ModelNode>("MagicSphere/MagicSphere");

                if (_modelNode.Children.OfType <LightNode>().Count() == 0)
                {
                    // This is the first time the "MagicSphere" is loaded.

                    // Change the size of the sphere.
                    var meshNode = _modelNode.Children.OfType <MeshNode>().First();
                    meshNode.ScaleLocal = new Vector3F(0.5f);

                    // Disable shadows. (The sphere acts as a light source.)
                    meshNode.CastsShadows = false;

                    // Add a point light.
                    var pointLight = new PointLight
                    {
                        Color             = new Vector3F(1, 1, 1),
                        DiffuseIntensity  = 4,
                        SpecularIntensity = 4,
                        Range             = 3,
                        Attenuation       = 1,
                        Texture           = contentManager.Load <TextureCube>("MagicSphere/ColorCube"),
                    };
                    var pointLightNode = new LightNode(pointLight)
                    {
                        // The point light uses shadow mapping to cast an omnidirectional shadow.
                        Shadow = new CubeMapShadow
                        {
                            PreferredSize   = 64,
                            DepthBiasScale  = 0.9f,
                            DepthBiasOffset = -0.01f,
                        }
                    };

                    _modelNode.Children.Add(pointLightNode);
                }

                _modelNode = _modelNode.Clone();
            }
            else if (_type == 5)
            {
                // A sphere of glass (or "bubble").
                _rigidBody = new RigidBody(new SphereShape(0.3f));
                _modelNode = contentManager.Load <ModelNode>("Bubble/Bubble").Clone();
                _modelNode.GetDescendants().OfType <MeshNode>().First().ScaleLocal = new Vector3F(0.3f);
            }
            else if (_type == 6)
            {
                // A rusty barrel with multiple levels of detail (LODs).
                _rigidBody = new RigidBody(new CylinderShape(0.35f, 1));
                _modelNode = contentManager.Load <ModelNode>("Barrel/Barrel").Clone();
            }
            else
            {
                // A cube consisting of a frame and transparent sides.
                _rigidBody = new RigidBody(new BoxShape(1, 1, 1));
                _modelNode = contentManager.Load <ModelNode>("GlassBox/GlassBox").Clone();
            }

            SampleHelper.EnablePerPixelLighting(_modelNode);

            // Set a random pose.
            var randomPosition = new Vector3F(
                RandomHelper.Random.NextFloat(-10, 10),
                RandomHelper.Random.NextFloat(2, 5),
                RandomHelper.Random.NextFloat(-20, 0));

            _rigidBody.Pose      = new Pose(randomPosition, RandomHelper.Random.NextQuaternionF());
            _modelNode.PoseWorld = _rigidBody.Pose;

            // Add rigid body to physics simulation and model to scene.
            var simulation = _services.GetInstance <Simulation>();

            simulation.RigidBodies.Add(_rigidBody);

            var scene = _services.GetInstance <IScene>();

            scene.Children.Add(_modelNode);
        }