root/OpenSceneGraph/trunk/src/osgVolume/FixedFunctionTechnique.cpp @ 12292

Revision 12292, 10.1 kB (checked in by robert, 3 years ago)

Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under 
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#include <osgVolume/FixedFunctionTechnique>
15#include <osgVolume/VolumeTile>
16
17#include <osg/Billboard>
18#include <osg/Texture3D>
19#include <osg/Geometry>
20#include <osg/ClipNode>
21#include <osg/Material>
22#include <osg/TexGenNode>
23#include <osg/AlphaFunc>
24#include <osg/BlendFunc>
25#include <osg/BlendEquation>
26#include <osg/TexEnv>
27#include <osg/io_utils>
28
29using namespace osgVolume;
30
31FixedFunctionTechnique::FixedFunctionTechnique():
32    _numSlices(500)
33{
34}
35
36FixedFunctionTechnique::FixedFunctionTechnique(const FixedFunctionTechnique& fft,const osg::CopyOp& copyop):
37    VolumeTechnique(fft,copyop),
38    _numSlices(fft._numSlices)
39{
40}
41
42FixedFunctionTechnique::~FixedFunctionTechnique()
43{
44}
45
46void FixedFunctionTechnique::setNumSlices(unsigned int numSlices)
47{
48    if (_numSlices==numSlices) return;
49   
50    _numSlices = numSlices;
51   
52    if (_volumeTile) _volumeTile->setDirty(true);
53}
54 
55osg::Node* createCube(const osg::Vec3& center, float size, unsigned int numSlices)
56{
57
58    // set up the Geometry.
59    osg::Geometry* geom = new osg::Geometry;
60
61    float halfSize = size*0.5f;
62    float y = halfSize;
63    float dy =-size/(float)(numSlices-1);
64
65    //y = -halfSize;
66    //dy *= 0.5;
67
68    osg::Vec3Array* coords = new osg::Vec3Array(4*numSlices);
69    geom->setVertexArray(coords);
70    for(unsigned int i=0;i<numSlices;++i, y+=dy)
71    {
72        (*coords)[i*4+0].set(-halfSize,y,halfSize);
73        (*coords)[i*4+1].set(-halfSize,y,-halfSize);
74        (*coords)[i*4+2].set(halfSize,y,-halfSize);
75        (*coords)[i*4+3].set(halfSize,y,halfSize);
76    }
77   
78    osg::Vec3Array* normals = new osg::Vec3Array(1);
79    (*normals)[0].set(0.0f,-1.0f,0.0f);
80    geom->setNormalArray(normals);
81    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
82
83    osg::Vec4Array* colors = new osg::Vec4Array(1);
84    (*colors)[0].set(1.0f,1.0f,1.0f,1.0);
85    geom->setColorArray(colors);
86    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
87
88    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,coords->size()));
89
90    osg::Billboard* billboard = new osg::Billboard;
91    billboard->setMode(osg::Billboard::POINT_ROT_WORLD);
92    billboard->addDrawable(geom);
93    billboard->setPosition(0,center);
94   
95    return billboard;
96}
97
98
99void FixedFunctionTechnique::init()
100{
101    OSG_INFO<<"FixedFunctionTechnique::init()"<<std::endl;
102
103    if (!_volumeTile)
104    {
105        OSG_NOTICE<<"FixedFunctionTechnique::init(), error no volume tile assigned."<<std::endl;
106        return;
107    }
108
109    if (_volumeTile->getLayer()==0)
110    {
111        OSG_NOTICE<<"FixedFunctionTechnique::init(), error no layer assigend to volume tile."<<std::endl;
112        return;
113    }
114
115    if (_volumeTile->getLayer()->getImage()==0)
116    {
117        OSG_NOTICE<<"FixedFunctionTechnique::init(), error no image assigned to layer."<<std::endl;
118        return;
119    }
120
121    float alphaFuncValue = 0.1;
122   
123    osg::Image* image_3d = 0;
124    osg::TransferFunction1D* tf = 0;
125    osgVolume::Locator* masterLocator = _volumeTile->getLocator();
126    osg::Texture::InternalFormatMode internalFormatMode = osg::Texture::USE_IMAGE_DATA_FORMAT;
127
128    image_3d = _volumeTile->getLayer()->getImage();
129   
130    CollectPropertiesVisitor cpv;
131    if (_volumeTile->getLayer()->getProperty())
132    {
133        _volumeTile->getLayer()->getProperty()->accept(cpv);
134    }
135
136    if (cpv._isoProperty.valid())
137    {
138        alphaFuncValue = cpv._isoProperty->getValue();
139    }
140
141    if (cpv._tfProperty.valid())
142    {
143        tf = dynamic_cast<osg::TransferFunction1D*>(cpv._tfProperty->getTransferFunction());
144    }
145
146    if (cpv._afProperty.valid())
147    {
148        alphaFuncValue = cpv._afProperty->getValue();
149    }
150
151
152    if (_volumeTile->getLayer() && !masterLocator)
153    {
154        masterLocator = _volumeTile->getLayer()->getLocator();
155        OSG_NOTICE<<"assigning locator = "<<masterLocator<<std::endl;
156    }
157
158    osg::Matrix matrix;
159    if (masterLocator)
160    {
161        matrix = masterLocator->getTransform();
162    }
163   
164    OSG_NOTICE<<"Matrix = "<<matrix<<std::endl;
165
166    osg::Texture::FilterMode minFilter = osg::Texture::NEAREST;
167    osg::Texture::FilterMode magFilter = osg::Texture::NEAREST;
168
169    osg::Vec3d v000 = osg::Vec3d(0.0,0.0,0.0) * matrix;
170    osg::Vec3d v100 = osg::Vec3d(1.0,0.0,0.0) * matrix;
171    osg::Vec3d v010 = osg::Vec3d(0.0,1.0,0.0) * matrix;
172    osg::Vec3d v110 = osg::Vec3d(1.0,1.0,0.0) * matrix;
173   
174    osg::Vec3d v001 = osg::Vec3d(0.0,0.0,1.0) * matrix;
175    osg::Vec3d v101 = osg::Vec3d(1.0,0.0,1.0) * matrix;
176    osg::Vec3d v011 = osg::Vec3d(0.0,1.0,1.0) * matrix;
177    osg::Vec3d v111 = osg::Vec3d(1.0,1.0,1.0) * matrix;
178   
179    double cubeSize = (v111-v000).length();
180    osg::Vec3d center = (v000+v111)*0.5;
181
182    osg::ClipNode* clipnode = new osg::ClipNode;
183    clipnode->addChild(createCube(center, cubeSize, _numSlices));
184
185    clipnode->addClipPlane(new osg::ClipPlane(0, osg::Plane(v000, v011, v001)));
186    clipnode->addClipPlane(new osg::ClipPlane(1, osg::Plane(v100, v111, v110)));
187    clipnode->addClipPlane(new osg::ClipPlane(2, osg::Plane(v000, v101, v100)));
188    clipnode->addClipPlane(new osg::ClipPlane(3, osg::Plane(v110, v011, v010)));
189    clipnode->addClipPlane(new osg::ClipPlane(4, osg::Plane(v000, v110, v010)));
190    clipnode->addClipPlane(new osg::ClipPlane(5, osg::Plane(v001, v111, v101)));
191
192    osg::TexGenNode* texgenNode_0 = new osg::TexGenNode;
193    texgenNode_0->addChild(clipnode);
194    texgenNode_0->setTextureUnit(0);                                                                                                                             
195    texgenNode_0->getTexGen()->setMode(osg::TexGen::EYE_LINEAR);                                                                                                 
196    texgenNode_0->getTexGen()->setPlanesFromMatrix(osg::Matrix::inverse(matrix));                                                           
197
198    osg::StateSet* stateset = texgenNode_0->getOrCreateStateSet();
199
200    stateset->setMode(GL_LIGHTING,osg::StateAttribute::ON);
201    stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
202   
203    if (cpv._afProperty.valid())
204    {
205        stateset->setAttributeAndModes(cpv._afProperty->getAlphaFunc(), osg::StateAttribute::ON);
206    }
207    else
208    {
209        stateset->setAttributeAndModes(new osg::AlphaFunc(osg::AlphaFunc::GREATER,alphaFuncValue), osg::StateAttribute::ON);
210    }
211   
212    osg::Material* material = new osg::Material;
213    material->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0f,1.0f,1.0f,1.0f));
214    stateset->setAttributeAndModes(material);
215   
216    if (cpv._mipProperty.valid())
217    {
218        stateset->setAttribute(new osg::BlendFunc(osg::BlendFunc::ONE, osg::BlendFunc::ONE));
219        stateset->setAttribute(new osg::BlendEquation(osg::BlendEquation::RGBA_MAX));
220    }
221
222
223    // set up the 3d texture itself,
224    // note, well set the filtering up so that mip mapping is disabled,
225    // gluBuild3DMipsmaps doesn't do a very good job of handled the
226    // imbalanced dimensions of the 256x256x4 texture.
227    osg::Texture3D* texture3D = new osg::Texture3D;
228    texture3D->setResizeNonPowerOfTwoHint(false);
229    texture3D->setFilter(osg::Texture3D::MIN_FILTER,minFilter);
230    texture3D->setFilter(osg::Texture3D::MAG_FILTER, magFilter);
231    texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP_TO_EDGE);
232    texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP_TO_EDGE);
233    texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP_TO_EDGE);
234    if (image_3d->getPixelFormat()==GL_ALPHA ||
235        image_3d->getPixelFormat()==GL_LUMINANCE)
236    {
237        texture3D->setInternalFormatMode(osg::Texture3D::USE_USER_DEFINED_FORMAT);
238        texture3D->setInternalFormat(GL_INTENSITY);
239    }
240    else
241    {
242        texture3D->setInternalFormatMode(internalFormatMode);
243    }
244
245    texture3D->setImage(image_3d);
246
247    stateset->setTextureAttributeAndModes(0,texture3D,osg::StateAttribute::ON);
248
249    stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON);
250    stateset->setTextureMode(0,GL_TEXTURE_GEN_T,osg::StateAttribute::ON);
251    stateset->setTextureMode(0,GL_TEXTURE_GEN_R,osg::StateAttribute::ON);
252
253    stateset->setTextureAttributeAndModes(0,new osg::TexEnv(),osg::StateAttribute::ON);
254       
255    _node = texgenNode_0;
256}
257
258void FixedFunctionTechnique::update(osgUtil::UpdateVisitor* uv)
259{
260//    OSG_NOTICE<<"FixedFunctionTechnique:update(osgUtil::UpdateVisitor* nv):"<<std::endl;
261}
262
263void FixedFunctionTechnique::cull(osgUtil::CullVisitor* cv)
264{
265    //OSG_NOTICE<<"FixedFunctionTechnique::cull(osgUtil::CullVisitor* nv)"<<std::endl;   
266    if (_node.valid())
267    {
268        _node->accept(*cv);
269    }
270}
271
272void FixedFunctionTechnique::cleanSceneGraph()
273{
274    OSG_NOTICE<<"FixedFunctionTechnique::cleanSceneGraph()"<<std::endl;
275}
276
277void FixedFunctionTechnique::traverse(osg::NodeVisitor& nv)
278{
279    // OSG_NOTICE<<"FixedFunctionTechnique::traverse(osg::NodeVisitor& nv)"<<std::endl;
280    if (!_volumeTile) return;
281
282    // if app traversal update the frame count.
283    if (nv.getVisitorType()==osg::NodeVisitor::UPDATE_VISITOR)
284    {
285        if (_volumeTile->getDirty()) _volumeTile->init();
286
287        osgUtil::UpdateVisitor* uv = dynamic_cast<osgUtil::UpdateVisitor*>(&nv);
288        if (uv)
289        {
290            update(uv);
291            return;
292        }       
293       
294    }
295    else if (nv.getVisitorType()==osg::NodeVisitor::CULL_VISITOR)
296    {
297        osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(&nv);
298        if (cv)
299        {
300            cull(cv);
301            return;
302        }
303    }
304
305
306    if (_volumeTile->getDirty())
307    {
308        OSG_INFO<<"******* Doing init ***********"<<std::endl;
309        _volumeTile->init();
310    }
311}
312   
Note: See TracBrowser for help on using the browser.