forked from trlsmax/imgui-vtk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example-imgui-vtk-sdl.cpp
157 lines (130 loc) · 5.02 KB
/
example-imgui-vtk-sdl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*=========================================================================
Program: Visualization Toolkit
Module: Mace.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkImGuiSDL2OpenGLRenderWindow.h"
#include "vtkImGuiSDL2RenderWindowInteractor.h"
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkGlyph3D.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkInteractorStyleSwitch.h>
#include <vtkSphereSource.h>
#include "imgui.h"
#include "imgui_impl_sdl.h"
#include "imgui_impl_opengl3.h"
#if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV))
#include <OpenGLES/ES3/gl.h>
#else
#include <GLES3/gl3.h>
#endif
int main(int, char**)
{
// VTK side, draw
vtkNew<vtkRenderer> renderer;
vtkNew<vtkImGuiSDL2OpenGLRenderWindow> renWin;
renWin->AddRenderer(renderer);
vtkNew<vtkImGuiSDL2RenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
vtkNew<vtkInteractorStyleSwitch> style;
style->SetCurrentStyleToTrackballCamera();
style->SetDefaultRenderer(renderer);
iren->SetInteractorStyle(style);
vtkNew<vtkSphereSource> sphere;
sphere->SetThetaResolution(8);
sphere->SetPhiResolution(8);
vtkNew<vtkPolyDataMapper> sphereMapper;
sphereMapper->SetInputConnection(sphere->GetOutputPort());
vtkNew<vtkActor> sphereActor;
sphereActor->SetMapper(sphereMapper);
vtkNew<vtkConeSource> cone;
cone->SetResolution(6);
vtkNew<vtkGlyph3D> glyph;
glyph->SetInputConnection(sphere->GetOutputPort());
glyph->SetSourceConnection(cone->GetOutputPort());
glyph->SetVectorModeToUseNormal();
glyph->SetScaleModeToScaleByVector();
glyph->SetScaleFactor(0.25);
vtkNew<vtkPolyDataMapper> spikeMapper;
spikeMapper->SetInputConnection(glyph->GetOutputPort());
vtkNew<vtkActor> spikeActor;
spikeActor->SetMapper(spikeMapper);
renderer->AddActor(sphereActor);
renderer->AddActor(spikeActor);
renderer->SetBackground(0.2, 0.3, 0.4);
renWin->SetSize(800, 800);
iren->Initialize();
// ImGui side. The window is created and handled by VTK, we pass it to ImGui.
SDL_Window * window = static_cast<SDL_Window*>(renWin->GetGenericWindowId());
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGuiIO& io = ImGui::GetIO(); (void)io;
iren->SetImguiIO(&io);
SDL_GLContext imgui_context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, imgui_context);
SDL_GL_SetSwapInterval(1); // Enable vsync
ImGui_ImplSDL2_InitForOpenGL(window, imgui_context);
ImGui_ImplOpenGL3_Init();
// Imgui state
bool show_demo_window = false;
// Main loop
bool done = false;
while (!done)
{
renWin->Render();
SDL_Event event;
while (SDL_PollEvent(&event))
{
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
done = true;
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE &&
event.window.windowID == SDL_GetWindowID(window))
done = true;
renWin->PushContext();
done = iren->ProcessEvent(&event);
renWin->PopContext();
}
SDL_GL_MakeCurrent(window, imgui_context);
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame(window);
ImGui::NewFrame();
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
// 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
{
static float f = 0.0f;
static int counter = 0;
ImGui::Begin("Hello imgui-vtk!"); // Create a window called and append into it.
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
bool spike_actor_visible = static_cast<bool>(spikeActor->GetVisibility());
ImGui::Checkbox("Show spikes", &spike_actor_visible);
spikeActor->SetVisibility(static_cast<vtkTypeBool>(spike_actor_visible));
ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
}
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window);
} // render loop
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
SDL_GL_DeleteContext(imgui_context);
SDL_DestroyWindow(window);
SDL_Quit();
}