Mercurial > hg > orthanc-stone
annotate Framework/Scene2D/Internals/OpenGLTextProgram.cpp @ 1014:86a18d201e27
Added tag toa2019092301 for changeset 53cc787bd7bc
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Mon, 23 Sep 2019 15:19:04 +0200 |
parents | a7351ad54960 |
children | 2d8ab34c8c91 |
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 | |
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 "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
|
23 #include "OpenGLShaderVersionDirective.h" |
592 | 24 |
25 #include "../../Fonts/OpenGLTextCoordinates.h" | |
26 | |
27 #include <Core/OrthancException.h> | |
28 | |
29 | |
30 static const unsigned int COMPONENTS = 2; | |
31 | |
32 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
|
33 ORTHANC_STONE_OPENGL_SHADER_VERSION_DIRECTIVE |
592 | 34 "attribute vec2 a_texcoord; \n" |
35 "attribute vec4 a_position; \n" | |
36 "uniform mat4 u_matrix; \n" | |
37 "varying vec2 v_texcoord; \n" | |
38 "void main() \n" | |
39 "{ \n" | |
40 " gl_Position = u_matrix * a_position; \n" | |
41 " v_texcoord = a_texcoord; \n" | |
42 "}"; | |
43 | |
44 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
|
45 ORTHANC_STONE_OPENGL_SHADER_VERSION_DIRECTIVE |
592 | 46 "uniform sampler2D u_texture; \n" |
47 "uniform vec3 u_color; \n" | |
48 "varying vec2 v_texcoord; \n" | |
49 "void main() \n" | |
50 "{ \n" | |
51 " vec4 v = texture2D(u_texture, v_texcoord); \n" | |
52 " gl_FragColor = vec4(u_color * v.w, v.w); \n" // Premultiplied alpha | |
53 "}"; | |
54 | |
55 | |
56 namespace OrthancStone | |
57 { | |
58 namespace Internals | |
59 { | |
60 OpenGLTextProgram::OpenGLTextProgram(OpenGL::IOpenGLContext& context) : | |
61 context_(context) | |
62 { | |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
63 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
|
64 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
65 context_.MakeCurrent(); |
592 | 66 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
67 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
|
68 program_->CompileShaders(VERTEX_SHADER, FRAGMENT_SHADER); |
592 | 69 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
70 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
|
71 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
|
72 } |
592 | 73 } |
74 | |
75 | |
76 OpenGLTextProgram::Data::Data(OpenGL::IOpenGLContext& context, | |
77 const GlyphTextureAlphabet& alphabet, | |
78 const TextSceneLayer& layer) : | |
79 context_(context), | |
804
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
80 red_(layer.GetColor().GetRedAsFloat()), |
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
81 green_(layer.GetColor().GetGreenAsFloat()), |
61ba4b504e9a
PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
699
diff
changeset
|
82 blue_(layer.GetColor().GetBlueAsFloat()), |
592 | 83 x_(layer.GetX()), |
84 y_(layer.GetY()), | |
85 border_(layer.GetBorder()), | |
86 anchor_(layer.GetAnchor()) | |
87 { | |
88 OpenGL::OpenGLTextCoordinates coordinates(alphabet, layer.GetText()); | |
89 textWidth_ = coordinates.GetTextWidth(); | |
90 textHeight_ = coordinates.GetTextHeight(); | |
91 | |
92 if (coordinates.IsEmpty()) | |
93 { | |
94 coordinatesCount_ = 0; | |
95 } | |
96 else | |
97 { | |
98 coordinatesCount_ = coordinates.GetRenderingCoords().size(); | |
99 | |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
100 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
|
101 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
102 context_.MakeCurrent(); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
103 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
|
104 ORTHANC_OPENGL_CHECK("glGenBuffers"); |
592 | 105 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
106 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
|
107 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
|
108 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
|
109 &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
|
110 ORTHANC_OPENGL_CHECK("glBufferData"); |
592 | 111 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
112 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
|
113 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
|
114 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
|
115 &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
|
116 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
|
117 } |
592 | 118 } |
119 } | |
120 | |
121 | |
122 OpenGLTextProgram::Data::~Data() | |
123 { | |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
124 if (!context_.IsContextLost() && !IsEmpty()) |
592 | 125 { |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
126 try |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
127 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
128 context_.MakeCurrent(); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
129 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
|
130 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
|
131 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
|
132 } |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
133 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
|
134 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
135 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
|
136 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
137 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
|
138 } |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
139 else |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
140 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
141 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
|
142 } |
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 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
|
145 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
146 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
|
147 } |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
148 catch (...) |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
149 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
150 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
|
151 } |
592 | 152 } |
153 } | |
154 | |
155 | |
156 GLuint OpenGLTextProgram::Data::GetSceneLocationsBuffer() const | |
157 { | |
158 if (IsEmpty()) | |
159 { | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
947
diff
changeset
|
160 LOG(ERROR) << "OpenGLTextProgram::Data::GetSceneLocationsBuffer(): (IsEmpty())"; |
592 | 161 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
162 } | |
163 else | |
164 { | |
165 return buffers_[0]; | |
166 } | |
167 } | |
168 | |
169 GLuint OpenGLTextProgram::Data::GetTextureLocationsBuffer() const | |
170 { | |
171 if (IsEmpty()) | |
172 { | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
947
diff
changeset
|
173 LOG(ERROR) << "OpenGLTextProgram::Data::GetTextureLocationsBuffer(): (IsEmpty())"; |
592 | 174 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
175 } | |
176 else | |
177 { | |
178 return buffers_[1]; | |
179 } | |
180 } | |
181 | |
182 void OpenGLTextProgram::Apply(OpenGL::OpenGLTexture& fontTexture, | |
183 const Data& data, | |
184 const AffineTransform2D& transform) | |
185 { | |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
804
diff
changeset
|
186 if (!context_.IsContextLost() && !data.IsEmpty()) |
592 | 187 { |
188 context_.MakeCurrent(); | |
189 program_->Use(); | |
190 | |
191 double dx, dy; // In pixels | |
192 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
|
193 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
|
194 static_cast<unsigned int>(data.GetBorder())); |
592 | 195 |
196 double x = data.GetX(); | |
197 double y = data.GetY(); | |
198 transform.Apply(x, y); | |
199 | |
200 const AffineTransform2D t = AffineTransform2D::CreateOffset(x + dx, y + dy); | |
201 | |
202 float m[16]; | |
203 t.ConvertToOpenGLMatrix(m, context_.GetCanvasWidth(), context_.GetCanvasHeight()); | |
204 | |
205 fontTexture.Bind(program_->GetUniformLocation("u_texture")); | |
206 glUniformMatrix4fv(program_->GetUniformLocation("u_matrix"), 1, GL_FALSE, m); | |
207 glUniform3f(program_->GetUniformLocation("u_color"), | |
208 data.GetRed(), data.GetGreen(), data.GetBlue()); | |
209 | |
210 glBindBuffer(GL_ARRAY_BUFFER, data.GetSceneLocationsBuffer()); | |
211 glEnableVertexAttribArray(positionLocation_); | |
212 glVertexAttribPointer(positionLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0); | |
213 | |
214 glBindBuffer(GL_ARRAY_BUFFER, data.GetTextureLocationsBuffer()); | |
215 glEnableVertexAttribArray(textureLocation_); | |
216 glVertexAttribPointer(textureLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0); | |
217 | |
218 glEnable(GL_BLEND); | |
219 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
|
220 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
|
221 static_cast<GLsizei>(data.GetCoordinatesCount() / COMPONENTS)); |
592 | 222 glDisable(GL_BLEND); |
223 | |
224 glDisableVertexAttribArray(positionLocation_); | |
225 glDisableVertexAttribArray(textureLocation_); | |
226 } | |
227 } | |
228 } | |
229 } |