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
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, well 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 frames 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 didnt specify a field-of-view, or camera angle. This means that we were using the default field-of-view setting of 0.5.
A viewports field-of-view can be changed with the Direct3DRMViewport SetField() function. Small values reduce the viewports field-of-view and have a telephoto effect. Large values widen the viewports 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 viewports 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 meshs rendering method at runtime
- Using the Direct3DRMMaterial interface to alter a meshs 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 classs 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 demos scene. Well 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 demos Animation menu. Well 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(). Well use this callback to change the viewports field-of-view during the programs execution.
Finally, three private data members are declared:
LPDIRECT3DRMMESHBUILDER meshbuilder;
static LPDIRECT3DRMFRAME zoomframe;
static LPDIRECT3DRMANIMATION animation;
The meshbuilder pointer is used to access the demos 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. Well use the animation interface and the dummy frame to animate the viewports field-of-view. Youll see how this works when we look at the AdjustField() function.
|