OpenGL Stencil

Hi Friends,

I want to use very basic Stencil tests in a VTK application.
I’m using VTK 9.2.2 under Windows, OpenGL backend.
All is done inside vtkContext2D (scene, actor), with RenderWindow.StencilCapableOn().

Code in the main rendering pass :

state.glPush();
state.glDisable(GL_DEPTH_TEST);
state.glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
state.glEnable(GL_STENCIL_TEST);
state.glStencilMask(0xFF);
state.glClearStencil(0);
state.glClear(GL_STENCIL_BUFFER_BIT);
state.glStencilFunc(GL_ALWAYS, 1, 0xFF);
state.glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
...
//call to several "glDrawPolygon" to fill some polygonal areas
...
state.glStencilMask(0x00);
state.glStencilFunc(GL_EQUAL, 1, 0xFF);
state.glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
state.glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
...
//draw a pattern with "glDrawLines", that fills the entire viewport

What I’m expecting, is that the pattern is only visible inside the polygonal areas.
What I’m seeing, is the pattern everywhere.

So I’ve tested a simpler code :

state.glPush();
state.glDisable(GL_DEPTH_TEST);
state.glEnable(GL_STENCIL_TEST);
state.glStencilMask(0xFF);
state.glClearStencil(0);
state.glClear(GL_STENCIL_BUFFER_BIT);
state.glStencilFunc(GL_NEVER, 0, 0xFF);
...
//call to several glDrawPolygon to fill some polygonal areas

What I’m expecting, is that there is nothing visible.
What I’m seeing is all my polygons.

I’ve dug inside VTK code, and found that “vtkOpenGLLabeledContourMapper” is using such an approach, but with some Shader (weird) code.

Could you please help me on that one ?
:wink:

Hello,

I don’t know how you got the state object, but I’d advise to take a look at this: How to use original OpenGL in VTK program? - Support - VTK.

So you basically need to create a standalone OpenGL window and then pass its OpenGL context to VTK to use for rendering. The code example is in C++, then you’ll need to translate it to whatever you’re using.

best,

PC

As the example code is no longer available, I saved it from Wayback Machine here: PauloCarvalhoRJ/VTKwithOpenGLcalls: This is an example of an application with direct OpenGL calls and VTK. (github.com). It’s an older VTK 5 code, but it can help you get started.

Tks Paulo, I’ll start with this sample to try to understand the whole thing.

Your sample is pretty straightforward, and I dont see any difference with vtkContext2D.

The only thing that I can see for now, is that the filter “vtkOpenGLLabeledContourMapper” uses a Shader (almost empty) to make it append.
Will dig that way.

Didn’t find any way to make it append …
Anyone else with some advice ?

It appears that current (checked until VTK 9.3.1) OpenGLContext2D implementation is not stencil capable, despite setting StencilCapableON.

The issue lies (at least) inside vtkOpenGLFramebufferObject.cxx, where buffer attachment is made with GL_DEPTH_ATTACHMENT, where it should be GL_DEPTH_STENCIL_ATTACHMENT when StencilCapableON.

Hello,

I think the community could benefit from your findings if you report it here: https://gitlab.kitware.com/vtk/vtk/-/issues

thanks,

PC

https://gitlab.kitware.com/vtk/vtk/-/issues/19419

1 Like