| 1 | #include <osg/Camera> |
|---|
| 2 | #include <osgDB/ObjectWrapper> |
|---|
| 3 | #include <osgDB/InputStream> |
|---|
| 4 | #include <osgDB/OutputStream> |
|---|
| 5 | |
|---|
| 6 | #ifndef GL_ACCUM_BUFFER_BIT |
|---|
| 7 | #define GL_ACCUM_BUFFER_BIT 0x00000200 |
|---|
| 8 | #endif |
|---|
| 9 | |
|---|
| 10 | BEGIN_USER_TABLE( RenderOrder, osg::Camera ); |
|---|
| 11 | ADD_USER_VALUE( PRE_RENDER ); |
|---|
| 12 | ADD_USER_VALUE( NESTED_RENDER ); |
|---|
| 13 | ADD_USER_VALUE( POST_RENDER ); |
|---|
| 14 | END_USER_TABLE() |
|---|
| 15 | |
|---|
| 16 | USER_READ_FUNC( RenderOrder, readOrderValue ) |
|---|
| 17 | USER_WRITE_FUNC( RenderOrder, writeOrderValue ) |
|---|
| 18 | |
|---|
| 19 | BEGIN_USER_TABLE( BufferComponent, osg::Camera ); |
|---|
| 20 | ADD_USER_VALUE( DEPTH_BUFFER ); |
|---|
| 21 | ADD_USER_VALUE( STENCIL_BUFFER ); |
|---|
| 22 | ADD_USER_VALUE( PACKED_DEPTH_STENCIL_BUFFER ); |
|---|
| 23 | ADD_USER_VALUE( COLOR_BUFFER ); |
|---|
| 24 | ADD_USER_VALUE( COLOR_BUFFER0 ); |
|---|
| 25 | ADD_USER_VALUE( COLOR_BUFFER1 ); |
|---|
| 26 | ADD_USER_VALUE( COLOR_BUFFER2 ); |
|---|
| 27 | ADD_USER_VALUE( COLOR_BUFFER3 ); |
|---|
| 28 | ADD_USER_VALUE( COLOR_BUFFER4 ); |
|---|
| 29 | ADD_USER_VALUE( COLOR_BUFFER5 ); |
|---|
| 30 | ADD_USER_VALUE( COLOR_BUFFER6 ); |
|---|
| 31 | ADD_USER_VALUE( COLOR_BUFFER7 ); |
|---|
| 32 | ADD_USER_VALUE( COLOR_BUFFER8 ); |
|---|
| 33 | ADD_USER_VALUE( COLOR_BUFFER9 ); |
|---|
| 34 | ADD_USER_VALUE( COLOR_BUFFER10 ); |
|---|
| 35 | ADD_USER_VALUE( COLOR_BUFFER11 ); |
|---|
| 36 | ADD_USER_VALUE( COLOR_BUFFER12 ); |
|---|
| 37 | ADD_USER_VALUE( COLOR_BUFFER13 ); |
|---|
| 38 | ADD_USER_VALUE( COLOR_BUFFER14 ); |
|---|
| 39 | ADD_USER_VALUE( COLOR_BUFFER15 ); |
|---|
| 40 | END_USER_TABLE() |
|---|
| 41 | |
|---|
| 42 | USER_READ_FUNC( BufferComponent, readBufferComponent ) |
|---|
| 43 | USER_WRITE_FUNC( BufferComponent, writeBufferComponent ) |
|---|
| 44 | |
|---|
| 45 | static osg::Camera::Attachment readBufferAttachment( osgDB::InputStream& is ) |
|---|
| 46 | { |
|---|
| 47 | osg::Camera::Attachment attachment; |
|---|
| 48 | char type = -1; is >> osgDB::PROPERTY("Type") >> type; |
|---|
| 49 | if ( type==0 ) |
|---|
| 50 | { |
|---|
| 51 | is >> osgDB::PROPERTY("InternalFormat") >> attachment._internalFormat; |
|---|
| 52 | return attachment; |
|---|
| 53 | } |
|---|
| 54 | else if ( type==1 ) |
|---|
| 55 | { |
|---|
| 56 | is >> osgDB::PROPERTY("Image"); |
|---|
| 57 | attachment._image = dynamic_cast<osg::Image*>( is.readObject() ); |
|---|
| 58 | } |
|---|
| 59 | else if ( type==2 ) |
|---|
| 60 | { |
|---|
| 61 | is >> osgDB::PROPERTY("Texture"); |
|---|
| 62 | attachment._texture = dynamic_cast<osg::Texture*>( is.readObject() ); |
|---|
| 63 | is >> osgDB::PROPERTY("Level") >> attachment._level; |
|---|
| 64 | is >> osgDB::PROPERTY("Face") >> attachment._face; |
|---|
| 65 | is >> osgDB::PROPERTY("MipMapGeneration") >> attachment._mipMapGeneration; |
|---|
| 66 | } |
|---|
| 67 | else |
|---|
| 68 | return attachment; |
|---|
| 69 | |
|---|
| 70 | is >> osgDB::PROPERTY("MultisampleSamples") >> attachment._multisampleSamples; |
|---|
| 71 | is >> osgDB::PROPERTY("MultisampleColorSamples") >> attachment._multisampleColorSamples; |
|---|
| 72 | return attachment; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | static void writeBufferAttachment( osgDB::OutputStream& os, const osg::Camera::Attachment& attachment ) |
|---|
| 76 | { |
|---|
| 77 | os << osgDB::PROPERTY("Type"); |
|---|
| 78 | if ( attachment._internalFormat!=GL_NONE ) |
|---|
| 79 | { |
|---|
| 80 | os << (char)0 << std::endl; |
|---|
| 81 | os << osgDB::PROPERTY("InternalFormat") << GLENUM(attachment._internalFormat) << std::endl; |
|---|
| 82 | return; |
|---|
| 83 | } |
|---|
| 84 | else if ( attachment._image.valid() ) |
|---|
| 85 | { |
|---|
| 86 | os << (char)1 << std::endl; |
|---|
| 87 | os << osgDB::PROPERTY("Image") << attachment._image.get(); |
|---|
| 88 | } |
|---|
| 89 | else if ( attachment._texture.valid() ) |
|---|
| 90 | { |
|---|
| 91 | os << (char)2 << std::endl; |
|---|
| 92 | os << osgDB::PROPERTY("Texture") << attachment._texture.get(); |
|---|
| 93 | os << osgDB::PROPERTY("Level") << attachment._level << std::endl; |
|---|
| 94 | os << osgDB::PROPERTY("Face") << attachment._face << std::endl; |
|---|
| 95 | os << osgDB::PROPERTY("MipMapGeneration") << attachment._mipMapGeneration << std::endl; |
|---|
| 96 | } |
|---|
| 97 | else |
|---|
| 98 | { |
|---|
| 99 | os << (char)-1 << std::endl; |
|---|
| 100 | return; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | os << osgDB::PROPERTY("MultisampleSamples") << attachment._multisampleSamples << std::endl; |
|---|
| 104 | os << osgDB::PROPERTY("MultisampleColorSamples") << attachment._multisampleColorSamples << std::endl; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | static bool checkClearMask( const osg::Camera& node ) |
|---|
| 109 | { |
|---|
| 110 | return node.getClearMask()!=(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | static bool readClearMask( osgDB::InputStream& is, osg::Camera& node ) |
|---|
| 114 | { |
|---|
| 115 | GLbitfield mask = 0; |
|---|
| 116 | if ( is.isBinary() ) |
|---|
| 117 | { |
|---|
| 118 | int maskValue; is >> maskValue; |
|---|
| 119 | mask = (GLbitfield)maskValue; |
|---|
| 120 | } |
|---|
| 121 | else |
|---|
| 122 | { |
|---|
| 123 | std::string maskSetString; is >> maskSetString; |
|---|
| 124 | osgDB::StringList maskList; osgDB::split( maskSetString, maskList, '|' ); |
|---|
| 125 | for ( unsigned int i=0; i<maskList.size(); ++i ) |
|---|
| 126 | { |
|---|
| 127 | const std::string& maskValue = maskList[i]; |
|---|
| 128 | if ( maskValue=="COLOR" ) mask |= GL_COLOR_BUFFER_BIT; |
|---|
| 129 | else if ( maskValue=="DEPTH" ) mask |= GL_DEPTH_BUFFER_BIT; |
|---|
| 130 | else if ( maskValue=="ACCUM" ) mask |= GL_ACCUM_BUFFER_BIT; |
|---|
| 131 | else if ( maskValue=="STENCIL" ) mask |= GL_STENCIL_BUFFER_BIT; |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | node.setClearMask( mask ); |
|---|
| 135 | return true; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | static bool writeClearMask( osgDB::OutputStream& os, const osg::Camera& node ) |
|---|
| 139 | { |
|---|
| 140 | GLbitfield mask = node.getClearMask(); |
|---|
| 141 | if ( os.isBinary() ) |
|---|
| 142 | os << (int)mask; |
|---|
| 143 | else |
|---|
| 144 | { |
|---|
| 145 | std::string maskString; |
|---|
| 146 | if ( mask==GL_COLOR_BUFFER_BIT ) maskString += std::string("COLOR|"); |
|---|
| 147 | if ( mask==GL_DEPTH_BUFFER_BIT ) maskString += std::string("DEPTH|"); |
|---|
| 148 | if ( mask==GL_ACCUM_BUFFER_BIT ) maskString += std::string("ACCUM|"); |
|---|
| 149 | if ( mask==GL_STENCIL_BUFFER_BIT ) maskString += std::string("STENCIL|"); |
|---|
| 150 | if ( !maskString.size() ) maskString = std::string("NONE|"); |
|---|
| 151 | os << maskString.substr(0, maskString.size()-1) << std::endl; |
|---|
| 152 | } |
|---|
| 153 | return true; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | static bool checkRenderOrder( const osg::Camera& node ) |
|---|
| 158 | { |
|---|
| 159 | return true; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | static bool readRenderOrder( osgDB::InputStream& is, osg::Camera& node ) |
|---|
| 163 | { |
|---|
| 164 | int order = readOrderValue(is); |
|---|
| 165 | int orderNumber = 0; is >> orderNumber; |
|---|
| 166 | node.setRenderOrder( static_cast<osg::Camera::RenderOrder>(order), orderNumber ); |
|---|
| 167 | return true; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | static bool writeRenderOrder( osgDB::OutputStream& os, const osg::Camera& node ) |
|---|
| 171 | { |
|---|
| 172 | writeOrderValue( os, (int)node.getRenderOrder() ); |
|---|
| 173 | os << node.getRenderOrderNum() << std::endl; |
|---|
| 174 | return true; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | static bool checkBufferAttachmentMap( const osg::Camera& node ) |
|---|
| 179 | { |
|---|
| 180 | return node.getBufferAttachmentMap().size()>0; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | static bool readBufferAttachmentMap( osgDB::InputStream& is, osg::Camera& node ) |
|---|
| 184 | { |
|---|
| 185 | unsigned int size = is.readSize(); is >> osgDB::BEGIN_BRACKET; |
|---|
| 186 | for ( unsigned int i=0; i<size; ++i ) |
|---|
| 187 | { |
|---|
| 188 | is >> osgDB::PROPERTY("Attachment"); |
|---|
| 189 | osg::Camera::BufferComponent bufferComponent = |
|---|
| 190 | static_cast<osg::Camera::BufferComponent>( readBufferComponent(is) ); |
|---|
| 191 | is >> osgDB::BEGIN_BRACKET; |
|---|
| 192 | osg::Camera::Attachment attachment = readBufferAttachment(is); |
|---|
| 193 | is >> osgDB::END_BRACKET; |
|---|
| 194 | |
|---|
| 195 | if ( attachment._internalFormat!=GL_NONE ) |
|---|
| 196 | { |
|---|
| 197 | node.attach( bufferComponent, attachment._internalFormat ); |
|---|
| 198 | } |
|---|
| 199 | else if ( attachment._image.valid() ) |
|---|
| 200 | { |
|---|
| 201 | node.attach( bufferComponent, attachment._image.get(), |
|---|
| 202 | attachment._multisampleSamples, attachment._multisampleColorSamples ); |
|---|
| 203 | } |
|---|
| 204 | else if ( attachment._texture.valid() ) |
|---|
| 205 | { |
|---|
| 206 | node.attach( bufferComponent, attachment._texture.get(), |
|---|
| 207 | attachment._level, attachment._face, attachment._mipMapGeneration, |
|---|
| 208 | attachment._multisampleSamples, attachment._multisampleColorSamples ); |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | is >> osgDB::END_BRACKET; |
|---|
| 212 | return true; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | static bool writeBufferAttachmentMap( osgDB::OutputStream& os, const osg::Camera& node ) |
|---|
| 216 | { |
|---|
| 217 | const osg::Camera::BufferAttachmentMap& map = node.getBufferAttachmentMap(); |
|---|
| 218 | os.writeSize(map.size()); os<< osgDB::BEGIN_BRACKET << std::endl; |
|---|
| 219 | for ( osg::Camera::BufferAttachmentMap::const_iterator itr=map.begin(); |
|---|
| 220 | itr!=map.end(); ++itr ) |
|---|
| 221 | { |
|---|
| 222 | os << osgDB::PROPERTY("Attachment"); writeBufferComponent( os, itr->first ); |
|---|
| 223 | os << osgDB::BEGIN_BRACKET << std::endl; |
|---|
| 224 | writeBufferAttachment( os, itr->second ); |
|---|
| 225 | os << osgDB::END_BRACKET << std::endl; |
|---|
| 226 | } |
|---|
| 227 | os << osgDB::END_BRACKET << std::endl; |
|---|
| 228 | return true; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | REGISTER_OBJECT_WRAPPER( Camera, |
|---|
| 232 | new osg::Camera, |
|---|
| 233 | osg::Camera, |
|---|
| 234 | "osg::Object osg::Node osg::Group osg::Transform osg::Camera" ) |
|---|
| 235 | { |
|---|
| 236 | ADD_BOOL_SERIALIZER( AllowEventFocus, true ); |
|---|
| 237 | ADD_USER_SERIALIZER( ClearMask ); |
|---|
| 238 | ADD_VEC4_SERIALIZER( ClearColor, osg::Vec4() ); |
|---|
| 239 | ADD_VEC4_SERIALIZER( ClearAccum, osg::Vec4() ); |
|---|
| 240 | ADD_DOUBLE_SERIALIZER( ClearDepth, 1.0 ); |
|---|
| 241 | ADD_INT_SERIALIZER( ClearStencil, 0 ); |
|---|
| 242 | ADD_OBJECT_SERIALIZER( ColorMask, osg::ColorMask, NULL ); |
|---|
| 243 | ADD_OBJECT_SERIALIZER( Viewport, osg::Viewport, NULL ); |
|---|
| 244 | |
|---|
| 245 | BEGIN_ENUM_SERIALIZER( TransformOrder, PRE_MULTIPLY ); |
|---|
| 246 | ADD_ENUM_VALUE( PRE_MULTIPLY ); |
|---|
| 247 | ADD_ENUM_VALUE( POST_MULTIPLY ); |
|---|
| 248 | END_ENUM_SERIALIZER(); |
|---|
| 249 | |
|---|
| 250 | BEGIN_ENUM_SERIALIZER( ProjectionResizePolicy, HORIZONTAL ); |
|---|
| 251 | ADD_ENUM_VALUE( FIXED ); |
|---|
| 252 | ADD_ENUM_VALUE( HORIZONTAL ); |
|---|
| 253 | ADD_ENUM_VALUE( VERTICAL ); |
|---|
| 254 | END_ENUM_SERIALIZER(); |
|---|
| 255 | |
|---|
| 256 | ADD_MATRIXD_SERIALIZER( ProjectionMatrix, osg::Matrixd() ); |
|---|
| 257 | ADD_MATRIXD_SERIALIZER( ViewMatrix, osg::Matrixd() ); |
|---|
| 258 | ADD_USER_SERIALIZER( RenderOrder ); |
|---|
| 259 | ADD_GLENUM_SERIALIZER( DrawBuffer, GLenum, GL_NONE ); |
|---|
| 260 | ADD_GLENUM_SERIALIZER( ReadBuffer, GLenum, GL_NONE ); |
|---|
| 261 | |
|---|
| 262 | BEGIN_ENUM_SERIALIZER( RenderTargetImplementation, FRAME_BUFFER ); |
|---|
| 263 | ADD_ENUM_VALUE( FRAME_BUFFER_OBJECT ); |
|---|
| 264 | ADD_ENUM_VALUE( PIXEL_BUFFER_RTT ); |
|---|
| 265 | ADD_ENUM_VALUE( PIXEL_BUFFER ); |
|---|
| 266 | ADD_ENUM_VALUE( FRAME_BUFFER ); |
|---|
| 267 | ADD_ENUM_VALUE( SEPERATE_WINDOW ); |
|---|
| 268 | END_ENUM_SERIALIZER(); |
|---|
| 269 | |
|---|
| 270 | ADD_USER_SERIALIZER( BufferAttachmentMap ); |
|---|
| 271 | ADD_OBJECT_SERIALIZER( InitialDrawCallback, osg::Camera::DrawCallback, NULL ); |
|---|
| 272 | ADD_OBJECT_SERIALIZER( PreDrawCallback, osg::Camera::DrawCallback, NULL ); |
|---|
| 273 | ADD_OBJECT_SERIALIZER( PostDrawCallback, osg::Camera::DrawCallback, NULL ); |
|---|
| 274 | ADD_OBJECT_SERIALIZER( FinalDrawCallback, osg::Camera::DrawCallback, NULL ); |
|---|
| 275 | } |
|---|