source: experimental/distortionNG/distortionNG.cpp @ 310

Last change on this file since 310 was 310, checked in by Torben Dannhauer, 13 years ago
File size: 6.0 KB
Line 
1#include "distortionNG.h"
2
3#include<osg/Texture2D>
4#include<osg/Point>
5#include<osg/ComputeBoundsVisitor>
6
7#include<osgViewer/Viewer>
8
9#include<osgDB/ReadFile>
10
11#include <osgUtil/SmoothingVisitor>
12
13
14
15distortionNG::distortionNG()
16{
17}
18
19distortionNG::~distortionNG()
20{
21}
22
23osg::Geometry* distortionNG::createMesh( unsigned int column, unsigned int row )
24{
25        osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array(column * row);
26        osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array(column * row);
27        for ( unsigned int i=0; i<row; ++i )
28        {
29                for ( unsigned int j=0; j<column; ++j )
30                {
31                        (*vertices)[i*column + j].set( (float)i, (float)j, 0.0f );
32                        (*texcoords)[i*column + j].set( (float)i/(float)row, (float)j/(float)column );
33                }
34        }
35
36        osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
37        geom->setUseDisplayList( false );
38        geom->setUseVertexBufferObjects( true );
39        geom->setVertexArray( vertices.get() );
40        geom->setTexCoordArray( 0, texcoords.get() );
41        for ( unsigned int i=0; i<row-1; ++i )
42        {
43                osg::ref_ptr<osg::DrawElementsUInt> de = new osg::DrawElementsUInt(GL_QUAD_STRIP, column*2);
44                for ( unsigned int j=0; j<column; ++j )
45                {
46                        (*de)[j*2 + 0] = i*column + j;
47                        (*de)[j*2 + 1] = (i+1)*column + j;
48                }
49                geom->addPrimitiveSet( de.get() );
50        }
51
52
53
54        osg::ComputeBoundsVisitor cbv;
55        cbv.applyDrawable(geom);
56        osg::BoundingBox box = cbv.getBoundingBox();
57        if (!box.valid()) 
58                std::cout << "Invalid bounding box!"; 
59        geom->setInitialBound(box);
60
61        osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
62        texture->setImage( osgDB::readImageFile("Images/osg256.png") );
63        texture->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR );
64        texture->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR );
65        geom->getOrCreateStateSet()->setTextureAttributeAndModes( 0, texture.get() );
66
67        // Create normals
68        osgUtil::SmoothingVisitor::smooth( *geom );
69
70        return geom.release();
71}
72
73osg::Geode* distortionHandler::createVertexHighlighter()
74{
75        osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1);
76        (*colors)[0] = _highlightColor;
77
78        _highlighter = new osg::Geometry;
79        _highlighter->setDataVariance( osg::Object::DYNAMIC );
80        _highlighter->setUseDisplayList( false );
81        _highlighter->setUseVertexBufferObjects( true );
82        _highlighter->setVertexArray( new osg::Vec3Array(1) );
83        _highlighter->setColorArray( colors.get() );
84        _highlighter->setColorBinding( osg::Geometry::BIND_OVERALL );
85        _highlighter->addPrimitiveSet( new osg::DrawArrays(GL_POINTS, 0, 1) );
86
87        osg::ref_ptr<osg::Geode> geode = new osg::Geode;
88        geode->addDrawable( _highlighter.get() );
89        geode->getOrCreateStateSet()->setAttributeAndModes( new osg::Point(8.0f) );
90        geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
91
92        return geode.release();
93}
94
95bool distortionHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
96{
97        // Only pressing CTRL activates distortion editing
98        if ( !(ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL) )
99                return false;   // False means not handled.
100
101
102    if ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON )
103        {
104                osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
105                if ( viewer )
106                {
107                        osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
108                        osgUtil::IntersectionVisitor iv( intersector.get() );
109                        viewer->getCamera()->accept( iv );
110               
111                        if ( intersector->containsIntersections() )
112                        {
113                                osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin());
114                                computeSelectedVertex( result );
115                        }
116                }
117        }
118
119        if ( ea.getEventType() == osgGA::GUIEventAdapter::DRAG  )
120        {
121        osg::notify(osg::ALWAYS)<<std::endl<<"ea.getGraphicsContext()="<<ea.getGraphicsContext()<<std::endl;
122        osg::notify(osg::ALWAYS)<<"ea.getXnormalized()="<<ea.getXnormalized()<<std::endl;
123        osg::notify(osg::ALWAYS)<<"ea.getYnormalized()="<<ea.getYnormalized()<<std::endl;
124        osg::notify(osg::ALWAYS)<<"ea.getX()="<<ea.getX()<<std::endl;
125        osg::notify(osg::ALWAYS)<<"ea.getXin()="<<ea.getXmin()<<std::endl;
126        osg::notify(osg::ALWAYS)<<"ea.getXmax()="<<ea.getXmax()<<std::endl;
127        osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl;
128        osg::notify(osg::ALWAYS)<<"ea.getYin()="<<ea.getYmin()<<std::endl;
129        osg::notify(osg::ALWAYS)<<"ea.getYmax()="<<ea.getYmax()<<std::endl;
130
131                return true;    // true means event handeld: not forwarded to the camera manipulator;
132        }
133
134
135    return false;       // False means not handled.
136}
137
138void distortionHandler::computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result )
139{
140        osg::Geometry* geom = dynamic_cast<osg::Geometry*>( result.drawable.get() );
141        if ( !geom || !_highlighter || geom==_highlighter )
142                return;
143
144        osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() );
145        osg::Vec3Array* selVertices = dynamic_cast<osg::Vec3Array*>( _highlighter->getVertexArray() );
146        if ( !vertices || !selVertices )
147                return;
148
149        osg::Vec3 point = result.getWorldIntersectPoint();
150        osg::Matrix matrix = osg::computeLocalToWorld( result.nodePath );       // To compute the intersection vertices in world coordinates not in model coordinates
151        //std::cout << "Intersection-indices: Size=" << result.indexList.size() << std::endl;
152        const std::vector<unsigned int>& selIndices = result.indexList;
153        {
154                double maxRatio = 0.0;
155                int closestVertexIndex = 0;
156                for ( unsigned int i=0; i<3 && i<result.ratioList.size(); i++ ) //iterate through rations and search for max
157                {
158                        if(result.ratioList[i] > maxRatio)
159                        {
160                                maxRatio = result.ratioList[i];
161                                closestVertexIndex = result.indexList[i];
162                        }
163                }
164                osg::Vec3 vertex = (*vertices)[closestVertexIndex] * matrix;
165                selVertices->front() = vertex;
166        }
167
168        selVertices->dirty();
169        _highlighter->dirtyBound(); 
170}
Note: See TracBrowser for help on using the repository browser.