Mercurial > hg > orthanc-stone
comparison Framework/Scene2D/Internals/OpenGLTextureProgram.cpp @ 591:b66ced2c43d4
OpenGLTextureProgram
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 26 Apr 2019 12:05:38 +0200 |
parents | |
children | e3f21a265be5 |
comparison
equal
deleted
inserted
replaced
590:5430bcffba57 | 591:b66ced2c43d4 |
---|---|
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 "OpenGLTextureProgram.h" | |
23 | |
24 static const unsigned int COMPONENTS = 2; | |
25 static const unsigned int COUNT = 6; // 2 triangles in 2D | |
26 | |
27 static const char* VERTEX_SHADER = | |
28 "attribute vec2 a_texcoord; \n" | |
29 "attribute vec4 a_position; \n" | |
30 "uniform mat4 u_matrix; \n" | |
31 "varying vec2 v_texcoord; \n" | |
32 "void main() \n" | |
33 "{ \n" | |
34 " gl_Position = u_matrix * a_position; \n" | |
35 " v_texcoord = a_texcoord; \n" | |
36 "}"; | |
37 | |
38 | |
39 namespace OrthancStone | |
40 { | |
41 namespace Internals | |
42 { | |
43 void OpenGLTextureProgram::InitializeExecution(OpenGL::OpenGLTexture& texture, | |
44 const AffineTransform2D& transform) | |
45 { | |
46 context_.MakeCurrent(); | |
47 program_->Use(); | |
48 | |
49 AffineTransform2D scale = AffineTransform2D::CreateScaling | |
50 (texture.GetWidth(), texture.GetHeight()); | |
51 | |
52 AffineTransform2D t = AffineTransform2D::Combine(transform, scale); | |
53 | |
54 float m[16]; | |
55 t.ConvertToOpenGLMatrix(m, context_.GetCanvasWidth(), context_.GetCanvasHeight()); | |
56 | |
57 texture.Bind(program_->GetUniformLocation("u_texture")); | |
58 glUniformMatrix4fv(program_->GetUniformLocation("u_matrix"), 1, GL_FALSE, m); | |
59 | |
60 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]); | |
61 glEnableVertexAttribArray(positionLocation_); | |
62 glVertexAttribPointer(positionLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0); | |
63 | |
64 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]); | |
65 glEnableVertexAttribArray(textureLocation_); | |
66 glVertexAttribPointer(textureLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0); | |
67 } | |
68 | |
69 | |
70 void OpenGLTextureProgram::FinalizeExecution() | |
71 { | |
72 glDisableVertexAttribArray(positionLocation_); | |
73 glDisableVertexAttribArray(textureLocation_); | |
74 } | |
75 | |
76 | |
77 OpenGLTextureProgram::OpenGLTextureProgram(OpenGL::IOpenGLContext& context, | |
78 const char* fragmentShader) : | |
79 context_(context) | |
80 { | |
81 static const float POSITIONS[COMPONENTS * COUNT] = { | |
82 0, 0, | |
83 0, 1, | |
84 1, 0, | |
85 1, 0, | |
86 0, 1, | |
87 1, 1 | |
88 }; | |
89 | |
90 context_.MakeCurrent(); | |
91 | |
92 program_.reset(new OpenGL::OpenGLProgram); | |
93 program_->CompileShaders(VERTEX_SHADER, fragmentShader); | |
94 | |
95 positionLocation_ = program_->GetAttributeLocation("a_position"); | |
96 textureLocation_ = program_->GetAttributeLocation("a_texcoord"); | |
97 | |
98 glGenBuffers(2, buffers_); | |
99 | |
100 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]); | |
101 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * COMPONENTS * COUNT, POSITIONS, GL_STATIC_DRAW); | |
102 | |
103 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]); | |
104 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * COMPONENTS * COUNT, POSITIONS, GL_STATIC_DRAW); | |
105 } | |
106 | |
107 | |
108 OpenGLTextureProgram::~OpenGLTextureProgram() | |
109 { | |
110 context_.MakeCurrent(); | |
111 glDeleteBuffers(2, buffers_); | |
112 } | |
113 | |
114 | |
115 void OpenGLTextureProgram::Execution::DrawTriangles() | |
116 { | |
117 glDrawArrays(GL_TRIANGLES, 0, COUNT); | |
118 } | |
119 } | |
120 } |