# OpenGL Stencil

**URL:** https://discourse.vtk.org/t/opengl-stencil/14272
**Category:** Support
**Created:** [July 22, 2024, 9:19am UTC](https://discourse.vtk.org/t/opengl-stencil/14272 "2024-07-22T09:19:38Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Olivier](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/o/d2c977/32.png) [@Olivier](https://discourse.vtk.org/u/Olivier)
#### Post date: [July 22, 2024, 9:19am UTC](https://discourse.vtk.org/t/opengl-stencil/14272/1 "2024-07-22T09:19:38Z")

</div>

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 ?  
😉

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [July 22, 2024, 2:09pm UTC](https://discourse.vtk.org/t/opengl-stencil/14272/2 "2024-07-22T14:09:28Z")

</div>

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](https://discourse.vtk.org/t/how-to-use-original-opengl-in-vtk-program/4636).

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

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [July 22, 2024, 2:22pm UTC](https://discourse.vtk.org/t/opengl-stencil/14272/3 "2024-07-22T14:22:23Z")

</div>

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)](https://github.com/PauloCarvalhoRJ/VTKwithOpenGLcalls/tree/main). It’s an older VTK 5 code, but it can help you get started.

---

<div class="post-metadata">

### Author: ![Olivier](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/o/d2c977/32.png) [@Olivier](https://discourse.vtk.org/u/Olivier)
#### Post date: [July 22, 2024, 2:24pm UTC](https://discourse.vtk.org/t/opengl-stencil/14272/4 "2024-07-22T14:24:57Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Olivier](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/o/d2c977/32.png) [@Olivier](https://discourse.vtk.org/u/Olivier)
#### Post date: [July 22, 2024, 2:28pm UTC](https://discourse.vtk.org/t/opengl-stencil/14272/5 "2024-07-22T14:28:39Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Olivier](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/o/d2c977/32.png) [@Olivier](https://discourse.vtk.org/u/Olivier)
#### Post date: [July 25, 2024, 2:48pm UTC](https://discourse.vtk.org/t/opengl-stencil/14272/6 "2024-07-25T14:48:58Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Olivier](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/o/d2c977/32.png) [@Olivier](https://discourse.vtk.org/u/Olivier)
#### Post date: [August 3, 2024, 3:07pm UTC](https://discourse.vtk.org/t/opengl-stencil/14272/7 "2024-08-03T15:07:04Z")

</div>

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`.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [August 5, 2024, 5:01pm UTC](https://discourse.vtk.org/t/opengl-stencil/14272/8 "2024-08-05T17:01:43Z")

</div>

Hello,

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

thanks,

PC

---

<div class="post-metadata">

### Author: ![Olivier](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/o/d2c977/32.png) [@Olivier](https://discourse.vtk.org/u/Olivier)
#### Post date: [August 6, 2024, 6:53am UTC](https://discourse.vtk.org/t/opengl-stencil/14272/9 "2024-08-06T06:53:41Z")

</div>

[https://gitlab.kitware.com/vtk/vtk/-/issues/19419](https://gitlab.kitware.com/vtk/vtk/-/issues/19419)
