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 4 The Code
- The Sample Program
- COM Interface Dependencies
- C++ Class Design
- Initializing Direct3D
- Constructing Scenes
- Maintaining Scenes
- Shutting Down Direct3D
- Convenience Functions
Its time to get to work. We can talk about graphics terminology and programming models all we want, but nothing is going to get done unless we start programming.
In this chapter, well talk about the structure of a Direct3D program. Well discuss the sequence of events that must take place in order to initialize and maintain the Direct3D interfaces. After that, we will talk about class design and the functions that do the work.
The Direct3D AppWizard
Chapter 3 introduced the Direct3D AppWizard. For review, the Direct3D AppWizard is a custom Visual C++ tool that you can use to create complete Direct3D applications. The AppWizard presents a number of dialog boxes that you can use to specify options for the new application. You can decide which object the application will display, what light sources will be used, and even what the class names will be.
The code that the AppWizard generates is the subject of this chapter. Well create an application with the AppWizard and use the resulting code throughout the chapter. All of the books demos were started with the Direct3D AppWizard, so they share the same general structure. Understanding the discussion in this chapter means that you will have a strong understanding of all of the demos.
The Sample Application
We will use the Direct3D AppWizard to create a project called Sample. Chapter 1 outlines the steps required to create a new project, so we wont repeat them here. We will accept the AppWizard defaults in every dialog, but with one exception: Instead of using the default directional light source, we will specify an animated spotlight. The AppWizards Lighting dialog box appears in Figure 4.1.
Figure 4.1: The Direct3D AppWizard light selection dialog box.
Deselect the Directional checkbox, check the Spotlight checkbox, then check the Animate spotlight checkbox. The figure appears with the settings we will use.
Accept the default settings on the remaining dialogs. The AppWizard will create a project that is ready to compile.
Direct3D COM Interface Dependencies
In Chapter 3, we discussed the Direct3D COM interfaces and the functionalities that they support. During the discussion, we learned that some of the interfaces cannot be created without others. The interfaces must be created in a specific order. In this section, well talk about these dependencies and how they affect our programs.
Dependency Trees
The easiest way to analyze dependencies is with a dependency tree. A dependency tree illustrates each interfaces reliance on other interfaces by representing each interface as a node in a tree. A tree root node represents an interface that does not depend on the existence of other interfaces. Subsequent nodes represent interfaces that depend on the root interface either directly or indirectly. Figure 4.2 is a dependency tree for the Direct3D interfaces.
Figure 4.2: A dependency tree for the Direct3D interfaces.
The nodes in the figure contain both the interface name (in bold) and the variable name that we will use to identify each interface.
The figure shows that the only interface that does not depend on other interfaces is the Direct3DRM interface. The lines that travel from Direct3DRM to the DirectDrawClipper and Direct3DRMFrame nodes indicate that these interfaces depend on the existence of the Direct3DRM interface.
The system of nodes extents from the root node to the Direct3DRMViewport, Direct3DRMLight, and Direct3DRMMeshBuilder interface nodes. These interfaces depend indirectly on all of the other interfaces in the tree. Notice that the Direct3DRMViewport interface depends directly on two interfaces.
Growing Your Own Dependency Trees
The dependency tree shown in Figure 4.2 can be divided into two parts. The nodes near the root of the tree represent interfaces that are present in all Direct3D programs, while the remaining nodes represent interfaces vary from one application to the next. Well call the first group the standard interfaces and the second application specific interfaces. Figure 4.3 shows the tree as it appears once this distinction has been made.
Figure 4.3: Separating standard interfaces from application specific interfaces.
The figure illustrates that the interfaces that make up the scene itself are application defined. This distinction will play an important role later when we talk about class design. Our goal will be to automate the creation of the standard interfaces while allowing the application specific interfaces to be created and modified according to the demands of the application.
Sequence Of Events
Dependency trees imply creation order. For example, the Direct3DRM object appears at the top of the tree, so all other objects in the tree depend on its existence. This means that the Direct3DRM object must be created first. After it has been created, either the scene frame or the clipper object can be created. This continues until all of the objects on the branches have been created. Figure 4.4 shows a creation order that complies with the demands of the dependency tree.
Figure 4.4: Interface creation order.
The figure shows that the standard interfaces are created first, and then the application specific interfaces are created. This is not the only possible order because some interface pairs, such as the meshbuilder and meshframe interfaces, can be switched, but the code that we will look at later uses this order.
|