| 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 "volumegeometry.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 "visitor.h" |
| 15 |
|
| 16 |
#include <string> |
| 17 |
#include <fstream> |
| 18 |
#include <sstream> |
| 19 |
#include <iostream> |
| 20 |
|
| 21 |
#include "volume_box_fs.h" // bounding‐box fragment shader |
| 22 |
#include "volume_box_vs.h" // bounding‐box vertex shader |
| 23 |
#include "volume_fs.h" // main volume fragment shader |
| 24 |
#include "volume_vs.h" // main volume vertex shader |
| 25 |
|
| 26 |
namespace Avogadro::Rendering { |
| 27 |
|
| 28 |
namespace { |
| 29 |
// A simple fullscreen quad (two triangles). |
| 30 |
static const GLfloat s_fullscreenQuad[] = { |
| 31 |
-1.0f, -1.0f, 0.0f, // Bottom-left |
| 32 |
1.0f, -1.0f, 0.0f, // Bottom-right |
| 33 |
-1.0f, 1.0f, 0.0f, // Top-left |
| 34 |
|
| 35 |
-1.0f, 1.0f, 0.0f, // Top-left |
| 36 |
1.0f, -1.0f, 0.0f, // Bottom-right |
| 37 |
1.0f, 1.0f, 0.0f // Top-right |
| 38 |
}; |
| 39 |
|
| 40 |
// A simple box (cube) for front/back passes |
| 41 |
static const GLfloat boxVertices[] = { |
| 42 |
// x y z |
| 43 |
// Front face |
| 44 |
-1.0f, |
| 45 |
-1.0f, |
| 46 |
1.0f, |
| 47 |
1.0f, |
| 48 |
-1.0f, |
| 49 |
1.0f, |
| 50 |
1.0f, |
| 51 |
1.0f, |
| 52 |
1.0f, |
| 53 |
-1.0f, |
| 54 |
1.0f, |
| 55 |
1.0f, |
| 56 |
|
| 57 |
// Back face |
| 58 |
-1.0f, |
| 59 |
-1.0f, |
| 60 |
-1.0f, |
| 61 |
1.0f, |
| 62 |
-1.0f, |
| 63 |
-1.0f, |
| 64 |
1.0f, |
| 65 |
1.0f, |
| 66 |
-1.0f, |
| 67 |
-1.0f, |
| 68 |
1.0f, |
| 69 |
-1.0f, |
| 70 |
}; |
| 71 |
|
| 72 |
static const GLuint boxIndices[] = { |
| 73 |
// front |
| 74 |
0, |
| 75 |
1, |
| 76 |
2, |
| 77 |
0, |
| 78 |
2, |
| 79 |
3, |
| 80 |
// right |
| 81 |
1, |
| 82 |
5, |
| 83 |
6, |
| 84 |
1, |
| 85 |
6, |
| 86 |
2, |
| 87 |
// back |
| 88 |
5, |
| 89 |
4, |
| 90 |
7, |
| 91 |
5, |
| 92 |
7, |
| 93 |
6, |
| 94 |
// left |
| 95 |
4, |
| 96 |
0, |
| 97 |
3, |
| 98 |
4, |
| 99 |
3, |
| 100 |
7, |
| 101 |
// top |
| 102 |
2, |
| 103 |
6, |
| 104 |
7, |
| 105 |
2, |
| 106 |
7, |
| 107 |
3, |
| 108 |
// bottom |
| 109 |
4, |
| 110 |
5, |
| 111 |
1, |
| 112 |
4, |
| 113 |
1, |
| 114 |
0, |
| 115 |
}; |
| 116 |
|
| 117 |
void initializeFramebuffers(GLuint* outFBO, GLuint* texRGB, GLuint* texDepth, |
| 118 |
int width, int height) |
| 119 |
{ |
| 120 |
glGenFramebuffers(1, outFBO); |
| 121 |
glBindFramebuffer(GL_FRAMEBUFFER, *outFBO); |
| 122 |
|
| 123 |
// Color attachment. |
| 124 |
glGenTextures(1, texRGB); |
| 125 |
glBindTexture(GL_TEXTURE_2D, *texRGB); |
| 126 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 127 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 128 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, |
| 129 |
GL_UNSIGNED_BYTE, nullptr); |
| 130 |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 131 |
*texRGB, 0); |
| 132 |
|
| 133 |
// Depth attachment. |
| 134 |
glGenTextures(1, texDepth); |
| 135 |
glBindTexture(GL_TEXTURE_2D, *texDepth); |
| 136 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 137 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 138 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 139 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 140 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, |
| 141 |
GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); |
| 142 |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, |
| 143 |
*texDepth, 0); |
| 144 |
|
| 145 |
// Check for completeness. |
| 146 |
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 147 |
if (status != GL_FRAMEBUFFER_COMPLETE) { |
| 148 |
std::cerr << "Error: Framebuffer incomplete: 0x" << std::hex << status |
| 149 |
<< std::endl; |
| 150 |
} else { |
| 151 |
#ifndef NDEBUG |
| 152 |
std::cout << "Framebuffer complete.\n"; |
| 153 |
#endif |
| 154 |
} |
| 155 |
|
| 156 |
// Unbind the FBO: |
| 157 |
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 158 |
} |
| 159 |
|
| 160 |
// Floating-point framebuffer for storing world positions |
| 161 |
void initializePositionFramebuffer(GLuint* outFBO, GLuint* texRGB, |
| 162 |
GLuint* texDepth, int width, int height) |
| 163 |
{ |
| 164 |
glGenFramebuffers(1, outFBO); |
| 165 |
glBindFramebuffer(GL_FRAMEBUFFER, *outFBO); |
| 166 |
|
| 167 |
// Color attachment - use floating point for world positions |
| 168 |
glGenTextures(1, texRGB); |
| 169 |
glBindTexture(GL_TEXTURE_2D, *texRGB); |
| 170 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 171 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 172 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 173 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 174 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, |
| 175 |
nullptr); |
| 176 |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 177 |
*texRGB, 0); |
| 178 |
|
| 179 |
// Depth attachment. |
| 180 |
glGenTextures(1, texDepth); |
| 181 |
glBindTexture(GL_TEXTURE_2D, *texDepth); |
| 182 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 183 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 184 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 185 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 186 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, |
| 187 |
GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); |
| 188 |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, |
| 189 |
*texDepth, 0); |
| 190 |
|
| 191 |
// Check for completeness. |
| 192 |
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 193 |
if (status != GL_FRAMEBUFFER_COMPLETE) { |
| 194 |
std::cerr << "Error: Position framebuffer incomplete: 0x" << std::hex |
| 195 |
<< status << std::endl; |
| 196 |
} |
| 197 |
|
| 198 |
// Unbind the FBO: |
| 199 |
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 200 |
} |
| 201 |
|
| 202 |
} // end anonymous namespace |
| 203 |
|
| 204 |
class VolumeGeometry::Private |
| 205 |
{ |
| 206 |
public: |
| 207 |
Private() |
| 208 |
: vertexShader(nullptr), fragmentShader(nullptr), program(nullptr), vao(0), |
| 209 |
vbo(0), defaultFBO(0), renderFBO(0), renderTexture(0), depthTexture(0), |
| 210 |
frontFBO(0), frontColorTexture(0), frontDepthTexture(0), backFBO(0), |
| 211 |
backColorTexture(0), backDepthTexture(0), boxVertexShader(nullptr), |
| 212 |
boxFragmentShader(nullptr), boxShaders(nullptr), transferTexture(0), |
| 213 |
volumeBoxVao(0), volumeBoxVbo(0), volumeBoxEbo(0), volumeTexture(0) |
| 214 |
{ |
| 215 |
} |
| 216 |
|
| 217 |
// GL resources |
| 218 |
Shader* vertexShader; |
| 219 |
Shader* fragmentShader; |
| 220 |
ShaderProgram* program; |
| 221 |
|
| 222 |
GLuint vao; |
| 223 |
GLuint vbo; |
| 224 |
|
| 225 |
GLuint frontFBO; |
| 226 |
GLuint frontColorTexture; |
| 227 |
GLuint frontDepthTexture; |
| 228 |
|
| 229 |
GLuint backFBO; |
| 230 |
GLuint backColorTexture; |
| 231 |
GLuint backDepthTexture; |
| 232 |
|
| 233 |
Shader* boxVertexShader; |
| 234 |
Shader* boxFragmentShader; |
| 235 |
ShaderProgram* boxShaders; |
| 236 |
|
| 237 |
GLuint transferTexture; |
| 238 |
GLuint volumeBoxVao; |
| 239 |
GLuint volumeBoxVbo; |
| 240 |
GLuint volumeBoxEbo; |
| 241 |
GLuint volumeTexture; |
| 242 |
|
| 243 |
GLuint defaultFBO; |
| 244 |
GLuint renderFBO; |
| 245 |
GLuint renderTexture; |
| 246 |
GLuint depthTexture; |
| 247 |
}; |
| 248 |
|
| 249 |
VolumeGeometry::VolumeGeometry() |
| 250 |
: m_cube(nullptr), m_dirty(true), m_positiveColor(255, 0, 0), |
| 251 |
m_negativeColor(0, 0, 255), d(new Private) |
| 252 |
{ |
| 253 |
} |
| 254 |
|
| 255 |
VolumeGeometry::~VolumeGeometry() |
| 256 |
{ |
| 257 |
end(); |
| 258 |
delete d; |
| 259 |
} |
| 260 |
|
| 261 |
VolumeGeometry::VolumeGeometry(const VolumeGeometry& other) |
| 262 |
: Drawable(other), m_positiveColor(other.m_positiveColor), |
| 263 |
m_negativeColor(other.m_negativeColor), m_cube(other.m_cube), |
| 264 |
m_dirty(other.m_dirty), d(new Private(*other.d)), m_width(other.m_width), |
| 265 |
m_height(other.m_height) |
| 266 |
{ |
| 267 |
} |
| 268 |
|
| 269 |
VolumeGeometry& VolumeGeometry::operator=(VolumeGeometry other) |
| 270 |
{ |
| 271 |
swap(*this, other); |
| 272 |
return *this; |
| 273 |
} |
| 274 |
|
| 275 |
void swap(VolumeGeometry& lhs, VolumeGeometry& rhs) |
| 276 |
{ |
| 277 |
using std::swap; |
| 278 |
swap(lhs.m_cube, rhs.m_cube); |
| 279 |
swap(lhs.m_dirty, rhs.m_dirty); |
| 280 |
swap(lhs.m_positiveColor, rhs.m_positiveColor); |
| 281 |
swap(lhs.m_negativeColor, rhs.m_negativeColor); |
| 282 |
swap(static_cast<Drawable&>(lhs), static_cast<Drawable&>(rhs)); |
| 283 |
swap(lhs.d, rhs.d); |
| 284 |
swap(lhs.m_width, rhs.m_width); |
| 285 |
swap(lhs.m_height, rhs.m_height); |
| 286 |
} |
| 287 |
|
| 288 |
void VolumeGeometry::accept(Visitor& visitor) |
| 289 |
{ |
| 290 |
visitor.visit(*this); |
| 291 |
} |
| 292 |
|
| 293 |
void VolumeGeometry::clear() |
| 294 |
{ |
| 295 |
m_cube = nullptr; |
| 296 |
m_dirty = true; |
| 297 |
} |
| 298 |
|
| 299 |
void VolumeGeometry::setCube(const Core::Cube& cube) |
| 300 |
{ |
| 301 |
m_cube = &cube; |
| 302 |
m_dirty = true; |
| 303 |
} |
| 304 |
|
| 305 |
void VolumeGeometry::resizeFBO(int newWidth, int newHeight) |
| 306 |
{ |
| 307 |
m_width = newWidth; |
| 308 |
m_height = newHeight; |
| 309 |
|
| 310 |
if (d->renderFBO) { |
| 311 |
glDeleteFramebuffers(1, &d->renderFBO); |
| 312 |
d->renderFBO = 0; |
| 313 |
} |
| 314 |
if (d->renderTexture) { |
| 315 |
glDeleteTextures(1, &d->renderTexture); |
| 316 |
d->renderTexture = 0; |
| 317 |
} |
| 318 |
if (d->depthTexture) { |
| 319 |
glDeleteTextures(1, &d->depthTexture); |
| 320 |
d->depthTexture = 0; |
| 321 |
} |
| 322 |
|
| 323 |
if (d->frontFBO) { |
| 324 |
glDeleteFramebuffers(1, &d->frontFBO); |
| 325 |
d->frontFBO = 0; |
| 326 |
} |
| 327 |
if (d->frontColorTexture) { |
| 328 |
glDeleteTextures(1, &d->frontColorTexture); |
| 329 |
d->frontColorTexture = 0; |
| 330 |
} |
| 331 |
if (d->frontDepthTexture) { |
| 332 |
glDeleteTextures(1, &d->frontDepthTexture); |
| 333 |
d->frontDepthTexture = 0; |
| 334 |
} |
| 335 |
|
| 336 |
if (d->backFBO) { |
| 337 |
glDeleteFramebuffers(1, &d->backFBO); |
| 338 |
d->backFBO = 0; |
| 339 |
} |
| 340 |
if (d->backColorTexture) { |
| 341 |
glDeleteTextures(1, &d->backColorTexture); |
| 342 |
d->backColorTexture = 0; |
| 343 |
} |
| 344 |
if (d->backDepthTexture) { |
| 345 |
glDeleteTextures(1, &d->backDepthTexture); |
| 346 |
d->backDepthTexture = 0; |
| 347 |
} |
| 348 |
|
| 349 |
initializeFramebuffers(&d->renderFBO, &d->renderTexture, &d->depthTexture, |
| 350 |
m_width, m_height); |
| 351 |
|
| 352 |
// Use floating-point framebuffers for position storage |
| 353 |
initializePositionFramebuffer(&d->backFBO, &d->backColorTexture, |
| 354 |
&d->backDepthTexture, m_width, m_height); |
| 355 |
|
| 356 |
initializePositionFramebuffer(&d->frontFBO, &d->frontColorTexture, |
| 357 |
&d->frontDepthTexture, m_width, m_height); |
| 358 |
} |
| 359 |
|
| 360 |
void VolumeGeometry::initialize() |
| 361 |
{ |
| 362 |
if (!m_cube || !m_dirty) |
| 363 |
return; |
| 364 |
|
| 365 |
GLint vp[4]; |
| 366 |
glGetIntegerv(GL_VIEWPORT, vp); |
| 367 |
int currentW = vp[2]; |
| 368 |
int currentH = vp[3]; |
| 369 |
|
| 370 |
// Build (or rebuild) the volume texture if needed: |
| 371 |
{ |
| 372 |
int nx = m_cube->nx(); |
| 373 |
int ny = m_cube->ny(); |
| 374 |
int nz = m_cube->nz(); |
| 375 |
std::vector<float> volumeData(nx * ny * nz); |
| 376 |
const float* src = m_cube->data()->data(); |
| 377 |
|
| 378 |
// Reorder data to x‐fastest if needed |
| 379 |
for (int z = 0; z < nz; ++z) { |
| 380 |
for (int y = 0; y < ny; ++y) { |
| 381 |
for (int x = 0; x < nx; ++x) { |
| 382 |
int oldIndex = z + nz * (y + ny * x); |
| 383 |
int newIndex = x + nx * (y + ny * z); |
| 384 |
volumeData[newIndex] = src[oldIndex]; |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
glGenTextures(1, &d->volumeTexture); |
| 390 |
glBindTexture(GL_TEXTURE_3D, d->volumeTexture); |
| 391 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 392 |
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 393 |
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 394 |
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 395 |
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 396 |
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); |
| 397 |
|
| 398 |
glTexImage3D(GL_TEXTURE_3D, 0, GL_R32F, nx, ny, nz, 0, GL_RED, GL_FLOAT, |
| 399 |
volumeData.data()); |
| 400 |
} |
| 401 |
|
| 402 |
// Simple 1D transfer function texture: |
| 403 |
{ |
| 404 |
std::vector<unsigned char> tfData(256 * 4); |
| 405 |
for (int i = 0; i < 256; ++i) { |
| 406 |
float t = float(i) / 255.0f; |
| 407 |
tfData[i * 4 + 0] = static_cast<unsigned char>(128.0f * t); // R |
| 408 |
tfData[i * 4 + 1] = static_cast<unsigned char>(128.0f); // G |
| 409 |
tfData[i * 4 + 2] = static_cast<unsigned char>(0.0f); // B |
| 410 |
tfData[i * 4 + 3] = static_cast<unsigned char>(255.0f * t); // A |
| 411 |
} |
| 412 |
|
| 413 |
glGenTextures(1, &d->transferTexture); |
| 414 |
glBindTexture(GL_TEXTURE_2D, d->transferTexture); |
| 415 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 416 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 417 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 418 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 419 |
|
| 420 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 1, 0, GL_RGBA, |
| 421 |
GL_UNSIGNED_BYTE, tfData.data()); |
| 422 |
glBindTexture(GL_TEXTURE_2D, 0); |
| 423 |
} |
| 424 |
|
| 425 |
// Box geometry for front/back pass: |
| 426 |
{ |
| 427 |
glGenVertexArrays(1, &d->volumeBoxVao); |
| 428 |
glBindVertexArray(d->volumeBoxVao); |
| 429 |
|
| 430 |
glGenBuffers(1, &d->volumeBoxVbo); |
| 431 |
glBindBuffer(GL_ARRAY_BUFFER, d->volumeBoxVbo); |
| 432 |
glBufferData(GL_ARRAY_BUFFER, sizeof(boxVertices), boxVertices, |
| 433 |
GL_STATIC_DRAW); |
| 434 |
glEnableVertexAttribArray(0); |
| 435 |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), |
| 436 |
(GLvoid*)0); |
| 437 |
|
| 438 |
glGenBuffers(1, &d->volumeBoxEbo); |
| 439 |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, d->volumeBoxEbo); |
| 440 |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(boxIndices), boxIndices, |
| 441 |
GL_STATIC_DRAW); |
| 442 |
|
| 443 |
glBindVertexArray(0); |
| 444 |
} |
| 445 |
|
| 446 |
// Fullscreen quad geometry: |
| 447 |
{ |
| 448 |
glGenBuffers(1, &d->vbo); |
| 449 |
glBindBuffer(GL_ARRAY_BUFFER, d->vbo); |
| 450 |
glBufferData(GL_ARRAY_BUFFER, sizeof(s_fullscreenQuad), s_fullscreenQuad, |
| 451 |
GL_STATIC_DRAW); |
| 452 |
glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 453 |
|
| 454 |
glGenVertexArrays(1, &d->vao); |
| 455 |
glBindVertexArray(d->vao); |
| 456 |
glBindBuffer(GL_ARRAY_BUFFER, d->vbo); |
| 457 |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, |
| 458 |
reinterpret_cast<void*>(0)); |
| 459 |
glEnableVertexAttribArray(0); |
| 460 |
glBindVertexArray(0); |
| 461 |
} |
| 462 |
|
| 463 |
// Create (or re‐create) our FBOs at this newly detected size: |
| 464 |
resizeFBO(currentW, currentH); |
| 465 |
|
| 466 |
// Main volume shaders: |
| 467 |
if (!d->vertexShader) { |
| 468 |
d->vertexShader = new Shader(Shader::Vertex); |
| 469 |
d->vertexShader->setSource(volume_vs); |
| 470 |
if (!d->vertexShader->compile()) |
| 471 |
std::cerr << "VolumeGeometry: Vertex shader compile failed.\n"; |
| 472 |
} |
| 473 |
|
| 474 |
if (!d->fragmentShader) { |
| 475 |
d->fragmentShader = new Shader(Shader::Fragment); |
| 476 |
d->fragmentShader->setSource(volume_fs); |
| 477 |
if (!d->fragmentShader->compile()) |
| 478 |
std::cout << d->fragmentShader->error() << std::endl; |
| 479 |
} |
| 480 |
|
| 481 |
if (!d->program) { |
| 482 |
d->program = new ShaderProgram(); |
| 483 |
d->program->attachShader(*d->vertexShader); |
| 484 |
d->program->attachShader(*d->fragmentShader); |
| 485 |
if (!d->program->link()) |
| 486 |
std::cerr << "VolumeGeometry: Shader program linking failed.\n"; |
| 487 |
} |
| 488 |
|
| 489 |
// Box pass shaders (for front/back): |
| 490 |
if (!d->boxVertexShader) { |
| 491 |
d->boxVertexShader = new Shader(Shader::Vertex); |
| 492 |
d->boxVertexShader->setSource(volume_box_vs); |
| 493 |
if (!d->boxVertexShader->compile()) |
| 494 |
std::cerr << "VolumeGeometry: Box vertex shader compile failed.\n"; |
| 495 |
} |
| 496 |
|
| 497 |
if (!d->boxFragmentShader) { |
| 498 |
d->boxFragmentShader = new Shader(Shader::Fragment); |
| 499 |
d->boxFragmentShader->setSource(volume_box_fs); |
| 500 |
if (!d->boxFragmentShader->compile()) |
| 501 |
std::cerr << "VolumeGeometry: Box fragment shader compile failed.\n"; |
| 502 |
} |
| 503 |
|
| 504 |
if (!d->boxShaders) { |
| 505 |
d->boxShaders = new ShaderProgram(); |
| 506 |
d->boxShaders->attachShader(*d->boxVertexShader); |
| 507 |
d->boxShaders->attachShader(*d->boxFragmentShader); |
| 508 |
if (!d->boxShaders->link()) |
| 509 |
std::cerr << "VolumeGeometry: Box shader program linking failed.\n"; |
| 510 |
} |
| 511 |
|
| 512 |
// Done initializing: |
| 513 |
m_dirty = false; |
| 514 |
} |
| 515 |
|
| 516 |
void VolumeGeometry::render(const Camera& camera) |
| 517 |
{ |
| 518 |
if (!m_cube) |
| 519 |
return; |
| 520 |
|
| 521 |
// Capture the default FBO before initialize() which may change it |
| 522 |
// (resizeFBO creates new framebuffers and unbinds to FBO 0, losing |
| 523 |
// the Qt widget's actual default FBO) |
| 524 |
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&d->defaultFBO); |
| 525 |
|
| 526 |
if (m_dirty) |
| 527 |
initialize(); |
| 528 |
|
| 529 |
GLint vp[4]; |
| 530 |
glGetIntegerv(GL_VIEWPORT, vp); |
| 531 |
int newW = vp[2]; |
| 532 |
int newH = vp[3]; |
| 533 |
if (newW != m_width || newH != m_height) { |
| 534 |
m_dirty = true; |
| 535 |
initialize(); |
| 536 |
} |
| 537 |
|
| 538 |
// Get cube bounds in world space |
| 539 |
Eigen::Vector3f boxMin = m_cube->min().cast<float>(); |
| 540 |
Eigen::Vector3f boxMax = m_cube->max().cast<float>(); |
| 541 |
Eigen::Matrix4f mv = camera.modelView().matrix(); |
| 542 |
Eigen::Matrix4f proj = camera.projection().matrix(); |
| 543 |
|
| 544 |
// Compute camera position in world space from inverse modelView |
| 545 |
Eigen::Matrix4f mvInv = mv.inverse(); |
| 546 |
Eigen::Vector3f cameraPos = mvInv.block<3, 1>(0, 3); |
| 547 |
|
| 548 |
// Compute view direction (for orthographic projection) |
| 549 |
// View direction in camera space is -Z, transform to world space |
| 550 |
Eigen::Vector3f viewDirWorld = |
| 551 |
(mvInv.block<3, 3>(0, 0) * Eigen::Vector3f(0, 0, -1)).normalized(); |
| 552 |
|
| 553 |
// Get projection type (0 = perspective, 1 = orthographic) |
| 554 |
int projType = (camera.projectionType() == Projection::Orthographic) ? 1 : 0; |
| 555 |
|
| 556 |
// 2) Render the BACK faces of bounding box (exit points): |
| 557 |
{ |
| 558 |
glBindFramebuffer(GL_FRAMEBUFFER, d->backFBO); |
| 559 |
glViewport(0, 0, m_width, m_height); |
| 560 |
glEnable(GL_DEPTH_TEST); |
| 561 |
glEnable(GL_CULL_FACE); |
| 562 |
|
| 563 |
glCullFace(GL_FRONT); |
| 564 |
// Use sentinel value for clear color to detect "no hit" pixels |
| 565 |
glClearColor(-1e6f, -1e6f, -1e6f, 0.f); |
| 566 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 567 |
|
| 568 |
d->boxShaders->bind(); |
| 569 |
|
| 570 |
d->boxShaders->setUniformValue("uModelView", mv); |
| 571 |
d->boxShaders->setUniformValue("uProjection", proj); |
| 572 |
d->boxShaders->setUniformValue("uBoxMin", boxMin); |
| 573 |
d->boxShaders->setUniformValue("uBoxMax", boxMax); |
| 574 |
|
| 575 |
glBindVertexArray(d->volumeBoxVao); |
| 576 |
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); |
| 577 |
glBindVertexArray(0); |
| 578 |
|
| 579 |
d->boxShaders->release(); |
| 580 |
|
| 581 |
glDisable(GL_CULL_FACE); |
| 582 |
glDisable(GL_DEPTH_TEST); |
| 583 |
} |
| 584 |
|
| 585 |
// 3) Render the FRONT faces of bounding box (entry points): |
| 586 |
{ |
| 587 |
glBindFramebuffer(GL_FRAMEBUFFER, d->frontFBO); |
| 588 |
glViewport(0, 0, m_width, m_height); |
| 589 |
glEnable(GL_DEPTH_TEST); |
| 590 |
glEnable(GL_CULL_FACE); |
| 591 |
|
| 592 |
glCullFace(GL_BACK); |
| 593 |
// Use sentinel value for clear color to detect "no hit" pixels |
| 594 |
glClearColor(-1e6f, -1e6f, -1e6f, 0.f); |
| 595 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 596 |
|
| 597 |
d->boxShaders->bind(); |
| 598 |
|
| 599 |
d->boxShaders->setUniformValue("uModelView", mv); |
| 600 |
d->boxShaders->setUniformValue("uProjection", proj); |
| 601 |
d->boxShaders->setUniformValue("uBoxMin", boxMin); |
| 602 |
d->boxShaders->setUniformValue("uBoxMax", boxMax); |
| 603 |
|
| 604 |
glBindVertexArray(d->volumeBoxVao); |
| 605 |
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); |
| 606 |
glBindVertexArray(0); |
| 607 |
|
| 608 |
d->boxShaders->release(); |
| 609 |
|
| 610 |
glDisable(GL_CULL_FACE); |
| 611 |
glDisable(GL_DEPTH_TEST); |
| 612 |
} |
| 613 |
|
| 614 |
// 4) Final pass: ray marching into the default framebuffer: |
| 615 |
glBindFramebuffer(GL_FRAMEBUFFER, d->defaultFBO); |
| 616 |
glViewport(0, 0, m_width, m_height); |
| 617 |
|
| 618 |
// Disable depth test and depth write - the fullscreen quad should always draw |
| 619 |
// (ray marching handles occlusion internally) but shouldn't corrupt depth |
| 620 |
// buffer |
| 621 |
glDisable(GL_DEPTH_TEST); |
| 622 |
glDepthMask(GL_FALSE); |
| 623 |
|
| 624 |
// Enable premultiplied alpha blending so volume composites over existing |
| 625 |
// scene |
| 626 |
glEnable(GL_BLEND); |
| 627 |
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 628 |
|
| 629 |
d->program->bind(); |
| 630 |
|
| 631 |
GLint pId = 0; |
| 632 |
glGetIntegerv(GL_CURRENT_PROGRAM, &pId); |
| 633 |
|
| 634 |
// inFrontPosTex -> unit 0 |
| 635 |
{ |
| 636 |
GLint loc = glGetUniformLocation(pId, "inFrontPosTex"); |
| 637 |
if (loc >= 0) { |
| 638 |
glActiveTexture(GL_TEXTURE0); |
| 639 |
glBindTexture(GL_TEXTURE_2D, d->frontColorTexture); |
| 640 |
glUniform1i(loc, 0); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
// inBackPosTex -> unit 1 |
| 645 |
{ |
| 646 |
GLint loc = glGetUniformLocation(pId, "inBackPosTex"); |
| 647 |
if (loc >= 0) { |
| 648 |
glActiveTexture(GL_TEXTURE1); |
| 649 |
glBindTexture(GL_TEXTURE_2D, d->backColorTexture); |
| 650 |
glUniform1i(loc, 1); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
// uVolumeData -> unit 2 |
| 655 |
{ |
| 656 |
GLint loc = glGetUniformLocation(pId, "uVolumeData"); |
| 657 |
if (loc >= 0) { |
| 658 |
glActiveTexture(GL_TEXTURE2); |
| 659 |
glBindTexture(GL_TEXTURE_3D, d->volumeTexture); |
| 660 |
glUniform1i(loc, 2); |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
// Colors, etc. |
| 665 |
Eigen::Vector3f posCol(m_positiveColor[0] / 255.0f, |
| 666 |
m_positiveColor[1] / 255.0f, |
| 667 |
m_positiveColor[2] / 255.0f); |
| 668 |
Eigen::Vector3f negCol(m_negativeColor[0] / 255.0f, |
| 669 |
m_negativeColor[1] / 255.0f, |
| 670 |
m_negativeColor[2] / 255.0f); |
| 671 |
d->program->setUniformValue("width", float(m_width)); |
| 672 |
d->program->setUniformValue("height", float(m_height)); |
| 673 |
d->program->setUniformValue("numSteps", 150); |
| 674 |
d->program->setUniformValue("alphaScale", 0.6f); |
| 675 |
d->program->setUniformValue("positiveColor", posCol); |
| 676 |
d->program->setUniformValue("negativeColor", negCol); |
| 677 |
d->program->setUniformValue("uBoxMin", boxMin); |
| 678 |
d->program->setUniformValue("uBoxMax", boxMax); |
| 679 |
d->program->setUniformValue("uCameraPos", cameraPos); |
| 680 |
d->program->setUniformValue("uViewDir", viewDirWorld); |
| 681 |
d->program->setUniformValue("uProjectionType", projType); |
| 682 |
|
| 683 |
glBindVertexArray(d->vao); |
| 684 |
glDrawArrays(GL_TRIANGLES, 0, 6); |
| 685 |
glBindVertexArray(0); |
| 686 |
|
| 687 |
d->program->release(); |
| 688 |
|
| 689 |
// Restore GL state for other translucent objects |
| 690 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 691 |
glDepthMask(GL_TRUE); |
| 692 |
glEnable(GL_DEPTH_TEST); |
| 693 |
} |
| 694 |
|
| 695 |
void VolumeGeometry::end() |
| 696 |
{ |
| 697 |
if (d->vbo) { |
| 698 |
glDeleteBuffers(1, &d->vbo); |
| 699 |
d->vbo = 0; |
| 700 |
} |
| 701 |
if (d->vao) { |
| 702 |
glDeleteVertexArrays(1, &d->vao); |
| 703 |
d->vao = 0; |
| 704 |
} |
| 705 |
if (d->renderTexture) { |
| 706 |
glDeleteTextures(1, &d->renderTexture); |
| 707 |
d->renderTexture = 0; |
| 708 |
} |
| 709 |
if (d->depthTexture) { |
| 710 |
glDeleteTextures(1, &d->depthTexture); |
| 711 |
d->depthTexture = 0; |
| 712 |
} |
| 713 |
if (d->renderFBO) { |
| 714 |
glDeleteFramebuffers(1, &d->renderFBO); |
| 715 |
d->renderFBO = 0; |
| 716 |
} |
| 717 |
if (d->backFBO) { |
| 718 |
glDeleteFramebuffers(1, &d->backFBO); |
| 719 |
d->backFBO = 0; |
| 720 |
} |
| 721 |
if (d->backColorTexture) { |
| 722 |
glDeleteTextures(1, &d->backColorTexture); |
| 723 |
d->backColorTexture = 0; |
| 724 |
} |
| 725 |
if (d->backDepthTexture) { |
| 726 |
glDeleteTextures(1, &d->backDepthTexture); |
| 727 |
d->backDepthTexture = 0; |
| 728 |
} |
| 729 |
if (d->frontFBO) { |
| 730 |
glDeleteFramebuffers(1, &d->frontFBO); |
| 731 |
d->frontFBO = 0; |
| 732 |
} |
| 733 |
if (d->frontColorTexture) { |
| 734 |
glDeleteTextures(1, &d->frontColorTexture); |
| 735 |
d->frontColorTexture = 0; |
| 736 |
} |
| 737 |
if (d->frontDepthTexture) { |
| 738 |
glDeleteTextures(1, &d->frontDepthTexture); |
| 739 |
d->frontDepthTexture = 0; |
| 740 |
} |
| 741 |
if (d->volumeBoxVbo) { |
| 742 |
glDeleteBuffers(1, &d->volumeBoxVbo); |
| 743 |
d->volumeBoxVbo = 0; |
| 744 |
} |
| 745 |
if (d->volumeBoxEbo) { |
| 746 |
glDeleteBuffers(1, &d->volumeBoxEbo); |
| 747 |
d->volumeBoxEbo = 0; |
| 748 |
} |
| 749 |
if (d->volumeBoxVao) { |
| 750 |
glDeleteVertexArrays(1, &d->volumeBoxVao); |
| 751 |
d->volumeBoxVao = 0; |
| 752 |
} |
| 753 |
if (d->volumeTexture) { |
| 754 |
glDeleteTextures(1, &d->volumeTexture); |
| 755 |
d->volumeTexture = 0; |
| 756 |
} |
| 757 |
if (d->transferTexture) { |
| 758 |
glDeleteTextures(1, &d->transferTexture); |
| 759 |
d->transferTexture = 0; |
| 760 |
} |
| 761 |
|
| 762 |
if (d->boxShaders) { |
| 763 |
delete d->boxShaders; |
| 764 |
d->boxShaders = nullptr; |
| 765 |
} |
| 766 |
if (d->boxVertexShader) { |
| 767 |
delete d->boxVertexShader; |
| 768 |
d->boxVertexShader = nullptr; |
| 769 |
} |
| 770 |
if (d->boxFragmentShader) { |
| 771 |
delete d->boxFragmentShader; |
| 772 |
d->boxFragmentShader = nullptr; |
| 773 |
} |
| 774 |
|
| 775 |
if (d->program) { |
| 776 |
delete d->program; |
| 777 |
d->program = nullptr; |
| 778 |
} |
| 779 |
if (d->vertexShader) { |
| 780 |
delete d->vertexShader; |
| 781 |
d->vertexShader = nullptr; |
| 782 |
} |
| 783 |
if (d->fragmentShader) { |
| 784 |
delete d->fragmentShader; |
| 785 |
d->fragmentShader = nullptr; |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
} // End namespace Avogadro::Rendering |