annotate OrthancStone/Sources/Scene2D/Internals/OpenGLLinesProgram.cpp @ 1640:52b8b96cb55f

cleaning namespaces
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 10 Nov 2020 16:55:22 +0100
parents 59f95b9ea858
children 9ac2a65d4172
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
1270
2d8ab34c8c91 upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 956
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
1598
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
8 * modify it under the terms of the GNU Lesser General Public License
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
1598
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
15 * Lesser General Public License for more details.
1596
4fb8fdf03314 removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
16 *
1598
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
18 * License along with this program. If not, see
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
19 * <http://www.gnu.org/licenses/>.
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "OpenGLLinesProgram.h"
611
e3f21a265be5 Added version directive to GLSL shader code + glew init function in sample code
Benjamin Golinvaux <bgo@osimis.io>
parents: 592
diff changeset
24 #include "OpenGLShaderVersionDirective.h"
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
1624
59f95b9ea858 fix build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1598
diff changeset
26 #include <Logging.h>
1455
30deba7bc8e2 simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1270
diff changeset
27 #include <OrthancException.h>
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 static const unsigned int COMPONENTS_POSITION = 3;
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
31 static const unsigned int COMPONENTS_COLOR = 3;
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 static const unsigned int COMPONENTS_MITER = 2;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 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: 592
diff changeset
36 ORTHANC_STONE_OPENGL_SHADER_VERSION_DIRECTIVE
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 "attribute vec2 a_miter_direction; \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 "attribute vec4 a_position; \n"
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
39 "attribute vec3 a_color; \n"
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 "uniform float u_thickness; \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 "uniform mat4 u_matrix; \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 "varying float v_distance; \n"
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
43 "varying vec3 v_color; \n"
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 "void main() \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 "{ \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 " v_distance = a_position.z; \n"
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
47 " v_color = a_color; \n"
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 " gl_Position = u_matrix * vec4(a_position.xy + a_position.z * a_miter_direction * u_thickness, 0, 1); \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 "}";
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 static const char* FRAGMENT_SHADER =
611
e3f21a265be5 Added version directive to GLSL shader code + glew init function in sample code
Benjamin Golinvaux <bgo@osimis.io>
parents: 592
diff changeset
53 ORTHANC_STONE_OPENGL_SHADER_VERSION_DIRECTIVE
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 "uniform bool u_antialiasing; \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 "uniform float u_antialiasing_start; \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 "varying float v_distance; \n" // Distance of the point to the segment
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
57 "varying vec3 v_color; \n"
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 "void main() \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 "{ \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 " float d = abs(v_distance); \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 " if (!u_antialiasing || \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 " d <= u_antialiasing_start) \n"
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
63 " gl_FragColor = vec4(v_color, 1); \n"
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 " else if (d >= 1.0) \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 " gl_FragColor = vec4(0, 0, 0, 0); \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 " else \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 " { \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 " float alpha = 1.0 - smoothstep(u_antialiasing_start, 1.0, d); \n"
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
69 " gl_FragColor = vec4(v_color * alpha, alpha); \n"
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 " } \n"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 "}";
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 namespace OrthancStone
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 namespace Internals
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 class OpenGLLinesProgram::Data::Segment
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 private:
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 bool isEmpty_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 double x1_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 double y1_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 double x2_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 double y2_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 double miterX1_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 double miterY1_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 double miterX2_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 double miterY2_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 Vector lineAbove_; // In homogeneous coordinates (size = 3)
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 Vector lineBelow_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 public:
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 Segment(const PolylineSceneLayer::Chain& chain,
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 size_t index1,
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 size_t index2) :
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 isEmpty_(false)
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 if (index1 >= chain.size() ||
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 index2 >= chain.size())
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 else
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 const ScenePoint2D& p = chain[index1];
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 const ScenePoint2D& q = chain[index2];
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 x1_ = p.GetX();
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 y1_ = p.GetY();
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 x2_ = q.GetX();
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 y2_ = q.GetY();
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 const double dx = x2_ - x1_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 const double dy = y2_ - y1_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 const double norm = sqrt(dx * dx + dy * dy);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 if (LinearAlgebra::IsCloseToZero(norm))
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 isEmpty_ = true;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 else
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 isEmpty_ = false;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 const double normalX = -dy / norm;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 const double normalY = dx / norm;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 miterX1_ = normalX;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 miterY1_ = normalY;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 miterX2_ = normalX;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 miterY2_ = normalY;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 Vector a = LinearAlgebra::CreateVector(x1_ + normalX, y1_ + normalY, 1);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 Vector b = LinearAlgebra::CreateVector(x2_ + normalX, y2_ + normalY, 1);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 LinearAlgebra::CrossProduct(lineAbove_, a, b);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 a = LinearAlgebra::CreateVector(x1_ - normalX, y1_ - normalY, 1);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 b = LinearAlgebra::CreateVector(x2_ - normalX, y2_ - normalY, 1);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 LinearAlgebra::CrossProduct(lineBelow_, a, b);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 bool IsEmpty() const
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 return isEmpty_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 static double ComputeSignedArea(double x1,
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 double y1,
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 double x2,
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 double y2,
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 double x3,
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 double y3)
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 // This computes the signed area of a 2D triangle. This
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 // formula is e.g. used in the sorting algorithm of Graham's
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 // scan to compute the convex hull.
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 // https://en.wikipedia.org/wiki/Graham_scan
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 return (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 static void CreateMiter(Segment& left,
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 Segment& right)
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 if (!left.IsEmpty() &&
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 !right.IsEmpty())
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 Vector above, below;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 LinearAlgebra::CrossProduct(above, left.lineAbove_, right.lineAbove_);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 LinearAlgebra::CrossProduct(below, left.lineBelow_, right.lineBelow_);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 if (!LinearAlgebra::IsCloseToZero(above[2]) &&
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 !LinearAlgebra::IsCloseToZero(below[2]))
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 // Back to inhomogeneous 2D coordinates
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 above /= above[2];
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 below /= below[2];
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 // Check whether "above" and "below" intersection points
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 // are on the half-plane defined by the endpoints of the
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 // two segments. This is an indicator of whether the angle
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 // is too acute.
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 double s1 = ComputeSignedArea(left.x1_, left.y1_,
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 above[0], above[1],
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 right.x2_, right.y2_);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 double s2 = ComputeSignedArea(left.x1_, left.y1_,
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 below[0], below[1],
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 right.x2_, right.y2_);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 // The two signed areas must have the same sign
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 if (s1 * s2 >= 0)
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 left.miterX2_ = above[0] - left.x2_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 left.miterY2_ = above[1] - left.y2_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 right.miterX1_ = left.miterX2_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 right.miterY1_ = left.miterY2_;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 void AddTriangles(std::vector<float>& coords,
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
206 std::vector<float>& miterDirections,
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
207 std::vector<float>& colors,
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
208 const Color& color)
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 if (isEmpty_)
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 {
956
a7351ad54960 Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents: 947
diff changeset
212 LOG(ERROR) << "OpenGLLinesProgram -- AddTriangles: (isEmpty_)";
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 // First triangle
693
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
217 coords.push_back(static_cast<float>(x1_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
218 coords.push_back(static_cast<float>(y1_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
219 coords.push_back(static_cast<float>(1));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
220 coords.push_back(static_cast<float>(x2_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
221 coords.push_back(static_cast<float>(y2_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
222 coords.push_back(static_cast<float>(-1));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
223 coords.push_back(static_cast<float>(x2_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
224 coords.push_back(static_cast<float>(y2_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
225 coords.push_back(static_cast<float>(1));
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226
693
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
227 miterDirections.push_back(static_cast<float>(miterX1_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
228 miterDirections.push_back(static_cast<float>(miterY1_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
229 miterDirections.push_back(static_cast<float>(miterX2_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
230 miterDirections.push_back(static_cast<float>(miterY2_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
231 miterDirections.push_back(static_cast<float>(miterX2_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
232 miterDirections.push_back(static_cast<float>(miterY2_));
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 // Second triangle
693
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
235 coords.push_back(static_cast<float>(x1_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
236 coords.push_back(static_cast<float>(y1_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
237 coords.push_back(static_cast<float>(1));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
238 coords.push_back(static_cast<float>(x1_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
239 coords.push_back(static_cast<float>(y1_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
240 coords.push_back(static_cast<float>(-1));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
241 coords.push_back(static_cast<float>(x2_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
242 coords.push_back(static_cast<float>(y2_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
243 coords.push_back(static_cast<float>(-1));
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244
693
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
245 miterDirections.push_back(static_cast<float>(miterX1_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
246 miterDirections.push_back(static_cast<float>(miterY1_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
247 miterDirections.push_back(static_cast<float>(miterX1_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
248 miterDirections.push_back(static_cast<float>(miterY1_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
249 miterDirections.push_back(static_cast<float>(miterX2_));
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
250 miterDirections.push_back(static_cast<float>(miterY2_));
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
251
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
252 // Add the colors of the 2 triangles (leading to 2 * 3 values)
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
253 for (unsigned int i = 0; i < 6; i++)
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
254 {
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
255 colors.push_back(color.GetRedAsFloat());
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
256 colors.push_back(color.GetGreenAsFloat());
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
257 colors.push_back(color.GetBlueAsFloat());
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
258 }
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 };
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 OpenGLLinesProgram::Data::Data(OpenGL::IOpenGLContext& context,
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 const PolylineSceneLayer& layer) :
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 context_(context),
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 verticesCount_(0),
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
267 thickness_(static_cast<float>(layer.GetThickness()))
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
269 if (!context_.IsContextLost())
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
271 // High-level reference:
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
272 // https://mattdesl.svbtle.com/drawing-lines-is-hard
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
273 // https://forum.libcinder.org/topic/smooth-thick-lines-using-geometry-shader
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
275 size_t countVertices = 0;
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
276 for (size_t i = 0; i < layer.GetChainsCount(); i++)
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
278 size_t countSegments = layer.GetChain(i).size() - 1;
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 if (layer.IsClosedChain(i))
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
282 countSegments++;
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
285 // Each segment is made of 2 triangles. One triangle is
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
286 // defined by 3 points in 2D => 6 vertices per segment.
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
287 countVertices += countSegments * 2 * 3;
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
288 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
289
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
290 std::vector<float> coords, colors, miterDirections;
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
291 coords.reserve(countVertices * COMPONENTS_POSITION);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
292 colors.reserve(countVertices * COMPONENTS_COLOR);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
293 miterDirections.reserve(countVertices * COMPONENTS_MITER);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
294
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
295 for (size_t i = 0; i < layer.GetChainsCount(); i++)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
296 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
297 const PolylineSceneLayer::Chain& chain = layer.GetChain(i);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
298
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
299 if (chain.size() > 1)
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
301 std::vector<Segment> segments;
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
302 for (size_t j = 1; j < chain.size(); j++)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
303 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
304 segments.push_back(Segment(chain, j - 1, j));
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
305 }
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
307 if (layer.IsClosedChain(i))
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
308 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
309 segments.push_back(Segment(chain, chain.size() - 1, 0));
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
310 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
311
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
312 // Try and create nice miters
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
313 for (size_t j = 1; j < segments.size(); j++)
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
315 Segment::CreateMiter(segments[j - 1], segments[j]);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
316 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
317
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
318 if (layer.IsClosedChain(i))
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
319 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
320 Segment::CreateMiter(segments.back(), segments.front());
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
321 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
322
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
323 for (size_t j = 0; j < segments.size(); j++)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
324 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
325 if (!segments[j].IsEmpty())
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
326 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
327 segments[j].AddTriangles(coords, miterDirections, colors, layer.GetColor(i));
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
328 }
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
329 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
333 assert(coords.size() == colors.size());
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
334
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
335 if (!coords.empty())
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
336 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
337 verticesCount_ = coords.size() / COMPONENTS_POSITION;
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
338
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
339 context_.MakeCurrent();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
340 glGenBuffers(3, buffers_);
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
341
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
342 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: 804
diff changeset
343 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * coords.size(), &coords[0], GL_STATIC_DRAW);
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
345 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: 804
diff changeset
346 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * miterDirections.size(), &miterDirections[0], GL_STATIC_DRAW);
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
347
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
348 glBindBuffer(GL_ARRAY_BUFFER, buffers_[2]);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
349 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * colors.size(), &colors[0], GL_STATIC_DRAW);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
350 }
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 OpenGLLinesProgram::Data::~Data()
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
357 if (!context_.IsContextLost() && !IsEmpty())
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 context_.MakeCurrent();
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
360 ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glDeleteBuffers");
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
361 glDeleteBuffers(3, buffers_);
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
362 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365 GLuint OpenGLLinesProgram::Data::GetVerticesBuffer() const
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
366 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
367 if (IsEmpty())
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
368 {
956
a7351ad54960 Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents: 947
diff changeset
369 LOG(ERROR) << "OpenGLLinesProgram::Data::GetVerticesBuffer(): (IsEmpty())";
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
370 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372 else
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
373 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374 return buffers_[0];
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379 GLuint OpenGLLinesProgram::Data::GetMiterDirectionsBuffer() const
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381 if (IsEmpty())
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382 {
956
a7351ad54960 Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents: 947
diff changeset
383 LOG(ERROR) << "OpenGLLinesProgram::Data::GetMiterDirectionsBuffer(): (IsEmpty())";
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386 else
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 return buffers_[1];
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
393 GLuint OpenGLLinesProgram::Data::GetColorsBuffer() const
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
394 {
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
395 if (IsEmpty())
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
396 {
956
a7351ad54960 Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents: 947
diff changeset
397 LOG(ERROR) << "OpenGLLinesProgram::Data::GetColorsBuffer(): (IsEmpty())";
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
398 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
399 }
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
400 else
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
401 {
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
402 return buffers_[2];
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
403 }
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
404 }
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
405
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
406
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407 OpenGLLinesProgram::OpenGLLinesProgram(OpenGL::IOpenGLContext& context) :
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408 context_(context)
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
410 if (!context_.IsContextLost())
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
411 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
412 context_.MakeCurrent();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
413 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: 804
diff changeset
414 program_->CompileShaders(VERTEX_SHADER, FRAGMENT_SHADER);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
415 }
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
416 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
417
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
418 void OpenGLLinesProgram::Apply(const Data& data,
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
419 const AffineTransform2D& transform,
1576
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
420 unsigned int canvasWidth,
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
421 unsigned int canvasHeight,
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
422 bool antialiasing,
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
423 bool scaleIndependantThickness)
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
424 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 804
diff changeset
425 if (!context_.IsContextLost() && !data.IsEmpty())
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
426 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
427 context_.MakeCurrent();
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
428 program_->Use();
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
429
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
430 GLint locationPosition = program_->GetAttributeLocation("a_position");
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
431 GLint locationMiterDirection = program_->GetAttributeLocation("a_miter_direction");
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
432 GLint locationColor = program_->GetAttributeLocation("a_color");
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
433
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
434 float m[16];
1576
92fca2b3ba3d sanitizing the handling of canvas size
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
435 transform.ConvertToOpenGLMatrix(m, canvasWidth, canvasHeight);
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
436
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
437 glUniformMatrix4fv(program_->GetUniformLocation("u_matrix"), 1, GL_FALSE, m);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
438
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
439 glBindBuffer(GL_ARRAY_BUFFER, data.GetVerticesBuffer());
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
440 glEnableVertexAttribArray(locationPosition);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
441 glVertexAttribPointer(locationPosition, COMPONENTS_POSITION, GL_FLOAT, GL_FALSE, 0, 0);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
442
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
443 glBindBuffer(GL_ARRAY_BUFFER, data.GetMiterDirectionsBuffer());
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
444 glEnableVertexAttribArray(locationMiterDirection);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
445 glVertexAttribPointer(locationMiterDirection, COMPONENTS_MITER, GL_FLOAT, GL_FALSE, 0, 0);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
446
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
447 glBindBuffer(GL_ARRAY_BUFFER, data.GetColorsBuffer());
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
448 glEnableVertexAttribArray(locationColor);
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
449 glVertexAttribPointer(locationColor, COMPONENTS_COLOR, GL_FLOAT, GL_FALSE, 0, 0);
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
450
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
451 glUniform1i(program_->GetUniformLocation("u_antialiasing"), (antialiasing ? 1 : 0));
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
452
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
453 const double zoom = transform.ComputeZoom();
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
454 const double thickness = data.GetThickness() / 2.0;
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
455 const double aliasingBorder = 2.0; // Border for antialiasing ramp, in pixels
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
456 assert(aliasingBorder > 0); // Prevent division by zero with "t1"
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
457
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
458 if (scaleIndependantThickness)
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
459 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
460 if (antialiasing)
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
461 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
462 double t1 = std::max(thickness, aliasingBorder);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
463 double t0 = std::max(0.0, thickness - aliasingBorder);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
464
693
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
465 glUniform1f(program_->GetUniformLocation("u_thickness"),
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
466 static_cast<GLfloat>(t1 / zoom));
694
7c6197d9acc9 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 693
diff changeset
467 glUniform1f(program_->GetUniformLocation("u_antialiasing_start"),
693
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
468 static_cast<GLfloat>(t0 / t1));
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
469 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
470 else
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
471 {
694
7c6197d9acc9 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 693
diff changeset
472 glUniform1f(program_->GetUniformLocation("u_thickness"),
693
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
473 static_cast<GLfloat>(thickness / zoom));
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
474 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
475 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
476 else
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
477 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
478 if (antialiasing)
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
479 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
480 double t1 = std::max(thickness, aliasingBorder / zoom);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
481 double t0 = std::max(0.0, thickness - aliasingBorder / zoom);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
482
694
7c6197d9acc9 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 693
diff changeset
483 glUniform1f(program_->GetUniformLocation("u_thickness"),
693
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
484 static_cast<GLfloat>(t1));
694
7c6197d9acc9 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 693
diff changeset
485 glUniform1f(program_->GetUniformLocation("u_antialiasing_start"),
693
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
486 static_cast<GLfloat>(t0 / t1));
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
487 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
488 else
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
489 {
694
7c6197d9acc9 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 693
diff changeset
490 glUniform1f(program_->GetUniformLocation("u_thickness"),
693
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 611
diff changeset
491 static_cast<GLfloat>(thickness));
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
492 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
493 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
494
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
495 if (antialiasing)
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
496 {
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
497 glEnable(GL_BLEND);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
498 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
694
7c6197d9acc9 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 693
diff changeset
499 glDrawArrays(GL_TRIANGLES, 0,
7c6197d9acc9 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 693
diff changeset
500 static_cast<GLsizei>(data.GetVerticesCount()));
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
501 glDisable(GL_BLEND);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
502 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
503 else
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
504 {
694
7c6197d9acc9 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 693
diff changeset
505 glDrawArrays(GL_TRIANGLES, 0,
7c6197d9acc9 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 693
diff changeset
506 static_cast<GLsizei>(data.GetVerticesCount()));
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
507 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
508
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
509 glDisableVertexAttribArray(locationPosition);
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
510 glDisableVertexAttribArray(locationMiterDirection);
804
61ba4b504e9a PolylineSceneLayer now has one color per chain
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 702
diff changeset
511 glDisableVertexAttribArray(locationColor);
592
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
512 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
513 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
514 }
bbe29efd3d1c OpenGLLinesProgram
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
515 }