ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
line_painter.cc
Go to the documentation of this file.
1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 // its contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #include "ui/line_painter.h"
33 
34 #include "util/opengl_utils.h"
35 
36 namespace colmap {
37 
38 LinePainter::LinePainter() : num_geoms_(0) {}
39 
41  vao_.destroy();
42  vbo_.destroy();
43 }
44 
46  vao_.destroy();
47  vbo_.destroy();
48  if (shader_program_.isLinked()) {
49  shader_program_.release();
50  shader_program_.removeAllShaders();
51  }
52 
53  shader_program_.addShaderFromSourceFile(QOpenGLShader::Vertex,
54  ":/shaders/lines.v.glsl");
55  shader_program_.addShaderFromSourceFile(QOpenGLShader::Geometry,
56  ":/shaders/lines.g.glsl");
57  shader_program_.addShaderFromSourceFile(QOpenGLShader::Fragment,
58  ":/shaders/lines.f.glsl");
59  shader_program_.link();
60  shader_program_.bind();
61 
62  vao_.create();
63  vbo_.create();
64 
65 #if DEBUG
66  glDebugLog();
67 #endif
68 }
69 
70 void LinePainter::Upload(const std::vector<LinePainter::Data>& data) {
71  num_geoms_ = data.size();
72  if (num_geoms_ == 0) {
73  return;
74  }
75 
76  vao_.bind();
77  vbo_.bind();
78 
79  // Upload data array to GPU
80  vbo_.setUsagePattern(QOpenGLBuffer::DynamicDraw);
81  vbo_.allocate(data.data(),
82  static_cast<int>(data.size() * sizeof(LinePainter::Data)));
83 
84  // in_position
85  shader_program_.enableAttributeArray("a_pos");
86  shader_program_.setAttributeBuffer("a_pos", GL_FLOAT, 0, 3,
87  sizeof(PointPainter::Data));
88 
89  // in_color
90  shader_program_.enableAttributeArray("a_color");
91  shader_program_.setAttributeBuffer("a_color", GL_FLOAT, 3 * sizeof(GLfloat),
92  4, sizeof(PointPainter::Data));
93 
94  // Make sure they are not changed from the outside
95  vbo_.release();
96  vao_.release();
97 
98 #if DEBUG
99  glDebugLog();
100 #endif
101 }
102 
103 void LinePainter::Render(const QMatrix4x4& pmv_matrix, const int width,
104  const int height, const float line_width) {
105  if (num_geoms_ == 0) {
106  return;
107  }
108 
109  shader_program_.bind();
110  vao_.bind();
111 
112  shader_program_.setUniformValue("u_pmv_matrix", pmv_matrix);
113  shader_program_.setUniformValue("u_inv_viewport",
114  QVector2D(1.0f / width, 1.0f / height));
115  shader_program_.setUniformValue("u_line_width", line_width);
116 
117  QOpenGLFunctions* gl_funcs = QOpenGLContext::currentContext()->functions();
118  gl_funcs->glDrawArrays(GL_LINES, 0, (GLsizei)(2 * num_geoms_));
119 
120  // Make sure the VAO is not changed from the outside
121  vao_.release();
122 
123 #if DEBUG
124  glDebugLog();
125 #endif
126 }
127 
128 } // namespace colmap
int width
int height
void Render(const QMatrix4x4 &pmv_matrix, const int width, const int height, const float line_width)
void Upload(const std::vector< LinePainter::Data > &data)
Definition: line_painter.cc:70
GraphType data
Definition: graph_cut.cc:138
::ccHObject Geometry
Definition: Geometry.h:18
#define glDebugLog()
Definition: opengl_utils.h:26