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

Revision 13130, 5.5 kB (checked in by robert, 69 minutes ago)

Added Node:getNumParents() and Node:getParent(index) support

  • Property svn:eol-style set to native
Line 
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#if defined (__APPLE__) && (!__LP64__)
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)
30        attributes.push_back(AGL_COMPLIANT);
31    else
32        attributes.push_back(AGL_CLOSEST_POLICY);
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_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{
76    if (!_valid) {
77        OSG_WARN << "PixelBufferCarbon::realizeImplementation() aglChoosePixelFormat failed! " << aglErrorString(aglGetError()) << std::endl;
78        return false;
79    }
80
81    AGLContext sharedContext = NULL;
82
83    // get any shared AGL contexts
84    GraphicsHandleCarbon* graphicsHandleCarbon = dynamic_cast<GraphicsHandleCarbon*>(_traits->sharedContext.get());
85    if (graphicsHandleCarbon)
86    {
87        sharedContext = graphicsHandleCarbon->getAGLContext();
88    }
89
90    _context = aglCreateContext (_pixelformat, sharedContext);
91
92    if (!_context) {
93        OSG_WARN << "PixelBufferCarbon::realizeImplementation() aglCreateContext failed! " << aglErrorString(aglGetError()) << std::endl;
94        return false;
95    }
96
97
98
99    _realized = aglCreatePBuffer (_traits->width, _traits->height, _traits->target, GL_RGBA, _traits->level, &(_pbuffer));
100    if (!_realized) {
101        OSG_WARN << "PixelBufferCarbon::realizeImplementation() aglCreatePBuffer failed! " << aglErrorString(aglGetError()) << std::endl;
102    }
103
104    makeCurrentImplementation();
105
106    _realized = aglSetPBuffer(_context, _pbuffer, _traits->face, _traits->level, 0);
107    if (!_realized) {
108        OSG_WARN << "PixelBufferCarbon::realizeImplementation() aglSetPBuffer failed! " << aglErrorString(aglGetError()) << std::endl;
109    }
110    return _realized;
111}
112
113void  PixelBufferCarbon::closeImplementation()
114{
115    if (_pbuffer) aglDestroyPBuffer(_pbuffer);
116    if (_context) aglDestroyContext(_context);
117    if (_pixelformat) aglDestroyPixelFormat(_pixelformat);
118
119    _pbuffer = NULL;
120    _context = NULL;
121    _pixelformat = NULL;
122
123    _valid = _realized = false;
124}
125
126/** Make this graphics context current implementation.
127  * Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
128bool  PixelBufferCarbon::makeCurrentImplementation()
129{
130    return (_realized) ? (aglSetCurrentContext(_context) == GL_TRUE) : false;
131}
132
133/** Make this graphics context current with specified read context implementation.
134  * Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
135bool  PixelBufferCarbon::makeContextCurrentImplementation(GraphicsContext* /*readContext*/)  {
136    return makeCurrentImplementation();
137}
138
139/** Release the graphics context.*/
140bool PixelBufferCarbon::releaseContextImplementation()
141{
142     return (aglSetCurrentContext(NULL) == GL_TRUE);
143}
144
145
146/** Pure virtual, Bind the graphics context to associated texture implementation.
147  * Pure virtual - must be implemented by concrate implementations of GraphicsContext. */
148void PixelBufferCarbon::bindPBufferToTextureImplementation( GLenum buffer ){
149
150    OSG_NOTICE<<"GraphicsWindow::void bindPBufferToTextureImplementation(..) not implemented."<<std::endl;
151}
152
153/** Swap the front and back buffers implementation.
154  * Pure virtual - must be implemented by Concrate implementations of GraphicsContext. */
155void PixelBufferCarbon::swapBuffersImplementation()
156{
157     aglSwapBuffers(_context);
158}
159
160
161PixelBufferCarbon::~PixelBufferCarbon()
162{
163    close(true);
164}
165
166
167
168#endif
Note: See TracBrowser for help on using the browser.