Search code examples
graphicsdirectxdirectx-9directx-11

draw stipple line using vertex buffer in directx11


In OpenGL we can set line pattern usingglEnable (GL_LINE_STIPPLE);glLineStipple(2,0x00FF); And in dx9, we can draw stipple line using ID3DXLine's method SetPattern(0x00FF).

But it seems that there is not such a method in dx11 to set pattern for stipple line. If it is true i wonder if there is a smart way to draw stipple line in dx11?


Solution

  • You might look at this question. It asks how to do line stipple in non-deprecated modern OpenGL, which is similar in functionality to Direct3D 10+.

    My answer basically was to use a combination of alpha testing and the geometry shader to do it:

    Perhaps you could also use a 1D texture with the alpha (or red) channel encoding the pattern as 0.0 (no line) or 1.0 (line) and then have the line's texture coordinate go from 0 to 1 and in the fragment shader you make a simple alpha test, discarding fragments with alpha below some threshold. You can facilitate the geometry shader to generate your line's texCoords, as otherwise you need different vertices for every line. This way you can also make the texCoord dependent on the screen space length of the line.

    The whole thing get's more difficult if you draw triangles (using polygon mode GL_LINE). Then you have to do the triangle-line transformation yourself in the geometry shader, putting in triangles and putting out lines (that could also be a reason for deprecating polygon mode in the future, if it hasn't already).

    Although this question was about OpenGL, the basic principles are exactly the same, you just have to map the shaders from the answer to HLSL, which shouldn't be too difficult given their simplicity.