Example #1
0
bool S3D::ReadVector( std::istream& aFile, SGVECTOR& aVector )
{
    double x, y, z;
    aFile.read( (char*)&x, sizeof(double) );
    aFile.read( (char*)&y, sizeof(double) );
    aFile.read( (char*)&z, sizeof(double) );
    aVector.SetVector( x, y, z );

    if( aFile.fail() )
        return false;

    return true;
}
static SCENEGRAPH* vrmlToSG( VRML_LAYER& vpcb, int idxColor, SGNODE* aParent, double top, double bottom )
{
    vpcb.Tesselate( NULL );
    std::vector< double > vertices;
    std::vector< int > idxPlane;
    std::vector< int > idxSide;

    if( top < bottom )
    {
        double tmp = top;
        top = bottom;
        bottom = tmp;
    }

    if( !vpcb.Get3DTriangles( vertices, idxPlane, idxSide, top, bottom ) )
    {
        #ifdef DEBUG
        do {
            std::ostringstream ostr;
            std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
            std::cerr << " * [INFO] no vertex data";
            wxLogTrace( MASK_IDF, "%s\n", ostr.str().c_str() );
        } while( 0 );
        #endif

        return NULL;
    }

    if( ( idxPlane.size() % 3 ) || ( idxSide.size() % 3 ) )
    {
        #ifdef DEBUG
        do {
            std::ostringstream ostr;
            std::cerr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
            std::cerr << " * [BUG] index lists are not a multiple of 3 (not a triangle list)";
            wxLogTrace( MASK_IDF, "%s\n", ostr.str().c_str() );
        } while( 0 );
        #endif

        return NULL;
    }

    std::vector< SGPOINT > vlist;
    size_t nvert = vertices.size() / 3;
    size_t j = 0;

    for( size_t i = 0; i < nvert; ++i, j+= 3 )
        vlist.push_back( SGPOINT( vertices[j], vertices[j+1], vertices[j+2] ) );

    // create the intermediate scenegraph
    IFSG_TRANSFORM* tx0 = new IFSG_TRANSFORM( aParent );            // tx0 = Transform for this outline
    IFSG_SHAPE* shape = new IFSG_SHAPE( *tx0 );                     // shape will hold (a) all vertices and (b) a local list of normals
    IFSG_FACESET* face = new IFSG_FACESET( *shape );                // this face shall represent the top and bottom planes
    IFSG_COORDS* cp = new IFSG_COORDS( *face );                     // coordinates for all faces
    cp->SetCoordsList( nvert, &vlist[0] );
    IFSG_COORDINDEX* coordIdx = new IFSG_COORDINDEX( *face );       // coordinate indices for top and bottom planes only
    coordIdx->SetIndices( idxPlane.size(), &idxPlane[0] );
    IFSG_NORMALS* norms = new IFSG_NORMALS( *face );                // normals for the top and bottom planes

    // number of TOP (and bottom) vertices
    j = nvert / 2;

    // set the TOP normals
    for( size_t i = 0; i < j; ++i )
        norms->AddNormal( 0.0, 0.0, 1.0 );

    // set the BOTTOM normals
    for( size_t i = 0; i < j; ++i )
        norms->AddNormal( 0.0, 0.0, -1.0 );

    // assign a color from the palette
    SGNODE* modelColor = getColor( *shape, idxColor );

    // create a second shape describing the vertical walls of the IDF extrusion
    // using per-vertex-per-face-normals
    shape->NewNode( *tx0 );
    shape->AddRefNode( modelColor );    // set the color to be the same as the top/bottom
    face->NewNode( *shape );
    cp->NewNode( *face );               // new vertex list
    norms->NewNode( *face );            // new normals list
    coordIdx->NewNode( *face );         // new index list

    // populate the new per-face vertex list and its indices and normals
    std::vector< int >::iterator sI = idxSide.begin();
    std::vector< int >::iterator eI = idxSide.end();

    size_t sidx = 0;    // index to the new coord set
    SGPOINT p1, p2, p3;
    SGVECTOR vnorm;

    while( sI != eI )
    {
        p1 = vlist[*sI];
        cp->AddCoord( p1 );
        ++sI;

        p2 = vlist[*sI];
        cp->AddCoord( p2 );
        ++sI;

        p3 = vlist[*sI];
        cp->AddCoord( p3 );
        ++sI;

        vnorm.SetVector( S3D::CalcTriNorm( p1, p2, p3 ) );
        norms->AddNormal( vnorm );
        norms->AddNormal( vnorm );
        norms->AddNormal( vnorm );

        coordIdx->AddIndex( (int)sidx );
        ++sidx;
        coordIdx->AddIndex( (int)sidx );
        ++sidx;
        coordIdx->AddIndex( (int)sidx );
        ++sidx;
    }

    SCENEGRAPH* data = (SCENEGRAPH*)tx0->GetRawPtr();

    // delete the API wrappers
    delete shape;
    delete face;
    delete coordIdx;
    delete cp;
    delete tx0;

    return data;
}