source: experimental/distortionNG/extViewer.cpp @ 320

Last change on this file since 320 was 320, checked in by Torben Dannhauer, 13 years ago
File size: 8.6 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(osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(width,0.0f,0.0f), osg::Vec3(0.0f,height,0.0f), 3, 3));
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("Dist Cam");
157
158        addSlave(camera.get(), osg::Matrixd(), osg::Matrixd(), false);
159    }
160}
161
162osg::Geometry* extViewer::createMesh(const osg::Vec3& origin, const osg::Vec3& widthVector, const osg::Vec3& heightVector, unsigned int columns, unsigned int rows )
163{
164        // Create Quad to render on
165        osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
166
167        geom->setUseDisplayList( false );
168
169        osg::Vec3 xAxis(widthVector);
170    float width = widthVector.length();
171    xAxis /= width;
172
173    osg::Vec3 yAxis(heightVector);
174    float height = heightVector.length();
175    yAxis /= height;
176
177        osg::Vec3 dx = xAxis*(width/((float)(columns-1)));
178    osg::Vec3 dy = yAxis*(height/((float)(rows-1)));
179
180
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
189        for ( unsigned int row=0; row<rows; row++ )
190        {
191                for ( unsigned int col=0; col<columns; col++ )
192                {
193                        vertices->push_back( origin+dy*row+dx*col );
194                        texcoords0->push_back( osg::Vec2((float)col/(float)(columns-1), (float)row/(float)(rows-1)) );
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
206                }
207        }
208
209
210        // Pass the created vertex array to the points geometry object.
211        geom->setUseVertexBufferObjects( true );
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
225        for ( unsigned int row=0; row<rows-1; row++ )   // each strip consists of two affected vertex rows, so we need only row-1 strips.
226        {
227                osg::ref_ptr<osg::DrawElementsUInt> de = new osg::DrawElementsUInt(GL_QUAD_STRIP, columns*2);   // columns*2 = number of involved vertices for this strip.
228                for ( unsigned int col=0; col<columns; col++ )
229                {
230                        (*de)[col*2 + 0] = row*columns + col;
231                        (*de)[col*2 + 1] = (row+1)*columns + col;
232                }
233                geom->addPrimitiveSet( de.get() );
234        }
235
236
237
238        /*osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
239        texture->setImage( osgDB::readImageFile("Images/osg256.png") );
240        texture->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR );
241        texture->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR );
242        geom->getOrCreateStateSet()->setTextureAttributeAndModes( 0, texture.get() );*/
243
244
245        return geom.release();
246}
Note: See TracBrowser for help on using the repository browser.