ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
opengl_utils.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #ifdef GUI_ENABLED
11 #include <QAction>
12 #include <QApplication>
13 #include <QOffscreenSurface>
14 #include <QOpenGLContext>
15 #include <QThread>
16 #include <QWaitCondition>
17 #endif
18 
19 #include "util/threading.h"
20 
21 namespace colmap {
22 
23 #ifdef DEBUG
24 #define glDebugLog() glError(__FILE__, __LINE__)
25 #else
26 #define glDebugLog()
27 #endif
28 
29 #ifdef GUI_ENABLED
30 
31 // This class manages a thread-safe OpenGL context. Note that this class must be
32 // instantiated in the main Qt thread, since an OpenGL context must be created
33 // in it. The context can then be made current in any other thread.
34 class OpenGLContextManager : public QObject {
35 public:
36  OpenGLContextManager(int opengl_major_version = 2,
37  int opengl_minor_version = 1);
38 
39  // Make the OpenGL context available by moving it from the thread where it
40  // was created to the current thread and making it current.
41  void MakeCurrent();
42 
43  // Check whether the machine has OpenGL and we can create the context.
44  static bool HasOpenGL();
45 
46 private:
47  QOffscreenSurface surface_;
48  QOpenGLContext context_;
49  QThread* parent_thread_;
50  QThread* current_thread_;
51  QAction* make_current_action_;
52 };
53 
54 // Run and wait for the thread, that uses the OpenGLContextManager, e.g.:
55 //
56 // class TestThread : public Thread {
57 // private:
58 // void Run() { opengl_context_.MakeCurrent(); }
59 // OpenGLContextManager opengl_context_;
60 // };
61 // QApplication app(argc, argv);
62 // TestThread thread;
63 // RunThreadWithOpenGLContext(&thread);
64 //
65 void RunThreadWithOpenGLContext(Thread* thread);
66 
67 // Get the OpenGL errors and print them to stderr.
68 void GLError(const char* file, const int line);
69 
70 #else
71 
72 // Dummy implementation when GUI support is disabled
74 public:
75  OpenGLContextManager(int opengl_major_version = 2,
76  int opengl_minor_version = 1) {}
77  inline void MakeCurrent() {}
78  inline static bool HasOpenGL() { return false; }
79 };
80 
81 inline void RunThreadWithOpenGLContext(Thread* thread) {}
82 
83 inline void GLError(const char* file, const int line) {}
84 
85 #endif
86 
87 } // namespace colmap
OpenGLContextManager(int opengl_major_version=2, int opengl_minor_version=1)
Definition: opengl_utils.h:75
void GLError(const char *file, const int line)
Definition: opengl_utils.h:83
void RunThreadWithOpenGLContext(Thread *thread)
Definition: opengl_utils.h:81