// https://docs.wxwidgets.org/3.0/overview_helloworld.html // プリプロセッサに以下二つを追加 // __WXMSW__ // WXUSINGDLL // サブシステムをWindowsに設定(WinMainで呼び出すので) // Windows (/SUBSYSTEM:WINDOWS) #ifndef WX_PRECOMP #include <wx/wx.h> #endif #include <wx/gdicmn.h> // wxPointに必要 #include <wx/frame.h> // wxFrameに必要 #ifdef _DEBUG #pragma comment(lib,"wxbase32ud.lib") #pragma comment(lib,"wxbase32ud_net.lib") #pragma comment(lib,"wxbase32ud_xml.lib") #pragma comment(lib,"wxmsw32ud_adv.lib") #pragma comment(lib,"wxmsw32ud_aui.lib") #pragma comment(lib,"wxmsw32ud_core.lib") #pragma comment(lib,"wxmsw32ud_gl.lib") #pragma comment(lib,"wxmsw32ud_html.lib") #pragma comment(lib,"wxmsw32ud_media.lib") #pragma comment(lib,"wxmsw32ud_propgrid.lib") #pragma comment(lib,"wxmsw32ud_qa.lib") #pragma comment(lib,"wxmsw32ud_ribbon.lib") #pragma comment(lib,"wxmsw32ud_richtext.lib") #pragma comment(lib,"wxmsw32ud_stc.lib") #pragma comment(lib,"wxmsw32ud_webview.lib") #pragma comment(lib,"wxmsw32ud_xrc.lib") #else #pragma comment(lib,"wxbase32u.lib") #pragma comment(lib,"wxbase32u_net.lib") #pragma comment(lib,"wxbase32u_xml.lib") #pragma comment(lib,"wxmsw32u_adv.lib") #pragma comment(lib,"wxmsw32u_aui.lib") #pragma comment(lib,"wxmsw32u_core.lib") #pragma comment(lib,"wxmsw32u_gl.lib") #pragma comment(lib,"wxmsw32u_html.lib") #pragma comment(lib,"wxmsw32u_media.lib") #pragma comment(lib,"wxmsw32u_propgrid.lib") #pragma comment(lib,"wxmsw32u_qa.lib") #pragma comment(lib,"wxmsw32u_ribbon.lib") #pragma comment(lib,"wxmsw32u_richtext.lib") #pragma comment(lib,"wxmsw32u_stc.lib") #pragma comment(lib,"wxmsw32u_webview.lib") #pragma comment(lib,"wxmsw32u_xrc.lib") #endif #include <vtkCylinderSource.h> #include <vtkPolyDataMapper.h> ///////////////////////////////////// ///////////////////////////////////// ///////////////////////////////////// #include <string> #include <wx/wx.h> #include <wx/window.h> #include <vtkRenderWindow.h> #include <vtkRenderer.h> #include <vtkRenderWindowInteractor.h> #include <vtkAutoInit.h> #pragma comment(lib,"opengl32.lib") #pragma comment(lib, "crypt32") #pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "Psapi.Lib") #pragma comment(lib, "winmm.lib") #pragma comment(lib, "shlwapi.lib") #pragma comment(lib, "dbghelp.lib") VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK 8 以降で必要 VTK_MODULE_INIT(vtkInteractionStyle);
// VTKを表示するwxPanelを表示する class MyVTKPanel : public wxPanel { vtkSmartPointer<vtkRenderWindow> renderWindow; vtkSmartPointer<vtkRenderer> renderer; public: vtkSmartPointer<vtkRenderer> getRenderer() {return renderer;} vtkSmartPointer<vtkRenderWindow> getRenderWindow(){return renderWindow;} MyVTKPanel(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size) : wxPanel(parent, id, pos, size) { renderer = vtkSmartPointer<vtkRenderer>::New(); renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer); renderWindow->SetParentId(reinterpret_cast<void*>(this->GetHandle()));// PanelにVTKを設定 renderWindow->SetSize(this->GetSize().GetWidth(), this->GetSize().GetHeight()); // 背景を黒に設定 renderer->SetBackground(0.0, 0.0, 0.0); // wxWidgets ウィンドウの背景色を設定 // これは表示されないので黄色が出てきたらVTK表示が失敗している証拠 this->SetBackgroundColour(*wxYELLOW); // ペイントイベントのハンドラを設定 this->Bind(wxEVT_PAINT, &MyVTKPanel::OnPaint, this); // サイズ変更イベントのハンドラを設定 this->Bind(wxEVT_SIZE, &MyVTKPanel::OnSize, this); this->Layout(); } // 描画イベントのハンドラ void OnPaint(wxPaintEvent& event) { wxPaintDC dc(this); renderWindow->Render(); } // サイズ変更イベントのハンドラ void OnSize(wxSizeEvent& event) { renderWindow->SetSize(this->GetSize().GetWidth(), this->GetSize().GetHeight()); renderWindow->Render(); } };
// マウスで回転・拡大縮小・平行移動を行う設定 void interactor(vtkSmartPointer<vtkRenderWindow> rendererwindow) { vtkSmartPointer<vtkRenderWindowInteractor>renderWindowInteractor; renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); renderWindowInteractor->SetRenderWindow(rendererwindow); }
// 表示するアイテムを追加 void setitem(vtkSmartPointer<vtkRenderer> renderer) { // Create a vtkCylinderSource vtkSmartPointer<vtkCylinderSource> cylinderSource = vtkSmartPointer<vtkCylinderSource>::New(); cylinderSource->SetCenter(0.0, 0.0, 0.0); cylinderSource->SetRadius(5.0); cylinderSource->SetHeight(7.0); cylinderSource->SetResolution(100); // Create a mapper and actor vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper->SetInputConnection(cylinderSource->GetOutputPort()); vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New(); actor->SetMapper(mapper); renderer->AddActor(actor); }
///////////////////////////////////// class MyFrame : public wxFrame { public: MyFrame() : wxFrame(NULL, wxID_ANY, "VTK wx サンプル", wxDefaultPosition, wxSize(400, 300)) { Centre();// ウィンドウを画面中央に表示 // VTKを張り付け MyVTKPanel* vtkpanel = new MyVTKPanel(this, wxID_ANY, wxDefaultPosition, wxSize(300, 300)); // マウスイベントを有効にする interactor(vtkpanel->getRenderWindow()); // アイテムを追加 setitem(vtkpanel->getRenderer()); // ウィンドウをセンタリング wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL); sizer->Add(0, 0, 1, wxEXPAND, 5); sizer->Add(vtkpanel, 0, wxALIGN_CENTER | wxALL, 5); sizer->Add(0, 0, 1, wxEXPAND, 5); this->SetSizer(sizer); } };
class MyApp : public wxApp { public: virtual bool OnInit() { MyFrame* frame = new MyFrame(); // ウィンドウサイズを600x600 frame->SetSize(600, 600); frame->Show(true); return true; } }; ///////////////////////////////////// ///////////////////////////////////// ///////////////////////////////////// // WinMainをマクロで定義 wxIMPLEMENT_APP(MyApp);