Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Scene2D/Internals/OpenGLTextProgram.cpp @ 1615:f5d4bd7b5593
new class: OsiriXLayerFactory
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 30 Oct 2020 17:26:44 +0100 |
parents | 8563ea5d8ae4 |
children | 59f95b9ea858 |
rev | line source |
---|---|
592 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
1270
2d8ab34c8c91
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
956
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
592 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public License |
592 | 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 | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
15 * Lesser General Public License for more details. |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1576
diff
changeset
|
16 * |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
18 * License along with this program. If not, see |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
19 * <http://www.gnu.org/licenses/>. |
592 | 20 **/ |
21 | |
22 | |
23 #include "OpenGLTextProgram.h" | |
611
e3f21a265be5
Added version directive to GLSL shader code + glew init function in sample code
Benjamin Golinvaux <bgo@osimis.io>
parents:
592
diff
changeset
|
24 #include "OpenGLShaderVersionDirective.h" |
592 | 25 |
26 #include "../../Fonts/OpenGLTextCoordinates.h" | |
27 | |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
28 #include <OrthancException.h> |
592 | 29 |
30 | |
31 static const unsigned int COMPONENTS = 2; | |
32 | |
33 static const char* VERTEX_SHADER = | |
611
e3f21a265be5
Added version directive to GLSL shader code + glew init function in sample code
Benjamin Golinvaux <bgo@osimis.io>
parents:
592
diff
changeset
|
34 ORTHANC_STONE_OPENGL_SHADER_VERSION_DIRECTIVE |
592 | 35 "attribute vec2 a_texcoord; \n" |
36 "attribute vec4 a_position; \n" | |
37 "uniform mat4 u_matrix; \n" | |
38 "varying vec2 v_texcoord; \n" | |
39 "void main() \n" | |
40 "{ \n" | |
41 " gl_Position = u_matrix * a_position; \n" | |
42 " v_texcoord = a_texcoord; \n" | |
43 "}"; | |
44 | |
45 static const char* FRAGMENT_SHADER = | |
611
e3f21a265be5
Added version directive to GLSL shader code + glew init function in sample code
Benjamin Golinvaux <bgo@osimis.io>
parents:
592
diff
changeset
|
46 ORTHANC_STONE_OPENGL_SHADER_VERSION_DIRECTIVE |
592 | 47 "uniform sampler2D u_texture; \n" |
48 "uniform vec3 u_color; \n" | |
49 "varying vec2 v_texcoord; \n" | |
50 "void main() \n" | |
51 "{ \n" | |
52 " vec4 v = texture2D(u_texture, v_texcoord); \n" | |
53 " gl_FragColor = vec4(u_color * v.w, v.w); \n" // Premultiplied alpha | |
54 "}"; | |
55 | |
56 | |
57 namespace OrthancStone | |
58 { | |
59 namespace Internals | |
60 { | |
61 OpenGLTextProgram::OpenGLTextProgram(OpenGL::IOpenGLContext& context) : | |
62 context_(context) | |
63 { | |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
64 if (!context_.IsContextLost()) |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
65 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
66 context_.MakeCurrent(); |
592 | 67 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
68 program_.reset(new OpenGL::OpenGLProgram(context_)); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
69 program_->CompileShaders(VERTEX_SHADER, FRAGMENT_SHADER); |
592 | 70 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
71 positionLocation_ = program_->GetAttributeLocation("a_position"); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
72 textureLocation_ = program_->GetAttributeLocation("a_texcoord"); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
73 } |
592 | 74 } |
75 | |
76 | |
77 OpenGLTextProgram::Data::Data(OpenGL::IOpenGLContext& context, | |
78 const GlyphTextureAlphabet& alphabet, | |
79 const TextSceneLayer& layer) : | |
80 context_(context), | |
804
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
81 red_(layer.GetColor().GetRedAsFloat()), |
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
82 green_(layer.GetColor().GetGreenAsFloat()), |
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
83 blue_(layer.GetColor().GetBlueAsFloat()), |
592 | 84 x_(layer.GetX()), |
85 y_(layer.GetY()), | |
86 border_(layer.GetBorder()), | |
87 anchor_(layer.GetAnchor()) | |
88 { | |
89 OpenGL::OpenGLTextCoordinates coordinates(alphabet, layer.GetText()); | |
90 textWidth_ = coordinates.GetTextWidth(); | |
91 textHeight_ = coordinates.GetTextHeight(); | |
92 | |
93 if (coordinates.IsEmpty()) | |
94 { | |
95 coordinatesCount_ = 0; | |
96 } | |
97 else | |
98 { | |
99 coordinatesCount_ = coordinates.GetRenderingCoords().size(); | |
100 | |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
101 if (!context_.IsContextLost()) |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
102 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
103 context_.MakeCurrent(); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
104 glGenBuffers(2, buffers_); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
105 ORTHANC_OPENGL_CHECK("glGenBuffers"); |
592 | 106 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
107 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
108 ORTHANC_OPENGL_CHECK("glBindBuffer"); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
109 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * coordinatesCount_, |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
110 &coordinates.GetRenderingCoords()[0], GL_STATIC_DRAW); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
111 ORTHANC_OPENGL_CHECK("glBufferData"); |
592 | 112 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
113 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
114 ORTHANC_OPENGL_CHECK("glBindBuffer"); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
115 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * coordinatesCount_, |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
116 &coordinates.GetTextureCoords()[0], GL_STATIC_DRAW); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
117 ORTHANC_OPENGL_CHECK("glBufferData"); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
118 } |
592 | 119 } |
120 } | |
121 | |
122 | |
123 OpenGLTextProgram::Data::~Data() | |
124 { | |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
125 if (!context_.IsContextLost() && !IsEmpty()) |
592 | 126 { |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
127 try |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
128 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
129 context_.MakeCurrent(); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
130 ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glDeleteBuffers"); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
131 glDeleteBuffers(2, buffers_); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
132 ORTHANC_OPENGL_CHECK("glDeleteBuffers"); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
133 } |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
134 catch (const Orthanc::OrthancException& e) |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
135 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
136 if (e.HasDetails()) |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
137 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
138 LOG(ERROR) << "OrthancException in ~Data: " << e.What() << " Details: " << e.GetDetails(); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
139 } |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
140 else |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
141 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
142 LOG(ERROR) << "OrthancException in ~Data: " << e.What(); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
143 } |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
144 } |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
145 catch (const std::exception& e) |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
146 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
147 LOG(ERROR) << "std::exception in ~Data: " << e.what(); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
148 } |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
149 catch (...) |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
150 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
151 LOG(ERROR) << "Unknown exception in ~Data"; |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
152 } |
592 | 153 } |
154 } | |
155 | |
156 | |
157 GLuint OpenGLTextProgram::Data::GetSceneLocationsBuffer() const | |
158 { | |
159 if (IsEmpty()) | |
160 { | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
947
diff
changeset
|
161 LOG(ERROR) << "OpenGLTextProgram::Data::GetSceneLocationsBuffer(): (IsEmpty())"; |
592 | 162 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
163 } | |
164 else | |
165 { | |
166 return buffers_[0]; | |
167 } | |
168 } | |
169 | |
170 GLuint OpenGLTextProgram::Data::GetTextureLocationsBuffer() const | |
171 { | |
172 if (IsEmpty()) | |
173 { | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
947
diff
changeset
|
174 LOG(ERROR) << "OpenGLTextProgram::Data::GetTextureLocationsBuffer(): (IsEmpty())"; |
592 | 175 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
176 } | |
177 else | |
178 { | |
179 return buffers_[1]; | |
180 } | |
181 } | |
182 | |
183 void OpenGLTextProgram::Apply(OpenGL::OpenGLTexture& fontTexture, | |
184 const Data& data, | |
1576
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
185 const AffineTransform2D& transform, |
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
186 unsigned int canvasWidth, |
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
187 unsigned int canvasHeight) |
592 | 188 { |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
189 if (!context_.IsContextLost() && !data.IsEmpty()) |
592 | 190 { |
191 context_.MakeCurrent(); | |
192 program_->Use(); | |
193 | |
194 double dx, dy; // In pixels | |
195 ComputeAnchorTranslation(dx, dy, data.GetAnchor(), | |
693
9a474e90e832
Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
196 data.GetTextWidth(), data.GetTextHeight(), |
9a474e90e832
Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
197 static_cast<unsigned int>(data.GetBorder())); |
592 | 198 |
199 double x = data.GetX(); | |
200 double y = data.GetY(); | |
201 transform.Apply(x, y); | |
202 | |
203 const AffineTransform2D t = AffineTransform2D::CreateOffset(x + dx, y + dy); | |
204 | |
205 float m[16]; | |
1576
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
206 t.ConvertToOpenGLMatrix(m, canvasWidth, canvasHeight); |
592 | 207 |
208 fontTexture.Bind(program_->GetUniformLocation("u_texture")); | |
209 glUniformMatrix4fv(program_->GetUniformLocation("u_matrix"), 1, GL_FALSE, m); | |
210 glUniform3f(program_->GetUniformLocation("u_color"), | |
211 data.GetRed(), data.GetGreen(), data.GetBlue()); | |
212 | |
213 glBindBuffer(GL_ARRAY_BUFFER, data.GetSceneLocationsBuffer()); | |
214 glEnableVertexAttribArray(positionLocation_); | |
215 glVertexAttribPointer(positionLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0); | |
216 | |
217 glBindBuffer(GL_ARRAY_BUFFER, data.GetTextureLocationsBuffer()); | |
218 glEnableVertexAttribArray(textureLocation_); | |
219 glVertexAttribPointer(textureLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0); | |
220 | |
221 glEnable(GL_BLEND); | |
222 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); | |
693
9a474e90e832
Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
223 glDrawArrays(GL_TRIANGLES, 0, |
9a474e90e832
Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
224 static_cast<GLsizei>(data.GetCoordinatesCount() / COMPONENTS)); |
592 | 225 glDisable(GL_BLEND); |
226 | |
227 glDisableVertexAttribArray(positionLocation_); | |
228 glDisableVertexAttribArray(textureLocation_); | |
229 } | |
230 } | |
231 } | |
232 } |