[osg-users] Visualizing triangle data

Sam Brkopac brkopac at gmail.com
Thu Jun 14 00:39:16 PDT 2018


Hi Robert,

I have the mesh setup correctly using the DrawElementsUShort. My question was regarding drawing the walkable triangles that are attached to the mesh. These don't have indices and just consist of the vertex information. Here was my approach to draw them.


Code:

for (size_t index = 0; index < sno.mystery_section_count(); index++)
{
    const auto * mystery_section = sno.mystery()->at(index);

    const auto triangleSectionCount = mystery_section->triangle_section_count();

    osg::ref_ptr<osg::Geode> flags = new osg::Geode;
    grouping->addChild(flags);

    const osg::Vec4 non(1.0f, 0.0f, 0.0f, 0.5f); // red for non-walkable
    const osg::Vec4 floor(0.0f, 1.0f, 0.0f, 0.5f); // green for walkable
    const osg::Vec4 water(0.0f, 0.0f, 1.0f, 0.5f); // blue for water

    osg::ref_ptr<osg::Vec4Array> color(new osg::Vec4Array(1));

    switch (mystery_section->floor())
    {
    case sno_t::floor_t::FLOOR_IGNORED:
        (*color)[0] = non;
        break;
    case sno_t::floor_t::FLOOR_FLOOR:
        (*color)[0] = floor;
        break;
    case sno_t::floor_t::FLOOR_WATER:
        (*color)[0] = water;
        break;
    }

    for (size_t j = 0; j < triangleSectionCount; j++)
    {
        const auto * triangle_section = mystery_section->triangle_section()->at(j);

        const auto * triangleA = triangle_section->triangle()->a();
        const auto * triangleB = triangle_section->triangle()->b();
        const auto * triangleC = triangle_section->triangle()->c();

        // setup the vertices based on the triangles
        osg::ref_ptr<osg::Vec3Array> vertices(new osg::Vec3Array(3));
        (*vertices)[0] = osg::Vec3(triangleA->x(), triangleA->y(), triangleA->z());
        (*vertices)[1] = osg::Vec3(triangleB->x(), triangleB->y(), triangleB->z());
        (*vertices)[2] = osg::Vec3(triangleC->x(), triangleC->y(), triangleC->z());

        // setup our geometry 
        osg::ref_ptr<osg::Geometry> geometry(new osg::Geometry);
        geometry->setVertexArray(vertices);
        geometry->setColorArray(color);
        geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
        geometry->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLES, 0, 3));

        osg::ref_ptr<osg::Geode> geode(new osg::Geode);
        geode->addDrawable(geometry);
        geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
        geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
        geode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);

        // prevent z-fighting.
        osg::ref_ptr<osg::PolygonOffset> polyOffset = new osg::PolygonOffset;
        polyOffset->setFactor(-1.0f);
        polyOffset->setUnits(-1.0f);

        osg::ref_ptr<osg::PolygonMode> polyMode = new osg::PolygonMode;
        polyMode->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL);
        geode->getOrCreateStateSet()->setAttributeAndModes(polyOffset, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
        geode->getOrCreateStateSet()->setAttributeAndModes(polyMode, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);

        flags->addChild(geode);
    }
}




If you have any feedback on the methodology, I'd love to hear it.

Thanks,
Sam[/code]

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=74070#74070







More information about the osg-users mailing list