# How to only render one vtkRender in vtkRenderWindow with two vtkRenders

**URL:** <https://discourse.vtk.org/t/how-to-only-render-one-vtkrender-in-vtkrenderwindow-with-two-vtkrenders/11425>\
**Category:** Development\
**Tags:** code\
**Created:** [May 10, 2023, 11:48am UTC](https://discourse.vtk.org/t/how-to-only-render-one-vtkrender-in-vtkrenderwindow-with-two-vtkrenders/11425 "2023-05-10T11:48:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pisces2018](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pisces2018/32/7063_2.png) [@Pisces2018](https://discourse.vtk.org/u/Pisces2018)\
**Post date:** [May 10, 2023, 11:48am UTC](https://discourse.vtk.org/t/how-to-only-render-one-vtkrender-in-vtkrenderwindow-with-two-vtkrenders/11425/1 "2023-05-10T11:48:31Z")

</div>

Hello everyone,  
I want to draw a polygon on the volumn data in freehand drawing. When mouse move, the volumn data and the ploygon render together, so the screen will stutter.  
So I split the two data into two renders, when mosue move, only render the vtkRender with polygon. The method works, but the drawing result is wrong, I want the polygon without triangles.

```auto
m_pClipRender = vtkRenderer::New();
 this->m_pClipRender->SetLayer(1);

vtkNew<vtkPoints> points1;
for (int i = 0; i < vecDisPts.size(); i++)
{
	points1->InsertNextPoint(vecDisPts[i].GetX(), vecDisPts[i].GetY(), 0.0);
}
 
 vtkNew<vtkCellArray> polys1;
 polys1->InsertNextCell(points1->GetNumberOfPoints() + 1);
 for (int i = 0; i < points1->GetNumberOfPoints(); i++)
 {
 	polys1->InsertCellPoint(i);
 }
 polys1->InsertCellPoint(0);

//Polyline
m_pClipPolylinePolyData->SetPoints(points1);
m_pClipPolylinePolyData->SetLines(polys1);

```

[/uploads/default/original/2X/6/600c3605f30804887b28d432e725d793d57aef96.mp4](https://discourse.vtk.org/uploads/default/original/2X/6/600c3605f30804887b28d432e725d793d57aef96.mp4)

---

<div class="post-metadata">

**Author:** ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)\
**Post date:** [May 10, 2023, 2:44pm UTC](https://discourse.vtk.org/t/how-to-only-render-one-vtkrender-in-vtkrenderwindow-with-two-vtkrenders/11425/2 "2023-05-10T14:44:33Z")

</div>

The code for creating the polyline seems fine. I wonder if you’re adding a new actor to the renderer (or a new renderer) each time the polyline is re-created.

---

<div class="post-metadata">

**Author:** ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)\
**Post date:** [May 10, 2023, 4:43pm UTC](https://discourse.vtk.org/t/how-to-only-render-one-vtkrender-in-vtkrenderwindow-with-two-vtkrenders/11425/3 "2023-05-10T16:43:09Z")

</div>

I think you need to remove this line from your code:

> [@Pisces2018](#):
>
> `polys1->InsertCellPoint(0);`

You only only want to do that when the polygon is complete.

---

<div class="post-metadata">

**Author:** ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)\
**Post date:** [May 10, 2023, 5:28pm UTC](https://discourse.vtk.org/t/how-to-only-render-one-vtkrender-in-vtkrenderwindow-with-two-vtkrenders/11425/4 "2023-05-10T17:28:59Z")

</div>

Also, you asked about “one vtkRenderWindow, two vtkRenderers”, maybe the following information is useful:

The color buffer and the depth buffer are owned by the window, not by the renderers. The renderers share the window’s color buffer and depth buffer. So when you render the polygon (in Layer 1), the rendered pixels stay in the buffer until the volume (in Layer 0) is re-rendered.

---

<div class="post-metadata">

**Author:** ![Pisces2018](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pisces2018/32/7063_2.png) [@Pisces2018](https://discourse.vtk.org/u/Pisces2018)\
**Post date:** [May 11, 2023, 1:04am UTC](https://discourse.vtk.org/t/how-to-only-render-one-vtkrender-in-vtkrenderwindow-with-two-vtkrenders/11425/5 "2023-05-11T01:04:49Z")

</div>

If I remove this line, the result is a polyline, not a closed polygon.Like this:

```auto
 	vtkNew<vtkCellArray> polys1;
 	polys1->InsertNextCell(points1->GetNumberOfPoints()/* + 1*/);
 	for (int i = 0; i < points1->GetNumberOfPoints(); i++)
 	{
 		polys1->InsertCellPoint(i);
 	}
// polys1->InsertCellPoint(0);

```

[/uploads/default/original/2X/d/d11184166417f3ab9ce55855a68812d7db3bb863.mp4](https://discourse.vtk.org/uploads/default/original/2X/d/d11184166417f3ab9ce55855a68812d7db3bb863.mp4)

---

<div class="post-metadata">

**Author:** ![Pisces2018](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pisces2018/32/7063_2.png) [@Pisces2018](https://discourse.vtk.org/u/Pisces2018)\
**Post date:** [May 11, 2023, 1:11am UTC](https://discourse.vtk.org/t/how-to-only-render-one-vtkrender-in-vtkrenderwindow-with-two-vtkrenders/11425/6 "2023-05-11T01:11:06Z")

</div>

When the left mouse button is pressed, I create vtkPolyData, vtkPolyDataMapper2D, and vtkActor2D; When the mouse moves, I just update the point information in vtkPolyData.

---

<div class="post-metadata">

**Author:** ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)\
**Post date:** [May 11, 2023, 1:42am UTC](https://discourse.vtk.org/t/how-to-only-render-one-vtkrender-in-vtkrenderwindow-with-two-vtkrenders/11425/7 "2023-05-11T01:42:08Z")

</div>

So you mean you that you want the polygon to be filled, not empty? You can use the [vtkContourTriangulator](https://vtk.org/doc/nightly/html/classvtkContourTriangulator.html) filter for that.

---

<div class="post-metadata">

**Author:** ![Pisces2018](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pisces2018/32/7063_2.png) [@Pisces2018](https://discourse.vtk.org/u/Pisces2018)\
**Post date:** [May 11, 2023, 3:33am UTC](https://discourse.vtk.org/t/how-to-only-render-one-vtkrender-in-vtkrenderwindow-with-two-vtkrenders/11425/8 "2023-05-11T03:33:14Z")

</div>

Thanks for your kindly reply!  
At first, I hope the polygon is filled, but during the drawing process, the problem of concave polygons and self-intersecting polygons can occur, which can lead to repeated drawing.Like this:

 ![876231236186160](https://discourse.vtk.org/uploads/default/original/2X/2/2e7ca7532d2c9dc0156a5673f4693c710cb5a393.png)  
I want the result, like this:  
 ![WeChat Screenshot_20230416155916](https://discourse.vtk.org/uploads/default/original/2X/7/7d96f9d0a6f9506c24ce26403e2c8f21dbb3c5d7.png)

Because this problem is not solved, I now want the polygon to be empty!

---

<div class="post-metadata">

**Author:** ![Pisces2018](https://discourse.vtk.org/user_avatar/discourse.vtk.org/pisces2018/32/7063_2.png) [@Pisces2018](https://discourse.vtk.org/u/Pisces2018)\
**Post date:** [May 11, 2023, 3:40am UTC](https://discourse.vtk.org/t/how-to-only-render-one-vtkrender-in-vtkrenderwindow-with-two-vtkrenders/11425/9 "2023-05-11T03:40:14Z")

</div>

Maybe that’s the problem, is there a way to make the process of drawing polygons not jammed, in one vtkRenderWindow with one vtkRender?

---

<div class="post-metadata">

**Author:** ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)\
**Post date:** [May 11, 2023, 4:50am UTC](https://discourse.vtk.org/t/how-to-only-render-one-vtkrender-in-vtkrenderwindow-with-two-vtkrenders/11425/10 "2023-05-11T04:50:18Z")

</div>

> [@Pisces2018](#):
>
> not jammed

I don’t know what you mean, what is “not jammed”?

Maybe after rendering the volume, you want to save the color buffer (the image in the window), and then re-use that image instead of re-rendering the volume every time? That would fix the stutter. But I don’t know a good way to do that with VTK.

---

<div class="post-metadata">

**Author:** ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)\
**Post date:** [May 11, 2023, 4:13pm UTC](https://discourse.vtk.org/t/how-to-only-render-one-vtkrender-in-vtkrenderwindow-with-two-vtkrenders/11425/11 "2023-05-11T16:13:10Z")

</div>

I think the issue is something to do with the points themselves where the first point is being repeated somehow. Perhaps, share more of your code to provide more insight.

---

<div class="post-metadata">

**Author:** ![Hedwig\_Huang](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hedwig_huang/32/7745_2.png) [@Hedwig\_Huang](https://discourse.vtk.org/u/Hedwig_Huang)\
**Post date:** [January 29, 2024, 3:04am UTC](https://discourse.vtk.org/t/how-to-only-render-one-vtkrender-in-vtkrenderwindow-with-two-vtkrenders/11425/12 "2024-01-29T03:04:33Z")

</div>

@Pisces2018 Hi there! It’s very inspiring! I am implementing this feature, too. I am curious that each time when activating the clip function, do you create a new renderer for the window and when finishing the clipping, you null it? And how to update polylines without influcing the volume(before I really need to delete something)? I tried to add widget instead of actor/mapper but it seems not correct.
