Esempio n. 1
0
        protected override void LoadContent()
        {
            Window.Title = "Render Target Sample";

            //Create the box we'll render off screen
            _offscreenCube          = new Box("Offscreen box", 10, 10, 10);
            _offscreenCube.Material = ContentManager.Load <Material>("LitBasicTexture.tem");
            _offscreenCube.Material.SetParameter("DiffuseMap", ContentManager.Load <Texture2D>("Textures//rock_diff.dds"));
            _offscreenCube.AddController(new RotateController(Vector3.Normalize(Vector3.Add(Vector3.Up, Vector3.Right)), 45.0f));

            PointLight pl = new PointLight();

            pl.Diffuse  = Color.CornflowerBlue;
            pl.Position = new Vector3(0, 25, 25);
            pl.Ambient  = Color.LightBlue;
            _offscreenCube.AddLight(pl);
            _offscreenCube.SceneHints.LightCombineHint = LightCombineHint.Local;
            _offscreenCube.RenderBucketType            = RenderBucketType.Skip;

            //Create a render target that also has a depth stencil.
            _renderTarget = new RenderTarget2D(512, 512, true, SurfaceFormat.Color, DepthFormat.Depth24Stencil8);

            //Camera we'll use for the off screen rendering
            _targetCamera          = new Camera(new Viewport(0, 0, 512, 512));
            _targetCamera.Position = new Vector3(0, 0, 50);
            _targetCamera.SetProjection(45.0f, 0.1f, 5000.0f);
            _targetCamera.Update();

            //Create a box that we'll attach to the scene graph and render as normal.
            _box          = new Box("OnScreen box", 50, 50, 50);
            _box.Material = ContentManager.Load <Material>("BasicTexture.tem");
            //Since render target is a texture, we can set it to the material's effect.
            _box.Material.SetParameter("DiffuseMap", _renderTarget);
            RootNode.AddChild(_box);
        }
Esempio n. 2
0
        /// <summary>
        /// Please see the SceneGraph.rtf description for scene graph organization.
        /// </summary>
        protected override void LoadContent()
        {
            ClearColor   = Color.CornflowerBlue;
            Window.Title = "Scene Graph Sample";

            //Create the teapot sub-tree, we're also creating a small box showing the position of
            //the node in world space
            Node teapotParent       = new Node("TeapotParent");
            Box  teapotParentVisual = new Box("TeapotParentVisual", 1f, 1f, 1f);

            teapotParentVisual.Material = ContentManager.Load <Material>("LitBasicColor.tem").Clone();
            teapotParent.AddChild(teapotParentVisual);

            //Set a rotate controller onto the teapot parent, so it'll rotate every frame
            teapotParent.AddController(new RotateController(Vector3.UnitY, 25));

            //Create the teapot mesh
            Teapot teapot = new Teapot("Teapot");

            //Load up a default lit color material
            teapot.Material = ContentManager.Load <Material>("LitBasicColor.tem").Clone();
            //Create another rotate controller that will rotate the teapot about its X-Axis
            //45 degrees per second. This shows how we can -easily- concatenate transforms
            //using the hierarchy of the scene graph.
            teapot.AddController(new RotateController(Vector3.UnitX, 45f));

            //Set a red color to the material.
            teapot.Material.SetParameter("MatDiffuse", Color.Crimson.ToVector3());
            teapot.Scale = new Vector3(5f, 5f, 5f);

            //Position and add to parent
            teapot.Translation = new Vector3(-50, 0, 0);
            teapotParent.AddChild(teapot);

            //Add the teapot sub tree to the root.
            RootNode.AddChild(teapotParent);

            //Add a white light to the root node
            RootNode.RemoveAllLights();
            PointLight pl = new PointLight();

            pl.Attenuate = false;
            pl.Position  = new Vector3(-100, 50, 100);
            RootNode.AddLight(pl);

            //Create the box
            Box box = new Box("Box", 10, 10, 10);

            box.Translation = new Vector3(50, -50, 0);
            box.Material    = ContentManager.Load <Material>("LitBasicColor.tem").Clone();
            box.Material.SetParameter("MatDiffuse", Color.Gray.ToVector3());

            //Set the box to only use lights that are attached to it
            box.SceneHints.LightCombineHint = LightCombineHint.Local;

            //Create a green point light
            PointLight pl2 = new PointLight();

            pl2.Attenuate = false;
            pl2.Diffuse   = Color.Green;
            pl2.Position  = new Vector3(100, 50, 100);
            box.AddLight(pl2);

            //Add the box to the root
            RootNode.AddChild(box);

            //Set a bounding box to be used as the volume for each node/mesh in the scene graph.
            RootNode.SetModelBound(new BoundingBox());

            //Create some input handling to show how you can apply
            //a scaling value to a parent node (this case the scene Root),
            //and see it be applied to all children.
            InputLayer.RegisterTrigger(new InputTrigger(new KeyPressedCondition(Keys.J, false), new InputAction(delegate(GameTime time) {
                if (RootNode.Controllers.Count > 0)
                {
                    RootNode.RemoveAllControllers();
                    RootNode.SetScale(1.0f);
                    scaleOn = false;
                }
                else
                {
                    RootNode.AddController(new ScaleController(1.0f, .1f, 2.0f));
                    scaleOn = true;
                }
            })));

            batch = new SpriteBatch();
            font  = ContentManager.Load <SpriteFont>("Fonts//comicsans.fnt");
        }