comparison Framework/OpenGL/ColorTextureOpenGLProgram.cpp @ 585:b9ce24c606ae

TextSceneLayer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 19 Apr 2019 17:57:58 +0200
parents
children
comparison
equal deleted inserted replaced
584:434ceeb0bcab 585:b9ce24c606ae
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 "ColorTextureOpenGLProgram.h"
23
24 namespace OrthancStone
25 {
26 namespace OpenGL
27 {
28 ColorTextureOpenGLProgram::ColorTextureOpenGLProgram(IOpenGLContext& context) :
29 context_(context)
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 "varying vec2 v_texcoord; \n"
45 "void main() \n"
46 "{ \n"
47 " gl_FragColor = texture2D(u_texture, v_texcoord); \n"
48 "}";
49
50 static const float POSITIONS[COMPONENTS * COUNT] = {
51 0, 0,
52 0, 1,
53 1, 0,
54 1, 0,
55 0, 1,
56 1, 1
57 };
58
59 context_.MakeCurrent();
60
61 program_.reset(new OpenGLProgram);
62 program_->CompileShaders(VERTEX_SHADER, FRAGMENT_SHADER);
63
64 positionLocation_ = program_->GetAttributeLocation("a_position");
65 textureLocation_ = program_->GetAttributeLocation("a_texcoord");
66
67 glGenBuffers(2, buffers_);
68
69 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]);
70 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * COMPONENTS * COUNT, POSITIONS, GL_STATIC_DRAW);
71
72 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]);
73 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * COMPONENTS * COUNT, POSITIONS, GL_STATIC_DRAW);
74 }
75
76
77 ColorTextureOpenGLProgram::~ColorTextureOpenGLProgram()
78 {
79 context_.MakeCurrent();
80 glDeleteBuffers(2, buffers_);
81 }
82
83
84 void ColorTextureOpenGLProgram::Apply(OpenGLTexture& texture,
85 const AffineTransform2D& transform,
86 bool useAlpha)
87 {
88 context_.MakeCurrent();
89 program_->Use();
90
91 AffineTransform2D scale = AffineTransform2D::CreateScaling
92 (texture.GetWidth(), texture.GetHeight());
93
94 AffineTransform2D t = AffineTransform2D::Combine(transform, scale);
95
96 float m[16];
97 t.ConvertToOpenGLMatrix(m, context_.GetCanvasWidth(), context_.GetCanvasHeight());
98
99 texture.Bind(program_->GetUniformLocation("u_texture"));
100 glUniformMatrix4fv(program_->GetUniformLocation("u_matrix"), 1, GL_FALSE, m);
101
102 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]);
103 glEnableVertexAttribArray(positionLocation_);
104 glVertexAttribPointer(positionLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0);
105
106 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]);
107 glEnableVertexAttribArray(textureLocation_);
108 glVertexAttribPointer(textureLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0);
109
110 if (useAlpha)
111 {
112 glEnable(GL_BLEND);
113 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
114 glDrawArrays(GL_TRIANGLES, 0, COUNT);
115 glDisable(GL_BLEND);
116 }
117 else
118 {
119 glDrawArrays(GL_TRIANGLES, 0, COUNT);
120 }
121
122 glDisableVertexAttribArray(positionLocation_);
123 glDisableVertexAttribArray(textureLocation_);
124 }
125 }
126 }