annotate Framework/OpenGL/SdlOpenGLContext.cpp @ 1047:efc5b62b9539

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 09 Oct 2019 18:06:58 +0200
parents Applications/Sdl/SdlOpenGLContext.cpp@6a9300ecfa13
children bc27c199eefe 2d8ab34c8c91
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 {
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
71 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
72 "Cannot initialize glew");
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
73 }
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
74
614
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 613
diff changeset
75 isGlewInitialized_ = true;
613
412a2d01a189 automatic initialization of glew
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 595
diff changeset
76 }
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 #endif
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 }
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
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
82 SdlOpenGLContext::~SdlOpenGLContext()
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 SDL_GL_DeleteContext(context_);
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
87 void SdlOpenGLContext::MakeCurrent()
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 {
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 if (SDL_GL_MakeCurrent(window_.GetObject(), context_) != 0)
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 891
diff changeset
91 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
92 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
93 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
94 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
95 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, errStr.c_str());
595
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 891
diff changeset
98 // 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
99 SDL_GL_SetSwapInterval(1);
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
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
103 void SdlOpenGLContext::SwapBuffer()
595
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 // Swap our buffer to display the current contents of buffer on screen
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 SDL_GL_SwapWindow(window_.GetObject());
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 }
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
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
110 unsigned int SdlOpenGLContext::GetCanvasWidth() const
595
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 int w = 0;
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 SDL_GL_GetDrawableSize(window_.GetObject(), &w, NULL);
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 return static_cast<unsigned int>(w);
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
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
118 unsigned int SdlOpenGLContext::GetCanvasHeight() const
595
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 int h = 0;
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 SDL_GL_GetDrawableSize(window_.GetObject(), NULL, &h);
6e471e6cf09b CairoPolylineRenderer, SdlOpenGLWindow
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 return static_cast<unsigned int>(h);
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 }
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 #endif