Esempio n. 1
0
void VRNature::addScrub(VRPolygonPtr area, bool addGround) {
    float a = area->computeArea();
    if (a == 0) return;

    //VRTimer timer; timer.start();
    //int t0 = timer.stop();

    if (auto t = terrain.lock()) t->elevatePolygon(area, 0.18);
    Vec3d median = area->getBoundingBox().center();
    area->translate(-median);
    for (auto p : area->getRandomPoints(1.0, -0.1, 0.7)) createRandomBush(median+p);

    if (addGround) {
        Triangulator tri;
        tri.add(*area);
        auto ground = tri.compute();
        ground->translate(median);
        ground->setPositionalTexCoords(1.0, 0, Vec3i(0,2,1));
        ground->updateNormals();
        ground->flipNormals();

        if (!groundPatches) {
            groundPatches = VRGeometry::create("groundPatches");
            addChild( groundPatches );
            VRTextureGenerator tg;
            tg.addSimpleNoise( Vec3i(128,128,1), false, Color4f(0.85,0.8,0.75,1), Color4f(0.5,0.3,0,1) );
            auto mat = VRMaterial::create("earth");
            mat->setTexture(tg.compose());
            groundPatches->setMaterial(mat);
        }
        groundPatches->merge(ground, ground->getPose());
        groundPatches->setPositionalTexCoords(1.0, 0, Vec3i(0,2,1)); // TODO: fix issues in VRGeoData
    }
}
Esempio n. 2
0
void VRNature::addGrassPatch(VRPolygonPtr Area, bool updateLODs, bool addGround) { // TODO: needs optimizations!
    //VRTimer timer; timer.start();
    //int t0 = timer.stop();
    //cout << "VRNature::addGrassPatch " << t0 << endl;
    int i=0;
    auto ground = VRGeometry::create("ground");

    float a = Area->computeArea();
    if (a == 0) return;
    //cout << "VRNature::addGrassPatch " << a << endl;

    map<VRLodLeafPtr, bool> toUpdate;

    for (auto area : Area->gridSplit(10.0)) {
        if (area->isCCW()) area->reverseOrder();
        //cout << " sub Area " << i << "  " << timer.stop() - t0 << endl;
        if (auto t = terrain.lock()) t->elevatePolygon(area, 0.18);
        Vec3d median = area->getBoundingBox().center();
        area->translate(-median);
        //cout << "  A1 " << timer.stop() - t0 << endl;
        auto grass = VRGrassPatch::create();
        grass->addAttachment("grass", 0);
        grass->setArea(area);
        //cout << "  A2 " << timer.stop() - t0 << endl;
        grassPatchRefs[grass.get()] = grass;
        auto leaf = lodTree->addObject(grass, median, 0); // pose contains the world position!
        grass->setWorldPosition(median);
        toUpdate[leaf] = true;


        //cout << "  A3 " << timer.stop() - t0 << endl;

        //cout << " VRNature::addGrassPatch " << median << "   " << area->computeArea() << endl;

        if (addGround) {
            Triangulator tri;
            tri.add(*area);
            auto geo = tri.compute();
            geo->translate(median);
            ground->merge(geo);
        }

        i++;
    }

    if (updateLODs) for (auto l : toUpdate) computeLODs(l.first);

    if (addGround) {
        VRTextureGenerator tg;
        tg.addSimpleNoise( Vec3i(128,128,1), true, Color4f(0.85,0.8,0.75,1), Color4f(0.5,0.3,0,1) );
        auto mat = VRMaterial::create("earth");
        mat->setTexture(tg.compose());
        mat->clearTransparency();
        ground->setPositionalTexCoords(1.0, 0, Vec3i(0,2,1));
        //addChild(ground);
        if (!grassGroundPatches) {
            grassGroundPatches = VRGeometry::create("grassGroundPatches");
            addChild( grassGroundPatches );
            grassGroundPatches->setMaterial(mat);
        }
        grassGroundPatches->merge(ground);
    }
}