#include #include #include #include #include "Sphere.h" //////////////////////////////////////////////////////////// // // This class defines a sphere in an Inventor scene graph. // // This sphere incorporates three nodes underneath a // separator node. So a sphere has a location (with the // center given by transform.translation ) and a color // (given by material.emissiveColor ). // // The color model is emissive only. No reflectance. // // root // | // ----------------------------- // | | | // material transform sphere // // // The class has a method intersect() that computes the // intersection of the sphere with a ray. //////////////////////////////////////////////////////////// // // Here comes an optimization HACK. // // We're only going to put one prototype sphere in the // scene graph. Other spheres just reference it. SoSphere *sphereProto = new SoSphere(); //////////////////////////////////////////////////////////// // // Constructor Sphere:: Sphere() { radius = 1.0; center = SbVec3f(0.0, 0.0, 0.0); root = new SoSeparator(); material = new SoMaterial(); material->ambientColor.setValue(SbColor(0.0, 0.0, 0.0)); material->diffuseColor.setValue(SbColor(0.0, 0.0, 0.0)); transform = new SoTransform(); sphere = sphereProto; root->addChild(material); root->addChild(transform); root->addChild(sphere); } //////////////////////////////////////////////////////////// // // Constructor Sphere:: Sphere(const SbVec3f &_center, float _radius) { radius = _radius; center = SbVec3f(); center = _center; root = new SoSeparator(); material = new SoMaterial(); transform = new SoTransform(); transform->translation = _center; sphere = new SoSphere(); transform->scaleFactor.setValue(radius, radius, radius); root->addChild(material); root->addChild(transform); root->addChild(sphere); } //////////////////////////////////////////////////////////// // // Radius methods // // We use a HACK. Rather than actually set the radius // member variable of the SoSphere, we instead scale // the sphere to the desired size using the SoTransform // node ahead of it. void Sphere:: setRadius(float _radius) { radius = _radius; transform->scaleFactor.setValue(radius, radius, radius); } float Sphere:: getRadius() const { return (radius); } //////////////////////////////////////////////////////////// // // Center methods. // // The center is set via the transform node // (field transform.translation) ahead // of the sphere in the scene graph. void Sphere:: setCenter(const SbVec3f &_center) { center = _center; transform->translation = _center; } SbVec3f Sphere:: getCenter() const { return (center); } //////////////////////////////////////////////////////////// // // Orientation methods. // // The north pole points in the given direction. void Sphere:: setOrientation(const SbVec3f &north) { transform->rotation = SbRotation(SbVec3f(0.0, 1.0, 0.0), north); } //////////////////////////////////////////////////////////// // // Color methods. // // The color model is emissive only. No reflectance. void Sphere:: setEmissiveColor(const SbColor &color) { material->emissiveColor = color; material->diffuseColor = color; } void Sphere:: setEmissiveColor(const float red, const float green, const float blue) { material->emissiveColor.setValue(SbColor(red, green, blue)); material->diffuseColor = material->emissiveColor; } SbColor Sphere:: getEmissiveColor(float &red, float &green, float &blue) const { red = material->emissiveColor[0][0]; green = material->emissiveColor[0][1]; blue = material->emissiveColor[0][2]; return (SbColor(red, green, blue)); } SbColor Sphere:: getEmissiveColor() const { return (SbColor( material->emissiveColor[0][0], material->emissiveColor[0][1], material->emissiveColor[0][2])); } //////////////////////////////////////////////////////////// // // Access to the root-level node. SoSeparator * Sphere:: getRoot() const { return (root); } //////////////////////////////////////////////////////////// // // Compute the sphere's intersection point when given // a ray (defined by point p0 and direction dp). // // Return the number of intersections. int Sphere:: intersect(SbVec3f &intersection, const SbVec3f &p0, const SbVec3f &dp) const { return (0); }