annotate Framework/Scene2D/Internals/OpenGLTextureProgram.cpp @ 826:2de01660debe

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 29 May 2019 16:48:56 +0200
parents e3f21a265be5
children 1091b2adeb5a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
591
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 static const unsigned int COMPONENTS = 2;
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 static const unsigned int COUNT = 6; // 2 triangles in 2D
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 "attribute vec2 a_texcoord; \n"
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 "attribute vec4 a_position; \n"
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 "uniform mat4 u_matrix; \n"
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 "varying vec2 v_texcoord; \n"
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 "void main() \n"
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 "{ \n"
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 " gl_Position = u_matrix * a_position; \n"
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 " v_texcoord = a_texcoord; \n"
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 "}";
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 namespace OrthancStone
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 {
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 namespace Internals
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 void OpenGLTextureProgram::InitializeExecution(OpenGL::OpenGLTexture& texture,
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 const AffineTransform2D& transform)
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 {
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 context_.MakeCurrent();
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 program_->Use();
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 AffineTransform2D scale = AffineTransform2D::CreateScaling
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 (texture.GetWidth(), texture.GetHeight());
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 AffineTransform2D t = AffineTransform2D::Combine(transform, scale);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 float m[16];
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 t.ConvertToOpenGLMatrix(m, context_.GetCanvasWidth(), context_.GetCanvasHeight());
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 texture.Bind(program_->GetUniformLocation("u_texture"));
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 glUniformMatrix4fv(program_->GetUniformLocation("u_matrix"), 1, GL_FALSE, m);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 glEnableVertexAttribArray(positionLocation_);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 glVertexAttribPointer(positionLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 glEnableVertexAttribArray(textureLocation_);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 glVertexAttribPointer(textureLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 }
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 void OpenGLTextureProgram::FinalizeExecution()
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 {
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 glDisableVertexAttribArray(positionLocation_);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 glDisableVertexAttribArray(textureLocation_);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 }
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 OpenGLTextureProgram::OpenGLTextureProgram(OpenGL::IOpenGLContext& context,
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 const char* fragmentShader) :
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 context_(context)
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 {
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 static const float POSITIONS[COMPONENTS * COUNT] = {
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 0, 0,
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 0, 1,
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 1, 0,
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 1, 0,
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 0, 1,
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 1, 1
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 };
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 context_.MakeCurrent();
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 program_.reset(new OpenGL::OpenGLProgram);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 program_->CompileShaders(VERTEX_SHADER, fragmentShader);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 positionLocation_ = program_->GetAttributeLocation("a_position");
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 textureLocation_ = program_->GetAttributeLocation("a_texcoord");
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 glGenBuffers(2, buffers_);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * COMPONENTS * COUNT, POSITIONS, GL_STATIC_DRAW);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * COMPONENTS * COUNT, POSITIONS, GL_STATIC_DRAW);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 }
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 OpenGLTextureProgram::~OpenGLTextureProgram()
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 {
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 context_.MakeCurrent();
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 glDeleteBuffers(2, buffers_);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 }
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 void OpenGLTextureProgram::Execution::DrawTriangles()
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 {
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 glDrawArrays(GL_TRIANGLES, 0, COUNT);
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 }
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 }
b66ced2c43d4 OpenGLTextureProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 }