void CreateScene()
        {
            var cache = ResourceCache;

            {
                rttScene = new Scene();
                // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
                rttScene.CreateComponent<Octree>();

                // Create a Zone for ambient light & fog control
                Node zoneNode = rttScene.CreateChild("Zone");
                Zone zone = zoneNode.CreateComponent<Zone>();
                // Set same volume as the Octree, set a close bluish fog and some ambient light
                zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
                zone.AmbientColor = new Color(0.05f, 0.1f, 0.15f);
                zone.FogColor = new Color(0.1f, 0.2f, 0.3f);
                zone.FogStart = 10.0f;
                zone.FogEnd = 100.0f;

                // Create randomly positioned and oriented box StaticModels in the scene
                const uint numObjects = 2000;
                for (uint i = 0; i < numObjects; ++i)
                {
                    Node boxNode = rttScene.CreateChild("Box");
                    boxNode.Position = new Vector3(NextRandom(200.0f) - 100.0f, NextRandom(200.0f) - 100.0f,
                        NextRandom(200.0f) - 100.0f);
                    // Orient using random pitch, yaw and roll Euler angles
                    boxNode.Rotation = new Quaternion(NextRandom(360.0f), NextRandom(360.0f), NextRandom(360.0f));
                    StaticModel boxObject = boxNode.CreateComponent<StaticModel>();
                    boxObject.Model = cache.GetModel("Models/Box.mdl");
                    boxObject.SetMaterial(cache.GetMaterial("Materials/Stone.xml"));

                    // Add our custom Rotator component which will rotate the scene node each frame, when the scene sends its update event.
                    // Simply set same rotation speed for all objects
                    Rotator rotator = new Rotator();
                    boxNode.AddComponent(rotator);
                    rotator.SetRotationSpeed(new Vector3(10.0f, 20.0f, 30.0f));
                }

                // Create a camera for the render-to-texture scene. Simply leave it at the world origin and let it observe the scene
                rttCameraNode = rttScene.CreateChild("Camera");
                Camera camera = rttCameraNode.CreateComponent<Camera>();
                camera.FarClip = 100.0f;

                // Create a point light to the camera scene node
                Light light = rttCameraNode.CreateComponent<Light>();
                light.LightType = LightType.Point;
                light.Range = 30.0f;
            }

            {
                // Create the scene in which we move around

                scene = new Scene();

                // Create octree, use also default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
                scene.CreateComponent<Octree>();

                // Create a Zone component for ambient lighting & fog control
                Node zoneNode = scene.CreateChild("Zone");
                Zone zone = zoneNode.CreateComponent<Zone>();
                zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
                zone.AmbientColor = new Color(0.1f, 0.1f, 0.1f);
                zone.FogStart = 100.0f;
                zone.FogEnd = 300.0f;

                // Create a directional light without shadows
                Node lightNode = scene.CreateChild("DirectionalLight");
                lightNode.SetDirection(new Vector3(0.5f, -1.0f, 0.5f));
                Light light = lightNode.CreateComponent<Light>();
                light.LightType = LightType.Directional;
                light.Color = new Color(0.2f, 0.2f, 0.2f);
                light.SpecularIntensity = 1.0f;

                // Create a "floor" consisting of several tiles
                for (int y = -5; y <= 5; ++y)
                {
                    for (int x = -5; x <= 5; ++x)
                    {
                        Node floorNode = scene.CreateChild("FloorTile");
                        floorNode.Position = new Vector3(x*20.5f, -0.5f, y*20.5f);
                        floorNode.Scale = new Vector3(20.0f, 1.0f, 20.0f);
                        StaticModel floorObject = floorNode.CreateComponent<StaticModel>();
                        floorObject.Model = cache.GetModel("Models/Box.mdl");
                        floorObject.SetMaterial(cache.GetMaterial("Materials/Stone.xml"));
                    }
                }

                // Create a "screen" like object for viewing the second scene. Construct it from two StaticModels, a box for the frame
                // and a plane for the actual view
                {
                    Node boxNode = scene.CreateChild("ScreenBox");
                    boxNode.Position = new Vector3(0.0f, 10.0f, 0.0f);
                    boxNode.Scale = new Vector3(21.0f, 16.0f, 0.5f);
                    StaticModel boxObject = boxNode.CreateComponent<StaticModel>();
                    boxObject.Model = cache.GetModel("Models/Box.mdl");
                    boxObject.SetMaterial(cache.GetMaterial("Materials/Stone.xml"));

                    Node screenNode = scene.CreateChild("Screen");
                    screenNode.Position = new Vector3(0.0f, 10.0f, -0.27f);
                    screenNode.Rotation = new Quaternion(-90.0f, 0.0f, 0.0f);
                    screenNode.Scale = new Vector3(20.0f, 0.0f, 15.0f);
                    StaticModel screenObject = screenNode.CreateComponent<StaticModel>();
                    screenObject.Model = cache.GetModel("Models/Plane.mdl");

                    // Create a renderable texture (1024x768, RGB format), enable bilinear filtering on it
                    Texture2D renderTexture = new Texture2D();
                    renderTexture.SetSize(1024, 768, Graphics.RGBFormat, TextureUsage.Rendertarget);
                    renderTexture.FilterMode = TextureFilterMode.Bilinear;

                    // Create a new material from scratch, use the diffuse unlit technique, assign the render texture
                    // as its diffuse texture, then assign the material to the screen plane object
                    Material renderMaterial = new Material();
                    renderMaterial.SetTechnique(0, cache.GetTechnique("Techniques/DiffUnlit.xml"), 0, 0);
                    renderMaterial.SetTexture(TextureUnit.Diffuse, renderTexture);
                    screenObject.SetMaterial(renderMaterial);

                    // Get the texture's RenderSurface object (exists when the texture has been created in rendertarget mode)
                    // and define the viewport for rendering the second scene, similarly as how backbuffer viewports are defined
                    // to the Renderer subsystem. By default the texture viewport will be updated when the texture is visible
                    // in the main view
                    RenderSurface surface = renderTexture.RenderSurface;
                    Viewport rttViewport = new Viewport(Context, rttScene, rttCameraNode.GetComponent<Camera>(), null);
                    surface.SetViewport(0, rttViewport);
                }

                // Create the camera. Limit far clip distance to match the fog
                CameraNode = scene.CreateChild("Camera");
                var camera = CameraNode.CreateComponent<Camera>();
                camera.FarClip = 300.0f;
                // Set an initial position for the camera scene node above the plane
                CameraNode.Position = new Vector3(0.0f, 7.0f, -30.0f);
            }
        }
Example #2
0
        void CreateScene()
        {
            var cache = ResourceCache;

            {
                rttScene = new Scene();
                // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
                rttScene.CreateComponent <Octree>();

                // Create a Zone for ambient light & fog control
                Node zoneNode = rttScene.CreateChild("Zone");
                Zone zone     = zoneNode.CreateComponent <Zone>();
                // Set same volume as the Octree, set a close bluish fog and some ambient light
                zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
                zone.AmbientColor = new Color(0.05f, 0.1f, 0.15f);
                zone.FogColor     = new Color(0.1f, 0.2f, 0.3f);
                zone.FogStart     = 10.0f;
                zone.FogEnd       = 100.0f;

                // Create randomly positioned and oriented box StaticModels in the scene
                const uint numObjects = 2000;
                for (uint i = 0; i < numObjects; ++i)
                {
                    Node boxNode = rttScene.CreateChild("Box");
                    boxNode.Position = new Vector3(NextRandom(200.0f) - 100.0f, NextRandom(200.0f) - 100.0f,
                                                   NextRandom(200.0f) - 100.0f);
                    // Orient using random pitch, yaw and roll Euler angles
                    boxNode.Rotation = new Quaternion(NextRandom(360.0f), NextRandom(360.0f), NextRandom(360.0f));
                    StaticModel boxObject = boxNode.CreateComponent <StaticModel>();
                    boxObject.Model = cache.GetModel("Models/Box.mdl");
                    boxObject.SetMaterial(cache.GetMaterial("Materials/Stone.xml"));

                    // Add our custom Rotator component which will rotate the scene node each frame, when the scene sends its update event.
                    // Simply set same rotation speed for all objects
                    Rotator rotator = new Rotator();
                    boxNode.AddComponent(rotator);
                    rotator.SetRotationSpeed(new Vector3(10.0f, 20.0f, 30.0f));
                }

                // Create a camera for the render-to-texture scene. Simply leave it at the world origin and let it observe the scene
                rttCameraNode = rttScene.CreateChild("Camera");
                Camera camera = rttCameraNode.CreateComponent <Camera>();
                camera.FarClip = 100.0f;

                // Create a point light to the camera scene node
                Light light = rttCameraNode.CreateComponent <Light>();
                light.LightType = LightType.Point;
                light.Range     = 30.0f;
            }

            {
                // Create the scene in which we move around

                scene = new Scene();

                // Create octree, use also default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
                scene.CreateComponent <Octree>();

                // Create a Zone component for ambient lighting & fog control
                Node zoneNode = scene.CreateChild("Zone");
                Zone zone     = zoneNode.CreateComponent <Zone>();
                zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
                zone.AmbientColor = new Color(0.1f, 0.1f, 0.1f);
                zone.FogStart     = 100.0f;
                zone.FogEnd       = 300.0f;

                // Create a directional light without shadows
                Node lightNode = scene.CreateChild("DirectionalLight");
                lightNode.SetDirection(new Vector3(0.5f, -1.0f, 0.5f));
                Light light = lightNode.CreateComponent <Light>();
                light.LightType         = LightType.Directional;
                light.Color             = new Color(0.2f, 0.2f, 0.2f);
                light.SpecularIntensity = 1.0f;

                // Create a "floor" consisting of several tiles
                for (int y = -5; y <= 5; ++y)
                {
                    for (int x = -5; x <= 5; ++x)
                    {
                        Node floorNode = scene.CreateChild("FloorTile");
                        floorNode.Position = new Vector3(x * 20.5f, -0.5f, y * 20.5f);
                        floorNode.Scale    = new Vector3(20.0f, 1.0f, 20.0f);
                        StaticModel floorObject = floorNode.CreateComponent <StaticModel>();
                        floorObject.Model = cache.GetModel("Models/Box.mdl");
                        floorObject.SetMaterial(cache.GetMaterial("Materials/Stone.xml"));
                    }
                }

                // Create a "screen" like object for viewing the second scene. Construct it from two StaticModels, a box for the frame
                // and a plane for the actual view
                {
                    Node boxNode = scene.CreateChild("ScreenBox");
                    boxNode.Position = new Vector3(0.0f, 10.0f, 0.0f);
                    boxNode.Scale    = new Vector3(21.0f, 16.0f, 0.5f);
                    StaticModel boxObject = boxNode.CreateComponent <StaticModel>();
                    boxObject.Model = cache.GetModel("Models/Box.mdl");
                    boxObject.SetMaterial(cache.GetMaterial("Materials/Stone.xml"));

                    Node screenNode = scene.CreateChild("Screen");
                    screenNode.Position = new Vector3(0.0f, 10.0f, -0.27f);
                    screenNode.Rotation = new Quaternion(-90.0f, 0.0f, 0.0f);
                    screenNode.Scale    = new Vector3(20.0f, 0.0f, 15.0f);
                    StaticModel screenObject = screenNode.CreateComponent <StaticModel>();
                    screenObject.Model = cache.GetModel("Models/Plane.mdl");

                    // Create a renderable texture (1024x768, RGB format), enable bilinear filtering on it
                    Texture2D renderTexture = new Texture2D();
                    renderTexture.SetSize(1024, 768, Graphics.RGBFormat, TextureUsage.Rendertarget);
                    renderTexture.FilterMode = TextureFilterMode.Bilinear;

                    // Create a new material from scratch, use the diffuse unlit technique, assign the render texture
                    // as its diffuse texture, then assign the material to the screen plane object
                    Material renderMaterial = new Material();
                    renderMaterial.SetTechnique(0, cache.GetTechnique("Techniques/DiffUnlit.xml"), 0, 0);
                    renderMaterial.SetTexture(TextureUnit.Diffuse, renderTexture);
                    screenObject.SetMaterial(renderMaterial);

                    // Get the texture's RenderSurface object (exists when the texture has been created in rendertarget mode)
                    // and define the viewport for rendering the second scene, similarly as how backbuffer viewports are defined
                    // to the Renderer subsystem. By default the texture viewport will be updated when the texture is visible
                    // in the main view
                    RenderSurface surface     = renderTexture.RenderSurface;
                    Viewport      rttViewport = new Viewport(Context, rttScene, rttCameraNode.GetComponent <Camera>(), null);
                    surface.SetViewport(0, rttViewport);
                }


                // Create the camera. Limit far clip distance to match the fog
                CameraNode = scene.CreateChild("Camera");
                var camera = CameraNode.CreateComponent <Camera>();
                camera.FarClip = 300.0f;
                // Set an initial position for the camera scene node above the plane
                CameraNode.Position = new Vector3(0.0f, 7.0f, -30.0f);
            }
        }
        void CreateScene()
        {
            var cache = ResourceCache;
            scene = new Scene();

            // Create the Octree component to the scene so that drawable objects can be rendered. Use default volume
            // (-1000, -1000, -1000) to (1000, 1000, 1000)
            scene.CreateComponent<Octree>();

            // Create a Zone component into a child scene node. The Zone controls ambient lighting and fog settings. Like the Octree,
            // it also defines its volume with a bounding box, but can be rotated (so it does not need to be aligned to the world X, Y
            // and Z axes.) Drawable objects "pick up" the zone they belong to and use it when rendering; several zones can exist
            var zoneNode = scene.CreateChild("Zone");
            var zone = zoneNode.CreateComponent<Zone>();

            // Set same volume as the Octree, set a close bluish fog and some ambient light
            zone.SetBoundingBox(new BoundingBox(-1000.0f, 1000.0f));
            zone.AmbientColor = new Color(0.05f, 0.1f, 0.15f);
            zone.FogColor = new Color(0.1f, 0.2f, 0.3f);
            zone.FogStart = 10;
            zone.FogEnd = 100;

            var boxesNode = scene.CreateChild("Boxes");

            const int numObjects = 2000;
            for (var i = 0; i < numObjects; ++i)
            {
                Node boxNode = new Node();
                boxesNode.AddChild(boxNode, 0);
                boxNode.Position = new Vector3(NextRandom(200f) - 100f, NextRandom(200f) - 100f, NextRandom(200f) - 100f);
                // Orient using random pitch, yaw and roll Euler angles
                boxNode.Rotation = new Quaternion(NextRandom(360.0f), NextRandom(360.0f), NextRandom(360.0f));

                using (var boxObject = boxNode.CreateComponent<StaticModel>())
                {
                    boxObject.Model = cache.GetModel("Models/Box.mdl");
                    boxObject.SetMaterial(cache.GetMaterial("Materials/Stone.xml"));
                    //we don't need this component in C# anymore so let's just delete a MCW for it (howerver, we can access it anytime if we need via GetComponent<>)
                    //it's just an optimization to reduce cached objects count
                }

                // Add our custom Rotator component which will rotate the scene node each frame, when the scene sends its update event.
                // The Rotator component derives from the base class LogicComponent, which has convenience functionality to subscribe
                // to the various update events, and forward them to virtual functions that can be implemented by subclasses. This way
                // writing logic/update components in C++ becomes similar to scripting.
                // Now we simply set same rotation speed for all objects

                var rotationSpeed = new Vector3(10.0f, 20.0f, 30.0f);

                // First style: use a Rotator instance, which is a component subclass, and
                // add it to the boxNode.
                var rotator = new Rotator() { RotationSpeed = rotationSpeed };
                boxNode.AddComponent(rotator);
            }
            // Create the camera. Let the starting position be at the world origin. As the fog limits maximum visible distance, we can
            // bring the far clip plane closer for more effective culling of distant objects
            CameraNode = scene.CreateChild("Camera");
            var camera = CameraNode.CreateComponent<Camera>();
            camera.FarClip = 100.0f;

            // Create a point light to the camera scene node
            var light = CameraNode.CreateComponent<Light>();
            light.LightType = LightType.Point;
            light.Range = 30.0f;
        }