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
|
/**************************************************************************\
*
* 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
*
\**************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H
#ifdef HAVE_VRML97
/*!
\class SoVRMLBackground SoVRMLBackground.h Inventor/VRMLnodes/SoVRMLBackground.h
\brief The SoVRMLBackground class is used for specifying a viewer panorama.
\ingroup VRMLnodes
\WEB3DCOPYRIGHT
\verbatim
Background {
eventIn SFBool set_bind
exposedField MFFloat groundAngle [] # [0,pi/2]
exposedField MFColor groundColor [] # [0,1]
exposedField MFString backUrl []
exposedField MFString bottomUrl []
exposedField MFString frontUrl []
exposedField MFString leftUrl []
exposedField MFString rightUrl []
exposedField MFString topUrl []
exposedField MFFloat skyAngle [] # [0,pi]
exposedField MFColor skyColor 0 0 0 # [0,1]
eventOut SFBool isBound
}
\endverbatim
The Background node is used to specify a colour backdrop that
simulates ground and sky, as well as a background texture, or
panorama, that is placed behind all geometry in the scene and in
front of the ground and sky. Background nodes are specified in the
local coordinate system and are affected by the accumulated rotation
of their ancestors as described below. Background nodes are
bindable nodes as described in 4.6.10, Bindable children nodes
(<http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-VRML97/part1/concepts.html#4.6.10>).
There exists a Background stack, in which the top-most Background on
the stack is the currently active Background. To move a Background
to the top of the stack, a TRUE value is sent to the set_bind
eventIn. Once active, the Background is then bound to the browsers
view. A FALSE value sent to set_bind removes the Background from the
stack and unbinds it from the browser's view. More detail on the
bind stack is described in 4.6.10, Bindable children nodes
(<http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-VRML97/part1/concepts.html#4.6.10>).
The backdrop is conceptually a partial sphere (the ground) enclosed
inside of a full sphere (the sky) in the local coordinate system
with the viewer placed at the centre of the spheres. Both spheres
have infinite radius and each is painted with concentric circles of
interpolated colour perpendicular to the local Y-axis of the
sphere. The Background node is subject to the accumulated rotations
of its ancestors' transformations. Scaling and translation
transformations are ignored. The sky sphere is always slightly
farther away from the viewer than the ground partial sphere causing
the ground to appear in front of the sky where they overlap. The
skyColor field specifies the colour of the sky at various angles on
the sky sphere. The first value of the skyColor field specifies the
colour of the sky at 0.0 radians representing the zenith (i.e.,
straight up from the viewer). The skyAngle field specifies the
angles from the zenith in which concentric circles of colour
appear. The zenith of the sphere is implicitly defined to be 0.0
radians, the natural horizon is at pi/2 radians, and the nadir
(i.e., straight down from the viewer) is at pi radians. skyAngle is
restricted to non-decreasing values in the range [0.0, pi]. There
shall be one more skyColor value than there are skyAngle values. The
first colour value is the colour at the zenith, which is not
specified in the skyAngle field. If the last skyAngle is less than
pi, then the colour band between the last skyAngle and the nadir is
clamped to the last skyColor. The sky colour is linearly
interpolated between the specified skyColor values.
The groundColor field specifies the colour of the ground at the
various angles on the ground partial sphere. The first value of the
groundColor field specifies the colour of the ground at 0.0 radians
representing the nadir (i.e., straight down from the user). The
groundAngle field specifies the angles from the nadir that the
concentric circles of colour appear. The nadir of the sphere is
implicitly defined at 0.0 radians. groundAngle is restricted to
non-decreasing values in the range [0.0, pi/2]. There shall be one
more groundColor value than there are groundAngle values. The first
colour value is for the nadir which is not specified in the
groundAngle field. If the last groundAngle is less than pi/2, the
region between the last groundAngle and the equator is
non-existant. The ground colour is linearly interpolated between the
specified groundColor values.
The backUrl, bottomUrl, frontUrl, leftUrl, rightUrl, and topUrl
fields specify a set of images that define a background panorama
between the ground/sky backdrop and the scene's geometry. The
panorama consists of six images, each of which is mapped onto a face
of an infinitely large cube contained within the backdrop spheres
and centred in the local coordinate system. The images are applied
individually to each face of the cube. On the front, back, right,
and left faces of the cube, when viewed from the origin looking down
the negative Z-axis with the Y-axis as the view up direction, each
image is mapped onto the corresponding face with the same
orientation as if the image were displayed normally in 2D (backUrl
to back face, frontUrl to front face, leftUrl to left face, and
rightUrl to right face). On the top face of the cube, when viewed
from the origin looking along the +Y-axis with the +Z-axis as the
view up direction, the topUrl image is mapped onto the face with the
same orientation as if the image were displayed normally in 2D. On
the bottom face of the box, when viewed from the origin along the
negative Y-axis with the negative Z-axis as the view up direction,
the bottomUrl image is mapped onto the face with the same
orientation as if the image were displayed normally in 2D.
<center>
<img src="http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-VRML97/Images/background.gif">
Figure 6.1
</center>
Figure 6.1 illustrates the Background node backdrop and background
textures. Alpha values in the panorama images (i.e., two or four
component images) specify that the panorama is semi-transparent or
transparent in regions, allowing the groundColor and skyColor to be
visible. See 4.6.11, Texture maps, for a general description of
texture maps. Often, the bottomUrl and topUrl images will not be
specified, to allow sky and ground to show. The other four images
may depict surrounding mountains or other distant scenery. Browsers
shall support the JPEG (see 2.[JPEG]) and PNG (see 2.[PNG]) image
file formats, and in addition, may support any other image format
(e.g., CGM) that can be rendered into a 2D image. Support for the
GIF (see E.[GIF]) format is recommended (including transparency).
More detail on the url fields can be found in 4.5, VRML and the
World Wide Web
(<http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-VRML97/part1/concepts.html#4.5>).
*/
// *************************************************************************
#include <Inventor/VRMLnodes/SoVRMLBackground.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <Inventor/SbRotation.h>
#include <Inventor/SoDB.h>
#include <Inventor/SoInput.h>
#include <Inventor/VRMLnodes/SoVRMLImageTexture.h>
#include <Inventor/VRMLnodes/SoVRMLMacros.h>
#include <Inventor/actions/SoGLRenderAction.h>
#include <Inventor/actions/SoGetBoundingBoxAction.h>
#include <Inventor/elements/SoCacheElement.h>
#include <Inventor/elements/SoModelMatrixElement.h>
#include <Inventor/elements/SoProjectionMatrixElement.h>
#include <Inventor/elements/SoViewVolumeElement.h>
#include <Inventor/elements/SoViewVolumeElement.h>
#include <Inventor/elements/SoViewingMatrixElement.h>
#include <Inventor/elements/SoViewportRegionElement.h>
#include <Inventor/elements/SoDepthBufferElement.h>
#include <Inventor/errors/SoDebugError.h>
#include <Inventor/misc/SoChildList.h>
#include <Inventor/nodes/SoIndexedFaceSet.h>
#include <Inventor/nodes/SoIndexedTriangleStripSet.h>
#include <Inventor/nodes/SoLightModel.h>
#include <Inventor/nodes/SoMaterialBinding.h>
#include <Inventor/nodes/SoPerspectiveCamera.h>
#include <Inventor/nodes/SoScale.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoShapeHints.h>
#include <Inventor/nodes/SoVertexProperty.h>
#include <Inventor/sensors/SoFieldSensor.h>
#include <Inventor/system/gl.h>
#include <Inventor/C/tidbits.h>
#include "nodes/SoSubNodeP.h"
// *************************************************************************
/*!
SoMFFloat SoVRMLBackground::groundAngle
The ground angles where different colors should be used.
*/
/*!
SoMFColor SoVRMLBackground::groundColor
The color for each groundAngle.
*/
/*!
SoMFFloat SoVRMLBackground::skyAngle
The sky angles where different colors should be used.
*/
/*!
SoMFColor SoVRMLBackground::skyColor
The color for each skyAngle.
*/
/*!
SoMFString SoVRMLBackground::backUrl
URL for the background image.
*/
/*!
SoMFString SoVRMLBackground::bottomUrl
URL for the bottom image.
*/
/*!
SoMFString SoVRMLBackground::frontUrl
URL for the front image.
*/
/*!
SoMFString SoVRMLBackground::leftUrl
URL for the left image.
*/
/*!
SoMFString SoVRMLBackground::rightUrl
URL for the right image.
*/
/*!
SoMFString SoVRMLBackground::topUrl
URL for the top image.
*/
/*!
SoSFBool SoVRMLBackground::set_bind
An eventIn which is triggered when the node is bound.
*/
/*!
SoSFBool SoVRMLBackground::isBound
An eventOut that is sent after the node has been bound/unbound.
*/
// *************************************************************************
SO_NODE_SOURCE(SoVRMLBackground);
// *************************************************************************
static char background_scenery_data[] = {
"#Inventor 2.1 ascii\n\n"
" BaseColor { rgb [1 1 1] }\n"
" Coordinate3 { point [-1 -1 -1, -1 1 -1, 1 1 -1, 1 -1 -1, -1 -1 1, -1 1 1, 1 1 1, 1 -1 1]}\n"
" TextureCoordinate2 { point [0 0, 1 0, 1 1, 0 1] }\n"
" TextureCoordinateBinding { value PER_VERTEX_INDEXED }\n"
};
// *************************************************************************
static void background_geometrychangeCB(void * data, SoSensor * sensor);
static void background_vrmltexturechangeCB(void * data, SoSensor * sensor);
static void background_bindingchangeCB(void * data, SoSensor * sensor);
static float vrmlbackground_viewup[] = {0.0f, 1.0f, 0.0f};
static SbBool vrmlbackground_viewup_set = FALSE;
// *************************************************************************
class SoVRMLBackgroundP {
public:
SoVRMLBackgroundP(SoVRMLBackground * masterptr) {
this->master = masterptr;
};
SoVRMLBackground * master;
SoSeparator * rootnode;
SoPerspectiveCamera * camera;
SoChildList * children;
SoFieldSensor * setbindsensor;
SoFieldSensor * isboundsensor;
SoFieldSensor * groundanglesensor;
SoFieldSensor * groundcolorsensor;
SoFieldSensor * skyanglesensor;
SoFieldSensor * skycolorsensor;
SoFieldSensor * backurlsensor;
SoFieldSensor * fronturlsensor;
SoFieldSensor * lefturlsensor;
SoFieldSensor * righturlsensor;
SoFieldSensor * bottomurlsensor;
SoFieldSensor * topurlsensor;
SoVRMLImageTexture * fronttexture;
SoVRMLImageTexture * backtexture;
SoVRMLImageTexture * lefttexture;
SoVRMLImageTexture * righttexture;
SoVRMLImageTexture * toptexture;
SoVRMLImageTexture * bottomtexture;
SoSeparator * frontface;
SoSeparator * backface;
SoSeparator * bottomface;
SoSeparator * topface;
SoSeparator * leftface;
SoSeparator * rightface;
SbBool geometrybuilt;
void buildGeometry(void);
void modifyCubeFace(SoMFString & urls, SoSeparator * facesep, const int32_t * vindices);
SoSeparator * createCubeFace(SoMFString & urls, SoSeparator * sep, const int32_t * vindices);
void buildIndexList(SoIndexedTriangleStripSet * sphere, int len, int slices, int matlength);
};
#define PRIVATE(p) (p->pimpl)
#define PUBLIC(p) (p->master)
// *************************************************************************
// dummy callback for SoGetBoundingBoxAction. A background node has no
// boundingbox, so we just invalidate the bbox cache so stop the node
// from being culled.
static void
background_bbfix(SoAction * action, SoNode * node)
{
SoCacheElement::invalidate(action->getState());
}
// Doc in parent
void
SoVRMLBackground::initClass(void) // static
{
SO_NODE_INTERNAL_INIT_CLASS(SoVRMLBackground, SO_VRML97_NODE_TYPE);
SoGetBoundingBoxAction::addMethod(SoVRMLBackground::getClassTypeId(),
background_bbfix);
const char * env = coin_getenv("COIN_VIEWUP");
if (env) {
float data[3];
int n = sscanf(env, "%f%f%f", &data[0], &data[1], &data[2]);
if (n == 3) {
SbVec3f v(data[0], data[1], data[2]);
v.normalize();
vrmlbackground_viewup[0] = v[0];
vrmlbackground_viewup[1] = v[1];
vrmlbackground_viewup[2] = v[2];
vrmlbackground_viewup_set = TRUE;
}
}
}
/*!
Constructor.
*/
SoVRMLBackground::SoVRMLBackground(void)
{
SO_VRMLNODE_INTERNAL_CONSTRUCTOR(SoVRMLBackground);
SO_VRMLNODE_ADD_EMPTY_EXPOSED_MFIELD(groundColor);
SO_VRMLNODE_ADD_EMPTY_EXPOSED_MFIELD(skyColor);
SO_VRMLNODE_ADD_EMPTY_EXPOSED_MFIELD(groundAngle);
SO_VRMLNODE_ADD_EMPTY_EXPOSED_MFIELD(skyAngle);
SO_VRMLNODE_ADD_EMPTY_EXPOSED_MFIELD(backUrl);
SO_VRMLNODE_ADD_EMPTY_EXPOSED_MFIELD(bottomUrl);
SO_VRMLNODE_ADD_EMPTY_EXPOSED_MFIELD(frontUrl);
SO_VRMLNODE_ADD_EMPTY_EXPOSED_MFIELD(leftUrl);
SO_VRMLNODE_ADD_EMPTY_EXPOSED_MFIELD(rightUrl);
SO_VRMLNODE_ADD_EMPTY_EXPOSED_MFIELD(topUrl);
SO_VRMLNODE_ADD_EVENT_IN(set_bind);
SO_VRMLNODE_ADD_EVENT_OUT(isBound);
PRIVATE(this) = new SoVRMLBackgroundP(this);
PRIVATE(this)->children = new SoChildList(this);
// Binding sensors
PRIVATE(this)->setbindsensor = new SoFieldSensor(background_bindingchangeCB, PRIVATE(this));
PRIVATE(this)->isboundsensor = new SoFieldSensor(background_bindingchangeCB, PRIVATE(this));
PRIVATE(this)->setbindsensor->attach(&this->set_bind);
PRIVATE(this)->isboundsensor->attach(&this->isBound);
PRIVATE(this)->setbindsensor->setPriority(5);
PRIVATE(this)->isboundsensor->setPriority(5);
// Geometry sensors
PRIVATE(this)->groundanglesensor = new SoFieldSensor(background_geometrychangeCB, PRIVATE(this));
PRIVATE(this)->groundcolorsensor = new SoFieldSensor(background_geometrychangeCB, PRIVATE(this));
PRIVATE(this)->skyanglesensor = new SoFieldSensor(background_geometrychangeCB, PRIVATE(this));
PRIVATE(this)->skycolorsensor = new SoFieldSensor(background_geometrychangeCB, PRIVATE(this));
PRIVATE(this)->groundanglesensor->attach(&this->groundAngle);
PRIVATE(this)->groundcolorsensor->attach(&this->groundColor);
PRIVATE(this)->skyanglesensor->attach(&this->skyAngle);
PRIVATE(this)->skycolorsensor->attach(&this->skyColor);
PRIVATE(this)->groundanglesensor->setPriority(5);
PRIVATE(this)->groundcolorsensor->setPriority(5);
PRIVATE(this)->skyanglesensor->setPriority(5);
PRIVATE(this)->skycolorsensor->setPriority(5);
// URL/skybox sensors
PRIVATE(this)->backurlsensor = new SoFieldSensor(background_vrmltexturechangeCB, PRIVATE(this));
PRIVATE(this)->fronturlsensor = new SoFieldSensor(background_vrmltexturechangeCB, PRIVATE(this));
PRIVATE(this)->lefturlsensor = new SoFieldSensor(background_vrmltexturechangeCB, PRIVATE(this));
PRIVATE(this)->righturlsensor = new SoFieldSensor(background_vrmltexturechangeCB, PRIVATE(this));
PRIVATE(this)->bottomurlsensor = new SoFieldSensor(background_vrmltexturechangeCB, PRIVATE(this));
PRIVATE(this)->topurlsensor = new SoFieldSensor(background_vrmltexturechangeCB, PRIVATE(this));
PRIVATE(this)->backurlsensor->attach(&this->backUrl);
PRIVATE(this)->fronturlsensor->attach(&this->frontUrl);
PRIVATE(this)->lefturlsensor->attach(&this->leftUrl);
PRIVATE(this)->righturlsensor->attach(&this->rightUrl);
PRIVATE(this)->bottomurlsensor->attach(&this->bottomUrl);
PRIVATE(this)->topurlsensor->attach(&this->topUrl);
PRIVATE(this)->backurlsensor->setPriority(5);
PRIVATE(this)->fronturlsensor->setPriority(5);
PRIVATE(this)->lefturlsensor->setPriority(5);
PRIVATE(this)->righturlsensor->setPriority(5);
PRIVATE(this)->bottomurlsensor->setPriority(5);
PRIVATE(this)->topurlsensor->setPriority(5);
PRIVATE(this)->geometrybuilt = FALSE;
PRIVATE(this)->camera = NULL;
PRIVATE(this)->rootnode = NULL;
}
/*!
Destructor.
*/
SoVRMLBackground::~SoVRMLBackground()
{
if (PRIVATE(this)->geometrybuilt) {
PRIVATE(this)->rootnode->removeAllChildren();
PRIVATE(this)->rootnode->unref();
}
delete PRIVATE(this)->backurlsensor;
delete PRIVATE(this)->fronturlsensor;
delete PRIVATE(this)->lefturlsensor;
delete PRIVATE(this)->righturlsensor;
delete PRIVATE(this)->bottomurlsensor;
delete PRIVATE(this)->topurlsensor;
delete PRIVATE(this)->groundanglesensor;
delete PRIVATE(this)->groundcolorsensor;
delete PRIVATE(this)->skyanglesensor;
delete PRIVATE(this)->skycolorsensor;
delete PRIVATE(this)->setbindsensor;
delete PRIVATE(this)->isboundsensor;
delete PRIVATE(this)->children;
delete PRIVATE(this);
}
// Doc in parent
void
SoVRMLBackground::GLRender(SoGLRenderAction * action)
{
if (!PRIVATE(this)->geometrybuilt) { PRIVATE(this)->buildGeometry(); }
SoState * state = action->getState();
// push state since we're going to modify the model matrix
state->push();
const SbMatrix & tmp = SoViewingMatrixElement::get(state);
SbRotation rot(tmp);
if (vrmlbackground_viewup_set) {
// create a rotation from the positive Y axis to the new view up
SbRotation r2(SbVec3f(0.0f, 1.0f, 0.0f),
SbVec3f(vrmlbackground_viewup[0],
vrmlbackground_viewup[1],
vrmlbackground_viewup[2]));
r2 *= rot;
PRIVATE(this)->camera->orientation = r2.inverse();
}
else {
PRIVATE(this)->camera->orientation = rot.inverse();
}
// rotate background camera so that it matches the current camera
// set to identity before rendering subgraph
SoModelMatrixElement::makeIdentity(state, this);
SbBool test_out, write_out;
SoDepthBufferElement::DepthWriteFunction function_out;
SbVec2f range_out;
SoDepthBufferElement::get(state, test_out, write_out,
function_out, range_out);
range_out[0] = 1.0f;
range_out[1] = 1.0f;
SoDepthBufferElement::set(state, test_out, write_out,
function_out, range_out);
int numindices;
const int * indices;
if (action->getPathCode(numindices, indices) == SoAction::IN_PATH) {
PRIVATE(this)->children->traverseInPath(action, numindices, indices);
}
else {
PRIVATE(this)->children->traverse((SoAction *) action);
}
// pop back to the old model matrix
state->pop();
}
void
SoVRMLBackgroundP::buildGeometry(void)
{
float sphereradius = 1.5;
SbList <float> angles;
const int slices = 30; // Number of slices, i.e. vertical resolution of the spheres.
this->rootnode = new SoSeparator;
this->rootnode->ref();
// just insert a default perspective camera that is used when
// rendering the background
SoPerspectiveCamera * cam = new SoPerspectiveCamera;
cam->nearDistance = 0.1f;
this->rootnode->addChild(cam);
// just set camera pointer for easy lookup in GLRender()
this->camera = cam;
// Camera orientation will be changed each time GLRender() is
// executed -- to match actual scene graph camera -- so we disable
// notification from the camera to avoid non-stop continuous
// redraws.
(void)this->camera->enableNotify(FALSE);
SoLightModel * lightmodel = new SoLightModel;
lightmodel->model.setValue(SoLightModel::BASE_COLOR);
this->rootnode->addChild(lightmodel);
//
// Sky sphere
//
if ((PUBLIC(this)->skyAngle.getNum() > 0) || (PUBLIC(this)->skyColor.getNum() > 0)) {
angles.append(0);
float angle = 0.0f;
if (PUBLIC(this)->skyAngle.getNum() > 0) {
for (int k=0;k<PUBLIC(this)->skyAngle.getNum();++k) {
if (angle > PUBLIC(this)->skyAngle[k]) {
SoDebugError::postWarning("SoVRMLBackgroundP::buildGeometry","skyAngle array values must be non-decreasing.");
continue;
}
angle = PUBLIC(this)->skyAngle[k];
if (angle > M_PI) {
SoDebugError::postWarning("SoVRMLBackgroundP::buildGeometry","skyAngle=%f > PI not allowed.", angle);
angle = (float) M_PI;
} else if (angle < 0) {
SoDebugError::postWarning("SoVRMLBackgroundP::buildGeometry","skyAngle=%f < 0 not allowed.", angle);
angle = 0.0f;
}
angles.append(angle);
}
if (angle != M_PI)
angles.append((float)M_PI);
}
else { // No angles specified. Creating list based on number of colors.
int num = PUBLIC(this)->skyColor.getNum();
if (num == 1) ++num; // Special case for one-colored sky.
for (int i=0;i<=num;++i)
angles.append((float) ((M_PI/num)*i));
}
int len = angles.getLength();
SbVec3f * skyvertexarray = new SbVec3f[len * slices];
SoIndexedTriangleStripSet * sky = new SoIndexedTriangleStripSet;
SoVertexProperty * skyproperties = new SoVertexProperty;
skyproperties->normalBinding.setValue(SoVertexProperty::PER_VERTEX_INDEXED);
// Calculate vertices and normals
double x, y, z;
int counter = 0;
int i;
for (i=0;i<slices;++i) {
for (int j=0;j<len;++j) {
x = sphereradius * cos(i * ((2 * M_PI) / slices)) * sin(angles[j]);
y = sphereradius * cos(angles[j]);
z = sphereradius * sin(i * ((2 * M_PI) / slices)) * sin(angles[j]);
skyvertexarray[counter++] = SbVec3f((float) x, (float) y, (float) z);
}
}
for (i=0;i<len*slices;++i) {
skyproperties->vertex.set1Value(i, skyvertexarray[i]);
SbVec3f normal = -skyvertexarray[i];
normal.normalize();
skyproperties->normal.set1Value(i, normal);
}
delete [] skyvertexarray;
sky->vertexProperty.setValue(skyproperties);
// Setup color arrays
if (PUBLIC(this)->skyColor.getNum() > 0) {
for (int i=0;i<PUBLIC(this)->skyColor.getNum();++i)
skyproperties->orderedRGBA.set1Value(i, PUBLIC(this)->skyColor[i].getPackedValue(0));
skyproperties->materialBinding = SoMaterialBinding::PER_VERTEX_INDEXED;
} else {
SoDebugError::postWarning("SoVRMLBackgroundP::buildGeometry","No colors specified for the sky.");
return;
}
buildIndexList(sky, len, slices, PUBLIC(this)->skyColor.getNum());
SoShapeHints * shapehintssky = new SoShapeHints;
shapehintssky->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
shapehintssky->shapeType = SoShapeHints::SOLID;
shapehintssky->faceType = SoShapeHints::CONVEX;
this->rootnode->addChild(shapehintssky);
this->rootnode->addChild(sky);
}
//
// Ground sphere
//
if ((PUBLIC(this)->groundAngle.getNum() > 0) || (PUBLIC(this)->groundColor.getNum() > 0)) {
sphereradius = sphereradius * 0.9f;
angles.truncate(0);
angles.append(0);
float angle = 0;
if (PUBLIC(this)->groundAngle.getNum() > 0) {
for (int k=0;k<PUBLIC(this)->groundAngle.getNum();++k) {
if (angle > PUBLIC(this)->groundAngle[k]) {
SoDebugError::postWarning("SoVRMLBackgroundP::buildGeometry","groundAngle array values must be non-decreasing.");
continue;
}
angle = PUBLIC(this)->groundAngle[k];
if (angle > M_PI/2) {
SoDebugError::postWarning("SoVRMLBackgroundP::buildGeometry","groundAngle=%f > PI/2 not allowed.", angle);
angle = (float) (M_PI / 2.0);
} else if (angle < 0) {
SoDebugError::postWarning("SoVRMLBackgroundP::buildGeometry","groundAngle=%f < 0 not allowed.", angle);
angle = 0;
}
angles.append(angle);
}
if (angles.getLength() < 3) // A 'sphere' must have atleast 3 faces
angles.append(angle);
}
else {
int num = PUBLIC(this)->groundColor.getNum();
if (num == 1) ++num;
for (int i=0;i<num;++i)
angles.append((float) ((M_PI/(num))*i));
}
int len = angles.getLength();
SbVec3f * groundvertexarray = new SbVec3f[len * slices];
SoIndexedTriangleStripSet * ground = new SoIndexedTriangleStripSet;
SoVertexProperty * groundproperties = new SoVertexProperty;
groundproperties->normalBinding.setValue(SoVertexProperty::PER_VERTEX_INDEXED);
// Calculate vertices and normals
int counter = 0;
double x,y,z;
int i;
for (i=0;i<slices;++i) {
for (int j=0;j<len;++j) {
x = sphereradius * cos(i * ((2 * M_PI) / slices)) * sin(angles[j]);
y = -sphereradius * cos(angles[j]);
z = sphereradius * sin(i * ((2 * M_PI) / slices)) * sin(angles[j]);
groundvertexarray[counter++] = SbVec3f((float) x, (float) y, (float) z);
}
}
for (i=0;i<len*slices;++i) {
groundproperties->vertex.set1Value(i, groundvertexarray[i]);
SbVec3f normal = -groundvertexarray[i];
normal.normalize();
groundproperties->normal.set1Value(i, normal);
}
delete [] groundvertexarray;
ground->vertexProperty.setValue(groundproperties);
// Setup color arrays
if (PUBLIC(this)->groundColor.getNum() > 0) {
for (int i=0;i<PUBLIC(this)->groundColor.getNum();++i)
groundproperties->orderedRGBA.set1Value(i, PUBLIC(this)->groundColor[i].getPackedValue(0));
groundproperties->materialBinding = SoMaterialBinding::PER_VERTEX_INDEXED;
} else {
SoDebugError::postWarning("SoVRMLBackgroundP::buildGeometry","No colors specified for the ground.");
return;
}
// Build vertex and normal indices
buildIndexList(ground, len, slices, PUBLIC(this)->groundColor.getNum());
SoShapeHints * shapehintsground = new SoShapeHints;
shapehintsground->vertexOrdering = SoShapeHints::CLOCKWISE;
shapehintsground->shapeType = SoShapeHints::SOLID;
shapehintsground->faceType = SoShapeHints::CONVEX;
this->rootnode->addChild(shapehintsground);
this->rootnode->addChild(ground);
}
//
// Scenery cube
//
SoDB::init();
SoInput in;
in.setBuffer(background_scenery_data, strlen(background_scenery_data));
SoSeparator * cubedata = SoDB::readAll(&in);
SoShapeHints * shapehintscube = new SoShapeHints;
shapehintscube->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
shapehintscube->shapeType = SoShapeHints::SOLID;
shapehintscube->faceType = SoShapeHints::CONVEX;
this->rootnode->addChild(shapehintscube);
SoScale * scale = new SoScale;
SbVec3f factor(2, 2, 2);
scale->scaleFactor.setValue(factor);
this->rootnode->addChild(scale);
this->rootnode->addChild(cubedata);
this->frontface = NULL;
this->backface = NULL;
this->leftface = NULL;
this->rightface = NULL;
this->topface = NULL;
this->bottomface = NULL;
if (PUBLIC(this)->backUrl.getNum() != 0) {
const int32_t vindices[] = {4, 5, 6, 7, -1};
SoSeparator * sep = this->createCubeFace(PUBLIC(this)->backUrl,this->backface, vindices);
cubedata->addChild(sep);
}
if (PUBLIC(this)->leftUrl.getNum() != 0) {
const int32_t vindices[] = {0, 1, 5, 4, -1};
SoSeparator * sep = this->createCubeFace(PUBLIC(this)->leftUrl,this->leftface, vindices);
cubedata->addChild(sep);
}
if (PUBLIC(this)->frontUrl.getNum() != 0) {
const int32_t vindices[] = {3, 2, 1, 0, -1};
SoSeparator * sep = this->createCubeFace(PUBLIC(this)->frontUrl,this->frontface, vindices);
cubedata->addChild(sep);
}
if (PUBLIC(this)->rightUrl.getNum() != 0) {
const int32_t vindices[] = {7, 6, 2, 3, -1};
SoSeparator * sep = this->createCubeFace(PUBLIC(this)->rightUrl,this->rightface, vindices);
cubedata->addChild(sep);
}
if (PUBLIC(this)->bottomUrl.getNum() != 0) {
const int32_t vindices[] = {7, 3, 0, 4, -1};
SoSeparator * sep = this->createCubeFace(PUBLIC(this)->bottomUrl,this->bottomface, vindices);
cubedata->addChild(sep);
}
if (PUBLIC(this)->topUrl.getNum() != 0) {
const int32_t vindices[] = {2, 6, 5, 1, -1};
SoSeparator * sep = this->createCubeFace(PUBLIC(this)->topUrl,this->topface, vindices);
cubedata->addChild(sep);
}
this->children->append(rootnode);
angles.truncate(0);
this->geometrybuilt = TRUE;
}
void
SoVRMLBackgroundP::buildIndexList(SoIndexedTriangleStripSet * sphere, int len, int slices, int matlength)
{
// Build vertex and normal indices for triangle strips
int matindex = 0;
int counter = 0;
int i;
for (i=0;i<slices - 1;++i) {
for (int j=0;j<len;++j) {
sphere->materialIndex.set1Value(counter,matindex);
sphere->normalIndex.set1Value(counter, ((i + 1) * len) + j);
sphere->coordIndex.set1Value(counter++, ((i + 1) * len) + j);
sphere->materialIndex.set1Value(counter, matindex);
sphere->normalIndex.set1Value(counter, (i * len) + j);
sphere->coordIndex.set1Value(counter++, (i * len) + j);
++matindex;
if (matindex >= matlength)
matindex = matlength - 1;
}
sphere->materialIndex.set1Value(counter, -1);
sphere->coordIndex.set1Value(counter, -1);
sphere->normalIndex.set1Value(counter++, -1);
matindex = 0;
}
i = slices - 1;
for (int j=0;j<len;++j) {
sphere->materialIndex.set1Value(counter, matindex);
sphere->normalIndex.set1Value(counter, j);
sphere->coordIndex.set1Value(counter++, j);
sphere->materialIndex.set1Value(counter, matindex);
sphere->normalIndex.set1Value(counter, (i * len) + j);
sphere->coordIndex.set1Value(counter++, (i * len) + j);
++matindex;
if (matindex >= matlength)
matindex = matlength - 1;
}
sphere->materialIndex.set1Value(counter, -1);
sphere->coordIndex.set1Value(counter, -1);
sphere->normalIndex.set1Value(counter++, -1);
}
SoSeparator *
SoVRMLBackgroundP::createCubeFace(SoMFString & urls, SoSeparator * sep, const int32_t * vindices)
{
const int32_t tindices[] = {1, 2, 3, 0, -1};
sep = new SoSeparator;
sep->ref();
SoVRMLImageTexture * tex = new SoVRMLImageTexture;
tex->url = urls;
tex->repeatS.setValue(FALSE);
tex->repeatT.setValue(FALSE);
SoIndexedFaceSet * faceset = new SoIndexedFaceSet;
faceset->coordIndex.setValues(0, 5, vindices);
faceset->textureCoordIndex.setValues(0, 5, tindices);
sep->addChild(tex);
sep->addChild(faceset);
return sep;
}
void
SoVRMLBackgroundP::modifyCubeFace(SoMFString & urls, SoSeparator * sep, const int32_t * vindices)
{
SoVRMLImageTexture * tex;
if (urls.getNum() == 0) {
if (sep != NULL) {
sep->unref();
sep = NULL;
}
return;
}
else if (sep == NULL) {
sep = new SoSeparator;
sep->ref();
tex = new SoVRMLImageTexture;
tex->ref();
tex->repeatS.setValue(FALSE);
tex->repeatT.setValue(FALSE);
const int32_t tindices[] = {1, 2, 3, 0, -1};
SoIndexedFaceSet * faceset = new SoIndexedFaceSet;
faceset->textureCoordIndex.setValues(0, 5, tindices);
faceset->coordIndex.setValues(0, 5, vindices);
sep->addChild(tex);
sep->addChild(faceset);
}
else {
tex = (SoVRMLImageTexture *) sep->getChild(0);
}
tex->url = urls;
}
void
background_vrmltexturechangeCB(void * data, SoSensor * sensor)
{
SoVRMLBackgroundP * pimpl = (SoVRMLBackgroundP *) data;
if (!pimpl->geometrybuilt)
pimpl->buildGeometry();
SoVRMLImageTexture * tex = new SoVRMLImageTexture;
tex->ref();
tex->repeatS.setValue(FALSE);
tex->repeatT.setValue(FALSE);
const int32_t tindices[] = {1, 2, 3, 0, -1};
SoIndexedFaceSet * faceset = new SoIndexedFaceSet;
faceset->textureCoordIndex.setValues(0, 5, tindices);
if (sensor == pimpl->backurlsensor) {
const int32_t vindices[] = {4, 5, 6, 7, -1};
pimpl->modifyCubeFace(pimpl->master->backUrl, pimpl->backface, vindices);
}
else if (sensor == pimpl->lefturlsensor) {
const int32_t vindices[] = {0, 1, 5, 4, -1};
pimpl->modifyCubeFace(pimpl->master->leftUrl, pimpl->leftface, vindices);
}
else if (sensor == pimpl->fronturlsensor) {
const int32_t vindices[] = {3, 2, 1, 0, -1};
pimpl->modifyCubeFace(pimpl->master->frontUrl, pimpl->frontface, vindices);
}
else if (sensor == pimpl->righturlsensor) {
const int32_t vindices[] = {7, 6, 2, 3, -1};
pimpl->modifyCubeFace(pimpl->master->rightUrl, pimpl->rightface, vindices);
}
else if (sensor == pimpl->bottomurlsensor) {
const int32_t vindices[] = {7, 3, 0, 4, -1};
pimpl->modifyCubeFace(pimpl->master->bottomUrl, pimpl->bottomface, vindices);
}
else if (sensor == pimpl->topurlsensor) {
const int32_t vindices[] = {2, 6, 5, 1, -1};
pimpl->modifyCubeFace(pimpl->master->topUrl, pimpl->topface, vindices);
}
}
void
background_geometrychangeCB(void * data, SoSensor * sensor)
{
SoVRMLBackgroundP * pimpl = (SoVRMLBackgroundP *) data;
if (pimpl->rootnode) {
pimpl->rootnode->removeAllChildren();
pimpl->rootnode->unref();
pimpl->rootnode = NULL;
}
pimpl->buildGeometry();
}
void
background_bindingchangeCB(void * data, SoSensor * sensor)
{
SoVRMLBackgroundP * pimpl = (SoVRMLBackgroundP *) data;
// FIXME: Support for 'set_bind' and 'isBound' must be implemented.
// But first, a Coin viewer must support this kind of special node
// treatment (this applies to 'Fog', 'NavigationInfo' and 'Viewport'
// nodes aswell) (11Aug2003 handegar)
if (sensor == pimpl->setbindsensor) {
SoDebugError::postWarning("background_bindingchangeCB", "'set_bind' event not implemented yet");
}
else if (sensor == pimpl->isboundsensor) {
SoDebugError::postWarning("background_bindingchangeCB", "'isBound' event not implemented yet");
}
}
#undef PRIVATE
#undef PUBLIC
#endif // HAVE_VRML97
|