osg::Geometry inconsistant behavior

classic Classic list List threaded Threaded
2 messages Options
Blanky Blanky
Reply | Threaded
Open this post in threaded view
|

osg::Geometry inconsistant behavior

This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Blanky Blanky
Reply | Threaded
Open this post in threaded view
|

Re: osg::Geometry inconsistant behavior

void MainWindow::generateDisjointedRect(){
    _rectArr = new osg::Vec3Array;
    _colorRef = new osg::Vec4Array;
    _geometry = new osg::Geometry;

    _geometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OVERRIDE | osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
    _geometry->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);

    //Shouldn't have to touch these
    //_geometry->setUseVertexArrayObject(false);
    //_geometry->setUseVertexBufferObjects(false);

    _geometry->setDataVariance(osg::Object::DataVariance::DYNAMIC);
    _rectArr->setDataVariance(osg::Object::DataVariance::DYNAMIC);
    _colorRef->setDataVariance(osg::Object::DataVariance::DYNAMIC);

    srand48(time(0));

    for(int i = 0; i < 2000; ++i){
        double lat = drand48() * 20 + 30;
        double lon = drand48() * 50 - 125;
        double alt = drand48() * 1000;
        int points = 5;

        _rectArr->push_back(MapCoordUtils::toMercator(osg::Vec3d(lon, lat, alt)));
        _rectArr->push_back(MapCoordUtils::toMercator(osg::Vec3d(lon, lat + 1.0, alt)));
        _rectArr->push_back(MapCoordUtils::toMercator(osg::Vec3d(lon + 1.0, lat + 1.0, alt)));
        _rectArr->push_back(MapCoordUtils::toMercator(osg::Vec3d(lon + 1.0, lat, alt)));
        _rectArr->push_back(MapCoordUtils::toMercator(osg::Vec3d(lon, lat, alt)));

        _colorRef->push_back(MapCoordUtils::QColorToVec4(QColor(drand48() * 255.0, drand48() * 255.0, drand48() * 255.0, 128.0)));
        osg::ref_ptr<osg::DrawArrays> drawArray = new osg::DrawArrays(osg::PrimitiveSet::POLYGON, _rectArr->size() - points, _rectArr->size() - (_rectArr->size() - points));
        _geometry->addPrimitiveSet(drawArray);

    }

    _geometry->setColorArray(_colorRef, osg::Array::BIND_PER_PRIMITIVE_SET);
    _geometry->setVertexArray(_rectArr);


    osg::ref_ptr<osgEarth::DepthOffsetGroup> depthGroup = new osgEarth::DepthOffsetGroup;

    osg::ref_ptr<osgEarth::DrapeableNode> drapeableNode = new osgEarth::DrapeableNode;
    drapeableNode->setMapNode(driver->get2DMapNode());
    drapeableNode->addChild(_geometry);

    depthGroup->addChild(drapeableNode);

    get2DMapNode()->addChild(depthGroup);
}

...

QTimer emitting frequent calls to this function after the above setup:

void MainWindow::updatePointZones(){
    for(int i = 0; i < _rectArr->size() - 5; i += 5){
        double lat = drand48() * 20 + 30;
        double lon = drand48() * 50 - 125;
        double alt = drand48() * 1000000;

        //Assume the toMercator function acts as our GeoTransform equivalent for projected maps
        _rectArr->at(i)       = MapCoordUtils::toMercator(osg::Vec3d(lon, lat, alt));
        _rectArr->at(i + 1) = MapCoordUtils::toMercator(osg::Vec3d(lon, lat + 1.0, alt));
        _rectArr->at(i + 2) = MapCoordUtils::toMercator(osg::Vec3d(lon + 1.0, lat + 1.0, alt));
        _rectArr->at(i + 3) = MapCoordUtils::toMercator(osg::Vec3d(lon + 1.0, lat, alt));
        _rectArr->at(i + 4) = MapCoordUtils::toMercator(osg::Vec3d(lon, lat, alt));
    }

    for(int i = 0; i < _colorRef->size(); ++i){
        _colorRef->at(i) = MapCoordUtils::QColorToVec4(QColor(drand48() * 255.0, drand48() * 255.0, drand48() * 255.0, 128.0));
    }

}

And for the analogous functionality for the lines:

PathNode::PathNode(IPathObject *pathObject, osgEarth::MapNode* mapNode) : osgEarth::DrapeableNode(){
    QVector<ILineObject *> lines = pathObject->getLines();

    m_linePointsArray = new osg::Vec3Array;
    m_glPointsArray   = new osg::Vec3Array;

    m_lineColorsArray     = new osg::Vec4Array;
    m_glPointsColorsArray = new osg::Vec4Array;

    m_lineGeom     = new osg::Geometry;
    m_glPointsGeom = new osg::Geometry;

    m_lineStripGeode = new osg::Geode;
    m_glPointsGeode  = new osg::Geode;

    for(int i = 0; i < lines.size(); ++i){
        ILineObject* line = lines[i];
        MapInterfaceTypes::PositionType startPoint = line->getStartPoint();
        MapInterfaceTypes::PositionType endPoint = line->getEndPoint();

        //Line setup
        osg::ref_ptr<osg::Vec3Array> curLine = createLinesCoordArray(startPoint, endPoint);
        m_linePointsArray->push_back(curLine->at(0));
        m_linePointsArray->push_back(curLine->at(1));

        //We push back twice otherwise we'll have half of the vertices
        m_lineColorsArray->push_back(MapCoordUtils::QColorToVec4(line->getColor()));
        m_lineColorsArray->push_back(MapCoordUtils::QColorToVec4(line->getColor()));

        //Point setup
        if(i == 0){
            m_glPointsArray->push_back(generateGLPointVec3d(startPoint));
            m_glPointsColorsArray->push_back(MapCoordUtils::QColorToVec4(line->getColor()));
        }

        m_glPointsArray->push_back(generateGLPointVec3d(endPoint));
        m_glPointsColorsArray->push_back(MapCoordUtils::QColorToVec4(line->getColor()));

    }



    //Line Geometry
    m_lineGeom->setVertexArray(m_linePointsArray);
    osg::ref_ptr<osg::DrawArrays> lineDrawArray = new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, 0, m_linePointsArray->size());
    m_lineGeom->addPrimitiveSet(lineDrawArray);
    m_lineGeom->getOrCreateStateSet()->setAttributeAndModes(new osg::LineWidth(2), osg::StateAttribute::ON);

    m_lineGeom->setColorArray(m_lineColorsArray, osg::Array::BIND_PER_VERTEX);

    //Line Geode
    m_lineStripGeode->addDrawable(m_lineGeom);
    m_lineStripGeode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OVERRIDE | osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
    m_lineStripGeode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
    m_lineStripGeode->getOrCreateStateSet()->setMode(GL_LINE_SMOOTH, osg::StateAttribute::ON);

    //GL Point Geometry
    m_glPointsGeom->setColorArray(m_glPointsColorsArray, osg::Array::BIND_OVERALL);
    m_glPointsGeom->setVertexArray(m_glPointsArray);

    osg::ref_ptr<osg::DrawArrays> glPointsDrawArray = new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, m_glPointsArray->size());
    m_glPointsGeom->addPrimitiveSet(glPointsDrawArray);
    m_glPointsGeom->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OVERRIDE | osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
    m_glPointsGeom->getOrCreateStateSet()->setRenderingHint(osg::StateSet::RenderingHint::TRANSPARENT_BIN);
    osg::ref_ptr<osg::Point> pointAttributes = new osg::Point(10);
    m_glPointsGeom->getOrCreateStateSet()->setAttributeAndModes(pointAttributes, osg::StateAttribute::ON);

    m_glPointsGeom->setColorArray(m_glPointsColorsArray, osg::Array::BIND_PER_VERTEX);

    //GL Point Geode
    m_glPointsGeode->addDrawable(m_glPointsGeom);

    setMapNode(CommonMapNode::getMapNode());

    addChild(m_lineStripGeode);
    addChild(m_glPointsGeode);
}

And later when I update the vertices/colors the code is as simple as this:


//Sets the color array index corresponding to a LineObject whose setColor was called that's a part of this
void PathNode::setLineColor(int index, QColor color){
    //Needs to be index*2 because when we originally pushed back, it was 2 per line
    if(index * 2 + 1 < (int)m_lineColorsArray->size()){
        m_lineColorsArray->at(index * 2) = MapCoordUtils::QColorToVec4(color);
        m_lineColorsArray->at(index * 2 + 1) = MapCoordUtils::QColorToVec4(color);
    }
}

//Sets the color array index corresponding to a GLPoint
void PathNode::setVertexColor(int index, QColor color){
    //Needs to be index because when we originally pushed back, it was 1 per line
    if(index < (int)m_glPointsColorsArray->size()){
        m_glPointsColorsArray->at(index) = MapCoordUtils::QColorToVec4(color);
    }
}

void PathNode::setLineIndexPosition(int index, QPair<MapInterfaceTypes::PositionType, MapInterfaceTypes::PositionType> position){
    if(index * 2 + 1 < (int)m_linePointsArray->size() && index < (int)m_glPointsArray->size()){
        osg::ref_ptr<osg::Vec3Array> curLine = createLinesCoordArray(position.first, position.second);

        m_linePointsArray->at(index * 2) = curLine->at(0);
        m_linePointsArray->at(index * 2 + 1) = curLine->at(1);

        m_glPointsArray->at(index) = curLine->at(0);
        m_glPointsArray->at(index + 1) = curLine->at(1);
    }
}

And the lines just WORK. I don't get it. This is all the code. They have the same parental structure. The same settings. The same earth file. I rather they not update at all and at least be consistent with the behavior most people would expect so I can handle it normally.