00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <string>
00026
00027 #include <boost/lexical_cast.hpp>
00028
00029 #include <osg/Texture>
00030 #include <osg/Texture2D>
00031
00032 #include "../WGETextureHud.h"
00033
00034 #include "WGEOffscreenRenderPass.h"
00035
00036 WGEOffscreenRenderPass::WGEOffscreenRenderPass( size_t textureWidth, size_t textureHeight, int num ):
00037 osg::Camera(),
00038 m_width( textureWidth ),
00039 m_height( textureHeight ),
00040 m_fbo( new osg::FrameBufferObject() ),
00041 m_hud( NULL )
00042 {
00043
00044 setClearColor( osg::Vec4( 0.0, 0.0, 0.0, 0.0 ) );
00045 setClearMask( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00046 setReferenceFrame( osg::Transform::RELATIVE_RF );
00047 setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT );
00048 setRenderOrder( osg::Camera::PRE_RENDER, num );
00049 }
00050
00051 WGEOffscreenRenderPass::WGEOffscreenRenderPass( size_t textureWidth, size_t textureHeight, osg::ref_ptr< WGETextureHud > hud, std::string name,
00052 int num ):
00053 osg::Camera(),
00054 m_width( textureWidth ),
00055 m_height( textureHeight ),
00056 m_fbo( new osg::FrameBufferObject() ),
00057 m_hud( hud ),
00058 m_name( name )
00059 {
00060
00061 setClearColor( osg::Vec4( 0.0, 0.0, 0.0, 0.0 ) );
00062 setClearMask( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00063 setReferenceFrame( osg::Transform::RELATIVE_RF );
00064 setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT );
00065 setRenderOrder( osg::Camera::PRE_RENDER, num );
00066 }
00067
00068 WGEOffscreenRenderPass::~WGEOffscreenRenderPass()
00069 {
00070
00071 }
00072
00073 void WGEOffscreenRenderPass::attach( BufferComponent buffer, osg::ref_ptr< osg::Texture2D > texture )
00074 {
00075 m_fbo->setAttachment( buffer, osg::FrameBufferAttachment( texture ) );
00076
00077 if( m_hud )
00078 {
00079 m_hud->addTexture( new WGETextureHud::WGETextureHudEntry( texture, m_name + " - " + getBufferName( buffer ) ) );
00080 }
00081
00082 osg::Camera::attach( buffer, texture );
00083 }
00084
00085 osg::ref_ptr< osg::Texture2D > WGEOffscreenRenderPass::attach( BufferComponent buffer, GLint internalFormat )
00086 {
00087 osg::ref_ptr< osg::Texture2D > tex;
00088 if( buffer == DEPTH_BUFFER )
00089 {
00090 tex = createTexture( GL_DEPTH_COMPONENT );
00091 }
00092 else
00093 {
00094 #if defined(__APPLE__)
00095 tex = createTexture( GL_RGBA );
00096 #else
00097 tex = createTexture( internalFormat );
00098 #endif
00099 }
00100 attach( buffer, tex );
00101 return tex;
00102 }
00103
00104 void WGEOffscreenRenderPass::detach( BufferComponent buffer )
00105 {
00106
00107 if( m_hud && osg::Camera::getBufferAttachmentMap().count( buffer ) )
00108 {
00109 m_hud->removeTexture( osg::Camera::getBufferAttachmentMap()[ buffer ]._texture );
00110 }
00111
00112 m_fbo->setAttachment( buffer, osg::FrameBufferAttachment() );
00113
00114 osg::Camera::detach( buffer );
00115 }
00116
00117 osg::ref_ptr< osg::Texture2D > WGEOffscreenRenderPass::createTexture( GLint internalFormat )
00118 {
00119 osg::ref_ptr< osg::Texture2D > tex = new osg::Texture2D;
00120 tex->setTextureSize( m_width, m_height );
00121 tex->setInternalFormat( internalFormat );
00122
00123
00124 tex->setFilter( osg::Texture::MIN_FILTER, osg::Texture::LINEAR );
00125 tex->setFilter( osg::Texture::MAG_FILTER, osg::Texture::LINEAR );
00126
00127
00128 tex->setWrap( osg::Texture::WRAP_S, osg::Texture::REPEAT );
00129 tex->setWrap( osg::Texture::WRAP_T, osg::Texture::REPEAT );
00130
00131 return tex;
00132 }
00133
00134 std::string WGEOffscreenRenderPass::getName() const
00135 {
00136 return m_name;
00137 }
00138
00139 size_t WGEOffscreenRenderPass::getTextureWidth() const
00140 {
00141 return m_width;
00142 }
00143
00144 size_t WGEOffscreenRenderPass::getTextureHeight() const
00145 {
00146 return m_height;
00147 }
00148
00149 void WGEOffscreenRenderPass::addUniform( osg::ref_ptr< osg::Uniform > uniform )
00150 {
00151 this->getOrCreateStateSet()->addUniform( uniform );
00152 }
00153
00154 std::string WGEOffscreenRenderPass::getBufferName( BufferComponent buffer )
00155 {
00156 switch ( buffer )
00157 {
00158 case DEPTH_BUFFER:
00159 return "Depth";
00160 case STENCIL_BUFFER:
00161 return "Stencil";
00162 case PACKED_DEPTH_STENCIL_BUFFER:
00163 return "Depth+Stencil";
00164 case COLOR_BUFFER:
00165 return "Color 0";
00166 case COLOR_BUFFER0:
00167 return "Color 0";
00168 case COLOR_BUFFER1:
00169 return "Color 1";
00170 case COLOR_BUFFER2:
00171 return "Color 2";
00172 case COLOR_BUFFER3:
00173 return "Color 3";
00174 case COLOR_BUFFER4:
00175 return "Color 4";
00176 case COLOR_BUFFER5:
00177 return "Color 5";
00178 case COLOR_BUFFER6:
00179 return "Color 6";
00180 case COLOR_BUFFER7:
00181 return "Color 7";
00182 case COLOR_BUFFER8:
00183 return "Color 8";
00184 case COLOR_BUFFER9:
00185 return "Color 9";
00186 case COLOR_BUFFER10:
00187 return "Color 10";
00188 case COLOR_BUFFER11:
00189 return "Color 11";
00190 case COLOR_BUFFER12:
00191 return "Color 12";
00192 case COLOR_BUFFER13:
00193 return "Color 13";
00194 case COLOR_BUFFER14:
00195 return "Color 14";
00196 case COLOR_BUFFER15:
00197 return "Color 15";
00198 default:
00199 return "Unknown";
00200 }
00201 }
00202