Debian Codegraph @

avogadro/rendering/widelinegeometry.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 "widelinegeometry.h"
7
8 #include "avogadrogl.h"
9 #include "bufferobject.h"
10 #include "camera.h"
11 #include "shader.h"
12 #include "shaderprogram.h"
13 #include "vertexarrayobject.h"
14 #include "visitor.h"
15
16 #include <iostream>
17
18 namespace {
19 #include "wideline_fs.h"
20 #include "wideline_vs.h"
21 } // namespace
22
23 using Avogadro::Vector3f;
24 using Avogadro::Vector3ub;
25 using Avogadro::Vector4ub;
26 using Avogadro::Core::Array;
27
28 using std::cout;
29 using std::endl;
30
31 namespace Avogadro::Rendering {
32
33 class WideLineGeometry::Private
34 {
35 public:
36 Private() {}
37
38 BufferObject vbo;
39 BufferObject ibo;
40 VertexArrayObject vao;
41
42 Shader vertexShader;
43 Shader fragmentShader;
44 ShaderProgram program;
45 };
46
47 WideLineGeometry::WideLineGeometry()
48 : m_color(255, 0, 0), m_opacity(255), m_dirty(false), d(new Private)
49 {
50 }
51
52 WideLineGeometry::WideLineGeometry(const WideLineGeometry& other)
53 : Drawable(other), m_vertices(other.m_vertices), m_indices(other.m_indices),
54 m_color(other.m_color), m_opacity(other.m_opacity), m_dirty(true),
55 d(new Private)
56 {
57 }
58
59 WideLineGeometry::~WideLineGeometry()
60 {
61 delete d;
62 }
63
64 void WideLineGeometry::accept(Visitor& visitor)
65 {
66 visitor.visit(*this);
67 }
68
69 void WideLineGeometry::update()
70 {
71 if (m_vertices.empty())
72 return;
73
74 // Build and link the shader if it has not been used yet.
75 if (d->vertexShader.type() == Shader::Unknown) {
76 d->vertexShader.setType(Shader::Vertex);
77 d->vertexShader.setSource(wideline_vs);
78 d->fragmentShader.setType(Shader::Fragment);
79 d->fragmentShader.setSource(wideline_fs);
80 if (!d->vertexShader.compile())
81 cout << d->vertexShader.error() << endl;
82 if (!d->fragmentShader.compile())
83 cout << d->fragmentShader.error() << endl;
84 d->program.attachShader(d->vertexShader);
85 d->program.attachShader(d->fragmentShader);
86 if (!d->program.link())
87 cout << d->program.error() << endl;
88
89 d->program.detachShader(d->vertexShader);
90 d->program.detachShader(d->fragmentShader);
91 d->vertexShader.cleanup();
92 d->fragmentShader.cleanup();
93 }
94
95 // Upload vertex and index data if dirty.
96 if (!d->vbo.ready() || m_dirty) {
97 d->vbo.upload(m_vertices, BufferObject::ArrayBuffer);
98 d->ibo.upload(m_indices, BufferObject::ElementArrayBuffer);
99
100 // Set up VAO with vertex attribute bindings
101 d->vao.bind();
102 d->vbo.bind();
103 d->ibo.bind();
104
105 if (!d->program.enableAttributeArray("position"))
106 cout << d->program.error() << endl;
107 if (!d->program.useAttributeArray(
108 "position", PackedVertex::positionOffset(), sizeof(PackedVertex),
109 FloatType, 3, ShaderProgram::NoNormalize)) {
110 cout << d->program.error() << endl;
111 }
112
113 if (!d->program.enableAttributeArray("otherEnd"))
114 cout << d->program.error() << endl;
115 if (!d->program.useAttributeArray(
116 "otherEnd", PackedVertex::otherEndOffset(), sizeof(PackedVertex),
117 FloatType, 3, ShaderProgram::NoNormalize)) {
118 cout << d->program.error() << endl;
119 }
120
121 if (!d->program.enableAttributeArray("color"))
122 cout << d->program.error() << endl;
123 if (!d->program.useAttributeArray("color", PackedVertex::colorOffset(),
124 sizeof(PackedVertex), UCharType, 4,
125 ShaderProgram::Normalize)) {
126 cout << d->program.error() << endl;
127 }
128
129 if (!d->program.enableAttributeArray("widthSide"))
130 cout << d->program.error() << endl;
131 if (!d->program.useAttributeArray(
132 "widthSide", PackedVertex::widthSideOffset(), sizeof(PackedVertex),
133 FloatType, 1, ShaderProgram::NoNormalize)) {
134 cout << d->program.error() << endl;
135 }
136
137 if (!d->program.enableAttributeArray("lineParam"))
138 cout << d->program.error() << endl;
139 if (!d->program.useAttributeArray(
140 "lineParam", PackedVertex::lineParamOffset(), sizeof(PackedVertex),
141 FloatType, 1, ShaderProgram::NoNormalize)) {
142 cout << d->program.error() << endl;
143 }
144
145 d->vao.release();
146
147 m_dirty = false;
148 }
149 }
150
151 void WideLineGeometry::render(const Camera& camera)
152 {
153 if (m_vertices.empty() || m_indices.empty())
154 return;
155
156 // Prepare the VBOs, IBO, VAO, and shader program if necessary.
157 update();
158
159 if (!d->program.bind())
160 cout << d->program.error() << endl;
161
162 // Bind the VAO
163 d->vao.bind();
164
165 // Set up our uniforms
166 if (!d->program.setUniformValue("modelView", camera.modelView().matrix()))
167 cout << d->program.error() << endl;
168 if (!d->program.setUniformValue("projection", camera.projection().matrix()))
169 cout << d->program.error() << endl;
170
171 // Render the quads
172 glDrawRangeElements(GL_TRIANGLES, 0,
173 static_cast<GLuint>(m_vertices.size() - 1),
174 static_cast<GLsizei>(m_indices.size()), GL_UNSIGNED_INT,
175 reinterpret_cast<const GLvoid*>(0));
176
177 d->vao.release();
178 d->program.release();
179 }
180
181 void WideLineGeometry::clear()
182 {
183 m_vertices.clear();
184 m_indices.clear();
185 m_dirty = true;
186 }
187
188 void WideLineGeometry::reserve(size_t lineCount)
189 {
190 m_vertices.reserve(lineCount * 4);
191 m_indices.reserve(lineCount * 6);
192 }
193
194 void WideLineGeometry::addLine(const Vector3f& start, const Vector3f& end,
195 const Vector3ub& colorStart,
196 const Vector3ub& colorEnd, float lineWidth)
197 {
198 float halfWidth = lineWidth * 0.5f;
199 auto baseIndex = static_cast<unsigned int>(m_vertices.size());
200
201 Vector4ub rgba1(colorStart[0], colorStart[1], colorStart[2], m_opacity);
202 Vector4ub rgba2(colorEnd[0], colorEnd[1], colorEnd[2], m_opacity);
203
204 // Four vertices per line segment: 2 endpoints x 2 sides.
205 // End vertices swap position/otherEnd, which reverses the cross product
206 // in the vertex shader. Negate widthSide at the end to compensate,
207 // keeping the quad a proper rectangle instead of a bowtie.
208 // lineParam = 0 for solid lines (never triggers discard).
209 m_vertices.emplace_back(start, end, rgba1, -halfWidth, 0.0f);
210 m_vertices.emplace_back(start, end, rgba1, halfWidth, 0.0f);
211 m_vertices.emplace_back(end, start, rgba2, halfWidth, 0.0f);
212 m_vertices.emplace_back(end, start, rgba2, -halfWidth, 0.0f);
213
214 // Two triangles forming a quad
215 m_indices.push_back(baseIndex + 0);
216 m_indices.push_back(baseIndex + 1);
217 m_indices.push_back(baseIndex + 2);
218
219 m_indices.push_back(baseIndex + 2);
220 m_indices.push_back(baseIndex + 1);
221 m_indices.push_back(baseIndex + 3);
222
223 m_dirty = true;
224 }
225
226 void WideLineGeometry::addLine(const Vector3f& start, const Vector3f& end,
227 const Vector3ub& color, float lineWidth)
228 {
229 addLine(start, end, color, color, lineWidth);
230 }
231
232 void WideLineGeometry::addLineStrip(const Array<Vector3f>& vertices,
233 const Vector3ub& color, float lineWidth)
234 {
235 if (vertices.size() < 2)
236 return;
237 for (size_t i = 0; i + 1 < vertices.size(); ++i)
238 addLine(vertices[i], vertices[i + 1], color, lineWidth);
239 }
240
241 void WideLineGeometry::addLineStrip(const Array<Vector3f>& vertices,
242 const Array<Vector3ub>& colors,
243 float lineWidth)
244 {
245 if (vertices.size() < 2 || vertices.size() != colors.size())
246 return;
247 for (size_t i = 0; i + 1 < vertices.size(); ++i)
248 addLine(vertices[i], vertices[i + 1], colors[i], colors[i + 1], lineWidth);
249 }
250
251 void WideLineGeometry::addDashedLine(const Vector3f& start, const Vector3f& end,
252 const Vector3ub& color, float lineWidth,
253 int dashCount)
254 {
255 if (dashCount <= 0)
256 return;
257
258 float halfWidth = lineWidth * 0.5f;
259 auto baseIndex = static_cast<unsigned int>(m_vertices.size());
260
261 Vector4ub rgba(color[0], color[1], color[2], m_opacity);
262
263 // lineParam goes from 0 at start to dashCount*2 at end.
264 // The fragment shader discards when mod(lineParam, 2.0) > 1.0,
265 // producing exactly dashCount dashes with gaps between them.
266 float paramEnd = static_cast<float>(dashCount * 2);
267
268 // Use a small epsilon so the very start is not discarded
269 m_vertices.emplace_back(start, end, rgba, -halfWidth, 0.01f);
270 m_vertices.emplace_back(start, end, rgba, halfWidth, 0.01f);
271
272 // Negate widthSide at the end to compensate for reversed cross product
273 m_vertices.emplace_back(end, start, rgba, halfWidth, paramEnd);
274 m_vertices.emplace_back(end, start, rgba, -halfWidth, paramEnd);
275
276 m_indices.push_back(baseIndex + 0);
277 m_indices.push_back(baseIndex + 1);
278 m_indices.push_back(baseIndex + 2);
279
280 m_indices.push_back(baseIndex + 2);
281 m_indices.push_back(baseIndex + 1);
282 m_indices.push_back(baseIndex + 3);
283
284 m_dirty = true;
285 }
286
287 } // namespace Avogadro::Rendering