Mercurial > hg > orthanc-stone
annotate Framework/Scene2D/OpenGLCompositor.cpp @ 819:a68cd7ae8838
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 29 May 2019 13:39:31 +0200 |
parents | 7efa2543699d |
children | 266e2b0b9abc |
rev | line source |
---|---|
594 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "OpenGLCompositor.h" | |
23 | |
24 #include "Internals/OpenGLAdvancedPolylineRenderer.h" | |
25 #include "Internals/OpenGLBasicPolylineRenderer.h" | |
26 #include "Internals/OpenGLColorTextureRenderer.h" | |
27 #include "Internals/OpenGLFloatTextureRenderer.h" | |
28 #include "Internals/OpenGLInfoPanelRenderer.h" | |
29 #include "Internals/OpenGLTextRenderer.h" | |
30 | |
31 namespace OrthancStone | |
32 { | |
33 class OpenGLCompositor::Font : public boost::noncopyable | |
34 { | |
35 private: | |
36 std::auto_ptr<GlyphTextureAlphabet> alphabet_; | |
37 std::auto_ptr<OpenGL::OpenGLTexture> texture_; | |
38 | |
39 public: | |
40 Font(const GlyphBitmapAlphabet& dict) | |
41 { | |
42 alphabet_.reset(new GlyphTextureAlphabet(dict)); | |
43 texture_.reset(new OpenGL::OpenGLTexture); | |
44 | |
45 std::auto_ptr<Orthanc::ImageAccessor> bitmap(alphabet_->ReleaseTexture()); | |
46 texture_->Load(*bitmap, true /* enable linear interpolation */); | |
47 } | |
48 | |
49 OpenGL::OpenGLTexture& GetTexture() const | |
50 { | |
51 assert(texture_.get() != NULL); | |
52 return *texture_; | |
53 } | |
54 | |
55 const GlyphTextureAlphabet& GetAlphabet() const | |
56 { | |
57 assert(alphabet_.get() != NULL); | |
58 return *alphabet_; | |
59 } | |
60 }; | |
61 | |
62 | |
63 const OpenGLCompositor::Font* OpenGLCompositor::GetFont(size_t fontIndex) const | |
64 { | |
65 Fonts::const_iterator found = fonts_.find(fontIndex); | |
66 | |
67 if (found == fonts_.end()) | |
68 { | |
69 return NULL; // Unknown font, nothing should be drawn | |
70 } | |
71 else | |
72 { | |
73 assert(found->second != NULL); | |
74 return found->second; | |
75 } | |
76 } | |
77 | |
78 | |
79 Internals::CompositorHelper::ILayerRenderer* OpenGLCompositor::Create(const ISceneLayer& layer) | |
80 { | |
81 switch (layer.GetType()) | |
82 { | |
83 case ISceneLayer::Type_InfoPanel: | |
84 return new Internals::OpenGLInfoPanelRenderer | |
85 (context_, colorTextureProgram_, dynamic_cast<const InfoPanelSceneLayer&>(layer)); | |
86 | |
87 case ISceneLayer::Type_ColorTexture: | |
88 return new Internals::OpenGLColorTextureRenderer | |
89 (context_, colorTextureProgram_, dynamic_cast<const ColorTextureSceneLayer&>(layer)); | |
90 | |
91 case ISceneLayer::Type_FloatTexture: | |
92 return new Internals::OpenGLFloatTextureRenderer | |
93 (context_, floatTextureProgram_, dynamic_cast<const FloatTextureSceneLayer&>(layer)); | |
94 | |
95 case ISceneLayer::Type_Polyline: | |
96 return new Internals::OpenGLAdvancedPolylineRenderer | |
97 (context_, linesProgram_, dynamic_cast<const PolylineSceneLayer&>(layer)); | |
98 //return new Internals::OpenGLBasicPolylineRenderer(context_, dynamic_cast<const PolylineSceneLayer&>(layer)); | |
99 | |
100 case ISceneLayer::Type_Text: | |
101 { | |
102 const TextSceneLayer& l = dynamic_cast<const TextSceneLayer&>(layer); | |
103 const Font* font = GetFont(l.GetFontIndex()); | |
104 if (font == NULL) | |
105 { | |
106 return NULL; | |
107 } | |
108 else | |
109 { | |
110 return new Internals::OpenGLTextRenderer | |
111 (context_, textProgram_, font->GetAlphabet(), font->GetTexture(), l); | |
112 } | |
113 } | |
114 | |
115 default: | |
116 return NULL; | |
117 } | |
118 } | |
119 | |
120 | |
121 OpenGLCompositor::OpenGLCompositor(OpenGL::IOpenGLContext& context, | |
600
6129b1e5ba42
BasicScene SDL sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
598
diff
changeset
|
122 const Scene2D& scene) : |
594 | 123 context_(context), |
124 helper_(scene, *this), | |
125 colorTextureProgram_(context), | |
126 floatTextureProgram_(context), | |
127 linesProgram_(context), | |
128 textProgram_(context), | |
129 canvasWidth_(0), | |
130 canvasHeight_(0) | |
131 { | |
132 UpdateSize(); | |
133 } | |
134 | |
135 | |
136 OpenGLCompositor::~OpenGLCompositor() | |
137 { | |
138 for (Fonts::iterator it = fonts_.begin(); it != fonts_.end(); ++it) | |
139 { | |
140 assert(it->second != NULL); | |
141 delete it->second; | |
142 } | |
143 } | |
144 | |
145 | |
146 void OpenGLCompositor::UpdateSize() | |
147 { | |
148 canvasWidth_ = context_.GetCanvasWidth(); | |
149 canvasHeight_ = context_.GetCanvasHeight(); | |
150 | |
151 context_.MakeCurrent(); | |
152 glViewport(0, 0, canvasWidth_, canvasHeight_); | |
153 } | |
154 | |
155 | |
156 void OpenGLCompositor::Refresh() | |
157 { | |
158 context_.MakeCurrent(); | |
159 | |
160 glClearColor(0, 0, 0, 1); | |
161 glClear(GL_COLOR_BUFFER_BIT); | |
162 | |
163 helper_.Refresh(canvasWidth_, canvasHeight_); | |
164 | |
165 context_.SwapBuffer(); | |
166 } | |
167 | |
168 | |
169 void OpenGLCompositor::SetFont(size_t index, | |
170 const GlyphBitmapAlphabet& dict) | |
171 { | |
172 std::auto_ptr<Font> font(new Font(dict)); | |
173 | |
174 Fonts::iterator found = fonts_.find(index); | |
175 | |
176 if (found == fonts_.end()) | |
177 { | |
178 fonts_[index] = font.release(); | |
179 } | |
180 else | |
181 { | |
182 assert(found->second != NULL); | |
183 delete found->second; | |
184 | |
185 found->second = font.release(); | |
186 } | |
187 } | |
188 | |
189 | |
190 #if ORTHANC_ENABLE_LOCALE == 1 | |
191 void OpenGLCompositor::SetFont(size_t index, | |
192 Orthanc::EmbeddedResources::FileResourceId resource, | |
193 unsigned int fontSize, | |
194 Orthanc::Encoding codepage) | |
195 { | |
196 FontRenderer renderer; | |
598 | 197 renderer.LoadFont(resource, fontSize); |
594 | 198 |
199 GlyphBitmapAlphabet dict; | |
200 dict.LoadCodepage(renderer, codepage); | |
201 | |
202 SetFont(index, dict); | |
203 } | |
204 #endif | |
617 | 205 |
206 | |
207 ScenePoint2D OpenGLCompositor::GetPixelCenterCoordinates(int x, int y) const | |
208 { | |
209 return ScenePoint2D( | |
210 static_cast<double>(x) + 0.5 - static_cast<double>(canvasWidth_) / 2.0, | |
211 static_cast<double>(y) + 0.5 - static_cast<double>(canvasHeight_) / 2.0); | |
212 } | |
594 | 213 } |