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

@ -82,8 +82,8 @@ osg::Node* createSphereSegment()
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,6 +368,12 @@ void SphereSegment::Surface_drawImplementation(osg::State& /* state */) const
{ {
glColor4fv(_surfaceColor.ptr()); glColor4fv(_surfaceColor.ptr());
bool drawBackSide = true;
bool drawFrontSide = true;
// draw back side.
if (drawBackSide)
{
for(int i=0; i+1<=_density; i++) for(int i=0; i+1<=_density; i++)
{ {
// Because we're drawing quad strips, we need to work out // Because we're drawing quad strips, we need to work out
@ -368,7 +395,7 @@ void SphereSegment::Surface_drawImplementation(osg::State& /* state */) const
float y = cos(elev)*cos(az1); float y = cos(elev)*cos(az1);
float z = sin(elev); float z = sin(elev);
glNormal3f(x, y, z); glNormal3f(-x, -y, -z);
glVertex3f(_centre.x() + _radius*x, glVertex3f(_centre.x() + _radius*x,
_centre.y() + _radius*y, _centre.y() + _radius*y,
_centre.z() + _radius*z); _centre.z() + _radius*z);
@ -381,6 +408,53 @@ void SphereSegment::Surface_drawImplementation(osg::State& /* state */) const
y = cos(elev)*cos(az2); y = cos(elev)*cos(az2);
// z = sin(elev); z doesn't change // 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();
}
}
// 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); glNormal3f(x, y, z);
glVertex3f(_centre.x() + _radius*x, glVertex3f(_centre.x() + _radius*x,
_centre.y() + _radius*y, _centre.y() + _radius*y,
@ -390,6 +464,7 @@ void SphereSegment::Surface_drawImplementation(osg::State& /* state */) const
} }
} }
} }
}
bool SphereSegment::Surface_computeBound(osg::BoundingBox& bbox) const bool SphereSegment::Surface_computeBound(osg::BoundingBox& bbox) const
{ {
@ -541,6 +616,16 @@ void SphereSegment::Side_drawImplementation(osg::State& /* state */,
// ---------------------------- // ----------------------------
if(_drawMask & SIDES) if(_drawMask & SIDES)
{ {
bool drawBackSide = true;
bool drawFrontSide = true;
int start, end, delta;
glColor4fv(_planeColor.ptr());
// draw back side.
if (drawBackSide)
{
if(orientation == AZIM) // This is a plane at a given azimuth if(orientation == AZIM) // This is a plane at a given azimuth
{ {
const float az = (boundaryAngle==MIN?_azMin:_azMax); const float az = (boundaryAngle==MIN?_azMin:_azMax);
@ -549,13 +634,27 @@ void SphereSegment::Side_drawImplementation(osg::State& /* state */,
// Normal // Normal
osg::Vec3 normal = osg::Vec3(cos(_elevMin)*sin(az), cos(_elevMin)*cos(az), sin(_elevMin)) 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)); ^ 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());
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 // Tri fan
glNormal3f(-normal.x(),-normal.y(),-normal.z());
glBegin(GL_TRIANGLE_FAN); glBegin(GL_TRIANGLE_FAN);
glVertex3fv(_centre.ptr()); glVertex3fv(_centre.ptr());
for (int j=0; j<=_density; j++) for (int j=start; j!=end+delta; j+=delta)
{ {
float elev = _elevMin + (j*elevIncr); float elev = _elevMin + (j*elevIncr);
glVertex3f( _centre.x() + _radius*cos(elev)*sin(az), glVertex3f( _centre.x() + _radius*cos(elev)*sin(az),
@ -564,6 +663,35 @@ void SphereSegment::Side_drawImplementation(osg::State& /* state */,
} }
glEnd(); 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();
}
}
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 elev = (boundaryAngle==MIN?_elevMin:_elevMax);
@ -572,21 +700,70 @@ void SphereSegment::Side_drawImplementation(osg::State& /* state */,
// Normal // Normal
osg::Vec3 normal = osg::Vec3(cos(elev)*sin(_azMax), cos(elev)*cos(_azMax), sin(elev)) 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)); ^ 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());
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 // Tri fan
glBegin(GL_TRIANGLE_FAN); glBegin(GL_TRIANGLE_FAN);
glVertex3fv(_centre.ptr()); glVertex3fv(_centre.ptr());
for(int i=0; i<=_density; i++) for (int j=start; j!=end+delta; j+=delta)
{ {
float az = _azMin + (i*azIncr); float az = _azMin + (j*azIncr);
glVertex3f( _centre.x() + _radius*cos(elev)*sin(az), glVertex3f( _centre.x() + _radius*cos(elev)*sin(az),
_centre.y() + _radius*cos(elev)*cos(az), _centre.y() + _radius*cos(elev)*cos(az),
_centre.z() + _radius*sin(elev)); _centre.z() + _radius*sin(elev));
} }
glEnd(); 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();
}
}
}
} }
} }
@ -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();
} }