VIEWPORT AND CAMERA

At this lesson we will install the "floor" in the "vista", the 3D space.

A piece has a "definition" - the serie of tags we saw in the previous lesson - and need to have an "instalation".

At the file Window1.xaml the "definition" will be placed inside the tags Canvas.Resources.

TIP: A good idea is allways to download the .ZIP file of the lesson and take a look to the "real" code reading the lesson.

After this, the "instalation" will be made inside a Viewport. A "viewport" is the "view" area for the "vista". The lines are:

...
<Viewport3D  Name="VP" Canvas.Top="0" Canvas.Left="0 "  Width="1024"  Height="768"  >
   
    <ModelVisual3D>
     <ModelVisual3D.Content>        
     <Model3DGroup>	   
      

     <!-- Lights-->
     <!--  Children[0]-->
     <AmbientLight Color="White"/> 
     <!--  Children[1]-->
     <DirectionalLight Color="Black" Direction="-100,-100, -100" />  
     <!--  Children[2]-->
     <PointLight Color="Black" Position=" 0 , 1  ,  1000 " Range="1" ConstantAttenuation="3"/>
 
     <!-- Pieces-->
     <!--floor: Children[3]-->
     <Model3DGroup Transform="{StaticResource floorT}"> 
      <GeometryModel3D  Material="{StaticResource floorM}" Geometry="{StaticResource floorG}"/>                  
     </Model3DGroup>
    <!--floor-->

            
    </Model3DGroup>  
    </ModelVisual3D.Content>
   </ModelVisual3D>
  
</Viewport3D>
...

Pay attention that we are also installing, inside the viewport, 3 lights. 2 are not working here because they are black.

VERY IMPORTANT is the information that a piece can be captured by its "number-of-Children": n . This will be used, by example, to move an object - we will see it later.

Attention also to the name of the Viewport:VP, that will be used in the code-behind.

We need to install a camera in the Viewport. We will do it using code-behind (the file:Window1.xaml.cs).

The lines of the code about it we present here, bolded (look the complete code in the downloaded material):

...
public partial class Window1 : NavigationWindow{
  DispatcherTimer _timer = new DispatcherTimer();
  int cont=0;
  PerspectiveCamera myPCamera = new PerspectiveCamera();
  Point3D pos =new Point3D(0, 5, 50);

   public Window1(){
    InitializeComponent();
   }
 
   private void OnLoaded(object sender, EventArgs e){
    _timer.Interval = new System.TimeSpan(0, 0, 0, 0, 1000);
    _timer.Tick += new System.EventHandler(OnTimerEvent);
    //_timer.Stop();
    _timer.Start();
    myPCamera.FarPlaneDistance = 200;
    myPCamera.NearPlaneDistance = 1;
    myPCamera.FieldOfView = 45;
    myPCamera.Position = pos;
    myPCamera.LookDirection = new Vector3D(0, 5, -30);
    myPCamera.UpDirection = new Vector3D(0, 0.5, 0);
    VP.Camera = myPCamera;
  }
...

We need to include a reference to the file of the texture in the .CSPROJ:

...
 <ItemGroup>
...
  <Resource Include="floor.png" />
 </ItemGroup>
...

Look, in the figure, the exercice runing:

To look the source codes and/or run the exercice, create a folder having the name of the .ZIP file and download it - right-clicking the link - and unzip inside the folder :

tu2v.zip

Try to recreate the exercice changing the code or some texture.

Liberate the firewall and play this web3d application:

We are doing our tests at a: Notebook DELL Latitude D600 - Intel Pentium M755 2.0 GHZ - Memory: 512 MB - VideoCard ATI Radeon 9000 32MB DDR 4xAGP - Resolution 1400x1050 - Communication: ADSL 256 kbps (Download: 45 kBps, Upload: 14 kBps)


PREVIOUS LESSON NEXT LESSON
T.CONTENTS HOMEPAGE