File: Edge_container.cpp

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (209 lines) | stat: -rw-r--r-- 7,378 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <CGAL/Three/Edge_container.h>
#include <CGAL/Three/Three.h>


typedef Viewer_interface VI;
using namespace CGAL::Three;

struct Edge_d{

  Edge_d()
    :
      plane(QVector4D())
  {
    f_matrix.setToIdentity();
  }

  QVector4D plane;
  QMatrix4x4 f_matrix;
  GLfloat width;
  QVector2D viewport;
  bool is_surface;
};

Edge_container::Edge_container(int program, bool indexed)
  :Primitive_container(program, indexed),
    d(new Edge_d())
{
  std::vector<Vbo*> vbos(NbOfVbos, nullptr);
  setVbos(vbos);

}

void Edge_container::initGL(Viewer_interface *viewer)
{
  viewer->makeCurrent();
  if(viewer->isSharing())
  {
    if(!getVao(viewer))
      setVao(viewer, new Vao(getVao(Three::mainViewer()),
                             viewer->getShaderProgram(getProgram())));
  }
  else
  {
    if(!getVao(viewer))
      setVao(viewer, new Vao(viewer->getShaderProgram(getProgram())));
    if(isDataIndexed())
    {
      if(!getVbo(Vertices))
        setVbo(Vertices,
               new Vbo("vertex",
                       Vbo::GEOMETRY));
      if(!getVbo(Indices))
        setVbo(Indices,
               new Vbo("indices",
                       Vbo::GEOMETRY,
                       QOpenGLBuffer::IndexBuffer));
      getVao(viewer)->addVbo(getVbo(Vertices));
      getVao(viewer)->addVbo(getVbo(Indices));
    }
    else
    {
      if(!getVbo(Vertices))
        setVbo(Vertices,
               new Vbo("vertex",
                       Vbo::GEOMETRY));
      if(!getVbo(Colors))
        setVbo(Colors,
               new Vbo("colors",
                       Vbo::COLORS));
      setVao(viewer, new Vao(viewer->getShaderProgram(getProgram())));
      getVao(viewer)->addVbo(getVbo(Vertices));
      getVao(viewer)->addVbo(getVbo(Colors));

      if(viewer->getShaderProgram(getProgram())->property("hasNormals").toBool())
      {
        if(!getVbo(Normals))
          setVbo(Normals,
                 new Vbo("normals",
                         Vbo::NORMALS));
        getVao(viewer)->addVbo(getVbo(Normals));
      }
      if(viewer->getShaderProgram(getProgram())->property("hasRadius").toBool())
      {
        if(!getVbo(Radius))
          setVbo(Radius,
                 new Vbo("radius",
                         Vbo::NOT_INSTANCED,
                         QOpenGLBuffer::VertexBuffer, GL_FLOAT, 0, 1));
        getVao(viewer)->addVbo(getVbo(Radius));
      }
      if(viewer->getShaderProgram(getProgram())->property("hasCenter").toBool())
      {
        if(!getVbo(Centers))
          setVbo(Centers,
                 new Vbo("center",
                  viewer->getShaderProgram(getProgram())->property("isInstanced").toBool()
                  ? Vbo::NOT_INSTANCED
                  : Vbo::GEOMETRY));
        getVao(viewer)->addVbo(getVbo(Centers));
      }
      if(viewer->getShaderProgram(getProgram())->property("hasTexture").toBool())
      {
        if(!getVbo(Texture_map))
          setVbo(Texture_map,
                 new Vbo("v_texCoord",
                         Vbo::GEOMETRY,
                         QOpenGLBuffer::VertexBuffer,
                         GL_FLOAT, 0, 2));
        getVao(viewer)->addVbo(getVbo(Texture_map));
        if(!getTexture())
          setTexture(new Texture());
      }
      if(viewer->getShaderProgram(getProgram())->property("hasSubdomainIndicesValues").toBool())
      {
        if(!getVbo(Subdomain_indices))
          setVbo(Subdomain_indices,
                 new Vbo("subdomain_in",
                         Vbo::GEOMETRY,
                         QOpenGLBuffer::VertexBuffer, GL_FLOAT, 0, 2));
        getVao(viewer)->addVbo(getVbo(Subdomain_indices));
      }
    }
  }
  setGLInit(viewer, true);
}

void Edge_container::initializeBuffers(Viewer_interface *viewer)
{
  Primitive_container::initializeBuffers(viewer);
  if(viewer->getShaderProgram(getProgram())->property("isInstanced").toBool())
  {
    getVao(viewer)->bind();
    if(viewer->getShaderProgram(getProgram())->property("hasCenter").toBool())
      viewer->glVertexAttribDivisor(getVao(viewer)->program->attributeLocation("center"), 1);
    if(viewer->getShaderProgram(getProgram())->property("hasRadius").toBool())
      viewer->glVertexAttribDivisor(getVao(viewer)->program->attributeLocation("radius"), 1);
    viewer->glVertexAttribDivisor(getVao(viewer)->program->attributeLocation("colors"), 1);
    getVao(viewer)->release();
  }
}

void Edge_container::draw(Viewer_interface *viewer,
                          bool is_color_uniform)

{
  bindUniformValues(viewer);
  if(isDataIndexed())
  {
    getVao(viewer)->bind();
    if(is_color_uniform)
      getVao(viewer)->program->setAttributeValue("colors", getColor());
    if(getVao(viewer)->program->property("hasFMatrix").toBool())
      getVao(viewer)->program->setUniformValue("f_matrix", getFrameMatrix());
    getVbo(Indices)->bind();
    viewer->glDrawElements(GL_LINES, static_cast<GLuint>(getIdxSize()),
                           GL_UNSIGNED_INT, nullptr);
    getVbo(Indices)->release();
    getVao(viewer)->release();
  }
  else
  {
    getVao(viewer)->bind();
    if(getVao(viewer)->program->property("hasTexture").toBool()){
      viewer->glActiveTexture(GL_TEXTURE0);
      viewer->glBindTexture(GL_TEXTURE_2D, getTextureId());
    }
    if(is_color_uniform)
      getVao(viewer)->program->setAttributeValue("colors", getColor());
    if(viewer->getShaderProgram(getProgram())->property("hasCutPlane").toBool())
      getVao(viewer)->program->setUniformValue("cutplane", d->plane);
    if(getVao(viewer)->program->property("hasSurfaceMode").toBool())
      getVao(viewer)->program->setUniformValue("is_surface", d->is_surface);
    if(getVao(viewer)->program->property("hasFMatrix").toBool())
      getVao(viewer)->program->setUniformValue("f_matrix", getFrameMatrix());

    if(getVao(viewer)->program->property("hasViewport").toBool())
    {
      getVao(viewer)->program->setUniformValue("viewport", getViewport());
      getVao(viewer)->program->setUniformValue("near",static_cast<GLfloat>(viewer->camera()->zNear()));
      getVao(viewer)->program->setUniformValue("far",static_cast<GLfloat>(viewer->camera()->zFar()));
    }
    if(getVao(viewer)->program->property("hasWidth").toBool())
      getVao(viewer)->program->setUniformValue("width", getWidth());

    if(viewer->getShaderProgram(getProgram())->property("isInstanced").toBool())
    {
      viewer->glDrawArraysInstanced(GL_LINES, 0,
                                    static_cast<GLsizei>(getFlatDataSize()/getTupleSize()),
                                    static_cast<GLsizei>(getCenterSize()/3));
    }
    else
    {
      viewer->glDrawArrays(GL_LINES, 0, static_cast<GLsizei>(getFlatDataSize()/getTupleSize()));
    }
    getVao(viewer)->release();

  }
}

QVector4D Edge_container::getPlane() const { return d->plane; }
QMatrix4x4 Edge_container::getFrameMatrix() const { return d->f_matrix; }
GLfloat Edge_container::getWidth() const { return d->width; }
QVector2D Edge_container::getViewport() const { return d->viewport; }

void Edge_container::setPlane(const QVector4D& p) { d->plane = p; }
void Edge_container::setFrameMatrix(const QMatrix4x4& m) { d->f_matrix = m; }
void Edge_container::setWidth(const GLfloat & w) { d->width = w; }
void Edge_container::setViewport(const QVector2D & v) { d-> viewport = v; }
void Edge_container::setIsSurface  (const bool b) { d->is_surface = b; }