| [2815] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| [5799] | 9 | #include <osg/GL> |
|---|
| [5803] | 10 | #include <osg/Endian> |
|---|
| [10891] | 11 | #include <osg/Image> |
|---|
| [5799] | 12 | #include <osgDB/FileNameUtils> |
|---|
| [5656] | 13 | |
|---|
| [2815] | 14 | #include "MovieData.h" |
|---|
| 15 | #include "QTUtils.h" |
|---|
| 16 | |
|---|
| [5656] | 17 | |
|---|
| 18 | |
|---|
| [2815] | 19 | |
|---|
| [6701] | 20 | MovieData::MovieData() : _pointer(NULL), _movie(NULL), _gw(NULL), _fError(false), _isLooping(false) |
|---|
| [3100] | 21 | { |
|---|
| 22 | |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | MovieData::~MovieData() |
|---|
| [8792] | 27 | { |
|---|
| [5799] | 28 | if (_pointer) free(_pointer); |
|---|
| [3100] | 29 | if (_gw) DisposeGWorld(_gw); |
|---|
| [8792] | 30 | |
|---|
| 31 | if (_movie) { |
|---|
| 32 | DisposeMovie(_movie); |
|---|
| 33 | } |
|---|
| [3100] | 34 | } |
|---|
| [2815] | 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| [5799] | 39 | void MovieData::load(osg::Image* image, std::string afilename, float startTime) |
|---|
| [3100] | 40 | { |
|---|
| [9932] | 41 | bool isUrl( osgDB::containsServerAddress(afilename) ); |
|---|
| 42 | |
|---|
| [5799] | 43 | std::string filename = afilename; |
|---|
| [9932] | 44 | if (!isUrl) { |
|---|
| 45 | if (!osgDB::isFileNameNativeStyle(filename)) |
|---|
| 46 | filename = osgDB::convertFileNameToNativeStyle(filename); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | image->setFileName(filename); |
|---|
| 50 | |
|---|
| [5799] | 51 | |
|---|
| [9932] | 52 | QTNewMoviePropertyElement newMovieProperties[2]; |
|---|
| 53 | CFStringRef movieLocation = CFStringCreateWithCString(NULL, filename.c_str(), kCFStringEncodingUTF8); |
|---|
| 54 | CFURLRef movieURL(NULL); |
|---|
| 55 | Boolean trueValue = true; |
|---|
| 56 | |
|---|
| 57 | newMovieProperties[0].propClass = kQTPropertyClass_DataLocation; |
|---|
| 58 | if (!isUrl) |
|---|
| 59 | { |
|---|
| 60 | #ifdef __APPLE__ |
|---|
| 61 | newMovieProperties[0].propID = kQTDataLocationPropertyID_CFStringPosixPath; |
|---|
| 62 | #else |
|---|
| 63 | newMovieProperties[0].propID = kQTDataLocationPropertyID_CFStringWindowsPath; |
|---|
| 64 | #endif |
|---|
| 65 | |
|---|
| 66 | newMovieProperties[0].propValueSize = sizeof(CFStringRef); |
|---|
| 67 | newMovieProperties[0].propValueAddress = &movieLocation; |
|---|
| 68 | } |
|---|
| 69 | else |
|---|
| 70 | { |
|---|
| 71 | movieURL = CFURLCreateWithString(kCFAllocatorDefault, movieLocation, NULL); |
|---|
| 72 | |
|---|
| 73 | newMovieProperties[0].propID = kQTDataLocationPropertyID_CFURL; |
|---|
| 74 | newMovieProperties[0].propValueSize = sizeof(movieURL); |
|---|
| 75 | newMovieProperties[0].propValueAddress = (void*)&movieURL; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | newMovieProperties[1].propClass = kQTPropertyClass_NewMovieProperty; |
|---|
| 80 | newMovieProperties[1].propID = kQTNewMoviePropertyID_Active; |
|---|
| 81 | newMovieProperties[1].propValueSize = sizeof(trueValue); |
|---|
| 82 | newMovieProperties[1].propValueAddress = &trueValue; |
|---|
| [2815] | 83 | |
|---|
| [9932] | 84 | |
|---|
| 85 | OSStatus status = NewMovieFromProperties(2, newMovieProperties, 0, NULL, &_movie); |
|---|
| 86 | CFRelease(movieLocation); |
|---|
| 87 | if (movieURL) CFRelease(movieURL); |
|---|
| 88 | |
|---|
| 89 | if (status !=0) { |
|---|
| [2815] | 90 | _fError = true; |
|---|
| [9932] | 91 | osg::notify(osg::FATAL) << " MovieData :: NewMovieFromProperties failed with err " << status<< std::endl; |
|---|
| [2815] | 92 | return; |
|---|
| 93 | } |
|---|
| [9932] | 94 | |
|---|
| 95 | |
|---|
| [11035] | 96 | Rect bounds; |
|---|
| [9932] | 97 | |
|---|
| [11035] | 98 | #ifdef __APPLE__ |
|---|
| 99 | GetRegionBounds(GetMovieBoundsRgn(_movie), &bounds); |
|---|
| 100 | #else |
|---|
| 101 | bounds = (*GetMovieBoundsRgn(_movie))->rgnBBox; |
|---|
| 102 | #endif |
|---|
| 103 | |
|---|
| [11019] | 104 | _checkMovieError("Can't get movie bounds\n"); |
|---|
| 105 | |
|---|
| [2815] | 106 | OffsetRect(&bounds, -bounds.left, -bounds.top); |
|---|
| 107 | SetMovieBox(_movie, &bounds); |
|---|
| 108 | _checkMovieError("Can't set movie box\n"); |
|---|
| 109 | |
|---|
| 110 | _movieWidth = bounds.right; |
|---|
| 111 | _movieHeight = bounds.bottom; |
|---|
| 112 | |
|---|
| 113 | _timescale = (float)GetMovieTimeScale(_movie); |
|---|
| 114 | |
|---|
| 115 | _initImage(image); |
|---|
| 116 | if (!_fError) _initGWorldStuff(image); |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | if (!_fError) { |
|---|
| 120 | |
|---|
| 121 | if ( startTime == 0.0f) |
|---|
| 122 | GoToBeginningOfMovie(_movie); |
|---|
| 123 | else { |
|---|
| 124 | TimeValue t = (TimeValue) (startTime*_timescale); |
|---|
| 125 | SetMovieTimeValue(_movie,t); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | UpdateMovie(_movie); |
|---|
| 129 | SetMovieRate(_movie,0); |
|---|
| [5799] | 130 | SetMovieActive(_movie, true); |
|---|
| 131 | UpdateMovie(_movie); |
|---|
| 132 | MoviesTask(_movie,0); |
|---|
| [2815] | 133 | } |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| [3100] | 142 | void MovieData::_initImage(osg::Image* image) |
|---|
| 143 | { |
|---|
| [2815] | 144 | |
|---|
| [3100] | 145 | void* buffer; |
|---|
| [5799] | 146 | |
|---|
| [2815] | 147 | |
|---|
| 148 | _textureWidth = ((_movieWidth + 7) >> 3) << 3; |
|---|
| 149 | _textureHeight = _movieHeight; |
|---|
| 150 | |
|---|
| 151 | |
|---|
| [5799] | 152 | _pointer = (char*)malloc(4 * _textureWidth * _textureHeight + 32); |
|---|
| [2815] | 153 | |
|---|
| [5799] | 154 | if (_pointer == NULL) { |
|---|
| [2815] | 155 | osg::notify(osg::FATAL) << "MovieData: " << "Can't allocate texture buffer" << std::endl; |
|---|
| 156 | _fError= true; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| [5799] | 159 | buffer = (void*)(((unsigned long)(_pointer + 31) >> 5) << 5); |
|---|
| [5656] | 160 | |
|---|
| [5803] | 161 | GLenum internalFormat = (osg::getCpuByteOrder()==osg::BigEndian)? |
|---|
| [5656] | 162 | GL_UNSIGNED_INT_8_8_8_8_REV : |
|---|
| 163 | GL_UNSIGNED_INT_8_8_8_8; |
|---|
| 164 | |
|---|
| [5799] | 165 | image->setImage(_textureWidth,_textureHeight,1, |
|---|
| [4801] | 166 | (GLint) GL_RGBA8, |
|---|
| [10891] | 167 | (GLenum)GL_BGRA, |
|---|
| [5803] | 168 | internalFormat, |
|---|
| [5799] | 169 | (unsigned char*) buffer,osg::Image::NO_DELETE,4); |
|---|
| [2815] | 170 | |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | void MovieData::_initGWorldStuff(osg::Image * image) { |
|---|
| 179 | |
|---|
| 180 | Rect textureBounds; |
|---|
| 181 | OSStatus err; |
|---|
| [4801] | 182 | GDHandle origDevice; |
|---|
| 183 | CGrafPtr origPort; |
|---|
| [2815] | 184 | PixMapHandle pixmap = NULL; |
|---|
| 185 | |
|---|
| 186 | textureBounds.left = 0; |
|---|
| 187 | textureBounds.top = 0; |
|---|
| 188 | textureBounds.right = image->s(); |
|---|
| 189 | textureBounds.bottom = image->t(); |
|---|
| 190 | err = QTNewGWorldFromPtr(&_gw, k32ARGBPixelFormat, &textureBounds, NULL, NULL, 0, image->data(), 4 * image->s()); |
|---|
| 191 | |
|---|
| 192 | if (err !=0 ) |
|---|
| 193 | osg::notify(osg::FATAL) << "MovieData : Could not create gWorld" << std::endl; |
|---|
| 194 | |
|---|
| 195 | GetGWorld (&origPort, &origDevice); |
|---|
| [4801] | 196 | SetGWorld(_gw, NULL); |
|---|
| [2815] | 197 | SetMovieGWorld(_movie, (CGrafPtr)_gw, NULL); |
|---|
| 198 | |
|---|
| 199 | _checkMovieError("SetMovieGWorld failed"); |
|---|
| 200 | |
|---|
| 201 | pixmap = GetGWorldPixMap (_gw); |
|---|
| 202 | if (pixmap) |
|---|
| 203 | { |
|---|
| [4801] | 204 | if (!LockPixels (pixmap)) |
|---|
| [2815] | 205 | { |
|---|
| 206 | osg::notify(osg::FATAL) << "Could not lock PixMap" << std::endl; |
|---|
| 207 | ExitToShell (); |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | else |
|---|
| 211 | { |
|---|
| 212 | osg::notify(osg::FATAL) << "Could not GetGWorldPixMap" << std::endl; |
|---|
| 213 | ExitToShell (); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | SetGWorld(origPort, origDevice); |
|---|
| 217 | |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| [5799] | 220 | void MovieData::setMovieTime(float atime) { |
|---|
| 221 | float time = (atime > getMovieDuration()) ? getMovieDuration() : atime; |
|---|
| 222 | |
|---|
| 223 | TimeValue t = (TimeValue) (time * _timescale); |
|---|
| 224 | SetMovieTimeValue(_movie,t); |
|---|
| 225 | _checkMovieError("setMovieTime failed"); |
|---|
| 226 | UpdateMovie(_movie); |
|---|
| 227 | MoviesTask(_movie,0); |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | } |
|---|
| [2815] | 231 | |
|---|
| [5799] | 232 | void MovieData::setMovieRate(float rate) { |
|---|
| 233 | |
|---|
| 234 | _movieRate = rate; |
|---|
| [5803] | 235 | if ((rate != 0) && (_preRolled == false)) { |
|---|
| [5799] | 236 | PrerollMovie(_movie, GetMovieTime(_movie,NULL), X2Fix(rate)); |
|---|
| 237 | _checkMovieError("PrerollMovie failed"); |
|---|
| [5803] | 238 | _preRolled = true; |
|---|
| [5799] | 239 | } |
|---|
| 240 | |
|---|
| 241 | SetMovieRate(_movie, X2Fix(rate)); |
|---|
| 242 | _checkMovieError("setMovieRate failed"); |
|---|
| 243 | MoviesTask(_movie, 0); |
|---|
| 244 | _checkMovieError("MoviesTask failed"); |
|---|
| 245 | |
|---|
| 246 | UpdateMovie(_movie); |
|---|
| 247 | _checkMovieError("UpdateMovie failed"); |
|---|
| [2815] | 248 | |
|---|
| [5799] | 249 | } |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|