Пример #1
0
void Grid::initGeometry(const OSG::Real32 width, const OSG::Real32 height,
                        const OSG::Real32 granularity,
                        const Grid::Corner corner,
                        const OSG::Vec3f& cornerPos,
                        const OSG::Quaternion& rot,
                        const OSG::Color3f& color)
{
   OSG::SimpleMaterialPtr plane_mat = OSG::SimpleMaterial::create();

#if OSG_MAJOR_VERSION < 2
   OSG::CPEditor pme(plane_mat,
                     OSG::SimpleMaterial::AmbientFieldMask         |
                        OSG::SimpleMaterial::DiffuseFieldMask      |
                        OSG::SimpleMaterial::LitFieldMask          |
                        OSG::SimpleMaterial::TransparencyFieldMask);
#endif
   plane_mat->setLit(false);
   plane_mat->setAmbient(color);
   plane_mat->setDiffuse(color);
   plane_mat->setTransparency(0.90f);

   OSG::SimpleMaterialPtr line_mat =
#if OSG_MAJOR_VERSION < 2
      OSG::SimpleMaterialPtr::dcast(OSG::deepClone(plane_mat));
#else
      OSG::cast_dynamic<OSG::SimpleMaterialPtr>(OSG::deepClone(plane_mat));
#endif

#if OSG_MAJOR_VERSION < 2
   OSG::CPEditor lme(line_mat, OSG::SimpleMaterial::TransparencyFieldMask);
#endif
   line_mat->setTransparency(0.0f);

   mPlaneNode = OSG::makePlaneGeo(width, height, 1, 1);
#if OSG_MAJOR_VERSION < 2
   OSG::CPEditor pne(mPlaneNode.core(), OSG::Geometry::MaterialFieldMask);
#endif
   mPlaneNode->setMaterial(plane_mat);

   const float half_width(width / 2.0f);
   const float half_height(height / 2.0f);

   std::cout << "Corner = " << cornerPos << std::endl;
   OSG::Vec3f center_pt;

   switch ( corner )
   {
      case LOWER_LEFT:
         center_pt[0] = cornerPos[0] + half_width;
         center_pt[1] = cornerPos[1] + half_height;
         center_pt[2] = cornerPos[2];
         break;
      case LOWER_RIGHT:
         center_pt[0] = cornerPos[0] - half_width;
         center_pt[1] = cornerPos[1] + half_height;
         center_pt[2] = cornerPos[2];
         break;
      case UPPER_RIGHT:
         center_pt[0] = cornerPos[0] - half_width;
         center_pt[1] = cornerPos[1] - half_height;
         center_pt[2] = cornerPos[2];
         break;
      case UPPER_LEFT:
         center_pt[0] = cornerPos[0] + half_width;
         center_pt[1] = cornerPos[1] - half_height;
         center_pt[2] = cornerPos[2];
         break;
   }

   std::cout << "Center = " << center_pt << std::endl;

   mRoot = OSG::Transform::create();

   OSG::Matrix xform;
   xform.setTransform(center_pt, rot, OSG::Vec3f(1.0f, 1.0f, 1.0f),
                      OSG::Quaternion(), cornerPos - center_pt);

   move(xform);

   OSG::GeoPTypesPtr type = OSG::GeoPTypesUI8::create();
#if OSG_MAJOR_VERSION < 2
   OSG::CPEditor te(type, OSG::GeoPTypesUI8::GeoPropDataFieldMask);
#endif
   type->addValue(GL_LINES);

   unsigned int vertex_count(0);

   OSG::GeoPositions3fPtr pos = OSG::GeoPositions3f::create();
#if OSG_MAJOR_VERSION < 2
   OSG::CPEditor pe(pos, OSG::GeoPositions3f::GeoPropDataFieldMask);
#endif
   for ( float i = -half_width; i <= half_width; i += granularity )
   {
      pos->addValue(OSG::Pnt3f(i, -half_height, 0.0f));
      pos->addValue(OSG::Pnt3f(i, half_height, 0.0f));
      vertex_count += 2;
   }
   for ( float i = -half_height; i <= half_height; i += granularity )
   {
      pos->addValue(OSG::Pnt3f(-half_width, i, 0.0f));
      pos->addValue(OSG::Pnt3f(half_width, i, 0.0f));
      vertex_count += 2;
   }

//   std::cout << "vertex_count = " << vertex_count << std::endl;

   OSG::GeoPLengthsPtr length = OSG::GeoPLengthsUI32::create();
#if OSG_MAJOR_VERSION < 2
   OSG::CPEditor le(length, OSG::GeoPLengthsUI32::GeoPropDataFieldMask);
#endif
   length->addValue(vertex_count);

   OSG::GeoNormals3fPtr norms = OSG::GeoNormals3f::create();
#if OSG_MAJOR_VERSION < 2
   OSG::CPEditor ne(norms, OSG::GeoNormals3f::GeoPropDataFieldMask);
#endif
   for ( unsigned int i = 0; i < vertex_count; ++i )
   {
      norms->addValue(OSG::Vec3f(0.0f, 0.0f, 1.0f));
   }

   mGridNode = OSG::Geometry::create();

#if OSG_MAJOR_VERSION < 2
   OSG::CPEditor gnce(mGridNode.core(),
                      OSG::Geometry::TypesFieldMask        |
                         OSG::Geometry::LengthsFieldMask   |
                         OSG::Geometry::PositionsFieldMask |
                         OSG::Geometry::NormalsFieldMask   |
                         OSG::Geometry::MaterialFieldMask);
#endif
   mGridNode->setTypes(type);
   mGridNode->setLengths(length);
   mGridNode->setPositions(pos);
   mGridNode->setNormals(norms);
   mGridNode->setMaterial(line_mat);

#if OSG_MAJOR_VERSION < 2
   OSG::CPEditor re(mRoot.node(), OSG::Node::ChildrenFieldMask);
#endif
   mRoot.node()->addChild(mGridNode);
   mRoot.node()->addChild(mPlaneNode);

   setSelected(false);
}