< PREVIOUS: Introduction | NEXT: Mouse Events >
 

Creating a Basic Scene

The Scene Graph

Ascend uses an internal structure called a scene graph in rendering objects. An example scene graph representing a plane might look like the following:
scene_graph.png
A scene graph specifies how objects are rendered relative to each other. In the above example, the wings and fin would be rendered relative to the overall plane origin, while the engines would be rendered relative to their respective wing origins. In Ascend, objects in the scene graph are called scene nodes, and are represented by the SceneNode class. There are three distinct types of SceneNodes:
  • LightSceneNode: A SceneNode containing light content, used for illuminating a scene
  • ModelSceneNode: A SceneNode containing 3D geometry content
  • AnimatedSceneNode: A ModelSceneNode that has animation capabilities
A SceneNode exposes the following transformation properties:
  • X, Y, Z
  • AngleX, AngleY, AngleZ
  • ScaleX, ScaleY, ScaleZ
In actuality, SceneNode inherits from TransformableObject, which is the class that contains the transformation properties. Bone is an example of another type of TransformableObject, but it is not a SceneNode. The inheritance diagram below describes the set of Ascend scene-related objects. Bones will be described in more detail later (see the Armatures section).

hierarchy.png
In specifying tranformation properties, it is important to note that:
  • Ascend uses a Y-up coordinate system, with the positive Z-axis pointing out of the screen
  • Ascend follows a yaw-pitch-roll / YZX rotation convention and expects angles in degrees
  • Transformation properties are always applied relative to a TransformableObjects's transformation parent

Setting SceneNode Content

By default, a SceneNode is empty, having no content. In order for a SceneNode to have a visible effect on the scene, its appropriate content property must be set.

LightSceneNode

The content of a LightSceneNode can be set via the Light property, which is a System.Windows.Media.Media3D.Light. This value can also be set via the constructor. The following example shows how to create a LightSceneNode with a DirectionalLight as its content:

LightSceneNode light = new LightSceneNode(new DirectionalLight(Colors.White, new Vector3D(1, 1, 1)));

ModelSceneNode and AnimatedSceneNode

The content of a ModelSceneNode or AnimatedSceneNode can be set via the Model property, which is a System.Windows.Media.Media3D.GeometryModel3D. This value can also be set via the constructor. There are two ways to instantiate a GeometryModel3D:
  • Manually: The developer can manually create a GeometryModel3D instance using C#'s built-in APIs
  • Via ModelMaker: The developer can call one of Ascend's built-in APIs for generating common shapes via the ModelMaker static class (consult the API documentation for more details):
    • ModelMaker.MakeTriangle
    • ModelMaker.MakeCube
    • ModelMaker.MakeSphere
    • ModelMaker.MakeCylinder
The following example shows how to create a ModelSceneNode with a GeometryModel3D generated from ModelMaker as its content:

ModelSceneNode model = new ModelSceneNode(ModelMaker.MakeSphere(0.5, Colors.DodgerBlue));

Loading Ascend Models

The developer can load an Ascend (*.asc) model as a hierarchical SceneNode by using the static method Loader.Load. This method returns a ModelSceneNode instance that will contain everything necessary (geometry, animations, hierarchy) to represent the model in the scene graph. For details on creating a loadable Ascend model, see the Blender Integration section of this manual. The following example shows how to load an Ascend model from a file:

ModelSceneNode model = Loader.Load("birds.asc");

Note that if the model contains a texture, that texture file must reside in the same directory as the model. For advanced tips on loading models, see Advanced Model Loading.

The Scene3D Class

In Ascend, a scene is represented by the Scene3D class. Two basic components make up a Scene3D:
  • RenderRoot: A SceneNode specifying the root of the Scene3D's scene graph
  • Camera: The camera through which the scene is viewed
RenderRoot is itself a SceneNode but does not contain any content. Any object that is to be rendered must be attached to RenderRoot, either as a child or as a descendant. The following example shows how to attach a SceneNode to the scene graph:

MySceneNode.Parent = MyScene3D.RenderRoot;

// Or

MyScene3D.RenderRoot.Children.Add(MySceneNode);

The SceneViewer3D Control

In order to view a Scene3D in your application, a SceneViewer3D must be inserted into the application's view. The following example shows how to create a SceneViewer3D in XAML:

<!-- MainWindow.xaml -->

<Window x:Class="MyProgram.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:asc="clr-namespace:Ascend;assembly=Ascend"
    Title="MainWindow" Height="350" Width="525">
	
    <Grid>
        <!-- Set up a SceneViewer3D to render the Scene3D -->
        <asc:SceneViewer3D Scene="{Binding Scene}"/>
    </Grid>
</Window>

The main component of a SceneViewer3D is its Scene property, which should be bound to a Scene3D in the view's view-model:

// MainWindow.xaml.cs

// ...
using Ascend;

namespace MyProgram
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Set data context for bindings
            DataContext = this;

            // Initialize the scene
            Scene = new Scene3D();

            // Add objects and lights to scene
            // ...
        }

        /// <summary>
        /// The Scene3D that is rendered.
        /// </summary>
        public Scene3D Scene { get; private set; }
    }
}

Mouse Camera Control

In addition to displaying a scene, the SceneViewer3D class provides built-in capability for mouse camera control. You can enable this via the MouseCameraControl property:

<asc:SceneViewer3D Scene="{Binding Scene}" MouseCameraControl="True"/>

The following mouse button commands control the scene's camera when this property is enabled:
  • Left-click + drag: Rotate (turn-table style)
  • Scroll wheel: Zoom in/out
  • Right-click + drag: Pan along camera's field of view
  • Middle-click + drag: Pan along ground plane (X-Z plane)
For compatibility with laptop track-pads, keyboard keys can be used to emulate the middle and right mouse buttons, when held down while left-clicking. You can enable this via the KeyboardMouseEmulation property, and the emulation keys can be specified via the MiddleMouseEmulatorKey and RightMouseEmulatorKey properties:

<asc:SceneViewer3D Scene="{Binding Scene}" MouseCameraControl="True"
                   KeyboardMouseEmulation="True"
                   MiddleMouseEmulatorKey="LeftAlt"
                   RightMouseEmulatorKey="LeftCtrl"/>

< PREVIOUS: Introduction | NEXT: Mouse Events >
 

Last edited May 1, 2014 at 5:54 PM by menehune23, version 80

Comments

No comments yet.