| 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 "ambientocclusionspheregeometry.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 "sphere_ao_bake_fs.h" |
| 21 |
#include "sphere_ao_bake_vs.h" |
| 22 |
#include "sphere_ao_depth_fs.h" |
| 23 |
#include "sphere_ao_depth_vs.h" |
| 24 |
#include "sphere_ao_render_fs.h" |
| 25 |
#include "sphere_ao_render_vs.h" |
| 26 |
|
| 27 |
const int num_ao_points = 162; |
| 28 |
const float ao_points[] = { |
| 29 |
0.850650808352f, |
| 30 |
0.525731112119f, |
| 31 |
0.0f, |
| 32 |
-0.850650808352f, |
| 33 |
0.525731112119f, |
| 34 |
0.0f, |
| 35 |
0.850650808352f, |
| 36 |
-0.525731112119f, |
| 37 |
0.0f, |
| 38 |
-0.850650808352f, |
| 39 |
-0.525731112119f, |
| 40 |
0.0f, |
| 41 |
0.525731112119f, |
| 42 |
0.0f, |
| 43 |
0.850650808352f, |
| 44 |
0.525731112119f, |
| 45 |
0.0f, |
| 46 |
-0.850650808352f, |
| 47 |
-0.525731112119f, |
| 48 |
0.0f, |
| 49 |
0.850650808352f, |
| 50 |
-0.525731112119f, |
| 51 |
0.0f, |
| 52 |
-0.850650808352f, |
| 53 |
0.0f, |
| 54 |
0.850650808352f, |
| 55 |
0.525731112119f, |
| 56 |
0.0f, |
| 57 |
-0.850650808352f, |
| 58 |
0.525731112119f, |
| 59 |
0.0f, |
| 60 |
0.850650808352f, |
| 61 |
-0.525731112119f, |
| 62 |
0.0f, |
| 63 |
-0.850650808352f, |
| 64 |
-0.525731112119f, |
| 65 |
0.5f, |
| 66 |
0.809016994375f, |
| 67 |
0.309016994375f, |
| 68 |
0.309016994375f, |
| 69 |
0.5f, |
| 70 |
0.809016994375f, |
| 71 |
0.809016994375f, |
| 72 |
0.309016994375f, |
| 73 |
0.5f, |
| 74 |
0.809016994375f, |
| 75 |
0.309016994375f, |
| 76 |
-0.5f, |
| 77 |
0.309016994375f, |
| 78 |
0.5f, |
| 79 |
-0.809016994375f, |
| 80 |
0.5f, |
| 81 |
0.809016994375f, |
| 82 |
-0.309016994375f, |
| 83 |
0.809016994375f, |
| 84 |
-0.309016994375f, |
| 85 |
0.5f, |
| 86 |
0.309016994375f, |
| 87 |
-0.5f, |
| 88 |
0.809016994375f, |
| 89 |
0.5f, |
| 90 |
-0.809016994375f, |
| 91 |
0.309016994375f, |
| 92 |
0.5f, |
| 93 |
-0.809016994375f, |
| 94 |
-0.309016994375f, |
| 95 |
0.309016994375f, |
| 96 |
-0.5f, |
| 97 |
-0.809016994375f, |
| 98 |
0.809016994375f, |
| 99 |
-0.309016994375f, |
| 100 |
-0.5f, |
| 101 |
-0.809016994375f, |
| 102 |
0.309016994375f, |
| 103 |
0.5f, |
| 104 |
-0.309016994375f, |
| 105 |
0.5f, |
| 106 |
0.809016994375f, |
| 107 |
-0.5f, |
| 108 |
0.809016994375f, |
| 109 |
0.309016994375f, |
| 110 |
-0.5f, |
| 111 |
0.809016994375f, |
| 112 |
-0.309016994375f, |
| 113 |
-0.309016994375f, |
| 114 |
0.5f, |
| 115 |
-0.809016994375f, |
| 116 |
-0.809016994375f, |
| 117 |
0.309016994375f, |
| 118 |
-0.5f, |
| 119 |
-0.5f, |
| 120 |
-0.809016994375f, |
| 121 |
0.309016994375f, |
| 122 |
-0.309016994375f, |
| 123 |
-0.5f, |
| 124 |
0.809016994375f, |
| 125 |
-0.809016994375f, |
| 126 |
-0.309016994375f, |
| 127 |
0.5f, |
| 128 |
-0.809016994375f, |
| 129 |
-0.309016994375f, |
| 130 |
-0.5f, |
| 131 |
-0.309016994375f, |
| 132 |
-0.5f, |
| 133 |
-0.809016994375f, |
| 134 |
-0.5f, |
| 135 |
-0.809016994375f, |
| 136 |
-0.309016994375f, |
| 137 |
0.0f, |
| 138 |
1.0f, |
| 139 |
0.0f, |
| 140 |
0.0f, |
| 141 |
-1.0f, |
| 142 |
0.0f, |
| 143 |
1.0f, |
| 144 |
0.0f, |
| 145 |
0.0f, |
| 146 |
-1.0f, |
| 147 |
0.0f, |
| 148 |
0.0f, |
| 149 |
0.0f, |
| 150 |
0.0f, |
| 151 |
1.0f, |
| 152 |
0.0f, |
| 153 |
0.0f, |
| 154 |
-1.0f, |
| 155 |
0.702046444776f, |
| 156 |
0.69378047756f, |
| 157 |
0.16062203564f, |
| 158 |
0.688190960236f, |
| 159 |
0.587785252292f, |
| 160 |
0.425325404176f, |
| 161 |
0.862668480416f, |
| 162 |
0.433888564553f, |
| 163 |
0.259891913008f, |
| 164 |
0.16062203564f, |
| 165 |
0.702046444776f, |
| 166 |
0.69378047756f, |
| 167 |
0.425325404176f, |
| 168 |
0.688190960236f, |
| 169 |
0.587785252292f, |
| 170 |
0.259891913008f, |
| 171 |
0.862668480416f, |
| 172 |
0.433888564553f, |
| 173 |
0.69378047756f, |
| 174 |
0.16062203564f, |
| 175 |
0.702046444776f, |
| 176 |
0.587785252292f, |
| 177 |
0.425325404176f, |
| 178 |
0.688190960236f, |
| 179 |
0.433888564553f, |
| 180 |
0.259891913008f, |
| 181 |
0.862668480416f, |
| 182 |
0.862668480416f, |
| 183 |
0.433888564553f, |
| 184 |
-0.259891913008f, |
| 185 |
0.688190960236f, |
| 186 |
0.587785252292f, |
| 187 |
-0.425325404176f, |
| 188 |
0.702046444776f, |
| 189 |
0.69378047756f, |
| 190 |
-0.16062203564f, |
| 191 |
0.433888564553f, |
| 192 |
0.259891913008f, |
| 193 |
-0.862668480416f, |
| 194 |
0.587785252292f, |
| 195 |
0.425325404176f, |
| 196 |
-0.688190960236f, |
| 197 |
0.69378047756f, |
| 198 |
0.16062203564f, |
| 199 |
-0.702046444776f, |
| 200 |
0.259891913008f, |
| 201 |
0.862668480416f, |
| 202 |
-0.433888564553f, |
| 203 |
0.425325404176f, |
| 204 |
0.688190960236f, |
| 205 |
-0.587785252292f, |
| 206 |
0.16062203564f, |
| 207 |
0.702046444776f, |
| 208 |
-0.69378047756f, |
| 209 |
0.862668480416f, |
| 210 |
-0.433888564553f, |
| 211 |
0.259891913008f, |
| 212 |
0.688190960236f, |
| 213 |
-0.587785252292f, |
| 214 |
0.425325404176f, |
| 215 |
0.702046444776f, |
| 216 |
-0.69378047756f, |
| 217 |
0.16062203564f, |
| 218 |
0.433888564553f, |
| 219 |
-0.259891913008f, |
| 220 |
0.862668480416f, |
| 221 |
0.587785252292f, |
| 222 |
-0.425325404176f, |
| 223 |
0.688190960236f, |
| 224 |
0.69378047756f, |
| 225 |
-0.16062203564f, |
| 226 |
0.702046444776f, |
| 227 |
0.259891913008f, |
| 228 |
-0.862668480416f, |
| 229 |
0.433888564553f, |
| 230 |
0.425325404176f, |
| 231 |
-0.688190960236f, |
| 232 |
0.587785252292f, |
| 233 |
0.16062203564f, |
| 234 |
-0.702046444776f, |
| 235 |
0.69378047756f, |
| 236 |
0.702046444776f, |
| 237 |
-0.69378047756f, |
| 238 |
-0.16062203564f, |
| 239 |
0.688190960236f, |
| 240 |
-0.587785252292f, |
| 241 |
-0.425325404176f, |
| 242 |
0.862668480416f, |
| 243 |
-0.433888564553f, |
| 244 |
-0.259891913008f, |
| 245 |
0.16062203564f, |
| 246 |
-0.702046444776f, |
| 247 |
-0.69378047756f, |
| 248 |
0.425325404176f, |
| 249 |
-0.688190960236f, |
| 250 |
-0.587785252292f, |
| 251 |
0.259891913008f, |
| 252 |
-0.862668480416f, |
| 253 |
-0.433888564553f, |
| 254 |
0.69378047756f, |
| 255 |
-0.16062203564f, |
| 256 |
-0.702046444776f, |
| 257 |
0.587785252292f, |
| 258 |
-0.425325404176f, |
| 259 |
-0.688190960236f, |
| 260 |
0.433888564553f, |
| 261 |
-0.259891913008f, |
| 262 |
-0.862668480416f, |
| 263 |
-0.862668480416f, |
| 264 |
0.433888564553f, |
| 265 |
0.259891913008f, |
| 266 |
-0.688190960236f, |
| 267 |
0.587785252292f, |
| 268 |
0.425325404176f, |
| 269 |
-0.702046444776f, |
| 270 |
0.69378047756f, |
| 271 |
0.16062203564f, |
| 272 |
-0.433888564553f, |
| 273 |
0.259891913008f, |
| 274 |
0.862668480416f, |
| 275 |
-0.587785252292f, |
| 276 |
0.425325404176f, |
| 277 |
0.688190960236f, |
| 278 |
-0.69378047756f, |
| 279 |
0.16062203564f, |
| 280 |
0.702046444776f, |
| 281 |
-0.259891913008f, |
| 282 |
0.862668480416f, |
| 283 |
0.433888564553f, |
| 284 |
-0.425325404176f, |
| 285 |
0.688190960236f, |
| 286 |
0.587785252292f, |
| 287 |
-0.16062203564f, |
| 288 |
0.702046444776f, |
| 289 |
0.69378047756f, |
| 290 |
-0.702046444776f, |
| 291 |
0.69378047756f, |
| 292 |
-0.16062203564f, |
| 293 |
-0.688190960236f, |
| 294 |
0.587785252292f, |
| 295 |
-0.425325404176f, |
| 296 |
-0.862668480416f, |
| 297 |
0.433888564553f, |
| 298 |
-0.259891913008f, |
| 299 |
-0.16062203564f, |
| 300 |
0.702046444776f, |
| 301 |
-0.69378047756f, |
| 302 |
-0.425325404176f, |
| 303 |
0.688190960236f, |
| 304 |
-0.587785252292f, |
| 305 |
-0.259891913008f, |
| 306 |
0.862668480416f, |
| 307 |
-0.433888564553f, |
| 308 |
-0.69378047756f, |
| 309 |
0.16062203564f, |
| 310 |
-0.702046444776f, |
| 311 |
-0.587785252292f, |
| 312 |
0.425325404176f, |
| 313 |
-0.688190960236f, |
| 314 |
-0.433888564553f, |
| 315 |
0.259891913008f, |
| 316 |
-0.862668480416f, |
| 317 |
-0.702046444776f, |
| 318 |
-0.69378047756f, |
| 319 |
0.16062203564f, |
| 320 |
-0.688190960236f, |
| 321 |
-0.587785252292f, |
| 322 |
0.425325404176f, |
| 323 |
-0.862668480416f, |
| 324 |
-0.433888564553f, |
| 325 |
0.259891913008f, |
| 326 |
-0.16062203564f, |
| 327 |
-0.702046444776f, |
| 328 |
0.69378047756f, |
| 329 |
-0.425325404176f, |
| 330 |
-0.688190960236f, |
| 331 |
0.587785252292f, |
| 332 |
-0.259891913008f, |
| 333 |
-0.862668480416f, |
| 334 |
0.433888564553f, |
| 335 |
-0.69378047756f, |
| 336 |
-0.16062203564f, |
| 337 |
0.702046444776f, |
| 338 |
-0.587785252292f, |
| 339 |
-0.425325404176f, |
| 340 |
0.688190960236f, |
| 341 |
-0.433888564553f, |
| 342 |
-0.259891913008f, |
| 343 |
0.862668480416f, |
| 344 |
-0.862668480416f, |
| 345 |
-0.433888564553f, |
| 346 |
-0.259891913008f, |
| 347 |
-0.688190960236f, |
| 348 |
-0.587785252292f, |
| 349 |
-0.425325404176f, |
| 350 |
-0.702046444776f, |
| 351 |
-0.69378047756f, |
| 352 |
-0.16062203564f, |
| 353 |
-0.433888564553f, |
| 354 |
-0.259891913008f, |
| 355 |
-0.862668480416f, |
| 356 |
-0.587785252292f, |
| 357 |
-0.425325404176f, |
| 358 |
-0.688190960236f, |
| 359 |
-0.69378047756f, |
| 360 |
-0.16062203564f, |
| 361 |
-0.702046444776f, |
| 362 |
-0.259891913008f, |
| 363 |
-0.862668480416f, |
| 364 |
-0.433888564553f, |
| 365 |
-0.425325404176f, |
| 366 |
-0.688190960236f, |
| 367 |
-0.587785252292f, |
| 368 |
-0.16062203564f, |
| 369 |
-0.702046444776f, |
| 370 |
-0.69378047756f, |
| 371 |
0.525731112119f, |
| 372 |
0.850650808352f, |
| 373 |
0.0f, |
| 374 |
0.0f, |
| 375 |
0.961938357784f, |
| 376 |
-0.273266528913f, |
| 377 |
0.26286555606f, |
| 378 |
0.951056516295f, |
| 379 |
-0.162459848116f, |
| 380 |
0.26286555606f, |
| 381 |
0.951056516295f, |
| 382 |
0.162459848116f, |
| 383 |
0.0f, |
| 384 |
0.961938357784f, |
| 385 |
0.273266528913f, |
| 386 |
-0.525731112119f, |
| 387 |
0.850650808352f, |
| 388 |
0.0f, |
| 389 |
-0.26286555606f, |
| 390 |
0.951056516295f, |
| 391 |
0.162459848116f, |
| 392 |
-0.26286555606f, |
| 393 |
0.951056516295f, |
| 394 |
-0.162459848116f, |
| 395 |
0.525731112119f, |
| 396 |
-0.850650808352f, |
| 397 |
0.0f, |
| 398 |
0.0f, |
| 399 |
-0.961938357784f, |
| 400 |
0.273266528913f, |
| 401 |
0.26286555606f, |
| 402 |
-0.951056516295f, |
| 403 |
0.162459848116f, |
| 404 |
0.26286555606f, |
| 405 |
-0.951056516295f, |
| 406 |
-0.162459848116f, |
| 407 |
0.0f, |
| 408 |
-0.961938357784f, |
| 409 |
-0.273266528913f, |
| 410 |
-0.525731112119f, |
| 411 |
-0.850650808352f, |
| 412 |
0.0f, |
| 413 |
-0.26286555606f, |
| 414 |
-0.951056516295f, |
| 415 |
0.162459848116f, |
| 416 |
-0.26286555606f, |
| 417 |
-0.951056516295f, |
| 418 |
-0.162459848116f, |
| 419 |
0.850650808352f, |
| 420 |
0.0f, |
| 421 |
0.525731112119f, |
| 422 |
0.961938357784f, |
| 423 |
-0.273266528913f, |
| 424 |
0.0f, |
| 425 |
0.951056516295f, |
| 426 |
-0.162459848116f, |
| 427 |
0.26286555606f, |
| 428 |
0.951056516295f, |
| 429 |
0.162459848116f, |
| 430 |
0.26286555606f, |
| 431 |
0.961938357784f, |
| 432 |
0.273266528913f, |
| 433 |
0.0f, |
| 434 |
0.850650808352f, |
| 435 |
0.0f, |
| 436 |
-0.525731112119f, |
| 437 |
0.951056516295f, |
| 438 |
0.162459848116f, |
| 439 |
-0.26286555606f, |
| 440 |
0.951056516295f, |
| 441 |
-0.162459848116f, |
| 442 |
-0.26286555606f, |
| 443 |
-0.850650808352f, |
| 444 |
0.0f, |
| 445 |
0.525731112119f, |
| 446 |
-0.961938357784f, |
| 447 |
0.273266528913f, |
| 448 |
0.0f, |
| 449 |
-0.951056516295f, |
| 450 |
0.162459848116f, |
| 451 |
0.26286555606f, |
| 452 |
-0.951056516295f, |
| 453 |
-0.162459848116f, |
| 454 |
0.26286555606f, |
| 455 |
-0.961938357784f, |
| 456 |
-0.273266528913f, |
| 457 |
0.0f, |
| 458 |
-0.850650808352f, |
| 459 |
0.0f, |
| 460 |
-0.525731112119f, |
| 461 |
-0.951056516295f, |
| 462 |
-0.162459848116f, |
| 463 |
-0.26286555606f, |
| 464 |
-0.951056516295f, |
| 465 |
0.162459848116f, |
| 466 |
-0.26286555606f, |
| 467 |
0.0f, |
| 468 |
0.525731112119f, |
| 469 |
0.850650808352f, |
| 470 |
-0.273266528913f, |
| 471 |
0.0f, |
| 472 |
0.961938357784f, |
| 473 |
-0.162459848116f, |
| 474 |
0.26286555606f, |
| 475 |
0.951056516295f, |
| 476 |
0.162459848116f, |
| 477 |
0.26286555606f, |
| 478 |
0.951056516295f, |
| 479 |
0.273266528913f, |
| 480 |
0.0f, |
| 481 |
0.961938357784f, |
| 482 |
0.0f, |
| 483 |
-0.525731112119f, |
| 484 |
0.850650808352f, |
| 485 |
0.162459848116f, |
| 486 |
-0.26286555606f, |
| 487 |
0.951056516295f, |
| 488 |
-0.162459848116f, |
| 489 |
-0.26286555606f, |
| 490 |
0.951056516295f, |
| 491 |
0.0f, |
| 492 |
0.525731112119f, |
| 493 |
-0.850650808352f, |
| 494 |
0.273266528913f, |
| 495 |
0.0f, |
| 496 |
-0.961938357784f, |
| 497 |
0.162459848116f, |
| 498 |
0.26286555606f, |
| 499 |
-0.951056516295f, |
| 500 |
-0.162459848116f, |
| 501 |
0.26286555606f, |
| 502 |
-0.951056516295f, |
| 503 |
-0.273266528913f, |
| 504 |
0.0f, |
| 505 |
-0.961938357784f, |
| 506 |
0.0f, |
| 507 |
-0.525731112119f, |
| 508 |
-0.850650808352f, |
| 509 |
-0.162459848116f, |
| 510 |
-0.26286555606f, |
| 511 |
-0.951056516295f, |
| 512 |
0.162459848116f, |
| 513 |
-0.26286555606f, |
| 514 |
-0.951056516295f, |
| 515 |
}; |
| 516 |
} // namespace |
| 517 |
|
| 518 |
#include "avogadrogl.h" |
| 519 |
|
| 520 |
#include <iostream> |
| 521 |
|
| 522 |
using std::cout; |
| 523 |
using std::endl; |
| 524 |
|
| 525 |
namespace Avogadro::Rendering { |
| 526 |
|
| 527 |
class AmbientOcclusionRenderer |
| 528 |
{ |
| 529 |
public: |
| 530 |
virtual ~AmbientOcclusionRenderer() {} |
| 531 |
|
| 532 |
virtual void renderDepth(const Eigen::Matrix4f& modelView, |
| 533 |
const Eigen::Matrix4f& projection) = 0; |
| 534 |
virtual void renderAO(const Eigen::Matrix4f& modelView, |
| 535 |
const Eigen::Matrix4f& projection, GLint textureSize, |
| 536 |
float numDirections) = 0; |
| 537 |
}; |
| 538 |
|
| 539 |
class AmbientOcclusionBaker |
| 540 |
{ |
| 541 |
public: |
| 542 |
AmbientOcclusionBaker(AmbientOcclusionRenderer* renderer, GLint textureSize_) |
| 543 |
: m_renderer(renderer), m_textureSize(textureSize_), m_depthTexture(0), |
| 544 |
m_depthFBO(0), m_aoTexture(0), m_aoFBO(0) |
| 545 |
{ |
| 546 |
initialize(); |
| 547 |
} |
| 548 |
|
| 549 |
void destroy() |
| 550 |
{ |
| 551 |
// delete framebuffers |
| 552 |
glDeleteFramebuffers(1, &m_depthFBO); |
| 553 |
glDeleteFramebuffers(1, &m_aoFBO); |
| 554 |
// delete depth texture |
| 555 |
glDeleteTextures(1, &m_depthTexture); |
| 556 |
// note: ao texture not deleted since it contains the computed data... |
| 557 |
} |
| 558 |
|
| 559 |
GLint textureSize() const { return m_textureSize; } |
| 560 |
|
| 561 |
GLuint aoTexture() const { return m_aoTexture; } |
| 562 |
|
| 563 |
void accumulateAO(const Vector3f& center, float radius) |
| 564 |
{ |
| 565 |
// save OpenGL state |
| 566 |
m_openglState.save(); |
| 567 |
|
| 568 |
// set the viewport |
| 569 |
glViewport(0, 0, m_textureSize, m_textureSize); |
| 570 |
// set the clear depth value |
| 571 |
glClearDepth(1.0f); |
| 572 |
// set the clear color |
| 573 |
glClearColor(0.0, 0.0, 0.0, 1.0); |
| 574 |
// enable polygon offset to resolve depth fighting |
| 575 |
glEnable(GL_POLYGON_OFFSET_FILL); |
| 576 |
glPolygonOffset(2.0f, 4.0f); |
| 577 |
// enable alpha blending |
| 578 |
glEnable(GL_BLEND); |
| 579 |
glBlendFunc(GL_ONE, GL_ONE); |
| 580 |
// bind the depth texture for depth lookup |
| 581 |
glActiveTexture(GL_TEXTURE0); |
| 582 |
glBindTexture(GL_TEXTURE_2D, m_depthTexture); |
| 583 |
|
| 584 |
Camera camera; |
| 585 |
camera.calculateOrthographic(-radius, radius, -radius, radius, -radius, |
| 586 |
radius); |
| 587 |
Eigen::Matrix4f projection(camera.projection().matrix()); |
| 588 |
|
| 589 |
// clear draw buffer once, AO wil be accumulated using blending |
| 590 |
glBindFramebuffer(GL_FRAMEBUFFER, m_aoFBO); |
| 591 |
glClear(GL_COLOR_BUFFER_BIT); |
| 592 |
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 593 |
|
| 594 |
for (int i = 0; i < num_ao_points; ++i) { |
| 595 |
// random light direction |
| 596 |
Vector3f dir(ao_points[i * 3], ao_points[i * 3 + 1], |
| 597 |
ao_points[i * 3 + 2]); |
| 598 |
camera.lookAt(center + dir, center, Vector3f(0, 1, 0)); |
| 599 |
Eigen::Matrix4f modelView = camera.modelView().matrix(); |
| 600 |
|
| 601 |
// render depth to texture |
| 602 |
renderDepth(modelView, projection); |
| 603 |
// accumulate AO |
| 604 |
renderAO(modelView, projection, num_ao_points); |
| 605 |
} |
| 606 |
|
| 607 |
// load OpenGL state |
| 608 |
m_openglState.load(); |
| 609 |
} |
| 610 |
|
| 611 |
private: |
| 612 |
void initialize() |
| 613 |
{ |
| 614 |
// create depth texture & FBO |
| 615 |
createDepthTexture(); |
| 616 |
createDepthFBO(); |
| 617 |
// create AO texture & FBO |
| 618 |
createAOTexture(); |
| 619 |
createAOFBO(); |
| 620 |
} |
| 621 |
|
| 622 |
void checkFramebufferStatus() |
| 623 |
{ |
| 624 |
// check framebuffer status |
| 625 |
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 626 |
switch (status) { |
| 627 |
case GL_FRAMEBUFFER_COMPLETE: |
| 628 |
std::cerr << "GL_FRAMEBUFFER_COMPLETE" << std::endl; |
| 629 |
break; |
| 630 |
case GL_FRAMEBUFFER_UNDEFINED: |
| 631 |
std::cerr << "GL_FRAMEBUFFER_UNDEFINED" << std::endl; |
| 632 |
break; |
| 633 |
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: |
| 634 |
std::cerr << "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT" << std::endl; |
| 635 |
break; |
| 636 |
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: |
| 637 |
std::cerr << "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT" |
| 638 |
<< std::endl; |
| 639 |
break; |
| 640 |
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: |
| 641 |
std::cerr << "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER" << std::endl; |
| 642 |
break; |
| 643 |
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER: |
| 644 |
std::cerr << "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER" << std::endl; |
| 645 |
break; |
| 646 |
case GL_FRAMEBUFFER_UNSUPPORTED: |
| 647 |
std::cerr << "GL_FRAMEBUFFER_UNSUPPORTED" << std::endl; |
| 648 |
break; |
| 649 |
case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: |
| 650 |
std::cerr << "GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE" << std::endl; |
| 651 |
break; |
| 652 |
case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS: |
| 653 |
std::cerr << "GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS" << std::endl; |
| 654 |
break; |
| 655 |
default: |
| 656 |
std::cerr << "GL_FRAMEBUFFER_???" << std::endl; |
| 657 |
break; |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
void createDepthTexture(GLenum internalFormat = GL_DEPTH_COMPONENT32) |
| 662 |
{ |
| 663 |
// create depth texture |
| 664 |
glGenTextures(1, &m_depthTexture); |
| 665 |
// bind the depth texture |
| 666 |
glBindTexture(GL_TEXTURE_2D, m_depthTexture); |
| 667 |
// allocate storage for the texture |
| 668 |
glTexImage2D(GL_TEXTURE_2D, // target |
| 669 |
0, // level |
| 670 |
internalFormat, // internal format |
| 671 |
m_textureSize, m_textureSize, // texture size |
| 672 |
0, // border |
| 673 |
GL_DEPTH_COMPONENT, // format |
| 674 |
GL_FLOAT, // type |
| 675 |
nullptr); // data |
| 676 |
|
| 677 |
// set the filtering modes |
| 678 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 679 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 680 |
// set depth comparison mode |
| 681 |
// this does not work with nvidia driver, works with intel 3000... |
| 682 |
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, |
| 683 |
// GL_COMPARE_REF_TO_TEXTURE); |
| 684 |
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); |
| 685 |
// set wrap modes |
| 686 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 687 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 688 |
// unbind texture |
| 689 |
glBindTexture(GL_TEXTURE_2D, 0); |
| 690 |
} |
| 691 |
|
| 692 |
void createDepthFBO() |
| 693 |
{ |
| 694 |
// create FBO to render depth into |
| 695 |
glGenFramebuffers(1, &m_depthFBO); |
| 696 |
glBindFramebuffer(GL_FRAMEBUFFER, m_depthFBO); |
| 697 |
// attach the depth texture to the depth FBO |
| 698 |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, |
| 699 |
m_depthTexture, 0); |
| 700 |
|
| 701 |
// disable draw and read buffer |
| 702 |
glDrawBuffer(GL_NONE); |
| 703 |
glReadBuffer(GL_NONE); |
| 704 |
|
| 705 |
// check framebuffer status |
| 706 |
// checkFramebufferStatus(); // debug... |
| 707 |
|
| 708 |
// unbind framebuffer |
| 709 |
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 710 |
} |
| 711 |
|
| 712 |
void renderDepth(const Eigen::Matrix4f& modelView, |
| 713 |
const Eigen::Matrix4f& projection) |
| 714 |
{ |
| 715 |
// bind framebuffer |
| 716 |
glBindFramebuffer(GL_FRAMEBUFFER, m_depthFBO); |
| 717 |
|
| 718 |
// enable depth test |
| 719 |
glEnable(GL_DEPTH_TEST); |
| 720 |
// clear depth buffer |
| 721 |
glClear(GL_DEPTH_BUFFER_BIT); |
| 722 |
|
| 723 |
// render the scene |
| 724 |
m_renderer->renderDepth(modelView, projection); |
| 725 |
|
| 726 |
// unbind framebuffer |
| 727 |
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 728 |
} |
| 729 |
|
| 730 |
void createAOTexture() |
| 731 |
{ |
| 732 |
// create AO texture |
| 733 |
glGenTextures(1, &m_aoTexture); |
| 734 |
// bind the AO texture |
| 735 |
glBindTexture(GL_TEXTURE_2D, m_aoTexture); |
| 736 |
// allocate storage for the texture |
| 737 |
glTexImage2D(GL_TEXTURE_2D, // target |
| 738 |
0, // level |
| 739 |
GL_RGBA, // internal format |
| 740 |
m_textureSize, m_textureSize, // texture size |
| 741 |
0, // border |
| 742 |
GL_RGBA, // format |
| 743 |
GL_UNSIGNED_BYTE, // type |
| 744 |
nullptr); // data |
| 745 |
|
| 746 |
// set the filtering modes |
| 747 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 748 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 749 |
// set wrap modes |
| 750 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 751 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 752 |
// unbind texture |
| 753 |
glBindTexture(GL_TEXTURE_2D, 0); |
| 754 |
} |
| 755 |
|
| 756 |
void createAOFBO() |
| 757 |
{ |
| 758 |
// create FBO to render depth into |
| 759 |
glGenFramebuffers(1, &m_aoFBO); |
| 760 |
glBindFramebuffer(GL_FRAMEBUFFER, m_aoFBO); |
| 761 |
// attach the depth texture to the depth FBO |
| 762 |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 763 |
m_aoTexture, 0); |
| 764 |
|
| 765 |
// disable draw and read buffer |
| 766 |
glReadBuffer(GL_NONE); |
| 767 |
|
| 768 |
// check framebuffer status |
| 769 |
// checkFramebufferStatus(); // debug... |
| 770 |
|
| 771 |
// unbind framebuffer |
| 772 |
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 773 |
} |
| 774 |
|
| 775 |
void renderAO(const Eigen::Matrix4f& modelView, |
| 776 |
const Eigen::Matrix4f& projection, int numDirections) |
| 777 |
{ |
| 778 |
// bind framebuffer |
| 779 |
glBindFramebuffer(GL_FRAMEBUFFER, m_aoFBO); |
| 780 |
|
| 781 |
// disable depth testing |
| 782 |
glDisable(GL_DEPTH_TEST); |
| 783 |
|
| 784 |
// render the scene |
| 785 |
m_renderer->renderAO(modelView, projection, m_textureSize, |
| 786 |
static_cast<float>(numDirections)); |
| 787 |
|
| 788 |
// unbind framebuffer |
| 789 |
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 790 |
} |
| 791 |
|
| 792 |
struct OpenGLState |
| 793 |
{ |
| 794 |
void save() |
| 795 |
{ |
| 796 |
// bound texture |
| 797 |
glGetIntegerv(GL_TEXTURE_BINDING_2D, &boundTexture); |
| 798 |
// viewport |
| 799 |
glGetIntegerv(GL_VIEWPORT, viewport); |
| 800 |
// depth |
| 801 |
glGetBooleanv(GL_DEPTH_TEST, &depthTest); |
| 802 |
glGetFloatv(GL_DEPTH_CLEAR_VALUE, &clearDepthValue); |
| 803 |
// color |
| 804 |
glGetBooleanv(GL_BLEND, &blend); |
| 805 |
glGetIntegerv(GL_BLEND_SRC, &blendSrc); |
| 806 |
glGetIntegerv(GL_BLEND_DST, &blendDst); |
| 807 |
glGetFloatv(GL_COLOR_CLEAR_VALUE, clearColor); |
| 808 |
// polygon offset |
| 809 |
polygonOffset = glIsEnabled(GL_POLYGON_OFFSET_FILL); |
| 810 |
glGetFloatv(GL_POLYGON_OFFSET_FACTOR, &polygonOffsetFactor); |
| 811 |
glGetFloatv(GL_POLYGON_OFFSET_UNITS, &polygonOffsetUnits); |
| 812 |
} |
| 813 |
|
| 814 |
void load() |
| 815 |
{ |
| 816 |
// bound texture |
| 817 |
glBindTexture(GL_TEXTURE_2D, boundTexture); |
| 818 |
// viewport |
| 819 |
glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); |
| 820 |
// depth |
| 821 |
if (!depthTest) |
| 822 |
glDisable(GL_DEPTH_TEST); |
| 823 |
else |
| 824 |
glEnable(GL_DEPTH_TEST); |
| 825 |
glClearDepth(clearDepthValue); |
| 826 |
// color |
| 827 |
glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]); |
| 828 |
if (!blend) |
| 829 |
glDisable(GL_BLEND); |
| 830 |
else |
| 831 |
glEnable(GL_BLEND); |
| 832 |
glBlendFunc(blendSrc, blendDst); |
| 833 |
} |
| 834 |
|
| 835 |
// bound texture |
| 836 |
GLint boundTexture; |
| 837 |
// viewport |
| 838 |
GLint viewport[4]; |
| 839 |
// depth |
| 840 |
GLboolean depthTest; |
| 841 |
GLfloat clearDepthValue; |
| 842 |
// color |
| 843 |
GLfloat clearColor[4]; |
| 844 |
GLboolean blend; |
| 845 |
GLint blendSrc, blendDst; |
| 846 |
// polygon offset |
| 847 |
GLboolean polygonOffset; |
| 848 |
GLfloat polygonOffsetFactor; |
| 849 |
GLfloat polygonOffsetUnits; |
| 850 |
}; |
| 851 |
|
| 852 |
OpenGLState m_openglState; |
| 853 |
|
| 854 |
AmbientOcclusionRenderer* m_renderer; |
| 855 |
|
| 856 |
GLint m_textureSize; |
| 857 |
|
| 858 |
GLuint m_depthTexture; |
| 859 |
GLuint m_depthFBO; |
| 860 |
GLuint m_aoTexture; |
| 861 |
GLuint m_aoFBO; |
| 862 |
}; |
| 863 |
|
| 864 |
class SphereAmbientOcclusionRenderer : public AmbientOcclusionRenderer |
| 865 |
{ |
| 866 |
public: |
| 867 |
SphereAmbientOcclusionRenderer(BufferObject& vbo, BufferObject& ibo, |
| 868 |
int numSpheres, int numVertices, |
| 869 |
int numIndices) |
| 870 |
: m_vbo(vbo), m_ibo(ibo), m_numSpheres(numSpheres), |
| 871 |
m_numVertices(numVertices), m_numIndices(numIndices) |
| 872 |
{ |
| 873 |
initialize(); |
| 874 |
} |
| 875 |
|
| 876 |
void renderDepth(const Eigen::Matrix4f& modelView, |
| 877 |
const Eigen::Matrix4f& projection) override |
| 878 |
{ |
| 879 |
// bind buffer objects |
| 880 |
m_vbo.bind(); |
| 881 |
m_ibo.bind(); |
| 882 |
|
| 883 |
m_depthProgram.bind(); |
| 884 |
|
| 885 |
// set the uniforms |
| 886 |
if (!m_depthProgram.setUniformValue("u_modelView", modelView)) { |
| 887 |
cout << m_depthProgram.error() << endl; |
| 888 |
} |
| 889 |
if (!m_depthProgram.setUniformValue("u_projection", projection)) { |
| 890 |
cout << m_depthProgram.error() << endl; |
| 891 |
} |
| 892 |
|
| 893 |
// set the attributes |
| 894 |
if (!m_depthProgram.enableAttributeArray("a_pos")) |
| 895 |
cout << m_depthProgram.error() << endl; |
| 896 |
if (!m_depthProgram.useAttributeArray("a_pos", |
| 897 |
ColorTextureVertex::vertexOffset(), |
| 898 |
sizeof(ColorTextureVertex), FloatType, |
| 899 |
3, ShaderProgram::NoNormalize)) { |
| 900 |
cout << m_depthProgram.error() << endl; |
| 901 |
} |
| 902 |
if (!m_depthProgram.enableAttributeArray("a_corner")) |
| 903 |
cout << m_depthProgram.error() << endl; |
| 904 |
if (!m_depthProgram.useAttributeArray( |
| 905 |
"a_corner", ColorTextureVertex::textureCoordOffset(), |
| 906 |
sizeof(ColorTextureVertex), FloatType, 2, |
| 907 |
ShaderProgram::NoNormalize)) { |
| 908 |
cout << m_depthProgram.error() << endl; |
| 909 |
} |
| 910 |
|
| 911 |
// draw |
| 912 |
glDrawRangeElements(GL_TRIANGLES, 0, static_cast<GLuint>(m_numVertices), |
| 913 |
static_cast<GLsizei>(m_numIndices), GL_UNSIGNED_INT, |
| 914 |
(const GLvoid*)nullptr); |
| 915 |
|
| 916 |
m_vbo.release(); |
| 917 |
m_ibo.release(); |
| 918 |
|
| 919 |
m_depthProgram.disableAttributeArray("a_pos"); |
| 920 |
m_depthProgram.disableAttributeArray("a_corner"); |
| 921 |
|
| 922 |
m_depthProgram.release(); |
| 923 |
} |
| 924 |
|
| 925 |
void renderAO(const Eigen::Matrix4f& modelView, |
| 926 |
const Eigen::Matrix4f& projection, GLint textureSize, |
| 927 |
float numDirections) override |
| 928 |
{ |
| 929 |
// bind buffer objects |
| 930 |
m_vbo.bind(); |
| 931 |
m_ibo.bind(); |
| 932 |
|
| 933 |
m_aoProgram.bind(); |
| 934 |
|
| 935 |
// set the uniforms |
| 936 |
if (!m_aoProgram.setUniformValue("u_modelView", modelView)) { |
| 937 |
cout << m_aoProgram.error() << endl; |
| 938 |
} |
| 939 |
if (!m_aoProgram.setUniformValue("u_projection", projection)) { |
| 940 |
cout << m_aoProgram.error() << endl; |
| 941 |
} |
| 942 |
if (!m_aoProgram.setUniformValue("u_textureSize", |
| 943 |
static_cast<GLfloat>(textureSize))) { |
| 944 |
cout << m_aoProgram.error() << endl; |
| 945 |
} |
| 946 |
if (!m_aoProgram.setUniformValue( |
| 947 |
"u_tileSize", |
| 948 |
1.0f / std::ceil(std::sqrt(static_cast<float>(m_numSpheres))))) { |
| 949 |
cout << m_aoProgram.error() << endl; |
| 950 |
} |
| 951 |
if (!m_aoProgram.setUniformValue("u_depthTex", 0)) { |
| 952 |
cout << m_aoProgram.error() << endl; |
| 953 |
} |
| 954 |
if (!m_aoProgram.setUniformValue("u_intensity", |
| 955 |
1.0f / (0.3f * numDirections))) { |
| 956 |
cout << m_aoProgram.error() << endl; |
| 957 |
} |
| 958 |
|
| 959 |
// set the attributes |
| 960 |
if (!m_aoProgram.enableAttributeArray("a_pos")) |
| 961 |
cout << m_aoProgram.error() << endl; |
| 962 |
if (!m_aoProgram.useAttributeArray("a_pos", |
| 963 |
ColorTextureVertex::vertexOffset(), |
| 964 |
sizeof(ColorTextureVertex), FloatType, 3, |
| 965 |
ShaderProgram::NoNormalize)) { |
| 966 |
cout << m_aoProgram.error() << endl; |
| 967 |
} |
| 968 |
if (!m_aoProgram.enableAttributeArray("a_corner")) |
| 969 |
cout << m_aoProgram.error() << endl; |
| 970 |
if (!m_aoProgram.useAttributeArray("a_corner", |
| 971 |
ColorTextureVertex::textureCoordOffset(), |
| 972 |
sizeof(ColorTextureVertex), FloatType, 2, |
| 973 |
ShaderProgram::NoNormalize)) { |
| 974 |
cout << m_aoProgram.error() << endl; |
| 975 |
} |
| 976 |
if (!m_aoProgram.enableAttributeArray("a_tileOffset")) |
| 977 |
cout << m_aoProgram.error() << endl; |
| 978 |
if (!m_aoProgram.useAttributeArray( |
| 979 |
"a_tileOffset", ColorTextureVertex::textureCoord2Offset(), |
| 980 |
sizeof(ColorTextureVertex), FloatType, 2, |
| 981 |
ShaderProgram::NoNormalize)) { |
| 982 |
cout << m_aoProgram.error() << endl; |
| 983 |
} |
| 984 |
|
| 985 |
// draw |
| 986 |
glDrawRangeElements(GL_TRIANGLES, 0, static_cast<GLuint>(m_numVertices), |
| 987 |
static_cast<GLsizei>(m_numIndices), GL_UNSIGNED_INT, |
| 988 |
(const GLvoid*)nullptr); |
| 989 |
|
| 990 |
m_vbo.release(); |
| 991 |
m_ibo.release(); |
| 992 |
|
| 993 |
m_aoProgram.disableAttributeArray("a_pos"); |
| 994 |
m_aoProgram.disableAttributeArray("a_corner"); |
| 995 |
m_aoProgram.disableAttributeArray("a_tileOffset"); |
| 996 |
|
| 997 |
m_aoProgram.release(); |
| 998 |
} |
| 999 |
|
| 1000 |
void destroy() |
| 1001 |
{ |
| 1002 |
// depth shader |
| 1003 |
m_depthProgram.detachShader(m_depthVertexShader); |
| 1004 |
m_depthProgram.detachShader(m_depthFragmentShader); |
| 1005 |
m_depthFragmentShader.cleanup(); |
| 1006 |
m_depthVertexShader.cleanup(); |
| 1007 |
// ao shader |
| 1008 |
m_aoProgram.detachShader(m_aoVertexShader); |
| 1009 |
m_aoProgram.detachShader(m_aoFragmentShader); |
| 1010 |
m_aoVertexShader.cleanup(); |
| 1011 |
m_aoFragmentShader.cleanup(); |
| 1012 |
} |
| 1013 |
|
| 1014 |
private: |
| 1015 |
void initialize() |
| 1016 |
{ |
| 1017 |
// compile depth shaders |
| 1018 |
m_depthVertexShader.setType(Shader::Vertex); |
| 1019 |
m_depthVertexShader.setSource(sphere_ao_depth_vs); |
| 1020 |
if (!m_depthVertexShader.compile()) |
| 1021 |
cout << m_depthVertexShader.error() << endl; |
| 1022 |
m_depthFragmentShader.setType(Shader::Fragment); |
| 1023 |
m_depthFragmentShader.setSource(sphere_ao_depth_fs); |
| 1024 |
if (!m_depthFragmentShader.compile()) |
| 1025 |
cout << m_depthFragmentShader.error() << endl; |
| 1026 |
// link depth program |
| 1027 |
m_depthProgram.attachShader(m_depthVertexShader); |
| 1028 |
m_depthProgram.attachShader(m_depthFragmentShader); |
| 1029 |
if (!m_depthProgram.link()) |
| 1030 |
cout << m_depthProgram.error() << endl; |
| 1031 |
|
| 1032 |
// compile AO shaders |
| 1033 |
m_aoVertexShader.setType(Shader::Vertex); |
| 1034 |
m_aoVertexShader.setSource(sphere_ao_bake_vs); |
| 1035 |
if (!m_aoVertexShader.compile()) |
| 1036 |
cout << m_aoVertexShader.error() << endl; |
| 1037 |
m_aoFragmentShader.setType(Shader::Fragment); |
| 1038 |
m_aoFragmentShader.setSource(sphere_ao_bake_fs); |
| 1039 |
if (!m_aoFragmentShader.compile()) |
| 1040 |
cout << m_aoFragmentShader.error() << endl; |
| 1041 |
// link AO program |
| 1042 |
m_aoProgram.attachShader(m_aoVertexShader); |
| 1043 |
m_aoProgram.attachShader(m_aoFragmentShader); |
| 1044 |
if (!m_aoProgram.link()) |
| 1045 |
cout << m_aoProgram.error() << endl; |
| 1046 |
} |
| 1047 |
|
| 1048 |
Shader m_depthVertexShader; |
| 1049 |
Shader m_depthFragmentShader; |
| 1050 |
ShaderProgram m_depthProgram; |
| 1051 |
|
| 1052 |
Shader m_aoVertexShader; |
| 1053 |
Shader m_aoFragmentShader; |
| 1054 |
ShaderProgram m_aoProgram; |
| 1055 |
|
| 1056 |
BufferObject& m_vbo; |
| 1057 |
BufferObject& m_ibo; |
| 1058 |
int m_numSpheres; |
| 1059 |
int m_numVertices; |
| 1060 |
int m_numIndices; |
| 1061 |
}; |
| 1062 |
|
| 1063 |
class AmbientOcclusionSphereGeometry::Private |
| 1064 |
{ |
| 1065 |
public: |
| 1066 |
Private() : aoTextureSize(1024) {} |
| 1067 |
|
| 1068 |
BufferObject vbo; |
| 1069 |
BufferObject ibo; |
| 1070 |
VertexArrayObject vao; |
| 1071 |
|
| 1072 |
Shader vertexShader; |
| 1073 |
Shader fragmentShader; |
| 1074 |
ShaderProgram program; |
| 1075 |
|
| 1076 |
size_t numberOfVertices; |
| 1077 |
size_t numberOfIndices; |
| 1078 |
|
| 1079 |
Eigen::Matrix4f translate; |
| 1080 |
int aoTextureSize; |
| 1081 |
int aoTexture; |
| 1082 |
}; |
| 1083 |
|
| 1084 |
AmbientOcclusionSphereGeometry::AmbientOcclusionSphereGeometry() |
| 1085 |
: m_dirty(false), d(new Private) |
| 1086 |
{ |
| 1087 |
} |
| 1088 |
|
| 1089 |
AmbientOcclusionSphereGeometry::AmbientOcclusionSphereGeometry( |
| 1090 |
const AmbientOcclusionSphereGeometry& other) |
| 1091 |
: Drawable(other), m_spheres(other.m_spheres), m_indices(other.m_indices), |
| 1092 |
m_dirty(true), d(new Private) |
| 1093 |
{ |
| 1094 |
} |
| 1095 |
|
| 1096 |
AmbientOcclusionSphereGeometry::~AmbientOcclusionSphereGeometry() |
| 1097 |
{ |
| 1098 |
delete d; |
| 1099 |
} |
| 1100 |
|
| 1101 |
void AmbientOcclusionSphereGeometry::accept(Visitor& visitor) |
| 1102 |
{ |
| 1103 |
visitor.visit(*this); |
| 1104 |
} |
| 1105 |
|
| 1106 |
void AmbientOcclusionSphereGeometry::update() |
| 1107 |
{ |
| 1108 |
if (m_indices.empty() || m_spheres.empty()) |
| 1109 |
return; |
| 1110 |
|
| 1111 |
// Build and link the shader if it has not been used yet. |
| 1112 |
// Must be done before VAO setup since we need the program for attribute |
| 1113 |
// setup. |
| 1114 |
if (d->vertexShader.type() == Shader::Unknown) { |
| 1115 |
d->vertexShader.setType(Shader::Vertex); |
| 1116 |
d->vertexShader.setSource(sphere_ao_render_vs); |
| 1117 |
d->fragmentShader.setType(Shader::Fragment); |
| 1118 |
d->fragmentShader.setSource(sphere_ao_render_fs); |
| 1119 |
if (!d->vertexShader.compile()) |
| 1120 |
cout << d->vertexShader.error() << endl; |
| 1121 |
if (!d->fragmentShader.compile()) |
| 1122 |
cout << d->fragmentShader.error() << endl; |
| 1123 |
d->program.attachShader(d->vertexShader); |
| 1124 |
d->program.attachShader(d->fragmentShader); |
| 1125 |
if (!d->program.link()) |
| 1126 |
cout << d->program.error() << endl; |
| 1127 |
} |
| 1128 |
|
| 1129 |
// Check if the VBOs are ready, if not get them ready. |
| 1130 |
if (!d->vbo.ready() || m_dirty) { |
| 1131 |
std::vector<unsigned int> sphereIndices; |
| 1132 |
std::vector<ColorTextureVertex> sphereVertices; |
| 1133 |
sphereIndices.reserve(m_indices.size() * 4); |
| 1134 |
sphereVertices.reserve(m_spheres.size() * 4); |
| 1135 |
|
| 1136 |
int nSpheres = static_cast<int>(m_spheres.size()); |
| 1137 |
int nSpheresSquared = |
| 1138 |
static_cast<int>(std::ceil(std::sqrt(static_cast<float>(nSpheres)))); |
| 1139 |
float tileSize = 1.0f / static_cast<float>(nSpheresSquared); |
| 1140 |
float halfTileSize = tileSize / 2.0f; |
| 1141 |
int tileX = 0; |
| 1142 |
int tileY = 0; |
| 1143 |
|
| 1144 |
auto itIndex = m_indices.begin(); |
| 1145 |
auto itSphere = m_spheres.begin(); |
| 1146 |
|
| 1147 |
// calculate center |
| 1148 |
Vector3f center(Vector3f::Zero()); |
| 1149 |
for (auto& m_sphere : m_spheres) |
| 1150 |
center += m_sphere.center; |
| 1151 |
center /= static_cast<float>(nSpheres); |
| 1152 |
|
| 1153 |
// calculate radius |
| 1154 |
float radius = 0.0f; |
| 1155 |
for (auto& m_sphere : m_spheres) |
| 1156 |
if ((m_sphere.center - center).norm() > radius) |
| 1157 |
radius = (m_sphere.center - center).norm(); |
| 1158 |
|
| 1159 |
for (unsigned int i = 0; |
| 1160 |
itIndex != m_indices.end() && itSphere != m_spheres.end(); |
| 1161 |
++i, ++itIndex, ++itSphere) { |
| 1162 |
// Use our packed data structure... |
| 1163 |
float r = itSphere->radius; |
| 1164 |
unsigned int index = 4 * static_cast<unsigned int>(*itIndex); |
| 1165 |
ColorTextureVertex vert( |
| 1166 |
itSphere->center, itSphere->color, Vector2f(-r, -r), |
| 1167 |
Vector2f(halfTileSize + tileSize * static_cast<float>(tileX), |
| 1168 |
halfTileSize + tileSize * static_cast<float>(tileY))); |
| 1169 |
sphereVertices.push_back(vert); |
| 1170 |
vert.textureCoord = Vector2f(-r, r); |
| 1171 |
sphereVertices.push_back(vert); |
| 1172 |
vert.textureCoord = Vector2f(r, -r); |
| 1173 |
sphereVertices.push_back(vert); |
| 1174 |
vert.textureCoord = Vector2f(r, r); |
| 1175 |
sphereVertices.push_back(vert); |
| 1176 |
|
| 1177 |
// 6 indexed vertices to draw a quad... |
| 1178 |
sphereIndices.push_back(index + 0); |
| 1179 |
sphereIndices.push_back(index + 1); |
| 1180 |
sphereIndices.push_back(index + 2); |
| 1181 |
sphereIndices.push_back(index + 3); |
| 1182 |
sphereIndices.push_back(index + 2); |
| 1183 |
sphereIndices.push_back(index + 1); |
| 1184 |
|
| 1185 |
++tileX; |
| 1186 |
if (tileX >= nSpheresSquared) { |
| 1187 |
// start new tile row |
| 1188 |
tileX = 0; |
| 1189 |
++tileY; |
| 1190 |
} |
| 1191 |
} |
| 1192 |
|
| 1193 |
d->vbo.upload(sphereVertices, BufferObject::ArrayBuffer); |
| 1194 |
d->ibo.upload(sphereIndices, BufferObject::ElementArrayBuffer); |
| 1195 |
d->numberOfVertices = sphereVertices.size(); |
| 1196 |
d->numberOfIndices = sphereIndices.size(); |
| 1197 |
|
| 1198 |
// Set up VAO with vertex attribute bindings (OpenGL 4.0 core profile) |
| 1199 |
d->vao.bind(); |
| 1200 |
d->vbo.bind(); |
| 1201 |
d->ibo.bind(); |
| 1202 |
|
| 1203 |
if (!d->program.enableAttributeArray("a_pos")) |
| 1204 |
cout << d->program.error() << endl; |
| 1205 |
if (!d->program.useAttributeArray("a_pos", |
| 1206 |
ColorTextureVertex::vertexOffset(), |
| 1207 |
sizeof(ColorTextureVertex), FloatType, 3, |
| 1208 |
ShaderProgram::NoNormalize)) { |
| 1209 |
cout << d->program.error() << endl; |
| 1210 |
} |
| 1211 |
if (!d->program.enableAttributeArray("a_corner")) |
| 1212 |
cout << d->program.error() << endl; |
| 1213 |
if (!d->program.useAttributeArray("a_corner", |
| 1214 |
ColorTextureVertex::textureCoordOffset(), |
| 1215 |
sizeof(ColorTextureVertex), FloatType, 2, |
| 1216 |
ShaderProgram::NoNormalize)) { |
| 1217 |
cout << d->program.error() << endl; |
| 1218 |
} |
| 1219 |
if (!d->program.enableAttributeArray("a_tileOffset")) |
| 1220 |
cout << d->program.error() << endl; |
| 1221 |
if (!d->program.useAttributeArray("a_tileOffset", |
| 1222 |
ColorTextureVertex::textureCoord2Offset(), |
| 1223 |
sizeof(ColorTextureVertex), FloatType, 2, |
| 1224 |
ShaderProgram::NoNormalize)) { |
| 1225 |
cout << d->program.error() << endl; |
| 1226 |
} |
| 1227 |
if (!d->program.enableAttributeArray("a_color")) |
| 1228 |
cout << d->program.error() << endl; |
| 1229 |
if (!d->program.useAttributeArray( |
| 1230 |
"a_color", ColorTextureVertex::colorOffset(), |
| 1231 |
sizeof(ColorTextureVertex), UCharType, 3, ShaderProgram::Normalize)) { |
| 1232 |
cout << d->program.error() << endl; |
| 1233 |
} |
| 1234 |
|
| 1235 |
d->vao.release(); |
| 1236 |
|
| 1237 |
SphereAmbientOcclusionRenderer aoSphereRenderer( |
| 1238 |
d->vbo, d->ibo, static_cast<int>(m_spheres.size()), |
| 1239 |
static_cast<int>(d->numberOfVertices), |
| 1240 |
static_cast<int>(d->numberOfIndices)); |
| 1241 |
AmbientOcclusionBaker baker(&aoSphereRenderer, d->aoTextureSize); |
| 1242 |
baker.accumulateAO(center, radius + 2.0f); |
| 1243 |
d->aoTexture = baker.aoTexture(); |
| 1244 |
baker.destroy(); |
| 1245 |
aoSphereRenderer.destroy(); |
| 1246 |
|
| 1247 |
m_dirty = false; |
| 1248 |
} |
| 1249 |
} |
| 1250 |
|
| 1251 |
void AmbientOcclusionSphereGeometry::render(const Camera& camera) |
| 1252 |
{ |
| 1253 |
if (m_indices.empty() || m_spheres.empty()) |
| 1254 |
return; |
| 1255 |
|
| 1256 |
// Prepare the VBOs, IBOs, VAO, and shader program if necessary. |
| 1257 |
update(); |
| 1258 |
|
| 1259 |
glActiveTexture(GL_TEXTURE0); |
| 1260 |
glBindTexture(GL_TEXTURE_2D, d->aoTexture); |
| 1261 |
|
| 1262 |
if (!d->program.bind()) |
| 1263 |
cout << d->program.error() << endl; |
| 1264 |
|
| 1265 |
// Bind the VAO (captures all vertex attribute state) |
| 1266 |
d->vao.bind(); |
| 1267 |
|
| 1268 |
// Set up our uniforms |
| 1269 |
if (!d->program.setUniformValue("u_modelView", camera.modelView().matrix())) { |
| 1270 |
cout << d->program.error() << endl; |
| 1271 |
} |
| 1272 |
if (!d->program.setUniformValue( |
| 1273 |
"u_invModelView", |
| 1274 |
Eigen::Matrix3f( |
| 1275 |
camera.modelView().matrix().block<3, 3>(0, 0).inverse()))) { |
| 1276 |
cout << d->program.error() << endl; |
| 1277 |
} |
| 1278 |
if (!d->program.setUniformValue("u_projection", |
| 1279 |
camera.projection().matrix())) { |
| 1280 |
cout << d->program.error() << endl; |
| 1281 |
} |
| 1282 |
if (!d->program.setUniformValue("u_tex", 0)) { |
| 1283 |
cout << d->program.error() << endl; |
| 1284 |
} |
| 1285 |
|
| 1286 |
// To avoid texture interpolation from neighboring tiles, texture coords are |
| 1287 |
// scaled such that half a texel is removed from all sides of a tile. |
| 1288 |
|
| 1289 |
// width of a singl texel in texture coordinates [0, 1] |
| 1290 |
float texel = 1.0f / static_cast<float>(d->aoTextureSize); |
| 1291 |
// with of a single tile in texture coordinates [0, 1] |
| 1292 |
float tile = 1.f / std::ceil(std::sqrt(static_cast<float>(m_spheres.size()))); |
| 1293 |
|
| 1294 |
// The uv coordinates, centered around the tileOffset are originally in the |
| 1295 |
// range [-1, 1]. The denominator below ensures that these are scaled to |
| 1296 |
// values matching exactly one tile. The numerator is one minus a factor |
| 1297 |
// to ensure half a tile on each side is never reached to avoid texture |
| 1298 |
// interpolation taking values from neighboring texels into account. |
| 1299 |
if (!d->program.setUniformValue( |
| 1300 |
"u_texScale", (1.0f - 2.0f * texel / tile) / |
| 1301 |
(2.0f * std::ceil(std::sqrt( |
| 1302 |
static_cast<float>(m_spheres.size())))))) { |
| 1303 |
cout << d->program.error() << endl; |
| 1304 |
} |
| 1305 |
|
| 1306 |
// Render the loaded spheres using the shader and VAO. |
| 1307 |
glDrawRangeElements(GL_TRIANGLES, 0, static_cast<GLuint>(d->numberOfVertices), |
| 1308 |
static_cast<GLsizei>(d->numberOfIndices), GL_UNSIGNED_INT, |
| 1309 |
(const GLvoid*)nullptr); |
| 1310 |
|
| 1311 |
d->vao.release(); |
| 1312 |
d->program.release(); |
| 1313 |
} |
| 1314 |
|
| 1315 |
std::multimap<float, Identifier> AmbientOcclusionSphereGeometry::hits( |
| 1316 |
const Vector3f& rayOrigin, const Vector3f& rayEnd, |
| 1317 |
const Vector3f& rayDirection) const |
| 1318 |
{ |
| 1319 |
std::multimap<float, Identifier> result; |
| 1320 |
|
| 1321 |
// Check for intersection. |
| 1322 |
for (size_t i = 0; i < m_spheres.size(); ++i) { |
| 1323 |
const SphereColor& sphere = m_spheres[i]; |
| 1324 |
|
| 1325 |
Vector3f distance = sphere.center - rayOrigin; |
| 1326 |
float B = distance.dot(rayDirection); |
| 1327 |
float C = distance.dot(distance) - (sphere.radius * sphere.radius); |
| 1328 |
float D = B * B - C; |
| 1329 |
|
| 1330 |
// Test for intersection |
| 1331 |
if (D < 0) |
| 1332 |
continue; |
| 1333 |
|
| 1334 |
// Test for clipping |
| 1335 |
if (B < 0 || (sphere.center - rayEnd).dot(rayDirection) > 0) |
| 1336 |
continue; |
| 1337 |
|
| 1338 |
Identifier id; |
| 1339 |
id.molecule = m_identifier.molecule; |
| 1340 |
id.type = m_identifier.type; |
| 1341 |
id.index = i; |
| 1342 |
if (id.type != InvalidType) { |
| 1343 |
auto rootD = static_cast<float>(sqrt(D)); |
| 1344 |
float depth = std::min(std::abs(B + rootD), std::abs(B - rootD)); |
| 1345 |
result.insert(std::pair<float, Identifier>(depth, id)); |
| 1346 |
} |
| 1347 |
} |
| 1348 |
return result; |
| 1349 |
} |
| 1350 |
|
| 1351 |
void AmbientOcclusionSphereGeometry::addSphere(const Vector3f& position, |
| 1352 |
const Vector3ub& color, |
| 1353 |
float radius, size_t index) |
| 1354 |
{ |
| 1355 |
m_dirty = true; |
| 1356 |
m_spheres.push_back(SphereColor(position, radius, color)); |
| 1357 |
m_indices.push_back(index == MaxIndex ? m_indices.size() : index); |
| 1358 |
} |
| 1359 |
|
| 1360 |
void AmbientOcclusionSphereGeometry::clear() |
| 1361 |
{ |
| 1362 |
m_spheres.clear(); |
| 1363 |
m_indices.clear(); |
| 1364 |
} |
| 1365 |
|
| 1366 |
} // namespace Avogadro::Rendering |