1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
|
/**************************************************************************\
*
* This file is part of the Coin 3D visualization library.
* Copyright (C) by Kongsberg Oil & Gas Technologies.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* ("GPL") version 2 as published by the Free Software Foundation.
* See the file LICENSE.GPL at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using Coin with software that can not be combined with the GNU
* GPL, and for taking advantage of the additional benefits of our
* support services, please contact Kongsberg Oil & Gas Technologies
* about acquiring a Coin Professional Edition License.
*
* See http://www.coin3d.org/ for more information.
*
* Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
* http://www.sim.no/ sales@sim.no coin-support@coin3d.org
*
\**************************************************************************/
/*!
\class SoCamera SoCamera.h Inventor/nodes/SoCamera.h
\brief The SoCamera class is the abstract base class for camera definition nodes.
\ingroup nodes
To be able to view a scene, one needs to have a camera in the scene
graph. A camera node will set up the projection and viewing matrices
for rendering of the geometry in the scene.
This node just defines the abstract interface by collecting common
fields that all camera type nodes needs. Use the non-abstract camera
node subclasses within a scene graph. The ones that are default part
of the Coin library are SoPerspectiveCamera and
SoOrthographicCamera, which uses the two different projections given
by their name.
Note that the viewer components of the GUI glue libraries of Coin
(SoXt, SoQt, SoWin, etc) will automatically insert a camera into a
scene graph if none has been defined.
It is possible to have more than one camera in a scene graph. One
common trick is for instance to use a second camera to display
static geometry or overlay geometry (e.g. for head-up displays
("HUD")), as shown by this example code:
\code
#include <Inventor/Qt/SoQt.h>
#include <Inventor/Qt/viewers/SoQtExaminerViewer.h>
#include <Inventor/nodes/SoNodes.h>
int
main(int argc, char ** argv)
{
QWidget * mainwin = SoQt::init(argv[0]);
SoSeparator * root = new SoSeparator;
root->ref();
// Adds a camera and a red cone. The first camera found in the
// scene graph by the SoQtExaminerViewer will be picked up and
// initialized automatically.
root->addChild(new SoPerspectiveCamera);
SoMaterial * material = new SoMaterial;
material->diffuseColor.setValue(1.0, 0.0, 0.0);
root->addChild(material);
root->addChild(new SoCone);
// Set up a second camera for the remaining geometry. This camera
// will not be picked up and influenced by the viewer, so the
// geometry will be kept static.
SoPerspectiveCamera * pcam = new SoPerspectiveCamera;
pcam->position = SbVec3f(0, 0, 5);
pcam->nearDistance = 0.1;
pcam->farDistance = 10;
root->addChild(pcam);
// Adds a green cone to demonstrate static geometry.
SoMaterial * greenmaterial = new SoMaterial;
greenmaterial->diffuseColor.setValue(0, 1.0, 0.0);
root->addChild(greenmaterial);
root->addChild(new SoCone);
SoQtExaminerViewer * viewer = new SoQtExaminerViewer(mainwin);
viewer->setSceneGraph(root);
viewer->show();
SoQt::show(mainwin);
SoQt::mainLoop();
delete viewer;
root->unref();
return 0;
}
\endcode
*/
#include <Inventor/nodes/SoCamera.h>
#include <float.h> // for FLT_EPSILON
#include <Inventor/elements/SoCacheElement.h>
#include <Inventor/actions/SoCallbackAction.h>
#include <Inventor/actions/SoGLRenderAction.h>
#include <Inventor/actions/SoGetBoundingBoxAction.h>
#include <Inventor/actions/SoGetPrimitiveCountAction.h>
#include <Inventor/actions/SoHandleEventAction.h>
#include <Inventor/actions/SoRayPickAction.h>
#include <Inventor/actions/SoAudioRenderAction.h>
#include <Inventor/elements/SoFocalDistanceElement.h>
#include <Inventor/elements/SoGLProjectionMatrixElement.h>
#include <Inventor/elements/SoGLViewingMatrixElement.h>
#include <Inventor/elements/SoModelMatrixElement.h>
#include <Inventor/elements/SoViewVolumeElement.h>
#include <Inventor/elements/SoViewportRegionElement.h>
#include <Inventor/elements/SoDrawStyleElement.h>
#include <Inventor/elements/SoGLLineWidthElement.h>
#include <Inventor/elements/SoGLShapeHintsElement.h>
#include <Inventor/elements/SoCullElement.h>
#include <Inventor/elements/SoGLRenderPassElement.h>
#include <Inventor/elements/SoListenerPositionElement.h>
#include <Inventor/elements/SoListenerOrientationElement.h>
#include <Inventor/elements/SoListenerDopplerElement.h>
#include <Inventor/elements/SoListenerGainElement.h>
#include <Inventor/elements/SoGLTextureEnabledElement.h>
#include <Inventor/elements/SoGLTexture3EnabledElement.h>
#include <Inventor/elements/SoGLMultiTextureEnabledElement.h>
#include <Inventor/misc/SoState.h>
#include <Inventor/SbColor4f.h>
#include <Inventor/C/glue/gl.h>
#include <Inventor/C/tidbits.h>
#if COIN_DEBUG
#include <Inventor/errors/SoDebugError.h>
#endif // COIN_DEBUG
#include "elements/GL/SoResetMatrixElement.h"
#include "nodes/SoSubNodeP.h"
/*!
\enum SoCamera::ViewportMapping
Enumerates the available possibilities for how the render frame
should map the viewport.
*/
/*!
\var SoSFEnum SoCamera::viewportMapping
Set up how the render frame should map the viewport. The default is
SoCamera::ADJUST_CAMERA.
*/
/*!
\var SoSFVec3f SoCamera::position
Camera position. Defaults to <0,0,1>.
*/
/*!
\var SoSFRotation SoCamera::orientation
Camera orientation specified as a rotation value from the default
orientation where the camera is pointing along the negative z-axis,
with "up" along the positive y-axis.
E.g., to rotate the camera to point along the X axis:
\code
mycamera->orientation.setValue(SbRotation(SbVec3f(0, 1, 0), M_PI / 2.0f));
\endcode
For queries, e.g. to get the current "up" and "look at" vectors of
the camera:
\code
SbRotation camrot = mycamera->orientation.getValue();
SbVec3f upvec(0, 1, 0); // init to default up vector
camrot.multVec(upvec, upvec);
SbVec3f lookat(0, 0, -1); // init to default view direction vector
camrot.multVec(lookat, lookat);
\endcode
*/
/*!
\var SoSFFloat SoCamera::aspectRatio
Aspect ratio for the camera (i.e. width / height). Defaults to 1.0.
*/
/*!
\var SoSFFloat SoCamera::nearDistance
Distance from camera position to the near clipping plane in the
camera's view volume.
Default value is 1.0. Value must be larger than 0.0, or it will not
be possible to construct a valid viewing volume (for perspective
rendering, at least).
If you use one of the viewer components from the So[Xt|Qt|Win|Gtk]
GUI libraries provided by Kongsberg Oil & Gas Technologies, they
will automatically update this value for the scene camera according
to the scene bounding box. Ditto for the far clipping plane.
\sa SoCamera::farDistance
*/
/*!
\var SoSFFloat SoCamera::farDistance
Distance from camera position to the far clipping plane in the
camera's view volume.
Default value is 10.0. Must be larger than the
SoCamera::nearDistance value, or it will not be possible to
construct a valid viewing volume.
Note that the range [nearDistance, farDistance] decides the dynamic
range of the Z-buffer in the underlying polygon-rendering
rasterizer. What this means is that if the near and far clipping
planes of the camera are wide apart, the possibility of visual
artifacts will increase. The artifacts will manifest themselves in
the form of flickering of primitives close in depth.
It is therefore a good idea to keep the near and far clipping planes
of your camera(s) as closely fitted around the geometry of the
scene graph as possible.
\sa SoCamera::nearDistance, SoPolygonOffset
*/
/*!
\var SoSFFloat SoCamera::focalDistance
Distance from camera position to center of scene.
*/
/*!
\fn void SoCamera::scaleHeight(float scalefactor)
Sets a \a scalefactor for the height of the camera viewport. What
"viewport height" means exactly in this context depends on the
camera model. See documentation in subclasses.
*/
/*!
\fn SbViewVolume SoCamera::getViewVolume(float useaspectratio = 0.0f) const
Returns total view volume covered by the camera under the current
settings.
This view volume is not adjusted to account for viewport mapping.
If you want the same view volume as the one used during rendering,
you should do something like this:
\verbatim
SbViewVolume vv;
float aspectratio = myviewport.getViewportAspectRatio();
switch (camera->viewportMapping.getValue()) {
case SoCamera::CROP_VIEWPORT_FILL_FRAME:
case SoCamera::CROP_VIEWPORT_LINE_FRAME:
case SoCamera::CROP_VIEWPORT_NO_FRAME:
vv = camera->getViewVolume(0.0f);
break;
case SoCamera::ADJUST_CAMERA:
vv = camera->getViewVolume(aspectratio);
if (aspectratio < 1.0f) vv.scale(1.0f / aspectratio);
break;
case SoCamera::LEAVE_ALONE:
vv = camera->getViewVolume(0.0f);
break;
default:
assert(0 && "unknown viewport mapping");
break;
}
\endverbatim
Also, for the CROPPED viewport mappings, the viewport might
be changed if the viewport aspect ratio is not equal to the
camera aspect ratio. See SoCamera::getView() to see how this
is done.
*/
/*!
\fn void SoCamera::viewBoundingBox(const SbBox3f & box, float aspect, float slack)
Convenience method for setting up the camera definition to cover the
given bounding \a box with the given \a aspect ratio. Multiplies the
exact dimensions with a \a slack factor to have some space between
the rendered model and the borders of the rendering area.
If you define your own camera node class, be aware that this method
should \e not set the orientation field of the camera, only the
position, focal distance and near and far clipping planes.
*/
/*!
\enum SoCamera::StereoMode
Enumerates the possible stereo modes.
*/
/*!
\var SoCamera::MONOSCOPIC
No stereo.
*/
/*!
\var SoCamera::LEFT_VIEW
Left view.
*/
/*!
\var SoCamera::RIGHT_VIEW
Right view.
*/
SO_NODE_ABSTRACT_SOURCE(SoCamera);
/*!
Constructor.
*/
SoCamera::SoCamera()
{
SO_NODE_INTERNAL_CONSTRUCTOR(SoCamera);
SO_NODE_ADD_FIELD(viewportMapping, (ADJUST_CAMERA));
SO_NODE_ADD_FIELD(position, (0.0f, 0.0f, 1.0f));
SO_NODE_ADD_FIELD(orientation, (SbRotation(SbVec3f(0.0f, 0.0f, 1.0f), 0.0f)));
SO_NODE_ADD_FIELD(nearDistance, (1.0f));
SO_NODE_ADD_FIELD(farDistance, (10.0f));
SO_NODE_ADD_FIELD(aspectRatio, (1.0f));
SO_NODE_ADD_FIELD(focalDistance, (5.0f));
SO_NODE_DEFINE_ENUM_VALUE(ViewportMapping, CROP_VIEWPORT_FILL_FRAME);
SO_NODE_DEFINE_ENUM_VALUE(ViewportMapping, CROP_VIEWPORT_LINE_FRAME);
SO_NODE_DEFINE_ENUM_VALUE(ViewportMapping, CROP_VIEWPORT_NO_FRAME);
SO_NODE_DEFINE_ENUM_VALUE(ViewportMapping, ADJUST_CAMERA);
SO_NODE_DEFINE_ENUM_VALUE(ViewportMapping, LEAVE_ALONE);
SO_NODE_SET_SF_ENUM_TYPE(viewportMapping, ViewportMapping);
this->stereomode = MONOSCOPIC;
this->stereoadjustment = 0.1f;
this->balanceadjustment = 1.0f;
}
/*!
Destructor.
*/
SoCamera::~SoCamera()
{
}
// Doc in superclass.
void
SoCamera::initClass(void)
{
SO_NODE_INTERNAL_INIT_ABSTRACT_CLASS(SoCamera, SO_FROM_INVENTOR_1);
SO_ENABLE(SoGLRenderAction, SoFocalDistanceElement);
SO_ENABLE(SoGLRenderAction, SoGLProjectionMatrixElement);
SO_ENABLE(SoGLRenderAction, SoViewVolumeElement);
SO_ENABLE(SoGLRenderAction, SoGLViewingMatrixElement);
SO_ENABLE(SoGLRenderAction, SoResetMatrixElement);
SO_ENABLE(SoGLRenderAction, SoCullElement);
SO_ENABLE(SoGetBoundingBoxAction, SoFocalDistanceElement);
SO_ENABLE(SoGetBoundingBoxAction, SoProjectionMatrixElement);
SO_ENABLE(SoGetBoundingBoxAction, SoViewVolumeElement);
SO_ENABLE(SoGetBoundingBoxAction, SoViewingMatrixElement);
SO_ENABLE(SoRayPickAction, SoFocalDistanceElement);
SO_ENABLE(SoRayPickAction, SoProjectionMatrixElement);
SO_ENABLE(SoRayPickAction, SoViewVolumeElement);
SO_ENABLE(SoRayPickAction, SoViewingMatrixElement);
SO_ENABLE(SoCallbackAction, SoFocalDistanceElement);
SO_ENABLE(SoCallbackAction, SoProjectionMatrixElement);
SO_ENABLE(SoCallbackAction, SoViewVolumeElement);
SO_ENABLE(SoCallbackAction, SoViewingMatrixElement);
SO_ENABLE(SoGetPrimitiveCountAction, SoFocalDistanceElement);
SO_ENABLE(SoGetPrimitiveCountAction, SoProjectionMatrixElement);
SO_ENABLE(SoGetPrimitiveCountAction, SoViewVolumeElement);
SO_ENABLE(SoGetPrimitiveCountAction, SoViewingMatrixElement);
SO_ENABLE(SoAudioRenderAction, SoListenerPositionElement);
SO_ENABLE(SoAudioRenderAction, SoListenerOrientationElement);
SO_ENABLE(SoAudioRenderAction, SoListenerDopplerElement);
SO_ENABLE(SoAudioRenderAction, SoListenerGainElement);
}
/*!
Reorients the camera so that it points towards \a targetpoint.
The positive y-axis is used as the up vector of the camera, unless
the new camera direction is parallel to this axis, in which case the
positive z-axis will be used instead.
*/
void
SoCamera::pointAt(const SbVec3f & targetpoint)
{
SbVec3f dir = targetpoint - this->position.getValue();
if (dir.normalize() == 0.0f) {
#if COIN_DEBUG
SoDebugError::postInfo("SoCamera::pointAt",
"targetpoint == camera position.");
#endif // debug
return;
}
SbVec3f up(0.0f, 1.0f, 0.0f);
// use 0,1,0 as the up vector unless direction and up vector are parallel
if (SbAbs(dir.dot(up)) >= (1.0f - FLT_EPSILON)) up.setValue(0.0f, 0.0f, 1.0f);
this->lookAt(dir, up);
}
/*!
Reorients the camera so that it points towards \a targetpoint, using
\a upvector as the camera up vector.
\COIN_FUNCTION_EXTENSION
*/
void
SoCamera::pointAt(const SbVec3f & targetpoint, const SbVec3f & upvector)
{
SbVec3f dir = targetpoint - this->position.getValue();
if (dir.normalize() == 0.0f) {
#if COIN_DEBUG
SoDebugError::postInfo("SoCamera::pointAt",
"targetpoint == camera position.");
#endif // debug
return;
}
this->lookAt(dir, upvector);
}
// FIXME: should collect common code from the two viewAll() methods
// below. 20010824 mortene.
/*!
Position the camera so that all geometry of the scene from \a sceneroot
is contained in the view volume of the camera, while keeping the
camera orientation constant.
Finds the bounding box of the scene and calls
SoCamera::viewBoundingBox(). A bounding sphere will be calculated
from the scene bounding box, so the camera will "view all" even when
the scene is rotated, in any way.
The \a slack argument gives a multiplication factor to the distance
the camera is supposed to move out from the \a sceneroot mid-point.
A value less than 1.0 for the \a slack argument will therefore cause
the camera to come closer to the scene, a value of 1.0 will position
the camera as exactly outside the scene bounding sphere, and a value
larger than 1.0 will give "extra slack" versus the scene bounding
sphere.
*/
void
SoCamera::viewAll(SoNode * const sceneroot, const SbViewportRegion & vpregion,
const float slack)
{
SoGetBoundingBoxAction action(vpregion);
action.apply(sceneroot);
SbBox3f box = action.getBoundingBox();
#if COIN_DEBUG && 0 // debug
SoDebugError::postInfo("SoCamera::viewAll",
"bbox: <%f %f %f>, <%f %f %f>\n",
box.getMin()[0], box.getMin()[1], box.getMin()[2],
box.getMax()[0], box.getMax()[1], box.getMax()[2]);
SoDebugError::postInfo("SoCamera::viewAll",
"viewportregion, windowsize: <%f, %f>, <%d, %d>\n",
vpregion.getViewportSize()[0],
vpregion.getViewportSize()[1],
vpregion.getWindowSize()[0],
vpregion.getWindowSize()[1] );
#endif // debug
// Only check for "flagged" emptiness and don't use
// SbBox3f::hasVolume(), as we *can* handle flat boxes (in all
// dimensions).
if (box.isEmpty()) { return; }
this->viewBoundingBox(box, this->aspectRatio.getValue(), slack);
}
/*!
Position the camera so all geometry of the scene in \a path is
contained in the view volume of the camera.
Finds the bounding box of the scene and calls
SoCamera::viewBoundingBox().
*/
void
SoCamera::viewAll(SoPath * const path, const SbViewportRegion & vpregion,
const float slack)
{
SoGetBoundingBoxAction action(vpregion);
action.apply(path);
SbBox3f box = action.getBoundingBox();
// Only check for "flagged" emptiness and don't use
// SbBox3f::hasVolume(), as we *can* handle flat boxes (in all
// dimensions).
if (box.isEmpty()) { return; }
this->viewBoundingBox(box, this->aspectRatio.getValue(), slack);
}
/*!
Based in the SoCamera::viewportMapping setting, convert the values
of \a region to the viewport region we will actually render into.
*/
SbViewportRegion
SoCamera::getViewportBounds(const SbViewportRegion & region) const
{
SbViewportRegion vp = region;
switch (this->viewportMapping.getValue()) {
case CROP_VIEWPORT_FILL_FRAME:
case CROP_VIEWPORT_LINE_FRAME:
case CROP_VIEWPORT_NO_FRAME:
{
float vpaspect = region.getViewportAspectRatio();
float camaspect = this->aspectRatio.getValue();
if (vpaspect > camaspect) {
vp.scaleWidth(camaspect / vpaspect);
return vp;
}
else if (vpaspect < camaspect) {
vp.scaleHeight(vpaspect / camaspect);
}
}
break;
default:
// do nothing
break;
}
return vp;
}
// Doc in superclass.
void
SoCamera::GLRender(SoGLRenderAction * action)
{
SoState * state = action->getState();
SbViewportRegion vp;
SbViewVolume vv;
this->getView(action, vv, vp, FALSE);
SbMatrix affine, proj;
if (vv.getDepth() == 0.0f || vv.getWidth() == 0.0f || vv.getHeight() == 0.0f) {
// Handle empty scenes.
affine = proj = SbMatrix::identity();
}
else {
if (this->stereomode != MONOSCOPIC) {
SbViewVolume copyvv = vv;
SbMatrix dummy;
float offset = this->stereoadjustment * 0.5f;
if (this->stereomode == LEFT_VIEW) offset = -offset;
SbVec3f r = vv.getProjectionDirection().cross(vv.getViewUp());
(void) r.normalize();
// get the current camera transformation/size
vv.getMatrices(affine, proj);
affine = affine.inverse();
float nearv, farv, left, right, top, bottom;
nearv = vv.getNearDist();
farv = nearv + vv.getDepth();
right = vv.getWidth() * 0.5f;
left = -right;
top = vv.getHeight() * 0.5f;
bottom = -top;
// create a skewed frustum
float focaldist = this->focalDistance.getValue() * this->balanceadjustment;
if (focaldist < nearv) focaldist = nearv;
left -= offset * nearv / focaldist;
right -= offset * nearv / focaldist;
vv.frustum(left,right,bottom,top,nearv,farv);
// transform the skewed view volume to the same location as the original
vv.transform(affine);
// translate to account for left/right view
affine.setTranslate(r * offset);
vv.transform(affine);
// read out the stereo view volume
vv.getMatrices(affine, proj);
}
else {
vv.getMatrices(affine, proj);
}
SbBool identity;
const SbMatrix & mm = SoModelMatrixElement::get(state, identity);
if (!identity) {
affine.multRight(mm.inverse());
vv.transform(SoModelMatrixElement::get(state));
}
SoCullElement::setViewVolume(state, vv);
}
SoViewVolumeElement::set(state, this, vv);
if (action->getNumPasses() > 1) {
SbVec3f jittervec;
this->jitter(action->getNumPasses(), SoGLRenderPassElement::get(state),
vp, jittervec);
SbMatrix m;
m.setTranslate(jittervec);
proj.multRight(m);
}
SoProjectionMatrixElement::set(state, this, proj);
SoViewingMatrixElement::set(state, this, affine);
SoFocalDistanceElement::set(state, this, this->focalDistance.getValue());
}
// Documented in superclass.
void
SoCamera::audioRender(SoAudioRenderAction *action)
{
SoState * state = action->getState();
SbBool setbylistener;
setbylistener = SoListenerPositionElement::isSetByListener(state);
if ((! setbylistener) && (! this->position.isIgnored())) {
SbVec3f pos, worldpos;
pos = this->position.getValue();
SoModelMatrixElement::get(action->getState()).multVecMatrix(pos, worldpos);
SoListenerPositionElement::set(state, this, worldpos, FALSE);
#if COIN_DEBUG && 0
float x, y, z;
worldpos.getValue(x, y, z);
SoDebugError::postInfo("SoCamera::audioRender","listenerpos (%0.2f, %0.2f, %0.2f)\n", x, y, z);
#endif // debug
} else {
#if COIN_DEBUG && 0
SoDebugError::postInfo("SoCamera::audioRender","ignoring listenerpos\n");
#endif // debug
}
setbylistener = SoListenerOrientationElement::isSetByListener(state);
if ((! setbylistener) && (! this->orientation.isIgnored())) {
SbBool mmidentity;
SbRotation r;
SbMatrix m = SoModelMatrixElement::get(state, mmidentity);
if (!mmidentity) {
SbVec3f t;
SbVec3f s;
SbRotation so;
m.getTransform(t, r, s, so);
r *= this->orientation.getValue();
}
else {
r = this->orientation.getValue();
}
SoListenerOrientationElement::set(state, this, r, FALSE);
}
// Set view volume. This is needed for LOD nodes to work properly.
SbViewportRegion vp;
SbViewVolume vv;
this->getView(action, vv, vp, FALSE);
if (! (vv.getDepth() == 0.0f || vv.getWidth() == 0.0f || vv.getHeight() == 0.0f) ) {
SbBool identity;
const SbMatrix & mm = SoModelMatrixElement::get(state, identity);
if (!identity)
vv.transform(mm);
}
SoViewVolumeElement::set(state, this, vv);
}
// Doc in superclass.
void
SoCamera::getBoundingBox(SoGetBoundingBoxAction * action)
{
SoCacheElement::invalidate(action->getState());
SoCamera::doAction(action);
}
/*!
Picking actions can be triggered during handle event action
traversal, and to do picking we need to know the camera state.
\sa SoCamera::rayPick()
*/
void
SoCamera::handleEvent(SoHandleEventAction * action)
{
SbViewportRegion vp;
SbViewVolume vv;
this->getView(action, vv, vp, FALSE);
SoViewVolumeElement::set(action->getState(), this, vv);
}
/*!
"Jitter" the camera according to the current rendering pass (\a
curpass), to get an antialiased rendering of the scene when doing
multipass rendering.
*/
void
SoCamera::jitter(int numpasses, int curpass, const SbViewportRegion & vpreg,
SbVec3f & jitteramount) const
{
int vpsize[2];
vpsize[0] = vpreg.getViewportSizePixels()[0];
vpsize[1] = vpreg.getViewportSizePixels()[1];
coin_viewvolume_jitter(numpasses, curpass, vpsize, (float*) jitteramount.getValue());
}
// Documented in superclass. Overridden to set up the viewing and
// projection matrices.
void
SoCamera::doAction(SoAction * action)
{
SoState * state = action->getState();
SbViewportRegion vp;
SbViewVolume vv;
this->getView(action, vv, vp, FALSE);
SbMatrix affine, proj;
if (vv.getDepth() == 0.0f || vv.getWidth() == 0.0f || vv.getHeight() == 0.0f) {
// Handle empty scenes.
affine = proj = SbMatrix::identity();
}
else {
vv.getMatrices(affine, proj);
SbBool identity;
const SbMatrix & mm = SoModelMatrixElement::get(state, identity);
if (!identity) {
vv.transform(mm);
affine.multRight(mm.inverse());
}
}
SoViewVolumeElement::set(state, this, vv);
SoProjectionMatrixElement::set(state, this, proj);
SoViewingMatrixElement::set(state, this, affine);
SoFocalDistanceElement::set(state, this, this->focalDistance.getValue());
}
// Doc in superclass.
void
SoCamera::callback(SoCallbackAction * action)
{
SoCamera::doAction(action);
}
// Documented in superclass.
void
SoCamera::rayPick(SoRayPickAction * action)
{
// Overridden to calculate the coordinates of the ray within the
// current camera settings.
SoCamera::doAction(action);
// We need to check for a non-empty view volume, as caused by scene
// graphs with no geometry in them.
SbViewVolume vv = this->getViewVolume(1.0f);
if (vv.getDepth() != 0.0f &&
vv.getWidth() != 0.0f &&
vv.getHeight() != 0.0f) {
action->computeWorldSpaceRay();
}
}
// Documented in superclass.
void
SoCamera::getPrimitiveCount(SoGetPrimitiveCountAction * action)
{
// The number of primitives used to render a shape can change
// according to the shape's distance to the camera, so we need to
// override this method from the superclass to modify the traversal
// state settings for the camera view.
SoCamera::doAction(action);
}
//
// private method which calculates view volume, and calculates
// new viewport region if viewportMapping requires this.
// The state is updated with the new viewport, not with the
// new view volume.
//
void
SoCamera::getView(SoAction * action, SbViewVolume & resultvv, SbViewportRegion & resultvp,
const SbBool considermodelmatrix)
{
SoState * state = action->getState();
// need to test if vp element is enabled. SoGetPrimitiveCountAction
// does not enable this element, although I think it should (to get
// correct SCREEN_SPACE complexity handling). pederb, 2001-10-31
SbBool usevpelement =
state->isElementEnabled(SoViewportRegionElement::getClassStackIndex());
if (usevpelement) {
resultvp = SoViewportRegionElement::get(state);
}
else {
// just set it to some value. It's not important as the current
// action does not support viewports.
resultvp = SbViewportRegion(256, 256);
}
float aspectratio = resultvp.getViewportAspectRatio();
int vpm = this->viewportMapping.getValue();
SbBool adjustvp = FALSE;
switch (vpm) {
case CROP_VIEWPORT_FILL_FRAME:
case CROP_VIEWPORT_LINE_FRAME:
case CROP_VIEWPORT_NO_FRAME:
resultvv = this->getViewVolume(0.0f);
adjustvp = TRUE;
break;
case ADJUST_CAMERA:
resultvv = this->getViewVolume(aspectratio);
if (aspectratio < 1.0f) resultvv.scale(1.0f / aspectratio);
break;
case LEAVE_ALONE:
resultvv = this->getViewVolume(0.0f);
break;
default:
assert(0 && "unknown viewport mapping");
break;
}
if (considermodelmatrix) {
SbBool isidentity;
const SbMatrix &mm = SoModelMatrixElement::get(state, isidentity);
if (!isidentity) resultvv.transform(mm);
}
if (adjustvp) {
float cameraratio = this->aspectRatio.getValue();
if (aspectratio != cameraratio) {
SbViewportRegion oldvp = resultvp;
if (aspectratio < cameraratio) {
resultvp.scaleHeight(aspectratio/cameraratio);
}
else {
resultvp.scaleWidth(cameraratio/aspectratio);
}
// only draw if this is an SoGLRenderAction
if (action->isOfType(SoGLRenderAction::getClassTypeId())) {
this->drawCroppedFrame((SoGLRenderAction*)action, vpm, oldvp, resultvp);
}
if (usevpelement) {
SoViewportRegionElement::set(action->getState(), resultvp);
}
}
}
}
//
// private method that draws a cropped frame
//
void
SoCamera::drawCroppedFrame(SoGLRenderAction *action,
const int viewportmapping,
const SbViewportRegion & oldvp,
const SbViewportRegion & newvp)
{
if (viewportmapping == SoCamera::CROP_VIEWPORT_NO_FRAME) return;
if (action->handleTransparency(FALSE))
return;
SoState *state = action->getState();
state->push();
if (viewportmapping == SoCamera::CROP_VIEWPORT_LINE_FRAME) {
SoLineWidthElement::set(state, this, 1.0f);
}
else { // FILL
SoDrawStyleElement::set(state, this, SoDrawStyleElement::FILLED);
// turn off backface culling
SoGLShapeHintsElement::forceSend(state, TRUE, FALSE);
}
SbVec2s oldorigin = oldvp.getViewportOriginPixels();
SbVec2s oldsize = oldvp.getViewportSizePixels();
glMatrixMode(GL_PROJECTION);
// projection matrix will be set later, so don't push
glOrtho(oldorigin[0], oldorigin[0]+oldsize[0]-1,
oldorigin[1], oldorigin[1]+oldsize[1]-1,
-1, 1);
SoGLTextureEnabledElement::set(state, this, FALSE);
SoGLTexture3EnabledElement::set(state, this, FALSE);
SoGLMultiTextureEnabledElement::disableAll(state);
glPushAttrib(GL_LIGHTING_BIT|
GL_FOG_BIT|
GL_DEPTH_BUFFER_BIT|
GL_TEXTURE_BIT|
GL_CURRENT_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glDisable(GL_LIGHTING);
glDisable(GL_FOG);
glDisable(GL_DEPTH_TEST);
glColor3f(0.8f, 0.8f, 0.8f);
SbVec2s origin = newvp.getViewportOriginPixels();
SbVec2s size = newvp.getViewportSizePixels();
SbVec2s orgsize = oldvp.getViewportSizePixels();
if (size[0] < orgsize[0]) {
short minpos = origin[0] - 1;
short maxpos = origin[0] + size[0];
if (viewportmapping == SoCamera::CROP_VIEWPORT_LINE_FRAME) {
glBegin(GL_LINES);
glVertex2s(minpos, oldorigin[1]);
glVertex2s(minpos, oldorigin[1]+oldsize[1]);
glVertex2s(maxpos, oldorigin[1]);
glVertex2s(maxpos, oldorigin[1]+oldsize[1]);
glEnd();
}
else {
glBegin(GL_QUADS);
glVertex2s(oldorigin[0], oldorigin[1]);
glVertex2s(oldorigin[0], oldorigin[1]+oldsize[1]-1);
glVertex2s(minpos, oldorigin[1]+oldsize[1]);
glVertex2s(minpos, oldorigin[1]);
glVertex2s(maxpos, oldorigin[1]);
glVertex2s(maxpos, oldorigin[1]+oldsize[1]-1);
glVertex2s(oldorigin[0]+oldsize[0]-1, oldorigin[1]+oldsize[1]-1);
glVertex2s(oldorigin[0]+oldsize[0]-1, oldorigin[1]);
glEnd();
}
}
else if (size[1] < orgsize[1]) {
short minpos = origin[1] - 1;
short maxpos = origin[1] + size[1];
if (viewportmapping == SoCamera::CROP_VIEWPORT_LINE_FRAME) {
glBegin(GL_LINES);
glVertex2s(oldorigin[0], minpos);
glVertex2s(oldorigin[0]+oldsize[0], minpos);
glVertex2s(oldorigin[0], maxpos);
glVertex2s(oldorigin[0]+oldsize[0], maxpos);
glEnd();
}
else {
glBegin(GL_QUADS);
glVertex2s(oldorigin[0], minpos);
glVertex2s(oldorigin[0]+oldsize[0]-1, minpos);
glVertex2s(oldorigin[0]+oldsize[0]-1, oldorigin[1]);
glVertex2s(oldorigin[0], oldorigin[1]);
glVertex2s(oldorigin[0], maxpos);
glVertex2s(oldorigin[0], oldorigin[1]+oldsize[1]-1);
glVertex2s(oldorigin[0]+oldsize[0]-1, oldorigin[1]+oldsize[1]-1);
glVertex2s(oldorigin[1]+oldsize[0]-1, maxpos);
glEnd();
}
}
glPopMatrix();
glPopAttrib();
state->pop();
}
/*!
Sets the stereo mode.
*/
void
SoCamera::setStereoMode(StereoMode mode)
{
this->stereomode = mode;
}
/*!
Returns the stereo mode.
*/
SoCamera::StereoMode
SoCamera::getStereoMode(void) const
{
return this->stereomode;
}
/*!
Sets the stereo adjustment. This is the distance between the left
and right "eye" when doing stereo rendering.
When doing stereo rendering, Coin will render two views, one for the
left eye, and one for the right eye. The stereo adjustment is, a bit
simplified, how much the camera is translated along the local X-axis
between the left and the right view.
The default distance is 0.1, which is chosen since it's the
approximate distance between the human eyes.
To create a nice looking and visible stereo effect, the application
programmer will often have to adjust this value. If all you want to
do is examine simple stand-alone 3D objects, it is possible to
calculate a stereo offset based on the bounding box of the 3D model
(or scale the model down to an appropriate size).
However, if you have a large scene, where you want to fly around in
the scene, and see stereo on different objects as you approach them,
you can't calculate the stereo offset based on the bounding box
of the scene, but rather use a stereo offset based on the scale of
the individual objects/details you want to examine.
Please note that it's important to set a sensible focal distance
when doing stereo rendering. See setBalanceAdjustment() for
information about how the focal distance affects the stereo
rendering.
\sa setBalanceAdjustment()
*/
void
SoCamera::setStereoAdjustment(float adjustment)
{
this->stereoadjustment = adjustment;
}
/*!
Returns the stereo adjustment.
\sa setStereoAdjustment()
*/
float
SoCamera::getStereoAdjustment(void) const
{
return this->stereoadjustment;
}
/*!
Sets the stereo balance adjustment. This is a factor that enables you to
move the zero parallax plane. Geometry in front of the zero parallax plane
will appear to be in front of the screen.
The balance adjustment is multiplied with the focal distance to find
the zero parallax plane. The default value is 1.0, and the zero
parallax plane is then at the focal point.
\sa SoCamera::focalDistance
*/
void
SoCamera::setBalanceAdjustment(float adjustment)
{
this->balanceadjustment = adjustment;
}
/*!
Returns the stereo balance adjustment.
\sa setBalanceAdjustment()
*/
float
SoCamera::getBalanceAdjustment(void) const
{
return this->balanceadjustment;
}
// Private method that calculates a new orientation based on camera
// direction and camera up vector. Vectors must be unit length.
void
SoCamera::lookAt(const SbVec3f & dir, const SbVec3f & up)
{
SbVec3f z = -dir;
SbVec3f y = up;
SbVec3f x = y.cross(z);
// recompute y to create a valid coordinate system
y = z.cross(x);
// normalize x and y to create an orthonormal coord system
if ((y.normalize() == 0.0f) ||
(x.normalize() == 0.0f)) {
#if COIN_DEBUG
SoDebugError::postInfo("SoCamera::lookAt",
"Unable to create a rotation matrix "
"(dir = %g %g %g, up = %g %g %g)\n",
dir[0], dir[1], dir[2],
up[0], up[1], up[2]);
#endif // debug
return;
}
// create a rotation matrix
SbMatrix rot = SbMatrix::identity();
rot[0][0] = x[0];
rot[0][1] = x[1];
rot[0][2] = x[2];
rot[1][0] = y[0];
rot[1][1] = y[1];
rot[1][2] = y[2];
rot[2][0] = z[0];
rot[2][1] = z[1];
rot[2][2] = z[2];
this->orientation.setValue(SbRotation(rot));
}
|