| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | #include <iostream> |
|---|
| 11 | #include <osgViewer/api/Cocoa/PixelBufferCocoa> |
|---|
| 12 | #include <osgViewer/api/Cocoa/GraphicsWindowCocoa> |
|---|
| 13 | #include <Cocoa/Cocoa.h> |
|---|
| 14 | |
|---|
| 15 | namespace osgViewer { |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | void PixelBufferCocoa::init() |
|---|
| 19 | { |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | _valid = _initialized = true; |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | bool PixelBufferCocoa::realizeImplementation() |
|---|
| 28 | { |
|---|
| 29 | std::cout << "PixelBufferCocoa :: realizeImplementation not implemented yet " << std::endl; |
|---|
| 30 | |
|---|
| 31 | NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; |
|---|
| 32 | |
|---|
| 33 | NSOpenGLPixelFormatAttribute attr[32]; |
|---|
| 34 | int i = 0; |
|---|
| 35 | |
|---|
| 36 | attr[i++] = NSOpenGLPFADepthSize; |
|---|
| 37 | attr[i++] = static_cast<NSOpenGLPixelFormatAttribute>(_traits->depth); |
|---|
| 38 | |
|---|
| 39 | if (_traits->doubleBuffer) { |
|---|
| 40 | attr[i++] = NSOpenGLPFADoubleBuffer; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | if (_traits->alpha) { |
|---|
| 44 | attr[i++] = NSOpenGLPFAAlphaSize; |
|---|
| 45 | attr[i++] = static_cast<NSOpenGLPixelFormatAttribute>(_traits->alpha); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | if (_traits->stencil) { |
|---|
| 49 | attr[i++] = NSOpenGLPFAStencilSize; |
|---|
| 50 | attr[i++] = static_cast<NSOpenGLPixelFormatAttribute>(_traits->stencil); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | if (_traits->sampleBuffers) { |
|---|
| 55 | attr[i++] = NSOpenGLPFASampleBuffers; |
|---|
| 56 | attr[i++] = static_cast<NSOpenGLPixelFormatAttribute>(_traits->sampleBuffers); |
|---|
| 57 | attr[i++] = NSOpenGLPFASamples; |
|---|
| 58 | attr[i++] = static_cast<NSOpenGLPixelFormatAttribute>(_traits->samples); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | attr[i++] = NSOpenGLPFAPixelBuffer; |
|---|
| 62 | attr[i++] = NSOpenGLPFAAccelerated; |
|---|
| 63 | attr[i] = static_cast<NSOpenGLPixelFormatAttribute>(0); |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | NSOpenGLContext* sharedContext = NULL; |
|---|
| 67 | |
|---|
| 68 | GraphicsHandleCocoa* graphicsHandleCocoa = dynamic_cast<GraphicsHandleCocoa*>(_traits->sharedContext); |
|---|
| 69 | if (graphicsHandleCocoa) |
|---|
| 70 | { |
|---|
| 71 | sharedContext = graphicsHandleCocoa->getNSOpenGLContext(); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | NSOpenGLPixelFormat* pixelformat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr ]; |
|---|
| 76 | _context = [[NSOpenGLContext alloc] initWithFormat: pixelformat shareContext: sharedContext]; |
|---|
| 77 | NSOpenGLPixelBuffer* pbuffer = [[NSOpenGLPixelBuffer alloc] initWithTextureTarget: _traits->target textureInternalFormat: _traits->format textureMaxMipMapLevel: _traits->level pixelsWide: _traits->width pixelsHigh: _traits->height]; |
|---|
| 78 | |
|---|
| 79 | [_context setPixelBuffer: pbuffer cubeMapFace: _traits->face mipMapLevel:_traits->level currentVirtualScreen: nil]; |
|---|
| 80 | |
|---|
| 81 | [pool release]; |
|---|
| 82 | |
|---|
| 83 | _realized = (_context != nil); |
|---|
| 84 | return _realized; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | void PixelBufferCocoa::closeImplementation() |
|---|
| 89 | { |
|---|
| 90 | _realized = false; |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | bool PixelBufferCocoa::makeCurrentImplementation() |
|---|
| 99 | { |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | [_context makeCurrentContext]; |
|---|
| 103 | return true; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | bool PixelBufferCocoa::makeContextCurrentImplementation(osg::GraphicsContext* readContext) |
|---|
| 109 | { |
|---|
| 110 | return makeCurrentImplementation(); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | bool PixelBufferCocoa::releaseContextImplementation() |
|---|
| 115 | { |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | [NSOpenGLContext clearCurrentContext]; |
|---|
| 119 | return true; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | void PixelBufferCocoa::bindPBufferToTextureImplementation( GLenum buffer ) |
|---|
| 124 | { |
|---|
| 125 | std::cout << "PixelBufferCocoa :: bindPBufferToTextureImplementation not implemented yet " << std::endl; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | void PixelBufferCocoa::swapBuffersImplementation() |
|---|
| 130 | { |
|---|
| 131 | osg::notify(osg::INFO) << "PixelBufferCocoa::swapBuffersImplementation" << std::endl; |
|---|
| 132 | [_context flushBuffer]; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | PixelBufferCocoa::~PixelBufferCocoa() |
|---|
| 136 | { |
|---|
| 137 | [_context release]; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | } |
|---|