source: experimental/distortionNG/extViewer.cpp @ 328

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