コード例 #1
0
void Scene::createVertexNode()
{
    _vgeode = new osg::Geode;
    _vgeode->setDataVariance(osg::Object::DYNAMIC);

    osg::Vec3f lightDir(1.0f, 1.0f, 1.0f);
    lightDir.normalize();

    auto offsets = new osg::Uniform(osg::Uniform::FLOAT_VEC3, "offsets", Scene::VertexInstances);
    offsets->setDataVariance(osg::Object::DYNAMIC);

    auto stateSet = _vgeode->getOrCreateStateSet();
    stateSet->setAttributeAndModes(_instancedProgram, osg::StateAttribute::ON);
    stateSet->addUniform(new osg::Uniform("ecLightDirection", lightDir));
    stateSet->addUniform(new osg::Uniform("lightColor", osg::Vec3(1.0f, 1.0f, 1.0f)));
    stateSet->addUniform(offsets);

    osg::Vec3 scale(1.0f, 1.0f, 1.0f);
    osg::Vec4 color(0.25f, 0.25f, 0.25f, 1.0f);

    auto vertexMatrix = osg::Matrixf::scale(scale * 0.125f * 0.25f);
    auto normalMatrix = inverseTranspose(vertexMatrix);

    _vgeometry = new osgKaleido::PolyhedronGeometry("#27");
    _vgeometry->setUseDisplayList(false);
    _vgeometry->setUseVertexBufferObjects(true);

    _vgeometry->update(nullptr); // Force geometry generation

    auto vertices = dynamic_cast<osg::Vec3Array*>(_vgeometry->getVertexArray());
    if (vertices != nullptr) {
        transform(*vertices, vertexMatrix);
    }

    auto normals = dynamic_cast<osg::Vec3Array*>(_vgeometry->getNormalArray());
    if (normals != nullptr) {
        transform(*normals, normalMatrix);
    }

    auto colors = dynamic_cast<osg::Vec4Array*>(_vgeometry->getColorArray());
    if (colors != nullptr) {
        fill(*colors, color);
    }

    makeInstanced(_vgeometry, Scene::VertexInstances);

    auto size = 1.0f;
    osg::BoundingBox bb(-size, -size, -size, +size, +size, +size);
    _vgeometry->setInitialBound(bb);

    _vgeode->addDrawable(_vgeometry);

    addChild(_vgeode);
}