| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osg/GLExtensions> |
|---|
| 20 | #include <osg/Node> |
|---|
| 21 | #include <osg/Geometry> |
|---|
| 22 | #include <osg/Notify> |
|---|
| 23 | #include <osg/MatrixTransform> |
|---|
| 24 | #include <osg/Texture2D> |
|---|
| 25 | #include <osg/TextureRectangle> |
|---|
| 26 | #include <osg/ColorMask> |
|---|
| 27 | #include <osg/Material> |
|---|
| 28 | |
|---|
| 29 | #include <osgGA/TrackballManipulator> |
|---|
| 30 | #include <osgGA/FlightManipulator> |
|---|
| 31 | #include <osgGA/DriveManipulator> |
|---|
| 32 | |
|---|
| 33 | #include <osgViewer/Viewer> |
|---|
| 34 | |
|---|
| 35 | #include <iostream> |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | struct MyCameraPostDrawCallback : public osg::Camera::DrawCallback |
|---|
| 39 | { |
|---|
| 40 | MyCameraPostDrawCallback(osg::Image* image): |
|---|
| 41 | _image(image) |
|---|
| 42 | { |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | virtual void operator () (const osg::Camera& ) const |
|---|
| 46 | { |
|---|
| 47 | if (_image && _image->getPixelFormat()==GL_RGBA && _image->getDataType()==GL_UNSIGNED_BYTE) |
|---|
| 48 | { |
|---|
| 49 | |
|---|
| 50 | int column_start = _image->s()/4; |
|---|
| 51 | int column_end = 3*column_start; |
|---|
| 52 | |
|---|
| 53 | int row_start = _image->t()/4; |
|---|
| 54 | int row_end = 3*row_start; |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | for(int r=row_start; r<row_end; ++r) |
|---|
| 58 | { |
|---|
| 59 | unsigned char* data = _image->data(column_start, r); |
|---|
| 60 | for(int c=column_start; c<column_end; ++c) |
|---|
| 61 | { |
|---|
| 62 | (*data) = (*data)/2; ++data; |
|---|
| 63 | (*data) = (*data)/2; ++data; |
|---|
| 64 | (*data) = (*data)/2; ++data; |
|---|
| 65 | (*data) = 255; ++data; |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | _image->dirty(); |
|---|
| 70 | } |
|---|
| 71 | else if (_image && _image->getPixelFormat()==GL_RGBA && _image->getDataType()==GL_FLOAT) |
|---|
| 72 | { |
|---|
| 73 | |
|---|
| 74 | int column_start = _image->s()/4; |
|---|
| 75 | int column_end = 3*column_start; |
|---|
| 76 | |
|---|
| 77 | int row_start = _image->t()/4; |
|---|
| 78 | int row_end = 3*row_start; |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | for(int r=row_start; r<row_end; ++r) |
|---|
| 82 | { |
|---|
| 83 | float* data = (float*)_image->data(column_start, r); |
|---|
| 84 | for(int c=column_start; c<column_end; ++c) |
|---|
| 85 | { |
|---|
| 86 | (*data) = (*data)/2.0; ++data; |
|---|
| 87 | (*data) = (*data)/2.0; ++data; |
|---|
| 88 | (*data) = (*data)/2.0; ++data; |
|---|
| 89 | (*data) = 1.0f; ++data; |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | _image->dirty(); |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | float* data = (float*)_image->data(0, 0); |
|---|
| 97 | fprintf(stderr,"Float pixel data: r %e g %e b %e\n", data[0], data[1], data[2]); |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | osg::Image* _image; |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | #define NUM_TEXTURES 4 |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | osg::Group* createRTTQuad(unsigned int tex_width, unsigned int tex_height, bool useHDR) |
|---|
| 108 | { |
|---|
| 109 | osg::Group *top_group = new osg::Group; |
|---|
| 110 | |
|---|
| 111 | osg::ref_ptr<osg::Geode> quad_geode = new osg::Geode; |
|---|
| 112 | |
|---|
| 113 | osg::ref_ptr<osg::Vec3Array> quad_coords = new osg::Vec3Array; |
|---|
| 114 | |
|---|
| 115 | quad_coords->push_back(osg::Vec3d(0, 0, -1)); |
|---|
| 116 | quad_coords->push_back(osg::Vec3d(1, 0, -1)); |
|---|
| 117 | quad_coords->push_back(osg::Vec3d(1, 1, -1)); |
|---|
| 118 | quad_coords->push_back(osg::Vec3d(0, 1, -1)); |
|---|
| 119 | |
|---|
| 120 | osg::ref_ptr<osg::Vec2Array> quad_tcoords = new osg::Vec2Array; |
|---|
| 121 | quad_tcoords->push_back(osg::Vec2(0, 0)); |
|---|
| 122 | quad_tcoords->push_back(osg::Vec2(tex_width, 0)); |
|---|
| 123 | quad_tcoords->push_back(osg::Vec2(tex_width, tex_height)); |
|---|
| 124 | quad_tcoords->push_back(osg::Vec2(0, tex_height)); |
|---|
| 125 | |
|---|
| 126 | osg::ref_ptr<osg::Geometry> quad_geom = new osg::Geometry; |
|---|
| 127 | osg::ref_ptr<osg::DrawArrays> quad_da = new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4); |
|---|
| 128 | |
|---|
| 129 | osg::ref_ptr<osg::Vec4Array> quad_colors = new osg::Vec4Array; |
|---|
| 130 | quad_colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 131 | |
|---|
| 132 | quad_geom->setVertexArray(quad_coords.get()); |
|---|
| 133 | quad_geom->setTexCoordArray(0, quad_tcoords.get()); |
|---|
| 134 | quad_geom->addPrimitiveSet(quad_da.get()); |
|---|
| 135 | quad_geom->setColorArray(quad_colors.get()); |
|---|
| 136 | quad_geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 137 | |
|---|
| 138 | osg::StateSet *stateset = quad_geom->getOrCreateStateSet(); |
|---|
| 139 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 140 | |
|---|
| 141 | stateset->addUniform(new osg::Uniform("width", (int)tex_width)); |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | if (useHDR) { |
|---|
| 146 | static const char *shaderSource = { |
|---|
| 147 | "uniform int width;" |
|---|
| 148 | "void main(void)\n" |
|---|
| 149 | "{\n" |
|---|
| 150 | " gl_FragData[0] = vec4(-1e-12,0,0,1);\n" |
|---|
| 151 | " gl_FragData[1] = vec4(0,1e-12,0,1);\n" |
|---|
| 152 | " gl_FragData[2] = vec4(0,0,1e-12,1);\n" |
|---|
| 153 | " gl_FragData[3] = vec4(0,0,1e-12,1);\n" |
|---|
| 154 | "}\n" |
|---|
| 155 | }; |
|---|
| 156 | |
|---|
| 157 | osg::ref_ptr<osg::Shader> fshader = new osg::Shader( osg::Shader::FRAGMENT , shaderSource); |
|---|
| 158 | osg::ref_ptr<osg::Program> program = new osg::Program; |
|---|
| 159 | program->addShader(fshader.get()); |
|---|
| 160 | stateset->setAttributeAndModes(program.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE ); |
|---|
| 161 | } else { |
|---|
| 162 | static const char *shaderSource = { |
|---|
| 163 | "uniform int width;" |
|---|
| 164 | "void main(void)\n" |
|---|
| 165 | "{\n" |
|---|
| 166 | " gl_FragData[0] = vec4(1,0,0,1);\n" |
|---|
| 167 | " gl_FragData[1] = vec4(0,1,0,1);\n" |
|---|
| 168 | " gl_FragData[2] = vec4(0,0,1,1);\n" |
|---|
| 169 | " gl_FragData[3] = vec4(0,0,1,1);\n" |
|---|
| 170 | "}\n" |
|---|
| 171 | }; |
|---|
| 172 | |
|---|
| 173 | osg::ref_ptr<osg::Shader> fshader = new osg::Shader( osg::Shader::FRAGMENT , shaderSource); |
|---|
| 174 | osg::ref_ptr<osg::Program> program = new osg::Program; |
|---|
| 175 | program->addShader(fshader.get()); |
|---|
| 176 | stateset->setAttributeAndModes(program.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE ); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | quad_geode->addDrawable(quad_geom.get()); |
|---|
| 180 | |
|---|
| 181 | top_group->addChild(quad_geode.get()); |
|---|
| 182 | |
|---|
| 183 | return top_group; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | osg::Node* createScene(osg::Node* cam_subgraph, unsigned int tex_width, unsigned int tex_height, bool useHDR, bool useImage) |
|---|
| 189 | { |
|---|
| 190 | if (!cam_subgraph) return 0; |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | osg::Group* parent = new osg::Group; |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | osg::TextureRectangle* textureRect[NUM_TEXTURES] = {0,0,0,0}; |
|---|
| 197 | |
|---|
| 198 | for (int i=0;i<NUM_TEXTURES;i++) { |
|---|
| 199 | textureRect[i] = new osg::TextureRectangle; |
|---|
| 200 | textureRect[i]->setTextureSize(tex_width, tex_height); |
|---|
| 201 | textureRect[i]->setInternalFormat(GL_RGBA); |
|---|
| 202 | textureRect[i]->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); |
|---|
| 203 | textureRect[i]->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); |
|---|
| 204 | |
|---|
| 205 | if (useHDR) |
|---|
| 206 | { |
|---|
| 207 | textureRect[i]->setInternalFormat(GL_FLOAT_RGBA32_NV); |
|---|
| 208 | |
|---|
| 209 | textureRect[i]->setSourceFormat(GL_RGBA); |
|---|
| 210 | textureRect[i]->setSourceType(GL_FLOAT); |
|---|
| 211 | } |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | { |
|---|
| 216 | osg::Geometry* polyGeom = new osg::Geometry(); |
|---|
| 217 | |
|---|
| 218 | polyGeom->setSupportsDisplayList(false); |
|---|
| 219 | |
|---|
| 220 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 221 | osg::Vec2Array* texcoords = new osg::Vec2Array; |
|---|
| 222 | |
|---|
| 223 | vertices->push_back(osg::Vec3d(0,0,0)); |
|---|
| 224 | texcoords->push_back(osg::Vec2(0,0)); |
|---|
| 225 | |
|---|
| 226 | vertices->push_back(osg::Vec3d(1,0,0)); |
|---|
| 227 | texcoords->push_back(osg::Vec2(tex_width,0)); |
|---|
| 228 | |
|---|
| 229 | vertices->push_back(osg::Vec3d(1,0,1)); |
|---|
| 230 | texcoords->push_back(osg::Vec2(tex_width,tex_height)); |
|---|
| 231 | |
|---|
| 232 | vertices->push_back(osg::Vec3d(0,0,1)); |
|---|
| 233 | texcoords->push_back(osg::Vec2(0,tex_height)); |
|---|
| 234 | |
|---|
| 235 | polyGeom->setVertexArray(vertices); |
|---|
| 236 | polyGeom->setTexCoordArray(0,texcoords); |
|---|
| 237 | |
|---|
| 238 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 239 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 240 | polyGeom->setColorArray(colors); |
|---|
| 241 | polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 242 | |
|---|
| 243 | polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,vertices->size())); |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 247 | for (int i=0;i<NUM_TEXTURES;i++){ |
|---|
| 248 | stateset->setTextureAttributeAndModes(i, textureRect[i], osg::StateAttribute::ON); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | polyGeom->setStateSet(stateset); |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | if (useHDR) { |
|---|
| 255 | static const char *shaderSource = { |
|---|
| 256 | "uniform sampler2DRect textureID0;\n" |
|---|
| 257 | "uniform sampler2DRect textureID1;\n" |
|---|
| 258 | "uniform sampler2DRect textureID2;\n" |
|---|
| 259 | "uniform sampler2DRect textureID3;\n" |
|---|
| 260 | "uniform float width;\n" |
|---|
| 261 | "uniform float height; \n" |
|---|
| 262 | "void main(void)\n" |
|---|
| 263 | "{\n" |
|---|
| 264 | " gl_FragData[0] = \n" |
|---|
| 265 | " vec4( -1e12 * texture2DRect( textureID0, gl_TexCoord[0].st ).rgb, 1) + \n" |
|---|
| 266 | " vec4( 1e12 * texture2DRect( textureID1, gl_TexCoord[0].st ).rgb, 1) + \n" |
|---|
| 267 | " vec4( 1e12 * texture2DRect( textureID2, gl_TexCoord[0].st ).rgb, 1) + \n" |
|---|
| 268 | " vec4(-0.5e12 * texture2DRect( textureID3, gl_TexCoord[0].st ).rgb, 1); \n" |
|---|
| 269 | "}\n" |
|---|
| 270 | }; |
|---|
| 271 | osg::ref_ptr<osg::Shader> fshader = new osg::Shader( osg::Shader::FRAGMENT , shaderSource); |
|---|
| 272 | osg::ref_ptr<osg::Program> program = new osg::Program; |
|---|
| 273 | program->addShader( fshader.get()); |
|---|
| 274 | stateset->setAttributeAndModes( program.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE ); |
|---|
| 275 | } else { |
|---|
| 276 | static const char *shaderSource = { |
|---|
| 277 | "uniform sampler2DRect textureID0;\n" |
|---|
| 278 | "uniform sampler2DRect textureID1;\n" |
|---|
| 279 | "uniform sampler2DRect textureID2;\n" |
|---|
| 280 | "uniform sampler2DRect textureID3;\n" |
|---|
| 281 | "void main(void)\n" |
|---|
| 282 | "{\n" |
|---|
| 283 | " gl_FragData[0] = \n" |
|---|
| 284 | " vec4(texture2DRect( textureID0, gl_TexCoord[0].st ).rgb, 1) + \n" |
|---|
| 285 | " vec4(texture2DRect( textureID1, gl_TexCoord[0].st ).rgb, 1) + \n" |
|---|
| 286 | " vec4(texture2DRect( textureID2, gl_TexCoord[0].st ).rgb, 1) + \n" |
|---|
| 287 | " -0.5*vec4(texture2DRect( textureID3, gl_TexCoord[0].st ).rgb, 1); \n" |
|---|
| 288 | "}\n" |
|---|
| 289 | }; |
|---|
| 290 | osg::ref_ptr<osg::Shader> fshader = new osg::Shader( osg::Shader::FRAGMENT , shaderSource); |
|---|
| 291 | osg::ref_ptr<osg::Program> program = new osg::Program; |
|---|
| 292 | program->addShader( fshader.get()); |
|---|
| 293 | stateset->setAttributeAndModes( program.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE ); |
|---|
| 294 | |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | stateset->addUniform(new osg::Uniform("textureID0", 0)); |
|---|
| 298 | stateset->addUniform(new osg::Uniform("textureID1", 1)); |
|---|
| 299 | stateset->addUniform(new osg::Uniform("textureID2", 2)); |
|---|
| 300 | stateset->addUniform(new osg::Uniform("textureID3", 3)); |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | osg::Geode* geode = new osg::Geode(); |
|---|
| 305 | geode->addDrawable(polyGeom); |
|---|
| 306 | |
|---|
| 307 | parent->addChild(geode); |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | { |
|---|
| 312 | osg::Camera* camera = new osg::Camera; |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | camera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f)); |
|---|
| 316 | camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1,0,1)); |
|---|
| 320 | camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
|---|
| 321 | camera->setViewMatrix(osg::Matrix::identity()); |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | camera->setViewport(0, 0, tex_width, tex_height); |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | camera->setRenderOrder(osg::Camera::PRE_RENDER); |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | for (int i=0; i<NUM_TEXTURES; i++) { |
|---|
| 334 | camera->attach(osg::Camera::BufferComponent(osg::Camera::COLOR_BUFFER0+i), textureRect[i]); |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | if (useImage) { |
|---|
| 339 | |
|---|
| 340 | const int tex_to_get = 0; |
|---|
| 341 | |
|---|
| 342 | osg::Image* image = new osg::Image; |
|---|
| 343 | if (useHDR) { |
|---|
| 344 | image->allocateImage(tex_width, tex_height, 1, GL_RGBA, GL_FLOAT); |
|---|
| 345 | } else { |
|---|
| 346 | image->allocateImage(tex_width, tex_height, 1, GL_RGBA, GL_UNSIGNED_BYTE); |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | camera->attach(osg::Camera::BufferComponent(osg::Camera::COLOR_BUFFER0 + tex_to_get), image); |
|---|
| 351 | |
|---|
| 352 | camera->setPostDrawCallback(new MyCameraPostDrawCallback(image)); |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | textureRect[tex_to_get]->setImage(0, image); |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | camera->addChild(cam_subgraph); |
|---|
| 360 | |
|---|
| 361 | parent->addChild(camera); |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | return parent; |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | int main( int argc, char **argv ) |
|---|
| 368 | { |
|---|
| 369 | |
|---|
| 370 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName() + " demonstrates the use of multiple render targets (MRT) using frame buffer objects. A render to texture camera is used to render to four textures using a single shader. The four textures are then combined to texture the viewed geometry."); |
|---|
| 374 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] ..."); |
|---|
| 375 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information."); |
|---|
| 376 | arguments.getApplicationUsage()->addCommandLineOption("--width","Set the width of the render to texture."); |
|---|
| 377 | arguments.getApplicationUsage()->addCommandLineOption("--height","Set the height of the render to texture."); |
|---|
| 378 | arguments.getApplicationUsage()->addCommandLineOption("--image","Render one of the targets to an image, then apply a post draw callback to modify it, and use this image to update a texture."); |
|---|
| 379 | arguments.getApplicationUsage()->addCommandLineOption("--hdr","Use high dynamic range. Create floating point textures to render to."); |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | osgViewer::Viewer viewer(arguments); |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 386 | { |
|---|
| 387 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 388 | return 1; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | unsigned tex_width = 512; |
|---|
| 392 | unsigned tex_height = 512; |
|---|
| 393 | while (arguments.read("--width", tex_width)) {} |
|---|
| 394 | while (arguments.read("--height", tex_height)) {} |
|---|
| 395 | |
|---|
| 396 | bool useHDR = false; |
|---|
| 397 | while (arguments.read("--hdr")) { useHDR = true; } |
|---|
| 398 | |
|---|
| 399 | bool useImage = false; |
|---|
| 400 | while (arguments.read("--image")) { useImage = true; } |
|---|
| 401 | |
|---|
| 402 | osg::Group* subGraph = createRTTQuad(tex_width, tex_height, useHDR); |
|---|
| 403 | |
|---|
| 404 | osg::Group* rootNode = new osg::Group(); |
|---|
| 405 | rootNode->addChild(createScene(subGraph, tex_width, tex_height, useHDR, useImage)); |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | viewer.setSceneData( rootNode ); |
|---|
| 409 | |
|---|
| 410 | return viewer.run(); |
|---|
| 411 | } |
|---|