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

Revision 13041, 9.5 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • 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    osgVolume::Locator* masterLocator = _volumeTile->getLocator();
125    osg::Texture::InternalFormatMode internalFormatMode = osg::Texture::USE_IMAGE_DATA_FORMAT;
126
127    image_3d = _volumeTile->getLayer()->getImage();
128
129    CollectPropertiesVisitor cpv;
130    if (_volumeTile->getLayer()->getProperty())
131    {
132        _volumeTile->getLayer()->getProperty()->accept(cpv);
133    }
134
135    if (cpv._isoProperty.valid())
136    {
137        alphaFuncValue = cpv._isoProperty->getValue();
138    }
139
140    if (cpv._afProperty.valid())
141    {
142        alphaFuncValue = cpv._afProperty->getValue();
143    }
144
145
146    if (_volumeTile->getLayer() && !masterLocator)
147    {
148        masterLocator = _volumeTile->getLayer()->getLocator();
149        OSG_NOTICE<<"assigning locator = "<<masterLocator<<std::endl;
150    }
151
152    osg::Matrix matrix;
153    if (masterLocator)
154    {
155        matrix = masterLocator->getTransform();
156    }
157
158    OSG_NOTICE<<"Matrix = "<<matrix<<std::endl;
159
160    osg::Texture::FilterMode minFilter = osg::Texture::NEAREST;
161    osg::Texture::FilterMode magFilter = osg::Texture::NEAREST;
162
163    osg::Vec3d v000 = osg::Vec3d(0.0,0.0,0.0) * matrix;
164    osg::Vec3d v100 = osg::Vec3d(1.0,0.0,0.0) * matrix;
165    osg::Vec3d v010 = osg::Vec3d(0.0,1.0,0.0) * matrix;
166    osg::Vec3d v110 = osg::Vec3d(1.0,1.0,0.0) * matrix;
167
168    osg::Vec3d v001 = osg::Vec3d(0.0,0.0,1.0) * matrix;
169    osg::Vec3d v101 = osg::Vec3d(1.0,0.0,1.0) * matrix;
170    osg::Vec3d v011 = osg::Vec3d(0.0,1.0,1.0) * matrix;
171    osg::Vec3d v111 = osg::Vec3d(1.0,1.0,1.0) * matrix;
172
173    double cubeSize = (v111-v000).length();
174    osg::Vec3d center = (v000+v111)*0.5;
175
176    osg::ClipNode* clipnode = new osg::ClipNode;
177    clipnode->addChild(createCube(center, cubeSize, _numSlices));
178
179    clipnode->addClipPlane(new osg::ClipPlane(0, osg::Plane(v000, v011, v001)));
180    clipnode->addClipPlane(new osg::ClipPlane(1, osg::Plane(v100, v111, v110)));
181    clipnode->addClipPlane(new osg::ClipPlane(2, osg::Plane(v000, v101, v100)));
182    clipnode->addClipPlane(new osg::ClipPlane(3, osg::Plane(v110, v011, v010)));
183    clipnode->addClipPlane(new osg::ClipPlane(4, osg::Plane(v000, v110, v010)));
184    clipnode->addClipPlane(new osg::ClipPlane(5, osg::Plane(v001, v111, v101)));
185
186    osg::TexGenNode* texgenNode_0 = new osg::TexGenNode;
187    texgenNode_0->addChild(clipnode);
188    texgenNode_0->setTextureUnit(0);
189    texgenNode_0->getTexGen()->setMode(osg::TexGen::EYE_LINEAR);
190    texgenNode_0->getTexGen()->setPlanesFromMatrix(osg::Matrix::inverse(matrix));
191
192    osg::StateSet* stateset = texgenNode_0->getOrCreateStateSet();
193
194    stateset->setMode(GL_LIGHTING,osg::StateAttribute::ON);
195    stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
196
197    if (cpv._afProperty.valid())
198    {
199        stateset->setAttributeAndModes(cpv._afProperty->getAlphaFunc(), osg::StateAttribute::ON);
200    }
201    else
202    {
203        stateset->setAttributeAndModes(new osg::AlphaFunc(osg::AlphaFunc::GREATER,alphaFuncValue), osg::StateAttribute::ON);
204    }
205
206    osg::Material* material = new osg::Material;
207    material->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0f,1.0f,1.0f,1.0f));
208    stateset->setAttributeAndModes(material);
209
210    if (cpv._mipProperty.valid())
211    {
212        stateset->setAttribute(new osg::BlendFunc(osg::BlendFunc::ONE, osg::BlendFunc::ONE));
213        stateset->setAttribute(new osg::BlendEquation(osg::BlendEquation::RGBA_MAX));
214    }
215
216
217    // set up the 3d texture itself,
218    // note, well set the filtering up so that mip mapping is disabled,
219    // gluBuild3DMipsmaps doesn't do a very good job of handled the
220    // imbalanced dimensions of the 256x256x4 texture.
221    osg::Texture3D* texture3D = new osg::Texture3D;
222    texture3D->setResizeNonPowerOfTwoHint(false);
223    texture3D->setFilter(osg::Texture3D::MIN_FILTER,minFilter);
224    texture3D->setFilter(osg::Texture3D::MAG_FILTER, magFilter);
225    texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::CLAMP_TO_EDGE);
226    texture3D->setWrap(osg::Texture3D::WRAP_S,osg::Texture3D::CLAMP_TO_EDGE);
227    texture3D->setWrap(osg::Texture3D::WRAP_T,osg::Texture3D::CLAMP_TO_EDGE);
228    if (image_3d->getPixelFormat()==GL_ALPHA ||
229        image_3d->getPixelFormat()==GL_LUMINANCE)
230    {
231        texture3D->setInternalFormatMode(osg::Texture3D::USE_USER_DEFINED_FORMAT);
232        texture3D->setInternalFormat(GL_INTENSITY);
233    }
234    else
235    {
236        texture3D->setInternalFormatMode(internalFormatMode);
237    }
238
239    texture3D->setImage(image_3d);
240
241    stateset->setTextureAttributeAndModes(0,texture3D,osg::StateAttribute::ON);
242
243    stateset->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON);
244    stateset->setTextureMode(0,GL_TEXTURE_GEN_T,osg::StateAttribute::ON);
245    stateset->setTextureMode(0,GL_TEXTURE_GEN_R,osg::StateAttribute::ON);
246
247    stateset->setTextureAttributeAndModes(0,new osg::TexEnv(),osg::StateAttribute::ON);
248
249    _node = texgenNode_0;
250}
251
252void FixedFunctionTechnique::update(osgUtil::UpdateVisitor* uv)
253{
254//    OSG_NOTICE<<"FixedFunctionTechnique:update(osgUtil::UpdateVisitor* nv):"<<std::endl;
255}
256
257void FixedFunctionTechnique::cull(osgUtil::CullVisitor* cv)
258{
259    //OSG_NOTICE<<"FixedFunctionTechnique::cull(osgUtil::CullVisitor* nv)"<<std::endl;
260    if (_node.valid())
261    {
262        _node->accept(*cv);
263    }
264}
265
266void FixedFunctionTechnique::cleanSceneGraph()
267{
268    OSG_NOTICE<<"FixedFunctionTechnique::cleanSceneGraph()"<<std::endl;
269}
270
271void FixedFunctionTechnique::traverse(osg::NodeVisitor& nv)
272{
273    // OSG_NOTICE<<"FixedFunctionTechnique::traverse(osg::NodeVisitor& nv)"<<std::endl;
274    if (!_volumeTile) return;
275
276    // if app traversal update the frame count.
277    if (nv.getVisitorType()==osg::NodeVisitor::UPDATE_VISITOR)
278    {
279        if (_volumeTile->getDirty()) _volumeTile->init();
280
281        osgUtil::UpdateVisitor* uv = dynamic_cast<osgUtil::UpdateVisitor*>(&nv);
282        if (uv)
283        {
284            update(uv);
285            return;
286        }
287
288    }
289    else if (nv.getVisitorType()==osg::NodeVisitor::CULL_VISITOR)
290    {
291        osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(&nv);
292        if (cv)
293        {
294            cull(cv);
295            return;
296        }
297    }
298
299
300    if (_volumeTile->getDirty())
301    {
302        OSG_INFO<<"******* Doing init ***********"<<std::endl;
303        _volumeTile->init();
304    }
305}
Note: See TracBrowser for help on using the browser.