[310] | 1 | #include "extViewer.h" |
---|
| 2 | |
---|
[313] | 3 | #include<osg/Texture2D> |
---|
| 4 | #include<osg/TextureRectangle> |
---|
| 5 | #include<osg/TexMat> |
---|
| 6 | #include<osg/ComputeBoundsVisitor> |
---|
| 7 | |
---|
| 8 | #include<osgDB/ReadFile> |
---|
| 9 | |
---|
| 10 | #include <osgUtil/SmoothingVisitor> |
---|
| 11 | |
---|
[311] | 12 | extViewer::extViewer() : Viewer() |
---|
[310] | 13 | { |
---|
| 14 | } |
---|
| 15 | |
---|
[311] | 16 | extViewer::extViewer(osg::ArgumentParser& arguments) : Viewer(arguments) |
---|
[310] | 17 | { |
---|
[315] | 18 | |
---|
[310] | 19 | } |
---|
[311] | 20 | |
---|
| 21 | extViewer::extViewer(const osgViewer::Viewer& viewer, const osg::CopyOp& copyop) : Viewer(viewer, copyop) |
---|
| 22 | { |
---|
| 23 | |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | extViewer::~extViewer() |
---|
| 27 | { |
---|
| 28 | |
---|
| 29 | } |
---|
| 30 | |
---|
[315] | 31 | void extViewer::setUpViewForManualDistortion(unsigned int screenNum, osg::Image* intensityMap, const osg::Matrixd& projectorMatrix) |
---|
[311] | 32 | { |
---|
[313] | 33 | OSG_INFO<<"View::setUpViewForManualDistortion(sn="<<screenNum<<", im="<<intensityMap<<")"<<std::endl; |
---|
| 34 | |
---|
[311] | 35 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
---|
| 36 | if (!wsi) |
---|
| 37 | { |
---|
| 38 | OSG_NOTICE<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl; |
---|
| 39 | return; |
---|
| 40 | } |
---|
| 41 | |
---|
[313] | 42 | osg::GraphicsContext::ScreenIdentifier si; |
---|
[311] | 43 | si.readDISPLAY(); |
---|
| 44 | |
---|
| 45 | // displayNum has not been set so reset it to 0. |
---|
| 46 | if (si.displayNum<0) si.displayNum = 0; |
---|
| 47 | |
---|
| 48 | si.screenNum = screenNum; |
---|
| 49 | |
---|
| 50 | unsigned int width, height; |
---|
| 51 | wsi->getScreenResolution(si, width, height); |
---|
| 52 | |
---|
| 53 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
---|
| 54 | traits->hostName = si.hostName; |
---|
| 55 | traits->displayNum = si.displayNum; |
---|
| 56 | traits->screenNum = si.screenNum; |
---|
| 57 | traits->x = 0; |
---|
| 58 | traits->y = 0; |
---|
| 59 | traits->width = width; |
---|
| 60 | traits->height = height; |
---|
| 61 | traits->windowDecoration = false; |
---|
| 62 | traits->doubleBuffer = true; |
---|
| 63 | traits->sharedContext = 0; |
---|
| 64 | |
---|
[313] | 65 | bool applyIntensityMapAsColours = true; |
---|
[311] | 66 | |
---|
[313] | 67 | osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
---|
[311] | 68 | if (!gc) |
---|
| 69 | { |
---|
| 70 | OSG_NOTICE<<"GraphicsWindow has not been created successfully."<<std::endl; |
---|
| 71 | return; |
---|
| 72 | } |
---|
[313] | 73 | |
---|
| 74 | int tex_width = width; |
---|
| 75 | int tex_height = height; |
---|
| 76 | |
---|
| 77 | int camera_width = tex_width; |
---|
| 78 | int camera_height = tex_height; |
---|
| 79 | |
---|
| 80 | osg::TextureRectangle* texture = new osg::TextureRectangle; |
---|
| 81 | |
---|
| 82 | texture->setTextureSize(tex_width, tex_height); |
---|
| 83 | texture->setInternalFormat(GL_RGB); |
---|
| 84 | texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); |
---|
| 85 | texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); |
---|
| 86 | texture->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_EDGE); |
---|
| 87 | texture->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_EDGE); |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | #if 0 |
---|
| 91 | osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::SEPERATE_WINDOW; |
---|
| 92 | GLenum buffer = GL_FRONT; |
---|
| 93 | #else |
---|
| 94 | osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::FRAME_BUFFER_OBJECT; |
---|
| 95 | GLenum buffer = GL_FRONT; |
---|
| 96 | #endif |
---|
| 97 | |
---|
| 98 | // Scene camera |
---|
| 99 | { |
---|
| 100 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
---|
[315] | 101 | camera->setName("Scene cam"); |
---|
[313] | 102 | camera->setGraphicsContext(gc.get()); |
---|
| 103 | camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); |
---|
| 104 | camera->setDrawBuffer(buffer); |
---|
| 105 | camera->setReadBuffer(buffer); |
---|
| 106 | camera->setAllowEventFocus(false); |
---|
| 107 | // tell the camera to use OpenGL frame buffer object where supported. |
---|
| 108 | camera->setRenderTargetImplementation(renderTargetImplementation); |
---|
| 109 | |
---|
| 110 | // attach the texture and use it as the color buffer. |
---|
| 111 | camera->attach(osg::Camera::COLOR_BUFFER, texture); |
---|
| 112 | |
---|
| 113 | addSlave(camera.get(), osg::Matrixd(), osg::Matrixd()); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | // distortion correction set up. |
---|
| 117 | { |
---|
| 118 | osg::Geode* geode = new osg::Geode(); |
---|
[315] | 119 | //geode->addDrawable(createParoramicSphericalDisplayDistortionMesh(osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(width,0.0f,0.0f), osg::Vec3(0.0f,height,0.0f), 1, 0.45, 0, projectorMatrix)); |
---|
[316] | 120 | geode->addDrawable(createMesh(osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(width,0.0f,0.0f), osg::Vec3(0.0f,height,0.0f), 160, 190)); |
---|
[313] | 121 | |
---|
| 122 | // new we need to add the texture to the mesh, we do so by creating a |
---|
| 123 | // StateSet to contain the Texture StateAttribute. |
---|
| 124 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
---|
| 125 | stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON); |
---|
| 126 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
---|
| 127 | |
---|
| 128 | osg::TexMat* texmat = new osg::TexMat; |
---|
| 129 | texmat->setScaleByTextureRectangleSize(true); |
---|
| 130 | stateset->setTextureAttributeAndModes(0, texmat, osg::StateAttribute::ON); |
---|
| 131 | |
---|
| 132 | if (!applyIntensityMapAsColours && intensityMap) |
---|
| 133 | { |
---|
| 134 | stateset->setTextureAttributeAndModes(1, new osg::Texture2D(intensityMap), osg::StateAttribute::ON); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
---|
| 138 | camera->setGraphicsContext(gc.get()); |
---|
| 139 | camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); |
---|
| 140 | camera->setClearColor( osg::Vec4(0.0,0.0,0.0,1.0) ); |
---|
| 141 | camera->setViewport(new osg::Viewport(0, 0, width, height)); |
---|
| 142 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
---|
| 143 | camera->setDrawBuffer(buffer); |
---|
| 144 | camera->setReadBuffer(buffer); |
---|
| 145 | camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF); |
---|
| 146 | camera->setAllowEventFocus(false); |
---|
| 147 | camera->setInheritanceMask(camera->getInheritanceMask() & ~osg::CullSettings::CLEAR_COLOR & ~osg::CullSettings::COMPUTE_NEAR_FAR_MODE); |
---|
| 148 | //camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); |
---|
| 149 | |
---|
| 150 | camera->setProjectionMatrixAsOrtho2D(0,width,0,height); |
---|
| 151 | camera->setViewMatrix(osg::Matrix::identity()); |
---|
| 152 | |
---|
| 153 | // add subgraph to render |
---|
| 154 | camera->addChild(geode); |
---|
| 155 | |
---|
| 156 | camera->setName("DistortionCorrectionCamera"); |
---|
| 157 | |
---|
| 158 | addSlave(camera.get(), osg::Matrixd(), osg::Matrixd(), false); |
---|
| 159 | } |
---|
[311] | 160 | } |
---|
[313] | 161 | |
---|
[316] | 162 | osg::Geometry* extViewer::createMesh(const osg::Vec3& origin, const osg::Vec3& widthVector, const osg::Vec3& heightVector, unsigned int columns, unsigned int rows ) |
---|
[313] | 163 | { |
---|
[314] | 164 | // Create Quad to render on |
---|
| 165 | osg::ref_ptr<osg::Geometry> geom = new osg::Geometry; |
---|
| 166 | |
---|
| 167 | geom->setUseDisplayList( false ); |
---|
| 168 | |
---|
[316] | 169 | osg::Vec3 xAxis(widthVector); |
---|
| 170 | float width = widthVector.length(); |
---|
| 171 | xAxis /= width; |
---|
[314] | 172 | |
---|
[316] | 173 | osg::Vec3 yAxis(heightVector); |
---|
| 174 | float height = heightVector.length(); |
---|
| 175 | yAxis /= height; |
---|
| 176 | |
---|
| 177 | osg::Vec3 dx = xAxis*(width/((float)(columns))); |
---|
| 178 | osg::Vec3 dy = yAxis*(height/((float)(rows))); |
---|
| 179 | |
---|
| 180 | |
---|
[314] | 181 | // Create vertices and coordinates |
---|
| 182 | osg::Vec3Array* vertices = new osg::Vec3Array; |
---|
| 183 | osg::Vec2Array* texcoords0 = new osg::Vec2Array; |
---|
| 184 | //osg::Vec2Array* texcoords1 = intensityMap==0 ? new osg::Vec2Array : 0; |
---|
| 185 | osg::Vec4Array* colors = new osg::Vec4Array; |
---|
| 186 | |
---|
| 187 | geom->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED); |
---|
| 188 | |
---|
[316] | 189 | for ( unsigned int i=0; i<rows; ++i ) |
---|
[313] | 190 | { |
---|
[316] | 191 | for ( unsigned int j=0; j<columns; ++j ) |
---|
[313] | 192 | { |
---|
[316] | 193 | vertices->push_back( origin+dy*i+dx*j ); |
---|
| 194 | texcoords0->push_back( osg::Vec2((float)j/(float)columns, (float)i/(float)rows) ); |
---|
[314] | 195 | |
---|
| 196 | // if (intensityMap) |
---|
| 197 | // { |
---|
| 198 | // colors->push_back(intensityMap->getColor(texcoord1)); |
---|
| 199 | // } |
---|
| 200 | // else |
---|
| 201 | // { |
---|
| 202 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
---|
| 203 | // if (texcoords1) texcoords1->push_back( texcoord1 ); |
---|
| 204 | // } |
---|
| 205 | |
---|
[313] | 206 | } |
---|
| 207 | } |
---|
| 208 | |
---|
[314] | 209 | |
---|
| 210 | // Pass the created vertex array to the points geometry object. |
---|
[313] | 211 | geom->setUseVertexBufferObjects( true ); |
---|
[314] | 212 | geom->setVertexArray(vertices); |
---|
| 213 | |
---|
| 214 | geom->setColorArray(colors); |
---|
| 215 | geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
---|
| 216 | |
---|
| 217 | geom->setTexCoordArray(0,texcoords0); |
---|
| 218 | //if (texcoords1) geometry->setTexCoordArray(1,texcoords1); |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | // osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES); |
---|
| 223 | // geometry->addPrimitiveSet(elements); |
---|
| 224 | |
---|
[316] | 225 | for ( unsigned int i=0; i<rows-1; ++i ) |
---|
[313] | 226 | { |
---|
[316] | 227 | osg::ref_ptr<osg::DrawElementsUInt> de = new osg::DrawElementsUInt(GL_QUAD_STRIP, columns*2); |
---|
| 228 | for ( unsigned int j=0; j<columns; ++j ) |
---|
[313] | 229 | { |
---|
[316] | 230 | (*de)[j*2 + 0] = i*columns + j; |
---|
| 231 | (*de)[j*2 + 1] = (i+1)*columns + j; |
---|
[313] | 232 | } |
---|
| 233 | geom->addPrimitiveSet( de.get() ); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | |
---|
| 237 | |
---|
[315] | 238 | //osg::ComputeBoundsVisitor cbv; |
---|
| 239 | //cbv.applyDrawable(geom); |
---|
| 240 | //osg::BoundingBox box = cbv.getBoundingBox(); |
---|
| 241 | //if (!box.valid()) |
---|
| 242 | // std::cout << "Invalid bounding box!"; |
---|
| 243 | //geom->setInitialBound(box); |
---|
[313] | 244 | |
---|
[315] | 245 | /*osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D; |
---|
[313] | 246 | texture->setImage( osgDB::readImageFile("Images/osg256.png") ); |
---|
| 247 | texture->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR ); |
---|
| 248 | texture->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR ); |
---|
[315] | 249 | geom->getOrCreateStateSet()->setTextureAttributeAndModes( 0, texture.get() );*/ |
---|
[313] | 250 | |
---|
| 251 | // Create normals |
---|
[315] | 252 | //osgUtil::SmoothingVisitor::smooth( *geom ); |
---|
[313] | 253 | |
---|
| 254 | return geom.release(); |
---|
| 255 | } |
---|