source: experimental/distortionNG/extViewer.cpp @ 315

Last change on this file since 315 was 315, checked in by Torben Dannhauer, 13 years ago
File size: 8.2 KB
Line 
1#include "extViewer.h"
2
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
12extViewer::extViewer() : Viewer()
13{
14}
15
16extViewer::extViewer(osg::ArgumentParser& arguments) : Viewer(arguments)
17{
18
19}
20
21extViewer::extViewer(const osgViewer::Viewer& viewer, const osg::CopyOp& copyop) : Viewer(viewer, copyop)
22{
23
24}
25
26extViewer::~extViewer()
27{
28
29}
30
31void extViewer::setUpViewForManualDistortion(unsigned int screenNum, osg::Image* intensityMap, const osg::Matrixd& projectorMatrix)
32{
33        OSG_INFO<<"View::setUpViewForManualDistortion(sn="<<screenNum<<", im="<<intensityMap<<")"<<std::endl;
34
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
42        osg::GraphicsContext::ScreenIdentifier si;
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
65        bool applyIntensityMapAsColours = true;
66
67        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
68    if (!gc)
69    {
70        OSG_NOTICE<<"GraphicsWindow has not been created successfully."<<std::endl;
71        return;
72    }
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;
101        camera->setName("Scene cam");
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();
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));
120                geode->addDrawable(createMesh(160, 190));
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    }
160}
161
162osg::Geometry* extViewer::createMesh( unsigned int column, unsigned int row )
163{
164        // Create Quad to render on
165        osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
166
167        geom->setUseDisplayList( false );
168
169
170        // Create vertices and coordinates
171        osg::Vec3Array* vertices = new osg::Vec3Array;
172    osg::Vec2Array* texcoords0 = new osg::Vec2Array;
173    //osg::Vec2Array* texcoords1 = intensityMap==0 ? new osg::Vec2Array : 0;
174    osg::Vec4Array* colors = new osg::Vec4Array;
175
176        geom->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
177
178        for ( unsigned int i=0; i<row; ++i )
179        {
180                for ( unsigned int j=0; j<column; ++j )
181                {
182                        vertices->push_back( osg::Vec3((float)i, (float)j, 0.0f) );
183                        texcoords0->push_back( osg::Vec2((float)i/(float)row, (float)j/(float)column) );
184
185//      if (intensityMap)
186//    {
187//        colors->push_back(intensityMap->getColor(texcoord1));
188//    }
189//    else
190//    {
191        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
192//        if (texcoords1) texcoords1->push_back( texcoord1 );
193//    }
194
195                }
196        }
197
198
199        // Pass the created vertex array to the points geometry object.
200        geom->setUseVertexBufferObjects( true );
201        geom->setVertexArray(vertices);
202
203        geom->setColorArray(colors);
204    geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
205
206    geom->setTexCoordArray(0,texcoords0);
207    //if (texcoords1) geometry->setTexCoordArray(1,texcoords1);
208
209
210
211 //   osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES);
212 //   geometry->addPrimitiveSet(elements);
213
214        for ( unsigned int i=0; i<row-1; ++i )
215        {
216                osg::ref_ptr<osg::DrawElementsUInt> de = new osg::DrawElementsUInt(GL_QUAD_STRIP, column*2);
217                for ( unsigned int j=0; j<column; ++j )
218                {
219                        (*de)[j*2 + 0] = i*column + j;
220                        (*de)[j*2 + 1] = (i+1)*column + j;
221                }
222                geom->addPrimitiveSet( de.get() );
223        }
224
225
226
227        //osg::ComputeBoundsVisitor cbv;
228        //cbv.applyDrawable(geom);
229        //osg::BoundingBox box = cbv.getBoundingBox();
230        //if (!box.valid())
231        //      std::cout << "Invalid bounding box!";
232        //geom->setInitialBound(box);
233
234        /*osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
235        texture->setImage( osgDB::readImageFile("Images/osg256.png") );
236        texture->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR );
237        texture->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR );
238        geom->getOrCreateStateSet()->setTextureAttributeAndModes( 0, texture.get() );*/
239
240        // Create normals
241        //osgUtil::SmoothingVisitor::smooth( *geom );
242
243        return geom.release();
244}
Note: See TracBrowser for help on using the repository browser.