| 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 "curvegeometry.h" |
| 7 |
|
| 8 |
#include "camera.h" |
| 9 |
#include "scene.h" |
| 10 |
#include "visitor.h" |
| 11 |
|
| 12 |
namespace { |
| 13 |
#include "cylinders_fs.h" |
| 14 |
#include "cylinders_vs.h" |
| 15 |
} // namespace |
| 16 |
#include "avogadrogl.h" |
| 17 |
|
| 18 |
#include <iostream> |
| 19 |
#include <queue> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
using std::cout; |
| 23 |
using std::endl; |
| 24 |
|
| 25 |
namespace Avogadro::Rendering { |
| 26 |
|
| 27 |
using Core::Array; |
| 28 |
|
| 29 |
const size_t CurveGeometry::SKIPPED = 1; |
| 30 |
|
| 31 |
CurveGeometry::CurveGeometry() : m_dirty(true), m_canBeFlat(true) |
| 32 |
{ |
| 33 |
setRenderPass(SolidPass); |
| 34 |
} |
| 35 |
|
| 36 |
CurveGeometry::CurveGeometry(bool flat) : m_dirty(true), m_canBeFlat(flat) |
| 37 |
{ |
| 38 |
setRenderPass(SolidPass); |
| 39 |
} |
| 40 |
|
| 41 |
CurveGeometry::~CurveGeometry() |
| 42 |
{ |
| 43 |
for (auto& l : m_lines) { |
| 44 |
delete l; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
std::vector<ColorNormalVertex> CurveGeometry::computeCirclePoints( |
| 49 |
const Eigen::Affine3f& a, const Eigen::Affine3f& b, bool flat) const |
| 50 |
{ |
| 51 |
unsigned int circleResolution = flat ? 1 : 12; |
| 52 |
const float resolutionRadians = |
| 53 |
2.0f * static_cast<float>(M_PI) / static_cast<float>(circleResolution); |
| 54 |
std::vector<ColorNormalVertex> result; |
| 55 |
|
| 56 |
for (unsigned int i = 0; i < circleResolution; ++i) { |
| 57 |
float theta = i * resolutionRadians; |
| 58 |
Vector3f circle = Vector3f(std::cos(theta), 0.0f, std::sin(theta)); |
| 59 |
ColorNormalVertex vert1; |
| 60 |
vert1.normal = a.linear() * circle; |
| 61 |
vert1.vertex = a * circle; |
| 62 |
vert1.color = Vector3ub(0, 0, 0); |
| 63 |
result.push_back(vert1); |
| 64 |
|
| 65 |
ColorNormalVertex vert2; |
| 66 |
vert2.normal = b.linear() * circle; |
| 67 |
vert2.vertex = b * circle; |
| 68 |
vert2.color = Vector3ub(0, 0, 0); |
| 69 |
result.push_back(vert2); |
| 70 |
} |
| 71 |
return result; |
| 72 |
} |
| 73 |
|
| 74 |
float CurveGeometry::computeScale(size_t, float, float scale) const |
| 75 |
{ |
| 76 |
return scale; |
| 77 |
} |
| 78 |
|
| 79 |
void CurveGeometry::update(int index) |
| 80 |
{ |
| 81 |
// compute the middle points |
| 82 |
Line* line = m_lines[index]; |
| 83 |
unsigned int lineResolution = line->flat ? 20 : 15; |
| 84 |
size_t qttyPoints = line->points.size(); |
| 85 |
|
| 86 |
const size_t qttySegments = lineResolution * qttyPoints; |
| 87 |
Vector3f previous; |
| 88 |
std::vector<Eigen::Affine3f> points; |
| 89 |
size_t top = qttyPoints <= 4 ? 0 : line->points.size() - 4; |
| 90 |
auto it = line->points.begin(); |
| 91 |
for (size_t i = SKIPPED; i < top; ++i) { |
| 92 |
for (size_t j = 0; j < lineResolution; ++j) { |
| 93 |
float t = (i * lineResolution + j) / float(qttySegments); |
| 94 |
auto p = computeCurvePoint(t, line->points); |
| 95 |
if (i > SKIPPED) { |
| 96 |
Eigen::Matrix3f m; |
| 97 |
m.col(1) = (p - previous).normalized(); |
| 98 |
m.col(0) = m.col(1).unitOrthogonal() * -1.0f; |
| 99 |
if (i > SKIPPED + 1) { |
| 100 |
const auto& previousAngle = points.back().linear().col(0); |
| 101 |
float angle = previousAngle.dot(m.col(0)); |
| 102 |
// avoid nans |
| 103 |
if (std::isnan(angle)) { |
| 104 |
m.col(0) = previousAngle; |
| 105 |
break; |
| 106 |
// if angle > 90 flip it |
| 107 |
} else if (angle <= 0.0f) { |
| 108 |
m.col(0) *= -1.0f; |
| 109 |
} |
| 110 |
angle = previousAngle.dot(m.col(0)); |
| 111 |
float degrees = |
| 112 |
(std::acos(angle / (m.col(0).norm() * previousAngle.norm())) * |
| 113 |
RAD_TO_DEG_F); |
| 114 |
// if angle is > 25ยบ get the bisector |
| 115 |
while (degrees > 25.0f) { |
| 116 |
m.col(0) = (m.col(0) + previousAngle).normalized(); |
| 117 |
angle = previousAngle.dot(m.col(0)); |
| 118 |
degrees = |
| 119 |
(std::acos(angle / (m.col(0).norm() * previousAngle.norm())) * |
| 120 |
RAD_TO_DEG_F); |
| 121 |
} |
| 122 |
} |
| 123 |
m.col(2) = m.col(0).cross(m.col(1)) * -1.0f; |
| 124 |
Eigen::Affine3f affine; |
| 125 |
affine.translation() = p; |
| 126 |
affine.linear() = m; |
| 127 |
float r = !m_canBeFlat || !line->flat |
| 128 |
? computeScale(i, j / static_cast<float>(lineResolution), |
| 129 |
line->radius) |
| 130 |
: 0.01f; |
| 131 |
affine.scale(r); |
| 132 |
points.push_back(affine); |
| 133 |
} |
| 134 |
previous = p; |
| 135 |
} |
| 136 |
++it; |
| 137 |
} |
| 138 |
|
| 139 |
// prepare VBO and EBO |
| 140 |
std::vector<unsigned int> indices; |
| 141 |
std::vector<ColorNormalVertex> vertices; |
| 142 |
|
| 143 |
it = line->points.begin(); |
| 144 |
if (line->points.size() > 3) { |
| 145 |
it = std::next(it, 3); |
| 146 |
} |
| 147 |
for (size_t i = 1; i < points.size(); ++i) { |
| 148 |
if (i % lineResolution == 0) { |
| 149 |
++it; |
| 150 |
} |
| 151 |
std::vector<ColorNormalVertex> radials = |
| 152 |
computeCirclePoints(points[i], points[i - 1], line->flat); |
| 153 |
for (auto r : radials) { |
| 154 |
r.color = (*it)->color; |
| 155 |
vertices.push_back(r); |
| 156 |
} |
| 157 |
const auto tubeStart = static_cast<unsigned int>( |
| 158 |
vertices.size() - (line->flat && m_canBeFlat ? radials.size() : 0)); |
| 159 |
for (unsigned int j = 0; j < radials.size() / 2; ++j) { |
| 160 |
unsigned int r1 = j + j; |
| 161 |
unsigned int r2 = (j != 0 ? r1 : radials.size()) - 2; |
| 162 |
indices.push_back(tubeStart + r1); |
| 163 |
indices.push_back(tubeStart + r1 + 1); |
| 164 |
indices.push_back(tubeStart + r2); |
| 165 |
|
| 166 |
indices.push_back(tubeStart + r2); |
| 167 |
indices.push_back(tubeStart + r1 + 1); |
| 168 |
indices.push_back(tubeStart + r2 + 1); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
line->vbo.upload(vertices, BufferObject::ArrayBuffer); |
| 173 |
line->ibo.upload(indices, BufferObject::ElementArrayBuffer); |
| 174 |
|
| 175 |
// Set up VAO with vertex attribute bindings (OpenGL 4.0 core profile) |
| 176 |
line->vao.bind(); |
| 177 |
line->vbo.bind(); |
| 178 |
line->ibo.bind(); |
| 179 |
|
| 180 |
processShaderError(!m_shaderInfo.program.enableAttributeArray("vertex")); |
| 181 |
processShaderError(!m_shaderInfo.program.useAttributeArray( |
| 182 |
"vertex", ColorNormalVertex::vertexOffset(), sizeof(ColorNormalVertex), |
| 183 |
FloatType, 3, ShaderProgram::NoNormalize)); |
| 184 |
|
| 185 |
processShaderError(!m_shaderInfo.program.enableAttributeArray("color")); |
| 186 |
processShaderError(!m_shaderInfo.program.useAttributeArray( |
| 187 |
"color", ColorNormalVertex::colorOffset(), sizeof(ColorNormalVertex), |
| 188 |
UCharType, 3, ShaderProgram::Normalize)); |
| 189 |
|
| 190 |
processShaderError(!m_shaderInfo.program.enableAttributeArray("normal")); |
| 191 |
processShaderError(!m_shaderInfo.program.useAttributeArray( |
| 192 |
"normal", ColorNormalVertex::normalOffset(), sizeof(ColorNormalVertex), |
| 193 |
FloatType, 3, ShaderProgram::NoNormalize)); |
| 194 |
|
| 195 |
line->vao.release(); |
| 196 |
|
| 197 |
line->numberOfVertices = vertices.size(); |
| 198 |
line->numberOfIndices = indices.size(); |
| 199 |
|
| 200 |
line->dirty = false; |
| 201 |
} |
| 202 |
|
| 203 |
void CurveGeometry::accept(Visitor& visitor) |
| 204 |
{ |
| 205 |
visitor.visit(*this); |
| 206 |
} |
| 207 |
|
| 208 |
void checkShaderInfo(ShaderInfo& shaderInfo, const std::string& fs, |
| 209 |
const std::string& vs) |
| 210 |
{ |
| 211 |
if (shaderInfo.vertexShader.type() == Shader::Unknown) { |
| 212 |
shaderInfo.vertexShader.setType(Shader::Vertex); |
| 213 |
shaderInfo.vertexShader.setSource(vs); |
| 214 |
shaderInfo.fragmentShader.setType(Shader::Fragment); |
| 215 |
shaderInfo.fragmentShader.setSource(fs); |
| 216 |
if (!shaderInfo.vertexShader.compile()) |
| 217 |
cout << shaderInfo.vertexShader.error() << endl; |
| 218 |
if (!shaderInfo.fragmentShader.compile()) |
| 219 |
cout << shaderInfo.fragmentShader.error() << endl; |
| 220 |
shaderInfo.program.attachShader(shaderInfo.vertexShader); |
| 221 |
shaderInfo.program.attachShader(shaderInfo.fragmentShader); |
| 222 |
if (!shaderInfo.program.link()) |
| 223 |
cout << shaderInfo.program.error() << endl; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
void CurveGeometry::processShaderError(bool error) |
| 228 |
{ |
| 229 |
if (error) { |
| 230 |
cout << m_shaderInfo.program.error() << endl; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
void CurveGeometry::render(const Camera& camera) |
| 235 |
{ |
| 236 |
if (m_dirty) { |
| 237 |
checkShaderInfo(m_shaderInfo, cylinders_fs, cylinders_vs); |
| 238 |
m_dirty = false; |
| 239 |
} |
| 240 |
if (!m_dirty) { |
| 241 |
processShaderError(!m_shaderInfo.program.bind()); |
| 242 |
processShaderError(!m_shaderInfo.program.setUniformValue( |
| 243 |
"modelView", camera.modelView().matrix())); |
| 244 |
processShaderError(!m_shaderInfo.program.setUniformValue( |
| 245 |
"projection", camera.projection().matrix())); |
| 246 |
Eigen::Matrix3f normalMatrix = |
| 247 |
camera.modelView().linear().inverse().transpose(); |
| 248 |
processShaderError( |
| 249 |
!m_shaderInfo.program.setUniformValue("normalMatrix", normalMatrix)); |
| 250 |
|
| 251 |
for (size_t i = 0; i < m_lines.size(); ++i) { |
| 252 |
Line* line = m_lines[i]; |
| 253 |
if (line->dirty) { |
| 254 |
update(i); |
| 255 |
} |
| 256 |
|
| 257 |
// Bind the VAO (captures all vertex attribute state) |
| 258 |
line->vao.bind(); |
| 259 |
|
| 260 |
if (line->flat && m_canBeFlat) { |
| 261 |
glLineWidth(-line->radius); |
| 262 |
} |
| 263 |
glDrawRangeElements(line->flat && m_canBeFlat ? GL_LINE_STRIP |
| 264 |
: GL_TRIANGLES, |
| 265 |
0, static_cast<GLuint>(line->numberOfVertices), |
| 266 |
static_cast<GLsizei>(line->numberOfIndices), |
| 267 |
GL_UNSIGNED_INT, reinterpret_cast<const GLvoid*>(0)); |
| 268 |
line->vao.release(); |
| 269 |
} |
| 270 |
m_shaderInfo.program.release(); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
void CurveGeometry::addPoint(const Vector3f& pos, const Vector3ub& color, |
| 275 |
float radius, size_t group, size_t id) |
| 276 |
{ |
| 277 |
if (m_indexMap.find(group) == m_indexMap.end()) { |
| 278 |
m_indexMap[group] = m_lines.size(); |
| 279 |
m_lines.push_back(new Line(radius)); |
| 280 |
} |
| 281 |
m_lines[m_indexMap[group]]->radius = radius; |
| 282 |
m_lines[m_indexMap[group]]->flat = radius < 0.0f; |
| 283 |
m_lines[m_indexMap[group]]->add(new Point(pos, color, id)); |
| 284 |
} |
| 285 |
|
| 286 |
Array<Identifier> CurveGeometry::areaHits(const Frustrum& f) const |
| 287 |
{ |
| 288 |
Array<Identifier> result; |
| 289 |
// Check for intersection. |
| 290 |
for (const auto& line : m_lines) { |
| 291 |
size_t skip = 0; |
| 292 |
std::queue<size_t> previous; |
| 293 |
for (const auto& point : line->points) { |
| 294 |
previous.push(point->id); |
| 295 |
if (skip < 2) { |
| 296 |
++skip; |
| 297 |
continue; |
| 298 |
} |
| 299 |
int in = 0; |
| 300 |
for (; in < 4; ++in) { |
| 301 |
float dist = (point->pos - f.points[2 * in]).dot(f.planes[in]); |
| 302 |
if (dist > 0.0f) { |
| 303 |
// Outside of our frustrum, break. |
| 304 |
break; |
| 305 |
} |
| 306 |
} |
| 307 |
if (in == 4) { |
| 308 |
// The center is within the four planes that make our frustrum - hit. |
| 309 |
Identifier id; |
| 310 |
id.molecule = m_identifier.molecule; |
| 311 |
id.type = m_identifier.type; |
| 312 |
id.index = previous.front(); |
| 313 |
result.push_back(id); |
| 314 |
} |
| 315 |
previous.pop(); |
| 316 |
} |
| 317 |
} |
| 318 |
return result; |
| 319 |
} |
| 320 |
|
| 321 |
} // namespace Avogadro::Rendering |