Mercurial > hg > orthanc-stone
comparison Framework/Scene2D/Internals/OpenGLTextProgram.cpp @ 592:bbe29efd3d1c
OpenGLLinesProgram
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 26 Apr 2019 12:55:43 +0200 |
parents | |
children | e3f21a265be5 |
comparison
equal
deleted
inserted
replaced
591:b66ced2c43d4 | 592:bbe29efd3d1c |
---|---|
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" | |
23 | |
24 #include "../../Fonts/OpenGLTextCoordinates.h" | |
25 | |
26 #include <Core/OrthancException.h> | |
27 | |
28 | |
29 static const unsigned int COMPONENTS = 2; | |
30 | |
31 static const char* VERTEX_SHADER = | |
32 "attribute vec2 a_texcoord; \n" | |
33 "attribute vec4 a_position; \n" | |
34 "uniform mat4 u_matrix; \n" | |
35 "varying vec2 v_texcoord; \n" | |
36 "void main() \n" | |
37 "{ \n" | |
38 " gl_Position = u_matrix * a_position; \n" | |
39 " v_texcoord = a_texcoord; \n" | |
40 "}"; | |
41 | |
42 static const char* FRAGMENT_SHADER = | |
43 "uniform sampler2D u_texture; \n" | |
44 "uniform vec3 u_color; \n" | |
45 "varying vec2 v_texcoord; \n" | |
46 "void main() \n" | |
47 "{ \n" | |
48 " vec4 v = texture2D(u_texture, v_texcoord); \n" | |
49 " gl_FragColor = vec4(u_color * v.w, v.w); \n" // Premultiplied alpha | |
50 "}"; | |
51 | |
52 | |
53 namespace OrthancStone | |
54 { | |
55 namespace Internals | |
56 { | |
57 OpenGLTextProgram::OpenGLTextProgram(OpenGL::IOpenGLContext& context) : | |
58 context_(context) | |
59 { | |
60 | |
61 context_.MakeCurrent(); | |
62 | |
63 program_.reset(new OpenGL::OpenGLProgram); | |
64 program_->CompileShaders(VERTEX_SHADER, FRAGMENT_SHADER); | |
65 | |
66 positionLocation_ = program_->GetAttributeLocation("a_position"); | |
67 textureLocation_ = program_->GetAttributeLocation("a_texcoord"); | |
68 } | |
69 | |
70 | |
71 OpenGLTextProgram::Data::Data(OpenGL::IOpenGLContext& context, | |
72 const GlyphTextureAlphabet& alphabet, | |
73 const TextSceneLayer& layer) : | |
74 context_(context), | |
75 red_(layer.GetRedAsFloat()), | |
76 green_(layer.GetGreenAsFloat()), | |
77 blue_(layer.GetBlueAsFloat()), | |
78 x_(layer.GetX()), | |
79 y_(layer.GetY()), | |
80 border_(layer.GetBorder()), | |
81 anchor_(layer.GetAnchor()) | |
82 { | |
83 OpenGL::OpenGLTextCoordinates coordinates(alphabet, layer.GetText()); | |
84 textWidth_ = coordinates.GetTextWidth(); | |
85 textHeight_ = coordinates.GetTextHeight(); | |
86 | |
87 if (coordinates.IsEmpty()) | |
88 { | |
89 coordinatesCount_ = 0; | |
90 } | |
91 else | |
92 { | |
93 coordinatesCount_ = coordinates.GetRenderingCoords().size(); | |
94 | |
95 context_.MakeCurrent(); | |
96 glGenBuffers(2, buffers_); | |
97 | |
98 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]); | |
99 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * coordinatesCount_, | |
100 &coordinates.GetRenderingCoords() [0], GL_STATIC_DRAW); | |
101 | |
102 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]); | |
103 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * coordinatesCount_, | |
104 &coordinates.GetTextureCoords() [0], GL_STATIC_DRAW); | |
105 } | |
106 } | |
107 | |
108 | |
109 OpenGLTextProgram::Data::~Data() | |
110 { | |
111 if (!IsEmpty()) | |
112 { | |
113 context_.MakeCurrent(); | |
114 glDeleteBuffers(2, buffers_); | |
115 } | |
116 } | |
117 | |
118 | |
119 GLuint OpenGLTextProgram::Data::GetSceneLocationsBuffer() const | |
120 { | |
121 if (IsEmpty()) | |
122 { | |
123 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
124 } | |
125 else | |
126 { | |
127 return buffers_[0]; | |
128 } | |
129 } | |
130 | |
131 | |
132 GLuint OpenGLTextProgram::Data::GetTextureLocationsBuffer() const | |
133 { | |
134 if (IsEmpty()) | |
135 { | |
136 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
137 } | |
138 else | |
139 { | |
140 return buffers_[1]; | |
141 } | |
142 } | |
143 | |
144 | |
145 void OpenGLTextProgram::Apply(OpenGL::OpenGLTexture& fontTexture, | |
146 const Data& data, | |
147 const AffineTransform2D& transform) | |
148 { | |
149 if (!data.IsEmpty()) | |
150 { | |
151 context_.MakeCurrent(); | |
152 program_->Use(); | |
153 | |
154 double dx, dy; // In pixels | |
155 ComputeAnchorTranslation(dx, dy, data.GetAnchor(), | |
156 data.GetTextWidth(), data.GetTextHeight(), data.GetBorder()); | |
157 | |
158 double x = data.GetX(); | |
159 double y = data.GetY(); | |
160 transform.Apply(x, y); | |
161 | |
162 const AffineTransform2D t = AffineTransform2D::CreateOffset(x + dx, y + dy); | |
163 | |
164 float m[16]; | |
165 t.ConvertToOpenGLMatrix(m, context_.GetCanvasWidth(), context_.GetCanvasHeight()); | |
166 | |
167 fontTexture.Bind(program_->GetUniformLocation("u_texture")); | |
168 glUniformMatrix4fv(program_->GetUniformLocation("u_matrix"), 1, GL_FALSE, m); | |
169 glUniform3f(program_->GetUniformLocation("u_color"), | |
170 data.GetRed(), data.GetGreen(), data.GetBlue()); | |
171 | |
172 glBindBuffer(GL_ARRAY_BUFFER, data.GetSceneLocationsBuffer()); | |
173 glEnableVertexAttribArray(positionLocation_); | |
174 glVertexAttribPointer(positionLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0); | |
175 | |
176 glBindBuffer(GL_ARRAY_BUFFER, data.GetTextureLocationsBuffer()); | |
177 glEnableVertexAttribArray(textureLocation_); | |
178 glVertexAttribPointer(textureLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0); | |
179 | |
180 glEnable(GL_BLEND); | |
181 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); | |
182 glDrawArrays(GL_TRIANGLES, 0, data.GetCoordinatesCount() / COMPONENTS); | |
183 glDisable(GL_BLEND); | |
184 | |
185 glDisableVertexAttribArray(positionLocation_); | |
186 glDisableVertexAttribArray(textureLocation_); | |
187 } | |
188 } | |
189 } | |
190 } |