Search code examples
direct2dsharpdx

Is it possible to always draw a 1 pixel (device independent) line direct2d?


I'm using the DrawLine function on the render target and would like to always draw a line that is one (device independent) pixel thick.

My problem is that I have a transform which has wildly different horizontal and vertical dimensions, and it appears that I can only scale the strokeWidth for one of these dimensions.

I can just set the transform to the Identity, and use the matrix transform point to translate each point to my device independent coordinates which achieves the correct result, but the work isn't being offloaded to the GPU.

Is there a way to do this so that I can let the transform on the render target do the work?

I'm using SharpDX from C#, but I'm happy to translate any c++ answer.


Solution

  • You should be able to take advantage of the fact that the transform on ID2D1RenderTarget is absolute. There is no push/pop system, and you can always just set the transform to the identity matrix. With this knowledge, you should be able to 1) create the geometry that you want, 2) transform it by the matrix on the render target (ID2D1Factory::CreateTransformedGeometry(), although you're correct that this is not hardware accelerated), 3) set the render target's transform to the identity matrix, 4) draw the geometry w/ a 1px stroke width, 5) restore the original transform onto the render target.

    Also, the version of Direct2D that comes with Win8 has some features to let you always draw with a 1px wide line, regardless of the transform. You create a stroke style and specify D2D1_STROKE_TRANSFORM_TYPE_HAIRLINE for the transformType.