//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
-// Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//
+#ifdef HAVE_CONFIG_H
+# include <simgear_config.h>
+#endif
+
#include <plib/sg.h>
-#include <plib/ssg.h>
-#include <simgear/props/props.hxx>
#include <simgear/debug/logstream.hxx>
#include <simgear/screen/extensions.hxx>
#include <simgear/scene/model/animation.hxx>
#include <simgear/scene/model/model.hxx>
-#include SG_GLU_H
+#include <simgear/environment/visual_enviro.hxx>
+#include <osg/GLU>
#include "shadowvolume.hxx"
scenery_object ( 0 ),
first_select ( 0 ),
frameNumber ( 0 ),
- numTriangles ( 0 ),
indices ( 0 ),
+ numTriangles ( 0 ),
vertices ( 0 ),
lastSilhouetteIndicesCount ( 0 )
{
ssgBranch *branch = (ssgBranch *) _geometry_leaf;
while( branch && branch->getNumParents() > 0 ) {
- if( !first_select && branch->isA(ssgTypeSelector())) {
+ if( branch->isAKindOf(ssgTypeSelector())) {
first_select = branch;
break;
}
+ if( sgCheckAnimationBranch( (ssgEntity *) branch ) )
+ if( ((SGAnimation *) branch->getUserData())->get_animation_type() == 1) {
+ first_select = branch;
+ break;
+ }
branch = branch->getParent(0);
}
}
}
if( num_tri == 0 )
return;
+ isTranslucent |= geometry_leaf->isTranslucent() ? true : false;
int num_ind = geometry_leaf->getNumVertices();
ind_idx += num_ind;
}
glDrawElements ( GL_TRIANGLES, lastSilhouetteIndicesCount, GL_UNSIGNED_SHORT, silhouetteEdgeIndices ) ;
//Draw caps if required
- if(drawCaps)
+ if(drawCaps)
{
glBegin(GL_TRIANGLES);
{
// check the value of <select> and <range> animation
// wich have been computed during the rendering
-bool SGShadowVolume::ShadowCaster::isSelected ( ssgBranch * branch ) {
+bool SGShadowVolume::ShadowCaster::isSelected ( ssgBranch * branch, float dist ) {
while( branch && branch != lib_object) {
+ if( sgCheckAnimationBranch( (ssgEntity *) branch ) ) {
+ if( ((SGAnimation *) branch->getUserData())->get_animation_type() == 1)
+ if( ((SGShadowAnimation *) branch->getUserData())->get_condition_value() )
+ return false;
+ }
+ // recompute range check since the value in the branch is shared by multiple objects
+ // we can only have the last value
+ if( branch->isA(ssgTypeRangeSelector()) )
+ if( dist >= ((ssgRangeSelector *) branch)->getRange(1) ||
+ dist < ((ssgRangeSelector *) branch)->getRange(0))
+ return false;
if( branch->isA(ssgTypeSelector()) )
if( !((ssgSelector *) branch)->isSelected(0) )
return false;
/
perso2 *-----
*/
-void SGShadowVolume::ShadowCaster::computeShadows(sgMat4 rotation, sgMat4 rotation_translation) {
+void SGShadowVolume::ShadowCaster::computeShadows(sgMat4 rotation, sgMat4 rotation_translation,
+ OccluderType occluder_type) {
// check the select and range ssgSelector node
// object can't cast shadow if it is not visible
+ sgVec4 trans;
+ sgCopyVec4( trans, rotation_translation[3] );
+ sgAddVec4( trans, states->CameraViewM[3] );
+ float dist = sgLengthVec3( trans );
- if( first_select && ! isSelected( first_select) )
+ if( first_select && ! isSelected( first_select, dist) )
return;
- sgMat4 transform ;
- sgMat4 invTransform;
// get the transformations : this comes from animation code for example
// or static transf
+ sgMat4 transf;
+ sgVec3 lightPos;
+ int deltaFrame = occluder_type == SGShadowVolume::occluderTypeAircraft ? 0 : 9;
+ if( states->frameNumber - frameNumber > deltaFrame) {
+ sgMat4 transform ;
+ sgMat4 invTransform;
getNetTransform( (ssgBranch *) geometry_leaf, transform );
- sgMat4 transf;
- sgCopyMat4( transf, transform );
- sgPostMultMat4( transf, rotation_translation );
+ sgCopyMat4( last_transform, transform );
sgPostMultMat4( transform, rotation );
sgTransposeNegateMat4 ( invTransform, transform );
- glLoadMatrixf ( (float *) states->CameraViewM ) ;
- glMultMatrixf( (float *) transf );
-
- sgVec3 lightPos;
sgCopyVec3( lightPos, states->sunPos );
sgXformPnt3( lightPos, invTransform );
// if the geometry has rotated/moved enought then
// we need to recompute the silhouette
// but this computation does not need to be done each frame
- if( (deltaPos > 0.0) && ( states->frameNumber - frameNumber > 4)) {
+ if( deltaPos > 0.0 ) {
CalculateSilhouetteEdges( lightPos );
sgCopyVec3( last_lightpos, lightPosNorm );
frameNumber = states->frameNumber ;
statSilhouette ++;
}
+ }
+ sgCopyMat4( transf, last_transform );
+ sgPostMultMat4( transf, rotation_translation );
+ glLoadMatrixf ( (float *) states->CameraViewM ) ;
+ glMultMatrixf( (float *) transf );
if( states->shadowsDebug_enabled )
{
}
glCullFace(GL_BACK);
- DrawInfiniteShadowVolume( lightPos, false);
+ DrawInfiniteShadowVolume( lightPos, states->shadowsAC_transp_enabled & isTranslucent);
//Decrement stencil buffer for back face depth pass
if( states->use_alpha ) {
}
glCullFace(GL_FRONT);
- DrawInfiniteShadowVolume( lightPos, false);
+ DrawInfiniteShadowVolume( lightPos, states->shadowsAC_transp_enabled & isTranslucent);
}
}
void SGShadowVolume::SceneryObject::computeShadows(void) {
- bool intersect = true;
// compute intersection with view frustum
// use pending_object (pointer on obj transform) & tile transform
// to get position
- sgMat4 position, CamXpos;
- sgFrustum *f = ssgGetFrustum();
- pending_object->getParent(0)->getNetTransform( position );
- sgCopyMat4 ( CamXpos, states->CameraViewM ) ;
- sgPreMultMat4 ( CamXpos, position ) ;
-
- sgSphere tmp = *(pending_object->getBSphere()) ;
- if ( tmp.isEmpty () )
- intersect = false;
- else {
- // 7000
- float max_dist = 5000.0f;
- tmp . orthoXform ( CamXpos ) ;
- // cull if too far
- if ( -tmp.center[2] - tmp.radius > max_dist )
- intersect = false;
- else if( tmp.center[2] == 0.0 )
- intersect = true;
- // cull if too small on screen
- else if ( tmp.radius / sgAbs(tmp.center[2]) < 1.0 / 40.0 )
- intersect = false;
- else
- intersect = SSG_OUTSIDE != (ssgCullResult) f -> contains ( &tmp );
- }
-
- if( intersect ) {
if( !scenery_object ) {
if( states->frameNumber - states->lastTraverseTreeFrame > 5 ) {
find_trans();
sgSetVec4( rotation[3], 0, 0, 0, 1);
ShadowCaster_list::iterator iShadowCaster;
- for(iShadowCaster = parts.begin() ; iShadowCaster != parts.end() ; iShadowCaster ++ ) {
- (*iShadowCaster)->computeShadows(rotation, rotation_translation);
+ for(iShadowCaster = parts.begin() ; iShadowCaster != parts.end() ; ++iShadowCaster ) {
+ (*iShadowCaster)->computeShadows(rotation, rotation_translation, occluder_type);
+ }
+}
+
+static ssgCullResult cull_test ( ssgEntity *e, sgFrustum *f, sgMat4 m, int test_needed )
+{
+ if ( ! test_needed )
+ return SSG_INSIDE ;
+
+ sgSphere tmp = *(e->getBSphere()) ;
+
+ if ( tmp.isEmpty () )
+ return SSG_OUTSIDE ;
+
+ tmp . orthoXform ( m ) ;
+ if( tmp.center[2] == 0.0 )
+ return SSG_STRADDLE;
+ // cull if too small on screen
+ if ( tmp.radius / sgAbs(tmp.center[2]) < 1.0 / 40.0 )
+ return SSG_OUTSIDE ;
+
+ return (ssgCullResult) f -> contains ( &tmp ) ;
+}
+
+
+void SGShadowVolume::cull ( ssgBranch *b, sgFrustum *f, sgMat4 m, int test_needed )
+{
+ int cull_result = cull_test ( (ssgEntity *) b, f, m, test_needed ) ;
+
+ if ( cull_result == SSG_OUTSIDE )
+ return ;
+ if( b->isA( ssgTypeTransform() ) ) {
+
+ SceneryObject_map::iterator iSceneryObject = sceneryObjects.find( b );
+ if( iSceneryObject != sceneryObjects.end() ) {
+ SceneryObject *an_occluder = iSceneryObject->second;
+ if( shadowsTO_enabled && (an_occluder->occluder_type == occluderTypeTileObject) ||
+ shadowsAI_enabled && (an_occluder->occluder_type == occluderTypeAI ) ||
+ shadowsAC_enabled && (an_occluder->occluder_type == occluderTypeAircraft ) )
+ an_occluder->computeShadows();
+
+ return;
+ }
+ sgMat4 tmp, transform ;
+ sgCopyMat4 ( tmp, m ) ;
+ ((ssgTransform *)b)->getTransform( transform );
+ sgPreMultMat4 ( tmp, transform ) ;
+ glPushMatrix () ;
+ glLoadMatrixf ( (float *) tmp ) ;
+ for ( ssgEntity *e = b->getKid ( 0 ) ; e != NULL ; e = b->getNextKid() )
+ cull ( (ssgBranch *) e, f, tmp, cull_result != SSG_INSIDE ) ;
+ glPopMatrix () ;
+ } else if( b->isAKindOf( ssgTypeSelector() ) ) {
+ int s = ((ssgSelector *) b)->getSelect() ;
+ if( b->isA( ssgTypeRangeSelector() ) ) {
+ float range = sgLengthVec3 ( m [ 3 ] ) ;
+ s = (range < ((ssgRangeSelector *) b)->getRange(1) &&
+ range >= ((ssgRangeSelector *) b)->getRange(0) ) ? 1 : 0;
}
+ for ( ssgEntity *e = b->getKid ( 0 ) ; e != NULL ; e = b->getNextKid(), s >>= 1 )
+ if ( s & 1 )
+ cull ( (ssgBranch *) e, f, m, cull_result != SSG_INSIDE ) ;
+ } else if( b->isAKindOf( ssgTypeBranch() ) ) {
+ char *name = b->getName();
+ // quick exit for the hundreds of ground leafs
+ if( name && !strcmp(name, "LocalTerrain") )
+ return;
+ for ( ssgEntity *e = b->getKid ( 0 ) ; e != NULL ; e = b->getNextKid() )
+ if( ! e->isAKindOf( ssgTypeLeaf() ) )
+ cull ( (ssgBranch *) e, f, m, cull_result != SSG_INSIDE ) ;
}
}
overhead for matrix computation) and at the end this will reduce the number of
silouhette edge by a lot too.
*/
-static bool filterName(const char *leaf_name) {
+static bool filterLeaf(ssgLeaf *this_kid) {
+ const char *leaf_name = this_kid->getName();
+/* ssgSimpleState *sst = (ssgSimpleState *) this_kid->getState();
+ if( sst && sst->isTranslucent() )
+ return false;*/
if( ! leaf_name )
return true;
char lname[20];
- int l = 0;
+ unsigned int l = 0;
char *buff;
for( buff = lname; *leaf_name && l < (sizeof( lname )-1); ++buff, l++ )
*buff = tolower(*leaf_name++);
if( sgCheckAnimationBranch( (ssgEntity *) branch ) ) {
if( ((SGAnimation *) branch->getUserData())->get_animation_type() == 1)
- return;
+ if( ((SGShadowAnimation *) branch->getUserData())->get_condition_value() )
+ return;
}
for(int i = 0 ; i < branch->getNumKids() ; i++) {
ssgEntity *this_kid = branch->getKid( i );
if( this_kid->isAKindOf(ssgTypeLeaf()) ) {
- if( filterName( this_kid->getName()) ) {
+ if( filterLeaf( (ssgLeaf *) this_kid ) ) {
num_tri += ((ssgLeaf *) this_kid)->getNumTriangles();
num_leaf ++;
}
ShadowCaster *new_part = new ShadowCaster( num_tri, branch);
new_part->scenery_object = scenery_object;
new_part->lib_object = lib_object;
+ new_part->isTranslucent = false;
for(int i = 0 ; i < branch->getNumKids() ; i++) {
ssgEntity *this_kid = branch->getKid( i );
if( this_kid->isAKindOf(ssgTypeLeaf()) ) {
- if( filterName( this_kid->getName()) )
+ if( filterLeaf( (ssgLeaf *) this_kid ) )
new_part->addLeaf( tri_idx, ind_idx, (ssgLeaf *) this_kid );
}
}
+ // only do that for aircraft
+ if( occluder_type != SGShadowVolume::occluderTypeAircraft )
+ new_part->isTranslucent = false;
new_part->SetConnectivity();
parts.push_back( new_part );
}
void SGShadowVolume::SceneryObject::find_trans(void) {
ssgBranch *branch = pending_object;
- ssgBranch *prev_branch = pending_object;
// check the existence of the root node
while( branch && branch->getNumParents() > 0 ) {
- prev_branch = branch;
branch = branch->getParent(0);
}
// check if we are connected to the scene graph
}
SGShadowVolume::SceneryObject::SceneryObject(ssgBranch *_scenery_object, OccluderType _occluder_type) :
- pending_object ( _scenery_object ),
- occluder_type ( _occluder_type ),
- scenery_object ( 0 )
+ scenery_object ( 0 ),
+ pending_object ( _scenery_object ),
+ occluder_type ( _occluder_type )
{
// queue objects, don't do all the work in the first frames because of
// massive cpu power needed
SGShadowVolume::SceneryObject::~SceneryObject()
{
ShadowCaster_list::iterator iParts;
- for(iParts = parts.begin() ; iParts != parts.end(); iParts++ ) {
+ for(iParts = parts.begin() ; iParts != parts.end(); ++iParts ) {
delete *iParts;
}
parts.clear();
glDisable( GL_FOG );
glEnable( GL_CULL_FACE );
// glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
-// glPolygonOffset(0.0,5.0);
- glPolygonOffset(0.0,30.0);
+ glPolygonOffset(0.0,2.0);
+// glPolygonOffset(0.0,30.0);
glEnable(GL_POLYGON_OFFSET_FILL);
glShadeModel(GL_FLAT);
glEnable( GL_DEPTH_TEST );
glDepthFunc(GL_LESS);
- SceneryObject_map::iterator iSceneryObject;
- // compute shadows for each objects
- for(iSceneryObject = sceneryObjects.begin() ; iSceneryObject != sceneryObjects.end(); iSceneryObject++ ) {
- SceneryObject *an_occluder = iSceneryObject->second;
- if( shadowsTO_enabled && (an_occluder->occluder_type == occluderTypeTileObject) ||
- shadowsAI_enabled && (an_occluder->occluder_type == occluderTypeAI ) ||
- shadowsAC_enabled && (an_occluder->occluder_type == occluderTypeAircraft ) )
- an_occluder->computeShadows();
+ {
+ float w, h;
+ sgFrustum frustum;
+ sgEnviro.getFOV( w, h );
+ frustum.setFOV( w, h );
+ frustum.setNearFar(0.1f, 5000.0f);
+ sgMat4 m;
+ ssgGetModelviewMatrix( m );
+ cull( ssg_root, &frustum, m, true);
}
glPopAttrib();
}
-SGShadowVolume::SGShadowVolume() :
- init_done( false ),
+SGShadowVolume::SGShadowVolume( ssgBranch *root ) :
shadows_enabled( false ),
frameNumber( 0 ),
- lastTraverseTreeFrame ( 0 )
+ lastTraverseTreeFrame ( 0 ),
+ ssg_root( root ),
+ shadows_rendered( false )
{
states = this;
}
SGShadowVolume::~SGShadowVolume() {
SceneryObject_map::iterator iSceneryObject;
- for(iSceneryObject = sceneryObjects.begin() ; iSceneryObject != sceneryObjects.end(); iSceneryObject++ ) {
+ for(iSceneryObject = sceneryObjects.begin() ; iSceneryObject != sceneryObjects.end(); ++iSceneryObject ) {
delete iSceneryObject->second;
}
sceneryObjects.clear();
}
void SGShadowVolume::init(SGPropertyNode *sim_rendering_options) {
- init_done = true;
shadows_enabled = true;
sim_rendering = sim_rendering_options;
- int stencilBits = 0, alphaBits = 0;
+ GLint stencilBits = 0, alphaBits = 0;
glGetIntegerv( GL_STENCIL_BITS, &stencilBits );
glGetIntegerv( GL_ALPHA_BITS, &alphaBits );
bool hasSubtractiveBlend = SGIsOpenGLExtensionSupported("GL_EXT_blend_subtract");
void SGShadowVolume::startOfFrame(void) {
}
void SGShadowVolume::deleteOccluderFromTile(ssgBranch *tile) {
- SceneryObject_map::iterator iSceneryObject, iPrevious;
- iPrevious = sceneryObjects.begin();
- for(iSceneryObject = sceneryObjects.begin() ; iSceneryObject != sceneryObjects.end(); iSceneryObject++ ) {
- if( iSceneryObject->second->tile == tile ) {
- delete iSceneryObject->second;
- sceneryObjects.erase( iSceneryObject );
- iSceneryObject = iPrevious;
+ SceneryObject_map::iterator iSceneryObject;
+ for(iSceneryObject = sceneryObjects.begin() ; iSceneryObject != sceneryObjects.end(); ) {
+ SceneryObject_map::iterator iCurrent = iSceneryObject ++;
+ if( iCurrent->second->tile == tile ) {
+ delete iCurrent->second;
+ sceneryObjects.erase( iCurrent );
}
- iPrevious = iSceneryObject;
}
}
void SGShadowVolume::deleteOccluder(ssgBranch *occluder) {
- ssgBranch *branch = occluder;
// skip first node and go to first transform (placement)
while( occluder && !occluder->isA(ssgTypeTransform()))
occluder = (ssgBranch *) occluder->getKid(0);
}
void SGShadowVolume::addOccluder(ssgBranch *occluder, OccluderType occluder_type, ssgBranch *tile) {
-
- ssgBranch *branch = occluder;
-
// skip first node and go to first transform (placement)
while( occluder && !occluder->isA(ssgTypeTransform()))
occluder = (ssgBranch *) occluder->getKid(0);
double gst, double SunRightAscension, double SunDeclination, double sunAngle) {
shadowsAC_enabled = sim_rendering->getBoolValue("shadows-ac", false);
+ shadowsAC_transp_enabled = sim_rendering->getBoolValue("shadows-ac-transp", false);
shadowsAI_enabled = sim_rendering->getBoolValue("shadows-ai", false);
shadowsTO_enabled = sim_rendering->getBoolValue("shadows-to", false);
shadowsDebug_enabled = sim_rendering->getBoolValue("shadows-debug", false);
-// shadows_enabled = sim_rendering->getBoolValue("shadows", false);
shadows_enabled = shadowsAC_enabled || shadowsAI_enabled || shadowsTO_enabled;
shadows_enabled &= canDoAlpha || canDoStencil;
use_alpha = ((!canDoStencil) || sim_rendering->getBoolValue("shadows-alpha", false)) &&
if( ! shadows_enabled )
return;
- sgMat4 view_angle;
+ shadows_rendered = false;
sun_angle = sunAngle;
- {
- sgMat4 LON, LAT;
- sgVec3 axis;
-
- sgSetVec3( axis, 0.0, 0.0, 1.0 );
- sgMakeRotMat4( LON, lon, axis );
-
- sgSetVec3( axis, 0.0, 1.0, 0.0 );
- sgMakeRotMat4( LAT, 90.0 - lat, axis );
-
- sgMat4 TRANSFORM;
-
- sgMakeIdentMat4 ( TRANSFORM );
- sgPreMultMat4( TRANSFORM, LON );
- sgPreMultMat4( TRANSFORM, LAT );
-
- sgCoord pos;
- sgSetCoord( &pos, TRANSFORM );
-
- sgMakeCoordMat4( view_angle, &pos );
- }
{
sgMat4 GST, RA, DEC;
sgVec3 axis;
sgSetVec3( axis, 1.0, 0.0, 0.0 );
sgMakeRotMat4( DEC, SunDeclination * SGD_RADIANS_TO_DEGREES, axis );
- sgInvertMat4( invViewAngle, view_angle);
-
sgMat4 TRANSFORM;
sgMakeIdentMat4( TRANSFORM );
sgPreMultMat4( TRANSFORM, GST );
void SGShadowVolume::endOfFrame(void) {
if( ! shadows_enabled )
return;
+ if( shadows_rendered )
+ return;
glBindTexture(GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_1D, 0);
glMatrixMode(GL_MODELVIEW);
-
computeShadows();
-
frameNumber ++;
+ shadows_rendered = true;
+}
+
+int SGShadowVolume::ACpostTravCB( ssgEntity *entity, int traversal_mask ) {
+ if( states->shadowsAC_transp_enabled && (SSGTRAV_CULL & traversal_mask) )
+ states->endOfFrame();
+ return 0;
}