Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

gfx/VisualPath.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)2002 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: VisualPath.cpp 1030 2004-02-11 20:46:17Z jungd $
00019   $Revision: 1.2 $
00020   $Date: 2004-02-11 15:46:17 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #include <gfx/VisualPath>
00026 
00027 
00028 #include <osg/Node>
00029 #include <osg/Transform>
00030 #include <osg/MatrixTransform>
00031 #include <osg/PositionAttitudeTransform>
00032 #include <osg/Geode>
00033 #include <osg/Drawable>
00034 #include <osg/Geometry>
00035 
00036 using gfx::VisualPath;
00037 
00038 using osg::Node;
00039 using osg::Transform;
00040 using osg::MatrixTransform;
00041 using osg::PositionAttitudeTransform;
00042 using osg::Geometry;
00043 using osg::Vec3;
00044 
00045 
00046 osg::Node* VisualPath::createOSGVisual(Visual::Attributes visualAttributes) const
00047 {
00048   if ((node!=0) && (attributes==visualAttributes))
00049     return &(*node);
00050 
00051 
00052   // create a set of line segments between the positions at a sequence of t:[0..1]
00053   //  also explicitly include all distibguished t values.
00054 
00055   array<Real> ta; // array of t values we'll evaluate the path at
00056 
00057   Real t=0.0;
00058   Int di=0;                          // distinguished t index
00059   Real dt = distinguishedValue(di);  // "             t value
00060   while (t<1.0) {
00061 
00062     if ((t > dt) && (di < numDistinguishedValues())) {
00063       ta.push_back(dt);
00064       if (!Math::equals(t,dt)) 
00065         ta.push_back(t);
00066 
00067       di++;
00068       if (di < numDistinguishedValues())
00069         dt = distinguishedValue(di);
00070     }
00071     else
00072       ta.push_back(t);
00073 
00074     t += 0.01;
00075     if (t>1.0) t=1.0;
00076   }
00077   ta.push_back(1.0);
00078 
00079 
00080   // create geometry
00081   osg::Geode* geode = new osg::Geode();
00082 
00083   osg::Geometry* linesGeom = new osg::Geometry();
00084 
00085   osg::Vec3Array* vertices = new osg::Vec3Array((ta.size()-1)*2);
00086   for(Int i=0; i<ta.size()-1; i++) {
00087     Point3 p1(position(ta[i]));
00088     Point3 p2(position(ta[i+1]));
00089     (*vertices)[2*i].set(p1.x,p1.y,p1.z);
00090     (*vertices)[2*i+1].set(p2.x,p2.y,p2.z);
00091   }
00092 
00093   linesGeom->setVertexArray(vertices);
00094 
00095   // set the colors as before, plus using the aobve
00096   osg::Vec4Array& colors = *new osg::Vec4Array(1);
00097   colors[0] = osg::Vec4(1.0f,1.0f,0.0f,1.0f);
00098   linesGeom->setColorArray(&colors);
00099   linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
00100   
00101   
00102   // set the normal in the same way as color.
00103   osg::Vec3Array& normals = *new osg::Vec3Array(1);
00104   normals[0] = osg::Vec3(0.0f,-1.0f,0.0f);
00105   linesGeom->setNormalArray(&normals);
00106   linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
00107   
00108   linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,(ta.size()-1)*2));
00109 
00110   // add the points geomtry to the geode.
00111   geode->addDrawable(linesGeom);
00112 
00113   attributes = visualAttributes;
00114   geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
00115 
00116   node = geode;
00117 
00118   return &(*node);
00119 }
00120 

Generated on Thu Jul 29 15:56:20 2004 for OpenSim by doxygen 1.3.6