osgprimitive.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Robot Group Leipzig                             *
00003  *    martius@informatik.uni-leipzig.de                                    *
00004  *    fhesse@informatik.uni-leipzig.de                                     *
00005  *    der@informatik.uni-leipzig.de                                        *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  *                                                                         *
00022  ***************************************************************************
00023  *                                                                         *
00024  *   This file provides basic primitives for openscenegraph usage          *
00025  *   with ODE.                                                             *
00026  *                                                                         *
00027  *                                                                         *
00028  *   $Log: osgprimitive.cpp,v $
00029  *   Revision 1.1.2.11  2006/03/29 15:06:40  martius
00030  *   OSGMesh
00031  *
00032  *   Revision 1.1.2.10  2006/01/12 14:21:00  martius
00033  *   drawmode, material
00034  *
00035  *   Revision 1.1.2.9  2005/12/22 14:14:21  martius
00036  *   quality level
00037  *
00038  *   Revision 1.1.2.8  2005/12/15 17:03:42  martius
00039  *   cameramanupulator setPose is working
00040  *   joints have setter and getter parameters
00041  *   Primitives are not longer inherited from OSGPrimitive, moreover
00042  *   they aggregate them
00043  *
00044  *   Revision 1.1.2.7  2005/12/14 15:36:45  martius
00045  *   joints are visible now
00046  *
00047  *   Revision 1.1.2.6  2005/12/13 18:11:13  martius
00048  *   transform primitive added, some joints stuff done, forward declaration
00049  *
00050  *   Revision 1.1.2.5  2005/12/12 23:42:53  martius
00051  *   *** empty log message ***
00052  *
00053  *   Revision 1.1.2.4  2005/12/11 23:35:08  martius
00054  *   *** empty log message ***
00055  *
00056  *   Revision 1.1.2.3  2005/12/09 16:54:16  martius
00057  *   camera is woring now
00058  *
00059  *   Revision 1.1.2.2  2005/12/06 17:38:15  martius
00060  *   *** empty log message ***
00061  *
00062  *   Revision 1.1.2.1  2005/12/06 10:13:24  martius
00063  *   openscenegraph integration started
00064  *
00065  *                                                                 *
00066  *                                                                         *
00067  ***************************************************************************/
00068 
00069 #include <assert.h>
00070 
00071 #include <osg/Texture2D>
00072 #include <osg/Geode>
00073 #include <osg/ShapeDrawable>
00074 #include <osg/MatrixTransform>
00075 #include <osgDB/ReadFile>
00076 //#include <osg/Texture>
00077 //#include <osg/TexGen>
00078 //#include <osg/PolygonOffset>
00079 //#include <osg/Light>
00080 //#include <osg/LightSource>
00081 #include <osg/Material>
00082 #include <osgDB/ReadFile>
00083 
00084 
00085 #include "osgprimitive.h"
00086 
00087 namespace lpzrobots {
00088 
00089   using namespace osg;
00090   using namespace osgDB;
00091 
00092   // returns a material with the given color
00093   ref_ptr<Material> getMaterial (const Color& c, Material::ColorMode mode = Material::DIFFUSE );
00094 
00095   /******************************************************************************/
00096 
00097 
00098   OSGPrimitive::OSGPrimitive(){  }
00099 
00100   OSGPrimitive::~OSGPrimitive(){    
00101     Node::ParentList l = transform->getParents();
00102     for(Node::ParentList::iterator i = l.begin(); i != l.end(); i++){
00103       (*i)->removeChild(transform.get());  
00104     }
00105   }
00106 
00107 
00108   /******************************************************************************/
00109   void OSGPrimitive::setMatrix(const Matrix& m4x4){
00110     assert(!transform == false);
00111     transform->setMatrix(m4x4);
00112   }
00113 
00114   Group* OSGPrimitive::getGroup() { 
00115     return transform.get(); 
00116   }
00117 
00118   Transform* OSGPrimitive::getTransform() { 
00119     return transform.get(); 
00120   }
00121 
00122   void OSGPrimitive::setTexture(const std::string& filename){
00123     osg::Group* grp = getGroup();    
00124     osg::Texture2D* texture = new osg::Texture2D;
00125     texture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state.
00126     texture->setImage(osgDB::readImageFile(filename));
00127     osg::StateSet* stateset = grp->getOrCreateStateSet();
00128     stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
00129   }
00130 
00131   void OSGPrimitive::setColor(const Color& color){
00132     if(shape.valid())
00133       shape->setColor(color);
00134   }
00135 
00136 
00137 
00138   /******************************************************************************/
00139   OSGDummy::OSGDummy(){}
00140   
00141   void OSGDummy::init(const OsgHandle& osgHandle, Quality quality){
00142   }
00143   
00144   void OSGDummy::setMatrix( const osg::Matrix& m4x4 ) {
00145   }
00146   
00147   Group* OSGDummy::getGroup() { 
00148     return 0;
00149   }
00150 
00151   void OSGDummy::setTexture(const std::string& filename) {
00152     
00153   }
00154 
00155   void OSGDummy::setColor(const Color& color) {
00156 
00157   }
00158   
00159   Transform* OSGDummy::getTransform() {
00160     return 0;
00161   }
00162 
00163   /******************************************************************************/
00164   OSGPlane::OSGPlane() {
00165   }
00166 
00167   void OSGPlane::init(const OsgHandle& osgHandle, Quality quality){
00168     assert(osgHandle.scene);
00169     geode = new Geode;  
00170     transform = new MatrixTransform;
00171     transform->addChild(geode.get());
00172     osgHandle.scene->addChild(transform.get());
00173   
00174     //  shape = new ShapeDrawable(new InfinitePlane(), osgHandle.tesselhints);
00175     shape = new ShapeDrawable(new Box(Vec3(0.0f, 0.0f, 0.0f), 
00176                                       50, 50, 0.01), osgHandle.tesselhints[quality]); // TODO add larger values here
00177     shape->setColor(osgHandle.color);
00178     geode->addDrawable(shape.get());
00179     if(osgHandle.color.alpha() < 1.0){
00180       shape->setStateSet(osgHandle.transparentState);
00181     }else{
00182       shape->setStateSet(osgHandle.normalState);
00183     }
00184     shape->getOrCreateStateSet()->setAttributeAndModes(getMaterial(osgHandle.color).get(), 
00185                                                        StateAttribute::ON);
00186 
00187   }
00188 
00189 
00190   /******************************************************************************/
00191   OSGBox::OSGBox(float lengthX, float lengthY, float lengthZ)
00192     : lengthX(lengthX), lengthY(lengthY), lengthZ(lengthZ) {
00193   }
00194 
00195   void OSGBox::init(const OsgHandle& osgHandle, Quality quality){
00196     assert(osgHandle.scene);
00197     geode = new Geode;  
00198     transform = new MatrixTransform;
00199     transform->addChild(geode.get());
00200     osgHandle.scene->addChild(transform.get());
00201 
00202     shape = new ShapeDrawable(new Box(Vec3(0.0f, 0.0f, 0.0f), 
00203                                       lengthX, lengthY, lengthZ), osgHandle.tesselhints[quality]);
00204     shape->setColor(osgHandle.color);
00205     geode->addDrawable(shape.get());
00206     if(osgHandle.color.alpha() < 1.0){
00207       shape->setStateSet(osgHandle.transparentState);
00208     }else{
00209       shape->setStateSet(osgHandle.normalState);
00210     }
00211     shape->getOrCreateStateSet()->setAttributeAndModes(getMaterial(osgHandle.color).get(), 
00212                                                        StateAttribute::ON);
00213   }
00214 
00215   /******************************************************************************/
00216   OSGSphere::OSGSphere(float radius)
00217     : radius(radius) {
00218   }
00219 
00220   void OSGSphere::init(const OsgHandle& osgHandle, Quality quality){
00221     assert(osgHandle.scene);
00222     
00223     geode = new Geode;  
00224     transform = new MatrixTransform;
00225     transform->addChild(geode.get());
00226     osgHandle.scene->addChild(transform.get());
00227 
00228     shape = new ShapeDrawable(new Sphere(Vec3(0.0f, 0.0f, 0.0f), radius), osgHandle.tesselhints[quality]);
00229     shape->setColor(osgHandle.color);
00230     geode->addDrawable(shape.get());
00231     if(osgHandle.color.alpha() < 1.0){
00232       shape->setStateSet(osgHandle.transparentState);
00233     }else{
00234       shape->setStateSet(osgHandle.normalState);
00235     }
00236     shape->getOrCreateStateSet()->setAttributeAndModes(getMaterial(osgHandle.color).get(), 
00237                                                        StateAttribute::ON);
00238 
00239   }
00240 
00241   /******************************************************************************/
00242   OSGCapsule::OSGCapsule(float radius, float height)
00243     : radius(radius), height(height) {
00244   }
00245 
00246   void OSGCapsule::init(const OsgHandle& osgHandle, Quality quality){
00247     assert(osgHandle.scene);
00248 
00249     geode = new Geode;  
00250     transform = new MatrixTransform;
00251     transform->addChild(geode.get());
00252     osgHandle.scene->addChild(transform.get());
00253 
00254     shape = new ShapeDrawable(new Capsule(Vec3(0.0f, 0.0f, 0.0f), 
00255                                           radius, height), osgHandle.tesselhints[quality]);
00256     shape->setColor(osgHandle.color);
00257     geode->addDrawable(shape.get());
00258     if(osgHandle.color.alpha() < 1.0){
00259       shape->setStateSet(osgHandle.transparentState);
00260     }else{
00261       shape->setStateSet(osgHandle.normalState);
00262     }
00263     shape->getOrCreateStateSet()->setAttributeAndModes(getMaterial(osgHandle.color).get(), 
00264                                                        StateAttribute::ON);
00265   }
00266 
00267   /******************************************************************************/
00268   OSGCylinder::OSGCylinder(float radius, float height)
00269     : radius(radius), height(height) {
00270   }
00271 
00272   void OSGCylinder::init(const OsgHandle& osgHandle, Quality quality){
00273     assert(osgHandle.scene);
00274 
00275     geode = new Geode;  
00276     transform = new MatrixTransform;
00277     transform->addChild(geode.get());
00278     osgHandle.scene->addChild(transform.get());
00279 
00280     shape = new ShapeDrawable(new Cylinder(Vec3(0.0f, 0.0f, 0.0f), 
00281                                            radius, height), osgHandle.tesselhints[quality]);
00282     shape->setColor(osgHandle.color);
00283     geode->addDrawable(shape.get());
00284     if(osgHandle.color.alpha() < 1.0){
00285       shape->setStateSet(osgHandle.transparentState);
00286     }else{
00287       shape->setStateSet(osgHandle.normalState);
00288     }
00289     shape->getOrCreateStateSet()->setAttributeAndModes(getMaterial(osgHandle.color).get(), 
00290                                                        StateAttribute::ON);
00291 
00292   }
00293 
00294   /******************************************************************************/
00295   OSGMesh::OSGMesh(const std::string& filename, float scale, 
00296                    const ReaderWriter::Options* options)
00297     : filename(filename), scale(scale), options(options) 
00298   {
00299   }
00300 
00301   OSGMesh::~OSGMesh(){
00302   }
00303 
00304   void OSGMesh::init(const OsgHandle& osgHandle, Quality quality){
00305     assert(osgHandle.scene);
00306     transform = new MatrixTransform;        
00307     osgHandle.scene->addChild(transform.get());
00308     scaletrans = new MatrixTransform;    
00309     scaletrans->setMatrix(osg::Matrix::scale(scale,scale,scale));
00310     transform->addChild(scaletrans.get());
00311     mesh  = osgDB::readNodeFile(filename, options);
00312     scaletrans->addChild(mesh.get());
00313 
00314 //     if(osgHandle.color.alpha() < 1.0){
00315 //       shape->setStateSet(osgHandle.transparentState);
00316 //     }else{
00317 //       shape->setStateSet(osgHandle.normalState);
00318 //     }
00319 //     shape->getOrCreateStateSet()->setAttributeAndModes(getMaterial(osgHandle.color).get(), 
00320 //                                                     StateAttribute::ON);
00321 
00322   }
00323 
00324 
00325 
00326   /********************* HELPER ************************************************/
00327 
00328   // returns a material with the given color
00329   ref_ptr<Material> getMaterial (const Color& c, Material::ColorMode mode) {
00330     ref_ptr<Material> m = new Material ();
00331     m->setColorMode(mode);
00332     Color amb (c*0.3);
00333     amb.alpha()=c.alpha();
00334     Color dif(c*0.7);
00335     dif.alpha()=c.alpha();
00336     Color spec(c*0.2);
00337     spec.alpha()=c.alpha();
00338     m->setAmbient(Material::FRONT_AND_BACK, amb);
00339     m->setDiffuse(Material::FRONT_AND_BACK, dif);
00340     m->setSpecular(Material::FRONT_AND_BACK, spec);
00341     m->setShininess(Material::FRONT_AND_BACK, 5.0f);
00342     //    m->setShininess(Material::FRONT_AND_BACK, 25.0f);
00343     return m;
00344   }
00345 
00346 
00347 }

Generated on Tue Apr 4 19:05:04 2006 for Robotsystem from Robot Group Leipzig by  doxygen 1.4.5