Rendering to Multiple Windows

From Axiom

Jump to: navigation, search

This examples shows you how to render a scene to multiple windows using Axiom.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Axiom.Core;
using Axiom.Graphics;
using Axiom.Math;
using Axiom.Input;
using Axiom.Overlays;
using System.IO;
using Axiom.Configuration;

namespace Axiom.Samples.MulitpleRenderWindows
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.viewportOne = new System.Windows.Forms.PictureBox();
            this.viewportTwo = new System.Windows.Forms.PictureBox();
            ( (System.ComponentModel.ISupportInitialize)( this.viewportOne ) ).BeginInit();
            ( (System.ComponentModel.ISupportInitialize)( this.viewportTwo ) ).BeginInit();
            this.SuspendLayout();
            // 
            // viewportOne
            // 
            this.viewportOne.Location = new System.Drawing.Point( 3, 12 );
            this.viewportOne.Name = "viewportOne";
            this.viewportOne.Size = new System.Drawing.Size( 594, 625 );
            this.viewportOne.TabIndex = 0;
            this.viewportOne.TabStop = false;
            // 
            // viewportTwo
            // 
            this.viewportTwo.Location = new System.Drawing.Point( 603, 12 );
            this.viewportTwo.Name = "viewportTwo";
            this.viewportTwo.Size = new System.Drawing.Size( 422, 378 );
            this.viewportTwo.TabIndex = 1;
            this.viewportTwo.TabStop = false;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 13F );
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size( 1037, 700 );
            this.Controls.Add( this.viewportTwo );
            this.Controls.Add( this.viewportOne );
            this.Name = "Form1";
            this.Text = "Form1";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler( this.Form1_FormClosing );
            this.Load += new System.EventHandler( this.Form1_Load );
            ( (System.ComponentModel.ISupportInitialize)( this.viewportOne ) ).EndInit();
            ( (System.ComponentModel.ISupportInitialize)( this.viewportTwo ) ).EndInit();
            this.ResumeLayout( false );

        }

        #endregion

        private System.Windows.Forms.PictureBox viewportOne;
        private System.Windows.Forms.PictureBox viewportTwo;

        protected Root engine;
        protected Camera camera;
        protected Viewport viewport;
        protected Camera cameraTwo;
        protected Viewport viewport2;
        protected SceneManager scene;
        protected RenderWindow windowOne;
        protected RenderWindow windowTwo;
        protected InputReader input;
        protected Vector3 cameraVector = Vector3.Zero;
        protected float cameraScale;
        protected bool showDebugOverlay = true;
        protected float statDelay = 0.0f;
        protected float debugTextDelay = 0.0f;
        protected float toggleDelay = 0.0f;
        protected Vector3 camVelocity = Vector3.Zero;
        protected Vector3 camAccel = Vector3.Zero;
        protected float camSpeed = 2.5f;
        protected int aniso = 1;
        protected TextureFiltering filtering = TextureFiltering.Bilinear;
        private SceneNode headNode = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load( object sender, EventArgs e )
        {
            this.Show();
            
            engine = new Root( "EngineConfig.xml", "AxiomEngine.log" );
            _setupResources();
            engine.RenderSystem = engine.RenderSystems[ 0 ];

            engine.Initialize( false );

            Control target = this.viewportOne;
            windowOne = engine.CreateRenderWindow( "SampleOne", target.Width, target.Height, 32, false, 0, 0, true, true, target );

            target = this.viewportTwo;
            windowTwo = engine.CreateRenderWindow( "SampleTwo", target.Width, target.Height, 32, false, 0, 0, true, true, target );

            ShowDebugOverlay( showDebugOverlay );

            // Get the SceneManager, a generic one by default
            scene = engine.SceneManagers.GetSceneManager( SceneType.Generic );
            scene.ClearScene();

            // create a camera and initialize its position
            camera = scene.CreateCamera( "MainCamera" );
            camera.Position = new Vector3( 0, 0, 500 );
            camera.LookAt( new Vector3( 0, 0, -300 ) );

            // set the near clipping plane to be very close
            camera.Near = 5;

            // create a new viewport and set it's background color
            viewport = windowOne.AddViewport( camera, 0, 0, 1.0f, 1.0f, 100 );
            viewport.BackgroundColor = ColorEx.Blue;

            // create a camera and initialize its position
            cameraTwo = scene.CreateCamera( "CameraTwo" );
            cameraTwo.Position = new Vector3( 500, 0, 250 );
            cameraTwo.LookAt( new Vector3( 0, 0, -300 ) );

            // set the near clipping plane to be very close
            cameraTwo.Near = 5;

            // create a new viewport and set it's background color
            viewport2 = windowTwo.AddViewport( cameraTwo, 0, 0, 1.0f, 1.0f, 99);
            viewport2.BackgroundColor = ColorEx.Blue;

            // set default mipmap level
            TextureManager.Instance.DefaultNumMipMaps = 5;

            // call the overridden CreateScene method
            // set some ambient light
            scene.AmbientLight = new ColorEx( 1.0f, 0.2f, 0.2f, 0.2f );

            // create a skydome
            scene.SetSkyDome( true, "Examples/CloudySky", 5, 8 );

            // create a simple default point light
            Light light = scene.CreateLight( "MainLight" );
            light.Position = new Vector3( 20, 80, 50 );

            // create a plane for the plane mesh
            Plane plane = new Plane();
            plane.Normal = Vector3.UnitY;
            plane.D = 200;

            // create a plane mesh
            MeshManager.Instance.CreatePlane( "FloorPlane", plane, 200000, 200000, 20, 20, true, 1, 50, 50, Vector3.UnitZ );

            // create an entity to reference this mesh
            Entity planeEntity = scene.CreateEntity( "Floor", "FloorPlane" );
            planeEntity.MaterialName = "Examples/RustySteel";
            scene.RootSceneNode.CreateChildSceneNode().AttachObject( planeEntity );

            // create an entity to have follow the path
            Entity ogreHead = scene.CreateEntity( "OgreHead", "ogrehead.mesh" );

            // create a scene node for the entity and attach the entity
            headNode = scene.RootSceneNode.CreateChildSceneNode( "OgreHeadNode", Vector3.Zero, Quaternion.Identity );
            headNode.AttachObject( ogreHead );

            // retreive and initialize the input system
            input = PlatformManager.Instance.CreateInputReader();
            input.Initialize( windowOne, true, true, false, false );

            engine.StartRendering();

        }

        /// <summary>
        ///    Shows the debug overlay, which displays performance statistics.
        /// </summary>
        protected void ShowDebugOverlay( bool show )
        {
            // gets a reference to the default overlay
            Overlay o = OverlayManager.Instance.GetByName( "Core/DebugOverlay" );

            if ( o == null )
            {
                throw new Exception( string.Format( "Could not find overlay named '{0}'.", "Core/DebugOverlay" ) );
            }

            if ( show )
            {
                o.Show();
            }
            else
            {
                o.Hide();
            }
        }
        /// <summary>
        ///		Loads default resource configuration if one exists.
        /// </summary>
        private void _setupResources()
        {
            string resourceConfigPath = Path.GetFullPath( "EngineConfig.xml" );

            if ( File.Exists( resourceConfigPath ) )
            {
                EngineConfig config = new EngineConfig();

                // load the config file
                // relative from the location of debug and releases executables
                config.ReadXml( "EngineConfig.xml" );

                // interrogate the available resource paths
                foreach ( EngineConfig.FilePathRow row in config.FilePath )
                {
                    ResourceManager.AddCommonArchive( row.src, row.type );
                }
            }
        }

        private void Form1_FormClosing( object sender, FormClosingEventArgs e )
        {
            engine.Shutdown();
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose( bool disposing )
        {
            if ( disposing && ( components != null ) )
            {
                components.Dispose();
            }
            base.Dispose( disposing );
        }
    }
}

--Borrillis

Personal tools