ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
triangle_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/triangle_painter.h"
33 
34 #include "util/opengl_utils.h"
35 
36 namespace colmap {
37 
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/triangles.v.glsl");
55  shader_program_.addShaderFromSourceFile(QOpenGLShader::Fragment,
56  ":/shaders/triangles.f.glsl");
57  shader_program_.link();
58  shader_program_.bind();
59 
60  vao_.create();
61  vbo_.create();
62 
63 #if DEBUG
64  glDebugLog();
65 #endif
66 }
67 
68 void TrianglePainter::Upload(const std::vector<TrianglePainter::Data>& data) {
69  num_geoms_ = data.size();
70  if (num_geoms_ == 0) {
71  return;
72  }
73 
74  vao_.bind();
75  vbo_.bind();
76 
77  // Upload data array to GPU
78  vbo_.setUsagePattern(QOpenGLBuffer::DynamicDraw);
79  vbo_.allocate(data.data(),
80  static_cast<int>(data.size() * sizeof(TrianglePainter::Data)));
81 
82  // in_position
83  shader_program_.enableAttributeArray("a_position");
84  shader_program_.setAttributeBuffer("a_position", GL_FLOAT, 0, 3,
85  sizeof(PointPainter::Data));
86 
87  // in_color
88  shader_program_.enableAttributeArray("a_color");
89  shader_program_.setAttributeBuffer("a_color", GL_FLOAT, 3 * sizeof(GLfloat),
90  4, sizeof(PointPainter::Data));
91 
92  // Make sure they are not changed from the outside
93  vbo_.release();
94  vao_.release();
95 
96 #if DEBUG
97  glDebugLog();
98 #endif
99 }
100 
101 void TrianglePainter::Render(const QMatrix4x4& pmv_matrix) {
102  if (num_geoms_ == 0) {
103  return;
104  }
105 
106  shader_program_.bind();
107  vao_.bind();
108 
109  shader_program_.setUniformValue("u_pmv_matrix", pmv_matrix);
110 
111  QOpenGLFunctions* gl_funcs = QOpenGLContext::currentContext()->functions();
112  gl_funcs->glDrawArrays(GL_TRIANGLES, 0, (GLsizei)(3 * num_geoms_));
113 
114  // Make sure the VAO is not changed from the outside
115  vao_.release();
116 
117 #if DEBUG
118  glDebugLog();
119 #endif
120 }
121 
122 } // namespace colmap
void Upload(const std::vector< TrianglePainter::Data > &data)
void Render(const QMatrix4x4 &pmv_matrix)
GraphType data
Definition: graph_cut.cc:138
#define glDebugLog()
Definition: opengl_utils.h:26