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