| 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 "linestripgeometry.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 "linestrip_fs.h" |
| 25 |
#include "linestrip_vs.h" |
| 26 |
} // namespace |
| 27 |
|
| 28 |
using Avogadro::Vector3f; |
| 29 |
using Avogadro::Vector3ub; |
| 30 |
using Avogadro::Vector4ub; |
| 31 |
using Avogadro::Core::Array; |
| 32 |
|
| 33 |
using std::cout; |
| 34 |
using std::endl; |
| 35 |
|
| 36 |
namespace Avogadro::Rendering { |
| 37 |
|
| 38 |
const size_t LineStripGeometry::InvalidIndex = |
| 39 |
std::numeric_limits<size_t>::max(); |
| 40 |
|
| 41 |
class LineStripGeometry::Private |
| 42 |
{ |
| 43 |
public: |
| 44 |
Private() {} |
| 45 |
|
| 46 |
BufferObject vbo; |
| 47 |
VertexArrayObject vao; |
| 48 |
|
| 49 |
Shader vertexShader; |
| 50 |
Shader fragmentShader; |
| 51 |
ShaderProgram program; |
| 52 |
}; |
| 53 |
|
| 54 |
LineStripGeometry::LineStripGeometry() |
| 55 |
: m_color(255, 0, 0), m_opacity(255), m_dirty(false), d(new Private) |
| 56 |
{ |
| 57 |
} |
| 58 |
|
| 59 |
LineStripGeometry::LineStripGeometry(const LineStripGeometry& other) |
| 60 |
: Drawable(other), m_vertices(other.m_vertices), |
| 61 |
m_lineStarts(other.m_lineStarts), m_lineWidths(other.m_lineWidths), |
| 62 |
m_color(other.m_color), m_opacity(other.m_opacity), m_dirty(true), |
| 63 |
d(new Private) |
| 64 |
{ |
| 65 |
} |
| 66 |
|
| 67 |
LineStripGeometry::~LineStripGeometry() |
| 68 |
{ |
| 69 |
delete d; |
| 70 |
} |
| 71 |
|
| 72 |
void LineStripGeometry::accept(Visitor& visitor) |
| 73 |
{ |
| 74 |
visitor.visit(*this); |
| 75 |
} |
| 76 |
|
| 77 |
void LineStripGeometry::update() |
| 78 |
{ |
| 79 |
if (m_vertices.empty()) |
| 80 |
return; |
| 81 |
|
| 82 |
// Build and link the shader if it has not been used yet. |
| 83 |
// Must be done before VAO setup since we need the program for attribute |
| 84 |
// setup. |
| 85 |
if (d->vertexShader.type() == Shader::Unknown) { |
| 86 |
d->vertexShader.setType(Shader::Vertex); |
| 87 |
d->vertexShader.setSource(linestrip_vs); |
| 88 |
d->fragmentShader.setType(Shader::Fragment); |
| 89 |
d->fragmentShader.setSource(linestrip_fs); |
| 90 |
if (!d->vertexShader.compile()) |
| 91 |
cout << d->vertexShader.error() << endl; |
| 92 |
if (!d->fragmentShader.compile()) |
| 93 |
cout << d->fragmentShader.error() << endl; |
| 94 |
d->program.attachShader(d->vertexShader); |
| 95 |
d->program.attachShader(d->fragmentShader); |
| 96 |
if (!d->program.link()) |
| 97 |
cout << d->program.error() << endl; |
| 98 |
|
| 99 |
d->program.detachShader(d->vertexShader); |
| 100 |
d->program.detachShader(d->fragmentShader); |
| 101 |
d->vertexShader.cleanup(); |
| 102 |
d->fragmentShader.cleanup(); |
| 103 |
} |
| 104 |
|
| 105 |
// Check if the VBOs are ready, if not get them ready. |
| 106 |
if (!d->vbo.ready() || m_dirty) { |
| 107 |
d->vbo.upload(m_vertices, BufferObject::ArrayBuffer); |
| 108 |
|
| 109 |
// Set up VAO with vertex attribute bindings (OpenGL 4.0 core profile) |
| 110 |
d->vao.bind(); |
| 111 |
d->vbo.bind(); |
| 112 |
|
| 113 |
if (!d->program.enableAttributeArray("vertex")) |
| 114 |
cout << d->program.error() << endl; |
| 115 |
if (!d->program.useAttributeArray("vertex", PackedVertex::vertexOffset(), |
| 116 |
sizeof(PackedVertex), FloatType, 3, |
| 117 |
ShaderProgram::NoNormalize)) { |
| 118 |
cout << d->program.error() << endl; |
| 119 |
} |
| 120 |
if (!d->program.enableAttributeArray("color")) |
| 121 |
cout << d->program.error() << endl; |
| 122 |
if (!d->program.useAttributeArray("color", PackedVertex::colorOffset(), |
| 123 |
sizeof(PackedVertex), UCharType, 4, |
| 124 |
ShaderProgram::Normalize)) { |
| 125 |
cout << d->program.error() << endl; |
| 126 |
} |
| 127 |
|
| 128 |
d->vao.release(); |
| 129 |
|
| 130 |
m_dirty = false; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
void LineStripGeometry::render(const Camera& camera) |
| 135 |
{ |
| 136 |
if (m_vertices.empty() || m_lineStarts.empty() || |
| 137 |
m_lineWidths.size() != m_lineStarts.size()) |
| 138 |
return; |
| 139 |
|
| 140 |
// Prepare the VBO, VAO, and shader program if necessary. |
| 141 |
update(); |
| 142 |
|
| 143 |
if (!d->program.bind()) |
| 144 |
cout << d->program.error() << endl; |
| 145 |
|
| 146 |
// Bind the VAO (captures all vertex attribute state) |
| 147 |
d->vao.bind(); |
| 148 |
|
| 149 |
// Set up our uniforms (model-view and projection matrices right now). |
| 150 |
if (!d->program.setUniformValue("modelView", camera.modelView().matrix())) { |
| 151 |
cout << d->program.error() << endl; |
| 152 |
} |
| 153 |
if (!d->program.setUniformValue("projection", camera.projection().matrix())) { |
| 154 |
cout << d->program.error() << endl; |
| 155 |
} |
| 156 |
|
| 157 |
// Render the linestrips using the shader and VAO. |
| 158 |
auto startIter = m_lineStarts.begin(); |
| 159 |
auto startEnd = m_lineStarts.end(); |
| 160 |
auto widthIter = m_lineWidths.begin(); |
| 161 |
unsigned int startIndex; |
| 162 |
unsigned int endIndex; |
| 163 |
while (startIter + 1 != startEnd) { |
| 164 |
startIndex = *startIter; |
| 165 |
endIndex = *(startIter + 1); |
| 166 |
glLineWidth(*widthIter); |
| 167 |
glDrawArrays(GL_LINE_STRIP, static_cast<GLint>(startIndex), |
| 168 |
static_cast<GLsizei>(endIndex - startIndex)); |
| 169 |
++startIter; |
| 170 |
++widthIter; |
| 171 |
} |
| 172 |
|
| 173 |
// There is an implicit ending index of m_vertices.size(): |
| 174 |
startIndex = *startIter; |
| 175 |
endIndex = static_cast<unsigned int>(m_vertices.size()); |
| 176 |
glLineWidth(*widthIter); |
| 177 |
glDrawArrays(GL_LINE_STRIP, static_cast<GLint>(startIndex), |
| 178 |
static_cast<GLsizei>(endIndex - startIndex)); |
| 179 |
|
| 180 |
d->vao.release(); |
| 181 |
d->program.release(); |
| 182 |
} |
| 183 |
|
| 184 |
void LineStripGeometry::clear() |
| 185 |
{ |
| 186 |
m_vertices.clear(); |
| 187 |
m_lineStarts.clear(); |
| 188 |
m_lineWidths.clear(); |
| 189 |
m_dirty = true; |
| 190 |
} |
| 191 |
|
| 192 |
size_t LineStripGeometry::addLineStrip(const Core::Array<Vector3f>& vertices, |
| 193 |
const Core::Array<Vector4ub>& rgba, |
| 194 |
float lineWidth) |
| 195 |
{ |
| 196 |
if (vertices.empty() || vertices.size() != rgba.size()) |
| 197 |
return InvalidIndex; |
| 198 |
|
| 199 |
size_t result = m_lineStarts.size(); |
| 200 |
m_lineStarts.push_back(static_cast<unsigned int>(m_vertices.size())); |
| 201 |
m_lineWidths.push_back(lineWidth); |
| 202 |
|
| 203 |
auto colorIter(rgba.begin()); |
| 204 |
auto vertIter(vertices.begin()); |
| 205 |
auto vertEnd(vertices.end()); |
| 206 |
|
| 207 |
m_vertices.reserve(m_vertices.size() + vertices.size()); |
| 208 |
while (vertIter != vertEnd) |
| 209 |
m_vertices.push_back(PackedVertex(*(vertIter++), *(colorIter++))); |
| 210 |
|
| 211 |
m_dirty = true; |
| 212 |
return result; |
| 213 |
} |
| 214 |
|
| 215 |
size_t LineStripGeometry::addLineStrip(const Core::Array<Vector3f>& vertices, |
| 216 |
const Core::Array<Vector3ub>& rgb, |
| 217 |
float lineWidth) |
| 218 |
{ |
| 219 |
if (vertices.empty() || vertices.size() != rgb.size()) |
| 220 |
return InvalidIndex; |
| 221 |
|
| 222 |
size_t result = m_lineStarts.size(); |
| 223 |
m_lineStarts.push_back(static_cast<unsigned int>(m_vertices.size())); |
| 224 |
m_lineWidths.push_back(lineWidth); |
| 225 |
|
| 226 |
auto colorIter(rgb.begin()); |
| 227 |
auto vertIter(vertices.begin()); |
| 228 |
auto vertEnd(vertices.end()); |
| 229 |
|
| 230 |
m_vertices.reserve(m_vertices.size() + vertices.size()); |
| 231 |
Vector4ub tmpColor(0, 0, 0, m_opacity); |
| 232 |
while (vertIter != vertEnd) { |
| 233 |
tmpColor.head<3>() = *(colorIter++); |
| 234 |
m_vertices.push_back(PackedVertex(*(vertIter++), tmpColor)); |
| 235 |
} |
| 236 |
|
| 237 |
m_dirty = true; |
| 238 |
return result; |
| 239 |
} |
| 240 |
|
| 241 |
size_t LineStripGeometry::addLineStrip(const Core::Array<Vector3f>& vertices, |
| 242 |
const Vector3ub& rgb, float lineWidth) |
| 243 |
{ |
| 244 |
if (vertices.empty()) |
| 245 |
return InvalidIndex; |
| 246 |
|
| 247 |
size_t result = m_lineStarts.size(); |
| 248 |
m_lineStarts.push_back(static_cast<unsigned int>(m_vertices.size())); |
| 249 |
m_lineWidths.push_back(lineWidth); |
| 250 |
|
| 251 |
auto vertIter(vertices.begin()); |
| 252 |
auto vertEnd(vertices.end()); |
| 253 |
|
| 254 |
m_vertices.reserve(m_vertices.size() + vertices.size()); |
| 255 |
Vector4ub tmpColor(rgb[0], rgb[1], rgb[2], m_opacity); |
| 256 |
while (vertIter != vertEnd) { |
| 257 |
m_vertices.push_back(PackedVertex(*(vertIter++), tmpColor)); |
| 258 |
} |
| 259 |
|
| 260 |
m_dirty = true; |
| 261 |
return result; |
| 262 |
} |
| 263 |
|
| 264 |
size_t LineStripGeometry::addLineStrip(const Core::Array<Vector3f>& vertices, |
| 265 |
float lineWidth) |
| 266 |
{ |
| 267 |
if (vertices.empty()) |
| 268 |
return InvalidIndex; |
| 269 |
|
| 270 |
size_t result = m_lineStarts.size(); |
| 271 |
m_lineStarts.push_back(static_cast<unsigned int>(m_vertices.size())); |
| 272 |
m_lineWidths.push_back(lineWidth); |
| 273 |
|
| 274 |
auto vertIter(vertices.begin()); |
| 275 |
auto vertEnd(vertices.end()); |
| 276 |
|
| 277 |
m_vertices.reserve(m_vertices.size() + vertices.size()); |
| 278 |
Vector4ub tmpColor(m_color[0], m_color[1], m_color[2], m_opacity); |
| 279 |
while (vertIter != vertEnd) |
| 280 |
m_vertices.push_back(PackedVertex(*(vertIter++), tmpColor)); |
| 281 |
|
| 282 |
m_dirty = true; |
| 283 |
return result; |
| 284 |
} |
| 285 |
|
| 286 |
} // namespace Avogadro::Rendering |