MFC Support for Direct2D – Transforms (1)

Direct2D supports linear transforms like translation, scale, rotation and skew.
If using MFC, we can apply transforms to render targets by calling CRenderTarget::SetTransform. Here is an example that shifts the render target 100 points on x-axis and 150 points on y-axis.

It looks not very handy to fill the matrix structure, so Matrix3x2F utility class can make it easier.

Let’s begin to present each render target transform with a little bit less trivial examples.

Identity transform

Let’s say that we have to draw a picture in the default render target origin (top-left corner) and then, the same image moved at some x and y coordinates.
For first image, call CRenderTarget::SetTransform with an identity matrix, in order to reset the render target transforms. That’s it, any object will be drawn with its unchanged position, shape and size.

Translation transform

Now let’s complete the above code and draw the second image.

Someone can say: “Why applying render target transforms, while we can simply recalculate the destination drawing rectangle?” Well, in this simple case it’s easy to be done, but in the real programming world may be required to render much more complicated objects, and recalculating the drawing parameters may be an overkill. No mention the case of the other transform types (scale, rotation and skew)…

Demo application

Download: Direct2DTranslationDemo.zip (114)

Direct2D Translation Demo

Direct2D Translation Demo

Resources and related articles

(to be continued)

Leave a Reply