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