| 1 | #include <osg/Node> |
|---|
| 2 | #include <osg/Geometry> |
|---|
| 3 | #include <osg/Notify> |
|---|
| 4 | #include <osg/Texture3D> |
|---|
| 5 | #include <osg/TexGen> |
|---|
| 6 | #include <osg/Geode> |
|---|
| 7 | #include <osg/Billboard> |
|---|
| 8 | #include <osg/PositionAttitudeTransform> |
|---|
| 9 | #include <osg/ClipNode> |
|---|
| 10 | #include <osg/AlphaFunc> |
|---|
| 11 | #include <osg/TexGenNode> |
|---|
| 12 | #include <osg/TexEnv> |
|---|
| 13 | #include <osg/TexEnvCombine> |
|---|
| 14 | #include <osg/Material> |
|---|
| 15 | #include <osg/Endian> |
|---|
| 16 | |
|---|
| 17 | #include <osgDB/Registry> |
|---|
| 18 | #include <osgDB/ReadFile> |
|---|
| 19 | #include <osgDB/WriteFile> |
|---|
| 20 | #include <osgDB/FileNameUtils> |
|---|
| 21 | |
|---|
| 22 | #include <osgUtil/CullVisitor> |
|---|
| 23 | |
|---|
| 24 | #include <osgProducer/Viewer> |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | typedef std::vector< osg::ref_ptr<osg::Image> > ImageList; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | template <typename T, class O> |
|---|
| 41 | void _readRow(unsigned int num, GLenum pixelFormat, T* data,float scale, const O& operation) |
|---|
| 42 | { |
|---|
| 43 | switch(pixelFormat) |
|---|
| 44 | { |
|---|
| 45 | case(GL_LUMINANCE): { for(unsigned int i=0;i<num;++i) { float l = float(*data++)*scale; operation.luminance(l); } } break; |
|---|
| 46 | case(GL_ALPHA): { for(unsigned int i=0;i<num;++i) { float a = float(*data++)*scale; operation.alpha(a); } } break; |
|---|
| 47 | 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; |
|---|
| 48 | 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; |
|---|
| 49 | 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; |
|---|
| 50 | 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; |
|---|
| 51 | 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; |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | template <class O> |
|---|
| 56 | void readRow(unsigned int num, GLenum pixelFormat, GLenum dataType, unsigned char* data, const O& operation) |
|---|
| 57 | { |
|---|
| 58 | switch(dataType) |
|---|
| 59 | { |
|---|
| 60 | case(GL_BYTE): _readRow(num,pixelFormat, (char*)data, 1.0f/128.0f, operation); break; |
|---|
| 61 | case(GL_UNSIGNED_BYTE): _readRow(num,pixelFormat, (unsigned char*)data, 1.0f/255.0f, operation); break; |
|---|
| 62 | case(GL_SHORT): _readRow(num,pixelFormat, (short*) data, 1.0f/32768.0f, operation); break; |
|---|
| 63 | case(GL_UNSIGNED_SHORT): _readRow(num,pixelFormat, (unsigned short*)data, 1.0f/65535.0f, operation); break; |
|---|
| 64 | case(GL_INT): _readRow(num,pixelFormat, (int*) data, 1.0f/2147483648.0f, operation); break; |
|---|
| 65 | case(GL_UNSIGNED_INT): _readRow(num,pixelFormat, (unsigned int*) data, 1.0f/4294967295.0f, operation); break; |
|---|
| 66 | case(GL_FLOAT): _readRow(num,pixelFormat, (float*) data, 1.0f, operation); break; |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | template <class O> |
|---|
| 71 | void readImage(osg::Image* image, const O& operation) |
|---|
| 72 | { |
|---|
| 73 | if (!image) return; |
|---|
| 74 | |
|---|
| 75 | for(int r=0;r<image->r();++r) |
|---|
| 76 | { |
|---|
| 77 | for(int t=0;t<image->t();++t) |
|---|
| 78 | { |
|---|
| 79 | readRow(image->s(), image->getPixelFormat(), image->getDataType(), image->data(0,t,r), operation); |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | template <typename T, class M> |
|---|
| 96 | void _modifyRow(unsigned int num, GLenum pixelFormat, T* data,float scale, const M& operation) |
|---|
| 97 | { |
|---|
| 98 | float inv_scale = 1.0f/scale; |
|---|
| 99 | switch(pixelFormat) |
|---|
| 100 | { |
|---|
| 101 | 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; |
|---|
| 102 | 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; |
|---|
| 103 | 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; |
|---|
| 104 | 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; |
|---|
| 105 | 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; |
|---|
| 106 | 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; |
|---|
| 107 | 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; |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | template <class M> |
|---|
| 112 | void modifyRow(unsigned int num, GLenum pixelFormat, GLenum dataType, unsigned char* data, const M& operation) |
|---|
| 113 | { |
|---|
| 114 | switch(dataType) |
|---|
| 115 | { |
|---|
| 116 | case(GL_BYTE): _modifyRow(num,pixelFormat, (char*)data, 1.0f/128.0f, operation); break; |
|---|
| 117 | case(GL_UNSIGNED_BYTE): _modifyRow(num,pixelFormat, (unsigned char*)data, 1.0f/255.0f, operation); break; |
|---|
| 118 | case(GL_SHORT): _modifyRow(num,pixelFormat, (short*) data, 1.0f/32768.0f, operation); break; |
|---|
| 119 | case(GL_UNSIGNED_SHORT): _modifyRow(num,pixelFormat, (unsigned short*)data, 1.0f/65535.0f, operation); break; |
|---|
| 120 | case(GL_INT): _modifyRow(num,pixelFormat, (int*) data, 1.0f/2147483648.0f, operation); break; |
|---|
| 121 | case(GL_UNSIGNED_INT): _modifyRow(num,pixelFormat, (unsigned int*) data, 1.0f/4294967295.0f, operation); break; |
|---|
| 122 | case(GL_FLOAT): _modifyRow(num,pixelFormat, (float*) data, 1.0f, operation); break; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | template <class M> |
|---|
| 127 | void modifyImage(osg::Image* image, const M& operation) |
|---|
| 128 | { |
|---|
| 129 | if (!image) return; |
|---|
| 130 | |
|---|
| 131 | for(int r=0;r<image->r();++r) |
|---|
| 132 | { |
|---|
| 133 | for(int t=0;t<image->t();++t) |
|---|
| 134 | { |
|---|
| 135 | modifyRow(image->s(), image->getPixelFormat(), image->getDataType(), image->data(0,t,r), operation); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | struct PassThroughTransformFunction |
|---|
| 141 | { |
|---|
| 142 | unsigned char operator() (unsigned char c) const { return c; } |
|---|
| 143 | }; |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | struct ProcessRow |
|---|
| 147 | { |
|---|
| 148 | virtual void operator() (unsigned int num, |
|---|
| 149 | GLenum source_pixelFormat, unsigned char* source, |
|---|
| 150 | GLenum dest_pixelFormat, unsigned char* dest) const |
|---|
| 151 | { |
|---|
| 152 | switch(source_pixelFormat) |
|---|
| 153 | { |
|---|
| 154 | case(GL_LUMINANCE): |
|---|
| 155 | case(GL_ALPHA): |
|---|
| 156 | switch(dest_pixelFormat) |
|---|
| 157 | { |
|---|
| 158 | case(GL_LUMINANCE): |
|---|
| 159 | case(GL_ALPHA): A_to_A(num, source, dest); break; |
|---|
| 160 | case(GL_LUMINANCE_ALPHA): A_to_LA(num, source, dest); break; |
|---|
| 161 | case(GL_RGB): A_to_RGB(num, source, dest); break; |
|---|
| 162 | case(GL_RGBA): A_to_RGBA(num, source, dest); break; |
|---|
| 163 | } |
|---|
| 164 | break; |
|---|
| 165 | case(GL_LUMINANCE_ALPHA): |
|---|
| 166 | switch(dest_pixelFormat) |
|---|
| 167 | { |
|---|
| 168 | case(GL_LUMINANCE): |
|---|
| 169 | case(GL_ALPHA): LA_to_A(num, source, dest); break; |
|---|
| 170 | case(GL_LUMINANCE_ALPHA): LA_to_LA(num, source, dest); break; |
|---|
| 171 | case(GL_RGB): LA_to_RGB(num, source, dest); break; |
|---|
| 172 | case(GL_RGBA): LA_to_RGBA(num, source, dest); break; |
|---|
| 173 | } |
|---|
| 174 | break; |
|---|
| 175 | case(GL_RGB): |
|---|
| 176 | switch(dest_pixelFormat) |
|---|
| 177 | { |
|---|
| 178 | case(GL_LUMINANCE): |
|---|
| 179 | case(GL_ALPHA): RGB_to_A(num, source, dest); break; |
|---|
| 180 | case(GL_LUMINANCE_ALPHA): RGB_to_LA(num, source, dest); break; |
|---|
| 181 | case(GL_RGB): RGB_to_RGB(num, source, dest); break; |
|---|
| 182 | case(GL_RGBA): RGB_to_RGBA(num, source, dest); break; |
|---|
| 183 | } |
|---|
| 184 | break; |
|---|
| 185 | case(GL_RGBA): |
|---|
| 186 | switch(dest_pixelFormat) |
|---|
| 187 | { |
|---|
| 188 | case(GL_LUMINANCE): |
|---|
| 189 | case(GL_ALPHA): RGBA_to_A(num, source, dest); break; |
|---|
| 190 | case(GL_LUMINANCE_ALPHA): RGBA_to_LA(num, source, dest); break; |
|---|
| 191 | case(GL_RGB): RGBA_to_RGB(num, source, dest); break; |
|---|
| 192 | case(GL_RGBA): RGBA_to_RGBA(num, source, dest); break; |
|---|
| 193 | } |
|---|
| 194 | break; |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | virtual void A_to_A(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 201 | { |
|---|
| 202 | for(unsigned int i=0;i<num;++i) |
|---|
| 203 | { |
|---|
| 204 | *dest++ = *source++; |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | virtual void A_to_LA(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 209 | { |
|---|
| 210 | for(unsigned int i=0;i<num;++i) |
|---|
| 211 | { |
|---|
| 212 | *dest++ = *source; |
|---|
| 213 | *dest++ = *source++; |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | virtual void A_to_RGB(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 218 | { |
|---|
| 219 | for(unsigned int i=0;i<num;++i) |
|---|
| 220 | { |
|---|
| 221 | *dest++ = *source; |
|---|
| 222 | *dest++ = *source; |
|---|
| 223 | *dest++ = *source++; |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | virtual void A_to_RGBA(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 228 | { |
|---|
| 229 | for(unsigned int i=0;i<num;++i) |
|---|
| 230 | { |
|---|
| 231 | *dest++ = *source; |
|---|
| 232 | *dest++ = *source; |
|---|
| 233 | *dest++ = *source; |
|---|
| 234 | *dest++ = *source++; |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | virtual void LA_to_A(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 241 | { |
|---|
| 242 | for(unsigned int i=0;i<num;++i) |
|---|
| 243 | { |
|---|
| 244 | ++source; |
|---|
| 245 | *dest++ = *source++; |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | virtual void LA_to_LA(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 | } |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | virtual void LA_to_RGB(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 259 | { |
|---|
| 260 | for(unsigned int i=0;i<num;++i) |
|---|
| 261 | { |
|---|
| 262 | *dest++ = *source; |
|---|
| 263 | *dest++ = *source; |
|---|
| 264 | *dest++ = *source; |
|---|
| 265 | source+=2; |
|---|
| 266 | } |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | virtual void LA_to_RGBA(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 270 | { |
|---|
| 271 | for(unsigned int i=0;i<num;++i) |
|---|
| 272 | { |
|---|
| 273 | *dest++ = *source; |
|---|
| 274 | *dest++ = *source; |
|---|
| 275 | *dest++ = *source++; |
|---|
| 276 | *dest++ = *source++; |
|---|
| 277 | } |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | virtual void RGB_to_A(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 283 | { |
|---|
| 284 | for(unsigned int i=0;i<num;++i) |
|---|
| 285 | { |
|---|
| 286 | unsigned char val = *source; |
|---|
| 287 | *dest++ = val; |
|---|
| 288 | source += 3; |
|---|
| 289 | } |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | virtual void RGB_to_LA(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 293 | { |
|---|
| 294 | for(unsigned int i=0;i<num;++i) |
|---|
| 295 | { |
|---|
| 296 | unsigned char val = *source; |
|---|
| 297 | *dest++ = val; |
|---|
| 298 | *dest++ = val; |
|---|
| 299 | source += 3; |
|---|
| 300 | } |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | virtual void RGB_to_RGB(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 304 | { |
|---|
| 305 | for(unsigned int i=0;i<num;++i) |
|---|
| 306 | { |
|---|
| 307 | *dest++ = *source++; |
|---|
| 308 | *dest++ = *source++; |
|---|
| 309 | *dest++ = *source++; |
|---|
| 310 | } |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | virtual void RGB_to_RGBA(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 314 | { |
|---|
| 315 | for(unsigned int i=0;i<num;++i) |
|---|
| 316 | { |
|---|
| 317 | unsigned char val = *source; |
|---|
| 318 | *dest++ = *source++; |
|---|
| 319 | *dest++ = *source++; |
|---|
| 320 | *dest++ = *source++; |
|---|
| 321 | *dest++ = val; |
|---|
| 322 | } |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | virtual void RGBA_to_A(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 328 | { |
|---|
| 329 | for(unsigned int i=0;i<num;++i) |
|---|
| 330 | { |
|---|
| 331 | source += 3; |
|---|
| 332 | *dest++ = *source++; |
|---|
| 333 | } |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | virtual void RGBA_to_LA(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 337 | { |
|---|
| 338 | for(unsigned int i=0;i<num;++i) |
|---|
| 339 | { |
|---|
| 340 | unsigned char val = *source; |
|---|
| 341 | source += 3; |
|---|
| 342 | *dest++ = val; |
|---|
| 343 | *dest++ = *source++; |
|---|
| 344 | } |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | virtual void RGBA_to_RGB(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 348 | { |
|---|
| 349 | for(unsigned int i=0;i<num;++i) |
|---|
| 350 | { |
|---|
| 351 | *dest++ = *source++; |
|---|
| 352 | *dest++ = *source++; |
|---|
| 353 | *dest++ = *source++; |
|---|
| 354 | ++source; |
|---|
| 355 | } |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | virtual void RGBA_to_RGBA(unsigned int num, unsigned char* source, unsigned char* dest) const |
|---|
| 359 | { |
|---|
| 360 | for(unsigned int i=0;i<num;++i) |
|---|
| 361 | { |
|---|
| 362 | *dest++ = *source++; |
|---|
| 363 | *dest++ = *source++; |
|---|
| 364 | *dest++ = *source++; |
|---|
| 365 | *dest++ = *source++; |
|---|
| 366 | } |
|---|
| 367 | } |
|---|
| 368 | }; |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | void clampToNearestValidPowerOfTwo(int& sizeX, int& sizeY, int& sizeZ, int s_maximumTextureSize, int t_maximumTextureSize, int r_maximumTextureSize) |
|---|
| 372 | { |
|---|
| 373 | |
|---|
| 374 | int s_nearestPowerOfTwo = 1; |
|---|
| 375 | while(s_nearestPowerOfTwo<sizeX && s_nearestPowerOfTwo<s_maximumTextureSize) s_nearestPowerOfTwo*=2; |
|---|
| 376 | |
|---|
| 377 | int t_nearestPowerOfTwo = 1; |
|---|
| 378 | while(t_nearestPowerOfTwo<sizeY && t_nearestPowerOfTwo<t_maximumTextureSize) t_nearestPowerOfTwo*=2; |
|---|
| 379 | |
|---|
| 380 | int r_nearestPowerOfTwo = 1; |
|---|
| 381 | while(r_nearestPowerOfTwo<sizeZ && r_nearestPowerOfTwo<r_maximumTextureSize) r_nearestPowerOfTwo*=2; |
|---|
| 382 | |
|---|
| 383 | sizeX = s_nearestPowerOfTwo; |
|---|
| 384 | sizeY = t_nearestPowerOfTwo; |
|---|
| 385 | sizeZ = r_nearestPowerOfTwo; |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | osg::Image* createTexture3D(ImageList& imageList, ProcessRow& processRow, |
|---|
| 389 | unsigned int numComponentsDesired, |
|---|
| 390 | int s_maximumTextureSize, |
|---|
| 391 | int t_maximumTextureSize, |
|---|
| 392 | int r_maximumTextureSize ) |
|---|
| 393 | { |
|---|
| 394 | int max_s = 0; |
|---|
| 395 | int max_t = 0; |
|---|
| 396 | unsigned int max_components = 0; |
|---|
| 397 | int total_r = 0; |
|---|
| 398 | ImageList::iterator itr; |
|---|
| 399 | for(itr=imageList.begin(); |
|---|
| 400 | itr!=imageList.end(); |
|---|
| 401 | ++itr) |
|---|
| 402 | { |
|---|
| 403 | osg::Image* image = itr->get(); |
|---|
| 404 | GLenum pixelFormat = image->getPixelFormat(); |
|---|
| 405 | if (pixelFormat==GL_ALPHA || |
|---|
| 406 | pixelFormat==GL_LUMINANCE || |
|---|
| 407 | pixelFormat==GL_LUMINANCE_ALPHA || |
|---|
| 408 | pixelFormat==GL_RGB || |
|---|
| 409 | pixelFormat==GL_RGBA) |
|---|
| 410 | { |
|---|
| 411 | max_s = osg::maximum(image->s(), max_s); |
|---|
| 412 | max_t = osg::maximum(image->t(), max_t); |
|---|
| 413 | max_components = osg::maximum(osg::Image::computeNumComponents(pixelFormat), max_components); |
|---|
| 414 | total_r += image->r(); |
|---|
| 415 | } |
|---|
| 416 | else |
|---|
| 417 | { |
|---|
| 418 | osg::notify(osg::NOTICE)<<"Image "<<image->getFileName()<<" has unsuitable pixel format"<< std::hex<< pixelFormat << std::dec << std::endl; |
|---|
| 419 | } |
|---|
| 420 | } |
|---|
| 421 | |
|---|
| 422 | if (numComponentsDesired!=0) max_components = numComponentsDesired; |
|---|
| 423 | |
|---|
| 424 | GLenum desiredPixelFormat = 0; |
|---|
| 425 | switch(max_components) |
|---|
| 426 | { |
|---|
| 427 | case(1): |
|---|
| 428 | osg::notify(osg::NOTICE)<<"desiredPixelFormat = GL_LUMINANCE" << std::endl; |
|---|
| 429 | desiredPixelFormat = GL_LUMINANCE; |
|---|
| 430 | break; |
|---|
| 431 | case(2): |
|---|
| 432 | osg::notify(osg::NOTICE)<<"desiredPixelFormat = GL_LUMINANCE_ALPHA" << std::endl; |
|---|
| 433 | desiredPixelFormat = GL_LUMINANCE_ALPHA; |
|---|
| 434 | break; |
|---|
| 435 | case(3): |
|---|
| 436 | osg::notify(osg::NOTICE)<<"desiredPixelFormat = GL_RGB" << std::endl; |
|---|
| 437 | desiredPixelFormat = GL_RGB; |
|---|
| 438 | break; |
|---|
| 439 | case(4): |
|---|
| 440 | osg::notify(osg::NOTICE)<<"desiredPixelFormat = GL_RGBA" << std::endl; |
|---|
| 441 | desiredPixelFormat = GL_RGBA; |
|---|
| 442 | break; |
|---|
| 443 | } |
|---|
| 444 | if (desiredPixelFormat==0) return 0; |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | int s_nearestPowerOfTwo = 1; |
|---|
| 448 | while(s_nearestPowerOfTwo<max_s && s_nearestPowerOfTwo<s_maximumTextureSize) s_nearestPowerOfTwo*=2; |
|---|
| 449 | |
|---|
| 450 | int t_nearestPowerOfTwo = 1; |
|---|
| 451 | while(t_nearestPowerOfTwo<max_t && t_nearestPowerOfTwo<t_maximumTextureSize) t_nearestPowerOfTwo*=2; |
|---|
| 452 | |
|---|
| 453 | int r_nearestPowerOfTwo = 1; |
|---|
| 454 | while(r_nearestPowerOfTwo<total_r && r_nearestPowerOfTwo<r_maximumTextureSize) r_nearestPowerOfTwo*=2; |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | osg::notify(osg::NOTICE)<<"max image width = "<<max_s<<" nearest power of two = "<<s_nearestPowerOfTwo<<std::endl; |
|---|
| 458 | osg::notify(osg::NOTICE)<<"max image height = "<<max_t<<" nearest power of two = "<<t_nearestPowerOfTwo<<std::endl; |
|---|
| 459 | osg::notify(osg::NOTICE)<<"max image depth = "<<total_r<<" nearest power of two = "<<r_nearestPowerOfTwo<<std::endl; |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | osg::ref_ptr<osg::Image> image_3d = new osg::Image; |
|---|
| 463 | image_3d->allocateImage(s_nearestPowerOfTwo,t_nearestPowerOfTwo,r_nearestPowerOfTwo, |
|---|
| 464 | desiredPixelFormat,GL_UNSIGNED_BYTE); |
|---|
| 465 | |
|---|
| 466 | |
|---|
| 467 | unsigned int r_offset = (total_r<r_nearestPowerOfTwo) ? r_nearestPowerOfTwo/2 - total_r/2 : 0; |
|---|
| 468 | |
|---|
| 469 | int curr_dest_r = r_offset; |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | for(itr=imageList.begin(); |
|---|
| 473 | itr!=imageList.end(); |
|---|
| 474 | ++itr) |
|---|
| 475 | { |
|---|
| 476 | osg::Image* image = itr->get(); |
|---|
| 477 | GLenum pixelFormat = image->getPixelFormat(); |
|---|
| 478 | if (pixelFormat==GL_ALPHA || |
|---|
| 479 | pixelFormat==GL_LUMINANCE || |
|---|
| 480 | pixelFormat==GL_LUMINANCE_ALPHA || |
|---|
| 481 | pixelFormat==GL_RGB || |
|---|
| 482 | pixelFormat==GL_RGBA) |
|---|
| 483 | { |
|---|
| 484 | |
|---|
| 485 | int num_r = osg::minimum(image->r(), (image_3d->r() - curr_dest_r)); |
|---|
| 486 | int num_t = osg::minimum(image->t(), image_3d->t()); |
|---|
| 487 | int num_s = osg::minimum(image->s(), image_3d->s()); |
|---|
| 488 | |
|---|
| 489 | unsigned int s_offset_dest = (image->s()<s_nearestPowerOfTwo) ? s_nearestPowerOfTwo/2 - image->s()/2 : 0; |
|---|
| 490 | unsigned int t_offset_dest = (image->t()<t_nearestPowerOfTwo) ? t_nearestPowerOfTwo/2 - image->t()/2 : 0; |
|---|
| 491 | |
|---|
| 492 | for(int r=0;r<num_r;++r, ++curr_dest_r) |
|---|
| 493 | { |
|---|
| 494 | for(int t=0;t<num_t;++t) |
|---|
| 495 | { |
|---|
| 496 | unsigned char* dest = image_3d->data(s_offset_dest,t+t_offset_dest,curr_dest_r); |
|---|
| 497 | unsigned char* source = image->data(0,t,r); |
|---|
| 498 | |
|---|
| 499 | processRow(num_s, image->getPixelFormat(), source, image_3d->getPixelFormat(), dest); |
|---|
| 500 | } |
|---|
| 501 | } |
|---|
| 502 | } |
|---|
| 503 | } |
|---|
| 504 | return image_3d.release(); |
|---|
| 505 | } |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | osg::Image* createNormalMapTexture(osg::Image* image_3d) |
|---|
| 509 | { |
|---|
| 510 | unsigned int sourcePixelIncrement = 1; |
|---|
| 511 | unsigned int alphaOffset = 0; |
|---|
| 512 | switch(image_3d->getPixelFormat()) |
|---|
| 513 | { |
|---|
| 514 | case(GL_ALPHA): |
|---|
| 515 | case(GL_LUMINANCE): |
|---|
| 516 | sourcePixelIncrement = 1; |
|---|
| 517 | alphaOffset = 0; |
|---|
| 518 | break; |
|---|
| 519 | case(GL_LUMINANCE_ALPHA): |
|---|
| 520 | sourcePixelIncrement = 2; |
|---|
| 521 | alphaOffset = 1; |
|---|
| 522 | break; |
|---|
| 523 | case(GL_RGB): |
|---|
| 524 | sourcePixelIncrement = 3; |
|---|
| 525 | alphaOffset = 0; |
|---|
| 526 | break; |
|---|
| 527 | case(GL_RGBA): |
|---|
| 528 | sourcePixelIncrement = 4; |
|---|
| 529 | alphaOffset = 3; |
|---|
| 530 | break; |
|---|
| 531 | default: |
|---|
| 532 | osg::notify(osg::NOTICE)<<"Source pixel format not support for normal map generation."<<std::endl; |
|---|
| 533 | return 0; |
|---|
| 534 | } |
|---|
| 535 | |
|---|
| 536 | osg::ref_ptr<osg::Image> normalmap_3d = new osg::Image; |
|---|
| 537 | normalmap_3d->allocateImage(image_3d->s(),image_3d->t(),image_3d->r(), |
|---|
| 538 | GL_RGBA,GL_UNSIGNED_BYTE); |
|---|
| 539 | |
|---|
| 540 | if (osg::getCpuByteOrder()==osg::LittleEndian) alphaOffset = sourcePixelIncrement-alphaOffset-1; |
|---|
| 541 | |
|---|
| 542 | for(int r=1;r<image_3d->r()-1;++r) |
|---|
| 543 | { |
|---|
| 544 | for(int t=1;t<image_3d->t()-1;++t) |
|---|
| 545 | { |
|---|
| 546 | unsigned char* ptr = image_3d->data(1,t,r)+alphaOffset; |
|---|
| 547 | unsigned char* left = image_3d->data(0,t,r)+alphaOffset; |
|---|
| 548 | unsigned char* right = image_3d->data(2,t,r)+alphaOffset; |
|---|
| 549 | unsigned char* above = image_3d->data(1,t+1,r)+alphaOffset; |
|---|
| 550 | unsigned char* below = image_3d->data(1,t-1,r)+alphaOffset; |
|---|
| 551 | unsigned char* in = image_3d->data(1,t,r+1)+alphaOffset; |
|---|
| 552 | unsigned char* out = image_3d->data(1,t,r-1)+alphaOffset; |
|---|
| 553 | |
|---|
| 554 | unsigned char* destination = (unsigned char*) normalmap_3d->data(1,t,r); |
|---|
| 555 | |
|---|
| 556 | for(int s=1;s<image_3d->s()-1;++s) |
|---|
| 557 | { |
|---|
| 558 | |
|---|
| 559 | osg::Vec3 grad((float)(*left)-(float)(*right), |
|---|
| 560 | (float)(*below)-(float)(*above), |
|---|
| 561 | (float)(*out) -(float)(*in)); |
|---|
| 562 | |
|---|
| 563 | grad.normalize(); |
|---|
| 564 | |
|---|
| 565 | if (grad.x()==0.0f && grad.y()==0.0f && grad.z()==0.0f) |
|---|
| 566 | { |
|---|
| 567 | grad.set(128.0f,128.0f,128.0f); |
|---|
| 568 | } |
|---|
| 569 | else |
|---|
| 570 | { |
|---|
| 571 | grad.x() = osg::clampBetween((grad.x()+1.0f)*128.0f,0.0f,255.0f); |
|---|
| 572 | grad.y() = osg::clampBetween((grad.y()+1.0f)*128.0f,0.0f,255.0f); |
|---|
| 573 | grad.z() = osg::clampBetween((grad.z()+1.0f)*128.0f,0.0f,255.0f); |
|---|
| 574 | } |
|---|
| 575 | |
|---|
| 576 | *(destination++) = (unsigned char)(grad.x()); |
|---|
| 577 | *(destination++) = (unsigned char)(grad.y()); |
|---|
| 578 | *(destination++) = (unsigned char)(grad.z()); |
|---|
| 579 | |
|---|
| 580 | *destination++ = *ptr; |
|---|
| 581 | |
|---|
| 582 | ptr += sourcePixelIncrement; |
|---|
| 583 | left += sourcePixelIncrement; |
|---|
| 584 | right += sourcePixelIncrement; |
|---|
| 585 | above += sourcePixelIncrement; |
|---|
| 586 | below += sourcePixelIncrement; |
|---|
| 587 | in += sourcePixelIncrement; |
|---|
| 588 | out += sourcePixelIncrement; |
|---|
| 589 | } |
|---|
| 590 | } |
|---|
| 591 | } |
|---|
| 592 | |
|---|
| 593 | return normalmap_3d.release(); |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| 596 | |
|---|
| 597 | |
|---|
| 598 | osg::Node* createCube(float size,float alpha, unsigned int numSlices, float sliceEnd=1.0f) |
|---|
| 599 | { |
|---|
| 600 | |
|---|
| 601 | |
|---|
| 602 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 603 | |
|---|
| 604 | float halfSize = size*0.5f; |
|---|
| 605 | float y = halfSize; |
|---|
| 606 | float dy =-size/(float)(numSlices-1)*sliceEnd; |
|---|
| 607 | |
|---|
| 608 | |
|---|
| 609 | |
|---|
| 610 | |
|---|
| 611 | osg::Vec3Array* coords = new osg::Vec3Array(4*numSlices); |
|---|
| 612 | geom->setVertexArray(coords); |
|---|
| 613 | for(unsigned int i=0;i<numSlices;++i, y+=dy) |
|---|
| 614 | { |
|---|
| 615 | (*coords)[i*4+0].set(-halfSize,y,halfSize); |
|---|
| 616 | (*coords)[i*4+1].set(-halfSize,y,-halfSize); |
|---|
| 617 | (*coords)[i*4+2].set(halfSize,y,-halfSize); |
|---|
| 618 | (*coords)[i*4+3].set(halfSize,y,halfSize); |
|---|
| 619 | } |
|---|
| 620 | |
|---|
| 621 | osg::Vec3Array* normals = new osg::Vec3Array(1); |
|---|
| 622 | (*normals)[0].set(0.0f,-1.0f,0.0f); |
|---|
| 623 | geom->setNormalArray(normals); |
|---|
| 624 | geom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 625 | |
|---|
| 626 | osg::Vec4Array* colors = new osg::Vec4Array(1); |
|---|
| 627 | (*colors)[0].set(1.0f,1.0f,1.0f,alpha); |
|---|
| 628 | geom->setColorArray(colors); |
|---|
| 629 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 630 | |
|---|
| 631 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,coords->size())); |
|---|
| 632 | |
|---|
| 633 | osg::Billboard* billboard = new osg::Billboard; |
|---|
| 634 | billboard->setMode(osg::Billboard::POINT_ROT_WORLD); |
|---|
| 635 | billboard->addDrawable(geom); |
|---|
| 636 | billboard->setPosition(0,osg::Vec3(0.0f,0.0f,0.0f)); |
|---|
| 637 | |
|---|
| 638 | return billboard; |
|---|
| 639 | } |
|---|
| 640 | |
|---|
| 641 | osg::Node* createModel(osg::ref_ptr<osg::Image>& image_3d, osg::ref_ptr<osg::Image>& normalmap_3d, |
|---|
| 642 | osg::Texture::InternalFormatMode internalFormatMode, |
|---|
| 643 | float xSize, float ySize, float zSize, |
|---|
| 644 | float xMultiplier, float yMultiplier, float zMultiplier, |
|---|
| 645 | unsigned int numSlices=500, float sliceEnd=1.0f, float alphaFuncValue=0.02f) |
|---|
| 646 | { |
|---|
| 647 | bool two_pass = normalmap_3d.valid() && (image_3d->getPixelFormat()==GL_RGB || image_3d->getPixelFormat()==GL_RGBA); |
|---|
| 648 | |
|---|
| 649 | osg::Group* group = new osg::Group; |
|---|
| 650 | |
|---|
| 651 | osg::TexGenNode* texgenNode_0 = new osg::TexGenNode; |
|---|
| 652 | texgenNode_0->setTextureUnit(0); |
|---|
| 653 | texgenNode_0->getTexGen()->setMode(osg::TexGen::EYE_LINEAR); |
|---|
| 654 | texgenNode_0->getTexGen()->setPlane(osg::TexGen::S, osg::Vec4(xMultiplier,0.0f,0.0f,0.5f)); |
|---|
| 655 | texgenNode_0->getTexGen()->setPlane(osg::TexGen::T, osg::Vec4(0.0f,yMultiplier,0.0f,0.5f)); |
|---|
| 656 | texgenNode_0->getTexGen()->setPlane(osg::TexGen::R, osg::Vec4(0.0f,0.0f,zMultiplier,0.5f)); |
|---|
| 657 | |
|---|
| 658 | if (two_pass) |
|---|
| 659 | { |
|---|
| 660 | osg::TexGenNode* texgenNode_1 = new osg::TexGenNode; |
|---|
| 661 | texgenNode_1->setTextureUnit(1); |
|---|
| 662 | texgenNode_1->getTexGen()->setMode(osg::TexGen::EYE_LINEAR); |
|---|
| 663 | texgenNode_1->getTexGen()->setPlane(osg::TexGen::S, texgenNode_0->getTexGen()->getPlane(osg::TexGen::S)); |
|---|
| 664 | texgenNode_1->getTexGen()->setPlane(osg::TexGen::T, texgenNode_0->getTexGen()->getPlane(osg::TexGen::T)); |
|---|
| 665 | texgenNode_1->getTexGen()->setPlane(osg::TexGen::R, texgenNode_0->getTexGen()->getPlane(osg::TexGen::R)); |
|---|
| 666 | |
|---|
| 667 | texgenNode_1->addChild(texgenNode_0); |
|---|
| 668 | |
|---|
| 669 | group->addChild(texgenNode_1); |
|---|
| 670 | } |
|---|
| 671 | else |
|---|
| 672 | { |
|---|
| 673 | group->addChild(texgenNode_0); |
|---|
| 674 | } |
|---|
| 675 | |
|---|
| 676 | osg::BoundingBox bb(-xSize*0.5f,-ySize*0.5f,-zSize*0.5f,xSize*0.5f,ySize*0.5f,zSize*0.5f); |
|---|
| 677 | |
|---|
| 678 | osg::ClipNode* clipnode = new osg::ClipNode; |
|---|
| 679 | clipnode->addChild(createCube(1.0f,1.0f, numSlices,sliceEnd)); |
|---|
| 680 | clipnode->createClipBox(bb); |
|---|
| 681 | |
|---|
| 682 | { |
|---|
| 683 | |
|---|
| 684 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 685 | |
|---|
| 686 | osg::Vec3Array* coords = new osg::Vec3Array(); |
|---|
| 687 | coords->push_back(bb.corner(0)); |
|---|
| 688 | coords->push_back(bb.corner(1)); |
|---|
| 689 | coords->push_back(bb.corner(2)); |
|---|
| 690 | coords->push_back(bb.corner(3)); |
|---|
| 691 | coords->push_back(bb.corner(4)); |
|---|
| 692 | coords->push_back(bb.corner(5)); |
|---|
| 693 | coords->push_back(bb.corner(6)); |
|---|
| 694 | coords->push_back(bb.corner(7)); |
|---|
| 695 | |
|---|
| 696 | geom->setVertexArray(coords); |
|---|
| 697 | |
|---|
| 698 | osg::Vec4Array* colors = new osg::Vec4Array(1); |
|---|
| 699 | (*colors)[0].set(1.0f,1.0f,1.0f,1.0f); |
|---|
| 700 | geom->setColorArray(colors); |
|---|
| 701 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 702 | |
|---|
| 703 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,coords->size())); |
|---|
| 704 | |
|---|
| 705 | osg::Geode* geode = new osg::Geode; |
|---|
| 706 | geode->addDrawable(geom); |
|---|
| 707 | |
|---|
| 708 | clipnode->addChild(geode); |
|---|
| 709 | |
|---|
| 710 | } |
|---|
| 711 | |
|---|
| 712 | texgenNode_0->addChild(clipnode); |
|---|
| 713 | |
|---|
| 714 | osg::StateSet* stateset = texgenNode_0->getOrCreateStateSet(); |
|---|
| 715 | |
|---|
| 716 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::ON); |
|---|
| 717 | stateset->setMode(GL_BLEND,osg::StateAttribute::ON); |
|---|
| 718 | stateset->setAttribute(new osg::AlphaFunc(osg::AlphaFunc::GREATER,alphaFuncValue)); |
|---|
| 719 | |
|---|
| 720 | osg::Material* material = new osg::Material; |
|---|
| 721 | material->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 722 | stateset->setAttributeAndModes(material); |
|---|
| 723 | |
|---|
| 724 | osg::Vec3 lightDirection(1.0f,-1.0f,1.0f); |
|---|
| 725 | lightDirection.normalize(); |
|---|
| 726 | |
|---|
| 727 | if (normalmap_3d.valid()) |
|---|
| 728 | { |
|---|
| 729 | if (two_pass) |
|---|
| 730 | { |
|---|
| 731 | |
|---|
| 732 | |
|---|
| 733 | osg::Texture3D* bump_texture3D = new osg::Texture3D; |
|---|
| 734 | bump_texture3D->setFilter(osg::Texture3D::MIN_FILTER,osg::Texture3D::LINEAR); |
|---|
| 735 | bump_texture3D->setFilter(osg::Texture3D::MAG_FILTER,osg::Texture3D::LINEAR); |
|---|
| 736 | bump_texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP); |
|---|
| 737 | bump_texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP); |
|---|
| 738 | bump_texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP); |
|---|
| 739 | bump_texture3D->setImage(normalmap_3d.get()); |
|---|
| 740 | |
|---|
| 741 | bump_texture3D->setInternalFormatMode(internalFormatMode); |
|---|
| 742 | |
|---|
| 743 | stateset->setTextureAttributeAndModes(0,bump_texture3D,osg::StateAttribute::ON); |
|---|
| 744 | |
|---|
| 745 | osg::TexEnvCombine* tec = new osg::TexEnvCombine; |
|---|
| 746 | tec->setConstantColorAsLightDirection(lightDirection); |
|---|
| 747 | |
|---|
| 748 | tec->setCombine_RGB(osg::TexEnvCombine::DOT3_RGB); |
|---|
| 749 | tec->setSource0_RGB(osg::TexEnvCombine::CONSTANT); |
|---|
| 750 | tec->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 751 | tec->setSource1_RGB(osg::TexEnvCombine::TEXTURE); |
|---|
| 752 | tec->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 753 | |
|---|
| 754 | tec->setCombine_Alpha(osg::TexEnvCombine::REPLACE); |
|---|
| 755 | tec->setSource0_Alpha(osg::TexEnvCombine::PRIMARY_COLOR); |
|---|
| 756 | tec->setOperand0_Alpha(osg::TexEnvCombine::SRC_ALPHA); |
|---|
| 757 | tec->setSource1_Alpha(osg::TexEnvCombine::TEXTURE); |
|---|
| 758 | tec->setOperand1_Alpha(osg::TexEnvCombine::SRC_ALPHA); |
|---|
| 759 | |
|---|
| 760 | stateset->setTextureAttributeAndModes(0, tec, osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
|---|
| 761 | |
|---|
| 762 | stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); |
|---|
| 763 | stateset->setTextureMode(0,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); |
|---|
| 764 | stateset->setTextureMode(0,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); |
|---|
| 765 | |
|---|
| 766 | |
|---|
| 767 | |
|---|
| 768 | osg::Texture3D* texture3D = new osg::Texture3D; |
|---|
| 769 | texture3D->setFilter(osg::Texture3D::MIN_FILTER,osg::Texture3D::LINEAR); |
|---|
| 770 | texture3D->setFilter(osg::Texture3D::MAG_FILTER,osg::Texture3D::LINEAR); |
|---|
| 771 | texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP); |
|---|
| 772 | texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP); |
|---|
| 773 | texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP); |
|---|
| 774 | if (image_3d->getPixelFormat()==GL_ALPHA || |
|---|
| 775 | image_3d->getPixelFormat()==GL_LUMINANCE) |
|---|
| 776 | { |
|---|
| 777 | texture3D->setInternalFormatMode(osg::Texture3D::USE_USER_DEFINED_FORMAT); |
|---|
| 778 | texture3D->setInternalFormat(GL_INTENSITY); |
|---|
| 779 | } |
|---|
| 780 | else |
|---|
| 781 | { |
|---|
| 782 | texture3D->setInternalFormatMode(internalFormatMode); |
|---|
| 783 | } |
|---|
| 784 | texture3D->setImage(image_3d.get()); |
|---|
| 785 | |
|---|
| 786 | stateset->setTextureAttributeAndModes(1,texture3D,osg::StateAttribute::ON); |
|---|
| 787 | |
|---|
| 788 | stateset->setTextureMode(1,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); |
|---|
| 789 | stateset->setTextureMode(1,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); |
|---|
| 790 | stateset->setTextureMode(1,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); |
|---|
| 791 | |
|---|
| 792 | stateset->setTextureAttributeAndModes(1,new osg::TexEnv(),osg::StateAttribute::ON); |
|---|
| 793 | |
|---|
| 794 | } |
|---|
| 795 | else |
|---|
| 796 | { |
|---|
| 797 | osg::ref_ptr<osg::Image> normalmap_3d = createNormalMapTexture(image_3d.get()); |
|---|
| 798 | osg::Texture3D* bump_texture3D = new osg::Texture3D; |
|---|
| 799 | bump_texture3D->setFilter(osg::Texture3D::MIN_FILTER,osg::Texture3D::LINEAR); |
|---|
| 800 | bump_texture3D->setFilter(osg::Texture3D::MAG_FILTER,osg::Texture3D::LINEAR); |
|---|
| 801 | bump_texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP); |
|---|
| 802 | bump_texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP); |
|---|
| 803 | bump_texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP); |
|---|
| 804 | bump_texture3D->setImage(normalmap_3d.get()); |
|---|
| 805 | |
|---|
| 806 | bump_texture3D->setInternalFormatMode(internalFormatMode); |
|---|
| 807 | |
|---|
| 808 | stateset->setTextureAttributeAndModes(0,bump_texture3D,osg::StateAttribute::ON); |
|---|
| 809 | |
|---|
| 810 | osg::TexEnvCombine* tec = new osg::TexEnvCombine; |
|---|
| 811 | tec->setConstantColorAsLightDirection(lightDirection); |
|---|
| 812 | |
|---|
| 813 | tec->setCombine_RGB(osg::TexEnvCombine::DOT3_RGB); |
|---|
| 814 | tec->setSource0_RGB(osg::TexEnvCombine::CONSTANT); |
|---|
| 815 | tec->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 816 | tec->setSource1_RGB(osg::TexEnvCombine::TEXTURE); |
|---|
| 817 | tec->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 818 | |
|---|
| 819 | tec->setCombine_Alpha(osg::TexEnvCombine::MODULATE); |
|---|
| 820 | tec->setSource0_Alpha(osg::TexEnvCombine::PRIMARY_COLOR); |
|---|
| 821 | tec->setOperand0_Alpha(osg::TexEnvCombine::SRC_ALPHA); |
|---|
| 822 | tec->setSource1_Alpha(osg::TexEnvCombine::TEXTURE); |
|---|
| 823 | tec->setOperand1_Alpha(osg::TexEnvCombine::SRC_ALPHA); |
|---|
| 824 | |
|---|
| 825 | stateset->setTextureAttributeAndModes(0, tec, osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); |
|---|
| 826 | |
|---|
| 827 | stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); |
|---|
| 828 | stateset->setTextureMode(0,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); |
|---|
| 829 | stateset->setTextureMode(0,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); |
|---|
| 830 | |
|---|
| 831 | image_3d = normalmap_3d; |
|---|
| 832 | } |
|---|
| 833 | } |
|---|
| 834 | else |
|---|
| 835 | { |
|---|
| 836 | |
|---|
| 837 | |
|---|
| 838 | |
|---|
| 839 | |
|---|
| 840 | osg::Texture3D* texture3D = new osg::Texture3D; |
|---|
| 841 | texture3D->setFilter(osg::Texture3D::MIN_FILTER,osg::Texture3D::LINEAR); |
|---|
| 842 | texture3D->setFilter(osg::Texture3D::MAG_FILTER,osg::Texture3D::LINEAR); |
|---|
| 843 | texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP); |
|---|
| 844 | texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP); |
|---|
| 845 | texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP); |
|---|
| 846 | if (image_3d->getPixelFormat()==GL_ALPHA || |
|---|
| 847 | image_3d->getPixelFormat()==GL_LUMINANCE) |
|---|
| 848 | { |
|---|
| 849 | texture3D->setInternalFormatMode(osg::Texture3D::USE_USER_DEFINED_FORMAT); |
|---|
| 850 | texture3D->setInternalFormat(GL_INTENSITY); |
|---|
| 851 | } |
|---|
| 852 | else |
|---|
| 853 | { |
|---|
| 854 | texture3D->setInternalFormatMode(internalFormatMode); |
|---|
| 855 | } |
|---|
| 856 | |
|---|
| 857 | texture3D->setImage(image_3d.get()); |
|---|
| 858 | |
|---|
| 859 | stateset->setTextureAttributeAndModes(0,texture3D,osg::StateAttribute::ON); |
|---|
| 860 | |
|---|
| 861 | stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); |
|---|
| 862 | stateset->setTextureMode(0,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); |
|---|
| 863 | stateset->setTextureMode(0,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); |
|---|
| 864 | |
|---|
| 865 | stateset->setTextureAttributeAndModes(0,new osg::TexEnv(),osg::StateAttribute::ON); |
|---|
| 866 | } |
|---|
| 867 | |
|---|
| 868 | return group; |
|---|
| 869 | } |
|---|
| 870 | |
|---|
| 871 | struct FindRangeOperator |
|---|
| 872 | { |
|---|
| 873 | FindRangeOperator(): |
|---|
| 874 | _rmin(FLT_MAX), |
|---|
| 875 | _rmax(-FLT_MAX), |
|---|
| 876 | _gmin(FLT_MAX), |
|---|
| 877 | _gmax(-FLT_MAX), |
|---|
| 878 | _bmin(FLT_MAX), |
|---|
| 879 | _bmax(-FLT_MAX), |
|---|
| 880 | _amin(FLT_MAX), |
|---|
| 881 | _amax(-FLT_MAX) {} |
|---|
| 882 | |
|---|
| 883 | mutable float _rmin, _rmax, _gmin, _gmax, _bmin, _bmax, _amin, _amax; |
|---|
| 884 | |
|---|
| 885 | inline void luminance(float l) const { rgb(l,l,l); } |
|---|
| 886 | inline void alpha(float a) const { _amin = osg::minimum(a,_amin); _amax = osg::maximum(a,_amax); } |
|---|
| 887 | inline void luminance_alpha(float l,float a) const { rgb(l,l,l); alpha(a); } |
|---|
| 888 | 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); } |
|---|
| 889 | inline void rgba(float r,float g,float b,float a) const { rgb(r,g,b); alpha(a); } |
|---|
| 890 | }; |
|---|
| 891 | |
|---|
| 892 | struct ScaleOperator |
|---|
| 893 | { |
|---|
| 894 | ScaleOperator():_scale(1.0f) {} |
|---|
| 895 | ScaleOperator(float scale):_scale(scale) {} |
|---|
| 896 | ScaleOperator(const ScaleOperator& so):_scale(so._scale) {} |
|---|
| 897 | |
|---|
| 898 | ScaleOperator& operator = (const ScaleOperator& so) { _scale = so._scale; return *this; } |
|---|
| 899 | |
|---|
| 900 | float _scale; |
|---|
| 901 | |
|---|
| 902 | inline void luminance(float& l) const { l*= _scale; } |
|---|
| 903 | inline void alpha(float& a) const { a*= _scale; } |
|---|
| 904 | inline void luminance_alpha(float& l,float& a) const { l*= _scale; a*= _scale; } |
|---|
| 905 | inline void rgb(float& r,float& g,float& b) const { r*= _scale; g*=_scale; b*=_scale; } |
|---|
| 906 | inline void rgba(float& r,float& g,float& b,float& a) const { r*= _scale; g*=_scale; b*=_scale; a*=_scale; } |
|---|
| 907 | }; |
|---|
| 908 | |
|---|
| 909 | struct RecordRowOperator |
|---|
| 910 | { |
|---|
| 911 | RecordRowOperator(unsigned int num):_colours(num),_pos(0) {} |
|---|
| 912 | |
|---|
| 913 | mutable std::vector<osg::Vec4> _colours; |
|---|
| 914 | mutable unsigned int _pos; |
|---|
| 915 | |
|---|
| 916 | inline void luminance(float l) const { rgba(l,l,l,1.0f); } |
|---|
| 917 | inline void alpha(float a) const { rgba(1.0f,1.0f,1.0f,a); } |
|---|
| 918 | inline void luminance_alpha(float l,float a) const { rgba(l,l,l,a); } |
|---|
| 919 | inline void rgb(float r,float g,float b) const { rgba(r,g,b,1.0f); } |
|---|
| 920 | inline void rgba(float r,float g,float b,float a) const { _colours[_pos++].set(r,g,b,a); } |
|---|
| 921 | }; |
|---|
| 922 | |
|---|
| 923 | struct WriteRowOperator |
|---|
| 924 | { |
|---|
| 925 | WriteRowOperator():_pos(0) {} |
|---|
| 926 | WriteRowOperator(unsigned int num):_colours(num),_pos(0) {} |
|---|
| 927 | |
|---|
| 928 | std::vector<osg::Vec4> _colours; |
|---|
| 929 | mutable unsigned int _pos; |
|---|
| 930 | |
|---|
| 931 | inline void luminance(float& l) const { l = _colours[_pos++].r(); } |
|---|
| 932 | inline void alpha(float& a) const { a = _colours[_pos++].a(); } |
|---|
| 933 | inline void luminance_alpha(float& l,float& a) const { l = _colours[_pos].r(); a = _colours[_pos++].a(); } |
|---|
| 934 | inline void rgb(float& r,float& g,float& b) const { r = _colours[_pos].r(); g = _colours[_pos].g(); b = _colours[_pos].b(); } |
|---|
| 935 | 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(); } |
|---|
| 936 | }; |
|---|
| 937 | |
|---|
| 938 | osg::Image* readRaw(int sizeX, int sizeY, int sizeZ, int numberBytesPerComponent, int numberOfComponents, const std::string& endian, const std::string& raw_filename) |
|---|
| 939 | { |
|---|
| 940 | std::ifstream fin(raw_filename.c_str()); |
|---|
| 941 | if (!fin) return 0; |
|---|
| 942 | |
|---|
| 943 | GLenum pixelFormat; |
|---|
| 944 | switch(numberOfComponents) |
|---|
| 945 | { |
|---|
| 946 | case 1 : pixelFormat = GL_LUMINANCE; break; |
|---|
| 947 | case 2 : pixelFormat = GL_LUMINANCE_ALPHA; break; |
|---|
| 948 | case 3 : pixelFormat = GL_RGB; break; |
|---|
| 949 | case 4 : pixelFormat = GL_RGBA; break; |
|---|
| 950 | default : |
|---|
| 951 | osg::notify(osg::NOTICE)<<"Error: numberOfComponents="<<numberOfComponents<<" not supported, only 1,2,3 or 4 are supported."<<std::endl; |
|---|
| 952 | return 0; |
|---|
| 953 | } |
|---|
| 954 | |
|---|
| 955 | |
|---|
| 956 | GLenum dataType; |
|---|
| 957 | switch(numberBytesPerComponent) |
|---|
| 958 | { |
|---|
| 959 | case 1 : dataType = GL_UNSIGNED_BYTE; break; |
|---|
| 960 | case 2 : dataType = GL_UNSIGNED_SHORT; break; |
|---|
| 961 | case 4 : dataType = GL_UNSIGNED_INT; break; |
|---|
| 962 | default : |
|---|
| 963 | osg::notify(osg::NOTICE)<<"Error: numberBytesPerComponent="<<numberBytesPerComponent<<" not supported, only 1,2 or 4 are supported."<<std::endl; |
|---|
| 964 | return 0; |
|---|
| 965 | } |
|---|
| 966 | |
|---|
| 967 | int s_maximumTextureSize=256, t_maximumTextureSize=256, r_maximumTextureSize=256; |
|---|
| 968 | |
|---|
| 969 | int sizeS = sizeX; |
|---|
| 970 | int sizeT = sizeY; |
|---|
| 971 | int sizeR = sizeZ; |
|---|
| 972 | clampToNearestValidPowerOfTwo(sizeS, sizeT, sizeR, s_maximumTextureSize, t_maximumTextureSize, r_maximumTextureSize); |
|---|
| 973 | |
|---|
| 974 | osg::ref_ptr<osg::Image> image = new osg::Image; |
|---|
| 975 | image->allocateImage(sizeS, sizeT, sizeR, pixelFormat, dataType); |
|---|
| 976 | |
|---|
| 977 | |
|---|
| 978 | bool endianSwap = (osg::getCpuByteOrder()==osg::BigEndian) ? (endian!="big") : (endian=="big"); |
|---|
| 979 | |
|---|
| 980 | unsigned int r_offset = (sizeZ<sizeR) ? sizeR/2 - sizeZ/2 : 0; |
|---|
| 981 | |
|---|
| 982 | int offset = endianSwap ? numberBytesPerComponent : 0; |
|---|
| 983 | int delta = endianSwap ? -1 : 1; |
|---|
| 984 | for(int r=0;r<sizeZ;++r) |
|---|
| 985 | { |
|---|
| 986 | for(int t=0;t<sizeY;++t) |
|---|
| 987 | { |
|---|
| 988 | char* data = (char*) image->data(0,t,r+r_offset); |
|---|
| 989 | for(int s=0;s<sizeX;++s) |
|---|
| 990 | { |
|---|
| 991 | if (!fin) return 0; |
|---|
| 992 | |
|---|
| 993 | for(int c=0;c<numberOfComponents;++c) |
|---|
| 994 | { |
|---|
| 995 | char* ptr = data+offset; |
|---|
| 996 | for(int b=0;b<numberBytesPerComponent;++b) |
|---|
| 997 | { |
|---|
| 998 | fin.read((char*)ptr, 1); |
|---|
| 999 | ptr += delta; |
|---|
| 1000 | } |
|---|
| 1001 | data += numberBytesPerComponent; |
|---|
| 1002 | } |
|---|
| 1003 | } |
|---|
| 1004 | } |
|---|
| 1005 | } |
|---|
| 1006 | |
|---|
| 1007 | |
|---|
| 1008 | |
|---|
| 1009 | { |
|---|
| 1010 | |
|---|
| 1011 | FindRangeOperator rangeOp; |
|---|
| 1012 | readImage(image.get(), rangeOp); |
|---|
| 1013 | modifyImage(image.get(),ScaleOperator(1.0f/rangeOp._rmax)); |
|---|
| 1014 | } |
|---|
| 1015 | |
|---|
| 1016 | |
|---|
| 1017 | fin.close(); |
|---|
| 1018 | |
|---|
| 1019 | if (dataType!=GL_UNSIGNED_BYTE) |
|---|
| 1020 | { |
|---|
| 1021 | |
|---|
| 1022 | |
|---|
| 1023 | osg::ref_ptr<osg::Image> new_image = new osg::Image; |
|---|
| 1024 | new_image->allocateImage(sizeS, sizeT, sizeR, pixelFormat, GL_UNSIGNED_BYTE); |
|---|
| 1025 | |
|---|
| 1026 | RecordRowOperator readOp(sizeS); |
|---|
| 1027 | WriteRowOperator writeOp; |
|---|
| 1028 | |
|---|
| 1029 | for(int r=0;r<sizeR;++r) |
|---|
| 1030 | { |
|---|
| 1031 | for(int t=0;t<sizeT;++t) |
|---|
| 1032 | { |
|---|
| 1033 | |
|---|
| 1034 | readOp._pos = 0; |
|---|
| 1035 | writeOp._pos = 0; |
|---|
| 1036 | |
|---|
| 1037 | |
|---|
| 1038 | readRow(sizeS, pixelFormat, dataType, image->data(0,t,r), readOp); |
|---|
| 1039 | |
|---|
| 1040 | |
|---|
| 1041 | writeOp._colours.swap(readOp._colours); |
|---|
| 1042 | |
|---|
| 1043 | modifyRow(sizeS, pixelFormat, GL_UNSIGNED_BYTE, new_image->data(0,t,r), writeOp); |
|---|
| 1044 | |
|---|
| 1045 | |
|---|
| 1046 | writeOp._colours.swap(readOp._colours); |
|---|
| 1047 | } |
|---|
| 1048 | } |
|---|
| 1049 | |
|---|
| 1050 | image = new_image; |
|---|
| 1051 | } |
|---|
| 1052 | |
|---|
| 1053 | return image.release(); |
|---|
| 1054 | |
|---|
| 1055 | |
|---|
| 1056 | } |
|---|
| 1057 | |
|---|
| 1058 | enum ColourSpaceOperation |
|---|
| 1059 | { |
|---|
| 1060 | NO_COLOUR_SPACE_OPERATION, |
|---|
| 1061 | MODULATE_ALPHA_BY_LUMINANCE, |
|---|
| 1062 | MODULATE_ALPHA_BY_COLOUR, |
|---|
| 1063 | REPLACE_ALPHA_WITH_LUMINACE |
|---|
| 1064 | }; |
|---|
| 1065 | |
|---|
| 1066 | struct ModulateAlphaByLuminanceOperator |
|---|
| 1067 | { |
|---|
| 1068 | ModulateAlphaByLuminanceOperator() {} |
|---|
| 1069 | |
|---|
| 1070 | inline void luminance(float&) const {} |
|---|
| 1071 | inline void alpha(float&) const {} |
|---|
| 1072 | inline void luminance_alpha(float& l,float& a) const { a*= l; } |
|---|
| 1073 | inline void rgb(float&,float&,float&) const {} |
|---|
| 1074 | inline void rgba(float& r,float& g,float& b,float& a) const { float l = (r+g+b)*0.3333333; a *= l;} |
|---|
| 1075 | }; |
|---|
| 1076 | |
|---|
| 1077 | struct ModulateAlphaByColourOperator |
|---|
| 1078 | { |
|---|
| 1079 | ModulateAlphaByColourOperator(const osg::Vec4& colour):_colour(colour) { _lum = _colour.length(); } |
|---|
| 1080 | |
|---|
| 1081 | osg::Vec4 _colour; |
|---|
| 1082 | float _lum; |
|---|
| 1083 | |
|---|
| 1084 | inline void luminance(float&) const {} |
|---|
| 1085 | inline void alpha(float&) const {} |
|---|
| 1086 | inline void luminance_alpha(float& l,float& a) const { a*= l*_lum; } |
|---|
| 1087 | inline void rgb(float&,float&,float&) const {} |
|---|
| 1088 | 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()); } |
|---|
| 1089 | }; |
|---|
| 1090 | |
|---|
| 1091 | struct ReplaceAlphaWithLuminanceOperator |
|---|
| 1092 | { |
|---|
| 1093 | ReplaceAlphaWithLuminanceOperator() {} |
|---|
| 1094 | |
|---|
| 1095 | inline void luminance(float&) const {} |
|---|
| 1096 | inline void alpha(float&) const {} |
|---|
| 1097 | inline void luminance_alpha(float& l,float& a) const { a= l; } |
|---|
| 1098 | inline void rgb(float&,float&,float&) const { } |
|---|
| 1099 | inline void rgba(float& r,float& g,float& b,float& a) const { float l = (r+g+b)*0.3333333; a = l; } |
|---|
| 1100 | }; |
|---|
| 1101 | |
|---|
| 1102 | void doColourSpaceConversion(ColourSpaceOperation op, osg::Image* image, osg::Vec4& colour) |
|---|
| 1103 | { |
|---|
| 1104 | switch(op) |
|---|
| 1105 | { |
|---|
| 1106 | case (MODULATE_ALPHA_BY_LUMINANCE): |
|---|
| 1107 | std::cout<<"doing conversion MODULATE_ALPHA_BY_LUMINANCE"<<std::endl; |
|---|
| 1108 | modifyImage(image,ModulateAlphaByLuminanceOperator()); |
|---|
| 1109 | break; |
|---|
| 1110 | case (MODULATE_ALPHA_BY_COLOUR): |
|---|
| 1111 | std::cout<<"doing conversion MODULATE_ALPHA_BY_COLOUR"<<std::endl; |
|---|
| 1112 | modifyImage(image,ModulateAlphaByColourOperator(colour)); |
|---|
| 1113 | break; |
|---|
| 1114 | case (REPLACE_ALPHA_WITH_LUMINACE): |
|---|
| 1115 | std::cout<<"doing conversion REPLACE_ALPHA_WITH_LUMINACE"<<std::endl; |
|---|
| 1116 | modifyImage(image,ReplaceAlphaWithLuminanceOperator()); |
|---|
| 1117 | break; |
|---|
| 1118 | default: |
|---|
| 1119 | break; |
|---|
| 1120 | } |
|---|
| 1121 | } |
|---|
| 1122 | |
|---|
| 1123 | int main( int argc, char **argv ) |
|---|
| 1124 | { |
|---|
| 1125 | |
|---|
| 1126 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 1127 | |
|---|
| 1128 | |
|---|
| 1129 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of 3D textures."); |
|---|
| 1130 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 1131 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 1132 | arguments.getApplicationUsage()->addCommandLineOption("-n","Create normal map for per voxel lighting."); |
|---|
| 1133 | arguments.getApplicationUsage()->addCommandLineOption("-s <numSlices>","Number of slices to create."); |
|---|
| 1134 | arguments.getApplicationUsage()->addCommandLineOption("--xSize <size>","Relative width of rendered brick."); |
|---|
| 1135 | arguments.getApplicationUsage()->addCommandLineOption("--ySize <size>","Relative length of rendered brick."); |
|---|
| 1136 | arguments.getApplicationUsage()->addCommandLineOption("--zSize <size>","Relative height of rendered brick."); |
|---|
| 1137 | arguments.getApplicationUsage()->addCommandLineOption("--xMultiplier <multiplier>","Tex coord x mulitplier."); |
|---|
| 1138 | arguments.getApplicationUsage()->addCommandLineOption("--yMultiplier <multiplier>","Tex coord y mulitplier."); |
|---|
| 1139 | arguments.getApplicationUsage()->addCommandLineOption("--zMultiplier <multiplier>","Tex coord z mulitplier."); |
|---|
| 1140 | arguments.getApplicationUsage()->addCommandLineOption("--clip <ratio>","clip volume as a ratio, 0.0 clip all, 1.0 clip none."); |
|---|
| 1141 | arguments.getApplicationUsage()->addCommandLineOption("--maxTextureSize <size>","Set the texture maximum resolution in the s,t,r (x,y,z) dimensions."); |
|---|
| 1142 | arguments.getApplicationUsage()->addCommandLineOption("--s_maxTextureSize <size>","Set the texture maximum resolution in the s (x) dimension."); |
|---|
| 1143 | arguments.getApplicationUsage()->addCommandLineOption("--t_maxTextureSize <size>","Set the texture maximum resolution in the t (y) dimension."); |
|---|
| 1144 | arguments.getApplicationUsage()->addCommandLineOption("--r_maxTextureSize <size>","Set the texture maximum resolution in the r (z) dimension."); |
|---|
| 1145 | arguments.getApplicationUsage()->addCommandLineOption("--compressed","Enable the usage of compressed textures."); |
|---|
| 1146 | arguments.getApplicationUsage()->addCommandLineOption("--compressed-arb","Enable the usage of OpenGL ARB compressed textures."); |
|---|
| 1147 | arguments.getApplicationUsage()->addCommandLineOption("--compressed-dxt1","Enable the usage of S3TC DXT1 compressed textures."); |
|---|
| 1148 | arguments.getApplicationUsage()->addCommandLineOption("--compressed-dxt3","Enable the usage of S3TC DXT3 compressed textures."); |
|---|
| 1149 | arguments.getApplicationUsage()->addCommandLineOption("--compressed-dxt5","Enable the usage of S3TC DXT5 compressed textures."); |
|---|
| 1150 | arguments.getApplicationUsage()->addCommandLineOption("--modulate-alpha-by-luminance","For each pixel multiple the alpha value by the luminance."); |
|---|
| 1151 | arguments.getApplicationUsage()->addCommandLineOption("--replace-alpha-with-luminance","For each pixel mSet the alpha value to the luminance."); |
|---|
| 1152 | arguments.getApplicationUsage()->addCommandLineOption("--num-components <num>","Set the number of components to in he target image."); |
|---|
| 1153 | |
|---|
| 1154 | |
|---|
| 1155 | |
|---|
| 1156 | osgProducer::Viewer viewer(arguments); |
|---|
| 1157 | |
|---|
| 1158 | |
|---|
| 1159 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 1160 | |
|---|
| 1161 | |
|---|
| 1162 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 1163 | |
|---|
| 1164 | |
|---|
| 1165 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 1166 | { |
|---|
| 1167 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 1168 | return 1; |
|---|
| 1169 | } |
|---|
| 1170 | |
|---|
| 1171 | std::string outputFile; |
|---|
| 1172 | while (arguments.read("-o",outputFile)) {} |
|---|
| 1173 | |
|---|
| 1174 | |
|---|
| 1175 | unsigned int numSlices=500; |
|---|
| 1176 | while (arguments.read("-s",numSlices)) {} |
|---|
| 1177 | |
|---|
| 1178 | |
|---|
| 1179 | float sliceEnd=1.0f; |
|---|
| 1180 | while (arguments.read("--clip",sliceEnd)) {} |
|---|
| 1181 | |
|---|
| 1182 | float alphaFunc=0.02f; |
|---|
| 1183 | while (arguments.read("--alphaFunc",alphaFunc)) {} |
|---|
| 1184 | |
|---|
| 1185 | |
|---|
| 1186 | bool createNormalMap = false; |
|---|
| 1187 | while (arguments.read("-n")) createNormalMap=true; |
|---|
| 1188 | |
|---|
| 1189 | float xSize=1.0f, ySize=1.0f, zSize=1.0f; |
|---|
| 1190 | while (arguments.read("--xSize",xSize)) {} |
|---|
| 1191 | while (arguments.read("--ySize",ySize)) {} |
|---|
| 1192 | while (arguments.read("--zSize",zSize)) {} |
|---|
| 1193 | |
|---|
| 1194 | float xMultiplier=1.0f, yMultiplier=1.0f, zMultiplier=1.0f; |
|---|
| 1195 | while (arguments.read("--xMultiplier",xMultiplier)) {} |
|---|
| 1196 | while (arguments.read("--yMultiplier",yMultiplier)) {} |
|---|
| 1197 | while (arguments.read("--zMultiplier",zMultiplier)) {} |
|---|
| 1198 | |
|---|
| 1199 | int s_maximumTextureSize = 256; |
|---|
| 1200 | int t_maximumTextureSize = 256; |
|---|
| 1201 | int r_maximumTextureSize = 256; |
|---|
| 1202 | int maximumTextureSize = 256; |
|---|
| 1203 | while(arguments.read("--maxTextureSize",maximumTextureSize)) |
|---|
| 1204 | { |
|---|
| 1205 | s_maximumTextureSize = maximumTextureSize; |
|---|
| 1206 | t_maximumTextureSize = maximumTextureSize; |
|---|
| 1207 | r_maximumTextureSize = maximumTextureSize; |
|---|
| 1208 | } |
|---|
| 1209 | while(arguments.read("--s_maxTextureSize",s_maximumTextureSize)) {} |
|---|
| 1210 | while(arguments.read("--t_maxTextureSize",t_maximumTextureSize)) {} |
|---|
| 1211 | while(arguments.read("--r_maxTextureSize",r_maximumTextureSize)) {} |
|---|
| 1212 | |
|---|
| 1213 | osg::Texture::InternalFormatMode internalFormatMode = osg::Texture::USE_IMAGE_DATA_FORMAT; |
|---|
| 1214 | while(arguments.read("--compressed") || arguments.read("--compressed-arb")) { internalFormatMode = osg::Texture::USE_ARB_COMPRESSION; } |
|---|
| 1215 | |
|---|
| 1216 | while(arguments.read("--compressed-dxt1")) { internalFormatMode = osg::Texture::USE_S3TC_DXT1_COMPRESSION; } |
|---|
| 1217 | while(arguments.read("--compressed-dxt3")) { internalFormatMode = osg::Texture::USE_S3TC_DXT3_COMPRESSION; } |
|---|
| 1218 | while(arguments.read("--compressed-dxt5")) { internalFormatMode = osg::Texture::USE_S3TC_DXT5_COMPRESSION; } |
|---|
| 1219 | |
|---|
| 1220 | |
|---|
| 1221 | |
|---|
| 1222 | ColourSpaceOperation colourSpaceOperation = NO_COLOUR_SPACE_OPERATION; |
|---|
| 1223 | osg::Vec4 colourModulate(0.25f,0.25f,0.25f,0.25f); |
|---|
| 1224 | while(arguments.read("--modulate-alpha-by-luminance")) { colourSpaceOperation = MODULATE_ALPHA_BY_LUMINANCE; } |
|---|
| 1225 | while(arguments.read("--modulate-alpha-by-colour", colourModulate.x(),colourModulate.y(),colourModulate.z(),colourModulate.w() )) { colourSpaceOperation = MODULATE_ALPHA_BY_COLOUR; } |
|---|
| 1226 | while(arguments.read("--replace-alpha-with-luminance")) { colourSpaceOperation = REPLACE_ALPHA_WITH_LUMINACE; } |
|---|
| 1227 | |
|---|
| 1228 | |
|---|
| 1229 | unsigned int numComponentsDesired = 0; |
|---|
| 1230 | while(arguments.read("--num-components", numComponentsDesired)) {} |
|---|
| 1231 | |
|---|
| 1232 | |
|---|
| 1233 | osg::ref_ptr<osg::Image> image_3d; |
|---|
| 1234 | |
|---|
| 1235 | int sizeX, sizeY, sizeZ, numberBytesPerComponent, numberOfComponents; |
|---|
| 1236 | std::string endian, raw_filename; |
|---|
| 1237 | while (arguments.read("--raw", sizeX, sizeY, sizeZ, numberBytesPerComponent, numberOfComponents, endian, raw_filename)) |
|---|
| 1238 | { |
|---|
| 1239 | image_3d = readRaw(sizeX, sizeY, sizeZ, numberBytesPerComponent, numberOfComponents, endian, raw_filename); |
|---|
| 1240 | } |
|---|
| 1241 | |
|---|
| 1242 | while (arguments.read("--images")) |
|---|
| 1243 | { |
|---|
| 1244 | ImageList imageList; |
|---|
| 1245 | for(int pos=1;pos<arguments.argc() && !arguments.isOption(pos);++pos) |
|---|
| 1246 | { |
|---|
| 1247 | |
|---|
| 1248 | osg::Image *image = osgDB::readImageFile( arguments[pos]); |
|---|
| 1249 | |
|---|
| 1250 | if(image) |
|---|
| 1251 | { |
|---|
| 1252 | imageList.push_back(image); |
|---|
| 1253 | } |
|---|
| 1254 | } |
|---|
| 1255 | |
|---|
| 1256 | |
|---|
| 1257 | ProcessRow processRow; |
|---|
| 1258 | image_3d = createTexture3D(imageList, processRow, numComponentsDesired, s_maximumTextureSize, t_maximumTextureSize, r_maximumTextureSize); |
|---|
| 1259 | } |
|---|
| 1260 | |
|---|
| 1261 | |
|---|
| 1262 | |
|---|
| 1263 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 1264 | |
|---|
| 1265 | |
|---|
| 1266 | if (arguments.errors()) |
|---|
| 1267 | { |
|---|
| 1268 | arguments.writeErrorMessages(std::cout); |
|---|
| 1269 | return 1; |
|---|
| 1270 | } |
|---|
| 1271 | |
|---|
| 1272 | |
|---|
| 1273 | for(int pos=1;pos<arguments.argc() && !image_3d;++pos) |
|---|
| 1274 | { |
|---|
| 1275 | if (!arguments.isOption(pos)) |
|---|
| 1276 | { |
|---|
| 1277 | |
|---|
| 1278 | image_3d = osgDB::readImageFile( arguments[pos]); |
|---|
| 1279 | } |
|---|
| 1280 | } |
|---|
| 1281 | |
|---|
| 1282 | if (!image_3d) return 0; |
|---|
| 1283 | |
|---|
| 1284 | if (colourSpaceOperation!=NO_COLOUR_SPACE_OPERATION) |
|---|
| 1285 | { |
|---|
| 1286 | doColourSpaceConversion(colourSpaceOperation, image_3d.get(), colourModulate); |
|---|
| 1287 | } |
|---|
| 1288 | |
|---|
| 1289 | osg::ref_ptr<osg::Image> normalmap_3d = createNormalMap ? createNormalMapTexture(image_3d.get()) : 0; |
|---|
| 1290 | |
|---|
| 1291 | |
|---|
| 1292 | |
|---|
| 1293 | |
|---|
| 1294 | osg::Node* rootNode = createModel(image_3d, normalmap_3d, |
|---|
| 1295 | internalFormatMode, |
|---|
| 1296 | xSize, ySize, zSize, |
|---|
| 1297 | xMultiplier, yMultiplier, zMultiplier, |
|---|
| 1298 | numSlices, sliceEnd, alphaFunc); |
|---|
| 1299 | |
|---|
| 1300 | if (!outputFile.empty()) |
|---|
| 1301 | { |
|---|
| 1302 | std::string ext = osgDB::getFileExtension(outputFile); |
|---|
| 1303 | std::string name_no_ext = osgDB::getNameLessExtension(outputFile); |
|---|
| 1304 | if (ext=="osg") |
|---|
| 1305 | { |
|---|
| 1306 | if (image_3d.valid()) |
|---|
| 1307 | { |
|---|
| 1308 | image_3d->setFileName(name_no_ext + ".dds"); |
|---|
| 1309 | osgDB::writeImageFile(*image_3d, image_3d->getFileName()); |
|---|
| 1310 | } |
|---|
| 1311 | if (normalmap_3d.valid()) |
|---|
| 1312 | { |
|---|
| 1313 | normalmap_3d->setFileName(name_no_ext + "_normalmap.dds"); |
|---|
| 1314 | osgDB::writeImageFile(*normalmap_3d, normalmap_3d->getFileName()); |
|---|
| 1315 | } |
|---|
| 1316 | |
|---|
| 1317 | osgDB::writeNodeFile(*rootNode, outputFile); |
|---|
| 1318 | } |
|---|
| 1319 | else if (ext=="ive") |
|---|
| 1320 | { |
|---|
| 1321 | osgDB::writeNodeFile(*rootNode, outputFile); |
|---|
| 1322 | } |
|---|
| 1323 | else if (ext=="dds") |
|---|
| 1324 | { |
|---|
| 1325 | osgDB::writeImageFile(*image_3d, outputFile); |
|---|
| 1326 | } |
|---|
| 1327 | else |
|---|
| 1328 | { |
|---|
| 1329 | std::cout<<"Extension not support for file output, not file written."<<std::endl; |
|---|
| 1330 | } |
|---|
| 1331 | |
|---|
| 1332 | return 0; |
|---|
| 1333 | } |
|---|
| 1334 | |
|---|
| 1335 | |
|---|
| 1336 | if (rootNode) |
|---|
| 1337 | { |
|---|
| 1338 | |
|---|
| 1339 | |
|---|
| 1340 | viewer.setSceneData(rootNode); |
|---|
| 1341 | |
|---|
| 1342 | |
|---|
| 1343 | viewer.realize(); |
|---|
| 1344 | |
|---|
| 1345 | while( !viewer.done() ) |
|---|
| 1346 | { |
|---|
| 1347 | |
|---|
| 1348 | viewer.sync(); |
|---|
| 1349 | |
|---|
| 1350 | |
|---|
| 1351 | |
|---|
| 1352 | viewer.update(); |
|---|
| 1353 | |
|---|
| 1354 | |
|---|
| 1355 | viewer.frame(); |
|---|
| 1356 | |
|---|
| 1357 | } |
|---|
| 1358 | |
|---|
| 1359 | |
|---|
| 1360 | |
|---|
| 1361 | viewer.sync(); |
|---|
| 1362 | } |
|---|
| 1363 | |
|---|
| 1364 | return 0; |
|---|
| 1365 | |
|---|
| 1366 | } |
|---|