Drawing perfectly uniform stipple lines.

Hello everyone,
I was wondering if I could get some help with rendering stippled lines. I’m currently using a vtkActor attached to a PolyDataMapper of which I customise the Shader property in order to get the desired effect, however on closer inspection I notice that some part (ie: as can be seen in the attached images) the top part of the lines although stippled, seem to be connected in a jagged manner. I was expecting all lines to be uniformly rendered.
Is there a better way of doing this? Thanks in advance.

1 Like


It seems to me that your shader is written to produce a checkerboard pattern in fragment (pixel) space, so, you won’t get the desired effect where the rendered pixels happen to be between the rows and columns of the pattern.

Please, take a look at this: opengl - Dashed line in OpenGL3? - Stack Overflow . They propose some shaders that will actually result in a dashed pattern, if I understood what you want. One of the answers includes a link to a reference on how to make the calculations in shaders to achive several effects accuratelly.



Hi Paulo,
thanks for the response, the shader had always produced the effect I wanted but I only recently noticed the phenomena described above (since moving to VTK 9 - or maybe I never really saw the issue back then). However, it’s interesting to see what I can come up with following your suggestion. I’ll definitely take a look at get back to you. Thanks a bunch

Hi Paulo,

Also, since there’s a possibility I would want to try out the shader suggested in that link I was wondering if by any chance you or anyone here knows where I can find a good VTK sample on writing vertex shaders, I’ve not really found a guide on it - I was fortunate in the past to come across a How-to for the fragment shader code I posted earlier. Thanks in advance

Hi, Seun,

Shaders are not VTK features per si. Shader langage is an OpenGL feature created so developers don’t need to write shaders in assembly or hardware specific languages. VTK exposes shader usage in its API so you don’t need to bother with lower level OpenGL calls. I suggest starting here: OpenGL Shading Language - Wikipedia . From there you can follow on some litterature on the shader language. Primers on game development and 3D art may give a good introduction to shaders with due depth.



Hi Paulo,
Oh no, I think you got me wrong or I probably didn’t explain things well. I know how to write shaders both vertex and fragments if I was writing an OpenGL application / program. My question to be exact is, are there if any samples out there on how to write them properly in VTK (or maybe a small guide, specifically examples on vertex shaders in VTK)? Sorry if I wasn’t clear initially.

Well, then what do you mean by writing shaders “properly in VTK”? I mean, if you program compiles and you get your shader working, you got it “properly in VTK”. VTK simply forwards whatever you wrote to OpenGL via a mapper object and that’s that… You don’t have access to VTK data from inside the shader context, if that’s what you’re looking for. Maybe you can pass information of your original data set via some mechanism (alpha channel?), but I’m speculating. Sorry if I’m missing something.

No worries, I understand I can write the shaders just the way it’s done in OpenGL but am asking for a guide or sample that may show me the little nuances that needs to be addressed in order to get things working in VTK.
an example is the fragment code which involves such lines as VTK::TCoord::Impl both in the shader itself as well as stating it in
inActor->GetShaderProperty()->AddFragmentShaderReplacement("//VTK::TCoord::Impl", true, shader, false);

Am merely seeking to know what I need to do to satisfy VTK, the rest should be pretty straightforward hence why I mentioned a guide or sample code. Thanks for your patience

There are some shader tests that can probably be used for reference:

Disclaimer: I haven’t read through the code to make sure these are relevant.

I can confirm that there are a lot of things you need to learn to make shaders work but there’s not much documentation that I’m aware of. Please share anything you find useful.

These links might help:


Hi David,
thanks for sharing. I find there isn’t sufficient documentation on (shaders themselves), I’m used to just writing shaders, setting my ins and out parameters and letting OpenGL take care of the the rest after compilation - however I struggle to understand the functionality of lines like these


and others, plus I noticed in examples, multiple calls to


I would have thought it would be sufficient to write a shader and just pass it directly to the


with the appropriate vtk decoration (i.e: //VTK::??? if required).

Thanks a bunch David. I’ll definitely read through to get a grip on how things need be setup.

Hi Steve,

Exactly, thanks for verifying what I’ve always experienced. It’s quite unfortunate there isn’t much documentation on shaders and their usage. One would think shaders ought to be pretty straightforward to deploy. Oh and when you say “I can confirm that there are a lot of things you need to learn to make shaders work” you mean in VTK right?
thanks for the link, I’ll add that to my growing list. I really need to get to the bottom of my stipple line issue.

Yes, I mean specifically VTK has conventions about variable naming, coordinate systems, and other details that so far I think you can only learn by reading the code.

I think it’s the line where you coerce to integer. As noted in one of the comments above this will lead to aliasing. You can instead use the fractional part of the fragment coordinate to modulate intensity, perhaps through a shaping function (e.g. use modf after dividing).

There is a VTK example for rendering stippled lines: https://kitware.github.io/vtk-examples/site/Python/Rendering/StippledLine/

If you figure out a simpler/better way to do this then please submit it to the vtk-examples repository.

1 Like

As noted in one of the comments above this will lead to aliasing. You can instead use the fractional part of the fragment coordinate to modulate intensity, perhaps through a shaping function (e.g. use modf after dividing)

That’s a good idea - it didn’t really occur to me, oh by the way that book is truly a gem. Thanks

Hi Andras
Thanks, I had a quick look but the stipple approach in there seems a bit different from what am seeking to achieve. If I do resolve this (that is if it’s better than what I currently have without the jagged edges) I’ll definitely submit it to the vtk-examples repository (hopefully it saves others the trouble in the future).