RenderStage::runCameraSetUp
文章目录
- RTT
- osg::Camera::_bufferAttachmentMap
- RenderStage::BufferComponent和RenderStage::_bufferAttachmentMap
- Camera::attach(BufferComponent buffer, GLenum internalFormat)
- Camera::attach(BufferComponent buffer, osg::Texture* texture.....
- Camera::attach(BufferComponent buffer, osg::Image* image.......
- RenderStage::runCameraSetUp
RTT
- RenderStage::runCameraSetUp实现了一个场景渲染过程中可能非常重要的功能,即纹理烘焙(Render To Texture,RTT),或者称之为“渲染到纹理”。RTT技术意味着我们可以将后台实时绘制得到的场景图像直接作为另一个场景中对象的纹理,从而实现更加丰富的场景表达效果。
- ogl中rtt的基本步骤:
1.首先创建一个“渲染纹理”(Render Texture),可以是例如FBO帧缓存对象,像素缓存对象等;
2.设置“渲染纹理”为图形设备的渲染目标(Render Target),即渲染输出所在地;
3.将“渲染纹理”绑定到一个纹理或图片对象上(添加附件方式);
4.此时图形设备的渲染将在后台进行,其结果将直接体现在所绑定的纹理或图片对象上。
- osg中rtt基本步骤在osg::Camera中,并对camera的renderStage产生影响,在每一帧中使用
osg::Texture2D* texture = new osg::Texture2D;
camera->setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT);
camera->attach( osg::Camera::COLOR_BUFFER, texture );
我们既可以将texture的内容保存成图片,作为场景的截图;也可以将纹理绑定到某个物体上,实现纹理烘焙的效果。这里osgprerender是一个很好的例子,使用附加参数–fb,–fbo,–pbuffer,–window等可以充分了解不同渲染目标实现的过程及其差异。
- 绑定到摄像机的实际纹理或者图片,在Camera类中均使用struct Camera::Attachment来保存
- 更多纹理细节参考
osg::Camera::_bufferAttachmentMap
//osg::camera中
typedef std::map< BufferComponent, Attachment> BufferAttachmentMap;
BufferAttachmentMap _bufferAttachmentMap;enum BufferComponent{DEPTH_BUFFER,STENCIL_BUFFER,PACKED_DEPTH_STENCIL_BUFFER,COLOR_BUFFER,COLOR_BUFFER0,COLOR_BUFFER1 = COLOR_BUFFER0+1,COLOR_BUFFER2 = COLOR_BUFFER0+2,COLOR_BUFFER3 = COLOR_BUFFER0+3,COLOR_BUFFER4 = COLOR_BUFFER0+4,COLOR_BUFFER5 = COLOR_BUFFER0+5,COLOR_BUFFER6 = COLOR_BUFFER0+6,COLOR_BUFFER7 = COLOR_BUFFER0+7,COLOR_BUFFER8 = COLOR_BUFFER0+8,COLOR_BUFFER9 = COLOR_BUFFER0+9,COLOR_BUFFER10 = COLOR_BUFFER0+10,COLOR_BUFFER11 = COLOR_BUFFER0+11,COLOR_BUFFER12 = COLOR_BUFFER0+12,COLOR_BUFFER13 = COLOR_BUFFER0+13,COLOR_BUFFER14 = COLOR_BUFFER0+14,COLOR_BUFFER15 = COLOR_BUFFER0+15};struct Attachment{Attachment():_internalFormat(GL_NONE),_level(0),_face(0),_mipMapGeneration(false),_multisampleSamples(0),_multisampleColorSamples(0) {}int width() const{if (_texture.valid()) return _texture->getTextureWidth();if (_image.valid()) return _image->s();return 0;};int height() const{if (_texture.valid()) return _texture->getTextureHeight();if (_image.valid()) return _image->t();return 0;};int depth() const{if (_texture.valid()) return _texture->getTextureDepth();if (_image.valid()) return _image->r();return 0;};GLenum _internalFormat;ref_ptr<Image> _image;ref_ptr<Texture> _texture;unsigned int _level;unsigned int _face;bool _mipMapGeneration;unsigned int _multisampleSamples;unsigned int _multisampleColorSamples;};
RenderStage::BufferComponent和RenderStage::_bufferAttachmentMap
std::map< osg::Camera::BufferComponent, Attachment> _bufferAttachmentMap;
enum BufferComponent{DEPTH_BUFFER,STENCIL_BUFFER,PACKED_DEPTH_STENCIL_BUFFER,COLOR_BUFFER,COLOR_BUFFER0,COLOR_BUFFER1 = COLOR_BUFFER0+1,COLOR_BUFFER2 = COLOR_BUFFER0+2,COLOR_BUFFER3 = COLOR_BUFFER0+3,COLOR_BUFFER4 = COLOR_BUFFER0+4,COLOR_BUFFER5 = COLOR_BUFFER0+5,COLOR_BUFFER6 = COLOR_BUFFER0+6,COLOR_BUFFER7 = COLOR_BUFFER0+7,COLOR_BUFFER8 = COLOR_BUFFER0+8,COLOR_BUFFER9 = COLOR_BUFFER0+9,COLOR_BUFFER10 = COLOR_BUFFER0+10,COLOR_BUFFER11 = COLOR_BUFFER0+11,COLOR_BUFFER12 = COLOR_BUFFER0+12,COLOR_BUFFER13 = COLOR_BUFFER0+13,COLOR_BUFFER14 = COLOR_BUFFER0+14,COLOR_BUFFER15 = COLOR_BUFFER0+15};
struct Attachment {osg::ref_ptr<osg::Image> _image;GLenum _imageReadPixelFormat;GLenum _imageReadPixelDataType;};
Camera::attach(BufferComponent buffer, GLenum internalFormat)
- 设定指定BufferComponent 的internalFormat
void Camera::attach(BufferComponent buffer, GLenum internalFormat)
{switch(buffer){case DEPTH_BUFFER:if(_bufferAttachmentMap.find(PACKED_DEPTH_STENCIL_BUFFER) != _bufferAttachmentMap.end()){OSG_WARN << "Camera: DEPTH_BUFFER already attached as PACKED_DEPTH_STENCIL_BUFFER !" << std::endl;}break;case STENCIL_BUFFER:if(_bufferAttachmentMap.find(PACKED_DEPTH_STENCIL_BUFFER) != _bufferAttachmentMap.end()){OSG_WARN << "Camera: STENCIL_BUFFER already attached as PACKED_DEPTH_STENCIL_BUFFER !" << std::endl;}break;case PACKED_DEPTH_STENCIL_BUFFER:if(_bufferAttachmentMap.find(DEPTH_BUFFER) != _bufferAttachmentMap.end()){OSG_WARN << "Camera: DEPTH_BUFFER already attached !" << std::endl;}if(_bufferAttachmentMap.find(STENCIL_BUFFER) != _bufferAttachmentMap.end()){OSG_WARN << "Camera: STENCIL_BUFFER already attached !" << std::endl;}break;default:break;}_bufferAttachmentMap[buffer]._internalFormat = internalFormat;
}
Camera::attach(BufferComponent buffer, osg::Texture* texture…
/** Attach a Texture to specified buffer component.* The level parameter controls the mip map level of the texture that is attached.* The face parameter controls the face of texture cube map or z level of 3d texture.* The mipMapGeneration flag controls whether mipmap generation should be done for texture.*/void attach(BufferComponent buffer, osg::Texture* texture, unsigned int level = 0, unsigned int face=0, bool mipMapGeneration=false,unsigned int multisampleSamples = 0,unsigned int multisampleColorSamples = 0);
void Camera::attach(BufferComponent buffer, osg::Texture* texture, unsigned int level, unsigned int face, bool mipMapGeneration,unsigned int multisampleSamples,unsigned int multisampleColorSamples)
{_bufferAttachmentMap[buffer]._texture = texture;_bufferAttachmentMap[buffer]._level = level;_bufferAttachmentMap[buffer]._face = face;_bufferAttachmentMap[buffer]._mipMapGeneration = mipMapGeneration;_bufferAttachmentMap[buffer]._multisampleSamples = multisampleSamples;_bufferAttachmentMap[buffer]._multisampleColorSamples = multisampleColorSamples;
}
Camera::attach(BufferComponent buffer, osg::Image* image…
/** Attach a Image to specified buffer component.*/void attach(BufferComponent buffer, osg::Image* image,unsigned int multisampleSamples = 0,unsigned int multisampleColorSamples = 0);
void Camera::attach(BufferComponent buffer, osg::Image* image,unsigned int multisampleSamples,unsigned int multisampleColorSamples)
{_bufferAttachmentMap[buffer]._image = image;_bufferAttachmentMap[buffer]._multisampleSamples = multisampleSamples;_bufferAttachmentMap[buffer]._multisampleColorSamples = multisampleColorSamples;
}
RenderStage::runCameraSetUp
-
RenderStage::runCameraSetUp则反复遍历Camera::BufferAttachmentMap _bufferAttachmentMap的映射表,检索并设置那些与颜色缓存(COLOR_BUFFER),深度缓存(DEPTH_BUFFER)等相对应的Attachment对象 到renderStage中。
-
renderTargetImplementation==osg::Camera::FRAME_BUFFER_OBJECT
void RenderStage::runCameraSetUp(osg::RenderInfo& renderInfo)
{_cameraRequiresSetUp = false;if (!_camera) return;OSG_INFO<<"RenderStage::runCameraSetUp(osg::RenderInfo& renderInfo) "<<this<<std::endl;_cameraAttachmentMapModifiedCount = _camera->getAttachmentMapModifiedCount();osg::State& state = *renderInfo.getState();osg::Camera::RenderTargetImplementation renderTargetImplementation = _camera->getRenderTargetImplementation();osg::Camera::RenderTargetImplementation renderTargetFallback = _camera->getRenderTargetFallback();osg::Camera::BufferAttachmentMap& bufferAttachments = _camera->getBufferAttachmentMap();// 将camera的设置到这里_bufferAttachmentMap.clear();// compute the required dimensionsint width = static_cast<int>(_viewport->x() + _viewport->width());int height = static_cast<int>(_viewport->y() + _viewport->height());int depth = 1;for(osg::Camera::BufferAttachmentMap::iterator itr = bufferAttachments.begin();itr != bufferAttachments.end();++itr){width = osg::maximum(width,itr->second.width());height = osg::maximum(height,itr->second.height());depth = osg::maximum(depth,itr->second.depth());}// OSG_NOTICE<<"RenderStage::runCameraSetUp viewport "<<_viewport->x()<<" "<<_viewport->y()<<" "<<_viewport->width()<<" "<<_viewport->height()<<std::endl;// OSG_NOTICE<<"RenderStage::runCameraSetUp computed "<<width<<" "<<height<<" "<<depth<<std::endl;// attach images that need to be copied after the stage is drawn.for(osg::Camera::BufferAttachmentMap::iterator itr = bufferAttachments.begin();itr != bufferAttachments.end();++itr){// if one exist attach image to the RenderStage.if (itr->second._image.valid()){osg::Image* image = itr->second._image.get();GLenum pixelFormat = image->getPixelFormat();GLenum dataType = image->getDataType();if (image->data()==0){if (pixelFormat==0) pixelFormat = itr->second._internalFormat;if (pixelFormat==0) pixelFormat = _imageReadPixelFormat;if (pixelFormat==0) pixelFormat = GL_RGBA;if (dataType==0) dataType = _imageReadPixelDataType;if (dataType==0) dataType = GL_UNSIGNED_BYTE;}_bufferAttachmentMap[itr->first]._imageReadPixelFormat = pixelFormat;_bufferAttachmentMap[itr->first]._imageReadPixelDataType = dataType;_bufferAttachmentMap[itr->first]._image = image;}if (itr->second._texture.valid()){osg::Texture* texture = itr->second._texture.get();osg::Texture1D* texture1D = 0;osg::Texture2D* texture2D = 0;osg::Texture2DMultisample* texture2DMS = 0;osg::Texture3D* texture3D = 0;osg::TextureCubeMap* textureCubeMap = 0;osg::TextureRectangle* textureRectangle = 0;if (0 != (texture1D=dynamic_cast<osg::Texture1D*>(texture))){if (texture1D->getTextureWidth()==0){texture1D->setTextureWidth(width);}}else if (0 != (texture2D = dynamic_cast<osg::Texture2D*>(texture))){if (texture2D->getTextureWidth()==0 || texture2D->getTextureHeight()==0){texture2D->setTextureSize(width,height);}}else if (0 != (texture2DMS = dynamic_cast<osg::Texture2DMultisample*>(texture))){if (texture2DMS->getTextureWidth()==0 || texture2DMS->getTextureHeight()==0){texture2DMS->setTextureSize(width,height);}}else if (0 != (texture3D = dynamic_cast<osg::Texture3D*>(texture))){if (texture3D->getTextureWidth()==0 || texture3D->getTextureHeight()==0 || texture3D->getTextureDepth()==0 ){// note we dont' have the depth here, so we'll heave to assume that height and depth are the same..texture3D->setTextureSize(width,height,height);}}else if (0 != (textureCubeMap = dynamic_cast<osg::TextureCubeMap*>(texture))){if (textureCubeMap->getTextureWidth()==0 || textureCubeMap->getTextureHeight()==0){textureCubeMap->setTextureSize(width,height);}}else if (0 != (textureRectangle = dynamic_cast<osg::TextureRectangle*>(texture))){if (textureRectangle->getTextureWidth()==0 || textureRectangle->getTextureHeight()==0){textureRectangle->setTextureSize(width,height);}}}}if (renderTargetImplementation==osg::Camera::FRAME_BUFFER_OBJECT){osg::GLExtensions* ext = state.get<osg::GLExtensions>();bool fbo_supported = ext->isFrameBufferObjectSupported;//一系列gl函数能使用if (fbo_supported){OSG_INFO<<"Setting up osg::Camera::FRAME_BUFFER_OBJECT"<<std::endl;OpenThreads::ScopedLock<OpenThreads::Mutex> lock(*(_camera->getDataChangeMutex()));osg::ref_ptr<osg::FrameBufferObject> fbo = new osg::FrameBufferObject;osg::ref_ptr<osg::FrameBufferObject> fbo_multisample;bool colorAttached = false;bool depthAttached = false;bool stencilAttached = false;unsigned samples = 0;unsigned colorSamples = 0;// This is not a cut and paste error. Set BOTH local masks// to the value of the Camera's use render buffers mask.// We'll change this if and only if we decide we're doing MSFBO(Multi-Sample Framebuffer Object).// true代表从_displaySettings或DisplaySettings::instance()获得隐式BufferAttachmentRenderMaskunsigned int renderBuffersMask = _camera->getImplicitBufferAttachmentRenderMask(true);unsigned int resolveBuffersMask = _camera->getImplicitBufferAttachmentRenderMask(true);if (ext->isRenderbufferMultisampleSupported()){for(osg::Camera::BufferAttachmentMap::iterator itr = bufferAttachments.begin();itr != bufferAttachments.end();++itr){osg::Camera::Attachment& attachment = itr->second;samples = maximum(samples, attachment._multisampleSamples);colorSamples = maximum(colorSamples, attachment._multisampleColorSamples);}if (colorSamples > samples){OSG_NOTIFY(WARN) << "Multisample color samples must be less than or ""equal to samples. Setting color samples equal to samples." << std::endl;colorSamples = samples;}if (samples){fbo_multisample = new osg::FrameBufferObject;// Use the value of the Camera's use resolve buffers mask as the// resolve mask.// 可以自定义,也可以从camera._displaySettings中获得resolveBuffersMask = _camera->getImplicitBufferAttachmentResolveMask(true);}}for(osg::Camera::BufferAttachmentMap::iterator itr = bufferAttachments.begin();itr != bufferAttachments.end();++itr){osg::Camera::BufferComponent buffer = itr->first;osg::Camera::Attachment& attachment = itr->second;// 往帧缓冲对象中设置完整的attachment// 可是设置自定义有效的texture或image,也可以使用默认新建的if (attachment._texture.valid() || attachment._image.valid())fbo->setAttachment(buffer, osg::FrameBufferAttachment(attachment));elsefbo->setAttachment(buffer, osg::FrameBufferAttachment(new osg::RenderBuffer(width, height, attachment._internalFormat)));if (fbo_multisample.valid()){GLenum internalFormat = attachment._internalFormat;if (!internalFormat){switch (buffer){case Camera::DEPTH_BUFFER:internalFormat = GL_DEPTH_COMPONENT24;break;case Camera::STENCIL_BUFFER:internalFormat = GL_STENCIL_INDEX8_EXT;break;case Camera::PACKED_DEPTH_STENCIL_BUFFER:internalFormat = GL_DEPTH_STENCIL_EXT;break;// all other buffers are color buffersdefault:// setup the internal format based on attached texture if such exists, otherwise just default formatif (attachment._texture)internalFormat = attachment._texture->getInternalFormat();elseinternalFormat = GL_RGBA;break;}}fbo_multisample->setAttachment(buffer,osg::FrameBufferAttachment(new osg::RenderBuffer(width, height, internalFormat,samples, colorSamples)));}if (buffer==osg::Camera::DEPTH_BUFFER) depthAttached = true;else if (buffer==osg::Camera::STENCIL_BUFFER) stencilAttached = true;else if (buffer==osg::Camera::PACKED_DEPTH_STENCIL_BUFFER){depthAttached = true;stencilAttached = true;}else if (buffer>=osg::Camera::COLOR_BUFFER) colorAttached = true;}// 根据resolveBuffersMask 的需求,完善fbo中的附件if (!depthAttached){// If doing MSFBO (and therefore need two FBOs, one for multisampled rendering and one for// final resolve), then configure "fbo" as the resolve FBO, and When done// configuring, swap it into "_resolveFbo" (see line 554),然后_fbo = fbo_multisample. // But, if not using MSFBO, then "fbo" is just the render fbo.// If using MSFBO, then resolveBuffersMask// is the value set by the app for the resolve buffers. But if not using// MSFBO, then resolveBuffersMask is the value set by the app for render// buffers. In both cases, resolveBuffersMask is used to configure "fbo".if( resolveBuffersMask & osg::Camera::IMPLICIT_DEPTH_BUFFER_ATTACHMENT ){fbo->setAttachment(osg::Camera::DEPTH_BUFFER, osg::FrameBufferAttachment(new osg::RenderBuffer(width, height, GL_DEPTH_COMPONENT24)));depthAttached = true;}if (fbo_multisample.valid() &&( renderBuffersMask & osg::Camera::IMPLICIT_DEPTH_BUFFER_ATTACHMENT ) ){fbo_multisample->setAttachment(osg::Camera::DEPTH_BUFFER,osg::FrameBufferAttachment(new osg::RenderBuffer(width,height, GL_DEPTH_COMPONENT24, samples, colorSamples)));}}if (!stencilAttached){if( resolveBuffersMask & osg::Camera::IMPLICIT_STENCIL_BUFFER_ATTACHMENT ){fbo->setAttachment(osg::Camera::STENCIL_BUFFER, osg::FrameBufferAttachment(new osg::RenderBuffer(width, height, GL_STENCIL_INDEX8_EXT)));stencilAttached = true;}if (fbo_multisample.valid() &&( renderBuffersMask & osg::Camera::IMPLICIT_STENCIL_BUFFER_ATTACHMENT ) ){fbo_multisample->setAttachment(osg::Camera::STENCIL_BUFFER,osg::FrameBufferAttachment(new osg::RenderBuffer(width,height, GL_STENCIL_INDEX8_EXT, samples, colorSamples)));}}if (!colorAttached){if( resolveBuffersMask & osg::Camera::IMPLICIT_COLOR_BUFFER_ATTACHMENT ){fbo->setAttachment(osg::Camera::COLOR_BUFFER, osg::FrameBufferAttachment(new osg::RenderBuffer(width, height, GL_RGB)));colorAttached = true;}if (fbo_multisample.valid() &&( renderBuffersMask & osg::Camera::IMPLICIT_COLOR_BUFFER_ATTACHMENT ) ){fbo_multisample->setAttachment(osg::Camera::COLOR_BUFFER,osg::FrameBufferAttachment(new osg::RenderBuffer(width,height, GL_RGB, samples, colorSamples)));}}// ogl操作://创建一个新的fbo//遍历设置的_attachments,编译纹理//ext->glBindFramebuffer(READ_DRAW_FRAMEBUFFER, fboID);//调用glDrawBuffers(GLsizei n, const GLenum *bufs);,指定在一个渲染操作中输出到多个颜色缓冲区的目标//遍历设置的_attachments,添加附件fbo->apply(state);// If no color attachment make sure to set glDrawBuffer/glReadBuffer to none// otherwise glCheckFramebufferStatus will fail// It has to be done after call to glBindFramebuffer (fbo->apply)// and before call to glCheckFramebufferStatusif ( !colorAttached ){#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) && !defined(OSG_GLES3_AVAILABLE)setDrawBuffer( GL_NONE, true );//true代表glDrawBuffer is called at each frame draw.state.glDrawBuffer( GL_NONE );#endif}//检查fbo是否完整GLenum status = ext->glCheckFramebufferStatus(GL_FRAMEBUFFER_EXT);if (status != GL_FRAMEBUFFER_COMPLETE_EXT){OSG_NOTICE<<"RenderStage::runCameraSetUp(), FBO setup failed, FBO status= 0x"<<std::hex<<status<<std::dec<<std::endl;fbo_supported = false;GLuint fboId = state.getGraphicsContext() ? state.getGraphicsContext()->getDefaultFboId() : 0;ext->glBindFramebuffer(GL_FRAMEBUFFER_EXT, fboId);fbo = 0;// clean up.osg::get<osg::GLRenderBufferManager>(state.getContextID())->flushAllDeletedGLObjects();osg::get<osg::GLFrameBufferObjectManager>(state.getContextID())->flushAllDeletedGLObjects();}else{//false代表glDrawBuffer is not called at each frame draw.setDrawBuffer(GL_NONE, false );setReadBuffer(GL_NONE, false );// 当前渲染台的帧缓冲替换为自定义帧缓冲_fbo = fbo;if (fbo_multisample.valid()){// glBindFrameBufferfbo_multisample->apply(state);status = ext->glCheckFramebufferStatus(GL_FRAMEBUFFER_EXT);if (status != GL_FRAMEBUFFER_COMPLETE_EXT){OSG_NOTICE << "RenderStage::runCameraSetUp(), ""multisample FBO setup failed, FBO status = 0x"<< std::hex << status << std::dec << std::endl;// 绑定回自定义的帧缓冲fbo->apply(state);fbo_multisample = 0;_resolveFbo = 0;// clean up.osg::get<osg::GLRenderBufferManager>(state.getContextID())->flushAllDeletedGLObjects();osg::get<osg::GLFrameBufferObjectManager>(state.getContextID())->flushAllDeletedGLObjects();}else{_resolveFbo.swap(_fbo);_fbo = fbo_multisample;}}else{_resolveFbo = 0;}}}if (!fbo_supported){if (renderTargetImplementation<renderTargetFallback)renderTargetImplementation = renderTargetFallback;elserenderTargetImplementation = osg::Camera::PIXEL_BUFFER_RTT;}}// check whether PBuffer-RTT is supported or notif (renderTargetImplementation==osg::Camera::PIXEL_BUFFER_RTT &&!osg::isGLExtensionSupported(state.getContextID(), "WGL_ARB_render_texture")){if (renderTargetImplementation<renderTargetFallback)renderTargetImplementation = renderTargetFallback;elserenderTargetImplementation = osg::Camera::PIXEL_BUFFER;}
}
。。。。。。
- 需要单独图形设备的,例如osg::Camera::PIXEL_BUFFER_RTT || PIXEL_BUFFER || SEPARATE_WINDOW
。。。。。。
while (!getGraphicsContext() &&(renderTargetImplementation==osg::Camera::PIXEL_BUFFER_RTT ||renderTargetImplementation==osg::Camera::PIXEL_BUFFER ||renderTargetImplementation==osg::Camera::SEPARATE_WINDOW) ){osg::ref_ptr<osg::GraphicsContext> context = getGraphicsContext();if (!context){// set up the traits of the graphics context that we wantosg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;traits->width = width;// viewport和纹理二者间的maxtraits->height = height;// OSG_NOTICE<<"traits = "<<traits->width<<" "<<traits->height<<std::endl;traits->pbuffer = (renderTargetImplementation==osg::Camera::PIXEL_BUFFER || renderTargetImplementation==osg::Camera::PIXEL_BUFFER_RTT);traits->windowDecoration = (renderTargetImplementation==osg::Camera::SEPARATE_WINDOW);traits->doubleBuffer = (renderTargetImplementation==osg::Camera::SEPARATE_WINDOW);osg::Texture* pBufferTexture = 0;GLenum bufferFormat = GL_NONE;unsigned int level = 0;unsigned int face = 0;bool colorAttached = false;bool depthAttached = false;for(osg::Camera::BufferAttachmentMap::iterator itr = bufferAttachments.begin();itr != bufferAttachments.end();++itr){osg::Camera::BufferComponent buffer = itr->first;osg::Camera::Attachment& attachment = itr->second;switch(buffer){case(osg::Camera::DEPTH_BUFFER):{traits->depth = 24;depthAttached = true;break;}case(osg::Camera::STENCIL_BUFFER):{traits->stencil = 8;break;}case(osg::Camera::PACKED_DEPTH_STENCIL_BUFFER):{traits->depth = 24;depthAttached = true;traits->stencil = 8;break;}case(osg::Camera::COLOR_BUFFER):{if (attachment._internalFormat!=GL_NONE){bufferFormat = attachment._internalFormat;}else{if (attachment._texture.valid()){pBufferTexture = attachment._texture.get();bufferFormat = attachment._texture->getInternalFormat();}else if (attachment._image.valid()){bufferFormat = attachment._image->getInternalTextureFormat();}else{bufferFormat = GL_RGBA;}}level = attachment._level;face = attachment._face;if (renderTargetImplementation==osg::Camera::PIXEL_BUFFER_RTT){traits->target = attachment._texture.valid() ? attachment._texture->getTextureTarget() : 0;traits->format = bufferFormat;traits->level = level;traits->face = face;traits->mipMapGeneration = attachment._mipMapGeneration;}break;}default:{if (renderTargetImplementation==osg::Camera::SEPARATE_WINDOW){OSG_NOTICE<<"Warning: RenderStage::runCameraSetUp(State&) Window ";}else{OSG_NOTICE<<"Warning: RenderStage::runCameraSetUp(State&) Pbuffer ";}OSG_NOTICE<<"does not support multiple color outputs."<<std::endl;break;}}}if (!depthAttached){traits->depth = 24;}if (!colorAttached){if (bufferFormat == GL_NONE) bufferFormat = GL_RGB;traits->red = 8;traits->green = 8;traits->blue = 8;traits->alpha = (bufferFormat==GL_RGBA) ? 8 : 0;}// share OpenGL objects if possible...if (state.getGraphicsContext()){traits->sharedContext = state.getGraphicsContext();const osg::GraphicsContext::Traits* sharedTraits = traits->sharedContext->getTraits();if (sharedTraits){traits->hostName = sharedTraits->hostName;traits->displayNum = sharedTraits->displayNum;traits->screenNum = sharedTraits->screenNum;}}// create the graphics context according to these traits.context = osg::GraphicsContext::createGraphicsContext(traits.get());if (context.valid() && context->realize()){OSG_INFO<<"RenderStage::runCameraSetUp(State&) Context has been realized "<<std::endl;// successfully set up graphics context as requested,// will assign this graphics context to the RenderStage and// associated parameters. Setting the graphics context will// single this while loop to exit successful.setGraphicsContext(context.get());// how to do we detect that an attempt to set up RTT has failed??setDrawBuffer(GL_FRONT);setReadBuffer(GL_FRONT);if (pBufferTexture && renderTargetImplementation==osg::Camera::PIXEL_BUFFER_RTT){OSG_INFO<<"RenderStage::runCameraSetUp(State&) Assign graphics context to Texture"<<std::endl;pBufferTexture->setReadPBuffer(context.get());}else{OSG_INFO<<"RenderStage::runCameraSetUp(State&) Assigning texture to RenderStage so that it does the copy"<<std::endl;setTexture(pBufferTexture, level, face);}}else{OSG_INFO<<"Failed to acquire Graphics Context"<<std::endl;if (renderTargetImplementation==osg::Camera::PIXEL_BUFFER_RTT){// fallback to using standard PBuffer, this will allow this while loop to continueif (renderTargetImplementation<renderTargetFallback)renderTargetImplementation = renderTargetFallback;elserenderTargetImplementation = osg::Camera::PIXEL_BUFFER;}else{renderTargetImplementation = osg::Camera::FRAME_BUFFER;}}}}
。。。。。。
- 别的都失败了,回退,renderTargetImplementation==osg::Camera::FRAME_BUFFER
// finally if all else has failed, then the frame buffer fallback will come in to play.if (renderTargetImplementation==osg::Camera::FRAME_BUFFER){OSG_INFO<<"Setting up osg::Camera::FRAME_BUFFER"<<std::endl;for(osg::Camera::BufferAttachmentMap::iterator itr = bufferAttachments.begin();itr != bufferAttachments.end();++itr){// assign the texture...if (itr->second._texture.valid()) setTexture(itr->second._texture.get(), itr->second._level, itr->second._face);}}