Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
113 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 Introduction
 DirectX
 DirectDraw
 Direct3D
 Rendering Engine
 File Format
 DirectX and COM
 Summary and
 Appendix


 Printable version

 


DirectX and COM

Before we conclude the overview on Direct3D, we would like to briefly comment in the relationship between DirectX and the Component Object Model (COM), and its usage.

DirectX is based on the principles of COM, which allow us to develop and distribute the required functionality, packaged as components or objects. Most of the objects and interfaces in DirectX are based on COM and many of the DirectX APIs are instantiated as a set of OLE objects.

COM Interface

All the functions supported by a COM object are available as interfaces of that object. An interface is nothing but a group of related functions. The user of a component has to query the component for an interface. If an interface is supported by an object, a reference is returned, which in turn can be used to access the different methods provided in the interface.

IUnknown

To allow a component user to query for an interface, all COM objects have to be derived from the standard IUnknown interface. This interface provides three methods, namely AddRef, QueryInterface and Release.

AddRef

All COM objects work on the principle of reference counting. Whenever a COM object is used, its reference count is incremented by one. The reference count is decremented by one, when the object is released, using the Release method. An object is a valid candidate for garbage collection, when its refernce count becomes zero.

QueryInterface

This method is used to query an object for its supported interface and hence the supported methods. The supported features can be accessed by asking for a specific interface from the COM object. If the interface is supported, QueryInterface returns a pointer to the interface and calls AddRef to increment the reference count on the COM object. It is the responsibility of the application to call Release, after its work with the COM object is over. After getting a pointer to the interface, the application can call specific methods from the interface, to get its job done.

Typically, DirectX provides one object per device.

COM Advantage

An advantage that we get by using COM is that we can have language independence between the COM object and its users. What this means is that a COM object can be used irrespective of the language being used for developing the application requiring 3D capabilities.

Though many languages can be used with COM objects the languages we briefly cover are the C programming language and the C++ programming language. We are considering these two languages as they are the part of the primary languages being used to develop applications and that the differences between using these languages though not being very major, are significant. Using these languages does not change the way we use COM objects for incorporating 3D content into our applications. Another motivitaing factor for choosing these two languages is the comfort level of the authors, in using these languages.

For more details on COM and its usage with other languages, refer [1].

C++ and COM

Code written C++ and COM is less complex that equivalent code written in C and COM. In C++, a COM interface is like an abstract base class, with all methods being pure virtual. Both C++ and COM use virtual tables (vtable) for pure virtual functions.

When COM objects are used through C++, the QueryInterface method returns a pointer to the virtual table and the different methods supported by the object can be accessed directly.

The sample in source listing 1 illustrates the usage of COM objects through C++.

Popup : Source Listing 1

In this sample, the QueryInterface method is being invoked for us by the Direct3DRMCreate function. This function returns a pointer to the Direct3DRM (Direct3D Retained Mode) object, which provides different methods like creation of a viewport, loading a mesh, etc. Notice that we are not calling the AddRef explicitly, but we are calling the Release method, after using the COM object.

C and COM

A major difference between using C and C++ and COM is that the QueryInterface method does no return a pointer to the virtual table, when COM objects are used through C. The methods of the COM object have to be explicitly invoked through the virtual table as is illustrated in the sample code in source listing 2.

Popup : Source Listing 2

Note the explicit use of the pointer to the virtual table lpVtbl and passing of the object itself as the first parameter in each method call.

A few points to be remembered while using COM objects through C are:

  • The first parameter of each method call on an object, has to refer to the object on which the methods is being invoked
  • Each method of an object has to be explicitly accessed through the pointer to the virtual table (lpVtbl)

Notes on Programming

For developing applications using Direct3D on Windows, knowledge of Windows programming using the SDK or MFC is necessary. For more details on programming using SDK and using MFC, refer [9] and [10] respectively.


Next : Summary and Appendix