示例#1
0
void HouseManager::load(const std::string& fileName)
{
    try {
        TiXmlDocument doc;
        doc.Parse(g_resources.readFileContents(fileName).c_str());
        if(doc.Error())
            stdext::throw_exception(stdext::format("failed to load '%s': %s (House XML)", fileName, doc.ErrorDesc()));

        TiXmlElement *root = doc.FirstChildElement();
        if(!root || root->ValueTStr() != "houses")
            stdext::throw_exception("invalid root tag name");

        for(TiXmlElement *elem = root->FirstChildElement(); elem; elem = elem->NextSiblingElement()) {
            if(elem->ValueTStr() != "house")
                stdext::throw_exception("invalid house tag.");

            uint32 houseId = elem->readType<uint32>("houseid");
            HousePtr house = getHouse(houseId);
            if(!house)
                house = HousePtr(new House(houseId)), addHouse(house);

            house->load(elem);
        }
    } catch(std::exception& e) {
        g_logger.error(stdext::format("Failed to load '%s': %s", fileName, e.what()));
    }
}
示例#2
0
static osg::Geode* createObjects( osg::HeightField * grid, unsigned int density )
{    
    osg::Geode* geode = new osg::Geode;

    // set up the texture of the base.
    osg::StateSet* stateset = new osg::StateSet();

    osg::Image * image = new osg::Image();
    image->setImage( 64,64,1,
                     GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
                     GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
                     GL_UNSIGNED_BYTE,
                     orderBytes( objectTexture, sizeof( objectTexture ) ),
                     osg::Image::NO_DELETE );

    osg::Texture2D* texture = new osg::Texture2D;
    texture->setImage(image);

    stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
    stateset->setMode( GL_BLEND,osg::StateAttribute::ON );
              
    stateset->setAttributeAndModes( new osg::CullFace() );
    stateset->setAttributeAndModes( new osg::Depth( osg::Depth::LESS, 0.f, 1.f, true ) );
    stateset->setAttributeAndModes( new osg::AlphaFunc( osg::AlphaFunc::GEQUAL, 0.5f ) );

    geode->setStateSet( stateset );

    osg::Geometry* geometry = new osg::Geometry;
    geode->addDrawable(geometry);

    osg::Vec3Array* vertices = new osg::Vec3Array;
    geometry->setVertexArray(vertices);

    osg::Vec3Array* normals = new osg::Vec3Array;
    geometry->setNormalArray(normals);
    geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);

    osg::Vec2Array* texCoords = new osg::Vec2Array;
    geometry->setTexCoordArray(0, texCoords);    

    osg::Vec4Array* colours = new osg::Vec4Array;
    geometry->setColorArray(colours);
    geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
    colours->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));

    for( unsigned int x = 0; x < grid->getNumColumns() - 1; x++ )
    {
        for( unsigned int y = 0; y < grid->getNumRows() - 1; y++ )
        {

            float z00 = grid->getHeight( x,y );
            float z01 = grid->getHeight( x,y+1 );
            float z10 = grid->getHeight( x+1,y );
            float z11 = grid->getHeight( x+1,y+1 );

            float z = 0.25 * ( z00 + z10 + z11 + z01 );

            if( z < 400 ) continue;
            if( z > 500 ) continue;

            osg::Vec3 o( float(x) * grid->getXInterval(), float(y) * grid->getYInterval(), 0.0f );
            o += grid->getOrigin();

            for( unsigned int d = 0; d < density; d++  )
            {
                osg::Vec3 p( float( rand() ) / RAND_MAX, float( rand() ) / RAND_MAX, 0 );

                z = ( 1.f - p[0] ) * ( 1.f - p[1] ) * z00 + 
                    ( 1.f - p[0] ) * ( p[1] ) * z01 +
                    ( p[0] ) * ( p[1] ) * z11 +
                    ( p[0] ) * ( 1.f - p[1] ) * z10;                

                osg::Vec3 pos(o + osg::Vec3(p.x() * grid->getXInterval(), p.y() * grid->getYInterval(), z));
     
                if( rand() % 3 > 0 )
                    addTree( pos, vertices, normals, texCoords );
                else 
                    addHouse( pos, vertices, normals, texCoords );

            }
        }
    }

    geometry->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, vertices->size()));

    geode->addDrawable(geometry);
    return geode;
}