| 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 "spheregeometry.h" |
| 7 |
|
| 8 |
#include "camera.h" |
| 9 |
#include "scene.h" |
| 10 |
|
| 11 |
#include "bufferobject.h" |
| 12 |
#include "vertexarrayobject.h" |
| 13 |
|
| 14 |
#include "shader.h" |
| 15 |
#include "shaderprogram.h" |
| 16 |
|
| 17 |
#include "visitor.h" |
| 18 |
|
| 19 |
namespace { |
| 20 |
#include "spheres_fs.h" |
| 21 |
#include "spheres_vs.h" |
| 22 |
} // namespace |
| 23 |
|
| 24 |
#include "avogadrogl.h" |
| 25 |
|
| 26 |
#include <iostream> |
| 27 |
|
| 28 |
using std::cout; |
| 29 |
using std::endl; |
| 30 |
|
| 31 |
namespace Avogadro::Rendering { |
| 32 |
|
| 33 |
using Core::Array; |
| 34 |
|
| 35 |
class SphereGeometry::Private |
| 36 |
{ |
| 37 |
public: |
| 38 |
Private() {} |
| 39 |
|
| 40 |
BufferObject vbo; |
| 41 |
BufferObject ibo; |
| 42 |
VertexArrayObject vao; |
| 43 |
|
| 44 |
inline static Shader* vertexShader = nullptr; |
| 45 |
inline static Shader* fragmentShader = nullptr; |
| 46 |
inline static ShaderProgram* program = nullptr; |
| 47 |
|
| 48 |
size_t numberOfVertices; |
| 49 |
size_t numberOfIndices; |
| 50 |
}; |
| 51 |
|
| 52 |
SphereGeometry::SphereGeometry() : m_dirty(false), d(new Private) |
| 53 |
{ |
| 54 |
setRenderPass(SolidPass); |
| 55 |
} |
| 56 |
|
| 57 |
SphereGeometry::SphereGeometry(const SphereGeometry& other) |
| 58 |
: Drawable(other), m_spheres(other.m_spheres), m_indices(other.m_indices), |
| 59 |
m_dirty(true), d(new Private) |
| 60 |
{ |
| 61 |
setRenderPass(SolidPass); |
| 62 |
} |
| 63 |
|
| 64 |
SphereGeometry::~SphereGeometry() |
| 65 |
{ |
| 66 |
delete d; |
| 67 |
} |
| 68 |
|
| 69 |
void SphereGeometry::accept(Visitor& visitor) |
| 70 |
{ |
| 71 |
visitor.visit(*this); |
| 72 |
} |
| 73 |
|
| 74 |
void SphereGeometry::update() |
| 75 |
{ |
| 76 |
if (m_indices.empty() || m_spheres.empty()) |
| 77 |
return; |
| 78 |
|
| 79 |
// Build and link the shader if it has not been used yet. |
| 80 |
// Must be done before VAO setup since we need the program for attribute |
| 81 |
// setup. |
| 82 |
if (d->vertexShader == nullptr) { |
| 83 |
d->vertexShader = new Shader; |
| 84 |
d->vertexShader->setType(Shader::Vertex); |
| 85 |
d->vertexShader->setSource(spheres_vs); |
| 86 |
d->fragmentShader = new Shader; |
| 87 |
d->fragmentShader->setType(Shader::Fragment); |
| 88 |
d->fragmentShader->setSource(spheres_fs); |
| 89 |
if (!d->vertexShader->compile()) |
| 90 |
cout << d->vertexShader->error() << endl; |
| 91 |
if (!d->fragmentShader->compile()) |
| 92 |
cout << d->fragmentShader->error() << endl; |
| 93 |
|
| 94 |
if (d->program == nullptr) |
| 95 |
d->program = new ShaderProgram; |
| 96 |
|
| 97 |
d->program->attachShader(*d->vertexShader); |
| 98 |
d->program->attachShader(*d->fragmentShader); |
| 99 |
if (!d->program->link()) |
| 100 |
cout << d->program->error() << endl; |
| 101 |
} |
| 102 |
|
| 103 |
// Check if the VBOs are ready, if not get them ready. |
| 104 |
if (!d->vbo.ready() || m_dirty) { |
| 105 |
std::vector<unsigned int> sphereIndices; |
| 106 |
std::vector<ColorTextureVertex> sphereVertices; |
| 107 |
sphereIndices.reserve(m_indices.size() * 4); |
| 108 |
sphereVertices.reserve(m_spheres.size() * 4); |
| 109 |
|
| 110 |
auto itIndex = m_indices.begin(); |
| 111 |
auto itSphere = m_spheres.begin(); |
| 112 |
|
| 113 |
for (size_t i = 0; |
| 114 |
itIndex != m_indices.end() && itSphere != m_spheres.end(); |
| 115 |
++i, ++itIndex, ++itSphere) { |
| 116 |
// Use our packed data structure... |
| 117 |
float r = itSphere->radius; |
| 118 |
unsigned int index = 4 * static_cast<unsigned int>(i); |
| 119 |
ColorTextureVertex vert(itSphere->center, itSphere->color, |
| 120 |
Vector2f(-r, -r)); |
| 121 |
sphereVertices.push_back(vert); |
| 122 |
vert.textureCoord = Vector2f(-r, r); |
| 123 |
sphereVertices.push_back(vert); |
| 124 |
vert.textureCoord = Vector2f(r, -r); |
| 125 |
sphereVertices.push_back(vert); |
| 126 |
vert.textureCoord = Vector2f(r, r); |
| 127 |
sphereVertices.push_back(vert); |
| 128 |
|
| 129 |
// 6 indexed vertices to draw a quad... |
| 130 |
sphereIndices.push_back(index + 0); |
| 131 |
sphereIndices.push_back(index + 1); |
| 132 |
sphereIndices.push_back(index + 2); |
| 133 |
sphereIndices.push_back(index + 3); |
| 134 |
sphereIndices.push_back(index + 2); |
| 135 |
sphereIndices.push_back(index + 1); |
| 136 |
} |
| 137 |
|
| 138 |
if (!d->vbo.upload(sphereVertices, BufferObject::ArrayBuffer)) |
| 139 |
cout << d->vbo.error() << endl; |
| 140 |
|
| 141 |
if (!d->ibo.upload(sphereIndices, BufferObject::ElementArrayBuffer)) |
| 142 |
cout << d->ibo.error() << endl; |
| 143 |
|
| 144 |
// Set up VAO with vertex attribute bindings (OpenGL 4.0 core profile) |
| 145 |
// Check bind() return values - if binding fails (e.g., no GL context), |
| 146 |
// early-return without clearing m_dirty so geometry will be retried later. |
| 147 |
if (!d->vao.bind()) { |
| 148 |
cout << "SphereGeometry: VAO bind failed" << endl; |
| 149 |
return; |
| 150 |
} |
| 151 |
if (!d->vbo.bind()) { |
| 152 |
cout << "SphereGeometry: VBO bind failed" << endl; |
| 153 |
d->vao.release(); |
| 154 |
return; |
| 155 |
} |
| 156 |
if (!d->ibo.bind()) { |
| 157 |
cout << "SphereGeometry: IBO bind failed" << endl; |
| 158 |
d->vao.release(); |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
ShaderProgram* program = d->program; |
| 163 |
if (!program->enableAttributeArray("vertex")) |
| 164 |
cout << program->error() << endl; |
| 165 |
if (!program->useAttributeArray("vertex", |
| 166 |
ColorTextureVertex::vertexOffset(), |
| 167 |
sizeof(ColorTextureVertex), FloatType, 3, |
| 168 |
ShaderProgram::NoNormalize)) { |
| 169 |
cout << program->error() << endl; |
| 170 |
} |
| 171 |
if (!program->enableAttributeArray("color")) |
| 172 |
cout << program->error() << endl; |
| 173 |
if (!program->useAttributeArray("color", ColorTextureVertex::colorOffset(), |
| 174 |
sizeof(ColorTextureVertex), UCharType, 3, |
| 175 |
ShaderProgram::Normalize)) { |
| 176 |
cout << program->error() << endl; |
| 177 |
} |
| 178 |
if (!program->enableAttributeArray("texCoordinate")) |
| 179 |
cout << program->error() << endl; |
| 180 |
if (!program->useAttributeArray("texCoordinate", |
| 181 |
ColorTextureVertex::textureCoordOffset(), |
| 182 |
sizeof(ColorTextureVertex), FloatType, 2, |
| 183 |
ShaderProgram::NoNormalize)) { |
| 184 |
cout << program->error() << endl; |
| 185 |
} |
| 186 |
|
| 187 |
d->vao.release(); |
| 188 |
|
| 189 |
d->numberOfVertices = sphereVertices.size(); |
| 190 |
d->numberOfIndices = sphereIndices.size(); |
| 191 |
|
| 192 |
m_dirty = false; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
void SphereGeometry::render(const Camera& camera) |
| 197 |
{ |
| 198 |
if (m_indices.empty() || m_spheres.empty()) |
| 199 |
return; |
| 200 |
|
| 201 |
// Prepare the VBOs, IBOs, VAO, and shader program if necessary. |
| 202 |
update(); |
| 203 |
|
| 204 |
if (!d->program->bind()) |
| 205 |
cout << d->program->error() << endl; |
| 206 |
|
| 207 |
// Bind the VAO (captures all vertex attribute state) |
| 208 |
// If bind fails (e.g., no GL context), skip rendering to avoid GL errors. |
| 209 |
if (!d->vao.bind()) { |
| 210 |
d->program->release(); |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
// Set up our uniforms (model-view and projection matrices right now). |
| 215 |
if (!d->program->setUniformValue("modelView", camera.modelView().matrix())) { |
| 216 |
cout << d->program->error() << endl; |
| 217 |
} |
| 218 |
if (!d->program->setUniformValue("projection", |
| 219 |
camera.projection().matrix())) { |
| 220 |
cout << d->program->error() << endl; |
| 221 |
} |
| 222 |
if (!d->program->setUniformValue("opacity", m_opacity)) { |
| 223 |
cout << d->program->error() << endl; |
| 224 |
} |
| 225 |
|
| 226 |
// Render the loaded spheres using the shader and VAO. |
| 227 |
glDrawRangeElements(GL_TRIANGLES, 0, static_cast<GLuint>(d->numberOfVertices), |
| 228 |
static_cast<GLsizei>(d->numberOfIndices), GL_UNSIGNED_INT, |
| 229 |
(const GLvoid*)nullptr); |
| 230 |
|
| 231 |
d->vao.release(); |
| 232 |
d->program->release(); |
| 233 |
} |
| 234 |
|
| 235 |
std::multimap<float, Identifier> SphereGeometry::hits( |
| 236 |
const Vector3f& rayOrigin, const Vector3f& rayEnd, |
| 237 |
const Vector3f& rayDirection) const |
| 238 |
{ |
| 239 |
std::multimap<float, Identifier> result; |
| 240 |
|
| 241 |
// Check for intersection. |
| 242 |
for (size_t i = 0; i < m_spheres.size(); ++i) { |
| 243 |
const SphereColor& sphere = m_spheres[i]; |
| 244 |
|
| 245 |
Vector3f distance = sphere.center - rayOrigin; |
| 246 |
float B = distance.dot(rayDirection); |
| 247 |
float C = distance.dot(distance) - (sphere.radius * sphere.radius); |
| 248 |
float D = B * B - C; |
| 249 |
|
| 250 |
// Test for intersection |
| 251 |
if (D < 0.0f) |
| 252 |
continue; |
| 253 |
|
| 254 |
// Test for clipping |
| 255 |
if (B < 0.0f || (sphere.center - rayEnd).dot(rayDirection) > 0.0f) |
| 256 |
continue; |
| 257 |
|
| 258 |
Identifier id; |
| 259 |
id.molecule = m_identifier.molecule; |
| 260 |
id.type = m_identifier.type; |
| 261 |
id.index = m_indices[i]; |
| 262 |
if (id.type != InvalidType) { |
| 263 |
auto rootD = static_cast<float>(sqrt(D)); |
| 264 |
float depth = std::min(std::abs(B + rootD), std::abs(B - rootD)); |
| 265 |
result.insert(std::pair<float, Identifier>(depth, id)); |
| 266 |
} |
| 267 |
} |
| 268 |
return result; |
| 269 |
} |
| 270 |
|
| 271 |
Array<Identifier> SphereGeometry::areaHits(const Frustrum& f) const |
| 272 |
{ |
| 273 |
Array<Identifier> result; |
| 274 |
|
| 275 |
// Check for intersection. |
| 276 |
for (size_t i = 0; i < m_spheres.size(); ++i) { |
| 277 |
const SphereColor& sphere = m_spheres[i]; |
| 278 |
|
| 279 |
bool inside = true; |
| 280 |
for (int p = 0; p < 4; ++p) { |
| 281 |
// Distance from sphere center to plane (using any point on the plane) |
| 282 |
float dist = (sphere.center - f.points[2 * p]).dot(f.planes[p]); |
| 283 |
|
| 284 |
// If sphere center is outside this plane (positive distance) |
| 285 |
// Note: could also test dist > -sphere.radius for sphere-plane |
| 286 |
// intersection |
| 287 |
if (dist > 0.0f) { |
| 288 |
inside = false; |
| 289 |
break; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
if (inside) { |
| 294 |
Identifier id; |
| 295 |
id.molecule = m_identifier.molecule; |
| 296 |
id.type = m_identifier.type; |
| 297 |
id.index = m_indices[i]; |
| 298 |
result.push_back(id); |
| 299 |
} |
| 300 |
} |
| 301 |
return result; |
| 302 |
} |
| 303 |
|
| 304 |
void SphereGeometry::addSphere(const Vector3f& position, const Vector3ub& color, |
| 305 |
float radius, size_t index) |
| 306 |
{ |
| 307 |
m_dirty = true; |
| 308 |
m_spheres.push_back(SphereColor(position, radius, color)); |
| 309 |
m_indices.push_back(index == MaxIndex ? m_indices.size() : index); |
| 310 |
} |
| 311 |
|
| 312 |
void SphereGeometry::clear() |
| 313 |
{ |
| 314 |
m_spheres.clear(); |
| 315 |
m_indices.clear(); |
| 316 |
} |
| 317 |
|
| 318 |
} // namespace Avogadro::Rendering |