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