comparison OrthancStone/Sources/Scene2D/Internals/OpenGLTextureProgram.cpp @ 1512:244ad1e4e76a

reorganization of folders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 16:21:02 +0200
parents Framework/Scene2D/Internals/OpenGLTextureProgram.cpp@30deba7bc8e2
children 92fca2b3ba3d
comparison
equal deleted inserted replaced
1511:9dfeee74c1e6 1512:244ad1e4e76a
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-2020 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 "OpenGLTextureProgram.h"
23 #include "OpenGLShaderVersionDirective.h"
24
25 #include <OrthancException.h>
26
27 static const unsigned int COMPONENTS = 2;
28 static const unsigned int COUNT = 6; // 2 triangles in 2D
29
30 static const char* VERTEX_SHADER =
31 ORTHANC_STONE_OPENGL_SHADER_VERSION_DIRECTIVE
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
43 namespace OrthancStone
44 {
45 namespace Internals
46 {
47 void OpenGLTextureProgram::InitializeExecution(OpenGL::OpenGLTexture& texture,
48 const AffineTransform2D& transform)
49 {
50 if (!context_.IsContextLost())
51 {
52 context_.MakeCurrent();
53 program_->Use();
54
55 AffineTransform2D scale = AffineTransform2D::CreateScaling
56 (texture.GetWidth(), texture.GetHeight());
57
58 AffineTransform2D t = AffineTransform2D::Combine(transform, scale);
59
60 float m[16];
61 t.ConvertToOpenGLMatrix(m, context_.GetCanvasWidth(), context_.GetCanvasHeight());
62
63 texture.Bind(program_->GetUniformLocation("u_texture"));
64 glUniformMatrix4fv(program_->GetUniformLocation("u_matrix"), 1, GL_FALSE, m);
65
66 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]);
67 glEnableVertexAttribArray(positionLocation_);
68 glVertexAttribPointer(positionLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0);
69
70 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]);
71 glEnableVertexAttribArray(textureLocation_);
72 glVertexAttribPointer(textureLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0);
73 }
74 }
75
76 void OpenGLTextureProgram::FinalizeExecution()
77 {
78 if (!context_.IsContextLost())
79 {
80 glDisableVertexAttribArray(positionLocation_);
81 glDisableVertexAttribArray(textureLocation_);
82 }
83 }
84
85 OpenGLTextureProgram::OpenGLTextureProgram(OpenGL::IOpenGLContext& context,
86 const char* fragmentShader) :
87 context_(context)
88 {
89 static const float POSITIONS[COMPONENTS * COUNT] = {
90 0, 0,
91 0, 1,
92 1, 0,
93 1, 0,
94 0, 1,
95 1, 1
96 };
97
98 if (!context_.IsContextLost())
99 {
100 context_.MakeCurrent();
101
102 program_.reset(new OpenGL::OpenGLProgram(context_));
103 program_->CompileShaders(VERTEX_SHADER, fragmentShader);
104
105 positionLocation_ = program_->GetAttributeLocation("a_position");
106 textureLocation_ = program_->GetAttributeLocation("a_texcoord");
107
108 glGenBuffers(2, buffers_);
109
110 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]);
111 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * COMPONENTS * COUNT, POSITIONS, GL_STATIC_DRAW);
112
113 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]);
114 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * COMPONENTS * COUNT, POSITIONS, GL_STATIC_DRAW);
115 }
116 }
117
118 OpenGLTextureProgram::~OpenGLTextureProgram()
119 {
120 if (!context_.IsContextLost())
121 {
122 ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("OpenGLTextureProgram::~OpenGLTextureProgram() | About to call glDeleteBuffers");
123 context_.MakeCurrent();
124 glDeleteBuffers(2, buffers_);
125 }
126 }
127
128
129 void OpenGLTextureProgram::Execution::DrawTriangles()
130 {
131 if (!that_.context_.IsContextLost())
132 {
133 glDrawArrays(GL_TRIANGLES, 0, COUNT);
134 }
135 }
136 }
137 }