• [C++][vtk][转载]利用vtk绘制文本


    测试环境:

    vs2019专业版

    vtk9.1

    代码:

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include  
    VTK_MODULE_INIT(vtkRenderingOpenGL2);
    VTK_MODULE_INIT(vtkInteractionStyle);
    const char* const HELLO_TEXT = "Hello world";

    int main(int argc, char* argv[])
    {


        auto sphereSource =
            vtkSmartPointer::New();
        sphereSource->SetRadius(3.0);
        sphereSource->SetCenter(0.0, 0.0, 0.0);

        auto sphereMapper =
            vtkSmartPointer::New();
        sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
        auto sphereActor =
            vtkSmartPointer::New();
        sphereActor->SetMapper(sphereMapper);
        sphereActor->GetProperty()->SetOpacity(0.3);

        auto renderer =
            vtkSmartPointer::New();
        renderer->AddActor(sphereActor);
        auto renWindow =
            vtkSmartPointer::New();
        renWindow->SetSize(400, 400);
        renWindow->AddRenderer(renderer);
        auto interactor =
            vtkSmartPointer::New();
        interactor->SetRenderWindow(renWindow);

        int dtype = 2;
        if (dtype == 1)
        {
            auto textActor =
                vtkSmartPointer::New();
            textActor->SetInput(HELLO_TEXT);
            textActor->SetPosition2(20, 40);
            textActor->GetTextProperty()->SetColor(1.0, 0.0, 0.0);
            textActor->GetTextProperty()->SetFontSize(24);
            renderer->AddActor2D(textActor);
        }
        else if (dtype == 2 || dtype == 3)
        {
            auto textSource =
                vtkSmartPointer::New();
            textSource->SetText(HELLO_TEXT);
            auto textMapper =
                vtkSmartPointer::New();
            textMapper->SetInputConnection(textSource->GetOutputPort());
            if (dtype == 2)
            {
                auto textActor =
                    vtkSmartPointer::New();
                textActor->SetMapper(textMapper);
                textActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
                renderer->AddActor(textActor);
            }
            else if (dtype == 3)
            {
                auto textFollower =
                    vtkSmartPointer::New();
                textFollower->SetMapper(textMapper);
                textFollower->GetProperty()->SetColor(1.0, 0.0, 0.0);
                textFollower->SetCamera(renderer->GetActiveCamera());
                renderer->AddActor(textFollower);
            }
        }

        renWindow->Render();
        renderer->ResetCamera();
        renWindow->Render();
        interactor->Start();

        return EXIT_SUCCESS;
    }
    结果:

     

  • 相关阅读:
    win10怎么安装.net framework 3.5?
    用 LMDeploy 高效部署 Llama-3-8B,1.8倍vLLM推理效率
    牛客刷题之图论-最小生成树
    Opencv算术操作
    MIT 6.S081学习笔记(第一章)
    教你自己写Arcpy批处理程序
    堆排序的筛选方法建立的初始堆
    按照规则生成括号
    JAVA 基础与进阶系列索引 -- JAVA 进阶系列
    Maixll-Dock 快速上手
  • 原文地址:https://blog.csdn.net/FL1623863129/article/details/126909995