Reader Level:
ARTICLE

Building a Stereoscopic Application

Posted by Andrey Lakhov Articles | DirectX C# October 28, 2011
The task is to develop a stereoscopic applications including visual stereocomponents (Stereo Vision components). This stereoscopic application can be used in architectural visualization, engineering and scientific visualization, educational programs and entertaining programs. As means of the development language, C# and DirectX 9.0 have been chosen.
  • 0
  • 0
  • 15131
Download Files:
 

Use of library SVN most adequately for a development interactive stereoscopic demoscenes.
 
Everyone demoscene represents a set: a subset of geometric objects, a subset of light sources, a subset of structures, a subset of cameras. Each geometric object is set: a subset of vertices (the vertex is defined by the 3D coordinates and coordinate of texture fitting to it), a subset of surfaces (surfaces are defined by three vertices) and a material (the material is characterized by a rate of dispersion and a reflex of light, a texture), a locating of an object (i.e. its translation, an axis of spin, an angle of spin, a rate of scaling). Each light source is set: its attitude, orientation (the vertex in which routes a light source), by type (background, target, dot), a color (in RGB a format). Each texture represents the rectangular raster file, the certain sizes (for example, 62*128, 128*256, 256*512). Each camera is set: The location, target (the vertex in which routes a camera), an angle of sight, an angle of turn concerning the axis.
 
Interactive stereoscopic dynamic demoscenes can is extra to contain: the script, digital characters, dialogues, design. Characters are usually developed in simulating packets, for example, 3DS MAX, Maya. At use of more modern means characters can be framed according to 3D scanning's.
 
As scopes of library of stereovisual components SVN it is possible to yield: architectural visualization, engineering and scientific visualization, educational programs and entertaining programs. Objects fashioned in such programs are better perceived in 3D. They require animations, lively interaction with the user. As a structure of similar programs it is possible to use stereoscopic demoscenes, animated models.
 
For realization 3D applications the applied programming interface should be used, for example - the Direct3D API. Properties of this API are: it is used in Windows, the program is written on C, C ++, or C#, use of API by means of calls from a code. The script 3D applications contains the following: a load of mesh and textures, for each frame - reading of user input, a calculation of physics, AI, a frame rendering by means of API calls, a determination of transformation matrix for meshes, a determination of parameters of drawing, a determination of parameters of a texture, meshes drawing.


Esplorer1.jpg

Pic. 1 Esplorer1
 
Program Esplorer v.1.0 is developed for realization of scientific visualization. The given program is intended for dynamic visualization of geometric models of building constructions (in particular geodetic domes), titles of building constructions, with realization of a stereomode of a viewing. The program works in fullscreen mode. For a viewing of a geometric models in a stereo it is necessary is extra to have a videocard of firm NVidea, to set the driver of a videocard and, fitting to it, the stereodriver. For stereovisualization it is possible to use method PageFlipping (with glasses of firm Edimensional), or a method anagliph (with glasses Red Cyan).
 
There are properties: road2 - a surface of an earth, road0 - a surface of road, road1 - a surface of an earth, house1 - the first dwelling, house2 - the second dwelling, obj - a geometric model of a building construction, word - a title of a construction.

Esplorer2.jpg

Pic.1 Esplorer 2

The graphic library of stereo-visual components SVN is applied to a development of stereoscopic interfaces in interactive demo scenes for architectural visualization and in stereoscopic dynamic applications for scientific visualization.

Example of the program Esplorer v.1.0

Application can become stereo-viewer, only after addition all auxillary classes. Also it loads all components which are required such as road, house, obj, word. The declaration of class is resulted futher:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using D3D=Microsoft.DirectX.Direct3D;
using Microsoft.DirectX.DirectInput;
 
namespace Code136 // Esplorer
{     
      public struct Position
      {     public float x;
            public float y;
            public float z;
      }
      public struct Size
      {
            public float width;
            public float height;
            public float depth;
      }
      public struct Orientation
      {
            public float x;
            public float y;
            public float z;
            public float angle;
      }
      public class Form1 : System.Windows.Forms.Form
      {
            private bool running = true;
            private D3D.Device device=null;
            private Device deviceM=null;
            public const int ScreenWidth = 800;
            public const int ScreenHeight = 600;
            public Road road0, road1, road2;
            public House house1, house2;
            public MyObject obj;
            public MyWord word;
            public Arrow arr1;
            public float Nz,Nx,Ny,Mz,My,Mx;
            public float sX;
            public float sY;
            public float sZ;
            public float Xold=0.0f;
            public float Yold=0.0f;
            public float Zold=0.0f;
            public float sXold;
            public float sYold;
            public float sZold;
            public float scale1;
            string msg;
            private float x1=2.0f;
            private float y1=2.0f;
            private float z1=2.0f;
            private float x2=2.0f;
            private float y2=2.0f;
            private float z2=2.0f;
            private Position pos1;
            private Size size1;
            private Orientation orient1;
            public float angle = 0.0f;
            private D3D.Mesh mesh = null;
            private D3D.Material[] meshMaterials;
            private D3D.Texture[] meshTextures;
            public string StatStr;
            public AlfabitEn AlfE;
            public Size SLetter;

This form contains the following components:

  • Road road1 the auxiliary object representing earth surface,
  • Road road0 the auxiliary object representing road surface,
  • Road road2 the auxiliary object representing earth surface,
  • House house1 the auxiliary object representing civil building,
  • House house2 the auxiliary object representing civil building,
  • MyObject obj the basic object representing geometrical model of architectural object,
  • MyWord word the auxiliary object representing name of basic object.

Application Explorer the constructor

The constructor of the given class looks so:

public Form1()
{
    InitializeComponent();
   
this.SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.Opaque,true);
   
this.KeyDown+=new KeyEventHandler(OnKeyDown);
   
this.MouseUp+=new MouseEventHandler(OnMouseUp);
   
this.MouseDown+=new MouseEventHandler(OnMouseDown);
    SLetter =
new Size();
    SLetter.width=1.0f; SLetter.depth=1.0f; SLetter.height=1.5f;
}

Loading components

The interface for our program stereo-viewer will consist of several components. They contain in the form and include the models of road, earth, building, architectural object. They allow the user to see dynamic stereoscopic virtual scene with architectural object. Component are loaded by separate methods causes InitializeGraphics() method, which is used to load in stereo-viewer a full set of components and establish width, height, and depth of the scene.

public void InitializeGraphics()
{
    string tmp;
   
D3D.PresentParameters presentParams=new D3D.PresentParameters();
    presentParams.SwapEffect =
D3D.SwapEffect.Discard;
   
D3D.Format current = D3D.Manager.Adapters[0].CurrentDisplayMode.Format;
   
if (D3D.Manager.CheckDeviceType(0, D3D.DeviceType.Hardware, current, current, false))
    {
        presentParams.Windowed =
false;
        presentParams.BackBufferFormat = current;
        presentParams.BackBufferCount = 1;
        presentParams.BackBufferWidth = ScreenWidth;
        presentParams.BackBufferHeight = ScreenHeight;
        presentParams.AutoDepthStencilFormat =
D3D.DepthFormat.D16;
        presentParams.EnableAutoDepthStencil =
true;
    }
   
else
   
{
        presentParams.Windowed =
true;
        presentParams.AutoDepthStencilFormat =
D3D.DepthFormat.D16;
        presentParams.EnableAutoDepthStencil =
true;
    }
    device=
new D3D.Device(0,D3D.DeviceType.Hardware, this, D3D.CreateFlags.SoftwareVertexProcessing,presentParams);
    AlfE=
new AlfabitEn(device);
    pos1=
new Position();
   
Position pos2= new Position();
    orient1=
new Orientation();
   
Orientation orient2= new Orientation();
    size1=
new Size();
    pos2.x=0.0f; pos2.y=20.0f; pos2.z=0.0f;
    orient2.x=1.0f; orient2.y=0.0f; orient2.z=0.0f; orient2.angle=1.50f;
    arr1=
new Arrow(device);
    arr1.pos=pos2;
    arr1.orient=orient1;
    Nz=arr1.pos.z;
//0.0f;
   
Nx=arr1.pos.x;//30.0f;
   
Ny=arr1.pos.y;//0.0f;
   
sZ=Nz;
    sY=Ny;
    sX=Nx;
    Mz=-0.81f;
    Mx=-0.81f;
    My=+0.81f;
    pos1.x=16.0f; pos1.y=0.0f; pos1.z=-30.0f;
    size1.width=16.0f; size1.height=0.10f; size1.depth=90.0f;
    road2 =
new Road(device,pos1, size1, "cubegg.x","gr256.bmp");
    pos1.x = 0.0f; pos1.y = 0.0f; pos1.z = -30.0f;
    size1.width = 16.0f; size1.height = 0.1f; size1.depth = 90.0f;
    road0 =
new Road(device,pos1, size1, "cubegr.x","tex0.bmp");
    pos1.x = -16.0f; pos1.y = 0.0f; pos1.z = -30.0f;
    size1.width = 16.0f; size1.height = 0.1f; size1.depth = 90.0f;
    road1 =
new Road(device,pos1, size1, "cubegg.x","gr256.bmp");
    pos1.x = -20.0f; pos1.y = 7.5f; pos1.z = -37.5f;
    size1.width = 8.0f; size1.height = 15.0f; size1.depth = 25.0f;
    house1 =
new House(device,pos1, size1,"cubev1.x","tex1256.bmp");
    pos1.x = 20.0f; pos1.y = 7.5f; pos1.z = -30.5f;
    size1.width = 8.0f; size1.height = 15.0f; size1.depth = 37.5f;
    house2 =
new House(device,pos1, size1,"cubev2.x","tex2256.bmp");
    pos1.x = 0.0f; pos1.y = 9.0f; pos1.z = -8.0f;
    size1.width = 10.0f; size1.height = 10.0f; size1.depth = 20.0f;
    obj =
new MyObject(device, pos1, size1, ""thief.x");
   
pos1.x = -8.0f; pos1.y = 1.0f; pos1.z = -70.0f;
    word =
new MyWord(device,"word","ILINSKAJA N.NOVGOROD", pos1, orient1, size1, 10, 1, 1.0f, AlfE);
    Location=
new Point(0,0);
    Cursor.Position=
new Point(Width/2, Height/2);
}

For stereo-mode it is nesessary to activate full - screen mode. It is made in this method also.

Drawing the stereo viewer

Drawing of the stereo viewer is made in method OnPaint():

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
    device.Clear(
D3D.ClearFlags.Target|D3D.ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0f, 0);
    SetupCamera();
    Graphics g= e.Graphics;
    device.BeginScene();
    arr1.pos.x=sX;
    arr1.pos.y=sY;
    arr1.pos.z=sZ;
    arr1.Draw(device);
    road2.Draw(0.0f,0.0f,0.0f,road2.pos,road2.size);
    road1.Draw(0.0f,0.0f,0.0f,road1.pos,road1.size);
    road0.Draw(0.0f,0.0f,0.0f,road0.pos,road0.size);
    house1.Draw(0.0f, 0.0f, 0.0f, house1.pos, house1.size);
    house2.Draw(0.0f, 0.0f, 0.0f, house2.pos, house2.size);
    obj.Draw(
ref angle, angle / (float)Math.PI, 0.0f,0.0f, obj.pos, obj.size);
    word.Draw(device, word.pos, word.orient, word.size,
"ILINSKAJA N.NOVGOROD");
    g.DrawString(StatStr,
new Font("Times New Roman", 12), new SolidBrush(Color.Black), 0, 0);
    device.EndScene();
    device.Present();
    UpdateInputState();
   
this.Invalidate();
}

Clearing

Clearing of resources is made by method Dispose() :

protected override void Dispose( bool disposing )
{
    if( disposing )
    {
    }
    base.Dispose( disposing );
}

Conclusion


In this article it is submitted the program of stereo-viewer. The application of stereo-viewer, however, can be advanced. Now, it does not support open of files with geometric models, move camera, among other functionalities. This elements could be added.

COMMENT USING

Trending up