# How to obtain the transformation matrix of VTK?

**URL:** <https://discourse.vtk.org/t/how-to-obtain-the-transformation-matrix-of-vtk/14833>\
**Category:** Development\
**Created:** [November 7, 2024, 6:32am UTC](https://discourse.vtk.org/t/how-to-obtain-the-transformation-matrix-of-vtk/14833 "2024-11-07T06:32:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincac](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lincac/32/9404_2.png) [@Lincac](https://discourse.vtk.org/u/Lincac)\
**Post date:** [November 7, 2024, 6:32am UTC](https://discourse.vtk.org/t/how-to-obtain-the-transformation-matrix-of-vtk/14833/1 "2024-11-07T06:32:59Z")

</div>

The matrix I obtained through the following method shows that the variable “wcdc” is different from the variable “MCDCMatrix” in the shader

```auto
	vtkMatrix4x4* wcdc;
	vtkMatrix4x4* wcvc;
	vtkMatrix3x3* norms;
	vtkMatrix4x4* vcdc;
	vtkOpenGLCamera* cam = (vtkOpenGLCamera*)(renderer->GetActiveCamera());
	cam->GetKeyMatrices(renderer, wcvc, norms, vcdc, wcdc);

```

---

<div class="post-metadata">

**Author:** ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)\
**Post date:** [November 7, 2024, 2:44pm UTC](https://discourse.vtk.org/t/how-to-obtain-the-transformation-matrix-of-vtk/14833/2 "2024-11-07T14:44:24Z")

</div>

Hello @Lincac

If your actor has been translated/scaled/rotated, you’ll need to fetch it’s model-to-world transformation matrix and multiply that with the world-to-display transform in order to get the correct model-to-display matrix.

```cpp
vtkMatrix4x4* mcdc = nullptr;

vtkMatrix4x4* wcdc = nullptr;
vtkMatrix4x4* wcvc = nullptr;
vtkMatrix3x3* norms = nullptr;
vtkMatrix4x4* vcdc = nullptr;
auto* camera = renderer->GetActiveCamera();
auto* oglCamera= vtkOpenGLCamera::SafeDownCast(camera);
cam->GetKeyMatrices(renderer, wcvc, norms, vcdc, wcdc);
if (!actor->GetIsIdentity())
{
  auto* oglActor = vtkOpenGLActor::SafeDownCast(actor);
  oglActor ->GetKeyMatrices(mcwc, norms);
  vtkMatrix4x4::Multiply4x4(mcwc, wcdc, mcdc);
}
else
{
  mcdc = wcdc;
}

```

---

<div class="post-metadata">

**Author:** ![Lincac](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lincac/32/9404_2.png) [@Lincac](https://discourse.vtk.org/u/Lincac)\
**Post date:** [November 8, 2024, 1:03am UTC](https://discourse.vtk.org/t/how-to-obtain-the-transformation-matrix-of-vtk/14833/3 "2024-11-08T01:03:41Z")

</div>

Thank you very much for your reply. In my program, I did not transform the Actor. However, when I used RenderDoc to capture the rendering call of VTk, I found that the data passed into the vertex shader was different from the data I read. Did it perform any other operations internally

**basic data**

 ![](https://discourse.vtk.org/uploads/default/original/2X/2/29f5e1a8f794575ef98fb02c3733f1a3c7dd6c5d.jpeg)

**Upload data**  
 ![](https://discourse.vtk.org/uploads/default/original/2X/e/e833fbd5682b42fe84adc875fd15121eff52c502.jpeg)

---

<div class="post-metadata">

**Author:** ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)\
**Post date:** [November 8, 2024, 2:18pm UTC](https://discourse.vtk.org/t/how-to-obtain-the-transformation-matrix-of-vtk/14833/4 "2024-11-08T14:18:45Z")

</div>

It could be possible that the mapper shifted and scaled your coordinates to prevent precision issues in shaders. You can disable that with `vtkOpenGLVertexBufferObject::SetGlobalCoordShiftAndScaleEnabled(false)` and check the `vertexMC` values again to confirm the shift scale is causing it.

When a shift scale is applied, the mapper pre-multiplies an internal 4x4 matrix called `VBOShiftScale` on the `mcwc`

```cpp
vtkMatrix4x4::Multiply4x4(VBOShiftScale, mcwc, mcwcShiftedAndScaled);

```

Unfortunately, there is no easy way to get a pointer to the `VBOShiftScale` matrix outside the mapper. Here’s a way to compute it yourself.

```cpp
if (auto* oglMapper = vtkOpenGLPolyDataMapper::SafeDownCast(mapper))
{
  vtkNew<vtkMatrix4x4> VBOShiftScale;
  auto* vbos = oglMapper->GetVBOs();
  posVBO = vbos->GetVBO("vertexMC");
  if (posVBO && 
      posVBO->GetCoordShiftAndScaleEnabled() &&
      !oglMapper->GetPauseShiftScale())
  {
    const auto& shift = posVBO->GetShift();
    const auto& scale = posVBO->GetScale();
    vtkNew<vtkTransform> transform;
    transform->Identity();
    transform->Translate(shift[0], shift[1], shift[2]);
    transform->Scale(1.0 / scale[0], 1.0 / scale[1], 1.0 / scale[2]);
    transform->GetTranspose(VBOShiftScale);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Lincac](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lincac/32/9404_2.png) [@Lincac](https://discourse.vtk.org/u/Lincac)\
**Post date:** [November 11, 2024, 1:09am UTC](https://discourse.vtk.org/t/how-to-obtain-the-transformation-matrix-of-vtk/14833/5 "2024-11-11T01:09:07Z")

</div>

Thank you very much for your prompt！Based on your advice, I have found the following interface, which will disable VBO offset

```auto
	auto mapper = vtkSmartPointer<vtkOpenGLPolyDataMapper>::New();
	mapper->SetVBOShiftScaleMethod(false);

```

After using this interface, VBO offset will no longer occur in VTK
