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 | |
---|
13 | extViewer::extViewer() : Viewer() |
---|
14 | { |
---|
15 | } |
---|
16 | |
---|
17 | extViewer::extViewer(osg::ArgumentParser& arguments) : Viewer(arguments) |
---|
18 | { |
---|
19 | |
---|
20 | } |
---|
21 | |
---|
22 | extViewer::extViewer(const osgViewer::Viewer& viewer, const osg::CopyOp& copyop) : Viewer(viewer, copyop) |
---|
23 | { |
---|
24 | |
---|
25 | } |
---|
26 | |
---|
27 | extViewer::~extViewer() |
---|
28 | { |
---|
29 | |
---|
30 | } |
---|
31 | |
---|
32 | void 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 | int tex_width = width; |
---|
76 | int tex_height = height; |
---|
77 | |
---|
78 | int camera_width = tex_width; |
---|
79 | int camera_height = tex_height; |
---|
80 | |
---|
81 | osg::TextureRectangle* texture = new osg::TextureRectangle; |
---|
82 | |
---|
83 | texture->setTextureSize(tex_width, tex_height); |
---|
84 | texture->setInternalFormat(GL_RGB); |
---|
85 | texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); |
---|
86 | texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); |
---|
87 | texture->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_EDGE); |
---|
88 | texture->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_EDGE); |
---|
89 | |
---|
90 | |
---|
91 | #if 0 |
---|
92 | osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::SEPERATE_WINDOW; |
---|
93 | GLenum buffer = GL_FRONT; |
---|
94 | #else |
---|
95 | osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::FRAME_BUFFER_OBJECT; |
---|
96 | GLenum buffer = GL_FRONT; |
---|
97 | #endif |
---|
98 | |
---|
99 | // Scene camera |
---|
100 | { |
---|
101 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
---|
102 | camera->setName("Scene cam"); |
---|
103 | camera->setGraphicsContext(gc.get()); |
---|
104 | camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); |
---|
105 | camera->setDrawBuffer(buffer); |
---|
106 | camera->setReadBuffer(buffer); |
---|
107 | camera->setAllowEventFocus(false); |
---|
108 | // tell the camera to use OpenGL frame buffer object where supported. |
---|
109 | camera->setRenderTargetImplementation(renderTargetImplementation); |
---|
110 | |
---|
111 | // attach the texture and use it as the color buffer. |
---|
112 | camera->attach(osg::Camera::COLOR_BUFFER, texture); |
---|
113 | |
---|
114 | addSlave(camera.get(), osg::Matrixd(), osg::Matrixd()); |
---|
115 | } |
---|
116 | |
---|
117 | // distortion correction set up. |
---|
118 | { |
---|
119 | osg::Geode* geode = new osg::Geode(); |
---|
120 | //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)); |
---|
121 | 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), 40, 40)); |
---|
122 | |
---|
123 | // new we need to add the texture to the mesh, we do so by creating a |
---|
124 | // StateSet to contain the Texture StateAttribute. |
---|
125 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
---|
126 | stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON); |
---|
127 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
---|
128 | |
---|
129 | osg::TexMat* texmat = new osg::TexMat; |
---|
130 | texmat->setScaleByTextureRectangleSize(true); |
---|
131 | stateset->setTextureAttributeAndModes(0, texmat, osg::StateAttribute::ON); |
---|
132 | |
---|
133 | if (!applyIntensityMapAsColours && intensityMap) |
---|
134 | { |
---|
135 | stateset->setTextureAttributeAndModes(1, new osg::Texture2D(intensityMap), osg::StateAttribute::ON); |
---|
136 | } |
---|
137 | |
---|
138 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
---|
139 | camera->setGraphicsContext(gc.get()); |
---|
140 | camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); |
---|
141 | camera->setClearColor( osg::Vec4(0.0,0.0,0.0,1.0) ); |
---|
142 | camera->setViewport(new osg::Viewport(0, 0, width, height)); |
---|
143 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
---|
144 | camera->setDrawBuffer(buffer); |
---|
145 | camera->setReadBuffer(buffer); |
---|
146 | camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF); |
---|
147 | camera->setAllowEventFocus(false); |
---|
148 | camera->setInheritanceMask(camera->getInheritanceMask() & ~osg::CullSettings::CLEAR_COLOR & ~osg::CullSettings::COMPUTE_NEAR_FAR_MODE); |
---|
149 | //camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); |
---|
150 | |
---|
151 | camera->setProjectionMatrixAsOrtho2D(0,width,0,height); |
---|
152 | camera->setViewMatrix(osg::Matrix::identity()); |
---|
153 | |
---|
154 | // add subgraph to render |
---|
155 | camera->addChild(geode); |
---|
156 | |
---|
157 | camera->setName("Dist Cam"); |
---|
158 | |
---|
159 | addSlave(camera.get(), osg::Matrixd(), osg::Matrixd(), false); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | osg::Geometry* extViewer::createMesh(const osg::Vec3& origin, const osg::Vec3& widthVector, const osg::Vec3& heightVector, unsigned int columns, unsigned int rows ) |
---|
164 | { |
---|
165 | // Create Quad to render on |
---|
166 | osg::ref_ptr<osg::Geometry> geom = new osg::Geometry; |
---|
167 | |
---|
168 | geom->setUseDisplayList( false ); |
---|
169 | |
---|
170 | osg::Vec3 xAxis(widthVector); |
---|
171 | float width = widthVector.length(); |
---|
172 | xAxis /= width; |
---|
173 | |
---|
174 | osg::Vec3 yAxis(heightVector); |
---|
175 | float height = heightVector.length(); |
---|
176 | yAxis /= height; |
---|
177 | |
---|
178 | osg::Vec3 dx = xAxis*(width/((float)(columns-1))); |
---|
179 | osg::Vec3 dy = yAxis*(height/((float)(rows-1))); |
---|
180 | |
---|
181 | |
---|
182 | // Create vertices and coordinates |
---|
183 | osg::Vec3Array* vertices = new osg::Vec3Array; |
---|
184 | osg::Vec2Array* texcoords0 = new osg::Vec2Array; |
---|
185 | //osg::Vec2Array* texcoords1 = intensityMap==0 ? new osg::Vec2Array : 0; |
---|
186 | osg::Vec4Array* colors = new osg::Vec4Array; |
---|
187 | |
---|
188 | geom->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED); |
---|
189 | |
---|
190 | for ( unsigned int row=0; row<rows; row++ ) |
---|
191 | { |
---|
192 | for ( unsigned int col=0; col<columns; col++ ) |
---|
193 | { |
---|
194 | vertices->push_back( origin+dy*row+dx*col ); |
---|
195 | osg::Vec2 texcoord = osg::Vec2((float)col/(float)(columns-1), (float)row/(float)(rows-1)); |
---|
196 | texcoords0->push_back( texcoord ); |
---|
197 | |
---|
198 | // if (intensityMap) |
---|
199 | // { |
---|
200 | // colors->push_back(intensityMap->getColor(texcoord1)); |
---|
201 | // } |
---|
202 | // else |
---|
203 | // { |
---|
204 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
---|
205 | // if (texcoords1) texcoords1->push_back( texcoord1 ); |
---|
206 | // } |
---|
207 | |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | // Pass the created vertex array to the points geometry object. |
---|
213 | geom->setUseVertexBufferObjects( true ); |
---|
214 | geom->setVertexArray(vertices); |
---|
215 | |
---|
216 | geom->setColorArray(colors); |
---|
217 | geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
---|
218 | |
---|
219 | geom->setTexCoordArray(0,texcoords0); |
---|
220 | //if (texcoords1) geometry->setTexCoordArray(1,texcoords1); |
---|
221 | |
---|
222 | |
---|
223 | |
---|
224 | // osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES); |
---|
225 | // geometry->addPrimitiveSet(elements); |
---|
226 | |
---|
227 | for ( unsigned int row=0; row<rows-1; row++ ) // each strip consists of two affected vertex rows, so we need only row-1 strips. |
---|
228 | { |
---|
229 | osg::ref_ptr<osg::DrawElementsUInt> de = new osg::DrawElementsUInt(GL_QUAD_STRIP, columns*2); // columns*2 = number of involved vertices for this strip. |
---|
230 | for ( unsigned int col=0; col<columns; col++ ) |
---|
231 | { |
---|
232 | (*de)[col*2 + 0] = row*columns + col; |
---|
233 | (*de)[col*2 + 1] = (row+1)*columns + col; |
---|
234 | } |
---|
235 | geom->addPrimitiveSet( de.get() ); |
---|
236 | } |
---|
237 | |
---|
238 | return geom.release(); |
---|
239 | } |
---|