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