annotate Framework/OpenGL/SdlOpenGLContext.cpp @ 1167:ad4e21df4e40 broker

enriching OrthancStone::StoneInitialize()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 20 Nov 2019 10:55:44 +0100
parents bc27c199eefe
children 0ca50d275b9a
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
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
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.
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
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"
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 891
diff changeset
23 #include "../../Framework/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
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include <Core/OrthancException.h>
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 namespace OrthancStone
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 {
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
39 SdlOpenGLContext::SdlOpenGLContext(const char* title,
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
40 unsigned int width,
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
41 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
42 bool allowDpiScaling)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 891
diff changeset
43 : 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
44 , context_(NULL)
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 {
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 context_ = SDL_GL_CreateContext(window_.GetObject());
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 if (context_ == NULL)
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 {
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 "Cannot initialize OpenGL");
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 }
613
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
53
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
54 #if ORTHANC_ENABLE_GLEW == 1
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
55 // The initialization function of glew (i.e. "glewInit()") can
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
56 // only be called once an OpenGL is setup.
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
57 // https://stackoverflow.com/a/45033669/881731
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
58 {
614
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 613
diff changeset
59 static boost::mutex mutex_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 613
diff changeset
60 static bool isGlewInitialized_ = false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 613
diff changeset
61
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 613
diff changeset
62 boost::mutex::scoped_lock lock(mutex_);
613
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
63
614
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 613
diff changeset
64 if (!isGlewInitialized_)
613
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
65 {
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
66 LOG(INFO) << "Initializing glew";
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
67
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
68 GLenum err = glewInit();
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
69 if (GLEW_OK != err)
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
70 {
1147
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1047
diff changeset
71 LOG(ERROR) << glewGetErrorString(err);
613
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
72 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
73 "Cannot initialize glew");
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
74 }
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
75
614
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 613
diff changeset
76 isGlewInitialized_ = true;
613
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 }
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
79 #endif
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
83 SdlOpenGLContext::~SdlOpenGLContext()
595
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 SDL_GL_DeleteContext(context_);
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
88 void SdlOpenGLContext::MakeCurrent()
595
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 if (SDL_GL_MakeCurrent(window_.GetObject(), context_) != 0)
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 891
diff changeset
92 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
93 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
94 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
95 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
96 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, errStr.c_str());
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 891
diff changeset
99 // 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
100 SDL_GL_SetSwapInterval(1);
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
104 void SdlOpenGLContext::SwapBuffer()
595
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 // Swap our buffer to display the current contents of buffer on screen
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 SDL_GL_SwapWindow(window_.GetObject());
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
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
111 unsigned int SdlOpenGLContext::GetCanvasWidth() const
595
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 int w = 0;
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 SDL_GL_GetDrawableSize(window_.GetObject(), &w, NULL);
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 return static_cast<unsigned int>(w);
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
119 unsigned int SdlOpenGLContext::GetCanvasHeight() const
595
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 int h = 0;
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 SDL_GL_GetDrawableSize(window_.GetObject(), NULL, &h);
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 return static_cast<unsigned int>(h);
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 #endif