i
i
i
i
i
i
i
i
12.4. The Component Object Model 299
Figure 12.2. An example of a COM interface specification.
offered, it would not be particularly useful. However, IUnknown is essential
because it is a COM object’s way of giving access to the other interfaces that
it supports, and it is the way memor y usage by COM objects is managed.
Instances of COM objects and their interfaces take up memory; therefore,
when they are no longer required, that memory should be released (freed up).
That sounds simple enough, but because the same COM object may be in
use by the different interfaces that it offers, by different programs and even
by different hosts, it is not easy to tell whether or not the memory should be
released. F or this reason, COM uses a reference-counting approach to count
the number of programs using an interface. When a pointer to an interface
is requested, (+1) is added to its reference count, and (1) is added when
it is released. Once the reference count reaches zero, memory used by the
i
i
i
i
i
i
i
i
300 12. Tools, Libraries and Templates for VR
interface may be freed, and when all the interfaces supported by a COM
object are released, the memory used by the COM object itself may be freed.
But herein lies a major problem! Get the reference counting wrong and your
program could try to use an object that no longer exists, or it could go on
creating new instances of the object. Not even exiting the program helps;
COM objects exist outside specific applications. Now, you may be a good
programmer who is very careful about freeing up the m emory that you use,
but it is not always easy to tell with the DirectX library how many COM
objects are created when you call for a particular interface.
You may be asking: what has this got to do with me? The answer
is simple: you will be developing programs to make your virtual
world a reality. Those programs will want to run in real time for
a long time (days or weeks of continuous execution). You need
to use COM if you want to use any component of DirectX (and
you will), so if you get reference counting wrong, your program
will either crash the computer or cause it to g rind to a halt!
Luckily, a recent development called the C++ Active Template Library (ATL)
has a special mechanism for automatically releasing COM objects when a
program no longer needs them. We shall employ this strategy in many of our
example programs. Of course, this adds yet another, and visually different,
way of writing COM code to the list, confusing the programmer even more.
ToreadmoreaboutCOM,andthereisalotmoretoberead,thebookby
Templeman [13] shows how COM fits in with the MFC, and Grimes et al. [6]
cov er using COM in the context of the ATL.
Additional details of how to use the COM and a template for writing
COM programs is given in Appendix F.
12.5 Summary
In this chapter, we have raced through a wide range of topics, topics that could
easily occupy several volumes on their own. We have tried to keep the topics
as brief as possible and referred to other sources where each of them is explored
in much greater detail. We hope that you have gained an appreciation of the
way the application programs in the rest of the book will be structured. It
is not essential to have a comprehensive understanding of the detail here in
order to appreciate the practical examples we are now turning to, but we hope
this will make it easier to find your way through the code and thus focus on
i
i
i
i
i
i
i
i
Bibliography 301
the more important parts, where the basic frameworks metamorphose into
the specific applications.
You may have noticed that the Direct3D version mentioned here is Ver-
sion 9. It may be that by the time you are reading, this it will be Version
10, 11, or even 97. The good news is because of COM, everything you read
here should still work exactly as described. We have also noticed that as each
Direct3D version changes, the differences become smaller and tend to focus
on advanced features, such as the change from Shader Model 2 to Shader
Model 3 or the changes that allow programs to take advantage of the ne w fea-
tures of the GPU. Thus, even a new Direct3D version will almost certainly
use initiation code very similar to that given here.
In the remainder of the book, we will get down to some specific examples
of programs in 3D graphics, multimedia and custom interaction for VR.
Bibliography
[1] Apple Computer, Inc. Learning Carbon. Sebastopol, CA: O’Reilly & Asso-
ciates, 2001.
[2] D. Chappell. Understanding ActiveX and OLE. Redmon d, WA: Microsoft Press,
1996.
[3] R. Fernando (editor). GPU Gems. Boston MA: Addison-Wesley Professional,
2004.
[4] Free Software Foundation, Inc. GCC, the GNU Compiler Collection”. http:
//gcc.gnu.org/.
[5] K. Gray. Microsoft DirectX 9 Programmable Graphics Pipeline. Redmond, WA:
Microsoft Press, 2003.
[6] R. Grimes et al. Beginning ATL COM Programming. Birmingham, UK: Wrox
Press, 1998.
[7] Microsoft Corporation. Visual C++ 2005 Express Edition”. http://msdn.
microsoft.com/vstudio/express/visualC/.
[8] M. Pesce. Programming Microsoft DirectShow for Digital Video and Television.
Redmond, WA: Microsoft Press, 2003.
[9] M. Pharr (editor), GPU Gems 2. Boston, MA: Addison-Wesley Professional,
2005.
[10] B. E. Rector and J. M. Newcomer. Win32 Programming. Reading, MA:
Addison-Wesley Professional, 1997.
i
i
i
i
i
i
i
i
302 12. Tools, Libraries and Templates for VR
[11] S. Stanfield. Visual C++ 6 How-To. Indianapolis, IN: Sams, 1997.
[12] J. Swanke. Visual C++ MFC Programming by Example. New York: McGraw
Hill, 1998.
[13] J. Templeman. Beginning MFC COM Programming. Birmingham, UK: Wrox
Press, 1997.
[14] C. Wright. 1001 Microsoft Visual C++ Programming Tips. Roseville, CA: James
Media Group, 2001.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.216.160