Exemplo n.º 1
0
MatrixTransform* ChessUtils::loadOSGModel(string name, float modelSize, Material* material, bool overrideMaterial,
	Vec3 modelCenterShift, double rotationAngle, Vec3 rotationAxis,
	Vec3 modelCenterOffsetPercentage) {
	// create a new node by reading in model from file
	Node* modelNode = osgDB::readNodeFile(name);
	if (modelNode != NULL) {
		// apply material
		if (material != NULL) {
			if (overrideMaterial) {
				modelNode->getOrCreateStateSet()->setAttributeAndModes(material, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
			} else {
				modelNode->getOrCreateStateSet()->setAttributeAndModes(material, osg::StateAttribute::ON);
			}
		}

		//put model in origin			
		//osg::BoundingSphere bound = modelNode->getBound();
		osg::ComputeBoundsVisitor cbVisitorOrigin;
		modelNode->accept(cbVisitorOrigin);
		osg::BoundingBox bound = cbVisitorOrigin.getBoundingBox();
		
		double scaleRatio = modelSize / bound.radius();

		MatrixTransform* unitTransform = new MatrixTransform();		
		unitTransform->postMult(Matrix::translate(-bound.center().x(), -bound.center().y(), -bound.center().z()));
		unitTransform->postMult(Matrix::rotate(rotationAngle, rotationAxis));
		unitTransform->postMult(Matrix::scale(scaleRatio, scaleRatio, scaleRatio));
		unitTransform->addChild(modelNode);		

		// put model in specified location
		osg::ComputeBoundsVisitor cbVisitor;
		unitTransform->accept(cbVisitor);
		osg::BoundingBox boundingBox = cbVisitor.getBoundingBox();
				
		float modelXOffset = (boundingBox.xMax() - boundingBox.xMin()) * modelCenterOffsetPercentage.x();
		float modelYOffset = (boundingBox.yMax() - boundingBox.yMin()) * modelCenterOffsetPercentage.y();
		float modelZOffset = (boundingBox.zMax() - boundingBox.zMin()) * modelCenterOffsetPercentage.z();
		unitTransform->postMult(Matrix::translate(modelXOffset, modelYOffset, modelZOffset));		

		MatrixTransform* modelPositionTransform = new MatrixTransform();		
		modelPositionTransform->postMult(Matrix::translate(modelCenterShift));
		modelPositionTransform->addChild(unitTransform);		

		return modelPositionTransform;
	}

	return NULL;
}
Exemplo n.º 2
0
osg::Node* Shape::createOSGAxes(const base::Dimension3& dim)
{
  const Real s = 1.5;
  Real d = Math::minimum(0.06,Math::minimum(dim.x,dim.y,dim.z)/16.0);

  Group* g = NewObj Group;
  g->setName("debug");

  // color the axes X:red, Y:green and Z:blue, with white end cones
  StateSet* red = NewObj StateSet();
  osg::Material* rmat = NewObj osg::Material();
  Vec4 cred(1,0,0,1);
  //  mat->setEmission( osg::Material::FRONT_AND_BACK, Vec4(0,0,0,0) );
  //mat->setAmbient( osg::Material::FRONT_AND_BACK, col );
  rmat->setDiffuse( osg::Material::FRONT_AND_BACK, cred );
  rmat->setSpecular( osg::Material::FRONT_AND_BACK, Vec4(1,1,1,0) );
  rmat->setShininess( osg::Material::FRONT_AND_BACK, 1.0);
  red->setAttribute( rmat );

  StateSet* green = NewObj StateSet();
  osg::Material* gmat = NewObj osg::Material();
  Vec4 cgreen(0,1,0,1);
  //  mat->setEmission( osg::Material::FRONT_AND_BACK, Vec4(0,0,0,0) );
  //mat->setAmbient( osg::Material::FRONT_AND_BACK, col );
  gmat->setDiffuse( osg::Material::FRONT_AND_BACK, cgreen );
  gmat->setSpecular( osg::Material::FRONT_AND_BACK, Vec4(1,1,1,0) );
  gmat->setShininess( osg::Material::FRONT_AND_BACK, 1.0);
  green->setAttribute( gmat );
  
  StateSet* blue = NewObj StateSet();
  osg::Material* bmat = NewObj osg::Material();
  Vec4 cblue(0,0,1,1);
  //  mat->setEmission( osg::Material::FRONT_AND_BACK, Vec4(0,0,0,0) );
  //mat->setAmbient( osg::Material::FRONT_AND_BACK, col );
  bmat->setDiffuse( osg::Material::FRONT_AND_BACK, cblue );
  bmat->setSpecular( osg::Material::FRONT_AND_BACK, Vec4(1,1,1,0) );
  bmat->setShininess( osg::Material::FRONT_AND_BACK, 1.0);
  blue->setAttribute( bmat );

  StateSet* white = NewObj StateSet();
  osg::Material* wmat = NewObj osg::Material();
  Vec4 cwhite(1,1,1,1);
  //  mat->setEmission( osg::Material::FRONT_AND_BACK, Vec4(0,0,0,0) );
  //mat->setAmbient( osg::Material::FRONT_AND_BACK, col );
  wmat->setDiffuse( osg::Material::FRONT_AND_BACK, cwhite );
  wmat->setSpecular( osg::Material::FRONT_AND_BACK, Vec4(1,1,1,0) );
  wmat->setShininess( osg::Material::FRONT_AND_BACK, 1.0);
  white->setAttribute( wmat );


  // a long Clyinder for the axis and a cone-like cylinder 
  //  for the arrow head of each X,Y and Z.

  MatrixTransform* xrot = NewObj MatrixTransform();
  xrot->setMatrix(osg::Matrix::rotate(consts::Pi/2.0,Vec3(0,1,0)));
  xrot->postMult(osg::Matrix::translate(s*dim.x/4.0,0,0));
  g->addChild(xrot);
		
  MatrixTransform* yrot = NewObj MatrixTransform();
  yrot->setMatrix(osg::Matrix::rotate(consts::Pi/2.0,Vec3(-1,0,0)));
  yrot->postMult(osg::Matrix::translate(0,s*dim.y/4.0,0));
  g->addChild(yrot);
		
  MatrixTransform* zrot = NewObj MatrixTransform();
  zrot->setMatrix(osg::Matrix::translate(0,0,s*dim.z/4.0));
  g->addChild(zrot);
		
  // the cylinder axes
  ref<Cylinder> xc(NewObj Cylinder(s*dim.x/2.0,d));
  xrot->addChild(xc->createOSGVisual());
  xrot->setStateSet(red);

  ref<Cylinder> yc(NewObj Cylinder(s*dim.y/2.0,d));
  yrot->addChild(yc->createOSGVisual());
  yrot->setStateSet(green);

  ref<Cylinder> zc(NewObj Cylinder(s*dim.z/2.0,d));
  zrot->addChild(zc->createOSGVisual());
  zrot->setStateSet(blue);

  // Translate each axis cone to the end
  MatrixTransform* xtrans = NewObj MatrixTransform();
  xtrans->setMatrix(osg::Matrix::translate(0,0,s*dim.x/4.0+d));
  xrot->addChild(xtrans);
  
  MatrixTransform* ytrans = NewObj MatrixTransform();
  ytrans->setMatrix(osg::Matrix::translate(0,0,s*dim.y/4.0+d));
  yrot->addChild(ytrans);
  
  MatrixTransform* ztrans = NewObj MatrixTransform();
  ztrans->setMatrix(osg::Matrix::translate(0,0,s*dim.z/4.0+d));
  zrot->addChild(ztrans);
  
  // the end cones
  ref<Cone> cone(NewObj Cone(4*d,2*d));
  osg::Node* coneNode = cone->createOSGVisual();
  coneNode->setStateSet(white);
  
  xtrans->addChild(coneNode);
  ytrans->addChild(coneNode);
  ztrans->addChild(coneNode);

  return g;
}