• OSG绘制金字塔geode+动态纹理坐标


    osg::Node* createPyramidModel()
    {
        // create the root node which will hold the model.
        osg::Group* root = new osg::Group();
    
        // turn off lighting
        root->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
    
        osg::Geode* pyramidGeode = new osg::Geode();
        osg::Geometry* pyramidGeometry = new osg::Geometry();
        pyramidGeode->setUpdateCallback(new TextureCoordUpdateCallback(0.01));
        pyramidGeode->setDataVariance(osg::Object::DYNAMIC);
        pyramidGeode->addDrawable(pyramidGeometry);
        root->addChild(pyramidGeode);
    
        osg::Vec3Array* pyramidVertices = new osg::Vec3Array;
        pyramidVertices->push_back(osg::Vec3(0, 0, 0)); // 左前 
        pyramidVertices->push_back(osg::Vec3(10, 0, 0)); // 右前 
        pyramidVertices->push_back(osg::Vec3(10, 10, 0)); // 右后 
        pyramidVertices->push_back(osg::Vec3(0, 10, 0)); // 左后 
        pyramidVertices->push_back(osg::Vec3(5, 5, 10)); // 塔尖
        pyramidGeometry->setVertexArray(pyramidVertices);
        osg::DrawElementsUInt* pyramidBase = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
        pyramidBase->push_back(3);
        pyramidBase->push_back(2);
        pyramidBase->push_back(1);
        pyramidBase->push_back(0);
        pyramidGeometry->addPrimitiveSet(pyramidBase);
        osg::DrawElementsUInt* pyramidFaceOne = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
        pyramidFaceOne->push_back(0);
        pyramidFaceOne->push_back(1);
        pyramidFaceOne->push_back(4);
        pyramidGeometry->addPrimitiveSet(pyramidFaceOne);
        osg::DrawElementsUInt* pyramidFaceTwo = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
        pyramidFaceTwo->push_back(1);
        pyramidFaceTwo->push_back(2);
        pyramidFaceTwo->push_back(4);
        pyramidGeometry->addPrimitiveSet(pyramidFaceTwo);
        osg::DrawElementsUInt* pyramidFaceThree = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
        pyramidFaceThree->push_back(2);
        pyramidFaceThree->push_back(3);
        pyramidFaceThree->push_back(4);
        pyramidGeometry->addPrimitiveSet(pyramidFaceThree);
        osg::DrawElementsUInt* pyramidFaceFour = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
        pyramidFaceFour->push_back(3);
        pyramidFaceFour->push_back(0);
        pyramidFaceFour->push_back(4);
        pyramidGeometry->addPrimitiveSet(pyramidFaceFour);
    
        osg::Vec4Array* colors = new osg::Vec4Array;
        colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f)); //红色    
        colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f)); //绿色    
        colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f)); //蓝色    
        colors->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f)); //白色
        pyramidGeometry->setColorArray(colors);
        pyramidGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
    
        osg::Vec3Array* normals = new osg::Vec3Array(1);
        (*normals)[0].set(0.0f, -1.0f, 0.0f);
        pyramidGeometry->setNormalArray(normals, osg::Array::BIND_OVERALL);
    
        osg::Vec2Array* texcoords = new osg::Vec2Array(5);
        (*texcoords)[0].set(0.00f, 0.0f);
        (*texcoords)[1].set(0.25f, 0.0f);
        (*texcoords)[2].set(0.50f, 0.0f);
        (*texcoords)[3].set(0.75f, 0.0f);
        (*texcoords)[4].set(0.50f, 1.0f);
        pyramidGeometry->setTexCoordArray(0, texcoords);
    
        // set up the texture state.
        osg::Texture2D* texture = new osg::Texture2D;
        texture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state.
        texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);
        texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);
        texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
        texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
        texture->setImage(osgDB::readImageFile("Images/road.png"));
        osg::StateSet* stateset = pyramidGeometry->getOrCreateStateSet();
        stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
    
        return root;
    }
    class TextureCoordUpdateCallback : public osg::NodeCallback
    {
    public:
    
        TextureCoordUpdateCallback(double delay = 1.0) :
            _delay(delay),
            _prevTime(0.0)
        {
        }
    
        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
        {
            if (nv->getFrameStamp())
            {
                double currTime = nv->getFrameStamp()->getSimulationTime();
                if (currTime - _prevTime > _delay)
                {
                    osg::Geode* geode = node->asGeode();
                    osg::Geometry* geom = geode->getDrawable(0)->asGeometry();
                    // 获取纹理坐标数组
                    osg::Array* tmp = geom->getTexCoordArray(0);
                    osg::Vec2Array* coorArray = (osg::Vec2Array*) tmp;
                    auto it = coorArray->begin();
                    for (; it < coorArray->end(); it++)
                    {
                        // 动起来
                        it->x() += 0.001;
                    }
                    // 更新纹理坐标数组
                    geom->setTexCoordArray(0, coorArray);
    
                    // record time
                    _prevTime = currTime;
                }
            }
        }
    
    
    protected:
        double                          _delay;
        double                          _prevTime;
    
    };
  • 相关阅读:
    Docker+Nginx 部署Vue+Element前端项目
    MySql取字段逗号分隔的第一个
    Mysql数据库中从表字段中存储了主表以逗号隔开的主键ID,现需求查询从表数据以及以逗号隔开的字段改为主表名称隔开(group_count() find_in_set())
    IIS 错误500.21解决方法
    IIS 出现500.19 错误处理解决方式
    Linux中vim的三种模式
    Centos7 .Net Core 使用Supervisor守护进程,进行后台运行
    Nginx反代理ASP.NET Core项目
    C# vb .net实现圆角矩形特效滤镜
    C# vb .net实现旋转特效滤镜
  • 原文地址:https://www.cnblogs.com/coolbear/p/7490232.html
Copyright © 2020-2023  润新知