Create mobile apps with HTML5, JavaScript and Visual Studio

(Elle) #1

76 msdn magazine


If you’re well-versed in 2D graphics,


you might assume that 3D is similar ex-


cept for the extra dimension. Not quite!


Anyone who’s dabbled in 3D graphics


programming knows how diffi cult it is.


3D graphics programming requires you to


master new and exotic concepts beyond


anything encountered in the conventional


2D world. A lot of preliminaries are


required to get just a little 3D on the


screen, and even then a slight miscalcula-


tion can render it invisible. Consequently,


the visual feedback so important to learn-


ing graphics programming is delayed


until all the programming pieces are in


place and working in harmony.


DirectX acknowledges the profound


difference between 2D and 3D graphics programming with the


division between Direct2D and Direct3D. Although you can mix


2D and 3D content on the same output device, these are very dis-


tinct and diff erent programming interfaces, and there’s no middle


ground. DirectX doesn’t allow you to be a little bit country, a


little bit rock-and-roll.


Or does it?


Interestingly, Direct2D includes some concepts and facilities that


originated in the 3D programming universe. Th rough features such


as geometry tessellation (the decomposition of complex geome-


tries into triangles) and 2D eff ects using shaders (which consist of


special code that runs on the graphics processing unit, or GPU),


it’s possible to exploit some powerful 3D concepts while still


remaining within the context of Direct2D.


Moreover, these 3D concepts can be encountered and explored


gradually, and you get the satisfaction of actually seeing the


results on the screen. You can get your 3D feet wet in Direct2D so


a later plunge into Direct3D programming is a little less shocking.


I guess it shouldn’t be all that surprising that Direct2D incor-


porates some 3D features. Architecturally, Direct2D is built on


top of Direct3D, which allows Direct2D to also take advantage of


the hardware acceleration of the GPU. Th is relationship between


Direct2D and Direct3D becomes more apparent as you begin


exploring the nether regions of Direct2D.


I’ll commence this exploration with


a review of 3D coordinates and coordi-


nate systems.


The Big Leap Outward


If you’ve been following this column


in recent months, you know it’s possi-


ble to call the GetGlyphRunOutline


method of an object that implements


the IDWriteFontFace interface to obtain


an ID2D1PathGeometry instance that


describes the outlines of text characters in


terms of straight lines and Bézier curves.


You can then manipulate the coordinates


of these lines and curves to distort the


text characters in various ways.


It’s also possible to convert the 2D


coordinates of a path geometry into 3D coordinates, and then manip-


ulate these 3D coordinates before converting them back into 2D to


display the path geometry normally. Does that sound like fun?


Coordinates in two-dimensional space are expressed as number


pairs (X, Y), which correspond to a location on the screen; 3D


coordinates are in the form (X, Y, Z) and, conceptually, the Z axis is


orthogonal to the screen. Unless you’re dealing with a holographic


display or a 3D printer, these Z coordinates aren’t nearly as real as


X and Y coordinates.


There are other differences between 2D and 3D coordinate


systems. Conventionally the 2D origin—the point (0, 0)—is the


upper-left corner of the display device. Th e X coordinates increase


to the right and Y coordinates increase going down. In 3D, very


oft en the origin is in the center of the screen, and it’s more akin to


a standard Cartesian coordinate system: Th e X coordinates still


increase going to the right, but the Y coordinates increase going up,


and there are negative coordinates as well. (Of course, the origin,


scale, and orientation of these axes can be altered with matrix


transforms, and usually are.)


Conceptually, the positive Z axis can either point out of the


screen or point into the screen. Th ese two conventions are known


as “right-hand” and “left-hand” coordinate systems, referring to


a technique to distinguish them: With a right-hand coordinate


system, if you point the index finger of your right hand in the


direction of the positive X axis, and the middle finger in the


direction of positive Y, your thumb points to positive Z. Also, if you


curve the fi ngers of your right hand from the positive X axis to the


A 2D Portal into a 3D World


DIRECTX FACTOR CHARLES PETZOLD


Code download available at msdn.microsoft.com/magazine/msdnmag0214.

Figure 1 The Coordinate System Used


for the Programs in This Article

Free download pdf