root/OpenSceneGraph/trunk/src/osgViewer/PixelBufferCarbon.cpp @ 10005

Revision 10005, 5.8 kB (checked in by robert, 5 years ago)

Converted tabs to four spaces

RevLine 
[7039]1/*
2 *  PixelBufferCarbon.cpp
3 *  OpenSceneGraph
4 *
5 *  Created by Stephan Huber on 27.06.07.
6 *  Copyright 2007 __MyCompanyName__. All rights reserved.
7 *
8 */
9 
10 #ifdef __APPLE__
11
12#include <osg/observer_ptr>
13#include <osgViewer/api/Carbon/PixelBufferCarbon>
14#include <osgViewer/api/Carbon/GraphicsWindowCarbon>
15#include <Carbon/Carbon.h>
16#include <OpenGL/OpenGL.h>
17using namespace osgViewer;
18
19
20
21
22/** creates a pixelformat from a Trait */       
23AGLPixelFormat PixelBufferCarbon::createPixelFormat(osg::GraphicsContext::Traits* traits) {
24
25    std::vector<GLint> attributes;
26   
27    attributes.push_back(AGL_NO_RECOVERY);
28    attributes.push_back(AGL_RGBA);
29    if (!traits->pbuffer)
[10005]30        attributes.push_back(AGL_COMPLIANT);
31    else
32        attributes.push_back(AGL_CLOSEST_POLICY);
[7039]33   
34    if (traits->doubleBuffer) attributes.push_back(AGL_DOUBLEBUFFER);
35    if (traits->quadBufferStereo) attributes.push_back(AGL_STEREO);
36   
37    attributes.push_back(AGL_RED_SIZE); attributes.push_back(traits->red);
38    attributes.push_back(AGL_GREEN_SIZE); attributes.push_back(traits->green);
39    attributes.push_back(AGL_BLUE_SIZE); attributes.push_back(traits->blue);
40    attributes.push_back(AGL_DEPTH_SIZE); attributes.push_back(traits->depth);
41   
42    if (traits->alpha) { attributes.push_back(AGL_ALPHA_SIZE); attributes.push_back(traits->alpha); }
43   
44    if (traits->stencil) { attributes.push_back(AGL_STENCIL_SIZE); attributes.push_back(traits->stencil); }
45   
46    // TODO
47    // missing accumulation-buffer-stuff
48
49#if defined(AGL_SAMPLE_BUFFERS_ARB) && defined (AGL_SAMPLES_ARB)
50
51    if (traits->sampleBuffers) { attributes.push_back(AGL_SAMPLE_BUFFERS_ARB); attributes.push_back(traits->sampleBuffers); }
52    if (traits->sampleBuffers) { attributes.push_back(AGL_SAMPLES_ARB); attributes.push_back(traits->samples); }
53
54#endif
55    attributes.push_back(AGL_NONE);
56   
57    return aglChoosePixelFormat(NULL, 0, &(attributes.front()));
58}
59
60
61void PixelBufferCarbon::init()
62{
63    _context = NULL;
64    _pixelformat = PixelBufferCarbon::createPixelFormat(_traits.get());
65    if (!_pixelformat)
66        osg::notify(osg::WARN) << "PixelBufferCarbon::init could not create a valid pixelformat" << std::endl;
67    _valid = (_pixelformat != NULL);
68}
69
70
71/** This is the class we need to create for pbuffers, note its not a GraphicsWindow as it won't need any of the event handling and window mapping facilities.*/
72/** Realise the GraphicsContext implementation,
73          * Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
74bool PixelBufferCarbon::realizeImplementation()
75{
[10005]76    if (!_valid) {
77        osg::notify(osg::WARN) << "PixelBufferCarbon::realizeImplementation() aglChoosePixelFormat failed! " << aglErrorString(aglGetError()) << std::endl;
78        return false;
79    }
80   
81    AGLContext sharedContext = NULL;
[7039]82
83    // get any shared GLX contexts   
84    GraphicsWindowCarbon* graphicsWindowCarbon = dynamic_cast<GraphicsWindowCarbon*>(_traits->sharedContext);
85    if (graphicsWindowCarbon)
86    {
87        sharedContext = graphicsWindowCarbon->getAGLContext();
88    }
89    else
90    {
91        PixelBufferCarbon* pixelBufferCarbon = dynamic_cast<PixelBufferCarbon*>(_traits->sharedContext);
92        if (pixelBufferCarbon)
93        {
94            sharedContext = pixelBufferCarbon->getAGLContext();
95        }
96    }
[10005]97   
98    _context = aglCreateContext (_pixelformat, sharedContext);
99   
100    if (!_context) {
101        osg::notify(osg::WARN) << "PixelBufferCarbon::realizeImplementation() aglCreateContext failed! " << aglErrorString(aglGetError()) << std::endl;
102        return false;
103    }
104   
[7039]105
[10005]106   
107    _realized = aglCreatePBuffer (_traits->width, _traits->height, _traits->target, GL_RGBA, _traits->level, &(_pbuffer));
108    if (!_realized) {
109        osg::notify(osg::WARN) << "PixelBufferCarbon::realizeImplementation() aglCreatePBuffer failed! " << aglErrorString(aglGetError()) << std::endl;
110    }
111   
112    makeCurrentImplementation();
113   
114    _realized = aglSetPBuffer(_context, _pbuffer, _traits->face, _traits->level, 0);
115    if (!_realized) {
116        osg::notify(osg::WARN) << "PixelBufferCarbon::realizeImplementation() aglSetPBuffer failed! " << aglErrorString(aglGetError()) << std::endl;
117    }
118    return _realized;
[7039]119}
120
121void  PixelBufferCarbon::closeImplementation() 
122{
[10005]123    if (_pbuffer) aglDestroyPBuffer(_pbuffer);
124    if (_context) aglDestroyContext(_context);
125    if (_pixelformat) aglDestroyPixelFormat(_pixelformat);
126    _valid = _realized = false;
[7039]127}
128
129/** Make this graphics context current implementation.
130  * Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
131bool  PixelBufferCarbon::makeCurrentImplementation()
132{
[10005]133    return (_realized) ? (aglSetCurrentContext(_context) == GL_TRUE) : false;
[7039]134}
135       
136/** Make this graphics context current with specified read context implementation.
137  * Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
138bool  PixelBufferCarbon::makeContextCurrentImplementation(GraphicsContext* /*readContext*/)  {
[10005]139    return makeCurrentImplementation();
[7039]140}
141
142/** Release the graphics context.*/
143bool PixelBufferCarbon::releaseContextImplementation()
144
[10005]145     return (aglSetCurrentContext(NULL) == GL_TRUE);
[7039]146}
147
148
149/** Pure virtual, Bind the graphics context to associated texture implementation.
150  * Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
151void PixelBufferCarbon::bindPBufferToTextureImplementation( GLenum buffer ){
152
[10005]153    osg::notify(osg::NOTICE)<<"GraphicsWindow::void bindPBufferToTextureImplementation(..) not implemented."<<std::endl;
[7039]154}
155
156/** Swap the front and back buffers implementation.
157  * Pure virtual - must be implemented by Concrate implementations of GraphicsContext. */
158void PixelBufferCarbon::swapBuffersImplementation() 
159{
[10005]160     aglSwapBuffers(_context);
[7039]161}
162   
[10005]163       
[7039]164PixelBufferCarbon::~PixelBufferCarbon()
165{
[10005]166    close(true);
[7039]167}
168
169
170
171#endif
Note: See TracBrowser for help on using the browser.