November 29, 2020
Hot Topics:

Creating an Interactive 3D world with C# and DirectX

  • By Fatima Ahmed
  • Send Email »
  • More Articles »

3. Set Camera

In the Form1 class, you declare and initialize some variables to set up the camera position and then move it through the scene as follows:

//Declaring variables for camera movement
float sngPositionX;
float sngPositionY;
float sngPositionZ;
float snglookatX;
float snglookatY;
float snglookatZ;
float sngAngleX;
float sngAngleY;

Now, coming back to the Form1_Load event, you set the position of the camera:

//Set the view to perspective

The arguments for the above function are:

sAngleFOV Angle in degrees representing the FOV (Field of vision) of the view.
sFarPlane Maximum distance of vision. Everything after this imaginary far plane will be not rendered.
//set our camera location and lookat point, etc.
sngPositionX = 0.0f;
sngPositionY = 0.3f;
sngPositionZ = 0.0f;
snglookatX   = 0.0f;
snglookatY   = 0.0f;
snglookatZ   = 0.0f;
sngAngleY    = -1.57f;

With SetCamera, you can't define a camera with a totally vertical orientation. To do this, you have to use the RotateX method or use special matrices. SetCamera also can be used with a trigonometry equation to get a fully working camera system.

TVScene.SetCamera(xpos As Single, ypos As Single,
                  zpos As Single, xlookat As Single,
                  ylookat As Single, zlookat As Single)
xpos X coordinate of the camera position
ypos Y coordinate of the camera position (altitude)
zpos Z coordinate of the camera position
xlookat X coordinate of the camera look at point
ylookat Y coordinate of the camera look at point (altitude)
zlookat Z coordinate of the camera look at point

The behaviour is undefined if you use Position = LookAtPoint values.

//The camera is initially set to the origin (0, 0, 0) values
TVScene.SetCamera(sngPositionX, sngPositionY, ngPositionZ, snglookatX,
                  snglookatY, snglookatZ);

Figure 9: Setting camera position and look at position

Page 4 of 7

This article was originally published on February 3, 2006

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date