Debian Codegraph @

avogadro/rendering/widelinegeometry.h

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 #ifndef AVOGADRO_RENDERING_WIDELINEGEOMETRY_H
7 #define AVOGADRO_RENDERING_WIDELINEGEOMETRY_H
8
9 #include "drawable.h"
10
11 #include <avogadro/core/array.h>
12
13 #include <vector>
14
15 namespace Avogadro {
16 namespace Rendering {
17
18 /**
19 * @class WideLineGeometry widelinegeometry.h
20 * <avogadro/rendering/widelinegeometry.h>
21 * @brief Renders line segments as camera-facing quads for wide line support.
22 *
23 * OpenGL 4.0 Core Profile does not guarantee glLineWidth() > 1.0. This class
24 * renders each line segment as a quad (two triangles) whose width is computed
25 * in the vertex shader, producing correct wide lines on all platforms.
26 *
27 * Supports both solid and dashed lines. Dashing is implemented in the fragment
28 * shader using a per-vertex lineParam attribute — no extra geometry is needed.
29 *
30 * Width is specified in world-space units.
31 */
32
33 class AVOGADRORENDERING_EXPORT WideLineGeometry : public Drawable
34 {
35 public:
36 struct PackedVertex
37 { // 36 bytes total:
38 Vector3f position; // 12 bytes - this endpoint
39 Vector3f otherEnd; // 12 bytes - the other endpoint
40 Vector4ub color; // 4 bytes - RGBA
41 float widthSide; // 4 bytes - half-width * side (+1 or -1)
42 float lineParam; // 4 bytes - dash parameter (0 = solid)
43
44 PackedVertex() = default;
45 PackedVertex(const Vector3f& pos, const Vector3f& other, const Vector4ub& c,
46 float ws, float lp = 0.0f)
47 : position(pos), otherEnd(other), color(c), widthSide(ws), lineParam(lp)
48 {
49 }
50
51 static int positionOffset() { return 0; }
52 static int otherEndOffset() { return static_cast<int>(sizeof(Vector3f)); }
53 static int colorOffset() { return static_cast<int>(2 * sizeof(Vector3f)); }
54 static int widthSideOffset()
55 {
56 return static_cast<int>(2 * sizeof(Vector3f) + sizeof(Vector4ub));
57 }
58 static int lineParamOffset()
59 {
60 return static_cast<int>(2 * sizeof(Vector3f) + sizeof(Vector4ub) +
61 sizeof(float));
62 }
63 };
64
65 /** Scale factor converting legacy pixel-based widths to world-space units. */
66 static constexpr float lineWidthScale = 0.035f;
67
68 WideLineGeometry();
69 WideLineGeometry(const WideLineGeometry& other);
70 ~WideLineGeometry() override;
71
72 WideLineGeometry& operator=(WideLineGeometry);
73 friend void swap(WideLineGeometry& lhs, WideLineGeometry& rhs);
74
75 /**
76 * Accept a visit from our friendly visitor.
77 */
78 void accept(Visitor&) override;
79
80 /**
81 * @brief Render the wide line geometry.
82 * @param camera The current camera to be used for rendering.
83 */
84 void render(const Camera& camera) override;
85
86 /**
87 * Clear the contents of the node.
88 */
89 void clear() override;
90
91 /**
92 * Add a single solid line segment.
93 * @param start Start position of the line segment.
94 * @param end End position of the line segment.
95 * @param colorStart Color at the start.
96 * @param colorEnd Color at the end.
97 * @param lineWidth Width of the line in world-space units.
98 */
99 void addLine(const Vector3f& start, const Vector3f& end,
100 const Vector3ub& colorStart, const Vector3ub& colorEnd,
101 float lineWidth);
102
103 /**
104 * Add a single solid line segment with a uniform color.
105 */
106 void addLine(const Vector3f& start, const Vector3f& end,
107 const Vector3ub& color, float lineWidth);
108
109 /**
110 * Add a connected strip of line segments from consecutive vertices.
111 * Each adjacent pair of vertices becomes one line segment.
112 * @param vertices The vertices forming the line strip.
113 * @param color Uniform color for the strip.
114 * @param lineWidth Width of the lines in world-space units.
115 */
116 void addLineStrip(const Core::Array<Vector3f>& vertices,
117 const Vector3ub& color, float lineWidth);
118
119 /**
120 * Add a connected strip with per-vertex colors.
121 */
122 void addLineStrip(const Core::Array<Vector3f>& vertices,
123 const Core::Array<Vector3ub>& colors, float lineWidth);
124
125 /**
126 * Add a dashed line segment.
127 * @param start Start position.
128 * @param end End position.
129 * @param color Line color.
130 * @param lineWidth Width in world-space units.
131 * @param dashCount Number of dashes.
132 */
133 void addDashedLine(const Vector3f& start, const Vector3f& end,
134 const Vector3ub& color, float lineWidth, int dashCount);
135
136 /**
137 * The default color of the lines.
138 * @{
139 */
140 void setColor(const Vector3ub& c) { m_color = c; }
141 Vector3ub color() const { return m_color; }
142 /** @} */
143
144 /**
145 * The default opacity of the lines.
146 * @{
147 */
148 void setOpacity(unsigned char opacity_) { m_opacity = opacity_; }
149 unsigned char opacity() const { return m_opacity; }
150 /** @} */
151
152 /**
153 * Reserve space for the given number of line segments.
154 * Each segment uses 4 vertices and 6 indices.
155 */
156 void reserve(size_t lineCount);
157
158 /** The vertex array (for geometry bounds computation). */
159 const std::vector<PackedVertex>& vertices() const { return m_vertices; }
160
161 /** The number of line segments. */
162 size_t lineCount() const { return m_vertices.size() / 4; }
163
164 private:
165 /**
166 * @brief Update the VBOs, IBOs etc ready for rendering.
167 */
168 void update();
169
170 std::vector<PackedVertex> m_vertices;
171 std::vector<unsigned int> m_indices;
172
173 Vector3ub m_color;
174 unsigned char m_opacity;
175
176 bool m_dirty;
177
178 class Private;
179 Private* d;
180 };
181
182 inline WideLineGeometry& WideLineGeometry::operator=(WideLineGeometry other)
183 {
184 using std::swap;
185 swap(*this, other);
186 return *this;
187 }
188
189 inline void swap(WideLineGeometry& lhs, WideLineGeometry& rhs)
190 {
191 using std::swap;
192 swap(static_cast<Drawable&>(lhs), static_cast<Drawable&>(rhs));
193 swap(lhs.m_vertices, rhs.m_vertices);
194 swap(lhs.m_indices, rhs.m_indices);
195 swap(lhs.m_color, rhs.m_color);
196 swap(lhs.m_opacity, rhs.m_opacity);
197 lhs.m_dirty = rhs.m_dirty = true;
198 }
199
200 } // End namespace Rendering
201 } // End namespace Avogadro
202
203 #endif // AVOGADRO_RENDERING_WIDELINEGEOMETRY_H