Search code examples
c++vtk

VTK face keeps rendering backwards


I am trying to render the faces of a cube using VTK 9.2.

The cube's vertices are ordered like so:

//              7-------6
//             /|      /|
//            4-+-----5 |
//            | |     | |   y
//            | 3-----+-2   | z
//            |/      |/    |/
//            0-------1     +--x

(Yes I know this is somewhat atypical ordering, but we're just rendering faces in VTK, so it shouldn't matter as long as we're consistent with the usage)

While one of the faces works perfectly, the other consistently renders backwards regardless of how I define it.

I am using the VTK_PIXEL ordering for each face.

Here is the code that does the rendering:

    vtkGenericOpenGLRenderWindow* renderWindow = vtkGenericOpenGLRenderWindow::New();
    vtkNew<vtkUnstructuredGrid> ugrid;

    // Create and insert vertices
    std::vector<std::vector<double>> vertices(8);
    double halfWidth = 20.0;
    vertices[0] = { - halfWidth, - halfWidth, - halfWidth }; // 0
    vertices[1] = { + halfWidth, - halfWidth, - halfWidth }; // 1
    vertices[2] = { + halfWidth, - halfWidth, + halfWidth }; // 2
    vertices[3] = { - halfWidth, - halfWidth, + halfWidth }; // 3
    vertices[4] = { - halfWidth, + halfWidth, - halfWidth }; // 4 
    vertices[5] = { + halfWidth, + halfWidth, - halfWidth }; // 5 
    vertices[6] = { + halfWidth, + halfWidth, + halfWidth }; // 6 
    vertices[7] = { - halfWidth, + halfWidth, + halfWidth }; // 7

    vtkNew<vtkPoints> points;
    for (auto i = 0; i < 8; ++i)
    {
        points->InsertNextPoint(vertices.at(i).at(0), vertices.at(i).at(1), vertices.at(i).at(2));
    }

    // Create faces
    std::vector<std::array<vtkIdType, 4>> faces;
    faces.push_back({ 3, 2, 7, 6 }); // +Z, works perfectly!

    // -Z:
    faces.push_back({ 1, 0, 5, 4 }); // backwards
    //faces.push_back({ 0, 1, 4, 5 }); // backwards
    //faces.push_back({ 0, 4, 1, 5 }); // backwards
    //faces.push_back({ 5, 1, 4, 0 }); // backwards
    //faces.push_back({ 4, 5, 0, 1 }); // backwards
    //faces.push_back({ 1, 5, 0, 4 }); // backwards
    //faces.push_back({ 4, 0, 5, 1 }); // backwards
    //faces.push_back({ 5, 4, 1, 0 }); // also backwards

    // Insert faces
    for(int i = 0; i < faces.size(); i++) 
    {
        ugrid->InsertNextCell(VTK_PIXEL, 4, faces.at(i).data());
    }

    ugrid->SetPoints(points);

    // Create new data mapper for this snapshot
    vtkNew<vtkDataSetMapper> mapper;
    mapper->SetInputData(ugrid);

    // Create new actor for this data snapshot
    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
    addActorToScene(0, 0.0, actor);

    renderWindow->Render();

The +Z face works fantastic and looks correct. However, the other face is always backwards no matter what node order I try.

This is what I see in my window:

enter image description here

As seen there, the +Z face (3, 2, 7, 6) works great. It appears white on the outside and black on the inside.

But the -Z face does not work - it appears white on the inside of the cube, and black on the outside.


Solution

  • It was a lighting issue, mi aculpa. I was doing the following outside of this code:

    vtkNew<vtkLight> light;
    renderWindow->AddLight(light);
    

    Once I removed that and the default lighting took over, both the inside and outside of each face appear white, so which side appears white does not indicate the direction of the face.