Improved osgSim::SphereSegmenet's handling of transparancy.

This commit is contained in:
Robert Osfield 2004-02-12 01:15:37 +00:00
parent 2f63f0fa21
commit eca2fc2837
4 changed files with 290 additions and 122 deletions

View File

@ -30,61 +30,61 @@ class MyNodeCallback: public osg::NodeCallback
// void MyNodeCallback::operator()(osg::Node* n,osg::NodeVisitor* nv) // void MyNodeCallback::operator()(osg::Node* n,osg::NodeVisitor* nv)
// { // {
// if(osgSim::SphereSegment* ss=dynamic_cast<osgSim::SphereSegment*>(n)) // if(osgSim::SphereSegment* ss=dynamic_cast<osgSim::SphereSegment*>(n))
// { // {
// osg::Vec3 vec; // osg::Vec3 vec;
// float azRange, elevRange; // float azRange, elevRange;
// ss->getArea(vec,azRange,elevRange); // ss->getArea(vec,azRange,elevRange);
// //
// float azRangeDeg = osg::RadiansToDegrees(azRange); // float azRangeDeg = osg::RadiansToDegrees(azRange);
// //
// static bool azAscending = false; // static bool azAscending = false;
// //
// if(azAscending){ // if(azAscending){
// azRangeDeg += 1.0f; // azRangeDeg += 1.0f;
// if(azRangeDeg>89.0f) azAscending = false; // if(azRangeDeg>89.0f) azAscending = false;
// }else{ // }else{
// azRangeDeg -= 1.0f; // azRangeDeg -= 1.0f;
// if(azRangeDeg<2.0f) azAscending = true; // if(azRangeDeg<2.0f) azAscending = true;
// } // }
// //
// ss->setArea(vec,osg::DegreesToRadians(azRangeDeg),elevRange); // ss->setArea(vec,osg::DegreesToRadians(azRangeDeg),elevRange);
// //
// } // }
// traverse(n,nv); // traverse(n,nv);
// } // }
void MyNodeCallback::operator()(osg::Node* n,osg::NodeVisitor* nv) void MyNodeCallback::operator()(osg::Node* n,osg::NodeVisitor* nv)
{ {
if(osgSim::SphereSegment* ss=dynamic_cast<osgSim::SphereSegment*>(n)) if(osgSim::SphereSegment* ss=dynamic_cast<osgSim::SphereSegment*>(n))
{ {
osg::Vec3 vec; osg::Vec3 vec;
float azRange, elevRange; float azRange, elevRange;
ss->getArea(vec,azRange,elevRange); ss->getArea(vec,azRange,elevRange);
static float angle = 0.0f; static float angle = 0.0f;
if(++angle > 359.0f) angle = 0.0f; if(++angle > 359.0f) angle = 0.0f;
vec.set(sin(osg::DegreesToRadians(angle)),cos(osg::DegreesToRadians(angle)),0.0f); vec.set(sin(osg::DegreesToRadians(angle)),cos(osg::DegreesToRadians(angle)),0.0f);
std::cout<<"angle "<<angle<<" degrees, vec is "<<vec std::cout<<"angle "<<angle<<" degrees, vec is "<<vec
<<", azRange is "<<osg::RadiansToDegrees(azRange) <<", azRange is "<<osg::RadiansToDegrees(azRange)
<<", elevRange is "<<osg::RadiansToDegrees(elevRange) <<", elevRange is "<<osg::RadiansToDegrees(elevRange)
<<std::endl; <<std::endl;
ss->setArea(vec,azRange,elevRange); ss->setArea(vec,azRange,elevRange);
} }
traverse(n,nv); traverse(n,nv);
} }
osg::Node* createSphereSegment() osg::Node* createSphereSegment()
{ {
SphereSegment* ss = new SphereSegment(osg::Vec3(0.0f,0.0f,0.0f), 1.0f, SphereSegment* ss = new SphereSegment(osg::Vec3(0.0f,0.0f,0.0f), 1.0f,
osg::Vec3(0.0f,1.0f,0.0f), osg::Vec3(0.0f,1.0f,0.0f),
osg::DegreesToRadians(90.0f), osg::DegreesToRadians(90.0f),
osg::DegreesToRadians(45.0f), osg::DegreesToRadians(45.0f),
60); 60);
ss->setAllColors(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); ss->setAllColors(osg::Vec4(1.0f,1.0f,1.0f,0.5f));
ss->setSideColor(osg::Vec4(0.0f,0.0f,1.0f,0.1f)); ss->setSideColor(osg::Vec4(0.0f,0.0f,1.0f,0.5f));
//ss->setDrawMask(SphereSegment::DrawMask(SphereSegment::SPOKES | SphereSegment::EDGELINE)); //ss->setDrawMask(SphereSegment::DrawMask(SphereSegment::SPOKES | SphereSegment::EDGELINE));
//ss->setUpdateCallback(new MyNodeCallback); //ss->setUpdateCallback(new MyNodeCallback);

View File

@ -23,7 +23,7 @@ class SG_EXPORT LineWidth : public StateAttribute
{ {
public : public :
LineWidth(); LineWidth(float width=1.0f);
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/ /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
LineWidth(const LineWidth& lw,const CopyOp& copyop=CopyOp::SHALLOW_COPY): LineWidth(const LineWidth& lw,const CopyOp& copyop=CopyOp::SHALLOW_COPY):

View File

@ -17,9 +17,9 @@
using namespace osg; using namespace osg;
LineWidth::LineWidth() LineWidth::LineWidth(float width)
{ {
_width = 1.0f; _width = width;
} }

View File

@ -1,5 +1,7 @@
#include <osgSim/SphereSegment> #include <osgSim/SphereSegment>
#include <osg/Notify> #include <osg/Notify>
#include <osg/CullFace>
#include <osg/LineWidth>
using namespace osgSim; using namespace osgSim;
@ -59,10 +61,11 @@ of the specified area of the sphere's EdgeLine.
class SphereSegment::EdgeLine: public osg::Drawable class SphereSegment::EdgeLine: public osg::Drawable
{ {
public: public:
EdgeLine(SphereSegment* ss): osg::Drawable(), _ss(ss) {} EdgeLine(SphereSegment* ss): osg::Drawable(), _ss(ss) { init(); }
EdgeLine():_ss(0) EdgeLine():_ss(0)
{ {
init();
osg::notify(osg::WARN)<< osg::notify(osg::WARN)<<
"Warning: unexpected call to osgSim::SphereSegment::EdgeLine() default constructor"<<std::endl; "Warning: unexpected call to osgSim::SphereSegment::EdgeLine() default constructor"<<std::endl;
} }
@ -79,6 +82,15 @@ public:
protected: protected:
void init()
{
// switch off lighting.
getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
//getOrCreateStateSet()->setAttributeAndModes(new osg::LineWidth(2.0),osg::StateAttribute::OFF);
}
virtual bool computeBound() const; virtual bool computeBound() const;
private: private:
@ -156,10 +168,11 @@ class SphereSegment::Spoke: public osg::Drawable
{ {
public: public:
Spoke(SphereSegment* ss, SphereSegment::BoundaryAngle azAngle, SphereSegment::BoundaryAngle elevAngle): Spoke(SphereSegment* ss, SphereSegment::BoundaryAngle azAngle, SphereSegment::BoundaryAngle elevAngle):
osg::Drawable(), _ss(ss), _azAngle(azAngle), _elevAngle(elevAngle) {} osg::Drawable(), _ss(ss), _azAngle(azAngle), _elevAngle(elevAngle) { init(); }
Spoke():_ss(0) Spoke():_ss(0)
{ {
init();
osg::notify(osg::WARN)<< osg::notify(osg::WARN)<<
"Warning: unexpected call to osgSim::SphereSegment::Spoke() default constructor"<<std::endl; "Warning: unexpected call to osgSim::SphereSegment::Spoke() default constructor"<<std::endl;
} }
@ -176,6 +189,14 @@ public:
protected: protected:
void init()
{
// switch off lighting.
getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
//getOrCreateStateSet()->setAttributeAndModes(new osg::LineWidth(2.0),osg::StateAttribute::OFF);
}
virtual bool computeBound() const; virtual bool computeBound() const;
private: private:
@ -347,47 +368,101 @@ void SphereSegment::Surface_drawImplementation(osg::State& /* state */) const
{ {
glColor4fv(_surfaceColor.ptr()); glColor4fv(_surfaceColor.ptr());
for(int i=0; i+1<=_density; i++) bool drawBackSide = true;
{ bool drawFrontSide = true;
// Because we're drawing quad strips, we need to work out
// two azimuth values, to form each edge of the (z-vertical)
// strips
float az1 = _azMin + (i*azIncr);
float az2 = _azMin + ((i+1)*azIncr);
glBegin(GL_QUAD_STRIP); // draw back side.
for (int j=0; j<=_density; j++) if (drawBackSide)
{
for(int i=0; i+1<=_density; i++)
{ {
float elev = _elevMin + (j*elevIncr); // Because we're drawing quad strips, we need to work out
// two azimuth values, to form each edge of the (z-vertical)
// strips
float az1 = _azMin + (i*azIncr);
float az2 = _azMin + ((i+1)*azIncr);
// QuadStrip Edge formed at az1 glBegin(GL_QUAD_STRIP);
// ---------------------------- for (int j=0; j<=_density; j++)
{
float elev = _elevMin + (j*elevIncr);
// Work out the sphere normal // QuadStrip Edge formed at az1
float x = cos(elev)*sin(az1); // ----------------------------
float y = cos(elev)*cos(az1);
float z = sin(elev);
glNormal3f(x, y, z); // Work out the sphere normal
glVertex3f(_centre.x() + _radius*x, float x = cos(elev)*sin(az1);
_centre.y() + _radius*y, float y = cos(elev)*cos(az1);
_centre.z() + _radius*z); float z = sin(elev);
// QuadStrip Edge formed at az2 glNormal3f(-x, -y, -z);
// ---------------------------- glVertex3f(_centre.x() + _radius*x,
_centre.y() + _radius*y,
_centre.z() + _radius*z);
// Work out the sphere normal // QuadStrip Edge formed at az2
x = cos(elev)*sin(az2); // ----------------------------
y = cos(elev)*cos(az2);
// z = sin(elev); z doesn't change
glNormal3f(x, y, z); // Work out the sphere normal
glVertex3f(_centre.x() + _radius*x, x = cos(elev)*sin(az2);
_centre.y() + _radius*y, y = cos(elev)*cos(az2);
_centre.z() + _radius*z); // z = sin(elev); z doesn't change
glNormal3f(-x, -y, -z);
glVertex3f(_centre.x() + _radius*x,
_centre.y() + _radius*y,
_centre.z() + _radius*z);
}
glEnd();
} }
glEnd(); }
}
// draw front side
if (drawFrontSide)
{
for(int i=0; i+1<=_density; i++)
{
// Because we're drawing quad strips, we need to work out
// two azimuth values, to form each edge of the (z-vertical)
// strips
float az1 = _azMin + (i*azIncr);
float az2 = _azMin + ((i+1)*azIncr);
glBegin(GL_QUAD_STRIP);
for (int j=0; j<=_density; j++)
{
float elev = _elevMin + (j*elevIncr);
// QuadStrip Edge formed at az1
// ----------------------------
// Work out the sphere normal
float x = cos(elev)*sin(az2);
float y = cos(elev)*cos(az2);
float z = sin(elev);
glNormal3f(x, y, z);
glVertex3f(_centre.x() + _radius*x,
_centre.y() + _radius*y,
_centre.z() + _radius*z);
// QuadStrip Edge formed at az2
// ----------------------------
// Work out the sphere normal
// z = sin(elev); z doesn't change
x = cos(elev)*sin(az1);
y = cos(elev)*cos(az1);
glNormal3f(x, y, z);
glVertex3f(_centre.x() + _radius*x,
_centre.y() + _radius*y,
_centre.z() + _radius*z);
}
glEnd();
}
}
} }
} }
@ -541,52 +616,154 @@ void SphereSegment::Side_drawImplementation(osg::State& /* state */,
// ---------------------------- // ----------------------------
if(_drawMask & SIDES) if(_drawMask & SIDES)
{ {
if(orientation == AZIM) // This is a plane at a given azimuth bool drawBackSide = true;
{ bool drawFrontSide = true;
const float az = (boundaryAngle==MIN?_azMin:_azMax); int start, end, delta;
const float elevIncr = (_elevMax - _elevMin)/_density;
// Normal glColor4fv(_planeColor.ptr());
osg::Vec3 normal = osg::Vec3(cos(_elevMin)*sin(az), cos(_elevMin)*cos(az), sin(_elevMin))
^ osg::Vec3(cos(_elevMax)*sin(az), cos(_elevMax)*cos(az), sin(_elevMax));
if(boundaryAngle==MIN) normal = -normal; // Make sure normals orientationint 'outwards'
glNormal3fv(normal.ptr());
// Tri fan // draw back side.
glBegin(GL_TRIANGLE_FAN); if (drawBackSide)
glVertex3fv(_centre.ptr()); {
for (int j=0; j<=_density; j++)
if(orientation == AZIM) // This is a plane at a given azimuth
{ {
float elev = _elevMin + (j*elevIncr); const float az = (boundaryAngle==MIN?_azMin:_azMax);
glVertex3f( _centre.x() + _radius*cos(elev)*sin(az), const float elevIncr = (_elevMax - _elevMin)/_density;
_centre.y() + _radius*cos(elev)*cos(az),
_centre.z() + _radius*sin(elev)); // Normal
osg::Vec3 normal = osg::Vec3(cos(_elevMin)*sin(az), cos(_elevMin)*cos(az), sin(_elevMin))
^ osg::Vec3(cos(_elevMax)*sin(az), cos(_elevMax)*cos(az), sin(_elevMax));
if (boundaryAngle==MIN)
{
start = _density;
end = 0;
}
else
{
start = 0;
end = _density;
normal = -normal; // Make sure normals orientationint 'outwards'
}
delta = end>start?1:-1;
if (drawBackSide)
{
// Tri fan
glNormal3f(-normal.x(),-normal.y(),-normal.z());
glBegin(GL_TRIANGLE_FAN);
glVertex3fv(_centre.ptr());
for (int j=start; j!=end+delta; j+=delta)
{
float elev = _elevMin + (j*elevIncr);
glVertex3f( _centre.x() + _radius*cos(elev)*sin(az),
_centre.y() + _radius*cos(elev)*cos(az),
_centre.z() + _radius*sin(elev));
}
glEnd();
}
if (boundaryAngle==MIN)
{
start = 0;
end = _density;
}
else
{
start = _density;
end = 0;
}
delta = end>start?1:-1;
if (drawFrontSide)
{
glNormal3fv(normal.ptr());
glBegin(GL_TRIANGLE_FAN);
glVertex3fv(_centre.ptr());
for (int j=start; j!=end+delta; j+=delta)
{
float elev = _elevMin + (j*elevIncr);
glVertex3f( _centre.x() + _radius*cos(elev)*sin(az),
_centre.y() + _radius*cos(elev)*cos(az),
_centre.z() + _radius*sin(elev));
}
glEnd();
}
} }
glEnd(); else if(orientation == ELEV) // This is a plane at a given elevation
}
else if(orientation == ELEV) // This is a plane at a given elevation
{
const float elev = (boundaryAngle==MIN?_elevMin:_elevMax);
const float azIncr = (_azMax - _azMin)/_density;
// Normal
osg::Vec3 normal = osg::Vec3(cos(elev)*sin(_azMax), cos(elev)*cos(_azMax), sin(elev))
^ osg::Vec3(cos(elev)*sin(_azMin), cos(elev)*cos(_azMin), sin(elev));
if(boundaryAngle==MIN) normal = -normal; // Make sure normals orientationint 'outwards'
glNormal3fv(normal.ptr());
// Tri fan
glBegin(GL_TRIANGLE_FAN);
glVertex3fv(_centre.ptr());
for(int i=0; i<=_density; i++)
{ {
float az = _azMin + (i*azIncr); const float elev = (boundaryAngle==MIN?_elevMin:_elevMax);
glVertex3f( _centre.x() + _radius*cos(elev)*sin(az), const float azIncr = (_azMax - _azMin)/_density;
_centre.y() + _radius*cos(elev)*cos(az),
_centre.z() + _radius*sin(elev)); // Normal
osg::Vec3 normal = osg::Vec3(cos(elev)*sin(_azMax), cos(elev)*cos(_azMax), sin(elev))
^ osg::Vec3(cos(elev)*sin(_azMin), cos(elev)*cos(_azMin), sin(elev));
if (boundaryAngle==MIN)
{
start = _density;
end = 0;
normal = -normal; // Make sure normals orientationint 'outwards'
}
else
{
start = 0;
end = _density;
}
delta = end>start?1:-1;
if (drawBackSide)
{
glNormal3f(-normal.x(),-normal.y(),-normal.z());
// Tri fan
glBegin(GL_TRIANGLE_FAN);
glVertex3fv(_centre.ptr());
for (int j=start; j!=end+delta; j+=delta)
{
float az = _azMin + (j*azIncr);
glVertex3f( _centre.x() + _radius*cos(elev)*sin(az),
_centre.y() + _radius*cos(elev)*cos(az),
_centre.z() + _radius*sin(elev));
}
glEnd();
}
if (boundaryAngle==MIN)
{
start = 0;
end = _density;
}
else
{
start = _density;
end = 0;
}
delta = end>start?1:-1;
if (drawFrontSide)
{
glNormal3fv(normal.ptr());
// Tri fan
glBegin(GL_TRIANGLE_FAN);
glVertex3fv(_centre.ptr());
for (int j=start; j!=end+delta; j+=delta)
{
float az = _azMin + (j*azIncr);
glVertex3f( _centre.x() + _radius*cos(elev)*sin(az),
_centre.y() + _radius*cos(elev)*cos(az),
_centre.z() + _radius*sin(elev));
}
glEnd();
}
} }
glEnd(); }
}
} }
} }
@ -631,8 +808,6 @@ bool SphereSegment::Side_computeBound(osg::BoundingBox& bbox,
void SphereSegment::Spoke_drawImplementation(osg::State&, BoundaryAngle azAngle, BoundaryAngle elevAngle) const void SphereSegment::Spoke_drawImplementation(osg::State&, BoundaryAngle azAngle, BoundaryAngle elevAngle) const
{ {
// FIXME: Disable lighting for this draw routine
if(_drawMask & SPOKES){ if(_drawMask & SPOKES){
glColor4fv(_spokeColor.ptr()); glColor4fv(_spokeColor.ptr());
@ -680,18 +855,11 @@ struct ActivateTransparencyOnType
{ {
if(typeid(*dptr)==_t) if(typeid(*dptr)==_t)
{ {
osg::StateSet* ss = dptr->getStateSet(); osg::StateSet* ss = dptr->getOrCreateStateSet();
if(!ss)
{
ss = new osg::StateSet();
dptr->setStateSet(ss);
}
ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
osg::BlendFunc* trans = new osg::BlendFunc; ss->setAttributeAndModes(new osg::CullFace(osg::CullFace::BACK),osg::StateAttribute::ON);
trans->setFunction(osg::BlendFunc::ONE,osg::BlendFunc::ONE); ss->setMode(GL_BLEND,osg::StateAttribute::ON);
ss->setAttributeAndModes(trans,osg::StateAttribute::ON);
dptr->dirtyDisplayList(); dptr->dirtyDisplayList();
} }