利用osg 实现机械手运动阴影效果

 1,运动前 添加阴影,运动后 等待几秒钟 删除阴影

2,

void RobotWidgget::drawOneShadow()
{
    if(!pRobot)
        return;

    //缓存的node模型节点
    int index = 0;

    osg::ref_ptr<osg::Group> group = new osg::Group;
    // 记录上一个转换节点
    osg::Matrix lastMat = (*pRobot->getRobotJoints().begin())->getMatrix();
    for (auto pJointMatrixform : pRobot->getRobotJoints()) {
        osg::ref_ptr<osg::Node> copyNode = nullptr;
        if (index < pRobot->getRobotJoints().size()) {
            copyNode = pRobot->getRobotJoints()[index]->getModel();
        }

        // 获取世界坐标系下的旋转矩阵
        osg::Matrix curMat = pJointMatrixform->getMatrix();
        // 但是我们想让这个坐标系是相对于上一个坐标系, 因此需要映射,所以左乘矩阵
        // osg矩阵相乘与矩阵理论刚好相反, 这里相当于是左乘
        if (index > 0) {
            curMat = curMat * lastMat;
            lastMat = curMat;
        }
        index++;

        // 为curMat新建一个转换节点
        osg::ref_ptr<osg::MatrixTransform> copyMatrixTransform = new osg::MatrixTransform(curMat);
        copyMatrixTransform->addChild(copyNode);
        group->addChild(copyMatrixTransform);
    }

    // 设置透明度
    osg::ref_ptr<osg::StateSet> state = group->getOrCreateStateSet();
    // 关闭灯光
    state->setMode(GL_LIGHTING,osg::StateAttribute::OFF|osg::StateAttribute::PROTECTED);
    // 打开混合融合模式
    state->setMode(GL_BLEND,osg::StateAttribute::ON);
    state->setMode(GL_DEPTH_TEST,osg::StateAttribute::ON);
    state->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
    // 使用BlendFunc实现透明效果
    osg::ref_ptr<osg::BlendColor> bc =new osg::BlendColor(osg::Vec4(1.0,1.0,1.0,0.0));
    osg::ref_ptr<osg::BlendFunc> bf = new osg::BlendFunc();
    state->setAttributeAndModes(bf,osg::StateAttribute::ON);
    state->setAttributeAndModes(bc,osg::StateAttribute::ON);
    bf->setSource(osg::BlendFunc::CONSTANT_ALPHA);
    bf->setDestination(osg::BlendFunc::ONE_MINUS_CONSTANT_ALPHA);
    bc->setConstantColor(osg::Vec4(1, 1, 1, 0.2));

    // 起个名字到后面好删除它
    group->setName("shadow");
    pRoot->addChild(group);

}

void RobotWidgget::removeShadow()
{
    FindNodeWithName findNodeName("shadow");
    for (int i = 0; i < 10; ++i) {
        pRoot->accept(findNodeName);
        pRoot->removeChild(findNodeName.getNode());
    }
}

 参考:六轴机器人仿真软件: 基于Qt平台、Osg渲染引擎开发的六轴机器人离线编程软件

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

土拨鼠不是老鼠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值