-
Notifications
You must be signed in to change notification settings - Fork 289
/
glDisplay.h
660 lines (540 loc) · 21.4 KB
/
glDisplay.h
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef __GL_VIEWPORT_H__
#define __GL_VIEWPORT_H__
#include "videoOutput.h"
#include "glUtility.h"
#include "glTexture.h"
#include "glEvents.h"
#include "glWidget.h"
#include <time.h>
#include <vector>
/**
* OpenGL display window and image/video renderer with CUDA interoperability.
*
* @note glDisplay implements the videoOutput interface and is intended to
* be used through that as opposed to directly. videoOutput implements
* additional command-line parsing of videoOptions to construct instances.
*
* @see videoOutput
* @ingroup OpenGL
*/
class glDisplay : public videoOutput
{
public:
/**
* Create a new OpenGL display window with the specified options.
*
* @param title window title bar string, or NULL for a default title
* @param width desired width of the window, or -1 to be maximized
* @param height desired height of the window, or -1 to be maximized
* @param r default background RGBA color, red component (0.0-1.0f)
* @param g default background RGBA color, green component (0.0-1.0f)
* @param b default background RGBA color, blue component (0.0-1.0f)
* @param a default background RGBA color, alpha component (0.0-1.0f)
*/
static glDisplay* Create( const char* title=NULL, int width=-1, int height=-1,
float r=0.05f, float g=0.05f, float b=0.05f, float a=1.0f );
/**
* Create a new OpenGL display window with the specified options.
*/
static glDisplay* Create( const videoOptions& options );
/**
* Destroy window
*/
~glDisplay();
/**
* Open the window.
* @see videoOutput::Open()
*/
virtual bool Open();
/**
* Returns true if the window is open.
*/
inline bool IsOpen() const { return mStreaming; }
/**
* Returns true if the window has been closed.
*/
inline bool IsClosed() const { return !mStreaming; }
/**
* Returns true if between BeginRender() and EndRender()
*/
inline bool IsRendering() const { return mRendering; }
/**
* Get the average frame time (in milliseconds).
*/
inline float GetFPS() const { return 1000000000.0f / mAvgTime; }
/**
* Get the ID of this display instance into glGetDisplay()
*/
inline uint32_t GetID() const { return mID; }
/**
* Return the interface type (glDisplay::Type)
*/
virtual inline uint32_t GetType() const { return Type; }
/**
* Unique type identifier of glDisplay class.
*/
static const uint32_t Type = (1 << 3);
//////////////////////////////////////////////////////////////////////////////////
/// @name Frame Begin + End
//////////////////////////////////////////////////////////////////////////////////
///@{
/**
* Clear window and begin rendering a frame.
* If processEvents is true, ProcessEvents() will automatically be called.
*/
void BeginRender( bool processEvents=true );
/**
* Finish rendering and refresh / flip the backbuffer.
*/
void EndRender();
///@}
//////////////////////////////////////////////////////////////////////////////////
/// @name Image Rendering
//////////////////////////////////////////////////////////////////////////////////
///@{
/**
* Render an OpenGL texture
* @note for more texture rendering methods, @see glTexture
*/
void Render( glTexture* texture, float x=5.0f, float y=30.0f );
/**
* Render a CUDA float4 image using OpenGL interop
* If normalize is true, the image's pixel values will be rescaled from the range of [0-255] to [0-1]
* If normalize is false, the image's pixel values are assumed to already be in the range of [0-1]
* Note that if normalization is selected to be performed, it will be done in-place on the image
*/
void Render( float* image, uint32_t width, uint32_t height, float x=0.0f, float y=30.0f, bool normalize=true, cudaStream_t stream=0 );
/**
* Render a CUDA image (uchar3, uchar4, float3, float4) using OpenGL interop.
* This is similar to RenderOnce(), in that it will begin/end the frame also.
* @see videoOutput::Render
*/
template<typename T> bool Render( T* image, uint32_t width, uint32_t height, cudaStream_t stream=0 ) { return Render((void**)image, width, height, imageFormatFromType<T>(), stream); }
/**
* Render a CUDA image (uchar3, uchar4, float3, float4) using OpenGL interop.
* This is similar to RenderOnce(), in that it will begin/end the frame also.
* @see videoOutput::Render
*/
virtual bool Render( void* image, uint32_t width, uint32_t height, imageFormat format, cudaStream_t stream=0 );
/**
* Render a CUDA image (uchar3, uchar4, float3, float4) using OpenGL interop.
* If normalize is true, the image's pixel values will be rescaled from the range of [0-255] to [0-1]
* If normalize is false, the image's pixel values are assumed to already be in the range of [0-1]
* Note that if normalization is selected to be performed, it will be done in-place on the image
*/
void RenderImage( void* image, uint32_t width, uint32_t height, imageFormat format, float x=0.0f, float y=30.0f, bool normalize=true, cudaStream_t stream=0 );
/**
* Begin the frame, render one CUDA image using OpenGL interop, and end the frame.
* Note that this function is only useful if you are rendering a single texture per frame.
* If normalize is true, the image's pixel values will be rescaled from the range of [0-255] to [0-1]
* If normalize is false, the image's pixel values are assumed to already be in the range of [0-1]
* Note that if normalization is selected to be performed, it will be done in-place on the image
*/
void RenderOnce( void* image, uint32_t width, uint32_t height, imageFormat format, float x=5.0f, float y=30.0f, bool normalize=true, cudaStream_t stream=0 );
/**
* Begin the frame, render one CUDA float4 image using OpenGL interop, and end the frame.
* Note that this function is only useful if you are rendering a single texture per frame.
* If normalize is true, the image's pixel values will be rescaled from the range of [0-255] to [0-1]
* If normalize is false, the image's pixel values are assumed to already be in the range of [0-1]
* Note that if normalization is selected to be performed, it will be done in-place on the image
*/
void RenderOnce( float* image, uint32_t width, uint32_t height, float x=5.0f, float y=30.0f, bool normalize=true, cudaStream_t stream=0 );
///@}
//////////////////////////////////////////////////////////////////////////////////
/// @name Vector Rendering
//////////////////////////////////////////////////////////////////////////////////
///@{
/**
* Render a line in screen coordinates with the specified color
* @note the RGBA color values are expected to be in the range of [0-1]
*/
void RenderLine( float x1, float y1, float x2, float y2, float r, float g, float b, float a=1.0f, float thickness=2.0f );
/**
* Render the outline of a rect in screen coordinates with the specified color
* @note the RGBA color values are expected to be in the range of [0-1]
*/
void RenderOutline( float x, float y, float width, float height, float r, float g, float b, float a=1.0f, float thickness=2.0f );
/**
* Render a filled rect in screen coordinates with the specified color
* @note the RGBA color values are expected to be in the range of [0-1]
*/
void RenderRect( float x, float y, float width, float height, float r, float g, float b, float a=1.0f );
/**
* Render a filled rect covering the current viewport with the specified color
* @note the RGBA color values are expected to be in the range of [0-1]
*/
void RenderRect( float r, float g, float b, float a=1.0f );
///@}
//////////////////////////////////////////////////////////////////////////////////
/// @name Window Resizing
//////////////////////////////////////////////////////////////////////////////////
///@{
/**
* Set the window's size.
*
* @param width the desired width of the window, in pixels.
* @param height the desired height of the window, in pixels.
*
* @note modifying the window's size will reset the viewport
* to cover the full area of the new size of the window.
*/
void SetSize( uint32_t width, uint32_t height );
/**
* Maximize or un-maximize the window.
*/
void SetMaximized( bool maximized );
/**
* Determine if the window is maximized or not.
*/
bool IsMaximized();
/**
* Set the window to fullscreen mode or not.
*/
void SetFullscreen( bool fullscreen );
/**
* Determine if the window is fullscreen or not.
*/
bool IsFullscreen();
///@}
//////////////////////////////////////////////////////////////////////////////////
/// @name Viewport Options
//////////////////////////////////////////////////////////////////////////////////
///@{
/**
* Retrieve the window's background color.
*/
void GetBackgroundColor( float* r, float* g, float* b, float* a=NULL );
/**
* Set the window's background color.
*
* @param r background RGBA color, red component (0.0-1.0f)
* @param g background RGBA color, green component (0.0-1.0f)
* @param b background RGBA color, blue component (0.0-1.0f)
* @param a background RGBA color, alpha component (0.0-1.0f)
*/
void SetBackgroundColor( float r, float g, float b, float a=1.0f );
/**
* Set the active viewport being rendered to.
*
* SetViewport() will update the GL_PROJECTION matrix
* with a new ortho matrix to reflect these changes.
*
* After done rendering to this viewport, you should
* reset it back to it's original with ResetViewport()
*/
void SetViewport( int left, int top, int right, int bottom );
/**
* Reset to the full viewport (and change back GL_PROJECTION)
*/
void ResetViewport();
///@}
//////////////////////////////////////////////////////////////////////////////////
/// @name Status Bar Text
//////////////////////////////////////////////////////////////////////////////////
///@{
/**
* Set the window title string.
*/
virtual void SetStatus( const char* str );
/**
* Set the window title string.
* @deprecated SetTitle() has been superceded by SetStatus() from videoOutput API.
* SetTitle() is functionally the same as SetStatus().
*/
void SetTitle( const char* str );
/**
* Default title bar name
*/
static const char* DEFAULT_TITLE;
///@}
//////////////////////////////////////////////////////////////////////////////////
/// @name Event Handling
//////////////////////////////////////////////////////////////////////////////////
///@{
/**
* Process UI event messages. Any queued events will be dispatched to the
* event message handlers that were registered with RegisterEventHandler()
*
* ProcessEvents() usually gets called automatically by BeginFrame(), so it
* is not typically necessary to explicitly call it unless you passed `false`
* to BeginFrame() and wish to process events at another time of your choosing.
*
* @see glEventType
* @see glEventHandler
*/
void ProcessEvents();
/**
* Register an event message handler that will be called by ProcessEvents()
* @param callback function pointer to the event message handler callback
* @param user optional user-specified pointer that will be passed to all
* invocations of this event handler (typically an object)
*/
void AddEventHandler( glEventHandler callback, void* user=NULL );
/**
* Remove an event message handler from being called by ProcessEvents()
* RemoveEventHandler() will search for previously registered event
* handlers that have the same function pointer and/or user pointer,
* and remove them for being called again in the future.
*/
void RemoveEventHandler( glEventHandler callback, void* user=NULL );
///@}
//////////////////////////////////////////////////////////////////////////////////
/// @name Mouse + Keyboard State
//////////////////////////////////////////////////////////////////////////////////
///@{
/**
* Get the mouse position.
*/
inline const int* GetMousePosition() const { return mMousePos; }
/**
* Get the mouse position.
*/
inline void GetMousePosition( int* x, int* y ) const { if(x) *x = mMousePos[0]; if(y) *y = mMousePos[1]; }
/**
* Get the mouse button state.
*
* @param button the button number, starting with 1.
* In X11, the left mouse button is 1.
* Here are the mouse button numbers:
*
* - 1 MOUSE_LEFT (left button)
* - 2 MOUSE_MIDDLE (middle button / scroll wheel button)
* - 3 MOUSE_RIGHT (right button)
* - 4 MOUSE_WHEEL_UP (scroll wheel up)
* - 5 MOUSE_WHEEL_DOWN (scroll wheel down)
*
* @returns true if the button is pressed, otherwise false
*/
inline bool GetMouseButton( uint32_t button ) const { if(button > sizeof(mMouseButtons)) return false; return mMouseButtons[button]; }
/**
* Get the state of a key (lowercase, without modifiers applied)
*
* Similar to glEvent::KEY_STATE, GetKey() queries the raw key state
* without being translated by modifier keys such as Shift, CapsLock,
* NumLock, ect. Alphanumeric keys will be left as lowercase, so
* query lowercase keys - uppercase keys will always return false.
*
* @param key the `XK_` key symbol (see `/usr/include/X11/keysymdef.h`)
*
* Uppercase keys like XK_A or XK_plus will always return false.
* Instead, query the lowercase keys such as XK_a, XK_1, ect.
*
* Other keys like F1-F12, shift, tab, ctrl/alt, arrow keys,
* backspace, delete, escape, and enter can all be queried.
*
* GetKey() caches the first 1024 key symbols. Other keys will
* return false, but can be subscribed to through a glEventHander.
*
* @returns true if the key is pressed, otherwise false
*/
inline bool GetKey( uint32_t key ) const { const uint32_t idx = key - KEY_OFFSET; if(idx > sizeof(mKeyStates)) return false; return mKeyStates[idx]; }
///@}
//////////////////////////////////////////////////////////////////////////////////
/// @name Mouse Cursor
//////////////////////////////////////////////////////////////////////////////////
///@{
/**
* Set the active mouse cursor.
*
* @param cursor one of the cursor ID's from `X11/cursorfont.h`
*
* @see ResetCursor() to restore the cursor back to default
* @see SetDefaultCursor() to change the default cursor
*/
void SetCursor( uint32_t cursor );
/**
* Set the default mouse cursor that gets used by ResetCursor()
*
* @param cursor one of the cursor ID's from `X11/cursorfont.h`.
* This will be the default cursor that gets set
* when ResetCursor() is called.
*
* @param activate if `true` (default), the active cursor will be
* changed to this new default cursor immediately.
* If `false`, the active cursor will stay the same
* until ResetCursor() gets called in the future.
*
* @see ResetCursor()
* @see ResetDefaultCursor()
* @see SetCursor()
*/
void SetDefaultCursor( uint32_t cursor, bool activate=true );
/**
* Reset the mouse cursor back to it's default.
* The default mouse cursor is defined by SetDefaultCursor().
*
* @see SetDefaultCursor()
* @see SetCursor()
*/
void ResetCursor();
/**
* Reset the default cursor back to it's original, the arrow.
*
* @param activate if `true` (default), the active cursor will be
* changed to this new default cursor immediately.
* If `false`, the active cursor will stay the same
* until ResetCursor() gets called in the future.
*
* @see SetDefaultCursor()
* @see SetCursor()
* @see ResetCursor()
*/
void ResetDefaultCursor( bool activate=true );
///@}
//////////////////////////////////////////////////////////////////////////////////
/// @name Mouse Dragging
//////////////////////////////////////////////////////////////////////////////////
///@{
/**
* Drag behavior enum
*
* @see GetDragMode()
* @see SetDragMode()
*/
enum DragMode
{
DragDefault, /**< Issue MOUSE_DRAG events, but nothing else. */
DragDisabled, /**< Disable issuing all MOUSE_DRAG events */
DragSelect, /**< Select widgets from a dragging rectangle, and issue WIDGET_SELECTED events */
DragCreate /**< Create a new widget from a dragging rectangle, and issue WIDGET_CREATED events */
};
/**
* Get the dragging behavior mode.
*/
inline DragMode GetDragMode() const { return mDragMode; }
/**
* Set the dragging behavior mode.
*/
inline void SetDragMode( DragMode mode ) { mDragMode = mode; }
/**
* Is the mouse currently dragging in the specified mode?
*/
inline bool IsDragging( DragMode mode=DragDefault ) const { return mode == DragCreate ? (mMouseDragOrigin[0] >= 0 && mMouseDragOrigin[1] >= 0) : (mMouseDrag[0] >= 0 && mMouseDrag[1] >= 0); }
/**
* Get the current dragging rectangle, or return false if not dragging.
*/
bool GetDragRect( int* x, int* y, int* width, int* height );
/**
* Get the current dragging coordinates, or return false if not dragging.
*/
bool GetDragCoords( int* x1, int* y1, int* x2, int* y2 );
///@}
//////////////////////////////////////////////////////////////////////////////////
/// @name Widgets
//////////////////////////////////////////////////////////////////////////////////
///@{
/**
* Add a widget to the window that recieves events and is rendered.
*/
glWidget* AddWidget( glWidget* widget );
/**
* Remove a widget from the window (and optionally delete it)
*/
void RemoveWidget( glWidget* widget, bool deleteWidget=true );
/**
* Remove a widget from the window (and optionally delete it)
*/
void RemoveWidget( uint32_t index, bool deleteWidget=true );
/**
* Remove all widgets from the window (and optionally delete them)
*/
void RemoveAllWidgets( bool deleteWidgets=true );
/**
* Retrieve the number of widgets.
*/
inline uint32_t GetNumWidgets() const { return mWidgets.size(); }
/**
* Retrieve a widget.
*/
inline glWidget* GetWidget( const uint32_t index ) const { return mWidgets[index]; }
/**
* Retrieve the index of a widget (or -1 if not found)
*/
int GetWidgetIndex( const glWidget* widget ) const;
/**
* Find first widget by coordinate, or NULL if no widget overlaps with that coordinate.
*/
glWidget* FindWidget( int x, int y );
/**
* Find all widgets by coordinate, or NULL if no widget overlaps with that coordinate.
*/
std::vector<glWidget*> FindWidgets( int x, int y );
///@}
protected:
glDisplay( const videoOptions& options );
bool initWindow();
bool initGL();
glTexture* allocTexture( uint32_t width, uint32_t height, imageFormat format );
void activateViewport();
void dispatchEvent( uint16_t msg, int a, int b );
static bool onEvent( uint16_t msg, int a, int b, void* user );
struct eventHandler
{
glEventHandler callback;
void* user;
};
static const int screenIdx = 0;
Display* mDisplayX;
Screen* mScreenX;
XVisualInfo* mVisualX;
Window mWindowX;
GLXContext mContextGL;
Cursor mCursors[256];
int mActiveCursor;
int mDefaultCursor;
bool mInitialShow;
bool mRendering;
bool mResizedToFeed;
Atom mWindowClosedMsg;
DragMode mDragMode;
uint32_t mID;
uint32_t mScreenWidth;
uint32_t mScreenHeight;
timespec mLastTime;
float mAvgTime;
float mBgColor[4];
int mViewport[4];
int mMousePos[2];
int mMouseDrag[2];
int mMouseDragOrigin[2];
bool mMouseButtons[16];
bool mKeyStates[1024];
float* mNormalizedCUDA;
uint32_t mNormalizedWidth;
uint32_t mNormalizedHeight;
std::vector<glWidget*> mWidgets;
std::vector<glTexture*> mTextures;
std::vector<eventHandler> mEventHandlers;
};
/**
* Retrieve a display window object
* @ingroup OpenGL
*/
glDisplay* glGetDisplay( uint32_t display=0 );
/**
* Return the number of created glDisplay windows
* @ingroup OpenGL
*/
uint32_t glGetNumDisplays();
#endif