开发环境:
- Windows 11 家庭中文版
- Microsoft Visual Studio Community 2019
- VTK-9.3.0.rc0
- vtk-example
- 参考代码
demo解决问题:点附近创建左边或其他信息,
且信息面板显示状态不受相机缩放、旋转影响
prj name: BillboardTextActor3D
#include <vtkActor.h>
#include <vtkBillboardTextActor3D.h>
#include <vtkMinimalStandardRandomSequence.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkTextProperty.h>#include <iostream>
#include <sstream>
#include <string>namespace {
void ActorCallback(vtkObject* caller, long unsigned int vtkNotUsed(eventId),void* clientData, void* vtkNotUsed(callData))
{auto textActor = static_cast<vtkBillboardTextActor3D*>(clientData);auto actor = static_cast<vtkActor*>(caller);std::ostringstream label;label << std::setprecision(3) << actor->GetPosition()[0] << ", "<< actor->GetPosition()[1] << ", " << actor->GetPosition()[2]<< std::endl;textActor->SetPosition(actor->GetPosition());textActor->SetInput(label.str().c_str());
}void RandomPosition(double p[3], double const& min_r, double const& max_r,vtkMinimalStandardRandomSequence* rng);} // namespaceint main(int, char*[])
{vtkNew<vtkNamedColors> colors;// For testingvtkNew<vtkMinimalStandardRandomSequence> rng;// rng->SetSeed(8775070);rng->SetSeed(5127);// Create a renderervtkNew<vtkRenderer> renderer;renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());// Create a render windowvtkNew<vtkRenderWindow> renderWindow;renderWindow->AddRenderer(renderer);renderWindow->SetWindowName("BillboardTextActor3D");// Create an interactorvtkNew<vtkRenderWindowInteractor> renderWindowInteractor;renderWindowInteractor->SetRenderWindow(renderWindow);// Create a spherevtkNew<vtkSphereSource> sphereSource;//公用一个data sourcesphereSource->SetCenter(0.0, 0.0, 0.0);sphereSource->SetRadius(1.0);auto min_r = -10.0;auto max_r = 10.0;for (auto i = 0; i < 10; ++i){if (i == 0){// Create an actor representing the originvtkNew<vtkPolyDataMapper> mapper;mapper->SetInputConnection(sphereSource->GetOutputPort());vtkNew<vtkActor> actor;actor->SetMapper(mapper);actor->SetPosition(0, 0, 0);actor->GetProperty()->SetColor(colors->GetColor3d("Peacock").GetData());renderer->AddActor(actor);}// Create a mappervtkNew<vtkPolyDataMapper> mapper;mapper->SetInputConnection(sphereSource->GetOutputPort());// Create an actorvtkNew<vtkActor> actor;actor->SetMapper(mapper);actor->SetPosition(0, 0, 0);actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());// Setup the text and add it to the renderervtkNew<vtkBillboardTextActor3D> textActor;textActor->SetInput("");textActor->SetPosition(actor->GetPosition());textActor->GetTextProperty()->SetFontSize(12);textActor->GetTextProperty()->SetColor(colors->GetColor3d("Gold").GetData());textActor->GetTextProperty()->SetJustificationToCentered();renderer->AddActor(actor);renderer->AddActor(textActor);// If you want to use a callback, do this:// vtkNew<vtkCallbackCommand> actorCallback;// actorCallback->SetCallback(ActorCallback);// actorCallback->SetClientData(textActor);// actor->AddObserver(vtkCommand::ModifiedEvent, actorCallback);// Otherwise do this:double position[3];RandomPosition(position, min_r, max_r, rng);//随机生成坐标actor->SetPosition(position);std::ostringstream label;label << std::setprecision(3) << actor->GetPosition()[0] << ", "<< actor->GetPosition()[1] << ", " << actor->GetPosition()[2]<< std::endl;textActor->SetPosition(actor->GetPosition());textActor->SetInput(label.str().c_str());}renderWindow->Render();renderWindow->SetWindowName("BillboardTextActor3D");renderWindowInteractor->Start();return EXIT_SUCCESS;
}namespace {void RandomPosition(double p[3], double const& min_r, double const& max_r,vtkMinimalStandardRandomSequence* rng)
{for (auto i = 0; i < 3; ++i){p[i] = rng->GetRangeValue(min_r, max_r);rng->Next();}
}} // namespace