| 197 | | /** creates a pixelformat from a Trait */ |
| 198 | | static AGLPixelFormat createPixelFormat(osg::GraphicsContext::Traits* traits) { |
| 199 | | |
| 200 | | std::vector<GLint> attributes; |
| 201 | | |
| 202 | | if (!traits->pbuffer) attributes.push_back(AGL_NO_RECOVERY); |
| 203 | | attributes.push_back(AGL_RGBA); |
| 204 | | if (!traits->pbuffer) attributes.push_back(AGL_COMPLIANT); |
| 205 | | |
| 206 | | if (traits->doubleBuffer) attributes.push_back(AGL_DOUBLEBUFFER); |
| 207 | | if (traits->quadBufferStereo) attributes.push_back(AGL_STEREO); |
| 208 | | |
| 209 | | attributes.push_back(AGL_RED_SIZE); attributes.push_back(traits->red); |
| 210 | | attributes.push_back(AGL_GREEN_SIZE); attributes.push_back(traits->green); |
| 211 | | attributes.push_back(AGL_BLUE_SIZE); attributes.push_back(traits->blue); |
| 212 | | attributes.push_back(AGL_DEPTH_SIZE); attributes.push_back(traits->depth); |
| 213 | | |
| 214 | | if (traits->alpha) { attributes.push_back(AGL_ALPHA_SIZE); attributes.push_back(traits->alpha); } |
| 215 | | |
| 216 | | if (traits->stencil) { attributes.push_back(AGL_STENCIL_SIZE); attributes.push_back(traits->stencil); } |
| 217 | | |
| 218 | | // TODO |
| 219 | | // missing accumulation-buffer-stuff |
| 220 | | |
| 221 | | #if defined(AGL_SAMPLE_BUFFERS_ARB) && defined (AGL_SAMPLES_ARB) |
| 222 | | |
| 223 | | if (traits->sampleBuffers) { attributes.push_back(AGL_SAMPLE_BUFFERS_ARB); attributes.push_back(traits->sampleBuffers); } |
| 224 | | if (traits->sampleBuffers) { attributes.push_back(AGL_SAMPLES_ARB); attributes.push_back(traits->samples); } |
| 225 | | |
| 226 | | #endif |
| 227 | | attributes.push_back(AGL_NONE); |
| 228 | | |
| 229 | | return aglChoosePixelFormat(NULL, 0, &(attributes.front())); |
| 230 | | } |
| 231 | | |
| 232 | | |
| 233 | | #pragma mark * * * GraphicsContextCarbon * * * |
| 234 | | |
| 235 | | /** 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.*/ |
| 236 | | class GraphicsContextCarbon : public osg::GraphicsContext |
| 237 | | { |
| 238 | | public: |
| 239 | | |
| 240 | | GraphicsContextCarbon(osg::GraphicsContext::Traits* traits): |
| 241 | | _valid(false), |
| 242 | | _realized(false), |
| 243 | | _context(NULL) |
| 244 | | { |
| 245 | | _traits = traits; |
| 246 | | |
| 247 | | _valid = true; |
| 248 | | } |
| 249 | | |
| 250 | | virtual bool valid() const { return _valid; } |
| 251 | | |
| 252 | | /** Realise the GraphicsContext implementation, |
| 253 | | * Pure virtual - must be implemented by concrate implementations of GraphicsContext. */ |
| 254 | | virtual bool realizeImplementation() |
| 255 | | { |
| 256 | | AGLPixelFormat pixelformat = createPixelFormat(_traits.get()); |
| 257 | | |
| 258 | | if (!pixelformat) { |
| 259 | | osg::notify(osg::WARN) << "GraphicsContext::realizeImplementation() aglChoosePixelFormat failed! " << aglErrorString(aglGetError()) << std::endl; |
| 260 | | return false; |
| 261 | | } |
| 262 | | |
| 263 | | _context = aglCreateContext (pixelformat, NULL); |
| 264 | | |
| 265 | | if (!_context) { |
| 266 | | osg::notify(osg::WARN) << "GraphicsContext::realizeImplementation() aglCreateContext failed! " << aglErrorString(aglGetError()) << std::endl; |
| 267 | | return false; |
| 268 | | } |
| 269 | | |
| 270 | | aglDestroyPixelFormat(pixelformat); |
| 271 | | |
| 272 | | _realized = aglCreatePBuffer (_traits->width, _traits->height, _traits->target, GL_RGBA, _traits->level, &(_pbuffer)); |
| 273 | | if (!_realized) { |
| 274 | | osg::notify(osg::WARN) << "GraphicsContext::realizeImplementation() aglCreatePBuffer failed! " << aglErrorString(aglGetError()) << std::endl; |
| 275 | | } |
| 276 | | return _realized; |
| 277 | | } |
| 278 | | |
| 279 | | /** Return true if the graphics context has been realised, and is ready to use, implementation. |
| 280 | | * Pure virtual - must be implemented by concrate implementations of GraphicsContext. */ |
| 281 | | virtual bool isRealizedImplementation() const { return _realized; } |
| 282 | | |
| 283 | | /** Close the graphics context implementation. |
| 284 | | * Pure virtual - must be implemented by concrate implementations of GraphicsContext. */ |
| 285 | | virtual void closeImplementation() |
| 286 | | { |
| 287 | | if (_pbuffer) aglDestroyPBuffer(_pbuffer); |
| 288 | | if (_context) aglDestroyContext(_context); |
| 289 | | _valid = _realized = false; |
| 290 | | } |
| 291 | | |
| 292 | | /** Make this graphics context current implementation. |
| 293 | | * Pure virtual - must be implemented by concrate implementations of GraphicsContext. */ |
| 294 | | virtual bool makeCurrentImplementation() |
| 295 | | { |
| 296 | | return (_realized) ? (aglSetCurrentContext(_context) == GL_TRUE) : false; |
| 297 | | } |
| 298 | | |
| 299 | | /** Make this graphics context current with specified read context implementation. |
| 300 | | * Pure virtual - must be implemented by concrate implementations of GraphicsContext. */ |
| 301 | | virtual bool makeContextCurrentImplementation(GraphicsContext* /*readContext*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::makeContextCurrentImplementation(..) not implemented."<<std::endl; return false;} |
| 302 | | |
| 303 | | /** Release the graphics context.*/ |
| 304 | | virtual bool releaseContextImplementation() |
| 305 | | { |
| 306 | | return (aglSetCurrentContext(NULL) == GL_TRUE); |
| 307 | | } |
| 308 | | |
| 309 | | |
| 310 | | /** Pure virtual, Bind the graphics context to associated texture implementation. |
| 311 | | * Pure virtual - must be implemented by concrate implementations of GraphicsContext. */ |
| 312 | | virtual void bindPBufferToTextureImplementation(GLenum /*buffer*/) { osg::notify(osg::NOTICE)<<"GraphicsWindow::void bindPBufferToTextureImplementation(..) not implemented."<<std::endl; } |
| 313 | | |
| 314 | | /** Swap the front and back buffers implementation. |
| 315 | | * Pure virtual - must be implemented by Concrate implementations of GraphicsContext. */ |
| 316 | | virtual void swapBuffersImplementation() |
| 317 | | { |
| 318 | | aglSwapBuffers(_context); |
| 319 | | } |
| 320 | | |
| 321 | | protected: |
| 322 | | |
| 323 | | bool _valid; |
| 324 | | bool _realized; |
| 325 | | AGLContext _context; |
| 326 | | AGLPbuffer _pbuffer; |
| 327 | | |
| 328 | | }; |
| | 198 | |
| | 199 | |