Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Scene2D/Internals/OpenGLTextureProgram.cpp @ 1596:4fb8fdf03314
removed annoying whitespace
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 26 Oct 2020 17:51:42 +0100 |
parents | 92fca2b3ba3d |
children | 8563ea5d8ae4 |
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 | |
1270
2d8ab34c8c91
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
947
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
591 | 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. | |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1576
diff
changeset
|
16 * |
591 | 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 |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
25 #include <OrthancException.h> |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
26 |
591 | 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 = | |
611
e3f21a265be5
Added version directive to GLSL shader code + glew init function in sample code
Benjamin Golinvaux <bgo@osimis.io>
parents:
591
diff
changeset
|
31 ORTHANC_STONE_OPENGL_SHADER_VERSION_DIRECTIVE |
591 | 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, | |
1576
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
48 const AffineTransform2D& transform, |
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
49 unsigned int canvasWidth, |
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
50 unsigned int canvasHeight) |
591 | 51 { |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
52 if (!context_.IsContextLost()) |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
53 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
54 context_.MakeCurrent(); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
55 program_->Use(); |
591 | 56 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
57 AffineTransform2D scale = AffineTransform2D::CreateScaling |
591 | 58 (texture.GetWidth(), texture.GetHeight()); |
59 | |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
60 AffineTransform2D t = AffineTransform2D::Combine(transform, scale); |
591 | 61 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
62 float m[16]; |
1576
92fca2b3ba3d
sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
63 t.ConvertToOpenGLMatrix(m, canvasWidth, canvasHeight); |
591 | 64 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
65 texture.Bind(program_->GetUniformLocation("u_texture")); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
66 glUniformMatrix4fv(program_->GetUniformLocation("u_matrix"), 1, GL_FALSE, m); |
591 | 67 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
68 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
69 glEnableVertexAttribArray(positionLocation_); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
70 glVertexAttribPointer(positionLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0); |
591 | 71 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
72 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
73 glEnableVertexAttribArray(textureLocation_); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
74 glVertexAttribPointer(textureLocation_, COMPONENTS, GL_FLOAT, GL_FALSE, 0, 0); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
75 } |
591 | 76 } |
77 | |
78 void OpenGLTextureProgram::FinalizeExecution() | |
79 { | |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
80 if (!context_.IsContextLost()) |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
81 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
82 glDisableVertexAttribArray(positionLocation_); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
83 glDisableVertexAttribArray(textureLocation_); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
84 } |
591 | 85 } |
86 | |
87 OpenGLTextureProgram::OpenGLTextureProgram(OpenGL::IOpenGLContext& context, | |
88 const char* fragmentShader) : | |
89 context_(context) | |
90 { | |
91 static const float POSITIONS[COMPONENTS * COUNT] = { | |
92 0, 0, | |
93 0, 1, | |
94 1, 0, | |
95 1, 0, | |
96 0, 1, | |
97 1, 1 | |
98 }; | |
99 | |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
100 if (!context_.IsContextLost()) |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
101 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
102 context_.MakeCurrent(); |
591 | 103 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
104 program_.reset(new OpenGL::OpenGLProgram(context_)); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
105 program_->CompileShaders(VERTEX_SHADER, fragmentShader); |
591 | 106 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
107 positionLocation_ = program_->GetAttributeLocation("a_position"); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
108 textureLocation_ = program_->GetAttributeLocation("a_texcoord"); |
591 | 109 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
110 glGenBuffers(2, buffers_); |
591 | 111 |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
112 glBindBuffer(GL_ARRAY_BUFFER, buffers_[0]); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
113 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * COMPONENTS * COUNT, POSITIONS, GL_STATIC_DRAW); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
114 |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
115 glBindBuffer(GL_ARRAY_BUFFER, buffers_[1]); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
116 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * COMPONENTS * COUNT, POSITIONS, GL_STATIC_DRAW); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
117 } |
591 | 118 } |
119 | |
120 OpenGLTextureProgram::~OpenGLTextureProgram() | |
121 { | |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
122 if (!context_.IsContextLost()) |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
123 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
124 ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("OpenGLTextureProgram::~OpenGLTextureProgram() | About to call glDeleteBuffers"); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
125 context_.MakeCurrent(); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
126 glDeleteBuffers(2, buffers_); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
127 } |
591 | 128 } |
129 | |
130 | |
131 void OpenGLTextureProgram::Execution::DrawTriangles() | |
132 { | |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
133 if (!that_.context_.IsContextLost()) |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
134 { |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
135 glDrawArrays(GL_TRIANGLES, 0, COUNT); |
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
611
diff
changeset
|
136 } |
591 | 137 } |
138 } | |
139 } |