forked from trlsmax/imgui-vtk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvtkSDL2OpenGLRenderWindow.cxx
376 lines (322 loc) · 8.89 KB
/
vtkSDL2OpenGLRenderWindow.cxx
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSDL2OpenGLRenderWindow.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 "vtkSDL2OpenGLRenderWindow.h"
#include "vtkCommand.h"
#include "vtkIdList.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLError.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenGLRenderer.h"
#include "vtkOpenGLShaderCache.h"
#include "vtkOpenGLState.h"
#include "vtkOpenGLVertexBufferObjectCache.h"
#include "vtkRendererCollection.h"
#include "vtkSDL2RenderWindowInteractor.h"
#include <cmath>
#include <sstream>
vtkStandardNewMacro(vtkSDL2OpenGLRenderWindow);
const std::string vtkSDL2OpenGLRenderWindow::DEFAULT_BASE_WINDOW_NAME =
"Visualization Toolkit - SDL2OpenGL #";
vtkSDL2OpenGLRenderWindow::vtkSDL2OpenGLRenderWindow()
: WindowId(nullptr)
, ContextId(nullptr)
{
this->SetWindowName(DEFAULT_BASE_WINDOW_NAME.c_str());
this->SetStencilCapable(1);
// set position to -1 to let SDL place the window
// SetPosition will still work. Defaults of 0,0 result
// in the window title bar being off screen.
this->Position[0] = -1;
this->Position[1] = -1;
this->FrameBlitMode = BlitToCurrent;
}
vtkSDL2OpenGLRenderWindow::~vtkSDL2OpenGLRenderWindow()
{
this->Finalize();
vtkRenderer* ren;
vtkCollectionSimpleIterator rit;
this->Renderers->InitTraversal(rit);
while ((ren = this->Renderers->GetNextRenderer(rit)))
{
ren->SetRenderWindow(nullptr);
}
}
void vtkSDL2OpenGLRenderWindow::Clean()
{
/* finish OpenGL rendering */
if (this->OwnContext && this->ContextId)
{
this->MakeCurrent();
this->CleanUpRenderers();
SDL_GL_DeleteContext(this->ContextId);
}
this->ContextId = nullptr;
}
void vtkSDL2OpenGLRenderWindow::CleanUpRenderers()
{
// tell each of the renderers that this render window/graphics context
// is being removed (the RendererCollection is removed by vtkRenderWindow's
// destructor)
this->ReleaseGraphicsResources(this);
}
void vtkSDL2OpenGLRenderWindow::SetWindowName(const char* title)
{
this->Superclass::SetWindowName(title);
if (this->WindowId)
{
SDL_SetWindowTitle(this->WindowId, title);
}
}
//------------------------------------------------------------------------------
void vtkSDL2OpenGLRenderWindow::MakeCurrent()
{
if (this->ContextId)
{
SDL_GL_MakeCurrent(this->WindowId, this->ContextId);
}
}
void vtkSDL2OpenGLRenderWindow::PushContext()
{
SDL_GLContext current = SDL_GL_GetCurrentContext();
this->ContextStack.push(current);
this->WindowStack.push(SDL_GL_GetCurrentWindow());
if (current != this->ContextId)
{
this->MakeCurrent();
}
}
void vtkSDL2OpenGLRenderWindow::PopContext()
{
SDL_GLContext current = SDL_GL_GetCurrentContext();
SDL_GLContext target = this->ContextStack.top();
SDL_Window* win = this->WindowStack.top();
this->ContextStack.pop();
this->WindowStack.pop();
if (target != current)
{
SDL_GL_MakeCurrent(win, target);
}
}
//------------------------------------------------------------------------------
// Description:
// Tells if this window is the current OpenGL context for the calling thread.
bool vtkSDL2OpenGLRenderWindow::IsCurrent()
{
return this->ContextId != 0 && this->ContextId == SDL_GL_GetCurrentContext();
}
bool vtkSDL2OpenGLRenderWindow::SetSwapControl(int i)
{
SDL_GL_SetSwapInterval(i);
return true;
}
//------------------------------------------------------------------------------
void vtkSDL2OpenGLRenderWindow::SetSize(int x, int y)
{
if ((this->Size[0] != x) || (this->Size[1] != y))
{
this->Superclass::SetSize(x, y);
if (this->Interactor)
{
this->Interactor->SetSize(x, y);
}
if (this->WindowId)
{
SDL_SetWindowSize(this->WindowId, x, y);
}
}
}
void vtkSDL2OpenGLRenderWindow::SetPosition(int x, int y)
{
if ((this->Position[0] != x) || (this->Position[1] != y))
{
this->Modified();
this->Position[0] = x;
this->Position[1] = y;
if (this->Mapped)
{
SDL_SetWindowPosition(this->WindowId, x, y);
}
}
}
void vtkSDL2OpenGLRenderWindow::Frame()
{
this->Superclass::Frame();
if (!this->AbortRender && this->DoubleBuffer && this->SwapBuffers)
{
SDL_GL_SwapWindow(this->WindowId);
}
}
int vtkSDL2OpenGLRenderWindow::GetColorBufferSizes(int* rgba)
{
if (rgba == nullptr)
{
return 0;
}
rgba[0] = 8;
rgba[1] = 8;
rgba[2] = 8;
rgba[3] = 8;
return 1;
}
void vtkSDL2OpenGLRenderWindow::SetShowWindow(bool val)
{
if (val == this->ShowWindow)
{
return;
}
if (this->WindowId)
{
if (val)
{
SDL_ShowWindow(this->WindowId);
}
else
{
SDL_HideWindow(this->WindowId);
}
this->Mapped = val;
}
this->Superclass::SetShowWindow(val);
}
void vtkSDL2OpenGLRenderWindow::CreateAWindow()
{
int x = ((this->Position[0] >= 0) ? this->Position[0] : SDL_WINDOWPOS_UNDEFINED);
int y = ((this->Position[1] >= 0) ? this->Position[1] : SDL_WINDOWPOS_UNDEFINED);
int height = ((this->Size[1] > 0) ? this->Size[1] : 300);
int width = ((this->Size[0] > 0) ? this->Size[0] : 300);
this->SetSize(width, height);
#ifdef __EMSCRIPTEN__
SDL_SetHint(SDL_HINT_EMSCRIPTEN_KEYBOARD_ELEMENT, "#canvas");
#endif
this->WindowId = SDL_CreateWindow(
this->WindowName, x, y, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
SDL_SetWindowResizable(this->WindowId, SDL_TRUE);
if (this->WindowId)
{
int idx = SDL_GetWindowDisplayIndex(this->WindowId);
float hdpi = 72.0;
SDL_GetDisplayDPI(idx, nullptr, &hdpi, nullptr);
this->SetDPI(hdpi);
}
}
// Initialize the rendering window.
void vtkSDL2OpenGLRenderWindow::Initialize()
{
int res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
if (res)
{
vtkErrorMacro("Error initializing SDL " << SDL_GetError());
}
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
#ifdef GL_ES_VERSION_3_0
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
#endif
if (!this->WindowId)
{
this->CreateAWindow();
}
if (!this->ContextId)
{
this->ContextId = SDL_GL_CreateContext(this->WindowId);
}
if (!this->ContextId)
{
vtkErrorMacro("Unable to create SDL2 opengl context");
}
this->OpenGLInit();
}
void vtkSDL2OpenGLRenderWindow::Finalize()
{
this->DestroyWindow();
}
void vtkSDL2OpenGLRenderWindow::DestroyWindow()
{
this->Clean();
if (this->WindowId)
{
SDL_DestroyWindow(this->WindowId);
this->WindowId = nullptr;
}
}
// Get the current size of the window.
int* vtkSDL2OpenGLRenderWindow::GetSize(void)
{
// if we aren't mapped then just return the ivar
if (this->WindowId && this->Mapped)
{
int w = 0;
int h = 0;
SDL_GetWindowSize(this->WindowId, &w, &h);
this->Size[0] = w;
this->Size[1] = h;
}
return this->vtkOpenGLRenderWindow::GetSize();
}
// Get the size of the whole screen.
int* vtkSDL2OpenGLRenderWindow::GetScreenSize(void)
{
SDL_Rect rect;
SDL_GetDisplayBounds(0, &rect);
this->Size[0] = rect.w;
this->Size[1] = rect.h;
return this->Size;
}
// Get the position in screen coordinates of the window.
int* vtkSDL2OpenGLRenderWindow::GetPosition(void)
{
// if we aren't mapped then just return the ivar
if (!this->Mapped)
{
return this->Position;
}
// Find the current window position
// x,y,&this->Position[0],&this->Position[1],&child);
return this->Position;
}
// Change the window to fill the entire screen.
void vtkSDL2OpenGLRenderWindow::SetFullScreen(vtkTypeBool arg)
{
if (this->FullScreen == arg)
{
return;
}
if (!this->Mapped)
{
return;
}
// set the mode
this->FullScreen = arg;
SDL_SetWindowFullscreen(this->WindowId, arg ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
this->Modified();
}
void vtkSDL2OpenGLRenderWindow::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "ContextId: " << this->ContextId << "\n";
os << indent << "Window Id: " << this->WindowId << "\n";
}
//------------------------------------------------------------------------------
void vtkSDL2OpenGLRenderWindow::HideCursor()
{
SDL_ShowCursor(SDL_DISABLE);
}
//------------------------------------------------------------------------------
void vtkSDL2OpenGLRenderWindow::ShowCursor()
{
SDL_ShowCursor(SDL_ENABLE);
}