| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osg/Node> |
|---|
| 20 | #include <osg/Geometry> |
|---|
| 21 | #include <osg/Notify> |
|---|
| 22 | #include <osg/Texture3D> |
|---|
| 23 | #include <osg/Texture1D> |
|---|
| 24 | #include <osg/ImageSequence> |
|---|
| 25 | #include <osg/TexGen> |
|---|
| 26 | #include <osg/Geode> |
|---|
| 27 | #include <osg/Billboard> |
|---|
| 28 | #include <osg/PositionAttitudeTransform> |
|---|
| 29 | #include <osg/ClipNode> |
|---|
| 30 | #include <osg/AlphaFunc> |
|---|
| 31 | #include <osg/TexGenNode> |
|---|
| 32 | #include <osg/TexEnv> |
|---|
| 33 | #include <osg/TexEnvCombine> |
|---|
| 34 | #include <osg/Material> |
|---|
| 35 | #include <osg/PrimitiveSet> |
|---|
| 36 | #include <osg/Endian> |
|---|
| 37 | #include <osg/BlendFunc> |
|---|
| 38 | #include <osg/BlendEquation> |
|---|
| 39 | #include <osg/TransferFunction> |
|---|
| 40 | #include <osg/MatrixTransform> |
|---|
| 41 | |
|---|
| 42 | #include <osgDB/Registry> |
|---|
| 43 | #include <osgDB/ReadFile> |
|---|
| 44 | #include <osgDB/WriteFile> |
|---|
| 45 | #include <osgDB/FileUtils> |
|---|
| 46 | #include <osgDB/FileNameUtils> |
|---|
| 47 | |
|---|
| 48 | #include <osgGA/EventVisitor> |
|---|
| 49 | #include <osgGA/TrackballManipulator> |
|---|
| 50 | #include <osgGA/FlightManipulator> |
|---|
| 51 | #include <osgGA/KeySwitchMatrixManipulator> |
|---|
| 52 | |
|---|
| 53 | #include <osgUtil/CullVisitor> |
|---|
| 54 | |
|---|
| 55 | #include <osgViewer/Viewer> |
|---|
| 56 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 57 | |
|---|
| 58 | #include <osg/io_utils> |
|---|
| 59 | |
|---|
| 60 | #include <algorithm> |
|---|
| 61 | #include <iostream> |
|---|
| 62 | |
|---|
| 63 | #include <osg/ImageUtils> |
|---|
| 64 | #include <osgVolume/Volume> |
|---|
| 65 | #include <osgVolume/VolumeTile> |
|---|
| 66 | #include <osgVolume/ShaderTechnique> |
|---|
| 67 | #include <osgVolume/FixedFunctionTechnique> |
|---|
| 68 | |
|---|
| 69 | typedef std::vector< osg::ref_ptr<osg::Image> > ImageList; |
|---|
| 70 | |
|---|
| 71 | enum ShadingModel |
|---|
| 72 | { |
|---|
| 73 | Standard, |
|---|
| 74 | Light, |
|---|
| 75 | Isosurface, |
|---|
| 76 | MaximumIntensityProjection |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | struct PassThroughTransformFunction |
|---|
| 92 | { |
|---|
| 93 | unsigned char operator() (unsigned char c) const { return c; } |
|---|
| 94 | }; |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | struct ProcessRow |
|---|
| 98 | { |
|---|
| 99 | virtual ~ProcessRow() {} |
|---|
| 100 | |
|---|
| 101 | virtual void operator() (unsigned int num, |
|---|
| 102 | GLenum source_pixelFormat, unsigned char* source, |
|---|
| 103 | GLenum dest_pixelFormat, unsigned char* dest) const |
|---|
| 104 | { |
|---|
| 105 | switch(source_pixelFormat) |
|---|
| 106 | { |
|---|
| 107 | case(GL_LUMINANCE): |
|---|
| 108 | case(GL_ALPHA): |
|---|
| 109 | switch(dest_pixelFormat) |
|---|
| 110 | { |
|---|
| 111 | case(GL_LUMINANCE): |
|---|
| 112 | case(GL_ALPHA): A_to_A(num, source, dest); break; |
|---|
| 113 | case(GL_LUMINANCE_ALPHA): A_to_LA(num, source, dest); break; |
|---|
| 114 | case(GL_RGB): A_to_RGB(num, source, dest); break; |
|---|
| 115 | case(GL_RGBA): A_to_RGBA(num, source, dest); break; |
|---|
| 116 | } |
|---|
| 117 | break; |
|---|
| 118 | case(GL_LUMINANCE_ALPHA): |
|---|
| 119 | switch(dest_pixelFormat) |
|---|
| 120 | { |
|---|
| 121 | case(GL_LUMINANCE): |
|---|
| 122 | case(GL_ALPHA): LA_to_A(num, source, dest); break; |
|---|
| 123 | case(GL_LUMINANCE_ALPHA): LA_to_LA(num, source, dest); break; |
|---|
| 124 | case(GL_RGB): LA_to_RGB(num, source, dest); break; |
|---|
| 125 | case(GL_RGBA): LA_to_RGBA(num, source, dest); break; |
|---|
| 126 | } |
|---|
| 127 | break; |
|---|
| 128 | case(GL_RGB): |
|---|
| 129 | switch(dest_pixelFormat) |
|---|
| 130 | { |
|---|
| 131 | case(GL_LUMINANCE): |
|---|
| 132 | case(GL_ALPHA): RGB_to_A(num, source, dest); break; |
|---|
| 133 | case(GL_LUMINANCE_ALPHA): RGB_to_LA(num, source, dest); break; |
|---|
| 134 | case(GL_RGB): RGB_to_RGB(num, source, dest); break; |
|---|
| 135 | case(GL_RGBA): RGB_to_RGBA(num, source, dest); break; |
|---|
| 136 | } |
|---|
| 137 | break; |
|---|
| 138 | case(GL_RGBA): |
|---|
| 139 | switch(dest_pixelFormat) |
|---|
| 140 | { |
|---|
| 141 | case(GL_LUMINANCE): |
|---|
| 142 | case(GL_ALPHA): RGBA_to_A(num, source, dest); break; |
|---|
| 143 | case(GL_LUMINANCE_ALPHA): RGBA_to_LA(num, source, dest); break; |
|---|
| 144 | case(GL_RGB): RGBA_to_RGB(num, source, dest); break; |
|---|
| 145 | case(GL_RGBA): RGBA_to_RGBA(num, source, dest); break; |
|---|
| 146 | } |
|---|
| 147 | break; |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | virtual void A_to_A(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 154 | { |
|---|
| 155 | for(unsigned int i=0;i<num;++i) |
|---|
| 156 | { |
|---|
| 157 | *dest++ = *source++; |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | virtual void A_to_LA(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 162 | { |
|---|
| 163 | for(unsigned int i=0;i<num;++i) |
|---|
| 164 | { |
|---|
| 165 | *dest++ = *source; |
|---|
| 166 | *dest++ = *source++; |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | virtual void A_to_RGB(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 171 | { |
|---|
| 172 | for(unsigned int i=0;i<num;++i) |
|---|
| 173 | { |
|---|
| 174 | *dest++ = *source; |
|---|
| 175 | *dest++ = *source; |
|---|
| 176 | *dest++ = *source++; |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | virtual void A_to_RGBA(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 181 | { |
|---|
| 182 | for(unsigned int i=0;i<num;++i) |
|---|
| 183 | { |
|---|
| 184 | *dest++ = *source; |
|---|
| 185 | *dest++ = *source; |
|---|
| 186 | *dest++ = *source; |
|---|
| 187 | *dest++ = *source++; |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | virtual void LA_to_A(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 194 | { |
|---|
| 195 | for(unsigned int i=0;i<num;++i) |
|---|
| 196 | { |
|---|
| 197 | ++source; |
|---|
| 198 | *dest++ = *source++; |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | virtual void LA_to_LA(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 203 | { |
|---|
| 204 | for(unsigned int i=0;i<num;++i) |
|---|
| 205 | { |
|---|
| 206 | *dest++ = *source++; |
|---|
| 207 | *dest++ = *source++; |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | virtual void LA_to_RGB(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 212 | { |
|---|
| 213 | for(unsigned int i=0;i<num;++i) |
|---|
| 214 | { |
|---|
| 215 | *dest++ = *source; |
|---|
| 216 | *dest++ = *source; |
|---|
| 217 | *dest++ = *source; |
|---|
| 218 | source+=2; |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | virtual void LA_to_RGBA(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 223 | { |
|---|
| 224 | for(unsigned int i=0;i<num;++i) |
|---|
| 225 | { |
|---|
| 226 | *dest++ = *source; |
|---|
| 227 | *dest++ = *source; |
|---|
| 228 | *dest++ = *source++; |
|---|
| 229 | *dest++ = *source++; |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | virtual void RGB_to_A(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 236 | { |
|---|
| 237 | for(unsigned int i=0;i<num;++i) |
|---|
| 238 | { |
|---|
| 239 | unsigned char val = *source; |
|---|
| 240 | *dest++ = val; |
|---|
| 241 | source += 3; |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | virtual void RGB_to_LA(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 246 | { |
|---|
| 247 | for(unsigned int i=0;i<num;++i) |
|---|
| 248 | { |
|---|
| 249 | unsigned char val = *source; |
|---|
| 250 | *dest++ = val; |
|---|
| 251 | *dest++ = val; |
|---|
| 252 | source += 3; |
|---|
| 253 | } |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | virtual void RGB_to_RGB(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 257 | { |
|---|
| 258 | for(unsigned int i=0;i<num;++i) |
|---|
| 259 | { |
|---|
| 260 | *dest++ = *source++; |
|---|
| 261 | *dest++ = *source++; |
|---|
| 262 | *dest++ = *source++; |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | virtual void RGB_to_RGBA(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 267 | { |
|---|
| 268 | for(unsigned int i=0;i<num;++i) |
|---|
| 269 | { |
|---|
| 270 | unsigned char val = *source; |
|---|
| 271 | *dest++ = *source++; |
|---|
| 272 | *dest++ = *source++; |
|---|
| 273 | *dest++ = *source++; |
|---|
| 274 | *dest++ = val; |
|---|
| 275 | } |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | virtual void RGBA_to_A(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 281 | { |
|---|
| 282 | for(unsigned int i=0;i<num;++i) |
|---|
| 283 | { |
|---|
| 284 | source += 3; |
|---|
| 285 | *dest++ = *source++; |
|---|
| 286 | } |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | virtual void RGBA_to_LA(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 290 | { |
|---|
| 291 | for(unsigned int i=0;i<num;++i) |
|---|
| 292 | { |
|---|
| 293 | unsigned char val = *source; |
|---|
| 294 | source += 3; |
|---|
| 295 | *dest++ = val; |
|---|
| 296 | *dest++ = *source++; |
|---|
| 297 | } |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | virtual void RGBA_to_RGB(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 301 | { |
|---|
| 302 | for(unsigned int i=0;i<num;++i) |
|---|
| 303 | { |
|---|
| 304 | *dest++ = *source++; |
|---|
| 305 | *dest++ = *source++; |
|---|
| 306 | *dest++ = *source++; |
|---|
| 307 | ++source; |
|---|
| 308 | } |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | virtual void RGBA_to_RGBA(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 312 | { |
|---|
| 313 | for(unsigned int i=0;i<num;++i) |
|---|
| 314 | { |
|---|
| 315 | *dest++ = *source++; |
|---|
| 316 | *dest++ = *source++; |
|---|
| 317 | *dest++ = *source++; |
|---|
| 318 | *dest++ = *source++; |
|---|
| 319 | } |
|---|
| 320 | } |
|---|
| 321 | }; |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | void clampToNearestValidPowerOfTwo(int& sizeX, int& sizeY, int& sizeZ, int s_maximumTextureSize, int t_maximumTextureSize, int r_maximumTextureSize) |
|---|
| 325 | { |
|---|
| 326 | |
|---|
| 327 | int s_nearestPowerOfTwo = 1; |
|---|
| 328 | while(s_nearestPowerOfTwo<sizeX && s_nearestPowerOfTwo<s_maximumTextureSize) s_nearestPowerOfTwo*=2; |
|---|
| 329 | |
|---|
| 330 | int t_nearestPowerOfTwo = 1; |
|---|
| 331 | while(t_nearestPowerOfTwo<sizeY && t_nearestPowerOfTwo<t_maximumTextureSize) t_nearestPowerOfTwo*=2; |
|---|
| 332 | |
|---|
| 333 | int r_nearestPowerOfTwo = 1; |
|---|
| 334 | while(r_nearestPowerOfTwo<sizeZ && r_nearestPowerOfTwo<r_maximumTextureSize) r_nearestPowerOfTwo*=2; |
|---|
| 335 | |
|---|
| 336 | sizeX = s_nearestPowerOfTwo; |
|---|
| 337 | sizeY = t_nearestPowerOfTwo; |
|---|
| 338 | sizeZ = r_nearestPowerOfTwo; |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | osg::Image* createTexture3D(ImageList& imageList, ProcessRow& processRow, |
|---|
| 342 | unsigned int numComponentsDesired, |
|---|
| 343 | int s_maximumTextureSize, |
|---|
| 344 | int t_maximumTextureSize, |
|---|
| 345 | int r_maximumTextureSize, |
|---|
| 346 | bool resizeToPowerOfTwo) |
|---|
| 347 | { |
|---|
| 348 | int max_s = 0; |
|---|
| 349 | int max_t = 0; |
|---|
| 350 | unsigned int max_components = 0; |
|---|
| 351 | int total_r = 0; |
|---|
| 352 | ImageList::iterator itr; |
|---|
| 353 | for(itr=imageList.begin(); |
|---|
| 354 | itr!=imageList.end(); |
|---|
| 355 | ++itr) |
|---|
| 356 | { |
|---|
| 357 | osg::Image* image = itr->get(); |
|---|
| 358 | GLenum pixelFormat = image->getPixelFormat(); |
|---|
| 359 | if (pixelFormat==GL_ALPHA || |
|---|
| 360 | pixelFormat==GL_INTENSITY || |
|---|
| 361 | pixelFormat==GL_LUMINANCE || |
|---|
| 362 | pixelFormat==GL_LUMINANCE_ALPHA || |
|---|
| 363 | pixelFormat==GL_RGB || |
|---|
| 364 | pixelFormat==GL_RGBA) |
|---|
| 365 | { |
|---|
| 366 | max_s = osg::maximum(image->s(), max_s); |
|---|
| 367 | max_t = osg::maximum(image->t(), max_t); |
|---|
| 368 | max_components = osg::maximum(osg::Image::computeNumComponents(pixelFormat), max_components); |
|---|
| 369 | total_r += image->r(); |
|---|
| 370 | } |
|---|
| 371 | else |
|---|
| 372 | { |
|---|
| 373 | osg::notify(osg::NOTICE)<<"Image "<<image->getFileName()<<" has unsuitable pixel format"<< std::hex<< pixelFormat << std::dec << std::endl; |
|---|
| 374 | } |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | if (numComponentsDesired!=0) max_components = numComponentsDesired; |
|---|
| 378 | |
|---|
| 379 | GLenum desiredPixelFormat = 0; |
|---|
| 380 | switch(max_components) |
|---|
| 381 | { |
|---|
| 382 | case(1): |
|---|
| 383 | osg::notify(osg::NOTICE)<<"desiredPixelFormat = GL_LUMINANCE" << std::endl; |
|---|
| 384 | desiredPixelFormat = GL_LUMINANCE; |
|---|
| 385 | break; |
|---|
| 386 | case(2): |
|---|
| 387 | osg::notify(osg::NOTICE)<<"desiredPixelFormat = GL_LUMINANCE_ALPHA" << std::endl; |
|---|
| 388 | desiredPixelFormat = GL_LUMINANCE_ALPHA; |
|---|
| 389 | break; |
|---|
| 390 | case(3): |
|---|
| 391 | osg::notify(osg::NOTICE)<<"desiredPixelFormat = GL_RGB" << std::endl; |
|---|
| 392 | desiredPixelFormat = GL_RGB; |
|---|
| 393 | break; |
|---|
| 394 | case(4): |
|---|
| 395 | osg::notify(osg::NOTICE)<<"desiredPixelFormat = GL_RGBA" << std::endl; |
|---|
| 396 | desiredPixelFormat = GL_RGBA; |
|---|
| 397 | break; |
|---|
| 398 | } |
|---|
| 399 | if (desiredPixelFormat==0) return 0; |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | int s_nearestPowerOfTwo = 1; |
|---|
| 404 | int t_nearestPowerOfTwo = 1; |
|---|
| 405 | int r_nearestPowerOfTwo = 1; |
|---|
| 406 | |
|---|
| 407 | if (resizeToPowerOfTwo) |
|---|
| 408 | { |
|---|
| 409 | while(s_nearestPowerOfTwo<max_s && s_nearestPowerOfTwo<s_maximumTextureSize) s_nearestPowerOfTwo*=2; |
|---|
| 410 | while(t_nearestPowerOfTwo<max_t && t_nearestPowerOfTwo<t_maximumTextureSize) t_nearestPowerOfTwo*=2; |
|---|
| 411 | while(r_nearestPowerOfTwo<total_r && r_nearestPowerOfTwo<r_maximumTextureSize) r_nearestPowerOfTwo*=2; |
|---|
| 412 | |
|---|
| 413 | osg::notify(osg::NOTICE)<<"max image width = "<<max_s<<" nearest power of two = "<<s_nearestPowerOfTwo<<std::endl; |
|---|
| 414 | osg::notify(osg::NOTICE)<<"max image height = "<<max_t<<" nearest power of two = "<<t_nearestPowerOfTwo<<std::endl; |
|---|
| 415 | osg::notify(osg::NOTICE)<<"max image depth = "<<total_r<<" nearest power of two = "<<r_nearestPowerOfTwo<<std::endl; |
|---|
| 416 | } |
|---|
| 417 | else |
|---|
| 418 | { |
|---|
| 419 | s_nearestPowerOfTwo = max_s; |
|---|
| 420 | t_nearestPowerOfTwo = max_t; |
|---|
| 421 | r_nearestPowerOfTwo = total_r; |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | osg::ref_ptr<osg::Image> image_3d = new osg::Image; |
|---|
| 426 | image_3d->allocateImage(s_nearestPowerOfTwo,t_nearestPowerOfTwo,r_nearestPowerOfTwo, |
|---|
| 427 | desiredPixelFormat,GL_UNSIGNED_BYTE); |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | unsigned int r_offset = (total_r<r_nearestPowerOfTwo) ? r_nearestPowerOfTwo/2 - total_r/2 : 0; |
|---|
| 431 | |
|---|
| 432 | int curr_dest_r = r_offset; |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | for(itr=imageList.begin(); |
|---|
| 436 | itr!=imageList.end(); |
|---|
| 437 | ++itr) |
|---|
| 438 | { |
|---|
| 439 | osg::Image* image = itr->get(); |
|---|
| 440 | GLenum pixelFormat = image->getPixelFormat(); |
|---|
| 441 | if (pixelFormat==GL_ALPHA || |
|---|
| 442 | pixelFormat==GL_LUMINANCE || |
|---|
| 443 | pixelFormat==GL_INTENSITY || |
|---|
| 444 | pixelFormat==GL_LUMINANCE_ALPHA || |
|---|
| 445 | pixelFormat==GL_RGB || |
|---|
| 446 | pixelFormat==GL_RGBA) |
|---|
| 447 | { |
|---|
| 448 | |
|---|
| 449 | int num_r = osg::minimum(image->r(), (image_3d->r() - curr_dest_r)); |
|---|
| 450 | int num_t = osg::minimum(image->t(), image_3d->t()); |
|---|
| 451 | int num_s = osg::minimum(image->s(), image_3d->s()); |
|---|
| 452 | |
|---|
| 453 | unsigned int s_offset_dest = (image->s()<s_nearestPowerOfTwo) ? s_nearestPowerOfTwo/2 - image->s()/2 : 0; |
|---|
| 454 | unsigned int t_offset_dest = (image->t()<t_nearestPowerOfTwo) ? t_nearestPowerOfTwo/2 - image->t()/2 : 0; |
|---|
| 455 | |
|---|
| 456 | for(int r=0;r<num_r;++r, ++curr_dest_r) |
|---|
| 457 | { |
|---|
| 458 | for(int t=0;t<num_t;++t) |
|---|
| 459 | { |
|---|
| 460 | unsigned char* dest = image_3d->data(s_offset_dest,t+t_offset_dest,curr_dest_r); |
|---|
| 461 | unsigned char* source = image->data(0,t,r); |
|---|
| 462 | |
|---|
| 463 | processRow(num_s, image->getPixelFormat(), source, image_3d->getPixelFormat(), dest); |
|---|
| 464 | } |
|---|
| 465 | } |
|---|
| 466 | } |
|---|
| 467 | } |
|---|
| 468 | return image_3d.release(); |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | osg::Image* createNormalMapTexture(osg::Image* image_3d) |
|---|
| 473 | { |
|---|
| 474 | osg::notify(osg::NOTICE)<<"Computing NormalMapTexture"<<std::endl; |
|---|
| 475 | |
|---|
| 476 | GLenum dataType = image_3d->getDataType(); |
|---|
| 477 | |
|---|
| 478 | unsigned int sourcePixelIncrement = 1; |
|---|
| 479 | unsigned int alphaOffset = 0; |
|---|
| 480 | switch(image_3d->getPixelFormat()) |
|---|
| 481 | { |
|---|
| 482 | case(GL_ALPHA): |
|---|
| 483 | case(GL_LUMINANCE): |
|---|
| 484 | sourcePixelIncrement = 1; |
|---|
| 485 | alphaOffset = 0; |
|---|
| 486 | break; |
|---|
| 487 | case(GL_LUMINANCE_ALPHA): |
|---|
| 488 | sourcePixelIncrement = 2; |
|---|
| 489 | alphaOffset = 1; |
|---|
| 490 | break; |
|---|
| 491 | case(GL_RGB): |
|---|
| 492 | sourcePixelIncrement = 3; |
|---|
| 493 | alphaOffset = 0; |
|---|
| 494 | break; |
|---|
| 495 | case(GL_RGBA): |
|---|
| 496 | sourcePixelIncrement = 4; |
|---|
| 497 | alphaOffset = 3; |
|---|
| 498 | break; |
|---|
| 499 | default: |
|---|
| 500 | osg::notify(osg::NOTICE)<<"Source pixel format not support for normal map generation."<<std::endl; |
|---|
| 501 | return 0; |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | |
|---|
| 505 | osg::ref_ptr<osg::Image> normalmap_3d = new osg::Image; |
|---|
| 506 | normalmap_3d->allocateImage(image_3d->s(),image_3d->t(),image_3d->r(), |
|---|
| 507 | GL_RGBA,GL_UNSIGNED_BYTE); |
|---|
| 508 | |
|---|
| 509 | if (osg::getCpuByteOrder()==osg::LittleEndian) alphaOffset = sourcePixelIncrement-alphaOffset-1; |
|---|
| 510 | |
|---|
| 511 | for(int r=1;r<image_3d->r()-1;++r) |
|---|
| 512 | { |
|---|
| 513 | for(int t=1;t<image_3d->t()-1;++t) |
|---|
| 514 | { |
|---|
| 515 | |
|---|
| 516 | if (dataType==GL_UNSIGNED_BYTE) |
|---|
| 517 | { |
|---|
| 518 | unsigned char* ptr = image_3d->data(1,t,r)+alphaOffset; |
|---|
| 519 | unsigned char* left = image_3d->data(0,t,r)+alphaOffset; |
|---|
| 520 | unsigned char* right = image_3d->data(2,t,r)+alphaOffset; |
|---|
| 521 | unsigned char* above = image_3d->data(1,t+1,r)+alphaOffset; |
|---|
| 522 | unsigned char* below = image_3d->data(1,t-1,r)+alphaOffset; |
|---|
| 523 | unsigned char* in = image_3d->data(1,t,r+1)+alphaOffset; |
|---|
| 524 | unsigned char* out = image_3d->data(1,t,r-1)+alphaOffset; |
|---|
| 525 | |
|---|
| 526 | unsigned char* destination = (unsigned char*) normalmap_3d->data(1,t,r); |
|---|
| 527 | |
|---|
| 528 | for(int s=1;s<image_3d->s()-1;++s) |
|---|
| 529 | { |
|---|
| 530 | |
|---|
| 531 | osg::Vec3 grad((float)(*left)-(float)(*right), |
|---|
| 532 | (float)(*below)-(float)(*above), |
|---|
| 533 | (float)(*out) -(float)(*in)); |
|---|
| 534 | |
|---|
| 535 | grad.normalize(); |
|---|
| 536 | |
|---|
| 537 | if (grad.x()==0.0f && grad.y()==0.0f && grad.z()==0.0f) |
|---|
| 538 | { |
|---|
| 539 | grad.set(128.0f,128.0f,128.0f); |
|---|
| 540 | } |
|---|
| 541 | else |
|---|
| 542 | { |
|---|
| 543 | grad.x() = osg::clampBetween((grad.x()+1.0f)*128.0f,0.0f,255.0f); |
|---|
| 544 | grad.y() = osg::clampBetween((grad.y()+1.0f)*128.0f,0.0f,255.0f); |
|---|
| 545 | grad.z() = osg::clampBetween((grad.z()+1.0f)*128.0f,0.0f,255.0f); |
|---|
| 546 | } |
|---|
| 547 | |
|---|
| 548 | *(destination++) = (unsigned char)(grad.x()); |
|---|
| 549 | *(destination++) = (unsigned char)(grad.y()); |
|---|
| 550 | *(destination++) = (unsigned char)(grad.z()); |
|---|
| 551 | |
|---|
| 552 | *destination++ = *ptr; |
|---|
| 553 | |
|---|
| 554 | ptr += sourcePixelIncrement; |
|---|
| 555 | left += sourcePixelIncrement; |
|---|
| 556 | right += sourcePixelIncrement; |
|---|
| 557 | above += sourcePixelIncrement; |
|---|
| 558 | below += sourcePixelIncrement; |
|---|
| 559 | in += sourcePixelIncrement; |
|---|
| 560 | out += sourcePixelIncrement; |
|---|
| 561 | } |
|---|
| 562 | } |
|---|
| 563 | else if (dataType==GL_SHORT) |
|---|
| 564 | { |
|---|
| 565 | short* ptr = (short*)(image_3d->data(1,t,r)+alphaOffset); |
|---|
| 566 | short* left = (short*)(image_3d->data(0,t,r)+alphaOffset); |
|---|
| 567 | short* right = (short*)(image_3d->data(2,t,r)+alphaOffset); |
|---|
| 568 | short* above = (short*)(image_3d->data(1,t+1,r)+alphaOffset); |
|---|
| 569 | short* below = (short*)(image_3d->data(1,t-1,r)+alphaOffset); |
|---|
| 570 | short* in = (short*)(image_3d->data(1,t,r+1)+alphaOffset); |
|---|
| 571 | short* out = (short*)(image_3d->data(1,t,r-1)+alphaOffset); |
|---|
| 572 | |
|---|
| 573 | unsigned char* destination = (unsigned char*) normalmap_3d->data(1,t,r); |
|---|
| 574 | |
|---|
| 575 | for(int s=1;s<image_3d->s()-1;++s) |
|---|
| 576 | { |
|---|
| 577 | |
|---|
| 578 | osg::Vec3 grad((float)(*left)-(float)(*right), |
|---|
| 579 | (float)(*below)-(float)(*above), |
|---|
| 580 | (float)(*out) -(float)(*in)); |
|---|
| 581 | |
|---|
| 582 | grad.normalize(); |
|---|
| 583 | |
|---|
| 584 | |
|---|
| 585 | |
|---|
| 586 | if (grad.x()==0.0f && grad.y()==0.0f && grad.z()==0.0f) |
|---|
| 587 | { |
|---|
| 588 | grad.set(128.0f,128.0f,128.0f); |
|---|
| 589 | } |
|---|
| 590 | else |
|---|
| 591 | { |
|---|
| 592 | grad.x() = osg::clampBetween((grad.x()+1.0f)*128.0f,0.0f,255.0f); |
|---|
| 593 | grad.y() = osg::clampBetween((grad.y()+1.0f)*128.0f,0.0f,255.0f); |
|---|
| 594 | grad.z() = osg::clampBetween((grad.z()+1.0f)*128.0f,0.0f,255.0f); |
|---|
| 595 | } |
|---|
| 596 | |
|---|
| 597 | |
|---|
| 598 | *(destination++) = (unsigned char)(grad.x()); |
|---|
| 599 | *(destination++) = (unsigned char)(grad.y()); |
|---|
| 600 | *(destination++) = (unsigned char)(grad.z()); |
|---|
| 601 | |
|---|
| 602 | *destination++ = *ptr/128; |
|---|
| 603 | |
|---|
| 604 | ptr += sourcePixelIncrement; |
|---|
| 605 | left += sourcePixelIncrement; |
|---|
| 606 | right += sourcePixelIncrement; |
|---|
| 607 | above += sourcePixelIncrement; |
|---|
| 608 | below += sourcePixelIncrement; |
|---|
| 609 | in += sourcePixelIncrement; |
|---|
| 610 | out += sourcePixelIncrement; |
|---|
| 611 | } |
|---|
| 612 | } |
|---|
| 613 | else if (dataType==GL_UNSIGNED_SHORT) |
|---|
| 614 | { |
|---|
| 615 | unsigned short* ptr = (unsigned short*)(image_3d->data(1,t,r)+alphaOffset); |
|---|
| 616 | unsigned short* left = (unsigned short*)(image_3d->data(0,t,r)+alphaOffset); |
|---|
| 617 | unsigned short* right = (unsigned short*)(image_3d->data(2,t,r)+alphaOffset); |
|---|
| 618 | unsigned short* above = (unsigned short*)(image_3d->data(1,t+1,r)+alphaOffset); |
|---|
| 619 | unsigned short* below = (unsigned short*)(image_3d->data(1,t-1,r)+alphaOffset); |
|---|
| 620 | unsigned short* in = (unsigned short*)(image_3d->data(1,t,r+1)+alphaOffset); |
|---|
| 621 | unsigned short* out = (unsigned short*)(image_3d->data(1,t,r-1)+alphaOffset); |
|---|
| 622 | |
|---|
| 623 | unsigned char* destination = (unsigned char*) normalmap_3d->data(1,t,r); |
|---|
| 624 | |
|---|
| 625 | for(int s=1;s<image_3d->s()-1;++s) |
|---|
| 626 | { |
|---|
| 627 | |
|---|
| 628 | osg::Vec3 grad((float)(*left)-(float)(*right), |
|---|
| 629 | (float)(*below)-(float)(*above), |
|---|
| 630 | (float)(*out) -(float)(*in)); |
|---|
| 631 | |
|---|
| 632 | grad.normalize(); |
|---|
| 633 | |
|---|
| 634 | if (grad.x()==0.0f && grad.y()==0.0f && grad.z()==0.0f) |
|---|
| 635 | { |
|---|
| 636 | grad.set(128.0f,128.0f,128.0f); |
|---|
| 637 | } |
|---|
| 638 | else |
|---|
| 639 | { |
|---|
| 640 | grad.x() = osg::clampBetween((grad.x()+1.0f)*128.0f,0.0f,255.0f); |
|---|
| 641 | grad.y() = osg::clampBetween((grad.y()+1.0f)*128.0f,0.0f,255.0f); |
|---|
| 642 | grad.z() = osg::clampBetween((grad.z()+1.0f)*128.0f,0.0f,255.0f); |
|---|
| 643 | } |
|---|
| 644 | |
|---|
| 645 | *(destination++) = (unsigned char)(grad.x()); |
|---|
| 646 | *(destination++) = (unsigned char)(grad.y()); |
|---|
| 647 | *(destination++) = (unsigned char)(grad.z()); |
|---|
| 648 | |
|---|
| 649 | *destination++ = *ptr/256; |
|---|
| 650 | |
|---|
| 651 | ptr += sourcePixelIncrement; |
|---|
| 652 | left += sourcePixelIncrement; |
|---|
| 653 | right += sourcePixelIncrement; |
|---|
| 654 | above += sourcePixelIncrement; |
|---|
| 655 | below += sourcePixelIncrement; |
|---|
| 656 | in += sourcePixelIncrement; |
|---|
| 657 | out += sourcePixelIncrement; |
|---|
| 658 | } |
|---|
| 659 | } |
|---|
| 660 | } |
|---|
| 661 | } |
|---|
| 662 | |
|---|
| 663 | |
|---|
| 664 | osg::notify(osg::NOTICE)<<"Created NormalMapTexture"<<std::endl; |
|---|
| 665 | |
|---|
| 666 | return normalmap_3d.release(); |
|---|
| 667 | } |
|---|
| 668 | |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | osg::Node* createCube(float size,float alpha, unsigned int numSlices, float sliceEnd=1.0f) |
|---|
| 672 | { |
|---|
| 673 | |
|---|
| 674 | |
|---|
| 675 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 676 | |
|---|
| 677 | float halfSize = size*0.5f; |
|---|
| 678 | float y = halfSize; |
|---|
| 679 | float dy =-size/(float)(numSlices-1)*sliceEnd; |
|---|
| 680 | |
|---|
| 681 | |
|---|
| 682 | |
|---|
| 683 | |
|---|
| 684 | osg::Vec3Array* coords = new osg::Vec3Array(4*numSlices); |
|---|
| 685 | geom->setVertexArray(coords); |
|---|
| 686 | for(unsigned int i=0;i<numSlices;++i, y+=dy) |
|---|
| 687 | { |
|---|
| 688 | (*coords)[i*4+0].set(-halfSize,y,halfSize); |
|---|
| 689 | (*coords)[i*4+1].set(-halfSize,y,-halfSize); |
|---|
| 690 | (*coords)[i*4+2].set(halfSize,y,-halfSize); |
|---|
| 691 | (*coords)[i*4+3].set(halfSize,y,halfSize); |
|---|
| 692 | } |
|---|
| 693 | |
|---|
| 694 | osg::Vec3Array* normals = new osg::Vec3Array(1); |
|---|
| 695 | (*normals)[0].set(0.0f,-1.0f,0.0f); |
|---|
| 696 | geom->setNormalArray(normals); |
|---|
| 697 | geom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 698 | |
|---|
| 699 | osg::Vec4Array* colors = new osg::Vec4Array(1); |
|---|
| 700 | (*colors)[0].set(1.0f,1.0f,1.0f,alpha); |
|---|
| 701 | geom->setColorArray(colors); |
|---|
| 702 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 703 | |
|---|
| 704 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,coords->size())); |
|---|
| 705 | |
|---|
| 706 | osg::Billboard* billboard = new osg::Billboard; |
|---|
| 707 | billboard->setMode(osg::Billboard::POINT_ROT_WORLD); |
|---|
| 708 | billboard->addDrawable(geom); |
|---|
| 709 | billboard->setPosition(0,osg::Vec3(0.0f,0.0f,0.0f)); |
|---|
| 710 | |
|---|
| 711 | return billboard; |
|---|
| 712 | } |
|---|
| 713 | |
|---|
| 714 | class FollowMouseCallback : public osgGA::GUIEventHandler, public osg::StateSet::Callback |
|---|
| 715 | { |
|---|
| 716 | public: |
|---|
| 717 | |
|---|
| 718 | FollowMouseCallback(bool shader = false): |
|---|
| 719 | _shader(shader) |
|---|
| 720 | { |
|---|
| 721 | _updateTransparency = false; |
|---|
| 722 | _updateAlphaCutOff = false; |
|---|
| 723 | _updateSampleDensity = false; |
|---|
| 724 | } |
|---|
| 725 | |
|---|
| 726 | FollowMouseCallback(const FollowMouseCallback&,const osg::CopyOp&) {} |
|---|
| 727 | |
|---|
| 728 | META_Object(osg,FollowMouseCallback); |
|---|
| 729 | |
|---|
| 730 | virtual void operator() (osg::StateSet* stateset, osg::NodeVisitor* nv) |
|---|
| 731 | { |
|---|
| 732 | if (nv->getVisitorType()==osg::NodeVisitor::EVENT_VISITOR) |
|---|
| 733 | { |
|---|
| 734 | osgGA::EventVisitor* ev = dynamic_cast<osgGA::EventVisitor*>(nv); |
|---|
| 735 | if (ev) |
|---|
| 736 | { |
|---|
| 737 | osgGA::GUIActionAdapter* aa = ev->getActionAdapter(); |
|---|
| 738 | osgGA::EventQueue::Events& events = ev->getEvents(); |
|---|
| 739 | for(osgGA::EventQueue::Events::iterator itr=events.begin(); |
|---|
| 740 | itr!=events.end(); |
|---|
| 741 | ++itr) |
|---|
| 742 | { |
|---|
| 743 | handle(*(*itr), *aa, stateset, ev); |
|---|
| 744 | } |
|---|
| 745 | } |
|---|
| 746 | } |
|---|
| 747 | } |
|---|
| 748 | |
|---|
| 749 | virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&, osg::Object* object, osg::NodeVisitor*) |
|---|
| 750 | { |
|---|
| 751 | osg::StateSet* stateset = dynamic_cast<osg::StateSet*>(object); |
|---|
| 752 | if (!stateset) return false; |
|---|
| 753 | |
|---|
| 754 | switch(ea.getEventType()) |
|---|
| 755 | { |
|---|
| 756 | case(osgGA::GUIEventAdapter::MOVE): |
|---|
| 757 | case(osgGA::GUIEventAdapter::DRAG): |
|---|
| 758 | { |
|---|
| 759 | float v = (ea.getY()-ea.getYmin())/(ea.getYmax()-ea.getYmin()); |
|---|
| 760 | if (_shader) |
|---|
| 761 | { |
|---|
| 762 | osg::Uniform* uniform = 0; |
|---|
| 763 | if (_updateTransparency && (uniform = stateset->getUniform("transparency"))) uniform->set(v); |
|---|
| 764 | if (_updateAlphaCutOff && (uniform = stateset->getUniform("alphaCutOff"))) uniform->set(v); |
|---|
| 765 | if (_updateSampleDensity && (uniform = stateset->getUniform("sampleDensity"))) |
|---|
| 766 | { |
|---|
| 767 | float value = powf(v,5); |
|---|
| 768 | osg::notify(osg::INFO)<<"sampleDensity = "<<value<<std::endl; |
|---|
| 769 | uniform->set(value); |
|---|
| 770 | } |
|---|
| 771 | } |
|---|
| 772 | else |
|---|
| 773 | { |
|---|
| 774 | if (_updateAlphaCutOff) |
|---|
| 775 | { |
|---|
| 776 | osg::AlphaFunc* alphaFunc = dynamic_cast<osg::AlphaFunc*>(stateset->getAttribute(osg::StateAttribute::ALPHAFUNC)); |
|---|
| 777 | if (alphaFunc) |
|---|
| 778 | { |
|---|
| 779 | alphaFunc->setReferenceValue(v); |
|---|
| 780 | } |
|---|
| 781 | } |
|---|
| 782 | |
|---|
| 783 | if (_updateTransparency) |
|---|
| 784 | { |
|---|
| 785 | osg::Material* material = dynamic_cast<osg::Material*>(stateset->getAttribute(osg::StateAttribute::MATERIAL)); |
|---|
| 786 | if (material) |
|---|
| 787 | { |
|---|
| 788 | material->setAlpha(osg::Material::FRONT_AND_BACK,v); |
|---|
| 789 | } |
|---|
| 790 | } |
|---|
| 791 | } |
|---|
| 792 | |
|---|
| 793 | break; |
|---|
| 794 | } |
|---|
| 795 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 796 | { |
|---|
| 797 | if (ea.getKey()=='t') _updateTransparency = true; |
|---|
| 798 | if (ea.getKey()=='a') _updateAlphaCutOff = true; |
|---|
| 799 | if (ea.getKey()=='d') _updateSampleDensity = true; |
|---|
| 800 | break; |
|---|
| 801 | } |
|---|
| 802 | case(osgGA::GUIEventAdapter::KEYUP): |
|---|
| 803 | { |
|---|
| 804 | if (ea.getKey()=='t') _updateTransparency = false; |
|---|
| 805 | if (ea.getKey()=='a') _updateAlphaCutOff = false; |
|---|
| 806 | if (ea.getKey()=='d') _updateSampleDensity = false; |
|---|
| 807 | break; |
|---|
| 808 | } |
|---|
| 809 | default: |
|---|
| 810 | break; |
|---|
| 811 | } |
|---|
| 812 | return false; |
|---|
| 813 | } |
|---|
| 814 | |
|---|
| 815 | bool _shader; |
|---|
| 816 | bool _updateTransparency; |
|---|
| 817 | bool _updateAlphaCutOff; |
|---|
| 818 | bool _updateSampleDensity; |
|---|
| 819 | |
|---|
| 820 | }; |
|---|
| 821 | |
|---|
| 822 | osg::Node* createShaderModel(ShadingModel shadingModel, |
|---|
| 823 | osg::ref_ptr<osg::Image>& image_3d, |
|---|
| 824 | osg::Image* normalmap_3d, |
|---|
| 825 | osg::TransferFunction1D* tf, |
|---|
| 826 | osg::Texture::InternalFormatMode internalFormatMode, |
|---|
| 827 | float xSize, float ySize, float zSize, |
|---|
| 828 | float , float , float , |
|---|
| 829 | unsigned int =500, float =1.0f, float alphaFuncValue=0.02f) |
|---|
| 830 | { |
|---|
| 831 | osg::Texture::FilterMode minFilter = osg::Texture::LINEAR; |
|---|
| 832 | osg::Texture::FilterMode magFilter = osg::Texture::LINEAR; |
|---|
| 833 | |
|---|
| 834 | osg::Group* root = new osg::Group; |
|---|
| 835 | |
|---|
| 836 | osg::Geode* geode = new osg::Geode; |
|---|
| 837 | root->addChild(geode); |
|---|
| 838 | |
|---|
| 839 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| 840 | |
|---|
| 841 | stateset->setEventCallback(new FollowMouseCallback(true)); |
|---|
| 842 | |
|---|
| 843 | stateset->setMode(GL_ALPHA_TEST,osg::StateAttribute::ON); |
|---|
| 844 | |
|---|
| 845 | |
|---|
| 846 | osg::Program* program = new osg::Program; |
|---|
| 847 | stateset->setAttribute(program); |
|---|
| 848 | |
|---|
| 849 | |
|---|
| 850 | |
|---|
| 851 | osg::Shader* vertexShader = osgDB::readShaderFile(osg::Shader::VERTEX, "volume.vert"); |
|---|
| 852 | if (vertexShader) |
|---|
| 853 | { |
|---|
| 854 | program->addShader(vertexShader); |
|---|
| 855 | } |
|---|
| 856 | else |
|---|
| 857 | { |
|---|
| 858 | #include "volume_vert.cpp" |
|---|
| 859 | program->addShader(new osg::Shader(osg::Shader::VERTEX, volume_vert)); |
|---|
| 860 | } |
|---|
| 861 | |
|---|
| 862 | if (!(normalmap_3d && tf)) |
|---|
| 863 | { |
|---|
| 864 | |
|---|
| 865 | |
|---|
| 866 | |
|---|
| 867 | |
|---|
| 868 | osg::Texture3D* texture3D = new osg::Texture3D; |
|---|
| 869 | texture3D->setResizeNonPowerOfTwoHint(false); |
|---|
| 870 | texture3D->setFilter(osg::Texture3D::MIN_FILTER,minFilter); |
|---|
| 871 | texture3D->setFilter(osg::Texture3D::MAG_FILTER, magFilter); |
|---|
| 872 | texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 873 | texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 874 | texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 875 | if (image_3d->getPixelFormat()==GL_ALPHA || |
|---|
| 876 | image_3d->getPixelFormat()==GL_LUMINANCE) |
|---|
| 877 | { |
|---|
| 878 | texture3D->setInternalFormatMode(osg::Texture3D::USE_USER_DEFINED_FORMAT); |
|---|
| 879 | texture3D->setInternalFormat(GL_INTENSITY); |
|---|
| 880 | } |
|---|
| 881 | else |
|---|
| 882 | { |
|---|
| 883 | texture3D->setInternalFormatMode(internalFormatMode); |
|---|
| 884 | } |
|---|
| 885 | texture3D->setImage(image_3d.get()); |
|---|
| 886 | |
|---|
| 887 | stateset->setTextureAttributeAndModes(0,texture3D,osg::StateAttribute::ON); |
|---|
| 888 | |
|---|
| 889 | osg::Uniform* baseTextureSampler = new osg::Uniform("baseTexture",0); |
|---|
| 890 | stateset->addUniform(baseTextureSampler); |
|---|
| 891 | } |
|---|
| 892 | |
|---|
| 893 | |
|---|
| 894 | if (shadingModel==MaximumIntensityProjection) |
|---|
| 895 | { |
|---|
| 896 | if (tf) |
|---|
| 897 | { |
|---|
| 898 | osg::Texture1D* texture1D = new osg::Texture1D; |
|---|
| 899 | texture1D->setImage(tf->getImage()); |
|---|
| 900 | stateset->setTextureAttributeAndModes(1,texture1D,osg::StateAttribute::ON); |
|---|
| 901 | |
|---|
| 902 | osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume_tf_mip.frag"); |
|---|
| 903 | if (fragmentShader) |
|---|
| 904 | { |
|---|
| 905 | program->addShader(fragmentShader); |
|---|
| 906 | } |
|---|
| 907 | else |
|---|
| 908 | { |
|---|
| 909 | #include "volume_tf_mip_frag.cpp" |
|---|
| 910 | program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_tf_mip_frag)); |
|---|
| 911 | } |
|---|
| 912 | |
|---|
| 913 | osg::Uniform* tfTextureSampler = new osg::Uniform("tfTexture",1); |
|---|
| 914 | stateset->addUniform(tfTextureSampler); |
|---|
| 915 | |
|---|
| 916 | } |
|---|
| 917 | else |
|---|
| 918 | { |
|---|
| 919 | osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume_mip.frag"); |
|---|
| 920 | if (fragmentShader) |
|---|
| 921 | { |
|---|
| 922 | program->addShader(fragmentShader); |
|---|
| 923 | } |
|---|
| 924 | else |
|---|
| 925 | { |
|---|
| 926 | #include "volume_mip_frag.cpp" |
|---|
| 927 | program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_mip_frag)); |
|---|
| 928 | } |
|---|
| 929 | } |
|---|
| 930 | } |
|---|
| 931 | else if (shadingModel==Isosurface) |
|---|
| 932 | { |
|---|
| 933 | |
|---|
| 934 | if (tf) |
|---|
| 935 | { |
|---|
| 936 | osg::Texture1D* texture1D = new osg::Texture1D; |
|---|
| 937 | texture1D->setImage(tf->getImage()); |
|---|
| 938 | texture1D->setResizeNonPowerOfTwoHint(false); |
|---|
| 939 | texture1D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); |
|---|
| 940 | texture1D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); |
|---|
| 941 | texture1D->setWrap(osg::Texture::WRAP_R,osg::Texture::CLAMP_TO_EDGE); |
|---|
| 942 | stateset->setTextureAttributeAndModes(1,texture1D,osg::StateAttribute::ON); |
|---|
| 943 | |
|---|
| 944 | osg::Uniform* tfTextureSampler = new osg::Uniform("tfTexture",1); |
|---|
| 945 | stateset->addUniform(tfTextureSampler); |
|---|
| 946 | |
|---|
| 947 | osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume_tf_iso.frag"); |
|---|
| 948 | if (fragmentShader) |
|---|
| 949 | { |
|---|
| 950 | program->addShader(fragmentShader); |
|---|
| 951 | } |
|---|
| 952 | else |
|---|
| 953 | { |
|---|
| 954 | #include "volume_tf_iso_frag.cpp" |
|---|
| 955 | program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_tf_iso_frag)); |
|---|
| 956 | } |
|---|
| 957 | } |
|---|
| 958 | else |
|---|
| 959 | { |
|---|
| 960 | osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume_iso.frag"); |
|---|
| 961 | if (fragmentShader) |
|---|
| 962 | { |
|---|
| 963 | program->addShader(fragmentShader); |
|---|
| 964 | } |
|---|
| 965 | else |
|---|
| 966 | { |
|---|
| 967 | #include "volume_iso_frag.cpp" |
|---|
| 968 | program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_iso_frag)); |
|---|
| 969 | } |
|---|
| 970 | } |
|---|
| 971 | } |
|---|
| 972 | else if (normalmap_3d) |
|---|
| 973 | { |
|---|
| 974 | osg::notify(osg::NOTICE)<<"Setting up normalmapping shader"<<std::endl; |
|---|
| 975 | |
|---|
| 976 | osg::Uniform* normalMapSampler = new osg::Uniform("normalMap",1); |
|---|
| 977 | stateset->addUniform(normalMapSampler); |
|---|
| 978 | |
|---|
| 979 | osg::Texture3D* normalMap = new osg::Texture3D; |
|---|
| 980 | normalMap->setImage(normalmap_3d); |
|---|
| 981 | normalMap->setResizeNonPowerOfTwoHint(false); |
|---|
| 982 | normalMap->setInternalFormatMode(internalFormatMode); |
|---|
| 983 | normalMap->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture::LINEAR); |
|---|
| 984 | normalMap->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture::LINEAR); |
|---|
| 985 | normalMap->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 986 | normalMap->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 987 | normalMap->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 988 | |
|---|
| 989 | stateset->setTextureAttributeAndModes(1,normalMap,osg::StateAttribute::ON); |
|---|
| 990 | |
|---|
| 991 | if (tf) |
|---|
| 992 | { |
|---|
| 993 | osg::Texture1D* texture1D = new osg::Texture1D; |
|---|
| 994 | texture1D->setImage(tf->getImage()); |
|---|
| 995 | texture1D->setResizeNonPowerOfTwoHint(false); |
|---|
| 996 | texture1D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); |
|---|
| 997 | texture1D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); |
|---|
| 998 | texture1D->setWrap(osg::Texture::WRAP_R,osg::Texture::CLAMP_TO_EDGE); |
|---|
| 999 | stateset->setTextureAttributeAndModes(0,texture1D,osg::StateAttribute::ON); |
|---|
| 1000 | |
|---|
| 1001 | osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume-tf-n.frag"); |
|---|
| 1002 | if (fragmentShader) |
|---|
| 1003 | { |
|---|
| 1004 | program->addShader(fragmentShader); |
|---|
| 1005 | } |
|---|
| 1006 | else |
|---|
| 1007 | { |
|---|
| 1008 | #include "volume_tf_n_frag.cpp" |
|---|
| 1009 | program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_tf_n_frag)); |
|---|
| 1010 | } |
|---|
| 1011 | |
|---|
| 1012 | osg::Uniform* tfTextureSampler = new osg::Uniform("tfTexture",0); |
|---|
| 1013 | stateset->addUniform(tfTextureSampler); |
|---|
| 1014 | } |
|---|
| 1015 | else |
|---|
| 1016 | { |
|---|
| 1017 | osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume-n.frag"); |
|---|
| 1018 | if (fragmentShader) |
|---|
| 1019 | { |
|---|
| 1020 | program->addShader(fragmentShader); |
|---|
| 1021 | } |
|---|
| 1022 | else |
|---|
| 1023 | { |
|---|
| 1024 | #include "volume_n_frag.cpp" |
|---|
| 1025 | program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_n_frag)); |
|---|
| 1026 | } |
|---|
| 1027 | } |
|---|
| 1028 | } |
|---|
| 1029 | else if (tf) |
|---|
| 1030 | { |
|---|
| 1031 | osg::Texture1D* texture1D = new osg::Texture1D; |
|---|
| 1032 | texture1D->setImage(tf->getImage()); |
|---|
| 1033 | texture1D->setResizeNonPowerOfTwoHint(false); |
|---|
| 1034 | texture1D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); |
|---|
| 1035 | texture1D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); |
|---|
| 1036 | texture1D->setWrap(osg::Texture::WRAP_R,osg::Texture::CLAMP_TO_EDGE); |
|---|
| 1037 | stateset->setTextureAttributeAndModes(1,texture1D,osg::StateAttribute::ON); |
|---|
| 1038 | |
|---|
| 1039 | osg::Uniform* tfTextureSampler = new osg::Uniform("tfTexture",1); |
|---|
| 1040 | stateset->addUniform(tfTextureSampler); |
|---|
| 1041 | |
|---|
| 1042 | osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume-tf.frag"); |
|---|
| 1043 | if (fragmentShader) |
|---|
| 1044 | { |
|---|
| 1045 | program->addShader(fragmentShader); |
|---|
| 1046 | } |
|---|
| 1047 | else |
|---|
| 1048 | { |
|---|
| 1049 | #include "volume_tf_frag.cpp" |
|---|
| 1050 | program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_tf_frag)); |
|---|
| 1051 | } |
|---|
| 1052 | |
|---|
| 1053 | } |
|---|
| 1054 | else |
|---|
| 1055 | { |
|---|
| 1056 | |
|---|
| 1057 | osg::Shader* fragmentShader = osgDB::readShaderFile(osg::Shader::FRAGMENT, "volume.frag"); |
|---|
| 1058 | if (fragmentShader) |
|---|
| 1059 | { |
|---|
| 1060 | program->addShader(fragmentShader); |
|---|
| 1061 | } |
|---|
| 1062 | else |
|---|
| 1063 | { |
|---|
| 1064 | #include "volume_frag.cpp" |
|---|
| 1065 | program->addShader(new osg::Shader(osg::Shader::FRAGMENT, volume_frag)); |
|---|
| 1066 | } |
|---|
| 1067 | } |
|---|
| 1068 | |
|---|
| 1069 | osg::Uniform* sampleDensity = new osg::Uniform("sampleDensity", 0.005f); |
|---|
| 1070 | stateset->addUniform(sampleDensity); |
|---|
| 1071 | |
|---|
| 1072 | osg::Uniform* transpancy = new osg::Uniform("transparency",0.5f); |
|---|
| 1073 | stateset->addUniform(transpancy); |
|---|
| 1074 | |
|---|
| 1075 | osg::Uniform* alphaCutOff = new osg::Uniform("alphaCutOff",alphaFuncValue); |
|---|
| 1076 | stateset->addUniform(alphaCutOff); |
|---|
| 1077 | |
|---|
| 1078 | stateset->setMode(GL_CULL_FACE, osg::StateAttribute::ON); |
|---|
| 1079 | |
|---|
| 1080 | osg::TexGen* texgen = new osg::TexGen; |
|---|
| 1081 | texgen->setMode(osg::TexGen::OBJECT_LINEAR); |
|---|
| 1082 | texgen->setPlane(osg::TexGen::S, osg::Plane(1.0f/xSize,0.0f,0.0f,0.0f)); |
|---|
| 1083 | texgen->setPlane(osg::TexGen::T, osg::Plane(0.0f,1.0f/ySize,0.0f,0.0f)); |
|---|
| 1084 | texgen->setPlane(osg::TexGen::R, osg::Plane(0.0f,0.0f,1.0f/zSize,0.0f)); |
|---|
| 1085 | texgen->setPlane(osg::TexGen::Q, osg::Plane(0.0f,0.0f,0.0f,1.0f)); |
|---|
| 1086 | |
|---|
| 1087 | stateset->setTextureAttributeAndModes(0, texgen, osg::StateAttribute::ON); |
|---|
| 1088 | |
|---|
| 1089 | { |
|---|
| 1090 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 1091 | |
|---|
| 1092 | osg::Vec3Array* coords = new osg::Vec3Array(8); |
|---|
| 1093 | (*coords)[0].set(0,0,0); |
|---|
| 1094 | (*coords)[1].set(xSize,0,0); |
|---|
| 1095 | (*coords)[2].set(xSize,ySize,0); |
|---|
| 1096 | (*coords)[3].set(0,ySize,0); |
|---|
| 1097 | (*coords)[4].set(0,0,zSize); |
|---|
| 1098 | (*coords)[5].set(xSize,0,zSize); |
|---|
| 1099 | (*coords)[6].set(ySize,ySize,zSize); |
|---|
| 1100 | (*coords)[7].set(0,ySize,zSize); |
|---|
| 1101 | geom->setVertexArray(coords); |
|---|
| 1102 | |
|---|
| 1103 | osg::Vec4Array* colours = new osg::Vec4Array(1); |
|---|
| 1104 | (*colours)[0].set(1.0f,1.0f,1.0,1.0f); |
|---|
| 1105 | geom->setColorArray(colours); |
|---|
| 1106 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 1107 | |
|---|
| 1108 | osg::DrawElementsUShort* drawElements = new osg::DrawElementsUShort(GL_QUADS); |
|---|
| 1109 | |
|---|
| 1110 | drawElements->push_back(0); |
|---|
| 1111 | drawElements->push_back(1); |
|---|
| 1112 | drawElements->push_back(2); |
|---|
| 1113 | drawElements->push_back(3); |
|---|
| 1114 | |
|---|
| 1115 | |
|---|
| 1116 | drawElements->push_back(3); |
|---|
| 1117 | drawElements->push_back(2); |
|---|
| 1118 | drawElements->push_back(6); |
|---|
| 1119 | drawElements->push_back(7); |
|---|
| 1120 | |
|---|
| 1121 | |
|---|
| 1122 | drawElements->push_back(0); |
|---|
| 1123 | drawElements->push_back(3); |
|---|
| 1124 | drawElements->push_back(7); |
|---|
| 1125 | drawElements->push_back(4); |
|---|
| 1126 | |
|---|
| 1127 | |
|---|
| 1128 | drawElements->push_back(5); |
|---|
| 1129 | drawElements->push_back(6); |
|---|
| 1130 | drawElements->push_back(2); |
|---|
| 1131 | drawElements->push_back(1); |
|---|
| 1132 | |
|---|
| 1133 | |
|---|
| 1134 | drawElements->push_back(1); |
|---|
| 1135 | drawElements->push_back(0); |
|---|
| 1136 | drawElements->push_back(4); |
|---|
| 1137 | drawElements->push_back(5); |
|---|
| 1138 | |
|---|
| 1139 | |
|---|
| 1140 | drawElements->push_back(7); |
|---|
| 1141 | drawElements->push_back(6); |
|---|
| 1142 | drawElements->push_back(5); |
|---|
| 1143 | drawElements->push_back(4); |
|---|
| 1144 | |
|---|
| 1145 | geom->addPrimitiveSet(drawElements); |
|---|
| 1146 | |
|---|
| 1147 | geode->addDrawable(geom); |
|---|
| 1148 | |
|---|
| 1149 | } |
|---|
| 1150 | return root; |
|---|
| 1151 | } |
|---|
| 1152 | |
|---|
| 1153 | osg::Node* createModel(ShadingModel shadeModel, |
|---|
| 1154 | osg::ref_ptr<osg::Image>& image_3d, |
|---|
| 1155 | osg::ref_ptr<osg::Image>& normalmap_3d, |
|---|
| 1156 | osg::Texture::InternalFormatMode internalFormatMode, |
|---|
| 1157 | float xSize, float ySize, float zSize, |
|---|
| 1158 | float xMultiplier, float yMultiplier, float zMultiplier, |
|---|
| 1159 | unsigned int numSlices=500, float sliceEnd=1.0f, float alphaFuncValue=0.02f, bool maximumIntensityProjection = false) |
|---|
| 1160 | { |
|---|
| 1161 | bool two_pass = normalmap_3d.valid() && (image_3d->getPixelFormat()==GL_RGB || image_3d->getPixelFormat()==GL_RGBA); |
|---|
| 1162 | |
|---|
| 1163 | osg::BoundingBox bb(-xSize*0.5f,-ySize*0.5f,-zSize*0.5f,xSize*0.5f,ySize*0.5f,zSize*0.5f); |
|---|
| 1164 | |
|---|
| 1165 | |
|---|
| 1166 | osg::Texture::FilterMode minFilter = osg::Texture::NEAREST; |
|---|
| 1167 | osg::Texture::FilterMode magFilter = osg::Texture::NEAREST; |
|---|
| 1168 | |
|---|
| 1169 | float maxAxis = xSize; |
|---|
| 1170 | if (ySize > maxAxis) maxAxis = ySize; |
|---|
| 1171 | if (zSize > maxAxis) maxAxis = zSize; |
|---|
| 1172 | |
|---|
| 1173 | osg::Group* group = new osg::Group; |
|---|
| 1174 | |
|---|
| 1175 | osg::TexGenNode* texgenNode_0 = new osg::TexGenNode; |
|---|
| 1176 | texgenNode_0->setTextureUnit(0); |
|---|
| 1177 | texgenNode_0->getTexGen()->setMode(osg::TexGen::EYE_LINEAR); |
|---|
| 1178 | texgenNode_0->getTexGen()->setPlane(osg::TexGen::S, osg::Plane(xMultiplier/xSize,0.0f,0.0f,0.5f)); |
|---|
| 1179 | texgenNode_0->getTexGen()->setPlane(osg::TexGen::T, osg::Plane(0.0f,yMultiplier/ySize,0.0f,0.5f)); |
|---|
| 1180 | texgenNode_0->getTexGen()->setPlane(osg::TexGen::R, osg::Plane(0.0f,0.0f,zMultiplier/zSize,0.5f)); |
|---|
| 1181 | |
|---|
| 1182 | if (two_pass) |
|---|
| 1183 | { |
|---|
| 1184 | osg::TexGenNode* texgenNode_1 = new osg::TexGenNode; |
|---|
| 1185 | texgenNode_1->setTextureUnit(1); |
|---|
| 1186 | texgenNode_1->getTexGen()->setMode(osg::TexGen::EYE_LINEAR); |
|---|
| 1187 | texgenNode_1->getTexGen()->setPlane(osg::TexGen::S, texgenNode_0->getTexGen()->getPlane(osg::TexGen::S)); |
|---|
| 1188 | texgenNode_1->getTexGen()->setPlane(osg::TexGen::T, texgenNode_0->getTexGen()->getPlane(osg::TexGen::T)); |
|---|
| 1189 | texgenNode_1->getTexGen()->setPlane(osg::TexGen::R, texgenNode_0->getTexGen()->getPlane(osg::TexGen::R)); |
|---|
| 1190 | |
|---|
| 1191 | texgenNode_1->addChild(texgenNode_0); |
|---|
| 1192 | |
|---|
| 1193 | group->addChild(texgenNode_1); |
|---|
| 1194 | } |
|---|
| 1195 | else |
|---|
| 1196 | { |
|---|
| 1197 | group->addChild(texgenNode_0); |
|---|
| 1198 | } |
|---|
| 1199 | |
|---|
| 1200 | float cubeSize = sqrtf(xSize*xSize+ySize*ySize+zSize*zSize); |
|---|
| 1201 | |
|---|
| 1202 | osg::ClipNode* clipnode = new osg::ClipNode; |
|---|
| 1203 | clipnode->addChild(createCube(cubeSize,1.0f, numSlices,sliceEnd)); |
|---|
| 1204 | clipnode->createClipBox(bb); |
|---|
| 1205 | |
|---|
| 1206 | { |
|---|
| 1207 | |
|---|
| 1208 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 1209 | |
|---|
| 1210 | osg::Vec3Array* coords = new osg::Vec3Array(); |
|---|
| 1211 | coords->push_back(bb.corner(0)); |
|---|
| 1212 | coords->push_back(bb.corner(1)); |
|---|
| 1213 | coords->push_back(bb.corner(2)); |
|---|
| 1214 | coords->push_back(bb.corner(3)); |
|---|
| 1215 | coords->push_back(bb.corner(4)); |
|---|
| 1216 | coords->push_back(bb.corner(5)); |
|---|
| 1217 | coords->push_back(bb.corner(6)); |
|---|
| 1218 | coords->push_back(bb.corner(7)); |
|---|
| 1219 | |
|---|
| 1220 | geom->setVertexArray(coords); |
|---|
| 1221 | |
|---|
| 1222 | osg::Vec4Array* colors = new osg::Vec4Array(1); |
|---|
| 1223 | (*colors)[0].set(1.0f,1.0f,1.0f,1.0f); |
|---|
| 1224 | geom->setColorArray(colors); |
|---|
| 1225 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 1226 | |
|---|
| 1227 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,coords->size())); |
|---|
| 1228 | |
|---|
| 1229 | osg::Geode* geode = new osg::Geode; |
|---|
| 1230 | geode->addDrawable(geom); |
|---|
| 1231 | |
|---|
| 1232 | clipnode->addChild(geode); |
|---|
| 1233 | |
|---|
| 1234 | } |
|---|
| 1235 | |
|---|
| 1236 | texgenNode_0->addChild(clipnode); |
|---|
| 1237 | |
|---|
| 1238 | osg::StateSet* stateset = texgenNode_0->getOrCreateStateSet(); |
|---|
| 1239 | |
|---|
| 1240 | stateset->setEventCallback(new FollowMouseCallback(false)); |
|---|
| 1241 | |
|---|
| 1242 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::ON); |
|---|
| 1243 | stateset->setMode(GL_BLEND,osg::StateAttribute::ON); |
|---|
| 1244 | stateset->setAttributeAndModes(new osg::AlphaFunc(osg::AlphaFunc::GREATER,alphaFuncValue), osg::StateAttribute::ON); |
|---|
| 1245 | |
|---|
| 1246 | osg::Material* material = new osg::Material; |
|---|
| 1247 | material->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 1248 | stateset->setAttributeAndModes(material); |
|---|
| 1249 | |
|---|
| 1250 | if (shadeModel==MaximumIntensityProjection) |
|---|
| 1251 | { |
|---|
| 1252 | stateset->setAttribute(new osg::BlendFunc(osg::BlendFunc::ONE, osg::BlendFunc::ONE)); |
|---|
| 1253 | stateset->setAttribute(new osg::BlendEquation(osg::BlendEquation::RGBA_MAX)); |
|---|
| 1254 | } |
|---|
| 1255 | |
|---|
| 1256 | osg::Vec3 lightDirection(1.0f,-1.0f,1.0f); |
|---|
| 1257 | lightDirection.normalize(); |
|---|
| 1258 | |
|---|
| 1259 | if (normalmap_3d.valid()) |
|---|
| 1260 | { |
|---|
| 1261 | if (two_pass) |
|---|
| 1262 | { |
|---|
| 1263 | |
|---|
| 1264 | |
|---|
| 1265 | osg::Texture3D* bump_texture3D = new osg::Texture3D; |
|---|
| 1266 | bump_texture3D->setFilter(osg::Texture3D::MIN_FILTER,minFilter); |
|---|
| 1267 | bump_texture3D->setFilter(osg::Texture3D::MAG_FILTER, magFilter); |
|---|
| 1268 | bump_texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 1269 | bump_texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 1270 | bump_texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 1271 | bump_texture3D->setImage(normalmap_3d.get()); |
|---|
| 1272 | |
|---|
| 1273 | bump_texture3D->setInternalFormatMode(internalFormatMode); |
|---|
| 1274 | |
|---|
| 1275 | stateset->setTextureAttributeAndModes(0,bump_texture3D,osg::StateAttribute::ON); |
|---|
| 1276 | |
|---|
| 1277 | osg::TexEnvCombine* tec = new osg::TexEnvCombine; |
|---|
| 1278 | tec->setConstantColorAsLightDirection(lightDirection); |
|---|
| 1279 | |
|---|
| 1280 | tec->setCombine_RGB(osg::TexEnvCombine::DOT3_RGB); |
|---|
| 1281 | tec->setSource0_RGB(osg::TexEnvCombine::CONSTANT); |
|---|
| 1282 | tec->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 1283 | tec->setSource1_RGB(osg::TexEnvCombine::TEXTURE); |
|---|
| 1284 | |
|---|
| 1285 | tec->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 1286 | |
|---|
| 1287 | tec->setCombine_Alpha(osg::TexEnvCombine::REPLACE); |
|---|
| 1288 | tec->setSource0_Alpha(osg::TexEnvCombine::PRIMARY_COLOR); |
|---|
| 1289 | tec->setOperand0_Alpha(osg::TexEnvCombine::SRC_ALPHA); |
|---|
| 1290 | tec->setSource1_Alpha(osg::TexEnvCombine::TEXTURE); |
|---|
| 1291 | tec->setOperand1_Alpha(osg::TexEnvCombine::SRC_ALPHA); |
|---|
| 1292 | |
|---|
| 1293 | stateset->setTextureAttributeAndModes(0, tec, osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
|---|
| 1294 | |
|---|
| 1295 | stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); |
|---|
| 1296 | stateset->setTextureMode(0,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); |
|---|
| 1297 | stateset->setTextureMode(0,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); |
|---|
| 1298 | |
|---|
| 1299 | |
|---|
| 1300 | |
|---|
| 1301 | osg::Texture3D* texture3D = new osg::Texture3D; |
|---|
| 1302 | texture3D->setResizeNonPowerOfTwoHint(false); |
|---|
| 1303 | texture3D->setFilter(osg::Texture3D::MIN_FILTER,minFilter); |
|---|
| 1304 | texture3D->setFilter(osg::Texture3D::MAG_FILTER, magFilter); |
|---|
| 1305 | texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 1306 | texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 1307 | texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 1308 | if (image_3d->getPixelFormat()==GL_ALPHA || |
|---|
| 1309 | image_3d->getPixelFormat()==GL_LUMINANCE) |
|---|
| 1310 | { |
|---|
| 1311 | texture3D->setInternalFormatMode(osg::Texture3D::USE_USER_DEFINED_FORMAT); |
|---|
| 1312 | texture3D->setInternalFormat(GL_INTENSITY); |
|---|
| 1313 | } |
|---|
| 1314 | else |
|---|
| 1315 | { |
|---|
| 1316 | texture3D->setInternalFormatMode(internalFormatMode); |
|---|
| 1317 | } |
|---|
| 1318 | texture3D->setImage(image_3d.get()); |
|---|
| 1319 | |
|---|
| 1320 | stateset->setTextureAttributeAndModes(1,texture3D,osg::StateAttribute::ON); |
|---|
| 1321 | |
|---|
| 1322 | stateset->setTextureMode(1,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); |
|---|
| 1323 | stateset->setTextureMode(1,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); |
|---|
| 1324 | stateset->setTextureMode(1,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); |
|---|
| 1325 | |
|---|
| 1326 | stateset->setTextureAttributeAndModes(1,new osg::TexEnv(),osg::StateAttribute::ON); |
|---|
| 1327 | |
|---|
| 1328 | } |
|---|
| 1329 | else |
|---|
| 1330 | { |
|---|
| 1331 | osg::Texture3D* bump_texture3D = new osg::Texture3D; |
|---|
| 1332 | bump_texture3D->setResizeNonPowerOfTwoHint(false); |
|---|
| 1333 | bump_texture3D->setFilter(osg::Texture3D::MIN_FILTER,minFilter); |
|---|
| 1334 | bump_texture3D->setFilter(osg::Texture3D::MAG_FILTER, magFilter); |
|---|
| 1335 | bump_texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 1336 | bump_texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 1337 | bump_texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 1338 | bump_texture3D->setImage(normalmap_3d.get()); |
|---|
| 1339 | |
|---|
| 1340 | bump_texture3D->setInternalFormatMode(internalFormatMode); |
|---|
| 1341 | |
|---|
| 1342 | stateset->setTextureAttributeAndModes(0,bump_texture3D,osg::StateAttribute::ON); |
|---|
| 1343 | |
|---|
| 1344 | osg::TexEnvCombine* tec = new osg::TexEnvCombine; |
|---|
| 1345 | tec->setConstantColorAsLightDirection(lightDirection); |
|---|
| 1346 | |
|---|
| 1347 | tec->setCombine_RGB(osg::TexEnvCombine::DOT3_RGB); |
|---|
| 1348 | tec->setSource0_RGB(osg::TexEnvCombine::CONSTANT); |
|---|
| 1349 | tec->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 1350 | tec->setSource1_RGB(osg::TexEnvCombine::TEXTURE); |
|---|
| 1351 | tec->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 1352 | |
|---|
| 1353 | tec->setCombine_Alpha(osg::TexEnvCombine::MODULATE); |
|---|
| 1354 | tec->setSource0_Alpha(osg::TexEnvCombine::PRIMARY_COLOR); |
|---|
| 1355 | tec->setOperand0_Alpha(osg::TexEnvCombine::SRC_ALPHA); |
|---|
| 1356 | tec->setSource1_Alpha(osg::TexEnvCombine::TEXTURE); |
|---|
| 1357 | tec->setOperand1_Alpha(osg::TexEnvCombine::SRC_ALPHA); |
|---|
| 1358 | |
|---|
| 1359 | stateset->setTextureAttributeAndModes(0, tec, osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
|---|
| 1360 | |
|---|
| 1361 | stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); |
|---|
| 1362 | stateset->setTextureMode(0,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); |
|---|
| 1363 | stateset->setTextureMode(0,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); |
|---|
| 1364 | |
|---|
| 1365 | image_3d = normalmap_3d; |
|---|
| 1366 | } |
|---|
| 1367 | } |
|---|
| 1368 | else |
|---|
| 1369 | { |
|---|
| 1370 | |
|---|
| 1371 | |
|---|
| 1372 | |
|---|
| 1373 | |
|---|
| 1374 | osg::Texture3D* texture3D = new osg::Texture3D; |
|---|
| 1375 | texture3D->setResizeNonPowerOfTwoHint(false); |
|---|
| 1376 | texture3D->setFilter(osg::Texture3D::MIN_FILTER,minFilter); |
|---|
| 1377 | texture3D->setFilter(osg::Texture3D::MAG_FILTER, magFilter); |
|---|
| 1378 | texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 1379 | texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 1380 | texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP_TO_EDGE); |
|---|
| 1381 | if (image_3d->getPixelFormat()==GL_ALPHA || |
|---|
| 1382 | image_3d->getPixelFormat()==GL_LUMINANCE) |
|---|
| 1383 | { |
|---|
| 1384 | texture3D->setInternalFormatMode(osg::Texture3D::USE_USER_DEFINED_FORMAT); |
|---|
| 1385 | texture3D->setInternalFormat(GL_INTENSITY); |
|---|
| 1386 | } |
|---|
| 1387 | else |
|---|
| 1388 | { |
|---|
| 1389 | texture3D->setInternalFormatMode(internalFormatMode); |
|---|
| 1390 | } |
|---|
| 1391 | |
|---|
| 1392 | texture3D->setImage(image_3d.get()); |
|---|
| 1393 | |
|---|
| 1394 | stateset->setTextureAttributeAndModes(0,texture3D,osg::StateAttribute::ON); |
|---|
| 1395 | |
|---|
| 1396 | stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); |
|---|
| 1397 | stateset->setTextureMode(0,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); |
|---|
| 1398 | stateset->setTextureMode(0,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); |
|---|
| 1399 | |
|---|
| 1400 | stateset->setTextureAttributeAndModes(0,new osg::TexEnv(),osg::StateAttribute::ON); |
|---|
| 1401 | } |
|---|
| 1402 | |
|---|
| 1403 | return group; |
|---|
| 1404 | } |
|---|
| 1405 | |
|---|
| 1406 | struct ScaleOperator |
|---|
| 1407 | { |
|---|
| 1408 | ScaleOperator():_scale(1.0f) {} |
|---|
| 1409 | ScaleOperator(float scale):_scale(scale) {} |
|---|
| 1410 | ScaleOperator(const ScaleOperator& so):_scale(so._scale) {} |
|---|
| 1411 | |
|---|
| 1412 | ScaleOperator& operator = (const ScaleOperator& so) { _scale = so._scale; return *this; } |
|---|
| 1413 | |
|---|
| 1414 | float _scale; |
|---|
| 1415 | |
|---|
| 1416 | inline void luminance(float& l) const { l*= _scale; } |
|---|
| 1417 | inline void alpha(float& a) const { a*= _scale; } |
|---|
| 1418 | inline void luminance_alpha(float& l,float& a) const { l*= _scale; a*= _scale; } |
|---|
| 1419 | inline void rgb(float& r,float& g,float& b) const { r*= _scale; g*=_scale; b*=_scale; } |
|---|
| 1420 | inline void rgba(float& r,float& g,float& b,float& a) const { r*= _scale; g*=_scale; b*=_scale; a*=_scale; } |
|---|
| 1421 | }; |
|---|
| 1422 | |
|---|
| 1423 | struct RecordRowOperator |
|---|
| 1424 | { |
|---|
| 1425 | RecordRowOperator(unsigned int num):_colours(num),_pos(0) {} |
|---|
| 1426 | |
|---|
| 1427 | mutable std::vector<osg::Vec4> _colours; |
|---|
| 1428 | mutable unsigned int _pos; |
|---|
| 1429 | |
|---|
| 1430 | inline void luminance(float l) const { rgba(l,l,l,1.0f); } |
|---|
| 1431 | inline void alpha(float a) const { rgba(1.0f,1.0f,1.0f,a); } |
|---|
| 1432 | inline void luminance_alpha(float l,float a) const { rgba(l,l,l,a); } |
|---|
| 1433 | inline void rgb(float r,float g,float b) const { rgba(r,g,b,1.0f); } |
|---|
| 1434 | inline void rgba(float r,float g,float b,float a) const { _colours[_pos++].set(r,g,b,a); } |
|---|
| 1435 | }; |
|---|
| 1436 | |
|---|
| 1437 | struct WriteRowOperator |
|---|
| 1438 | { |
|---|
| 1439 | WriteRowOperator():_pos(0) {} |
|---|
| 1440 | WriteRowOperator(unsigned int num):_colours(num),_pos(0) {} |
|---|
| 1441 | |
|---|
| 1442 | std::vector<osg::Vec4> _colours; |
|---|
| 1443 | mutable unsigned int _pos; |
|---|
| 1444 | |
|---|
| 1445 | inline void luminance(float& l) const { l = _colours[_pos++].r(); } |
|---|
| 1446 | inline void alpha(float& a) const { a = _colours[_pos++].a(); } |
|---|
| 1447 | inline void luminance_alpha(float& l,float& a) const { l = _colours[_pos].r(); a = _colours[_pos++].a(); } |
|---|
| 1448 | inline void rgb(float& r,float& g,float& b) const { r = _colours[_pos].r(); g = _colours[_pos].g(); b = _colours[_pos].b(); } |
|---|
| 1449 | inline void rgba(float& r,float& g,float& b,float& a) const { r = _colours[_pos].r(); g = _colours[_pos].g(); b = _colours[_pos].b(); a = _colours[_pos++].a(); } |
|---|
| 1450 | }; |
|---|
| 1451 | |
|---|
| 1452 | osg::Image* readRaw(int sizeX, int sizeY, int sizeZ, int numberBytesPerComponent, int numberOfComponents, const std::string& endian, const std::string& raw_filename) |
|---|
| 1453 | { |
|---|
| 1454 | osgDB::ifstream fin(raw_filename.c_str(), std::ifstream::binary); |
|---|
| 1455 | if (!fin) return 0; |
|---|
| 1456 | |
|---|
| 1457 | GLenum pixelFormat; |
|---|
| 1458 | switch(numberOfComponents) |
|---|
| 1459 | { |
|---|
| 1460 | case 1 : pixelFormat = GL_LUMINANCE; break; |
|---|
| 1461 | case 2 : pixelFormat = GL_LUMINANCE_ALPHA; break; |
|---|
| 1462 | case 3 : pixelFormat = GL_RGB; break; |
|---|
| 1463 | case 4 : pixelFormat = GL_RGBA; break; |
|---|
| 1464 | default : |
|---|
| 1465 | osg::notify(osg::NOTICE)<<"Error: numberOfComponents="<<numberOfComponents<<" not supported, only 1,2,3 or 4 are supported."<<std::endl; |
|---|
| 1466 | return 0; |
|---|
| 1467 | } |
|---|
| 1468 | |
|---|
| 1469 | |
|---|
| 1470 | GLenum dataType; |
|---|
| 1471 | switch(numberBytesPerComponent) |
|---|
| 1472 | { |
|---|
| 1473 | case 1 : dataType = GL_UNSIGNED_BYTE; break; |
|---|
| 1474 | case 2 : dataType = GL_UNSIGNED_SHORT; break; |
|---|
| 1475 | case 4 : dataType = GL_UNSIGNED_INT; break; |
|---|
| 1476 | default : |
|---|
| 1477 | osg::notify(osg::NOTICE)<<"Error: numberBytesPerComponent="<<numberBytesPerComponent<<" not supported, only 1,2 or 4 are supported."<<std::endl; |
|---|
| 1478 | return 0; |
|---|
| 1479 | } |
|---|
| 1480 | |
|---|
| 1481 | int s_maximumTextureSize=256, t_maximumTextureSize=256, r_maximumTextureSize=256; |
|---|
| 1482 | |
|---|
| 1483 | int sizeS = sizeX; |
|---|
| 1484 | int sizeT = sizeY; |
|---|
| 1485 | int sizeR = sizeZ; |
|---|
| 1486 | clampToNearestValidPowerOfTwo(sizeS, sizeT, sizeR, s_maximumTextureSize, t_maximumTextureSize, r_maximumTextureSize); |
|---|
| 1487 | |
|---|
| 1488 | osg::ref_ptr<osg::Image> image = new osg::Image; |
|---|
| 1489 | image->allocateImage(sizeS, sizeT, sizeR, pixelFormat, dataType); |
|---|
| 1490 | |
|---|
| 1491 | |
|---|
| 1492 | bool endianSwap = (osg::getCpuByteOrder()==osg::BigEndian) ? (endian!="big") : (endian=="big"); |
|---|
| 1493 | |
|---|
| 1494 | unsigned int r_offset = (sizeZ<sizeR) ? sizeR/2 - sizeZ/2 : 0; |
|---|
| 1495 | |
|---|
| 1496 | int offset = endianSwap ? numberBytesPerComponent : 0; |
|---|
| 1497 | int delta = endianSwap ? -1 : 1; |
|---|
| 1498 | for(int r=0;r<sizeZ;++r) |
|---|
| 1499 | { |
|---|
| 1500 | for(int t=0;t<sizeY;++t) |
|---|
| 1501 | { |
|---|
| 1502 | char* data = (char*) image->data(0,t,r+r_offset); |
|---|
| 1503 | for(int s=0;s<sizeX;++s) |
|---|
| 1504 | { |
|---|
| 1505 | if (!fin) return 0; |
|---|
| 1506 | |
|---|
| 1507 | for(int c=0;c<numberOfComponents;++c) |
|---|
| 1508 | { |
|---|
| 1509 | char* ptr = data+offset; |
|---|
| 1510 | for(int b=0;b<numberBytesPerComponent;++b) |
|---|
| 1511 | { |
|---|
| 1512 | fin.read((char*)ptr, 1); |
|---|
| 1513 | ptr += delta; |
|---|
| 1514 | } |
|---|
| 1515 | data += numberBytesPerComponent; |
|---|
| 1516 | } |
|---|
| 1517 | } |
|---|
| 1518 | } |
|---|
| 1519 | } |
|---|
| 1520 | |
|---|
| 1521 | |
|---|
| 1522 | |
|---|
| 1523 | { |
|---|
| 1524 | |
|---|
| 1525 | osg::Vec4 minValue, maxValue; |
|---|
| 1526 | osg::computeMinMax(image.get(), minValue, maxValue); |
|---|
| 1527 | osg::modifyImage(image.get(),ScaleOperator(1.0f/maxValue.r())); |
|---|
| 1528 | } |
|---|
| 1529 | |
|---|
| 1530 | |
|---|
| 1531 | fin.close(); |
|---|
| 1532 | |
|---|
| 1533 | if (dataType!=GL_UNSIGNED_BYTE) |
|---|
| 1534 | { |
|---|
| 1535 | |
|---|
| 1536 | |
|---|
| 1537 | osg::ref_ptr<osg::Image> new_image = new osg::Image; |
|---|
| 1538 | new_image->allocateImage(sizeS, sizeT, sizeR, pixelFormat, GL_UNSIGNED_BYTE); |
|---|
| 1539 | |
|---|
| 1540 | RecordRowOperator readOp(sizeS); |
|---|
| 1541 | WriteRowOperator writeOp; |
|---|
| 1542 | |
|---|
| 1543 | for(int r=0;r<sizeR;++r) |
|---|
| 1544 | { |
|---|
| 1545 | for(int t=0;t<sizeT;++t) |
|---|
| 1546 | { |
|---|
| 1547 | |
|---|
| 1548 | readOp._pos = 0; |
|---|
| 1549 | writeOp._pos = 0; |
|---|
| 1550 | |
|---|
| 1551 | |
|---|
| 1552 | osg::readRow(sizeS, pixelFormat, dataType, image->data(0,t,r), readOp); |
|---|
| 1553 | |
|---|
| 1554 | |
|---|
| 1555 | writeOp._colours.swap(readOp._colours); |
|---|
| 1556 | |
|---|
| 1557 | osg::modifyRow(sizeS, pixelFormat, GL_UNSIGNED_BYTE, new_image->data(0,t,r), writeOp); |
|---|
| 1558 | |
|---|
| 1559 | |
|---|
| 1560 | writeOp._colours.swap(readOp._colours); |
|---|
| 1561 | } |
|---|
| 1562 | } |
|---|
| 1563 | |
|---|
| 1564 | image = new_image; |
|---|
| 1565 | } |
|---|
| 1566 | |
|---|
| 1567 | return image.release(); |
|---|
| 1568 | |
|---|
| 1569 | |
|---|
| 1570 | } |
|---|
| 1571 | |
|---|
| 1572 | enum ColourSpaceOperation |
|---|
| 1573 | { |
|---|
| 1574 | NO_COLOUR_SPACE_OPERATION, |
|---|
| 1575 | MODULATE_ALPHA_BY_LUMINANCE, |
|---|
| 1576 | MODULATE_ALPHA_BY_COLOUR, |
|---|
| 1577 | REPLACE_ALPHA_WITH_LUMINANACE, |
|---|
| 1578 | REPLACE_RGB_WITH_LUMINANCE |
|---|
| 1579 | }; |
|---|
| 1580 | |
|---|
| 1581 | struct ModulateAlphaByLuminanceOperator |
|---|
| 1582 | { |
|---|
| 1583 | ModulateAlphaByLuminanceOperator() {} |
|---|
| 1584 | |
|---|
| 1585 | inline void luminance(float&) const {} |
|---|
| 1586 | inline void alpha(float&) const {} |
|---|
| 1587 | inline void luminance_alpha(float& l,float& a) const { a*= l; } |
|---|
| 1588 | inline void rgb(float&,float&,float&) const {} |
|---|
| 1589 | inline void rgba(float& r,float& g,float& b,float& a) const { float l = (r+g+b)*0.3333333; a *= l;} |
|---|
| 1590 | }; |
|---|
| 1591 | |
|---|
| 1592 | struct ModulateAlphaByColourOperator |
|---|
| 1593 | { |
|---|
| 1594 | ModulateAlphaByColourOperator(const osg::Vec4& colour):_colour(colour) { _lum = _colour.length(); } |
|---|
| 1595 | |
|---|
| 1596 | osg::Vec4 _colour; |
|---|
| 1597 | float _lum; |
|---|
| 1598 | |
|---|
| 1599 | inline void luminance(float&) const {} |
|---|
| 1600 | inline void alpha(float&) const {} |
|---|
| 1601 | inline void luminance_alpha(float& l,float& a) const { a*= l*_lum; } |
|---|
| 1602 | inline void rgb(float&,float&,float&) const {} |
|---|
| 1603 | inline void rgba(float& r,float& g,float& b,float& a) const { a = (r*_colour.r()+g*_colour.g()+b*_colour.b()+a*_colour.a()); } |
|---|
| 1604 | }; |
|---|
| 1605 | |
|---|
| 1606 | struct ReplaceAlphaWithLuminanceOperator |
|---|
| 1607 | { |
|---|
| 1608 | ReplaceAlphaWithLuminanceOperator() {} |
|---|
| 1609 | |
|---|
| 1610 | inline void luminance(float&) const {} |
|---|
| 1611 | inline void alpha(float&) const {} |
|---|
| 1612 | inline void luminance_alpha(float& l,float& a) const { a= l; } |
|---|
| 1613 | inline void rgb(float&,float&,float&) const { } |
|---|
| 1614 | inline void rgba(float& r,float& g,float& b,float& a) const { float l = (r+g+b)*0.3333333; a = l; } |
|---|
| 1615 | }; |
|---|
| 1616 | |
|---|
| 1617 | osg::Image* doColourSpaceConversion(ColourSpaceOperation op, osg::Image* image, osg::Vec4& colour) |
|---|
| 1618 | { |
|---|
| 1619 | switch(op) |
|---|
| 1620 | { |
|---|
| 1621 | case (MODULATE_ALPHA_BY_LUMINANCE): |
|---|
| 1622 | { |
|---|
| 1623 | std::cout<<"doing conversion MODULATE_ALPHA_BY_LUMINANCE"<<std::endl; |
|---|
| 1624 | osg::modifyImage(image,ModulateAlphaByLuminanceOperator()); |
|---|
| 1625 | return image; |
|---|
| 1626 | } |
|---|
| 1627 | case (MODULATE_ALPHA_BY_COLOUR): |
|---|
| 1628 | { |
|---|
| 1629 | std::cout<<"doing conversion MODULATE_ALPHA_BY_COLOUR"<<std::endl; |
|---|
| 1630 | osg::modifyImage(image,ModulateAlphaByColourOperator(colour)); |
|---|
| 1631 | return image; |
|---|
| 1632 | } |
|---|
| 1633 | case (REPLACE_ALPHA_WITH_LUMINANACE): |
|---|
| 1634 | { |
|---|
| 1635 | std::cout<<"doing conversion REPLACE_ALPHA_WITH_LUMINANACE"<<std::endl; |
|---|
| 1636 | osg::modifyImage(image,ReplaceAlphaWithLuminanceOperator()); |
|---|
| 1637 | return image; |
|---|
| 1638 | } |
|---|
| 1639 | case (REPLACE_RGB_WITH_LUMINANCE): |
|---|
| 1640 | { |
|---|
| 1641 | std::cout<<"doing conversion REPLACE_ALPHA_WITH_LUMINANACE"<<std::endl; |
|---|
| 1642 | osg::Image* newImage = new osg::Image; |
|---|
| 1643 | newImage->allocateImage(image->s(), image->t(), image->r(), GL_LUMINANCE, image->getDataType()); |
|---|
| 1644 | osg::copyImage(image, 0, 0, 0, image->s(), image->t(), image->r(), |
|---|
| 1645 | newImage, 0, 0, 0, false); |
|---|
| 1646 | return newImage; |
|---|
| 1647 | } |
|---|
| 1648 | default: |
|---|
| 1649 | return image; |
|---|
| 1650 | } |
|---|
| 1651 | } |
|---|
| 1652 | |
|---|
| 1653 | |
|---|
| 1654 | struct ApplyTransferFunctionOperator |
|---|
| 1655 | { |
|---|
| 1656 | ApplyTransferFunctionOperator(osg::TransferFunction1D* tf, unsigned char* data): |
|---|
| 1657 | _tf(tf), |
|---|
| 1658 | _data(data) {} |
|---|
| 1659 | |
|---|
| 1660 | inline void luminance(float l) const |
|---|
| 1661 | { |
|---|
| 1662 | osg::Vec4 c = _tf->getInterpolatedValue(l); |
|---|
| 1663 | |
|---|
| 1664 | *(_data++) = (unsigned char)(c[0]*255.0f + 0.5f); |
|---|
| 1665 | *(_data++) = (unsigned char)(c[1]*255.0f + 0.5f); |
|---|
| 1666 | *(_data++) = (unsigned char)(c[2]*255.0f + 0.5f); |
|---|
| 1667 | *(_data++) = (unsigned char)(c[3]*255.0f + 0.5f); |
|---|
| 1668 | } |
|---|
| 1669 | |
|---|
| 1670 | inline void alpha(float a) const |
|---|
| 1671 | { |
|---|
| 1672 | luminance(a); |
|---|
| 1673 | } |
|---|
| 1674 | |
|---|
| 1675 | inline void luminance_alpha(float l,float a) const |
|---|
| 1676 | { |
|---|
| 1677 | luminance(l); |
|---|
| 1678 | } |
|---|
| 1679 | |
|---|
| 1680 | inline void rgb(float r,float g,float b) const |
|---|
| 1681 | { |
|---|
| 1682 | luminance((r+g+b)*0.3333333); |
|---|
| 1683 | } |
|---|
| 1684 | |
|---|
| 1685 | inline void rgba(float r,float g,float b,float a) const |
|---|
| 1686 | { |
|---|
| 1687 | luminance(a); |
|---|
| 1688 | } |
|---|
| 1689 | |
|---|
| 1690 | mutable osg::ref_ptr<osg::TransferFunction1D> _tf; |
|---|
| 1691 | mutable unsigned char* _data; |
|---|
| 1692 | }; |
|---|
| 1693 | |
|---|
| 1694 | osg::Image* applyTransferFunction(osg::Image* image, osg::TransferFunction1D* transferFunction) |
|---|
| 1695 | { |
|---|
| 1696 | std::cout<<"Applying transfer function"<<std::endl; |
|---|
| 1697 | osg::Image* output_image = new osg::Image; |
|---|
| 1698 | output_image->allocateImage(image->s(),image->t(), image->r(), GL_RGBA, GL_UNSIGNED_BYTE); |
|---|
| 1699 | |
|---|
| 1700 | ApplyTransferFunctionOperator op(transferFunction, output_image->data()); |
|---|
| 1701 | osg::readImage(image,op); |
|---|
| 1702 | |
|---|
| 1703 | return output_image; |
|---|
| 1704 | } |
|---|
| 1705 | |
|---|
| 1706 | osg::TransferFunction1D* readTransferFunctionFile(const std::string& filename) |
|---|
| 1707 | { |
|---|
| 1708 | std::string foundFile = osgDB::findDataFile(filename); |
|---|
| 1709 | if (foundFile.empty()) |
|---|
| 1710 | { |
|---|
| 1711 | std::cout<<"Error: could not find transfer function file : "<<filename<<std::endl; |
|---|
| 1712 | return 0; |
|---|
| 1713 | } |
|---|
| 1714 | |
|---|
| 1715 | std::cout<<"Reading transfer function "<<filename<<std::endl; |
|---|
| 1716 | |
|---|
| 1717 | osg::TransferFunction1D::ValueMap valueMap; |
|---|
| 1718 | osgDB::ifstream fin(foundFile.c_str()); |
|---|
| 1719 | while(fin) |
|---|
| 1720 | { |
|---|
| 1721 | float value, red, green, blue, alpha; |
|---|
| 1722 | fin >> value >> red >> green >> blue >> alpha; |
|---|
| 1723 | if (fin) |
|---|
| 1724 | { |
|---|
| 1725 | std::cout<<"value = "<<value<<" ("<<red<<", "<<green<<", "<<blue<<", "<<alpha<<")"<<std::endl; |
|---|
| 1726 | valueMap[value] = osg::Vec4(red,green,blue,alpha); |
|---|
| 1727 | } |
|---|
| 1728 | } |
|---|
| 1729 | |
|---|
| 1730 | if (valueMap.empty()) |
|---|
| 1731 | { |
|---|
| 1732 | std::cout<<"Error: No values read from transfer function file: "<<filename<<std::endl; |
|---|
| 1733 | return 0; |
|---|
| 1734 | } |
|---|
| 1735 | |
|---|
| 1736 | osg::TransferFunction1D* tf = new osg::TransferFunction1D; |
|---|
| 1737 | tf->assign(valueMap, true); |
|---|
| 1738 | |
|---|
| 1739 | return tf; |
|---|
| 1740 | } |
|---|
| 1741 | |
|---|
| 1742 | |
|---|
| 1743 | class TestSupportOperation: public osg::GraphicsOperation |
|---|
| 1744 | { |
|---|
| 1745 | public: |
|---|
| 1746 | |
|---|
| 1747 | TestSupportOperation(): |
|---|
| 1748 | osg::GraphicsOperation("TestSupportOperation",false), |
|---|
| 1749 | supported(true), |
|---|
| 1750 | errorMessage(), |
|---|
| 1751 | maximumTextureSize(256) {} |
|---|
| 1752 | |
|---|
| 1753 | virtual void operator () (osg::GraphicsContext* gc) |
|---|
| 1754 | { |
|---|
| 1755 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(mutex); |
|---|
| 1756 | |
|---|
| 1757 | glGetIntegerv( GL_MAX_3D_TEXTURE_SIZE, &maximumTextureSize ); |
|---|
| 1758 | |
|---|
| 1759 | osg::notify(osg::NOTICE)<<"Max texture size="<<maximumTextureSize<<std::endl; |
|---|
| 1760 | } |
|---|
| 1761 | |
|---|
| 1762 | OpenThreads::Mutex mutex; |
|---|
| 1763 | bool supported; |
|---|
| 1764 | std::string errorMessage; |
|---|
| 1765 | GLint maximumTextureSize; |
|---|
| 1766 | }; |
|---|
| 1767 | |
|---|
| 1768 | |
|---|
| 1769 | |
|---|
| 1770 | int main( int argc, char **argv ) |
|---|
| 1771 | { |
|---|
| 1772 | |
|---|
| 1773 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 1774 | |
|---|
| 1775 | |
|---|
| 1776 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of 3D textures."); |
|---|
| 1777 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 1778 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 1779 | arguments.getApplicationUsage()->addCommandLineOption("-n","Create normal map for per voxel lighting."); |
|---|
| 1780 | arguments.getApplicationUsage()->addCommandLineOption("-s <numSlices>","Number of slices to create."); |
|---|
| 1781 | arguments.getApplicationUsage()->addCommandLineOption("--images [filenames]","Specify a stack of 2d images to build the 3d volume from."); |
|---|
| 1782 | arguments.getApplicationUsage()->addCommandLineOption("--shader","Use OpenGL Shading Language. (default)"); |
|---|
| 1783 | arguments.getApplicationUsage()->addCommandLineOption("--no-shader","Disable use of OpenGL Shading Language."); |
|---|
| 1784 | arguments.getApplicationUsage()->addCommandLineOption("--gpu-tf","Aply the transfer function on the GPU. (default)"); |
|---|
| 1785 | arguments.getApplicationUsage()->addCommandLineOption("--cpu-tf","Apply the transfer function on the CPU."); |
|---|
| 1786 | arguments.getApplicationUsage()->addCommandLineOption("--mip","Use Maximum Intensity Projection (MIP) filtering."); |
|---|
| 1787 | arguments.getApplicationUsage()->addCommandLineOption("--xSize <size>","Relative width of rendered brick."); |
|---|
| 1788 | arguments.getApplicationUsage()->addCommandLineOption("--ySize <size>","Relative length of rendered brick."); |
|---|
| 1789 | arguments.getApplicationUsage()->addCommandLineOption("--zSize <size>","Relative height of rendered brick."); |
|---|
| 1790 | arguments.getApplicationUsage()->addCommandLineOption("--xMultiplier <multiplier>","Tex coord x mulitplier."); |
|---|
| 1791 | arguments.getApplicationUsage()->addCommandLineOption("--yMultiplier <multiplier>","Tex coord y mulitplier."); |
|---|
| 1792 | arguments.getApplicationUsage()->addCommandLineOption("--zMultiplier <multiplier>","Tex coord z mulitplier."); |
|---|
| 1793 | arguments.getApplicationUsage()->addCommandLineOption("--clip <ratio>","clip volume as a ratio, 0.0 clip all, 1.0 clip none."); |
|---|
| 1794 | arguments.getApplicationUsage()->addCommandLineOption("--maxTextureSize <size>","Set the texture maximum resolution in the s,t,r (x,y,z) dimensions."); |
|---|
| 1795 | arguments.getApplicationUsage()->addCommandLineOption("--s_maxTextureSize <size>","Set the texture maximum resolution in the s (x) dimension."); |
|---|
| 1796 | arguments.getApplicationUsage()->addCommandLineOption("--t_maxTextureSize <size>","Set the texture maximum resolution in the t (y) dimension."); |
|---|
| 1797 | arguments.getApplicationUsage()->addCommandLineOption("--r_maxTextureSize <size>","Set the texture maximum resolution in the r (z) dimension."); |
|---|
| 1798 | arguments.getApplicationUsage()->addCommandLineOption("--compressed","Enable the usage of compressed textures."); |
|---|
| 1799 | arguments.getApplicationUsage()->addCommandLineOption("--compressed-arb","Enable the usage of OpenGL ARB compressed textures."); |
|---|
| 1800 | arguments.getApplicationUsage()->addCommandLineOption("--compressed-dxt1","Enable the usage of S3TC DXT1 compressed textures."); |
|---|
| 1801 | arguments.getApplicationUsage()->addCommandLineOption("--compressed-dxt3","Enable the usage of S3TC DXT3 compressed textures."); |
|---|
| 1802 | arguments.getApplicationUsage()->addCommandLineOption("--compressed-dxt5","Enable the usage of S3TC DXT5 compressed textures."); |
|---|
| 1803 | arguments.getApplicationUsage()->addCommandLineOption("--modulate-alpha-by-luminance","For each pixel multiply the alpha value by the luminance."); |
|---|
| 1804 | arguments.getApplicationUsage()->addCommandLineOption("--replace-alpha-with-luminance","For each pixel set the alpha value to the luminance."); |
|---|
| 1805 | arguments.getApplicationUsage()->addCommandLineOption("--replace-rgb-with-luminance","For each rgb pixel convert to the luminance."); |
|---|
| 1806 | arguments.getApplicationUsage()->addCommandLineOption("--num-components <num>","Set the number of components to in he target image."); |
|---|
| 1807 | arguments.getApplicationUsage()->addCommandLineOption("--no-rescale","Disable the rescaling of the pixel data to 0.0 to 1.0 range"); |
|---|
| 1808 | arguments.getApplicationUsage()->addCommandLineOption("--rescale","Enable the rescale of the pixel data to 0.0 to 1.0 range (default)."); |
|---|
| 1809 | arguments.getApplicationUsage()->addCommandLineOption("--shift-min-to-zero","Shift the pixel data so min value is 0.0."); |
|---|
| 1810 | |
|---|
| 1811 | |
|---|
| 1812 | |
|---|
| 1813 | osgViewer::Viewer viewer(arguments); |
|---|
| 1814 | |
|---|
| 1815 | |
|---|
| 1816 | viewer.addEventHandler(new osgViewer::WindowSizeHandler); |
|---|
| 1817 | |
|---|
| 1818 | { |
|---|
| 1819 | osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; |
|---|
| 1820 | |
|---|
| 1821 | keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); |
|---|
| 1822 | |
|---|
| 1823 | osgGA::FlightManipulator* flightManipulator = new osgGA::FlightManipulator(); |
|---|
| 1824 | flightManipulator->setYawControlMode(osgGA::FlightManipulator::NO_AUTOMATIC_YAW); |
|---|
| 1825 | keyswitchManipulator->addMatrixManipulator( '2', "Flight", flightManipulator ); |
|---|
| 1826 | |
|---|
| 1827 | viewer.setCameraManipulator( keyswitchManipulator.get() ); |
|---|
| 1828 | } |
|---|
| 1829 | |
|---|
| 1830 | |
|---|
| 1831 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 1832 | |
|---|
| 1833 | viewer.getCamera()->setClearColor(osg::Vec4(0.0f,0.0f,0.0f,0.0f)); |
|---|
| 1834 | |
|---|
| 1835 | |
|---|
| 1836 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 1837 | { |
|---|
| 1838 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 1839 | return 1; |
|---|
| 1840 | } |
|---|
| 1841 | |
|---|
| 1842 | std::string outputFile; |
|---|
| 1843 | while (arguments.read("-o",outputFile)) {} |
|---|
| 1844 | |
|---|
| 1845 | |
|---|
| 1846 | |
|---|
| 1847 | osg::ref_ptr<osg::TransferFunction1D> transferFunction; |
|---|
| 1848 | std::string tranferFunctionFile; |
|---|
| 1849 | while (arguments.read("--tf",tranferFunctionFile)) |
|---|
| 1850 | { |
|---|
| 1851 | transferFunction = readTransferFunctionFile(tranferFunctionFile); |
|---|
| 1852 | } |
|---|
| 1853 | |
|---|
| 1854 | unsigned int numSlices=500; |
|---|
| 1855 | while (arguments.read("-s",numSlices)) {} |
|---|
| 1856 | |
|---|
| 1857 | |
|---|
| 1858 | float sliceEnd=1.0f; |
|---|
| 1859 | while (arguments.read("--clip",sliceEnd)) {} |
|---|
| 1860 | |
|---|
| 1861 | float alphaFunc=0.02f; |
|---|
| 1862 | while (arguments.read("--alphaFunc",alphaFunc)) {} |
|---|
| 1863 | |
|---|
| 1864 | |
|---|
| 1865 | |
|---|
| 1866 | ShadingModel shadingModel = Standard; |
|---|
| 1867 | while(arguments.read("--mip")) shadingModel = MaximumIntensityProjection; |
|---|
| 1868 | |
|---|
| 1869 | bool createNormalMap = false; |
|---|
| 1870 | while (arguments.read("-n")) |
|---|
| 1871 | { |
|---|
| 1872 | shadingModel = Light; |
|---|
| 1873 | createNormalMap=true; |
|---|
| 1874 | } |
|---|
| 1875 | |
|---|
| 1876 | while (arguments.read("--isosurface")) |
|---|
| 1877 | { |
|---|
| 1878 | shadingModel = Isosurface; |
|---|
| 1879 | } |
|---|
| 1880 | |
|---|
| 1881 | float xSize=1.0f, ySize=1.0f, zSize=1.0f; |
|---|
| 1882 | while (arguments.read("--xSize",xSize)) {} |
|---|
| 1883 | while (arguments.read("--ySize",ySize)) {} |
|---|
| 1884 | while (arguments.read("--zSize",zSize)) {} |
|---|
| 1885 | |
|---|
| 1886 | float xMultiplier=1.0f, yMultiplier=1.0f, zMultiplier=1.0f; |
|---|
| 1887 | while (arguments.read("--xMultiplier",xMultiplier)) {} |
|---|
| 1888 | while (arguments.read("--yMultiplier",yMultiplier)) {} |
|---|
| 1889 | while (arguments.read("--zMultiplier",zMultiplier)) {} |
|---|
| 1890 | |
|---|
| 1891 | osg::ref_ptr<TestSupportOperation> testSupportOperation = new TestSupportOperation; |
|---|
| 1892 | viewer.setRealizeOperation(testSupportOperation.get()); |
|---|
| 1893 | |
|---|
| 1894 | viewer.realize(); |
|---|
| 1895 | |
|---|
| 1896 | int maximumTextureSize = testSupportOperation->maximumTextureSize; |
|---|
| 1897 | int s_maximumTextureSize = maximumTextureSize; |
|---|
| 1898 | int t_maximumTextureSize = maximumTextureSize; |
|---|
| 1899 | int r_maximumTextureSize = maximumTextureSize; |
|---|
| 1900 | while(arguments.read("--maxTextureSize",maximumTextureSize)) |
|---|
| 1901 | { |
|---|
| 1902 | s_maximumTextureSize = maximumTextureSize; |
|---|
| 1903 | t_maximumTextureSize = maximumTextureSize; |
|---|
| 1904 | r_maximumTextureSize = maximumTextureSize; |
|---|
| 1905 | } |
|---|
| 1906 | while(arguments.read("--s_maxTextureSize",s_maximumTextureSize)) {} |
|---|
| 1907 | while(arguments.read("--t_maxTextureSize",t_maximumTextureSize)) {} |
|---|
| 1908 | while(arguments.read("--r_maxTextureSize",r_maximumTextureSize)) {} |
|---|
| 1909 | |
|---|
| 1910 | osg::Texture::InternalFormatMode internalFormatMode = osg::Texture::USE_IMAGE_DATA_FORMAT; |
|---|
| 1911 | while(arguments.read("--compressed") || arguments.read("--compressed-arb")) { internalFormatMode = osg::Texture::USE_ARB_COMPRESSION; } |
|---|
| 1912 | |
|---|
| 1913 | while(arguments.read("--compressed-dxt1")) { internalFormatMode = osg::Texture::USE_S3TC_DXT1_COMPRESSION; } |
|---|
| 1914 | while(arguments.read("--compressed-dxt3")) { internalFormatMode = osg::Texture::USE_S3TC_DXT3_COMPRESSION; } |
|---|
| 1915 | while(arguments.read("--compressed-dxt5")) { internalFormatMode = osg::Texture::USE_S3TC_DXT5_COMPRESSION; } |
|---|
| 1916 | |
|---|
| 1917 | |
|---|
| 1918 | |
|---|
| 1919 | ColourSpaceOperation colourSpaceOperation = NO_COLOUR_SPACE_OPERATION; |
|---|
| 1920 | osg::Vec4 colourModulate(0.25f,0.25f,0.25f,0.25f); |
|---|
| 1921 | while(arguments.read("--modulate-alpha-by-luminance")) { colourSpaceOperation = MODULATE_ALPHA_BY_LUMINANCE; } |
|---|
| 1922 | while(arguments.read("--modulate-alpha-by-colour", colourModulate.x(),colourModulate.y(),colourModulate.z(),colourModulate.w() )) { colourSpaceOperation = MODULATE_ALPHA_BY_COLOUR; } |
|---|
| 1923 | while(arguments.read("--replace-alpha-with-luminance")) { colourSpaceOperation = REPLACE_ALPHA_WITH_LUMINANACE; } |
|---|
| 1924 | while(arguments.read("--replace-rgb-with-luminance")) { colourSpaceOperation = REPLACE_RGB_WITH_LUMINANCE; } |
|---|
| 1925 | |
|---|
| 1926 | |
|---|
| 1927 | enum RescaleOperation |
|---|
| 1928 | { |
|---|
| 1929 | NO_RESCALE, |
|---|
| 1930 | RESCALE_TO_ZERO_TO_ONE_RANGE, |
|---|
| 1931 | SHIFT_MIN_TO_ZERO |
|---|
| 1932 | }; |
|---|
| 1933 | |
|---|
| 1934 | RescaleOperation rescaleOperation = RESCALE_TO_ZERO_TO_ONE_RANGE; |
|---|
| 1935 | while(arguments.read("--no-rescale")) rescaleOperation = NO_RESCALE; |
|---|
| 1936 | while(arguments.read("--rescale")) rescaleOperation = RESCALE_TO_ZERO_TO_ONE_RANGE; |
|---|
| 1937 | while(arguments.read("--shift-min-to-zero")) rescaleOperation = SHIFT_MIN_TO_ZERO; |
|---|
| 1938 | |
|---|
| 1939 | |
|---|
| 1940 | bool resizeToPowerOfTwo = false; |
|---|
| 1941 | |
|---|
| 1942 | unsigned int numComponentsDesired = 0; |
|---|
| 1943 | while(arguments.read("--num-components", numComponentsDesired)) {} |
|---|
| 1944 | |
|---|
| 1945 | bool useOsgVolume = true; |
|---|
| 1946 | while(arguments.read("--osgVolume")) { useOsgVolume = true; } |
|---|
| 1947 | while(arguments.read("--no-osgVolume")) { useOsgVolume = false; } |
|---|
| 1948 | |
|---|
| 1949 | bool useShader = true; |
|---|
| 1950 | while(arguments.read("--shader")) { useShader = true; } |
|---|
| 1951 | while(arguments.read("--no-shader")) { useShader = true; } |
|---|
| 1952 | |
|---|
| 1953 | bool gpuTransferFunction = true; |
|---|
| 1954 | while(arguments.read("--gpu-tf")) { gpuTransferFunction = true; } |
|---|
| 1955 | while(arguments.read("--cpu-tf")) { gpuTransferFunction = false; } |
|---|
| 1956 | |
|---|
| 1957 | typedef std::list< osg::ref_ptr<osg::Image> > Images; |
|---|
| 1958 | Images images; |
|---|
| 1959 | |
|---|
| 1960 | |
|---|
| 1961 | std::string vh_filename; |
|---|
| 1962 | while (arguments.read("--vh", vh_filename)) |
|---|
| 1963 | { |
|---|
| 1964 | std::string raw_filename, transfer_filename; |
|---|
| 1965 | int xdim(0), ydim(0), zdim(0); |
|---|
| 1966 | |
|---|
| 1967 | osgDB::ifstream header(vh_filename.c_str()); |
|---|
| 1968 | if (header) |
|---|
| 1969 | { |
|---|
| 1970 | header >> raw_filename >> transfer_filename >> xdim >> ydim >> zdim >> xSize >> ySize >> zSize; |
|---|
| 1971 | } |
|---|
| 1972 | |
|---|
| 1973 | if (xdim*ydim*zdim==0) |
|---|
| 1974 | { |
|---|
| 1975 | std::cout<<"Error in reading volume header "<<vh_filename<<std::endl; |
|---|
| 1976 | return 1; |
|---|
| 1977 | } |
|---|
| 1978 | |
|---|
| 1979 | if (!raw_filename.empty()) |
|---|
| 1980 | { |
|---|
| 1981 | images.push_back(readRaw(xdim, ydim, zdim, 1, 1, "little", raw_filename)); |
|---|
| 1982 | } |
|---|
| 1983 | |
|---|
| 1984 | if (!transfer_filename.empty()) |
|---|
| 1985 | { |
|---|
| 1986 | osgDB::ifstream fin(transfer_filename.c_str()); |
|---|
| 1987 | if (fin) |
|---|
| 1988 | { |
|---|
| 1989 | osg::TransferFunction1D::ValueMap valueMap; |
|---|
| 1990 | float value = 0.0; |
|---|
| 1991 | while(fin && value<=1.0) |
|---|
| 1992 | { |
|---|
| 1993 | float red, green, blue, alpha; |
|---|
| 1994 | fin >> red >> green >> blue >> alpha; |
|---|
| 1995 | if (fin) |
|---|
| 1996 | { |
|---|
| 1997 | valueMap[value] = osg::Vec4(red/255.0f,green/255.0f,blue/255.0f,alpha/255.0f); |
|---|
| 1998 | std::cout<<"value = "<<value<<" ("<<red<<", "<<green<<", "<<blue<<", "<<alpha<<")"; |
|---|
| 1999 | std::cout<<" ("<<valueMap[value]<<")"<<std::endl; |
|---|
| 2000 | } |
|---|
| 2001 | value += 1/255.0; |
|---|
| 2002 | } |
|---|
| 2003 | |
|---|
| 2004 | if (valueMap.empty()) |
|---|
| 2005 | { |
|---|
| 2006 | std::cout<<"Error: No values read from transfer function file: "<<transfer_filename<<std::endl; |
|---|
| 2007 | return 0; |
|---|
| 2008 | } |
|---|
| 2009 | |
|---|
| 2010 | transferFunction = new osg::TransferFunction1D; |
|---|
| 2011 | transferFunction->assign(valueMap, true); |
|---|
| 2012 | } |
|---|
| 2013 | } |
|---|
| 2014 | |
|---|
| 2015 | } |
|---|
| 2016 | |
|---|
| 2017 | |
|---|
| 2018 | int sizeX, sizeY, sizeZ, numberBytesPerComponent, numberOfComponents; |
|---|
| 2019 | std::string endian, raw_filename; |
|---|
| 2020 | while (arguments.read("--raw", sizeX, sizeY, sizeZ, numberBytesPerComponent, numberOfComponents, endian, raw_filename)) |
|---|
| 2021 | { |
|---|
| 2022 | images.push_back(readRaw(sizeX, sizeY, sizeZ, numberBytesPerComponent, numberOfComponents, endian, raw_filename)); |
|---|
| 2023 | } |
|---|
| 2024 | |
|---|
| 2025 | int images_pos = arguments.find("--images"); |
|---|
| 2026 | if (images_pos>=0) |
|---|
| 2027 | { |
|---|
| 2028 | ImageList imageList; |
|---|
| 2029 | int pos=images_pos+1; |
|---|
| 2030 | for(;pos<arguments.argc() && !arguments.isOption(pos);++pos) |
|---|
| 2031 | { |
|---|
| 2032 | |
|---|
| 2033 | osg::Image *image = osgDB::readImageFile( arguments[pos]); |
|---|
| 2034 | |
|---|
| 2035 | if(image) |
|---|
| 2036 | { |
|---|
| 2037 | imageList.push_back(image); |
|---|
| 2038 | } |
|---|
| 2039 | } |
|---|
| 2040 | |
|---|
| 2041 | arguments.remove(images_pos, pos-images_pos); |
|---|
| 2042 | |
|---|
| 2043 | |
|---|
| 2044 | ProcessRow processRow; |
|---|
| 2045 | images.push_back(createTexture3D(imageList, processRow, numComponentsDesired, s_maximumTextureSize, t_maximumTextureSize, r_maximumTextureSize, resizeToPowerOfTwo)); |
|---|
| 2046 | } |
|---|
| 2047 | |
|---|
| 2048 | |
|---|
| 2049 | |
|---|
| 2050 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 2051 | |
|---|
| 2052 | |
|---|
| 2053 | if (arguments.errors()) |
|---|
| 2054 | { |
|---|
| 2055 | arguments.writeErrorMessages(std::cout); |
|---|
| 2056 | return 1; |
|---|
| 2057 | } |
|---|
| 2058 | |
|---|
| 2059 | |
|---|
| 2060 | |
|---|
| 2061 | for(int pos=1;pos<arguments.argc();++pos) |
|---|
| 2062 | { |
|---|
| 2063 | if (!arguments.isOption(pos)) |
|---|
| 2064 | { |
|---|
| 2065 | std::string filename = arguments[pos]; |
|---|
| 2066 | if (osgDB::getLowerCaseFileExtension(filename)=="dicom") |
|---|
| 2067 | { |
|---|
| 2068 | |
|---|
| 2069 | osg::Image *image = osgDB::readImageFile(filename); |
|---|
| 2070 | if(image) |
|---|
| 2071 | { |
|---|
| 2072 | images.push_back(image); |
|---|
| 2073 | } |
|---|
| 2074 | } |
|---|
| 2075 | else |
|---|
| 2076 | { |
|---|
| 2077 | osgDB::FileType fileType = osgDB::fileType(filename); |
|---|
| 2078 | if (fileType == osgDB::FILE_NOT_FOUND) |
|---|
| 2079 | { |
|---|
| 2080 | filename = osgDB::findDataFile(filename); |
|---|
| 2081 | fileType = osgDB::fileType(filename); |
|---|
| 2082 | } |
|---|
| 2083 | |
|---|
| 2084 | if (fileType == osgDB::DIRECTORY) |
|---|
| 2085 | { |
|---|
| 2086 | osg::Image *image = osgDB::readImageFile(filename+".dicom"); |
|---|
| 2087 | if(image) |
|---|
| 2088 | { |
|---|
| 2089 | images.push_back(image); |
|---|
| 2090 | } |
|---|
| 2091 | } |
|---|
| 2092 | else if (fileType == osgDB::REGULAR_FILE) |
|---|
| 2093 | { |
|---|
| 2094 | |
|---|
| 2095 | images.push_back(osgDB::readImageFile( filename )); |
|---|
| 2096 | } |
|---|
| 2097 | else |
|---|
| 2098 | { |
|---|
| 2099 | osg::notify(osg::NOTICE)<<"Error: could not find file: "<<filename<<std::endl; |
|---|
| 2100 | return 1; |
|---|
| 2101 | } |
|---|
| 2102 | } |
|---|
| 2103 | } |
|---|
| 2104 | } |
|---|
| 2105 | |
|---|
| 2106 | if (images.empty()) |
|---|
| 2107 | { |
|---|
| 2108 | std::cout<<"No model loaded, please specify and volumetric image file on the command line."<<std::endl; |
|---|
| 2109 | return 1; |
|---|
| 2110 | } |
|---|
| 2111 | |
|---|
| 2112 | |
|---|
| 2113 | Images::iterator sizeItr = images.begin(); |
|---|
| 2114 | xSize = (*sizeItr)->s(); |
|---|
| 2115 | ySize = (*sizeItr)->t(); |
|---|
| 2116 | zSize = (*sizeItr)->r(); |
|---|
| 2117 | ++sizeItr; |
|---|
| 2118 | |
|---|
| 2119 | for(;sizeItr != images.end(); ++sizeItr) |
|---|
| 2120 | { |
|---|
| 2121 | if ((*sizeItr)->s() != xSize || |
|---|
| 2122 | (*sizeItr)->t() != ySize || |
|---|
| 2123 | (*sizeItr)->r() != zSize) |
|---|
| 2124 | { |
|---|
| 2125 | std::cout<<"Images in sequence are not of the same dimensions."<<std::endl; |
|---|
| 2126 | return 1; |
|---|
| 2127 | } |
|---|
| 2128 | } |
|---|
| 2129 | |
|---|
| 2130 | |
|---|
| 2131 | osg::RefMatrix* matrix = dynamic_cast<osg::RefMatrix*>(images.front()->getUserData()); |
|---|
| 2132 | #if 0 |
|---|
| 2133 | if (matrix) |
|---|
| 2134 | { |
|---|
| 2135 | osg::notify(osg::NOTICE)<<"Image has Matrix = "<<*matrix<<std::endl; |
|---|
| 2136 | xSize = xSize * (*matrix)(0,0); |
|---|
| 2137 | ySize = ySize * (*matrix)(1,1); |
|---|
| 2138 | zSize = zSize * (*matrix)(2,2); |
|---|
| 2139 | } |
|---|
| 2140 | #endif |
|---|
| 2141 | |
|---|
| 2142 | osg::Vec4 minValue(FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX); |
|---|
| 2143 | osg::Vec4 maxValue(-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX); |
|---|
| 2144 | bool computeMinMax = false; |
|---|
| 2145 | for(Images::iterator itr = images.begin(); |
|---|
| 2146 | itr != images.end(); |
|---|
| 2147 | ++itr) |
|---|
| 2148 | { |
|---|
| 2149 | osg::Vec4 localMinValue, localMaxValue; |
|---|
| 2150 | if (osg::computeMinMax(itr->get(), localMinValue, localMaxValue)) |
|---|
| 2151 | { |
|---|
| 2152 | if (localMinValue.r()<minValue.r()) minValue.r() = localMinValue.r(); |
|---|
| 2153 | if (localMinValue.g()<minValue.g()) minValue.g() = localMinValue.g(); |
|---|
| 2154 | if (localMinValue.b()<minValue.b()) minValue.b() = localMinValue.b(); |
|---|
| 2155 | if (localMinValue.a()<minValue.a()) minValue.a() = localMinValue.a(); |
|---|
| 2156 | |
|---|
| 2157 | if (localMaxValue.r()>maxValue.r()) maxValue.r() = localMaxValue.r(); |
|---|
| 2158 | if (localMaxValue.g()>maxValue.g()) maxValue.g() = localMaxValue.g(); |
|---|
| 2159 | if (localMaxValue.b()>maxValue.b()) maxValue.b() = localMaxValue.b(); |
|---|
| 2160 | if (localMaxValue.a()>maxValue.a()) maxValue.a() = localMaxValue.a(); |
|---|
| 2161 | |
|---|
| 2162 | osg::notify(osg::NOTICE)<<" ("<<localMinValue<<") ("<<localMaxValue<<") "<<(*itr)->getFileName()<<std::endl; |
|---|
| 2163 | |
|---|
| 2164 | computeMinMax = true; |
|---|
| 2165 | } |
|---|
| 2166 | } |
|---|
| 2167 | |
|---|
| 2168 | if (computeMinMax) |
|---|
| 2169 | { |
|---|
| 2170 | osg::notify(osg::NOTICE)<<"Min value "<<minValue<<std::endl; |
|---|
| 2171 | osg::notify(osg::NOTICE)<<"Max value "<<maxValue<<std::endl; |
|---|
| 2172 | |
|---|
| 2173 | float minComponent = minValue[0]; |
|---|
| 2174 | minComponent = osg::minimum(minComponent,minValue[1]); |
|---|
| 2175 | minComponent = osg::minimum(minComponent,minValue[2]); |
|---|
| 2176 | minComponent = osg::minimum(minComponent,minValue[3]); |
|---|
| 2177 | |
|---|
| 2178 | float maxComponent = maxValue[0]; |
|---|
| 2179 | maxComponent = osg::maximum(maxComponent,maxValue[1]); |
|---|
| 2180 | maxComponent = osg::maximum(maxComponent,maxValue[2]); |
|---|
| 2181 | maxComponent = osg::maximum(maxComponent,maxValue[3]); |
|---|
| 2182 | |
|---|
| 2183 | |
|---|
| 2184 | switch(rescaleOperation) |
|---|
| 2185 | { |
|---|
| 2186 | case(NO_RESCALE): |
|---|
| 2187 | break; |
|---|
| 2188 | |
|---|
| 2189 | case(RESCALE_TO_ZERO_TO_ONE_RANGE): |
|---|
| 2190 | { |
|---|
| 2191 | float scale = 0.99f/(maxComponent-minComponent); |
|---|
| 2192 | float offset = -minComponent * scale; |
|---|
| 2193 | |
|---|
| 2194 | for(Images::iterator itr = images.begin(); |
|---|
| 2195 | itr != images.end(); |
|---|
| 2196 | ++itr) |
|---|
| 2197 | { |
|---|
| 2198 | osg::offsetAndScaleImage(itr->get(), |
|---|
| 2199 | osg::Vec4(offset, offset, offset, offset), |
|---|
| 2200 | osg::Vec4(scale, scale, scale, scale)); |
|---|
| 2201 | } |
|---|
| 2202 | break; |
|---|
| 2203 | } |
|---|
| 2204 | case(SHIFT_MIN_TO_ZERO): |
|---|
| 2205 | { |
|---|
| 2206 | float offset = -minComponent; |
|---|
| 2207 | |
|---|
| 2208 | for(Images::iterator itr = images.begin(); |
|---|
| 2209 | itr != images.end(); |
|---|
| 2210 | ++itr) |
|---|
| 2211 | { |
|---|
| 2212 | osg::offsetAndScaleImage(itr->get(), |
|---|
| 2213 | osg::Vec4(offset, offset, offset, offset), |
|---|
| 2214 | osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f)); |
|---|
| 2215 | } |
|---|
| 2216 | break; |
|---|
| 2217 | } |
|---|
| 2218 | }; |
|---|
| 2219 | |
|---|
| 2220 | } |
|---|
| 2221 | |
|---|
| 2222 | |
|---|
| 2223 | if (colourSpaceOperation!=NO_COLOUR_SPACE_OPERATION) |
|---|
| 2224 | { |
|---|
| 2225 | for(Images::iterator itr = images.begin(); |
|---|
| 2226 | itr != images.end(); |
|---|
| 2227 | ++itr) |
|---|
| 2228 | { |
|---|
| 2229 | (*itr) = doColourSpaceConversion(colourSpaceOperation, itr->get(), colourModulate); |
|---|
| 2230 | } |
|---|
| 2231 | } |
|---|
| 2232 | |
|---|
| 2233 | if (!gpuTransferFunction && transferFunction.valid()) |
|---|
| 2234 | { |
|---|
| 2235 | for(Images::iterator itr = images.begin(); |
|---|
| 2236 | itr != images.end(); |
|---|
| 2237 | ++itr) |
|---|
| 2238 | { |
|---|
| 2239 | *itr = applyTransferFunction(itr->get(), transferFunction.get()); |
|---|
| 2240 | } |
|---|
| 2241 | } |
|---|
| 2242 | |
|---|
| 2243 | osg::ref_ptr<osg::Image> image_3d = 0; |
|---|
| 2244 | |
|---|
| 2245 | if (images.size()==1) |
|---|
| 2246 | { |
|---|
| 2247 | osg::notify(osg::NOTICE)<<"Single image "<<images.size()<<" volumes."<<std::endl; |
|---|
| 2248 | image_3d = images.front(); |
|---|
| 2249 | } |
|---|
| 2250 | else |
|---|
| 2251 | { |
|---|
| 2252 | osg::notify(osg::NOTICE)<<"Creating sequence of "<<images.size()<<" volumes."<<std::endl; |
|---|
| 2253 | |
|---|
| 2254 | osg::ref_ptr<osg::ImageSequence> imageSequence = new osg::ImageSequence; |
|---|
| 2255 | imageSequence->setLength(10.0); |
|---|
| 2256 | image_3d = imageSequence.get(); |
|---|
| 2257 | for(Images::iterator itr = images.begin(); |
|---|
| 2258 | itr != images.end(); |
|---|
| 2259 | ++itr) |
|---|
| 2260 | { |
|---|
| 2261 | imageSequence->addImage(itr->get()); |
|---|
| 2262 | } |
|---|
| 2263 | imageSequence->play(); |
|---|
| 2264 | } |
|---|
| 2265 | |
|---|
| 2266 | osg::ref_ptr<osg::Image> normalmap_3d = 0; |
|---|
| 2267 | if (createNormalMap) |
|---|
| 2268 | { |
|---|
| 2269 | if (images.size()==1) |
|---|
| 2270 | { |
|---|
| 2271 | normalmap_3d = createNormalMapTexture(images.front().get()); |
|---|
| 2272 | } |
|---|
| 2273 | else |
|---|
| 2274 | { |
|---|
| 2275 | osg::ref_ptr<osg::ImageSequence> normalmapSequence = new osg::ImageSequence; |
|---|
| 2276 | normalmap_3d = normalmapSequence.get(); |
|---|
| 2277 | for(Images::iterator itr = images.begin(); |
|---|
| 2278 | itr != images.end(); |
|---|
| 2279 | ++itr) |
|---|
| 2280 | { |
|---|
| 2281 | normalmapSequence->addImage(createNormalMapTexture(itr->get())); |
|---|
| 2282 | } |
|---|
| 2283 | normalmapSequence->play(); |
|---|
| 2284 | } |
|---|
| 2285 | } |
|---|
| 2286 | |
|---|
| 2287 | |
|---|
| 2288 | osg::ref_ptr<osg::Node> rootNode = 0; |
|---|
| 2289 | |
|---|
| 2290 | if (useOsgVolume) |
|---|
| 2291 | { |
|---|
| 2292 | |
|---|
| 2293 | osg::ref_ptr<osgVolume::Volume> volume = new osgVolume::Volume; |
|---|
| 2294 | osg::ref_ptr<osgVolume::VolumeTile> tile = new osgVolume::VolumeTile; |
|---|
| 2295 | volume->addChild(tile); |
|---|
| 2296 | |
|---|
| 2297 | osg::ref_ptr<osgVolume::Layer> layer = new osgVolume::ImageLayer(image_3d); |
|---|
| 2298 | layer->setProperty(new osgVolume::TransferFunctionProperty(transferFunction.get())); |
|---|
| 2299 | |
|---|
| 2300 | if (matrix) |
|---|
| 2301 | { |
|---|
| 2302 | osgVolume::Locator* locator = new osgVolume::Locator(*matrix); |
|---|
| 2303 | layer->setLocator(locator); |
|---|
| 2304 | tile->setLocator(locator); |
|---|
| 2305 | } |
|---|
| 2306 | |
|---|
| 2307 | tile->setLayer(layer.get()); |
|---|
| 2308 | |
|---|
| 2309 | if (useShader) |
|---|
| 2310 | { |
|---|
| 2311 | switch(shadingModel) |
|---|
| 2312 | { |
|---|
| 2313 | case(Standard): |
|---|
| 2314 | break; |
|---|
| 2315 | case(Light): |
|---|
| 2316 | layer->addProperty(new osgVolume::LightingProperty); |
|---|
| 2317 | break; |
|---|
| 2318 | case(Isosurface): |
|---|
| 2319 | layer->addProperty(new osgVolume::IsoSurfaceProperty(alphaFunc)); |
|---|
| 2320 | break; |
|---|
| 2321 | case(MaximumIntensityProjection): |
|---|
| 2322 | layer->addProperty(new osgVolume::MaximumIntensityProjectionProperty); |
|---|
| 2323 | break; |
|---|
| 2324 | } |
|---|
| 2325 | |
|---|
| 2326 | layer->addProperty(new osgVolume::AlphaFuncProperty(alphaFunc)); |
|---|
| 2327 | |
|---|
| 2328 | tile->setVolumeTechnique(new osgVolume::ShaderTechnique); |
|---|
| 2329 | } |
|---|
| 2330 | else |
|---|
| 2331 | { |
|---|
| 2332 | tile->setVolumeTechnique(new osgVolume::FixedFunctionTechnique); |
|---|
| 2333 | } |
|---|
| 2334 | |
|---|
| 2335 | |
|---|
| 2336 | rootNode = volume.get(); |
|---|
| 2337 | |
|---|
| 2338 | } |
|---|
| 2339 | else |
|---|
| 2340 | { |
|---|
| 2341 | if (useShader) |
|---|
| 2342 | { |
|---|
| 2343 | rootNode = createShaderModel(shadingModel, |
|---|
| 2344 | image_3d, normalmap_3d.get(), |
|---|
| 2345 | (gpuTransferFunction ? transferFunction.get() : 0), |
|---|
| 2346 | internalFormatMode, |
|---|
| 2347 | xSize, ySize, zSize, |
|---|
| 2348 | xMultiplier, yMultiplier, zMultiplier, |
|---|
| 2349 | numSlices, sliceEnd, alphaFunc); |
|---|
| 2350 | } |
|---|
| 2351 | else |
|---|
| 2352 | { |
|---|
| 2353 | rootNode = createModel(shadingModel, |
|---|
| 2354 | image_3d, normalmap_3d, |
|---|
| 2355 | internalFormatMode, |
|---|
| 2356 | xSize, ySize, zSize, |
|---|
| 2357 | xMultiplier, yMultiplier, zMultiplier, |
|---|
| 2358 | numSlices, sliceEnd, alphaFunc); |
|---|
| 2359 | } |
|---|
| 2360 | |
|---|
| 2361 | if (matrix && rootNode) |
|---|
| 2362 | { |
|---|
| 2363 | osg::MatrixTransform* mt = new osg::MatrixTransform; |
|---|
| 2364 | mt->setMatrix(*matrix); |
|---|
| 2365 | mt->addChild(rootNode); |
|---|
| 2366 | |
|---|
| 2367 | rootNode = mt; |
|---|
| 2368 | } |
|---|
| 2369 | } |
|---|
| 2370 | |
|---|
| 2371 | if (!outputFile.empty()) |
|---|
| 2372 | { |
|---|
| 2373 | std::string ext = osgDB::getFileExtension(outputFile); |
|---|
| 2374 | std::string name_no_ext = osgDB::getNameLessExtension(outputFile); |
|---|
| 2375 | if (ext=="osg") |
|---|
| 2376 | { |
|---|
| 2377 | if (image_3d.valid()) |
|---|
| 2378 | { |
|---|
| 2379 | image_3d->setFileName(name_no_ext + ".dds"); |
|---|
| 2380 | osgDB::writeImageFile(*image_3d, image_3d->getFileName()); |
|---|
| 2381 | } |
|---|
| 2382 | if (normalmap_3d.valid()) |
|---|
| 2383 | { |
|---|
| 2384 | normalmap_3d->setFileName(name_no_ext + "_normalmap.dds"); |
|---|
| 2385 | osgDB::writeImageFile(*normalmap_3d, normalmap_3d->getFileName()); |
|---|
| 2386 | } |
|---|
| 2387 | |
|---|
| 2388 | osgDB::writeNodeFile(*rootNode, outputFile); |
|---|
| 2389 | } |
|---|
| 2390 | else if (ext=="ive") |
|---|
| 2391 | { |
|---|
| 2392 | osgDB::writeNodeFile(*rootNode, outputFile); |
|---|
| 2393 | } |
|---|
| 2394 | else if (ext=="dds") |
|---|
| 2395 | { |
|---|
| 2396 | osgDB::writeImageFile(*image_3d, outputFile); |
|---|
| 2397 | } |
|---|
| 2398 | else |
|---|
| 2399 | { |
|---|
| 2400 | std::cout<<"Extension not support for file output, not file written."<<std::endl; |
|---|
| 2401 | } |
|---|
| 2402 | |
|---|
| 2403 | return 0; |
|---|
| 2404 | } |
|---|
| 2405 | |
|---|
| 2406 | |
|---|
| 2407 | if (rootNode) |
|---|
| 2408 | { |
|---|
| 2409 | |
|---|
| 2410 | |
|---|
| 2411 | viewer.setSceneData(rootNode.get()); |
|---|
| 2412 | |
|---|
| 2413 | |
|---|
| 2414 | viewer.run(); |
|---|
| 2415 | } |
|---|
| 2416 | |
|---|
| 2417 | return 0; |
|---|
| 2418 | |
|---|
| 2419 | } |
|---|