Mercurial > hg > orthanc-stone
annotate Framework/Scene2D/Internals/OpenGLTextureProgram.cpp @ 920:5ca418d6579e refactor-viewport-controller
Close branch refactor-viewport-controller.
author | Alain Mazy <am@osimis.io> |
---|---|
date | Fri, 19 Jul 2019 14:15:49 +0000 |
parents | e3f21a265be5 |
children | 1091b2adeb5a |
rev | line source |
---|---|
591 | 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" | |
611
e3f21a265be5
Added version directive to GLSL shader code + glew init function in sample code
Benjamin Golinvaux <bgo@osimis.io>
parents:
591
diff
changeset
|
23 #include "OpenGLShaderVersionDirective.h" |
591 | 24 |
25 static const unsigned int COMPONENTS = 2; | |
26 static const unsigned int COUNT = 6; // 2 triangles in 2D | |
27 | |
28 static const char* VERTEX_SHADER = | |
611
e3f21a265be5
Added version directive to GLSL shader code + glew init function in sample code
Benjamin Golinvaux <bgo@osimis.io>
parents:
591
diff
changeset
|
29 ORTHANC_STONE_OPENGL_SHADER_VERSION_DIRECTIVE |
591 | 30 "attribute vec2 a_texcoord; \n" |
31 "attribute vec4 a_position; \n" | |
32 "uniform mat4 u_matrix; \n" | |
33 "varying vec2 v_texcoord; \n" | |
34 "void main() \n" | |
35 "{ \n" | |
36 " gl_Position = u_matrix * a_position; \n" | |
37 " v_texcoord = a_texcoord; \n" | |
38 "}"; | |
39 | |
40 | |
41 namespace OrthancStone | |
42 { | |
43 namespace Internals | |
44 { | |
45 void OpenGLTextureProgram::InitializeExecution(OpenGL::OpenGLTexture& texture, | |
46 const AffineTransform2D& transform) | |
47 { | |
48 context_.MakeCurrent(); | |
49 program_->Use(); | |
50 | |
51 AffineTransform2D scale = AffineTransform2D::CreateScaling | |
52 (texture.GetWidth(), texture.GetHeight()); | |
53 | |
54 AffineTransform2D t = AffineTransform2D::Combine(transform, scale); | |
55 | |
56 float m[16]; | |
57 t.ConvertToOpenGLMatrix(m, context_.GetCanvasWidth(), context_.GetCanvasHeight()); | |
58 | |
59 texture.Bind(program_->GetUniformLocation("u_texture")); | |
60 glUniformMatrix4fv(program_->GetUniformLocation("u_matrix"), 1, GL_FALSE, m); | |
61 | |
62 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]); | |
63 glEnableVertexAttribArray(positionLocation_); | |
64 glVertexAttribPointer(positionLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0); | |
65 | |
66 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]); | |
67 glEnableVertexAttribArray(textureLocation_); | |
68 glVertexAttribPointer(textureLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0); | |
69 } | |
70 | |
71 | |
72 void OpenGLTextureProgram::FinalizeExecution() | |
73 { | |
74 glDisableVertexAttribArray(positionLocation_); | |
75 glDisableVertexAttribArray(textureLocation_); | |
76 } | |
77 | |
78 | |
79 OpenGLTextureProgram::OpenGLTextureProgram(OpenGL::IOpenGLContext& context, | |
80 const char* fragmentShader) : | |
81 context_(context) | |
82 { | |
83 static const float POSITIONS[COMPONENTS * COUNT] = { | |
84 0, 0, | |
85 0, 1, | |
86 1, 0, | |
87 1, 0, | |
88 0, 1, | |
89 1, 1 | |
90 }; | |
91 | |
92 context_.MakeCurrent(); | |
93 | |
94 program_.reset(new OpenGL::OpenGLProgram); | |
95 program_->CompileShaders(VERTEX_SHADER, fragmentShader); | |
96 | |
97 positionLocation_ = program_->GetAttributeLocation("a_position"); | |
98 textureLocation_ = program_->GetAttributeLocation("a_texcoord"); | |
99 | |
100 glGenBuffers(2, buffers_); | |
101 | |
102 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]); | |
103 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * COMPONENTS * COUNT, POSITIONS, GL_STATIC_DRAW); | |
104 | |
105 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]); | |
106 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * COMPONENTS * COUNT, POSITIONS, GL_STATIC_DRAW); | |
107 } | |
108 | |
109 | |
110 OpenGLTextureProgram::~OpenGLTextureProgram() | |
111 { | |
112 context_.MakeCurrent(); | |
113 glDeleteBuffers(2, buffers_); | |
114 } | |
115 | |
116 | |
117 void OpenGLTextureProgram::Execution::DrawTriangles() | |
118 { | |
119 glDrawArrays(GL_TRIANGLES, 0, COUNT); | |
120 } | |
121 } | |
122 } |