Mercurial > hg > orthanc-stone
annotate Framework/Scene2D/Internals/ColorTextureOpenGLProgram.cpp @ 590:5430bcffba57
FloatTextureSceneLayer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 26 Apr 2019 11:33:57 +0200 |
parents | 5b4890d289cf |
children |
rev | line source |
---|---|
585 | 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 { | |
586 | 26 namespace Internals |
585 | 27 { |
586 | 28 ColorTextureOpenGLProgram::ColorTextureOpenGLProgram(OpenGL::IOpenGLContext& context) : |
585 | 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 | |
586 | 61 program_.reset(new OpenGL::OpenGLProgram); |
585 | 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 | |
586 | 84 void ColorTextureOpenGLProgram::Apply(OpenGL::OpenGLTexture& texture, |
585 | 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); | |
590
5430bcffba57
FloatTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
586
diff
changeset
|
113 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
5430bcffba57
FloatTextureSceneLayer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
586
diff
changeset
|
114 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
585 | 115 glDrawArrays(GL_TRIANGLES, 0, COUNT); |
116 glDisable(GL_BLEND); | |
117 } | |
118 else | |
119 { | |
120 glDrawArrays(GL_TRIANGLES, 0, COUNT); | |
121 } | |
122 | |
123 glDisableVertexAttribArray(positionLocation_); | |
124 glDisableVertexAttribArray(textureLocation_); | |
125 } | |
126 } | |
127 } |