Debian Codegraph @

avogadro/rendering/dashedlinegeometry.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "dashedlinegeometry.h"
7
8 #include "avogadrogl.h"
9 #include "bufferobject.h"
10 #include "camera.h"
11 #include "scene.h"
12 #include "shader.h"
13 #include "shaderprogram.h"
14 #include "vertexarrayobject.h"
15 #include "visitor.h"
16
17 #include <avogadro/core/matrix.h>
18 #include <avogadro/core/vector.h>
19
20 #include <iostream>
21 #include <limits>
22
23 namespace {
24 #include "dashedline_fs.h"
25 #include "dashedline_vs.h"
26 } // namespace
27
28 using Avogadro::Vector3f;
29 using Avogadro::Vector3ub;
30 using Avogadro::Vector4ub;
31
32 using std::cout;
33 using std::endl;
34
35 namespace Avogadro::Rendering {
36
37 class DashedLineGeometry::Private
38 {
39 public:
40 Private() {}
41
42 BufferObject vbo;
43 VertexArrayObject vao;
44
45 Shader vertexShader;
46 Shader fragmentShader;
47 ShaderProgram program;
48 };
49
50 DashedLineGeometry::DashedLineGeometry()
51 : m_lineWidth(1.0), m_lineCount(0), m_color(255, 0, 0), m_opacity(255),
52 m_dirty(false), d(new Private)
53 {
54 }
55
56 DashedLineGeometry::DashedLineGeometry(const DashedLineGeometry& other)
57 : Drawable(other), m_vertices(other.m_vertices),
58 m_lineWidth(other.m_lineWidth), m_lineCount(other.m_lineCount),
59 m_dirty(true), d(new Private)
60 {
61 }
62
63 DashedLineGeometry::~DashedLineGeometry()
64 {
65 delete d;
66 }
67
68 void DashedLineGeometry::accept(Visitor& visitor)
69 {
70 visitor.visit(*this);
71 }
72
73 void DashedLineGeometry::update()
74 {
75 if (m_vertices.empty())
76 return;
77
78 // Build and link the shader if it has not been used yet.
79 // Must be done before VAO setup since we need the program for attribute
80 // setup.
81 if (d->vertexShader.type() == Shader::Unknown) {
82 d->vertexShader.setType(Shader::Vertex);
83 d->vertexShader.setSource(dashedline_vs);
84 d->fragmentShader.setType(Shader::Fragment);
85 d->fragmentShader.setSource(dashedline_fs);
86 if (!d->vertexShader.compile())
87 cout << d->vertexShader.error() << endl;
88 if (!d->fragmentShader.compile())
89 cout << d->fragmentShader.error() << endl;
90 d->program.attachShader(d->vertexShader);
91 d->program.attachShader(d->fragmentShader);
92 if (!d->program.link())
93 cout << d->program.error() << endl;
94
95 d->program.detachShader(d->vertexShader);
96 d->program.detachShader(d->fragmentShader);
97 d->vertexShader.cleanup();
98 d->fragmentShader.cleanup();
99 }
100
101 // Check if the VBOs are ready, if not get them ready.
102 if (!d->vbo.ready() || m_dirty) {
103 d->vbo.upload(m_vertices, BufferObject::ArrayBuffer);
104
105 // Set up VAO with vertex attribute bindings (OpenGL 4.0 core profile)
106 d->vao.bind();
107 d->vbo.bind();
108
109 if (!d->program.enableAttributeArray("vertex"))
110 cout << d->program.error() << endl;
111 if (!d->program.useAttributeArray("vertex", PackedVertex::vertexOffset(),
112 sizeof(PackedVertex), FloatType, 3,
113 ShaderProgram::NoNormalize)) {
114 cout << d->program.error() << endl;
115 }
116 if (!d->program.enableAttributeArray("color"))
117 cout << d->program.error() << endl;
118 if (!d->program.useAttributeArray("color", PackedVertex::colorOffset(),
119 sizeof(PackedVertex), UCharType, 4,
120 ShaderProgram::Normalize)) {
121 cout << d->program.error() << endl;
122 }
123
124 d->vao.release();
125
126 m_dirty = false;
127 }
128 }
129
130 void DashedLineGeometry::render(const Camera& camera)
131 {
132 if (m_vertices.empty())
133 return;
134
135 // Prepare the VBO, VAO, and shader program if necessary.
136 update();
137
138 if (!d->program.bind())
139 cout << d->program.error() << endl;
140
141 // Bind the VAO (captures all vertex attribute state)
142 d->vao.bind();
143
144 // Set up our uniforms (model-view and projection matrices right now).
145 if (!d->program.setUniformValue("modelView", camera.modelView().matrix())) {
146 cout << d->program.error() << endl;
147 }
148 if (!d->program.setUniformValue("projection", camera.projection().matrix())) {
149 cout << d->program.error() << endl;
150 }
151
152 glEnable(GL_LINE_SMOOTH);
153 glLineWidth(m_lineWidth);
154
155 // Render the lines using the shader and VAO.
156 glDrawArrays(GL_LINES, static_cast<GLint>(0),
157 static_cast<GLsizei>(m_vertices.size()));
158
159 glDisable(GL_LINE_SMOOTH);
160
161 d->vao.release();
162 d->program.release();
163 }
164
165 void DashedLineGeometry::clear()
166 {
167 m_vertices.clear();
168 m_dirty = true;
169 }
170
171 size_t DashedLineGeometry::addDashedLine(const Vector3f& start,
172 const Vector3f& end,
173 const Vector4ub& rgba, int dashCount)
174 {
175 const int vertexCount = 2 * dashCount;
176 Vector3f delta = (end - start) / (vertexCount - 1);
177 Vector3f current = start;
178 for (int n = 0; n < vertexCount; n++) {
179 m_vertices.push_back(PackedVertex(current, rgba));
180 current += delta;
181 }
182
183 m_lineCount++;
184
185 m_dirty = true;
186 return m_lineCount - 1;
187 }
188
189 size_t DashedLineGeometry::addDashedLine(const Vector3f& start,
190 const Vector3f& end,
191 const Vector3ub& rgb, int dashCount)
192 {
193 Vector4ub rgba = Vector4ub(rgb(0), rgb(1), rgb(2), m_opacity);
194 return addDashedLine(start, end, rgba, dashCount);
195 }
196
197 size_t DashedLineGeometry::addDashedLine(const Vector3f& start,
198 const Vector3f& end, int dashCount)
199 {
200 return addDashedLine(start, end, m_color, dashCount);
201 }
202
203 } // namespace Avogadro::Rendering