Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Cutting Edge Direct 3D Programming
(Publisher: The Coriolis Group)
Author(s): Stan Trujillo
ISBN: 1576100502
Publication Date: 11/01/96

Bookmark It

Search this book:
 
Previous Table of Contents Next


Chapter 9
Viewports

  Field-Of-View Settings
  Picking
  Multiple Viewports

Most of the demos on the CD-ROM require that a viewport be created, so you are already familiar with viewports. In this chapter, we’ll take a closer look, demonstrating viewport features and techniques using the following demos:

  Zoom
  MeshPick
  FacePick
  MultiView

For review, a viewport is the component through which Direct3D scenes are viewed. Viewports are the cameras that we use to see our 3D creations. In order to use a viewport, it must be given a location and orientation. This requirement is met when a viewport is attached to a frame. The frame’s location and orientation determine the portion of the scene that is displayed.

Field-Of-View

In the demos that we looked at in previous chapters, we didn’t specify a field-of-view, or camera angle. This means that we were using the default field-of-view setting of 0.5.

A viewport’s field-of-view can be changed with the Direct3DRMViewport SetField() function. Small values reduce the viewport’s field-of-view and have a telephoto effect. Large values widen the viewport’s field-of-view, causing a wide-angle effect.

The Zoom Demo

In photography, a lens that has an adjustable field-of-view is called a zoom lens. Zoom lenses allow you to “zoom in” on distant objects and “zoom out” to accommodate more scenery.

The Zoom demo uses the SetField() function to adjust the viewport’s field-of-view. The demo places a mesh at the origin and zooms in on the mesh from a stationary position. The Zoom demo appears in Figure 9.1 (but it is very unlikely that you will be able to see any changes in field-of-view by looking at the figure).


Figure 9.1  The Zoom demo.

If you run the Zoom demo, it appears that the mesh is moving. Although the mesh is rotating, its position is stationary. The illusion is caused by the fact that the viewport field-of-view is being changed.

The Zoom demo supports the typical Render menu that allows the rendering method of the mesh to be changed at runtime. Also, an Animation menu is supported. The Animation menu allows you to change the style of the animation (linear versus spline).

The Zoom demo demonstrates the following techniques:

  Using the Direct3DRMViewport SetField() function
  Using the Direct3DRMAnimation to perform general purpose (non-frame) animation.
  Changing a mesh’s rendering method at runtime
  Using the Direct3DRMMaterial interface to alter a mesh’s appearance

The ZoomWin Class

The Zoom demo provides its functionality in the ZoomWin class:

class ZoomWin : public RMWin
{
public:
    ZoomWin();
    BOOL CreateScene();
protected:
    //{{AFX_MSG(ZoomWin)
    afx_msg void OnRenderWireframe();
    afx_msg void OnRenderFlat();
    afx_msg void OnRenderGouraud();
    afx_msg void OnUpdateRenderFlat(CCmdUI* pCmdUI);
    afx_msg void OnUpdateRenderGouraud(CCmdUI* pCmdUI);
    afx_msg void OnUpdateRenderWireframe(CCmdUI* pCmdUI);
    afx_msg void OnAnimationLinear();
    afx_msg void OnAnimationSpline();
    afx_msg void OnUpdateAnimationLinear(CCmdUI* pCmdUI);
    afx_msg void OnUpdateAnimationSpline(CCmdUI* pCmdUI);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
private:
    static void AdjustField(LPDIRECT3DRMFRAME frame, void*, D3DVALUE);
private:
    LPDIRECT3DRMMESHBUILDER meshbuilder;
    static LPDIRECT3DRMFRAME zoomframe;
    static LPDIRECT3DRMANIMATION animation;
};

The class declares two public functions: a constructor and the CreateScene() function. The constructor is used to initialize the class’s non-static data members. (The static data members are initialized to zero automatically, so constructor initialization is not necessary.) Only one non-static data member is present, so the code looks like this:

ZoomWin::ZoomWin()
{
    meshbuilder=0;
}

The CreateScene() function constructs the demo’s scene. We’ll look at CreateScene() soon.

Ten protected member functions are declared. The first six are the message handling functions that provide Render menu functionality. The remaining four implement the demo’s Animation menu. We’ll look at the Animation menu functions later. The Render menu functions are the same functions that appear in almost all of the demos.

One callback function is declared: AdjustField(). We’ll use this callback to change the viewport’s field-of-view during the program’s execution.

Finally, three private data members are declared:

LPDIRECT3DRMMESHBUILDER meshbuilder;
static LPDIRECT3DRMFRAME zoomframe;
static LPDIRECT3DRMANIMATION animation;

The meshbuilder pointer is used to access the demo’s mesh. It is used by the CreateScene() function and by the six Render menu command-handling functions. The zoomframe data member is a pointer to a dummy frame, and the animation data member is a pointer to the Direct3DRMAnimation interface. We’ll use the animation interface and the dummy frame to “animate” the viewport’s field-of-view. You’ll see how this works when we look at the AdjustField() function.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.