00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
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
00077
00078
00079
00080
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
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);
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
00175 shape = new ShapeDrawable(new Box(Vec3(0.0f, 0.0f, 0.0f),
00176 50, 50, 0.01), osgHandle.tesselhints[quality]);
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
00315
00316
00317
00318
00319
00320
00321
00322 }
00323
00324
00325
00326
00327
00328
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
00343 return m;
00344 }
00345
00346
00347 }