| 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 "cylindergeometry.h" |
| 7 |
|
| 8 |
#include "camera.h" |
| 9 |
#include "scene.h" |
| 10 |
#include "visitor.h" |
| 11 |
|
| 12 |
#include "bufferobject.h" |
| 13 |
#include "vertexarrayobject.h" |
| 14 |
|
| 15 |
#include "shader.h" |
| 16 |
#include "shaderprogram.h" |
| 17 |
|
| 18 |
namespace { |
| 19 |
#include "cylinders_fs.h" |
| 20 |
#include "cylinders_vs.h" |
| 21 |
} // namespace |
| 22 |
|
| 23 |
#include "avogadrogl.h" |
| 24 |
|
| 25 |
#include <avogadro/core/matrix.h> |
| 26 |
|
| 27 |
#include <iostream> |
| 28 |
|
| 29 |
using std::cout; |
| 30 |
using std::endl; |
| 31 |
|
| 32 |
namespace Avogadro::Rendering { |
| 33 |
|
| 34 |
class CylinderGeometry::Private |
| 35 |
{ |
| 36 |
public: |
| 37 |
Private() {} |
| 38 |
|
| 39 |
BufferObject vbo; |
| 40 |
BufferObject ibo; |
| 41 |
VertexArrayObject vao; |
| 42 |
|
| 43 |
inline static Shader* vertexShader = nullptr; |
| 44 |
inline static Shader* fragmentShader = nullptr; |
| 45 |
inline static ShaderProgram* program = nullptr; |
| 46 |
|
| 47 |
size_t numberOfVertices; |
| 48 |
size_t numberOfIndices; |
| 49 |
}; |
| 50 |
|
| 51 |
CylinderGeometry::CylinderGeometry() : m_dirty(false), d(new Private) |
| 52 |
{ |
| 53 |
setRenderPass(SolidPass); |
| 54 |
} |
| 55 |
|
| 56 |
CylinderGeometry::CylinderGeometry(const CylinderGeometry& other) |
| 57 |
: Drawable(other), m_cylinders(other.m_cylinders), m_indices(other.m_indices), |
| 58 |
m_indexMap(other.m_indexMap), m_dirty(true), d(new Private) |
| 59 |
{ |
| 60 |
setRenderPass(SolidPass); |
| 61 |
} |
| 62 |
|
| 63 |
CylinderGeometry::~CylinderGeometry() |
| 64 |
{ |
| 65 |
delete d; |
| 66 |
} |
| 67 |
|
| 68 |
void CylinderGeometry::accept(Visitor& visitor) |
| 69 |
{ |
| 70 |
visitor.visit(*this); |
| 71 |
} |
| 72 |
|
| 73 |
void CylinderGeometry::update() |
| 74 |
{ |
| 75 |
if (m_indices.empty() || m_cylinders.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 == nullptr) { |
| 82 |
d->vertexShader = new Shader; |
| 83 |
d->vertexShader->setType(Shader::Vertex); |
| 84 |
d->vertexShader->setSource(cylinders_vs); |
| 85 |
d->fragmentShader = new Shader; |
| 86 |
d->fragmentShader->setType(Shader::Fragment); |
| 87 |
d->fragmentShader->setSource(cylinders_fs); |
| 88 |
if (!d->vertexShader->compile()) |
| 89 |
cout << d->vertexShader->error() << endl; |
| 90 |
if (!d->fragmentShader->compile()) |
| 91 |
cout << d->fragmentShader->error() << endl; |
| 92 |
|
| 93 |
if (d->program == nullptr) |
| 94 |
d->program = new ShaderProgram; |
| 95 |
|
| 96 |
d->program->attachShader(*d->vertexShader); |
| 97 |
d->program->attachShader(*d->fragmentShader); |
| 98 |
if (!d->program->link()) |
| 99 |
cout << d->program->error() << endl; |
| 100 |
} |
| 101 |
|
| 102 |
// Check if the VBOs are ready, if not get them ready. |
| 103 |
if (!d->vbo.ready() || m_dirty) { |
| 104 |
// Set some defaults for our cylinders. |
| 105 |
const unsigned int resolution = 8; // points per circle |
| 106 |
const float resolutionRadians = |
| 107 |
2.0f * static_cast<float>(M_PI) / static_cast<float>(resolution); |
| 108 |
std::vector<Vector3f> radials; |
| 109 |
radials.reserve(resolution); |
| 110 |
|
| 111 |
std::vector<unsigned int> cylinderIndices; |
| 112 |
std::vector<ColorNormalVertex> cylinderVertices; |
| 113 |
|
| 114 |
auto itIndex = m_indices.begin(); |
| 115 |
auto itCylinder = m_cylinders.begin(); |
| 116 |
|
| 117 |
for (unsigned int i = 0; |
| 118 |
itIndex != m_indices.end() && itCylinder != m_cylinders.end(); |
| 119 |
++i, ++itIndex, ++itCylinder) { |
| 120 |
|
| 121 |
const Vector3f& position1 = itCylinder->end1; |
| 122 |
const Vector3f& position2 = itCylinder->end2; |
| 123 |
const Vector3f direction = (position2 - position1).normalized(); |
| 124 |
float radius = itCylinder->radius; |
| 125 |
|
| 126 |
// Generate the radial vectors |
| 127 |
Vector3f radialVec = direction.unitOrthogonal() * radius; |
| 128 |
Eigen::AngleAxisf transform(resolutionRadians, direction); |
| 129 |
radials.clear(); |
| 130 |
for (unsigned int j = 0; j < resolution; ++j) { |
| 131 |
radials.push_back(radialVec); |
| 132 |
radialVec = transform * radialVec; |
| 133 |
} |
| 134 |
|
| 135 |
// Cylinder |
| 136 |
ColorNormalVertex vert(itCylinder->color, -direction, position1); |
| 137 |
ColorNormalVertex vert2(itCylinder->color2, -direction, position1); |
| 138 |
const auto tubeStart = static_cast<unsigned int>(cylinderVertices.size()); |
| 139 |
for (auto& radial : radials) { |
| 140 |
vert.normal = radial; |
| 141 |
vert.vertex = position1 + radial; |
| 142 |
cylinderVertices.push_back(vert); |
| 143 |
vert2.normal = vert.normal; |
| 144 |
vert2.vertex = position2 + radial; |
| 145 |
cylinderVertices.push_back(vert2); |
| 146 |
} |
| 147 |
// Now to stitch it together. |
| 148 |
for (unsigned int j = 0; j < resolution; ++j) { |
| 149 |
unsigned int r1 = j + j; |
| 150 |
unsigned int r2 = (j != 0 ? r1 : resolution + resolution) - 2; |
| 151 |
cylinderIndices.push_back(tubeStart + r1); |
| 152 |
cylinderIndices.push_back(tubeStart + r1 + 1); |
| 153 |
cylinderIndices.push_back(tubeStart + r2); |
| 154 |
|
| 155 |
cylinderIndices.push_back(tubeStart + r2); |
| 156 |
cylinderIndices.push_back(tubeStart + r1 + 1); |
| 157 |
cylinderIndices.push_back(tubeStart + r2 + 1); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
d->vbo.upload(cylinderVertices, BufferObject::ArrayBuffer); |
| 162 |
d->ibo.upload(cylinderIndices, BufferObject::ElementArrayBuffer); |
| 163 |
|
| 164 |
// Set up VAO with vertex attribute bindings (OpenGL 4.0 core profile) |
| 165 |
d->vao.bind(); |
| 166 |
d->vbo.bind(); |
| 167 |
d->ibo.bind(); |
| 168 |
|
| 169 |
ShaderProgram* program = d->program; |
| 170 |
if (!program->enableAttributeArray("vertex")) |
| 171 |
cout << program->error() << endl; |
| 172 |
if (!program->useAttributeArray("vertex", ColorNormalVertex::vertexOffset(), |
| 173 |
sizeof(ColorNormalVertex), FloatType, 3, |
| 174 |
ShaderProgram::NoNormalize)) { |
| 175 |
cout << program->error() << endl; |
| 176 |
} |
| 177 |
if (!program->enableAttributeArray("color")) |
| 178 |
cout << program->error() << endl; |
| 179 |
if (!program->useAttributeArray("color", ColorNormalVertex::colorOffset(), |
| 180 |
sizeof(ColorNormalVertex), UCharType, 3, |
| 181 |
ShaderProgram::Normalize)) { |
| 182 |
cout << program->error() << endl; |
| 183 |
} |
| 184 |
if (!program->enableAttributeArray("normal")) |
| 185 |
cout << program->error() << endl; |
| 186 |
if (!program->useAttributeArray("normal", ColorNormalVertex::normalOffset(), |
| 187 |
sizeof(ColorNormalVertex), FloatType, 3, |
| 188 |
ShaderProgram::NoNormalize)) { |
| 189 |
cout << program->error() << endl; |
| 190 |
} |
| 191 |
|
| 192 |
d->vao.release(); |
| 193 |
|
| 194 |
d->numberOfVertices = cylinderVertices.size(); |
| 195 |
d->numberOfIndices = cylinderIndices.size(); |
| 196 |
|
| 197 |
m_dirty = false; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
void CylinderGeometry::render(const Camera& camera) |
| 202 |
{ |
| 203 |
if (m_indices.empty() || m_cylinders.empty()) |
| 204 |
return; |
| 205 |
|
| 206 |
// Prepare the VBOs, IBOs, VAO, and shader program if necessary. |
| 207 |
update(); |
| 208 |
|
| 209 |
if (!d->program->bind()) |
| 210 |
cout << d->program->error() << endl; |
| 211 |
|
| 212 |
// Bind the VAO (captures all vertex attribute state) |
| 213 |
d->vao.bind(); |
| 214 |
|
| 215 |
// Set up our uniforms (model-view and projection matrices right now). |
| 216 |
if (!d->program->setUniformValue("modelView", camera.modelView().matrix())) { |
| 217 |
cout << d->program->error() << endl; |
| 218 |
} |
| 219 |
if (!d->program->setUniformValue("projection", |
| 220 |
camera.projection().matrix())) { |
| 221 |
cout << d->program->error() << endl; |
| 222 |
} |
| 223 |
if (!d->program->setUniformValue("opacity", m_opacity)) { |
| 224 |
cout << d->program->error() << endl; |
| 225 |
} |
| 226 |
Matrix3f normalMatrix = camera.modelView().linear().inverse().transpose(); |
| 227 |
if (!d->program->setUniformValue("normalMatrix", normalMatrix)) |
| 228 |
std::cout << d->program->error() << std::endl; |
| 229 |
|
| 230 |
// Render the loaded cylinders using the shader and VAO. |
| 231 |
glDrawRangeElements(GL_TRIANGLES, 0, static_cast<GLuint>(d->numberOfVertices), |
| 232 |
static_cast<GLsizei>(d->numberOfIndices), GL_UNSIGNED_INT, |
| 233 |
reinterpret_cast<const GLvoid*>(0)); |
| 234 |
|
| 235 |
d->vao.release(); |
| 236 |
d->program->release(); |
| 237 |
} |
| 238 |
|
| 239 |
std::multimap<float, Identifier> CylinderGeometry::hits( |
| 240 |
const Vector3f& rayOrigin, const Vector3f& rayEnd, |
| 241 |
const Vector3f& rayDirection) const |
| 242 |
{ |
| 243 |
std::multimap<float, Identifier> result; |
| 244 |
|
| 245 |
for (size_t i = 0; i < m_cylinders.size(); ++i) { |
| 246 |
const CylinderColor& cylinder = m_cylinders[i]; |
| 247 |
|
| 248 |
// Check for cylinder intersection with the ray. |
| 249 |
Vector3f ao = rayOrigin - cylinder.end1; |
| 250 |
Vector3f ab = cylinder.end2 - cylinder.end1; |
| 251 |
Vector3f aoxab = ao.cross(ab); |
| 252 |
Vector3f vxab = rayDirection.cross(ab); |
| 253 |
|
| 254 |
float A = vxab.dot(vxab); |
| 255 |
float B = 2.0f * vxab.dot(aoxab); |
| 256 |
float C = |
| 257 |
aoxab.dot(aoxab) - ab.dot(ab) * (cylinder.radius * cylinder.radius); |
| 258 |
float D = B * B - 4.0f * A * C; |
| 259 |
|
| 260 |
// no intersection |
| 261 |
if (D < 0.0f) |
| 262 |
continue; |
| 263 |
|
| 264 |
float t = std::min((-B + std::sqrt(D)) / (2.0f * A), |
| 265 |
(-B - std::sqrt(D)) / (2.0f * A)); |
| 266 |
|
| 267 |
Vector3f ip = rayOrigin + (rayDirection * t); |
| 268 |
Vector3f ip1 = ip - cylinder.end1; |
| 269 |
Vector3f ip2 = ip - (cylinder.end1 + ab); |
| 270 |
|
| 271 |
// intersection below base or above top of the cylinder |
| 272 |
if (ip1.dot(ab) < 0.0f || ip2.dot(ab) > 0.0f) |
| 273 |
continue; |
| 274 |
|
| 275 |
// Test for clipping |
| 276 |
Vector3f distance = ip - rayOrigin; |
| 277 |
if (distance.dot(rayDirection) < 0.0f || |
| 278 |
(ip - rayEnd).dot(rayDirection) > 0.0f) |
| 279 |
continue; |
| 280 |
|
| 281 |
Identifier id; |
| 282 |
id.molecule = m_identifier.molecule; |
| 283 |
id.type = m_identifier.type; |
| 284 |
id.index = i; |
| 285 |
if (m_indexMap.size()) |
| 286 |
id.index = m_indexMap.find(i)->second; |
| 287 |
if (id.type != InvalidType) { |
| 288 |
float depth = distance.norm(); |
| 289 |
result.insert(std::pair<float, Identifier>(depth, id)); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
return result; |
| 294 |
} |
| 295 |
|
| 296 |
void CylinderGeometry::addCylinder(const Vector3f& pos1, const Vector3f& pos2, |
| 297 |
float radius, const Vector3ub& color) |
| 298 |
{ |
| 299 |
addCylinder(pos1, pos2, radius, color, color); |
| 300 |
} |
| 301 |
|
| 302 |
void CylinderGeometry::addCylinder(const Vector3f& pos1, const Vector3f& pos2, |
| 303 |
float radius, const Vector3ub& colorStart, |
| 304 |
const Vector3ub& colorEnd) |
| 305 |
{ |
| 306 |
m_dirty = true; |
| 307 |
m_cylinders.emplace_back(pos1, pos2, radius, colorStart, colorEnd); |
| 308 |
m_indices.push_back(m_indices.size()); |
| 309 |
} |
| 310 |
|
| 311 |
void CylinderGeometry::addCylinder(const Vector3f& pos1, const Vector3f& pos2, |
| 312 |
float radius, const Vector3ub& color, |
| 313 |
size_t index) |
| 314 |
{ |
| 315 |
m_indexMap[m_cylinders.size()] = index; |
| 316 |
addCylinder(pos1, pos2, radius, color, color); |
| 317 |
} |
| 318 |
|
| 319 |
void CylinderGeometry::addCylinder(const Vector3f& pos1, const Vector3f& pos2, |
| 320 |
float radius, const Vector3ub& colorStart, |
| 321 |
const Vector3ub& colorEnd, size_t index) |
| 322 |
{ |
| 323 |
m_indexMap[m_cylinders.size()] = index; |
| 324 |
addCylinder(pos1, pos2, radius, colorStart, colorEnd); |
| 325 |
} |
| 326 |
|
| 327 |
void CylinderGeometry::clear() |
| 328 |
{ |
| 329 |
m_cylinders.clear(); |
| 330 |
m_indices.clear(); |
| 331 |
m_indexMap.clear(); |
| 332 |
} |
| 333 |
|
| 334 |
} // namespace Avogadro::Rendering |