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
format
filament::Texture::InternalFormat format
Definition: FilamentResourceManager.cpp:181
colmap::RunThreadWithOpenGLContext
void RunThreadWithOpenGLContext(Thread *thread)
Definition: opengl_utils.h:81
colmap::JoinPaths
std::string JoinPaths(T const &... paths)
Definition: misc.h:128
colmap::HasFileExtension
bool HasFileExtension(const std::string &file_name, const std::string &ext)
Definition: misc.cc:51
colmap::Thread
Definition: threading.h:74
testing.h
colmap::OpenGLContextManager
Definition: opengl_utils.h:73
colmap::OpenGLContextManager::HasOpenGL
static bool HasOpenGL()
Definition: opengl_utils.h:78
context
ImGuiContext * context
Definition: Window.cpp:76
misc.h
colmap::RemoveCommandLineArgument
void RemoveCommandLineArgument(const std::string &arg, int *argc, char **argv)
Definition: misc.cc:327
colmap::GetPathBaseName
std::string GetPathBaseName(const std::string &path)
Definition: misc.cc:118
colmap::SplitFileExtension
void SplitFileExtension(const std::string &path, std::string *root, std::string *ext)
Definition: misc.cc:64
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(TestEnsureTrailingSlash)
Definition: misc_test.cc:39
threading.h
colmap::GetParentDir
std::string GetParentDir(const std::string &path)
Definition: misc.cc:128
colmap::EnsureTrailingSlash
std::string EnsureTrailingSlash(const std::string &str)
Definition: misc.cc:40
logging.h
colmap::OpenGLContextManager::OpenGLContextManager
OpenGLContextManager(int opengl_major_version=2, int opengl_minor_version=1)
Definition: opengl_utils.h:75
colmap::OpenGLContextManager::MakeCurrent
void MakeCurrent()
Definition: opengl_utils.h:77
colmap
Definition: AutomaticReconstructionController.h:17
colmap::GLError
void GLError(const char *file, const int line)
Definition: opengl_utils.h:83
opengl_utils.h