Exemple #1
0
osg::Geode *Road::createRoadGeode()
{
    roadGeode = new osg::Geode();

    //int n = 100;
    //double h = length/n;
    double h = 0.5;
    double texlength = 10.0;
    double texwidth = 10.0;

    std::map<double, LaneSection *>::iterator lsIt;
    LaneSection *ls;
    for (lsIt = laneSectionMap.begin(); lsIt != laneSectionMap.end(); ++lsIt)
    {
        double lsStart;
        double lsEnd;
        std::map<double, LaneSection *>::iterator nextLsIt = lsIt;
        ++nextLsIt;
        ls = (*lsIt).second;
        lsStart = ls->getStart();

        if (lsStart > length)
        {
            std::cerr << "Inconsistency: Lane Section defined with start position " << lsStart << " not in road boundaries 0 - " << lsEnd << "!" << std::endl;
            break;
        }
        else if (nextLsIt == laneSectionMap.end())
        {
            lsEnd = length;
        }
        else
        {
            lsEnd = (*(nextLsIt)).second->getStart();
            if (lsEnd > length)
                lsEnd = length;
        }

        osg::Geometry *roadGeometry;
        roadGeometry = new osg::Geometry();
        roadGeode->addDrawable(roadGeometry);

        osg::Vec3Array *roadVertices;
        roadVertices = new osg::Vec3Array;
        roadGeometry->setVertexArray(roadVertices);

        osg::Vec3Array *roadNormals;
        roadNormals = new osg::Vec3Array;
        roadGeometry->setNormalArray(roadNormals);
        roadGeometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);

        osg::Vec2Array *roadTexCoords;
        roadTexCoords = new osg::Vec2Array;
        roadGeometry->setTexCoordArray(0, roadTexCoords);

        osg::Vec4Array *roadMarks;
        roadMarks = new osg::Vec4Array;
        roadGeometry->setVertexAttribArray(6, roadMarks);
        roadGeometry->setVertexAttribBinding(6, osg::Geometry::BIND_PER_VERTEX);

        osg::DrawArrays *roadBase;

        int firstVertLane = 0;
        int numVertsLane = 0;
        for (int i = (ls->getNumLanesLeft()); i >= -(ls->getNumLanesRight()); --i)
        {
            if (i == 0)
                continue;
            bool lastRound = false;
            //std::cout << "Lane: " << i << ", lsStart: " << lsStart << ", lsEnd: " << lsEnd << std::endl;
            for (double s = lsStart; s < (lsEnd + h); s += h)
            {
                if (s > lsEnd)
                {
                    //std::cout << "Last round: s = " << s << ", lsEnd: " << lsEnd << std::endl;
                    s = lsEnd;
                    lastRound = true;
                }
                RoadPoint pointLeft;
                RoadPoint pointRight;
                double disLeft, disRight;
                RoadMark *markLeft;
                RoadMark *markRight;
                if (i < 0)
                {
                    getLaneRoadPoints(s, i, pointLeft, pointRight, disLeft, disRight);
                    markLeft = ls->getLaneRoadMark(s, i + 1);
                    markRight = ls->getLaneRoadMark(s, i);
                }
                else
                {
                    getLaneRoadPoints(s, i, pointRight, pointLeft, disRight, disLeft);
                    markLeft = ls->getLaneRoadMark(s, i);
                    markRight = ls->getLaneRoadMark(s, i - 1);
                }
                double width = fabs(disLeft - disRight);
                //std::cerr << "s: " << s << ", i: " << "Outer mark width: " << markOut->getWidth() << ", inner mark width: " << markIn->getWidth() << std::endl;
                //std::cerr << "s: " << s << ", i: " << i << ", width: " << width << std::endl;
                //std::cout << "s: " << s << ", i: " << i << ", left Point: x: " << pointLeft.x() << ", y: " << pointLeft.y() << ", z: " << pointLeft.z() << std::endl;
                //std::cerr << ", outer Point: x: " << pointOut.x() << ", y: " << pointOut.y() << ", z: " << pointOut.z() << std::endl;
                //std::cout << "s: " << s << ", i: " << i << ", inner Point: x: " << pointIn.nx() << ", y: " << pointIn.ny() << ", z: " << pointIn.nz();
                //std::cout << ", outer Point: x: " << pointOut.nx() << ", y: " << pointOut.ny() << ", z: " << pointOut.nz() << std::endl;

                roadVertices->push_back(osg::Vec3(pointLeft.x(), pointLeft.y(), pointLeft.z()));
                roadNormals->push_back(osg::Vec3(pointLeft.nx(), pointLeft.ny(), pointLeft.nz()));
                roadTexCoords->push_back(osg::Vec2(s / texlength, 0.5 + disLeft / texwidth));
                roadMarks->push_back(osg::Vec4(-markLeft->getWidth() / width, -markRight->getWidth() / width, -markLeft->getType(), -markRight->getType()));

                roadVertices->push_back(osg::Vec3(pointRight.x(), pointRight.y(), pointRight.z()));
                roadNormals->push_back(osg::Vec3(pointRight.nx(), pointRight.ny(), pointRight.nz()));
                roadTexCoords->push_back(osg::Vec2(s / texlength, 0.5 + disRight / texwidth));
                roadMarks->push_back(osg::Vec4(markLeft->getWidth() / width, markRight->getWidth() / width, markLeft->getType(), markRight->getType()));

                numVertsLane += 2;

                if (lastRound)
                    s = lsEnd + h;
            }
            roadBase = new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP, firstVertLane, numVertsLane);
            roadGeometry->addPrimitiveSet(roadBase);
            firstVertLane += numVertsLane;
            numVertsLane = 0;
        }
    }

    //osg::StateSet* roadStateSet = roadGeode->getOrCreateStateSet();
    //osg::PolygonMode* polygonMode = new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE);
    //roadStateSet->setAttributeAndModes(polygonMode);

    roadGeode->setName(this->id);
    return roadGeode;
}