| | 177 | |
|---|
| | 178 | void WindowCaptureCallback::ContextData::readPixels() |
|---|
| | 179 | { |
|---|
| | 180 | // std::cout<<"readPixels("<<_fileName<<" image "<<_currentImageIndex<<" "<<_currentPboIndex<<std::endl; |
|---|
| | 181 | |
|---|
| | 182 | unsigned int nextImageIndex = (_currentImageIndex+1)%_imageBuffer.size(); |
|---|
| | 183 | unsigned int nextPboIndex = _pboBuffer.empty() ? 0 : (_currentPboIndex+1)%_pboBuffer.size(); |
|---|
| | 184 | |
|---|
| | 185 | int width=0, height=0; |
|---|
| | 186 | getSize(_gc, width, height); |
|---|
| | 187 | if (width!=_width || _height!=height) |
|---|
| | 188 | { |
|---|
| | 189 | std::cout<<" Window resized "<<width<<", "<<height<<std::endl; |
|---|
| | 190 | _width = width; |
|---|
| | 191 | _height = height; |
|---|
| | 192 | } |
|---|
| | 193 | |
|---|
| | 194 | osg::Image* image = _imageBuffer[_currentImageIndex].get(); |
|---|
| | 195 | |
|---|
| | 196 | image->readPixels(0,0,_width,_height, |
|---|
| | 197 | _pixelFormat,_type); |
|---|
| | 198 | |
|---|
| | 199 | if (!_fileName.empty()) |
|---|
| | 200 | { |
|---|
| | 201 | // osgDB::writeImageFile(*image, _fileName); |
|---|
| | 202 | } |
|---|
| | 203 | |
|---|
| | 204 | _currentImageIndex = nextImageIndex; |
|---|
| | 205 | _currentPboIndex = nextPboIndex; |
|---|
| | 206 | } |
|---|
| | 207 | |
|---|
| | 208 | void WindowCaptureCallback::ContextData::singlePBO(osg::BufferObject::Extensions* ext) |
|---|
| | 209 | { |
|---|
| | 210 | // std::cout<<"singelPBO( "<<_fileName<<" image "<<_currentImageIndex<<" "<<_currentPboIndex<<std::endl; |
|---|
| | 211 | |
|---|
| | 212 | unsigned int nextImageIndex = (_currentImageIndex+1)%_imageBuffer.size(); |
|---|
| | 213 | |
|---|
| | 214 | int width=0, height=0; |
|---|
| | 215 | getSize(_gc, width, height); |
|---|
| | 216 | if (width!=_width || _height!=height) |
|---|
| | 217 | { |
|---|
| | 218 | std::cout<<" Window resized "<<width<<", "<<height<<std::endl; |
|---|
| | 219 | _width = width; |
|---|
| | 220 | _height = height; |
|---|
| | 221 | } |
|---|
| | 222 | |
|---|
| | 223 | GLuint& pbo = _pboBuffer[0]; |
|---|
| | 224 | |
|---|
| | 225 | osg::Image* image = _imageBuffer[_currentImageIndex].get(); |
|---|
| | 226 | if (image->s() != _width || |
|---|
| | 227 | image->t() != _height) |
|---|
| | 228 | { |
|---|
| | 229 | osg::notify(osg::NOTICE)<<"Allocating image "<<std::endl; |
|---|
| | 230 | image->allocateImage(_width, _height, 1, _pixelFormat, _type); |
|---|
| | 231 | |
|---|
| | 232 | if (pbo!=0) |
|---|
| | 233 | { |
|---|
| | 234 | osg::notify(osg::NOTICE)<<"deleting pbo "<<pbo<<std::endl; |
|---|
| | 235 | ext->glDeleteBuffers (1, &pbo); |
|---|
| | 236 | pbo = 0; |
|---|
| | 237 | } |
|---|
| | 238 | } |
|---|
| | 239 | |
|---|
| | 240 | |
|---|
| | 241 | if (pbo==0) |
|---|
| | 242 | { |
|---|
| | 243 | ext->glGenBuffers(1, &pbo); |
|---|
| | 244 | ext->glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo); |
|---|
| | 245 | ext->glBufferData(GL_PIXEL_PACK_BUFFER_ARB, image->getTotalSizeInBytes(), 0, GL_STREAM_READ); |
|---|
| | 246 | |
|---|
| | 247 | osg::notify(osg::NOTICE)<<"Generating pbo "<<pbo<<std::endl; |
|---|
| | 248 | } |
|---|
| | 249 | else |
|---|
| | 250 | { |
|---|
| | 251 | ext->glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo); |
|---|
| | 252 | } |
|---|
| | 253 | |
|---|
| | 254 | glReadPixels(0, 0, _width, _height, _pixelFormat, _type, 0); |
|---|
| | 255 | |
|---|
| | 256 | GLubyte* src = (GLubyte*)ext->glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, |
|---|
| | 257 | GL_READ_ONLY_ARB); |
|---|
| | 258 | |
|---|
| | 259 | if(src) |
|---|
| | 260 | { |
|---|
| | 261 | memcpy(image->data(), src, image->getTotalSizeInBytes()); |
|---|
| | 262 | |
|---|
| | 263 | ext->glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB); |
|---|
| | 264 | } |
|---|
| | 265 | |
|---|
| | 266 | if (!_fileName.empty()) |
|---|
| | 267 | { |
|---|
| | 268 | // osgDB::writeImageFile(*image, _fileName); |
|---|
| | 269 | } |
|---|
| | 270 | |
|---|
| | 271 | ext->glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0); |
|---|
| | 272 | |
|---|
| | 273 | _currentImageIndex = nextImageIndex; |
|---|
| | 274 | } |
|---|
| | 275 | |
|---|
| | 276 | void WindowCaptureCallback::ContextData::multiPBO(osg::BufferObject::Extensions* ext) |
|---|
| | 277 | { |
|---|
| | 278 | // std::cout<<"multiPBO( "<<_fileName<<" image "<<_currentImageIndex<<" "<<_currentPboIndex<<std::endl; |
|---|
| | 279 | unsigned int nextImageIndex = (_currentImageIndex+1)%_imageBuffer.size(); |
|---|
| | 280 | unsigned int nextPboIndex = (_currentPboIndex+1)%_pboBuffer.size(); |
|---|
| | 281 | |
|---|
| | 282 | int width=0, height=0; |
|---|
| | 283 | getSize(_gc, width, height); |
|---|
| | 284 | if (width!=_width || _height!=height) |
|---|
| | 285 | { |
|---|
| | 286 | std::cout<<" Window resized "<<width<<", "<<height<<std::endl; |
|---|
| | 287 | _width = width; |
|---|
| | 288 | _height = height; |
|---|
| | 289 | } |
|---|
| | 290 | |
|---|
| | 291 | GLuint& copy_pbo = _pboBuffer[_currentPboIndex]; |
|---|
| | 292 | GLuint& read_pbo = _pboBuffer[nextPboIndex]; |
|---|
| | 293 | |
|---|
| | 294 | osg::Image* image = _imageBuffer[_currentImageIndex].get(); |
|---|
| | 295 | if (image->s() != _width || |
|---|
| | 296 | image->t() != _height) |
|---|
| | 297 | { |
|---|
| | 298 | osg::notify(osg::NOTICE)<<"Allocating image "<<std::endl; |
|---|
| | 299 | image->allocateImage(_width, _height, 1, _pixelFormat, _type); |
|---|
| | 300 | |
|---|
| | 301 | if (read_pbo!=0) |
|---|
| | 302 | { |
|---|
| | 303 | osg::notify(osg::NOTICE)<<"deleting pbo "<<read_pbo<<std::endl; |
|---|
| | 304 | ext->glDeleteBuffers (1, &read_pbo); |
|---|
| | 305 | read_pbo = 0; |
|---|
| | 306 | } |
|---|
| | 307 | |
|---|
| | 308 | if (copy_pbo!=0) |
|---|
| | 309 | { |
|---|
| | 310 | osg::notify(osg::NOTICE)<<"deleting pbo "<<copy_pbo<<std::endl; |
|---|
| | 311 | ext->glDeleteBuffers (1, ©_pbo); |
|---|
| | 312 | copy_pbo = 0; |
|---|
| | 313 | } |
|---|
| | 314 | } |
|---|
| | 315 | |
|---|
| | 316 | |
|---|
| | 317 | if (read_pbo==0) |
|---|
| | 318 | { |
|---|
| | 319 | ext->glGenBuffers(1, &read_pbo); |
|---|
| | 320 | ext->glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, read_pbo); |
|---|
| | 321 | ext->glBufferData(GL_PIXEL_PACK_BUFFER_ARB, image->getTotalSizeInBytes(), 0, GL_STREAM_READ); |
|---|
| | 322 | |
|---|
| | 323 | osg::notify(osg::NOTICE)<<"Generating pbo "<<read_pbo<<std::endl; |
|---|
| | 324 | } |
|---|
| | 325 | else |
|---|
| | 326 | { |
|---|
| | 327 | ext->glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, read_pbo); |
|---|
| | 328 | } |
|---|
| | 329 | |
|---|
| | 330 | glReadPixels(0, 0, _width, _height, _pixelFormat, _type, 0); |
|---|
| | 331 | |
|---|
| | 332 | |
|---|
| | 333 | if (copy_pbo!=0) |
|---|
| | 334 | { |
|---|
| | 335 | |
|---|
| | 336 | ext->glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, copy_pbo); |
|---|
| | 337 | |
|---|
| | 338 | GLubyte* src = (GLubyte*)ext->glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, |
|---|
| | 339 | GL_READ_ONLY_ARB); |
|---|
| | 340 | |
|---|
| | 341 | if(src) |
|---|
| | 342 | { |
|---|
| | 343 | memcpy(image->data(), src, image->getTotalSizeInBytes()); |
|---|
| | 344 | |
|---|
| | 345 | ext->glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB); |
|---|
| | 346 | } |
|---|
| | 347 | |
|---|
| | 348 | if (!_fileName.empty()) |
|---|
| | 349 | { |
|---|
| | 350 | // osgDB::writeImageFile(*image, _fileName); |
|---|
| | 351 | } |
|---|
| | 352 | } |
|---|
| | 353 | |
|---|
| | 354 | ext->glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0); |
|---|
| | 355 | |
|---|
| | 356 | _currentImageIndex = nextImageIndex; |
|---|
| | 357 | _currentPboIndex = nextPboIndex; |
|---|
| | 358 | } |
|---|