annotate Applications/Platforms/Sdl/SdlOpenGLContext.cpp @ 1814:53f3411bf94b

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 25 May 2021 15:52:38 +0200
parents 9ac2a65d4172
children 3889ae96d2e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
1739
9ac2a65d4172 upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1626
diff changeset
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
1596
4fb8fdf03314 removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1591
diff changeset
16 *
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
22 #include "SdlOpenGLContext.h"
1591
5887a4f8594b moving platform-specific files out of the "OrthancStone" folder
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
23 #include "../../../OrthancStone/Sources/StoneException.h"
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #if ORTHANC_ENABLE_SDL == 1
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
613
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
27 #if !defined(ORTHANC_ENABLE_GLEW)
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
28 # error Macro ORTHANC_ENABLE_GLEW must be defined
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
29 #endif
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
30
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
31 #if ORTHANC_ENABLE_GLEW == 1
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
32 # include <GL/glew.h>
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
33 #endif
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
34
1626
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
35 #include <Logging.h>
1455
30deba7bc8e2 simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1279
diff changeset
36 #include <OrthancException.h>
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
1626
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
38 #include <boost/thread/mutex.hpp>
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
39
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 namespace OrthancStone
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 {
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
42 SdlOpenGLContext::SdlOpenGLContext(const char* title,
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
43 unsigned int width,
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
44 unsigned int height,
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 891
diff changeset
45 bool allowDpiScaling)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 891
diff changeset
46 : window_(title, width, height, true /* enable OpenGL */, allowDpiScaling)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 891
diff changeset
47 , context_(NULL)
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 context_ = SDL_GL_CreateContext(window_.GetObject());
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 if (context_ == NULL)
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 {
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 "Cannot initialize OpenGL");
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 }
613
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
56
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
57 #if ORTHANC_ENABLE_GLEW == 1
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
58 // The initialization function of glew (i.e. "glewInit()") can
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
59 // only be called once an OpenGL is setup.
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
60 // https://stackoverflow.com/a/45033669/881731
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
61 {
614
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 613
diff changeset
62 static boost::mutex mutex_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 613
diff changeset
63 static bool isGlewInitialized_ = false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 613
diff changeset
64
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 613
diff changeset
65 boost::mutex::scoped_lock lock(mutex_);
613
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
66
614
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 613
diff changeset
67 if (!isGlewInitialized_)
613
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
68 {
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
69 LOG(INFO) << "Initializing glew";
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
70
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
71 GLenum err = glewInit();
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
72 if (GLEW_OK != err)
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
73 {
1147
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1047
diff changeset
74 LOG(ERROR) << glewGetErrorString(err);
613
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
75 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
76 "Cannot initialize glew");
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
77 }
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
78
614
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 613
diff changeset
79 isGlewInitialized_ = true;
613
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
80 }
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
81 }
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
82 #endif
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
86 SdlOpenGLContext::~SdlOpenGLContext()
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 {
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 SDL_GL_DeleteContext(context_);
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
91 void SdlOpenGLContext::MakeCurrent()
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 {
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 if (SDL_GL_MakeCurrent(window_.GetObject(), context_) != 0)
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 891
diff changeset
95 const char* errText = SDL_GetError();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 891
diff changeset
96 std::stringstream ss;
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 891
diff changeset
97 ss << "Cannot set current OpenGL context. SDL error text: " << errText;
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 891
diff changeset
98 std::string errStr = ss.str();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 891
diff changeset
99 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, errStr.c_str());
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 891
diff changeset
102 // This makes our buffer swap synchronized with the monitor's vertical refresh
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 SDL_GL_SetSwapInterval(1);
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
107 void SdlOpenGLContext::SwapBuffer()
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 {
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 // Swap our buffer to display the current contents of buffer on screen
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 SDL_GL_SwapWindow(window_.GetObject());
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
114 unsigned int SdlOpenGLContext::GetCanvasWidth() const
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 {
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 int w = 0;
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 SDL_GL_GetDrawableSize(window_.GetObject(), &w, NULL);
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 return static_cast<unsigned int>(w);
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
122 unsigned int SdlOpenGLContext::GetCanvasHeight() const
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 {
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 int h = 0;
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 SDL_GL_GetDrawableSize(window_.GetObject(), NULL, &h);
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 return static_cast<unsigned int>(h);
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 #endif